block mocking support for Altair (#2867)
The existing functions for mocking blocks only support phase0 blocks. This extends block mocking support to also support Altair blocks.
This commit is contained in:
parent
ddbbbae3c8
commit
f2edf679bc
|
@ -15,7 +15,7 @@ import
|
||||||
chronicles,
|
chronicles,
|
||||||
# Specs
|
# Specs
|
||||||
../../beacon_chain/spec/datatypes/phase0,
|
../../beacon_chain/spec/datatypes/phase0,
|
||||||
../../beacon_chain/spec/[beaconstate, helpers, validator, signatures],
|
../../beacon_chain/spec/[beaconstate, forks, helpers, validator, signatures],
|
||||||
# Mocking procs
|
# Mocking procs
|
||||||
./mock_blocks,
|
./mock_blocks,
|
||||||
./mock_validator_keys
|
./mock_validator_keys
|
||||||
|
@ -27,7 +27,9 @@ proc mockAttestationData(
|
||||||
doAssert state.slot >= slot
|
doAssert state.slot >= slot
|
||||||
|
|
||||||
if slot == state.slot:
|
if slot == state.slot:
|
||||||
result.beacon_block_root = mockBlockForNextSlot(state).message.parent_root
|
let forkedState = (ref ForkedHashedBeaconState)(beaconStateFork: forkPhase0,
|
||||||
|
hbsPhase0: phase0.HashedBeaconState(root: hash_tree_root(state), data: state))[]
|
||||||
|
result.beacon_block_root = mockBlockForNextSlot(forkedState).phase0Block.message.parent_root
|
||||||
else:
|
else:
|
||||||
result.beacon_block_root = get_block_root_at_slot(state, slot)
|
result.beacon_block_root = get_block_root_at_slot(state, slot)
|
||||||
|
|
||||||
|
|
|
@ -6,54 +6,74 @@
|
||||||
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
||||||
|
|
||||||
import
|
import
|
||||||
options,
|
# Beacon chain internals
|
||||||
# Specs
|
../../beacon_chain/spec/[forks, helpers, signatures, state_transition],
|
||||||
../../beacon_chain/spec/datatypes/phase0,
|
|
||||||
../../beacon_chain/spec/[helpers, signatures, validator],
|
|
||||||
# Mock helpers
|
# Mock helpers
|
||||||
./mock_validator_keys
|
./mock_validator_keys
|
||||||
|
|
||||||
# Routines for mocking blocks
|
# Routines for mocking blocks
|
||||||
# ---------------------------------------------------------------
|
# ---------------------------------------------------------------
|
||||||
|
|
||||||
proc signMockBlockImpl(
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.0-beta.4/tests/core/pyspec/eth2spec/test/helpers/block.py#L26-L35
|
||||||
state: phase0.BeaconState,
|
proc applyRandaoReveal(state: ForkedHashedBeaconState, b: var ForkedSignedBeaconBlock) =
|
||||||
signedBlock: var phase0.SignedBeaconBlock
|
withBlck(b):
|
||||||
) =
|
doAssert getStateField(state, slot) <= blck.message.slot
|
||||||
let block_slot = signedBlock.message.slot
|
|
||||||
doAssert state.slot <= block_slot
|
|
||||||
|
|
||||||
let privkey = MockPrivKeys[signedBlock.message.proposer_index]
|
let proposer_index = blck.message.proposer_index
|
||||||
|
let privkey = MockPrivKeys[proposer_index]
|
||||||
|
|
||||||
signedBlock.message.body.randao_reveal = get_epoch_signature(
|
blck.message.body.randao_reveal =
|
||||||
state.fork, state.genesis_validators_root, block_slot.compute_epoch_at_slot,
|
get_epoch_signature(
|
||||||
privkey).toValidatorSig()
|
getStateField(state, fork),
|
||||||
signedBlock.root = hash_tree_root(signedBlock.message)
|
getStateField(state, genesis_validators_root),
|
||||||
signedBlock.signature = get_block_signature(
|
blck.message.slot.compute_epoch_at_slot,
|
||||||
state.fork, state.genesis_validators_root, block_slot,
|
privkey).toValidatorSig()
|
||||||
signedBlock.root, privkey).toValidatorSig()
|
|
||||||
|
|
||||||
proc signMockBlock*(state: phase0.BeaconState, signedBlock: var phase0.SignedBeaconBlock) =
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.0-beta.4/tests/core/pyspec/eth2spec/test/helpers/block.py#L38-L54
|
||||||
signMockBlockImpl(state, signedBlock)
|
proc signMockBlock*(state: ForkedHashedBeaconState, b: var ForkedSignedBeaconBlock) =
|
||||||
|
withBlck(b):
|
||||||
|
let proposer_index = blck.message.proposer_index
|
||||||
|
let privkey = MockPrivKeys[proposer_index]
|
||||||
|
|
||||||
proc mockBlock*(
|
blck.root = blck.message.hash_tree_root()
|
||||||
state: phase0.BeaconState,
|
blck.signature =
|
||||||
slot: Slot): phase0.SignedBeaconBlock =
|
get_block_signature(
|
||||||
|
getStateField(state, fork),
|
||||||
|
getStateField(state, genesis_validators_root),
|
||||||
|
blck.message.slot,
|
||||||
|
blck.root,
|
||||||
|
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 =
|
||||||
## TODO don't do this gradual construction, for exception safety
|
## TODO don't do this gradual construction, for exception safety
|
||||||
## Mock a BeaconBlock for the specific slot
|
## Mock a BeaconBlock for the specific slot
|
||||||
|
|
||||||
var emptyCache = StateCache()
|
var cache = StateCache()
|
||||||
let proposer_index = get_beacon_proposer_index(state, emptyCache, slot)
|
var rewards = RewardInfo()
|
||||||
result.message.slot = slot
|
var tmpState = assignClone(state)
|
||||||
result.message.proposer_index = proposer_index.get.uint64
|
doAssert process_slots(defaultRuntimeConfig, tmpState[], slot, cache, rewards, flags = {})
|
||||||
result.message.body.eth1_data.deposit_count = state.eth1_deposit_index
|
|
||||||
|
|
||||||
var previous_block_header = state.latest_block_header
|
var previous_block_header = getStateField(tmpState[], latest_block_header)
|
||||||
if previous_block_header.state_root == ZERO_HASH:
|
if previous_block_header.state_root == ZERO_HASH:
|
||||||
previous_block_header.state_root = state.hash_tree_root()
|
previous_block_header.state_root = tmpState[].hash_tree_root()
|
||||||
result.message.parent_root = previous_block_header.hash_tree_root()
|
|
||||||
|
|
||||||
signMockBlock(state, result)
|
result.kind = case tmpState[].beaconStateFork
|
||||||
|
of forkPhase0: BeaconBlockFork.Phase0
|
||||||
|
of forkAltair: BeaconBlockFork.Altair
|
||||||
|
withBlck(result):
|
||||||
|
blck.message.slot = slot
|
||||||
|
blck.message.proposer_index = get_beacon_proposer_index(tmpState[], cache, slot).get.uint64
|
||||||
|
blck.message.body.eth1_data.deposit_count = getStateField(tmpState[], eth1_deposit_index)
|
||||||
|
blck.message.parent_root = previous_block_header.hash_tree_root()
|
||||||
|
|
||||||
proc mockBlockForNextSlot*(state: phase0.BeaconState): phase0.SignedBeaconBlock =
|
applyRandaoReveal(tmpState[], result)
|
||||||
mockBlock(state, state.slot + 1)
|
|
||||||
|
if result.kind >= BeaconBlockFork.Altair:
|
||||||
|
result.altairBlock.message.body.sync_aggregate.sync_committee_signature = ValidatorSig.infinity
|
||||||
|
|
||||||
|
signMockBlock(tmpState[], result)
|
||||||
|
|
||||||
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.0-beta.4/tests/core/pyspec/eth2spec/test/helpers/block.py#L108-L109
|
||||||
|
proc mockBlockForNextSlot*(state: ForkedHashedBeaconState): ForkedSignedBeaconBlock =
|
||||||
|
mockBlock(state, getStateField(state, slot) + 1)
|
||||||
|
|
Loading…
Reference in New Issue