From 7bbe0258d06f22114f4d23dc9c2b4384f24d0f3f Mon Sep 17 00:00:00 2001 From: Etan Kissling Date: Tue, 14 Sep 2021 00:56:37 +0200 Subject: [PATCH] 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. --- tests/mocking/mock_genesis.nim | 21 ++++++++++++++----- .../test_process_attestation.nim | 3 +-- .../test_process_deposits.nim | 4 ++-- ...process_justification_and_finalization.nim | 3 +-- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/tests/mocking/mock_genesis.nim b/tests/mocking/mock_genesis.nim index de2092da7..05f88a7db 100644 --- a/tests/mocking/mock_genesis.nim +++ b/tests/mocking/mock_genesis.nim @@ -10,22 +10,33 @@ import # Specs - ../../beacon_chain/spec/datatypes/phase0, - ../../beacon_chain/spec/[beaconstate], + ../../beacon_chain/spec/[beaconstate, forks, state_transition], # Internals ../../beacon_chain/interop, # Mocking procs ./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( validatorCount = num_validators, amountInEth = 32, # We create canonical validators with 32 Eth flags = {} ) - initialize_hashed_beacon_state_from_eth1( - defaultRuntimeConfig, eth1BlockHash, 0, deposits, {}) + var cfg = defaultRuntimeConfig + 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: # Smoke test diff --git a/tests/spec_block_processing/test_process_attestation.nim b/tests/spec_block_processing/test_process_attestation.nim index a99f2b79f..287da629e 100644 --- a/tests/spec_block_processing/test_process_attestation.nim +++ b/tests/spec_block_processing/test_process_attestation.nim @@ -23,8 +23,7 @@ import suite "[Unit - Spec - Block processing] Attestations " & preset(): const NumValidators = uint64(8) * SLOTS_PER_EPOCH - let genesisState = (ref ForkedHashedBeaconState)( - hbsPhase0: initGenesisState(NumValidators), beaconStateFork: forkPhase0) + let genesisState = initGenesisState(NumValidators) doAssert getStateField(genesisState[], validators).lenu64 == NumValidators diff --git a/tests/spec_block_processing/test_process_deposits.nim b/tests/spec_block_processing/test_process_deposits.nim index ccc34057d..f5dd9d415 100644 --- a/tests/spec_block_processing/test_process_deposits.nim +++ b/tests/spec_block_processing/test_process_deposits.nim @@ -16,7 +16,7 @@ import # Standard library std/math, # Specs - ../../beacon_chain/spec/[state_transition_block], + ../../beacon_chain/spec/[forks, state_transition_block], ../../beacon_chain/spec/datatypes/base, # Internals # Mock helpers @@ -26,7 +26,7 @@ import suite "[Unit - Spec - Block processing] Deposits " & preset(): const NumValidators = uint64 5 * SLOTS_PER_EPOCH - let genesisState = newClone(initGenesisState(NumValidators)) + let genesisState = newClone(initGenesisState(NumValidators).hbsPhase0) doAssert genesisState.data.validators.lenu64 == NumValidators template valid_deposit(deposit_amount: uint64, name: string): untyped = diff --git a/tests/spec_epoch_processing/test_process_justification_and_finalization.nim b/tests/spec_epoch_processing/test_process_justification_and_finalization.nim index a0eff2a2d..99a79c3af 100644 --- a/tests/spec_epoch_processing/test_process_justification_and_finalization.nim +++ b/tests/spec_epoch_processing/test_process_justification_and_finalization.nim @@ -218,8 +218,7 @@ proc payload = echo " Finalization rules are detailed at https://github.com/protolambda/eth2-docs#justification-and-finalization" const NumValidators = uint64(8) * SLOTS_PER_EPOCH - let genesisState = (ref ForkedHashedBeaconState)( - hbsPhase0: initGenesisState(NumValidators), beaconStateFork: forkPhase0) + let genesisState = initGenesisState(NumValidators) doAssert getStateField(genesisState[], validators).lenu64 == NumValidators setup: