2019-08-28 12:07:00 +00:00
|
|
|
# beacon_chain
|
|
|
|
# Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
import
|
|
|
|
# Standard library
|
|
|
|
strformat,
|
|
|
|
# Specs
|
|
|
|
../../beacon_chain/spec/[datatypes, state_transition_epoch, validator, helpers],
|
|
|
|
# Internals
|
|
|
|
../../beacon_chain/[state_transition],
|
|
|
|
# Test helpers
|
|
|
|
../helpers/digest_helpers
|
|
|
|
|
|
|
|
# Justification and finalization utils
|
|
|
|
# ---------------------------------------------------------------
|
|
|
|
|
|
|
|
iterator getShardsForSlot(state: BeaconState, slot: Slot): Shard =
|
|
|
|
let
|
initial 0.9.0 spec sync (#509)
* rename compute_epoch_of_slot(...) to compute_epoch_at_slot(...)
* remove some unnecessary imports; remove some crosslink-related code and tests; complete renaming of compute_epoch_of_slot(...) to compute_epoch_at_slot(...)
* rm more transfer-related code and tests; rm more unnecessary strutils imports
* rm remaining unused imports
* remove useless get_empty_per_epoch_cache(...)/compute_start_slot_of_epoch(...) calls
* rename compute_start_slot_of_epoch(...) to compute_start_slot_at_epoch(...)
* rename ACTIVATION_EXIT_DELAY to MAX_SEED_LOOKAHEAD
* update domain types to 0.9.0
* mark AttesterSlashing, IndexedAttestation, AttestationDataAndCustodyBit, DepositData, BeaconBlockHeader, Fork, integer_squareroot(...), and process_voluntary_exit(...) as 0.9.0
* mark increase_balance(...), decrease_balance(...), get_block_root(...), CheckPoint, Deposit, PendingAttestation, HistoricalBatch, is_active_validator(...), and is_slashable_attestation_data(...) as 0.9.0
* mark compute_activation_exit_epoch(...), bls_verify(...), Validator, get_active_validator_indices(...), get_current_epoch(...), get_total_active_balance(...), and get_previous_epoch(...) as 0.9.0
* mark get_block_root_at_slot(...), ProposerSlashing, get_domain(...), VoluntaryExit, mainnet preset Gwei values, minimal preset max operations, process_block_header(...), and is_slashable_validator(...) as 0.9.0
* mark makeWithdrawalCredentials(...), get_validator_churn_limit(...), get_total_balance(...), is_valid_indexed_attestation(...), bls_aggregate_pubkeys(...), initial genesis value/constants, Attestation, get_randao_mix(...), mainnet preset max operations per block constants, minimal preset Gwei values and time parameters, process_eth1_data(...), get_shuffled_seq(...), compute_committee(...), and process_slots(...) as 0.9.0; partially update get_indexed_attestation(...) to 0.9.0 by removing crosslink refs and associated tests
* mark initiate_validator_exit(...), process_registry_updates(...), BeaconBlock, Eth1Data, compute_domain(...), process_randao(...), process_attester_slashing(...), get_base_reward(...), and process_slot(...) as 0.9.0
2019-10-30 19:41:19 +00:00
|
|
|
epoch = compute_epoch_at_slot(slot)
|
2019-08-28 12:07:00 +00:00
|
|
|
epoch_start_shard = get_start_shard(state, epoch)
|
2019-11-11 08:11:46 +00:00
|
|
|
committees_per_slot = get_committee_count_at_slot(state, slot)
|
2019-08-28 12:07:00 +00:00
|
|
|
shard = epoch_start_shard + committees_per_slot * (slot mod SLOTS_PER_EPOCH)
|
|
|
|
|
|
|
|
for i in 0 ..< committees_per_slot.int:
|
|
|
|
yield shard + Shard(i)
|
|
|
|
|
|
|
|
proc addMockAttestations*(
|
2019-09-24 14:56:12 +00:00
|
|
|
state: var 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
|
|
|
|
doAssert (state.slot + 1) mod SLOTS_PER_EPOCH == 0
|
|
|
|
|
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:
|
2019-09-24 14:56:12 +00:00
|
|
|
attestations = state.current_epoch_attestations.addr
|
2019-08-28 12:07:00 +00:00
|
|
|
elif state.get_previous_epoch() == epoch:
|
2019-09-24 14:56:12 +00:00
|
|
|
attestations = state.previous_epoch_attestations.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
|
|
|
|
var remaining_balance = state.get_total_active_balance().int64 * 2 div 3
|
|
|
|
|
initial 0.9.0 spec sync (#509)
* rename compute_epoch_of_slot(...) to compute_epoch_at_slot(...)
* remove some unnecessary imports; remove some crosslink-related code and tests; complete renaming of compute_epoch_of_slot(...) to compute_epoch_at_slot(...)
* rm more transfer-related code and tests; rm more unnecessary strutils imports
* rm remaining unused imports
* remove useless get_empty_per_epoch_cache(...)/compute_start_slot_of_epoch(...) calls
* rename compute_start_slot_of_epoch(...) to compute_start_slot_at_epoch(...)
* rename ACTIVATION_EXIT_DELAY to MAX_SEED_LOOKAHEAD
* update domain types to 0.9.0
* mark AttesterSlashing, IndexedAttestation, AttestationDataAndCustodyBit, DepositData, BeaconBlockHeader, Fork, integer_squareroot(...), and process_voluntary_exit(...) as 0.9.0
* mark increase_balance(...), decrease_balance(...), get_block_root(...), CheckPoint, Deposit, PendingAttestation, HistoricalBatch, is_active_validator(...), and is_slashable_attestation_data(...) as 0.9.0
* mark compute_activation_exit_epoch(...), bls_verify(...), Validator, get_active_validator_indices(...), get_current_epoch(...), get_total_active_balance(...), and get_previous_epoch(...) as 0.9.0
* mark get_block_root_at_slot(...), ProposerSlashing, get_domain(...), VoluntaryExit, mainnet preset Gwei values, minimal preset max operations, process_block_header(...), and is_slashable_validator(...) as 0.9.0
* mark makeWithdrawalCredentials(...), get_validator_churn_limit(...), get_total_balance(...), is_valid_indexed_attestation(...), bls_aggregate_pubkeys(...), initial genesis value/constants, Attestation, get_randao_mix(...), mainnet preset max operations per block constants, minimal preset Gwei values and time parameters, process_eth1_data(...), get_shuffled_seq(...), compute_committee(...), and process_slots(...) as 0.9.0; partially update get_indexed_attestation(...) to 0.9.0 by removing crosslink refs and associated tests
* mark initiate_validator_exit(...), process_registry_updates(...), BeaconBlock, Eth1Data, compute_domain(...), process_randao(...), process_attester_slashing(...), get_base_reward(...), and process_slot(...) as 0.9.0
2019-10-30 19:41:19 +00:00
|
|
|
let start_slot = compute_start_slot_at_epoch(epoch)
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# for-loop of distinct type is broken: https://github.com/nim-lang/Nim/issues/12074
|
|
|
|
for slot in start_slot.uint64 ..< start_slot.uint64 + SLOTS_PER_EPOCH:
|
|
|
|
for shard in getShardsForSlot(state, slot.Slot):
|
|
|
|
|
|
|
|
# TODO: can we move cache out of the loops
|
|
|
|
var cache = get_empty_per_epoch_cache()
|
|
|
|
|
|
|
|
let committee = get_crosslink_committee(
|
initial 0.9.0 spec sync (#509)
* rename compute_epoch_of_slot(...) to compute_epoch_at_slot(...)
* remove some unnecessary imports; remove some crosslink-related code and tests; complete renaming of compute_epoch_of_slot(...) to compute_epoch_at_slot(...)
* rm more transfer-related code and tests; rm more unnecessary strutils imports
* rm remaining unused imports
* remove useless get_empty_per_epoch_cache(...)/compute_start_slot_of_epoch(...) calls
* rename compute_start_slot_of_epoch(...) to compute_start_slot_at_epoch(...)
* rename ACTIVATION_EXIT_DELAY to MAX_SEED_LOOKAHEAD
* update domain types to 0.9.0
* mark AttesterSlashing, IndexedAttestation, AttestationDataAndCustodyBit, DepositData, BeaconBlockHeader, Fork, integer_squareroot(...), and process_voluntary_exit(...) as 0.9.0
* mark increase_balance(...), decrease_balance(...), get_block_root(...), CheckPoint, Deposit, PendingAttestation, HistoricalBatch, is_active_validator(...), and is_slashable_attestation_data(...) as 0.9.0
* mark compute_activation_exit_epoch(...), bls_verify(...), Validator, get_active_validator_indices(...), get_current_epoch(...), get_total_active_balance(...), and get_previous_epoch(...) as 0.9.0
* mark get_block_root_at_slot(...), ProposerSlashing, get_domain(...), VoluntaryExit, mainnet preset Gwei values, minimal preset max operations, process_block_header(...), and is_slashable_validator(...) as 0.9.0
* mark makeWithdrawalCredentials(...), get_validator_churn_limit(...), get_total_balance(...), is_valid_indexed_attestation(...), bls_aggregate_pubkeys(...), initial genesis value/constants, Attestation, get_randao_mix(...), mainnet preset max operations per block constants, minimal preset Gwei values and time parameters, process_eth1_data(...), get_shuffled_seq(...), compute_committee(...), and process_slots(...) as 0.9.0; partially update get_indexed_attestation(...) to 0.9.0 by removing crosslink refs and associated tests
* mark initiate_validator_exit(...), process_registry_updates(...), BeaconBlock, Eth1Data, compute_domain(...), process_randao(...), process_attester_slashing(...), get_base_reward(...), and process_slot(...) as 0.9.0
2019-10-30 19:41:19 +00:00
|
|
|
state, slot.Slot.compute_epoch_at_slot(),
|
2019-08-28 12:07:00 +00:00
|
|
|
shard, cache
|
|
|
|
)
|
|
|
|
|
|
|
|
# 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
|
|
|
|
remaining_balance -= state.validators[v].effective_balance.int64
|
|
|
|
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-11-08 00:12:24 +00:00
|
|
|
let (ad_slot, ad_index) = get_slot_and_index(state, epoch, shard)
|
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-08 00:12:24 +00:00
|
|
|
slot: ad_slot,
|
|
|
|
index: ad_index,
|
2019-08-28 12:07:00 +00:00
|
|
|
beacon_block_root: [byte 0xFF] * 32, # Irrelevant for testing
|
|
|
|
source: source,
|
|
|
|
target: target,
|
|
|
|
),
|
|
|
|
inclusion_delay: 1
|
|
|
|
)
|
|
|
|
|
|
|
|
proc getCheckpoints*(epoch: Epoch): tuple[c1, c2, c3, c4, c5: Checkpoint] =
|
|
|
|
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)
|
|
|
|
|
|
|
|
proc putCheckpointsInBlockRoots*(
|
|
|
|
state: var BeaconState,
|
|
|
|
checkpoints: openarray[Checkpoint]) =
|
|
|
|
for c in checkpoints:
|
initial 0.9.0 spec sync (#509)
* rename compute_epoch_of_slot(...) to compute_epoch_at_slot(...)
* remove some unnecessary imports; remove some crosslink-related code and tests; complete renaming of compute_epoch_of_slot(...) to compute_epoch_at_slot(...)
* rm more transfer-related code and tests; rm more unnecessary strutils imports
* rm remaining unused imports
* remove useless get_empty_per_epoch_cache(...)/compute_start_slot_of_epoch(...) calls
* rename compute_start_slot_of_epoch(...) to compute_start_slot_at_epoch(...)
* rename ACTIVATION_EXIT_DELAY to MAX_SEED_LOOKAHEAD
* update domain types to 0.9.0
* mark AttesterSlashing, IndexedAttestation, AttestationDataAndCustodyBit, DepositData, BeaconBlockHeader, Fork, integer_squareroot(...), and process_voluntary_exit(...) as 0.9.0
* mark increase_balance(...), decrease_balance(...), get_block_root(...), CheckPoint, Deposit, PendingAttestation, HistoricalBatch, is_active_validator(...), and is_slashable_attestation_data(...) as 0.9.0
* mark compute_activation_exit_epoch(...), bls_verify(...), Validator, get_active_validator_indices(...), get_current_epoch(...), get_total_active_balance(...), and get_previous_epoch(...) as 0.9.0
* mark get_block_root_at_slot(...), ProposerSlashing, get_domain(...), VoluntaryExit, mainnet preset Gwei values, minimal preset max operations, process_block_header(...), and is_slashable_validator(...) as 0.9.0
* mark makeWithdrawalCredentials(...), get_validator_churn_limit(...), get_total_balance(...), is_valid_indexed_attestation(...), bls_aggregate_pubkeys(...), initial genesis value/constants, Attestation, get_randao_mix(...), mainnet preset max operations per block constants, minimal preset Gwei values and time parameters, process_eth1_data(...), get_shuffled_seq(...), compute_committee(...), and process_slots(...) as 0.9.0; partially update get_indexed_attestation(...) to 0.9.0 by removing crosslink refs and associated tests
* mark initiate_validator_exit(...), process_registry_updates(...), BeaconBlock, Eth1Data, compute_domain(...), process_randao(...), process_attester_slashing(...), get_base_reward(...), and process_slot(...) as 0.9.0
2019-10-30 19:41:19 +00:00
|
|
|
let idx = c.epoch.compute_start_slot_at_epoch() mod SLOTS_PER_HISTORICAL_ROOT
|
2019-08-28 12:07:00 +00:00
|
|
|
state.block_roots[idx] = c.root
|