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
|
2019-12-04 10:49:59 +00:00
|
|
|
options,
|
2019-08-28 12:07:00 +00:00
|
|
|
# Specs
|
2021-04-26 20:39:44 +00:00
|
|
|
../../beacon_chain/spec/[crypto, datatypes, helpers, signatures, validator],
|
2019-08-28 12:07:00 +00:00
|
|
|
# Internals
|
2021-01-22 13:29:04 +00:00
|
|
|
../../beacon_chain/ssz,
|
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,
|
2021-04-26 20:39:44 +00:00
|
|
|
privkey).toValidatorSig()
|
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,
|
2021-04-26 20:39:44 +00:00
|
|
|
signedBlock.root, privkey).toValidatorSig()
|
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,
|
2021-01-22 13:29:04 +00:00
|
|
|
slot: Slot): 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-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
|
|
|
|
2021-01-22 13:29:04 +00:00
|
|
|
signMockBlock(state, result)
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2021-01-22 13:29:04 +00:00
|
|
|
proc mockBlockForNextSlot*(state: BeaconState): SignedBeaconBlock =
|
|
|
|
mockBlock(state, state.slot + 1)
|