2019-08-28 12:07:00 +00:00
|
|
|
# beacon_chain
|
2022-06-07 17:05:06 +00:00
|
|
|
# Copyright (c) 2018-2022 Status Research & Development GmbH
|
2019-08-28 12:07:00 +00:00
|
|
|
# Licensed and distributed under either of
|
2019-11-25 15:30:02 +00:00
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
2019-08-28 12:07:00 +00:00
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
import
|
|
|
|
# Standard library
|
2022-06-07 17:05:06 +00:00
|
|
|
std/strformat,
|
2019-08-28 12:07:00 +00:00
|
|
|
# Specs
|
2021-08-12 13:08:20 +00:00
|
|
|
../../beacon_chain/spec/datatypes/phase0,
|
|
|
|
../../beacon_chain/spec/[beaconstate, validator, helpers],
|
2019-08-28 12:07:00 +00:00
|
|
|
# Test helpers
|
|
|
|
../helpers/digest_helpers
|
|
|
|
|
|
|
|
# Justification and finalization utils
|
|
|
|
# ---------------------------------------------------------------
|
|
|
|
|
2021-08-25 14:51:52 +00:00
|
|
|
func addMockAttestations*(
|
2021-08-12 13:08:20 +00:00
|
|
|
state: var phase0.BeaconState, epoch: Epoch,
|
2019-08-28 12:07:00 +00:00
|
|
|
source, target: Checkpoint,
|
|
|
|
sufficient_support = false
|
|
|
|
) =
|
|
|
|
# We must be at the end of the epoch
|
2022-01-11 10:01:54 +00:00
|
|
|
doAssert (state.slot + 1).is_epoch
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2019-09-24 14:56:12 +00:00
|
|
|
# Alias the attestations container
|
|
|
|
var attestations: ptr seq[PendingAttestation]
|
2019-08-28 12:07:00 +00:00
|
|
|
if state.get_current_epoch() == epoch:
|
2020-05-18 17:49:22 +00:00
|
|
|
attestations = state.current_epoch_attestations.asSeq.addr
|
2019-08-28 12:07:00 +00:00
|
|
|
elif state.get_previous_epoch() == epoch:
|
2020-05-18 17:49:22 +00:00
|
|
|
attestations = state.previous_epoch_attestations.asSeq.addr
|
2019-08-28 12:07:00 +00:00
|
|
|
else:
|
|
|
|
raise newException(ValueError, &"Cannot include attestations from epoch {state.get_current_epoch()} in epoch {epoch}")
|
|
|
|
|
|
|
|
# TODO: Working with an unsigned Gwei balance is a recipe for underflows to happen
|
2020-07-15 10:44:18 +00:00
|
|
|
var cache = StateCache()
|
2020-06-03 05:42:08 +00:00
|
|
|
var remaining_balance = state.get_total_active_balance(cache).int64 * 2 div 3
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2020-07-23 17:01:07 +00:00
|
|
|
let
|
|
|
|
committees_per_slot = get_committee_count_per_slot(state, epoch, cache)
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2022-01-11 10:01:54 +00:00
|
|
|
for slot in epoch.slots():
|
2022-01-08 23:28:49 +00:00
|
|
|
for committee_index in get_committee_indices(committees_per_slot):
|
|
|
|
let committee = get_beacon_committee(state, slot, committee_index, cache)
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# Create a bitfield filled with the given count per attestation,
|
|
|
|
# exactly on the right-most part of the committee field.
|
|
|
|
var aggregation_bits = init(CommitteeValidatorsBits, committee.len)
|
|
|
|
for v in 0 ..< committee.len * 2 div 3 + 1:
|
|
|
|
if remaining_balance > 0:
|
|
|
|
# Beware of the underflows, use int
|
2022-05-30 13:30:42 +00:00
|
|
|
remaining_balance -= state.validators.item(v).effective_balance.int64
|
2019-08-28 12:07:00 +00:00
|
|
|
aggregation_bits[v] = true
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
|
|
|
# Remove just one attester to make the marginal support insufficient
|
|
|
|
if not sufficient_support:
|
|
|
|
# Find the first attester if any
|
|
|
|
let idx = aggregation_bits.find(true)
|
|
|
|
if idx != -1:
|
|
|
|
aggregation_bits[idx] = false
|
|
|
|
|
2019-09-24 14:56:12 +00:00
|
|
|
attestations[].add PendingAttestation(
|
2019-08-28 12:07:00 +00:00
|
|
|
aggregation_bits: aggregation_bits,
|
|
|
|
data: AttestationData(
|
2019-11-12 05:35:52 +00:00
|
|
|
slot: slot.Slot,
|
2022-01-08 23:28:49 +00:00
|
|
|
index: committee_index.uint64,
|
2019-08-28 12:07:00 +00:00
|
|
|
beacon_block_root: [byte 0xFF] * 32, # Irrelevant for testing
|
|
|
|
source: source,
|
|
|
|
target: target,
|
|
|
|
),
|
|
|
|
inclusion_delay: 1
|
|
|
|
)
|
|
|
|
|
2021-08-25 14:51:52 +00:00
|
|
|
func getCheckpoints*(epoch: Epoch): tuple[c1, c2, c3, c4, c5: Checkpoint] =
|
2019-08-28 12:07:00 +00:00
|
|
|
if epoch >= 1: result.c1 = Checkpoint(epoch: epoch - 1, root: [byte 0xAA] * 32)
|
|
|
|
if epoch >= 2: result.c2 = Checkpoint(epoch: epoch - 2, root: [byte 0xBB] * 32)
|
|
|
|
if epoch >= 3: result.c3 = Checkpoint(epoch: epoch - 3, root: [byte 0xCC] * 32)
|
|
|
|
if epoch >= 4: result.c4 = Checkpoint(epoch: epoch - 4, root: [byte 0xDD] * 32)
|
|
|
|
if epoch >= 5: result.c5 = Checkpoint(epoch: epoch - 5, root: [byte 0xEE] * 32)
|
|
|
|
|
2021-08-25 14:51:52 +00:00
|
|
|
func putCheckpointsInBlockRoots*(
|
2021-08-12 13:08:20 +00:00
|
|
|
state: var phase0.BeaconState,
|
2020-10-28 18:35:31 +00:00
|
|
|
checkpoints: openArray[Checkpoint]) =
|
2019-08-28 12:07:00 +00:00
|
|
|
for c in checkpoints:
|
2022-01-11 10:01:54 +00:00
|
|
|
let idx = c.epoch.start_slot() mod SLOTS_PER_HISTORICAL_ROOT
|
2019-08-28 12:07:00 +00:00
|
|
|
state.block_roots[idx] = c.root
|