2019-08-28 12:07:00 +00:00
|
|
|
# beacon_chain
|
2022-01-04 06:08:19 +00:00
|
|
|
# Copyright (c) 2018-2022 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
|
2021-10-07 09:28:09 +00:00
|
|
|
../../beacon_chain/spec/
|
|
|
|
[forks, helpers, signatures, state_transition, validator],
|
2022-01-06 11:25:35 +00:00
|
|
|
../../beacon_chain/spec/datatypes/[phase0, altair, bellatrix],
|
2021-09-30 15:14:03 +00:00
|
|
|
# Test utilities
|
|
|
|
../testblockutil
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# Routines for mocking blocks
|
|
|
|
# ---------------------------------------------------------------
|
|
|
|
|
2022-01-04 06:08:19 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.8/tests/core/pyspec/eth2spec/test/helpers/block.py#L26-L35
|
2021-11-05 07:34:34 +00:00
|
|
|
func apply_randao_reveal(state: ForkyBeaconState, blck: var ForkySignedBeaconBlock) =
|
2021-10-07 09:28:09 +00:00
|
|
|
doAssert state.slot <= blck.message.slot
|
|
|
|
let
|
|
|
|
proposer_index = blck.message.proposer_index.ValidatorIndex
|
|
|
|
privkey = MockPrivKeys[proposer_index]
|
|
|
|
|
|
|
|
blck.message.body.randao_reveal = get_epoch_signature(
|
|
|
|
state.fork,
|
|
|
|
state.genesis_validators_root,
|
|
|
|
blck.message.slot.compute_epoch_at_slot,
|
|
|
|
privkey).toValidatorSig()
|
|
|
|
|
2022-01-05 11:07:14 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.8/tests/core/pyspec/eth2spec/test/helpers/block.py#L38-L54
|
2021-11-05 07:34:34 +00:00
|
|
|
func sign_block(state: ForkyBeaconState, blck: var ForkySignedBeaconBlock) =
|
2021-10-07 09:28:09 +00:00
|
|
|
let
|
|
|
|
proposer_index = blck.message.proposer_index.ValidatorIndex
|
|
|
|
privkey = MockPrivKeys[proposer_index]
|
|
|
|
|
|
|
|
blck.root = blck.message.hash_tree_root()
|
|
|
|
blck.signature = get_block_signature(
|
|
|
|
state.fork,
|
|
|
|
state.genesis_validators_root,
|
|
|
|
blck.message.slot,
|
|
|
|
blck.root,
|
|
|
|
privkey).toValidatorSig()
|
|
|
|
|
2022-01-05 11:07:14 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.8/tests/core/pyspec/eth2spec/test/helpers/execution_payload.py#L1-L31
|
2022-01-06 11:25:35 +00:00
|
|
|
func build_empty_execution_payload(state: bellatrix.BeaconState): ExecutionPayload =
|
2021-10-07 09:28:09 +00:00
|
|
|
## Assuming a pre-state of the same slot, build a valid ExecutionPayload
|
|
|
|
## without any transactions.
|
|
|
|
let
|
|
|
|
latest = state.latest_execution_payload_header
|
|
|
|
timestamp = compute_timestamp_at_slot(state, state.slot)
|
|
|
|
randao_mix = get_randao_mix(state, get_current_epoch(state))
|
|
|
|
|
|
|
|
var payload = ExecutionPayload(
|
|
|
|
parent_hash: latest.block_hash,
|
|
|
|
state_root: latest.state_root, # no changes to the state
|
2021-12-09 11:18:38 +00:00
|
|
|
receipts_root: Eth2Digest(data: cast[array[32, uint8]](
|
2021-10-07 09:28:09 +00:00
|
|
|
"no receipts here\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0")),
|
|
|
|
block_number: latest.block_number + 1,
|
|
|
|
random: randao_mix,
|
|
|
|
gas_limit: latest.gas_limit, # retain same limit
|
|
|
|
gas_used: 0, # empty block, 0 gas
|
|
|
|
timestamp: timestamp,
|
|
|
|
base_fee_per_gas: latest.base_fee_per_gas) # retain same base_fee
|
|
|
|
|
|
|
|
payload.block_hash = withEth2Hash:
|
|
|
|
h.update payload.hash_tree_root().data
|
|
|
|
h.update cast[array[13, uint8]]("FAKE RLP HASH")
|
|
|
|
|
|
|
|
payload
|
|
|
|
|
2022-01-05 11:07:14 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.8/tests/core/pyspec/eth2spec/test/helpers/block.py#L75-L104
|
2021-09-22 17:50:10 +00:00
|
|
|
proc mockBlock*(
|
2021-10-07 09:28:09 +00:00
|
|
|
state: ForkedHashedBeaconState,
|
|
|
|
slot: Slot,
|
2021-09-22 17:50:10 +00:00
|
|
|
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-10-07 09:28:09 +00:00
|
|
|
var
|
2021-09-28 16:27:44 +00:00
|
|
|
cache = StateCache()
|
|
|
|
tmpState = assignClone(state)
|
|
|
|
if getStateField(state, slot) != slot:
|
2021-10-13 14:24:36 +00:00
|
|
|
var info = ForkedEpochInfo()
|
|
|
|
doAssert process_slots(cfg, tmpState[], slot, cache, info, flags = {})
|
2021-09-20 20:20:29 +00:00
|
|
|
|
2021-10-18 16:37:27 +00:00
|
|
|
result.kind = case tmpState[].kind
|
2022-01-04 09:45:38 +00:00
|
|
|
of BeaconStateFork.Phase0: BeaconBlockFork.Phase0
|
|
|
|
of BeaconStateFork.Altair: BeaconBlockFork.Altair
|
|
|
|
of BeaconStateFork.Bellatrix: BeaconBlockFork.Bellatrix
|
2021-10-07 09:28:09 +00:00
|
|
|
withStateAndBlck(tmpState[], result):
|
2021-09-20 20:20:29 +00:00
|
|
|
blck.message.slot = slot
|
2021-10-07 09:28:09 +00:00
|
|
|
blck.message.proposer_index =
|
|
|
|
get_beacon_proposer_index(state.data, cache, slot).get.uint64
|
|
|
|
blck.message.body.eth1_data.deposit_count = state.data.eth1_deposit_index
|
|
|
|
blck.message.parent_root = block:
|
|
|
|
var previous_block_header = state.data.latest_block_header
|
|
|
|
if previous_block_header.state_root == ZERO_HASH:
|
|
|
|
previous_block_header.state_root = state.data.hash_tree_root()
|
|
|
|
previous_block_header.hash_tree_root()
|
|
|
|
|
|
|
|
apply_randao_reveal(state.data, blck)
|
|
|
|
|
2021-10-18 16:37:27 +00:00
|
|
|
when stateFork >= BeaconStateFork.Altair:
|
2021-10-07 09:28:09 +00:00
|
|
|
blck.message.body.sync_aggregate = SyncAggregate.init()
|
2021-09-20 20:20:29 +00:00
|
|
|
|
2022-01-04 09:45:38 +00:00
|
|
|
when stateFork >= BeaconStateFork.Bellatrix:
|
2021-10-07 09:28:09 +00:00
|
|
|
blck.message.body.execution_payload =
|
|
|
|
build_empty_execution_payload(state.data)
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2021-10-07 09:28:09 +00:00
|
|
|
sign_block(state.data, blck)
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2022-01-05 11:07:14 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.8/tests/core/pyspec/eth2spec/test/helpers/block.py#L107-L108
|
2021-10-07 09:28:09 +00:00
|
|
|
proc mockBlockForNextSlot*(
|
|
|
|
state: ForkedHashedBeaconState): ForkedSignedBeaconBlock =
|
|
|
|
## Mock a BeaconBlock for the next slot
|
2021-09-20 20:20:29 +00:00
|
|
|
mockBlock(state, getStateField(state, slot) + 1)
|