2019-08-28 12:07:00 +00:00
|
|
|
# beacon_chain
|
2021-01-22 13:29:04 +00:00
|
|
|
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
2019-08-28 12:07:00 +00:00
|
|
|
# Licensed and distributed under either of
|
2019-11-25 15:30:02 +00:00
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
2019-08-28 12:07:00 +00:00
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
import
|
2021-09-20 20:20:29 +00:00
|
|
|
# Beacon chain internals
|
|
|
|
../../beacon_chain/spec/[forks, helpers, signatures, state_transition],
|
2019-08-28 12:07:00 +00:00
|
|
|
# Mock helpers
|
|
|
|
./mock_validator_keys
|
|
|
|
|
|
|
|
# Routines for mocking blocks
|
|
|
|
# ---------------------------------------------------------------
|
|
|
|
|
2021-09-20 20:20:29 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.0-beta.4/tests/core/pyspec/eth2spec/test/helpers/block.py#L26-L35
|
|
|
|
proc applyRandaoReveal(state: ForkedHashedBeaconState, b: var ForkedSignedBeaconBlock) =
|
|
|
|
withBlck(b):
|
|
|
|
doAssert getStateField(state, slot) <= blck.message.slot
|
|
|
|
|
|
|
|
let proposer_index = blck.message.proposer_index
|
|
|
|
let privkey = MockPrivKeys[proposer_index]
|
|
|
|
|
|
|
|
blck.message.body.randao_reveal =
|
|
|
|
get_epoch_signature(
|
|
|
|
getStateField(state, fork),
|
|
|
|
getStateField(state, genesis_validators_root),
|
|
|
|
blck.message.slot.compute_epoch_at_slot,
|
|
|
|
privkey).toValidatorSig()
|
|
|
|
|
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.0-beta.4/tests/core/pyspec/eth2spec/test/helpers/block.py#L38-L54
|
|
|
|
proc signMockBlock*(state: ForkedHashedBeaconState, b: var ForkedSignedBeaconBlock) =
|
|
|
|
withBlck(b):
|
|
|
|
let proposer_index = blck.message.proposer_index
|
|
|
|
let privkey = MockPrivKeys[proposer_index]
|
|
|
|
|
|
|
|
blck.root = blck.message.hash_tree_root()
|
|
|
|
blck.signature =
|
|
|
|
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
|
2021-09-22 17:50:10 +00:00
|
|
|
proc mockBlock*(
|
|
|
|
state: ForkedHashedBeaconState,
|
|
|
|
slot: Slot,
|
|
|
|
cfg = defaultRuntimeConfig): ForkedSignedBeaconBlock =
|
2020-03-14 21:54:45 +00:00
|
|
|
## TODO don't do this gradual construction, for exception safety
|
2019-08-28 12:07:00 +00:00
|
|
|
## Mock a BeaconBlock for the specific slot
|
|
|
|
|
2021-09-20 20:20:29 +00:00
|
|
|
var cache = StateCache()
|
|
|
|
var rewards = RewardInfo()
|
|
|
|
var tmpState = assignClone(state)
|
2021-09-22 17:50:10 +00:00
|
|
|
doAssert process_slots(cfg, tmpState[], slot, cache, rewards, flags = {})
|
2021-09-20 20:20:29 +00:00
|
|
|
|
|
|
|
var previous_block_header = getStateField(tmpState[], latest_block_header)
|
2019-08-28 12:07:00 +00:00
|
|
|
if previous_block_header.state_root == ZERO_HASH:
|
2021-09-20 20:20:29 +00:00
|
|
|
previous_block_header.state_root = tmpState[].hash_tree_root()
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
applyRandaoReveal(tmpState[], result)
|
|
|
|
|
|
|
|
if result.kind >= BeaconBlockFork.Altair:
|
|
|
|
result.altairBlock.message.body.sync_aggregate.sync_committee_signature = ValidatorSig.infinity
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2021-09-20 20:20:29 +00:00
|
|
|
signMockBlock(tmpState[], result)
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2021-09-20 20:20:29 +00:00
|
|
|
# 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)
|