Skip to content

Commit

Permalink
allow initializing test DBs from later forks (#5186)
Browse files Browse the repository at this point in the history
Running `makeTestDB` in tests currently always initializes DB with a
`phase0` state, preventing tests that configure a fork schedule that
starts in a different fork from working properly. Fix that by upgrading
the genesis state to whatever fork the fork schedule starts with.
  • Loading branch information
etan-status authored Jul 14, 2023
1 parent 703eead commit f1963bf
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions tests/testdbutil.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import
../beacon_chain/[beacon_chain_db],
../beacon_chain/consensus_object_pools/blockchain_dag,
../beacon_chain/spec/datatypes/phase0,
../beacon_chain/spec/[beaconstate, forks],
../beacon_chain/spec/[beaconstate, forks, state_transition],
eth/db/[kvstore, kvstore_sqlite3],
./testblockutil

Expand All @@ -21,22 +21,31 @@ proc makeTestDB*(
eth1Data = Opt.none(Eth1Data),
flags: UpdateFlags = {skipBlsValidation},
cfg = defaultRuntimeConfig): BeaconChainDB =
let
genState = (ref ForkedHashedBeaconState)(
kind: ConsensusFork.Phase0,
phase0Data: initialize_hashed_beacon_state_from_eth1(
cfg,
ZERO_HASH,
0,
makeInitialDeposits(validators.uint64, flags),
flags))
var genState = (ref ForkedHashedBeaconState)(
kind: ConsensusFork.Phase0,
phase0Data: initialize_hashed_beacon_state_from_eth1(
cfg,
ZERO_HASH,
0,
makeInitialDeposits(validators.uint64, flags),
flags))

# Override Eth1Data on request, skipping the lengthy Eth1 voting process
if eth1Data.isOk:
withState(genState[]):
forkyState.data.eth1_data = eth1Data.get
forkyState.root = hash_tree_root(forkyState.data)

# Upgrade genesis state to later fork, if required by fork schedule
cfg.maybeUpgradeState(genState[])
withState(genState[]):
when consensusFork > ConsensusFork.Phase0:
forkyState.data.fork.previous_version =
forkyState.data.fork.current_version
forkyState.data.latest_block_header.body_root =
hash_tree_root(default(BeaconBlockBodyType(consensusFork)))
forkyState.root = hash_tree_root(forkyState.data)

result = BeaconChainDB.new("", cfg = cfg, inMemory = true)
ChainDAGRef.preInit(result, genState[])

Expand Down

0 comments on commit f1963bf

Please sign in to comment.