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:
parent
6826ddb45d
commit
7bbe0258d0
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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 =
|
||||
|
|
|
@ -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:
|
||||
|
|
Loading…
Reference in New Issue