allow testing with custom cfg (#2884)

This extends some test utilities to allow simulating states that use
a custom RuntimeConfig, e.g., to test with an earlier ALTAIR_FORK_EPOCH.
This commit is contained in:
Etan Kissling 2021-09-22 19:50:10 +02:00 committed by GitHub
parent 2ec34e6761
commit 4491259c52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 24 deletions

View File

@ -45,14 +45,17 @@ proc signMockBlock*(state: ForkedHashedBeaconState, b: var ForkedSignedBeaconBlo
privkey).toValidatorSig()
# https://github.com/ethereum/consensus-specs/blob/v1.1.0-beta.4/tests/core/pyspec/eth2spec/test/helpers/block.py#L75-L105
proc mockBlock*(state: ForkedHashedBeaconState, slot: Slot): ForkedSignedBeaconBlock =
proc mockBlock*(
state: ForkedHashedBeaconState,
slot: Slot,
cfg = defaultRuntimeConfig): ForkedSignedBeaconBlock =
## TODO don't do this gradual construction, for exception safety
## Mock a BeaconBlock for the specific slot
var cache = StateCache()
var rewards = RewardInfo()
var tmpState = assignClone(state)
doAssert process_slots(defaultRuntimeConfig, tmpState[], slot, cache, rewards, flags = {})
doAssert process_slots(cfg, tmpState[], slot, cache, rewards, flags = {})
var previous_block_header = getStateField(tmpState[], latest_block_header)
if previous_block_header.state_root == ZERO_HASH:

View File

@ -17,18 +17,14 @@ import
./mock_deposits
proc initGenesisState*(
num_validators: uint64 = 8'u64 * SLOTS_PER_EPOCH,
beaconStateFork: BeaconStateFork = forkPhase0): ref ForkedHashedBeaconState =
num_validators = 8'u64 * SLOTS_PER_EPOCH,
cfg = defaultRuntimeConfig): ref ForkedHashedBeaconState =
let deposits = mockGenesisBalancedDeposits(
validatorCount = num_validators,
amountInEth = 32, # We create canonical validators with 32 Eth
flags = {}
)
var cfg = defaultRuntimeConfig
if beaconStateFork >= forkAltair:
cfg.ALTAIR_FORK_EPOCH = GENESIS_EPOCH
result = (ref ForkedHashedBeaconState)(
beaconStateFork: forkPhase0,
hbsPhase0: initialize_hashed_beacon_state_from_eth1(
@ -36,8 +32,6 @@ proc initGenesisState*(
maybeUpgradeStateToAltair(cfg, result[])
doAssert result.beaconStateFork == beaconStateFork
when isMainModule:
# Smoke test
let state = initGenesisState(num_validators = SLOTS_PER_EPOCH)

View File

@ -36,7 +36,7 @@ func hackPrivKey*(v: Validator): ValidatorPrivKey =
let i = int(uint64.fromBytesLE(bytes))
makeFakeValidatorPrivKey(i)
func makeDeposit*(i: int, flags: UpdateFlags = {}): DepositData =
func makeDeposit*(i: int, flags: UpdateFlags = {}, cfg = defaultRuntimeConfig): DepositData =
## Ugly hack for now: we stick the private key in withdrawal_credentials
## which means we can repro private key and randao reveal from this data,
## for testing :)
@ -51,13 +51,12 @@ func makeDeposit*(i: int, flags: UpdateFlags = {}): DepositData =
amount: MAX_EFFECTIVE_BALANCE)
if skipBLSValidation notin flags:
result.signature = get_deposit_signature(
defaultRuntimeConfig, result, privkey).toValidatorSig()
result.signature = get_deposit_signature(cfg, result, privkey).toValidatorSig()
func makeInitialDeposits*(
n = SLOTS_PER_EPOCH, flags: UpdateFlags = {}): seq[DepositData] =
n = SLOTS_PER_EPOCH, flags: UpdateFlags = {}, cfg = defaultRuntimeConfig): seq[DepositData] =
for i in 0..<n.int:
result.add makeDeposit(i, flags)
result.add makeDeposit(i, flags, cfg = cfg)
func signBlock(
fork: Fork, genesis_validators_root: Eth2Digest, forked: ForkedBeaconBlock,
@ -82,13 +81,13 @@ proc addTestBlock*(
deposits = newSeq[Deposit](),
graffiti = default(GraffitiBytes),
flags: set[UpdateFlag] = {},
nextSlot = true): ForkedSignedBeaconBlock =
nextSlot = true,
cfg = defaultRuntimeConfig): ForkedSignedBeaconBlock =
# Create and add a block to state - state will advance by one slot!
if nextSlot:
var rewards: RewardInfo
doAssert process_slots(
defaultRuntimeConfig, state, getStateField(state, slot) + 1, cache,
rewards, flags)
cfg, state, getStateField(state, slot) + 1, cache, rewards, flags)
let
proposer_index = get_beacon_proposer_index(
@ -107,7 +106,7 @@ proc addTestBlock*(
case state.beaconStateFork
of forkPhase0:
let res = makeBeaconBlock(
defaultRuntimeConfig,
cfg,
state.hbsPhase0,
proposer_index.get(),
parent_root,
@ -130,7 +129,7 @@ proc addTestBlock*(
ForkedBeaconBlock.init(res.get())
of forkAltair:
let res = makeBeaconBlock(
defaultRuntimeConfig,
cfg,
state.hbsAltair,
proposer_index.get(),
parent_root,
@ -169,7 +168,8 @@ proc makeTestBlock*(
eth1_data = Eth1Data(),
attestations = newSeq[Attestation](),
deposits = newSeq[Deposit](),
graffiti = default(GraffitiBytes)): ForkedSignedBeaconBlock =
graffiti = default(GraffitiBytes),
cfg = defaultRuntimeConfig): ForkedSignedBeaconBlock =
# Create a block for `state.slot + 1` - like a block proposer would do!
# It's a bit awkward - in order to produce a block for N+1, we need to
# calculate what the state will look like after that block has been applied,
@ -177,7 +177,7 @@ proc makeTestBlock*(
var tmpState = assignClone(state)
addTestBlock(
tmpState[], parent_root, cache, eth1_data, attestations, deposits,
graffiti)
graffiti, cfg = cfg)
func makeAttestationData*(
state: ForkedHashedBeaconState, slot: Slot, committee_index: CommitteeIndex,
@ -315,7 +315,8 @@ iterator makeTestBlocks*(
parent_root: Eth2Digest,
cache: var StateCache,
blocks: int,
attested: bool): ForkedSignedBeaconBlock =
attested: bool,
cfg = defaultRuntimeConfig): ForkedSignedBeaconBlock =
var
state = assignClone(state)[]
parent_root = parent_root
@ -326,7 +327,7 @@ iterator makeTestBlocks*(
@[]
let blck = addTestBlock(
state, parent_root, cache, attestations = attestations)
state, parent_root, cache, attestations = attestations, cfg = cfg)
yield blck
parent_root = blck.root