allow testing genesis with Altair (#2866)

The mocking framework was limited to chain configurations that do not
include Altair at genesis. This patch extends it so that genesis states
can be generated that are already upgraded to Altair. This is useful for
tests such as eth2spec/test/altair/unittests/test_sync_protocol.py.
This commit is contained in:
Etan Kissling 2021-09-14 00:56:37 +02:00 committed by GitHub
parent 6826ddb45d
commit 7bbe0258d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 11 deletions

View File

@ -10,22 +10,33 @@
import import
# Specs # Specs
../../beacon_chain/spec/datatypes/phase0, ../../beacon_chain/spec/[beaconstate, forks, state_transition],
../../beacon_chain/spec/[beaconstate],
# Internals # Internals
../../beacon_chain/interop, ../../beacon_chain/interop,
# Mocking procs # Mocking procs
./mock_deposits ./mock_deposits
proc initGenesisState*(num_validators: uint64): phase0.HashedBeaconState = proc initGenesisState*(
num_validators: uint64 = 8'u64 * SLOTS_PER_EPOCH,
beaconStateFork: BeaconStateFork = forkPhase0): ref ForkedHashedBeaconState =
let deposits = mockGenesisBalancedDeposits( let deposits = mockGenesisBalancedDeposits(
validatorCount = num_validators, validatorCount = num_validators,
amountInEth = 32, # We create canonical validators with 32 Eth amountInEth = 32, # We create canonical validators with 32 Eth
flags = {} flags = {}
) )
initialize_hashed_beacon_state_from_eth1( var cfg = defaultRuntimeConfig
defaultRuntimeConfig, eth1BlockHash, 0, deposits, {}) if beaconStateFork >= forkAltair:
cfg.ALTAIR_FORK_EPOCH = GENESIS_EPOCH
result = (ref ForkedHashedBeaconState)(
beaconStateFork: forkPhase0,
hbsPhase0: initialize_hashed_beacon_state_from_eth1(
cfg, eth1BlockHash, 0, deposits, {}))
maybeUpgradeStateToAltair(cfg, result[])
doAssert result.beaconStateFork == beaconStateFork
when isMainModule: when isMainModule:
# Smoke test # Smoke test

View File

@ -23,8 +23,7 @@ import
suite "[Unit - Spec - Block processing] Attestations " & preset(): suite "[Unit - Spec - Block processing] Attestations " & preset():
const NumValidators = uint64(8) * SLOTS_PER_EPOCH const NumValidators = uint64(8) * SLOTS_PER_EPOCH
let genesisState = (ref ForkedHashedBeaconState)( let genesisState = initGenesisState(NumValidators)
hbsPhase0: initGenesisState(NumValidators), beaconStateFork: forkPhase0)
doAssert getStateField(genesisState[], validators).lenu64 == NumValidators doAssert getStateField(genesisState[], validators).lenu64 == NumValidators

View File

@ -16,7 +16,7 @@ import
# Standard library # Standard library
std/math, std/math,
# Specs # Specs
../../beacon_chain/spec/[state_transition_block], ../../beacon_chain/spec/[forks, state_transition_block],
../../beacon_chain/spec/datatypes/base, ../../beacon_chain/spec/datatypes/base,
# Internals # Internals
# Mock helpers # Mock helpers
@ -26,7 +26,7 @@ import
suite "[Unit - Spec - Block processing] Deposits " & preset(): suite "[Unit - Spec - Block processing] Deposits " & preset():
const NumValidators = uint64 5 * SLOTS_PER_EPOCH const NumValidators = uint64 5 * SLOTS_PER_EPOCH
let genesisState = newClone(initGenesisState(NumValidators)) let genesisState = newClone(initGenesisState(NumValidators).hbsPhase0)
doAssert genesisState.data.validators.lenu64 == NumValidators doAssert genesisState.data.validators.lenu64 == NumValidators
template valid_deposit(deposit_amount: uint64, name: string): untyped = template valid_deposit(deposit_amount: uint64, name: string): untyped =

View File

@ -218,8 +218,7 @@ proc payload =
echo " Finalization rules are detailed at https://github.com/protolambda/eth2-docs#justification-and-finalization" echo " Finalization rules are detailed at https://github.com/protolambda/eth2-docs#justification-and-finalization"
const NumValidators = uint64(8) * SLOTS_PER_EPOCH const NumValidators = uint64(8) * SLOTS_PER_EPOCH
let genesisState = (ref ForkedHashedBeaconState)( let genesisState = initGenesisState(NumValidators)
hbsPhase0: initGenesisState(NumValidators), beaconStateFork: forkPhase0)
doAssert getStateField(genesisState[], validators).lenu64 == NumValidators doAssert getStateField(genesisState[], validators).lenu64 == NumValidators
setup: setup: