2019-08-28 12:07:00 +00:00
|
|
|
# beacon_chain
|
|
|
|
# Copyright (c) 2018-2019 Status Research & Development GmbH
|
|
|
|
# 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
|
2019-12-04 10:49:59 +00:00
|
|
|
options,
|
2019-08-28 12:07:00 +00:00
|
|
|
# Specs
|
2020-04-09 09:41:02 +00:00
|
|
|
../../beacon_chain/spec/[datatypes, validator, state_transition_block],
|
2019-08-28 12:07:00 +00:00
|
|
|
# Internals
|
|
|
|
../../beacon_chain/[ssz, extras, state_transition],
|
|
|
|
# Mock helpers
|
|
|
|
./mock_validator_keys
|
|
|
|
|
|
|
|
# Routines for mocking blocks
|
|
|
|
# ---------------------------------------------------------------
|
|
|
|
|
|
|
|
proc signMockBlockImpl(
|
|
|
|
state: BeaconState,
|
2020-04-09 09:41:02 +00:00
|
|
|
signedBlock: var SignedBeaconBlock
|
2019-08-28 12:07:00 +00:00
|
|
|
) =
|
2019-12-16 18:08:50 +00:00
|
|
|
let block_slot = signedBlock.message.slot
|
|
|
|
doAssert state.slot <= block_slot
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2020-04-09 09:41:02 +00:00
|
|
|
let privkey = MockPrivKeys[signedBlock.message.proposer_index]
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2020-03-19 23:48:03 +00:00
|
|
|
signedBlock.message.body.randao_reveal = get_epoch_signature(
|
2020-03-30 11:31:44 +00:00
|
|
|
state.fork, state.genesis_validators_root, block_slot, privkey)
|
2020-03-19 23:48:03 +00:00
|
|
|
signedBlock.signature = get_block_signature(
|
2020-03-30 11:31:44 +00:00
|
|
|
state.fork, state.genesis_validators_root, block_slot,
|
|
|
|
hash_tree_root(signedBlock.message), privkey)
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2020-04-09 09:41:02 +00:00
|
|
|
proc signMockBlock*(state: BeaconState, signedBlock: var SignedBeaconBlock) =
|
|
|
|
signMockBlockImpl(state, signedBlock)
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2019-12-16 18:08:50 +00:00
|
|
|
proc mockBlock(
|
2019-08-28 12:07:00 +00:00
|
|
|
state: BeaconState,
|
|
|
|
slot: Slot,
|
2019-12-16 18:08:50 +00:00
|
|
|
flags: UpdateFlags = {}): SignedBeaconBlock =
|
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
|
2020-03-05 12:52:10 +00:00
|
|
|
## Skip signature creation if block should not be signed (skipBlsValidation present)
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2020-03-14 21:54:45 +00:00
|
|
|
var emptyCache = get_empty_per_epoch_cache()
|
|
|
|
let proposer_index = get_beacon_proposer_index(state, emptyCache)
|
2019-12-16 18:08:50 +00:00
|
|
|
result.message.slot = slot
|
2020-03-14 21:54:45 +00:00
|
|
|
result.message.proposer_index = proposer_index.get.uint64
|
2019-12-16 18:08:50 +00:00
|
|
|
result.message.body.eth1_data.deposit_count = state.eth1_deposit_index
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
var previous_block_header = state.latest_block_header
|
|
|
|
if previous_block_header.state_root == ZERO_HASH:
|
|
|
|
previous_block_header.state_root = state.hash_tree_root()
|
2019-12-16 18:08:50 +00:00
|
|
|
result.message.parent_root = previous_block_header.hash_tree_root()
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2020-03-05 12:52:10 +00:00
|
|
|
if skipBlsValidation notin flags:
|
2019-08-28 12:07:00 +00:00
|
|
|
signMockBlock(state, result)
|
|
|
|
|
2019-12-16 18:08:50 +00:00
|
|
|
proc mockBlockForNextSlot*(state: BeaconState, flags: UpdateFlags = {}):
|
|
|
|
SignedBeaconBlock =
|
2019-08-28 12:07:00 +00:00
|
|
|
mockBlock(state, state.slot + 1, flags)
|
|
|
|
|
2020-04-30 06:44:19 +00:00
|
|
|
proc applyEmptyBlock*(state: var HashedBeaconState) =
|
2019-08-28 12:07:00 +00:00
|
|
|
## Do a state transition with an empty signed block
|
|
|
|
## on the current slot
|
2020-04-30 06:44:19 +00:00
|
|
|
let signedBlock = mockBlock(state.data, state.data.slot, flags = {})
|
2020-04-26 19:13:33 +00:00
|
|
|
doAssert state_transition(
|
|
|
|
state, signedBlock, {skipStateRootValidation}, noRollback)
|