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-06-16 05:45:04 +00:00
|
|
|
../../beacon_chain/spec/[datatypes, helpers, signatures, validator],
|
2019-08-28 12:07:00 +00:00
|
|
|
# Internals
|
2020-05-19 14:37:29 +00:00
|
|
|
../../beacon_chain/[ssz, extras],
|
2019-08-28 12:07:00 +00:00
|
|
|
# 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-06-16 05:45:04 +00:00
|
|
|
state.fork, state.genesis_validators_root, block_slot.compute_epoch_at_slot,
|
|
|
|
privkey)
|
2020-07-16 13:16:51 +00:00
|
|
|
signedBlock.root = hash_tree_root(signedBlock.message)
|
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,
|
2020-07-16 13:16:51 +00:00
|
|
|
signedBlock.root, 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,
|
2020-12-22 11:13:51 +00:00
|
|
|
flags: UpdateFlags = {}): SignedBeaconBlock[Unchecked] =
|
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-07-15 10:44:18 +00:00
|
|
|
var emptyCache = StateCache()
|
2020-03-14 21:54:45 +00:00
|
|
|
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 = {}):
|
2020-12-22 11:13:51 +00:00
|
|
|
SignedBeaconBlock[Unchecked] =
|
2019-08-28 12:07:00 +00:00
|
|
|
mockBlock(state, state.slot + 1, flags)
|