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
|
|
|
|
../../beacon_chain/spec/[datatypes, crypto, helpers, validator],
|
|
|
|
# Internals
|
|
|
|
../../beacon_chain/[ssz, extras, state_transition],
|
|
|
|
# Mock helpers
|
|
|
|
./mock_validator_keys
|
|
|
|
|
|
|
|
# Routines for mocking blocks
|
|
|
|
# ---------------------------------------------------------------
|
|
|
|
|
|
|
|
proc signMockBlockImpl(
|
|
|
|
state: BeaconState,
|
2019-12-16 18:08:50 +00:00
|
|
|
signedBlock: var SignedBeaconBlock,
|
2019-08-28 12:07:00 +00:00
|
|
|
proposer_index: ValidatorIndex
|
|
|
|
) =
|
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
|
|
|
|
|
|
|
let privkey = MockPrivKeys[proposer_index]
|
|
|
|
|
2019-12-16 18:08:50 +00:00
|
|
|
signedBlock.message.body.randao_reveal = bls_sign(
|
2019-08-28 12:07:00 +00:00
|
|
|
key = privkey,
|
2019-12-16 18:08:50 +00:00
|
|
|
msg = block_slot
|
initial 0.9.0 spec sync (#509)
* rename compute_epoch_of_slot(...) to compute_epoch_at_slot(...)
* remove some unnecessary imports; remove some crosslink-related code and tests; complete renaming of compute_epoch_of_slot(...) to compute_epoch_at_slot(...)
* rm more transfer-related code and tests; rm more unnecessary strutils imports
* rm remaining unused imports
* remove useless get_empty_per_epoch_cache(...)/compute_start_slot_of_epoch(...) calls
* rename compute_start_slot_of_epoch(...) to compute_start_slot_at_epoch(...)
* rename ACTIVATION_EXIT_DELAY to MAX_SEED_LOOKAHEAD
* update domain types to 0.9.0
* mark AttesterSlashing, IndexedAttestation, AttestationDataAndCustodyBit, DepositData, BeaconBlockHeader, Fork, integer_squareroot(...), and process_voluntary_exit(...) as 0.9.0
* mark increase_balance(...), decrease_balance(...), get_block_root(...), CheckPoint, Deposit, PendingAttestation, HistoricalBatch, is_active_validator(...), and is_slashable_attestation_data(...) as 0.9.0
* mark compute_activation_exit_epoch(...), bls_verify(...), Validator, get_active_validator_indices(...), get_current_epoch(...), get_total_active_balance(...), and get_previous_epoch(...) as 0.9.0
* mark get_block_root_at_slot(...), ProposerSlashing, get_domain(...), VoluntaryExit, mainnet preset Gwei values, minimal preset max operations, process_block_header(...), and is_slashable_validator(...) as 0.9.0
* mark makeWithdrawalCredentials(...), get_validator_churn_limit(...), get_total_balance(...), is_valid_indexed_attestation(...), bls_aggregate_pubkeys(...), initial genesis value/constants, Attestation, get_randao_mix(...), mainnet preset max operations per block constants, minimal preset Gwei values and time parameters, process_eth1_data(...), get_shuffled_seq(...), compute_committee(...), and process_slots(...) as 0.9.0; partially update get_indexed_attestation(...) to 0.9.0 by removing crosslink refs and associated tests
* mark initiate_validator_exit(...), process_registry_updates(...), BeaconBlock, Eth1Data, compute_domain(...), process_randao(...), process_attester_slashing(...), get_base_reward(...), and process_slot(...) as 0.9.0
2019-10-30 19:41:19 +00:00
|
|
|
.compute_epoch_at_slot()
|
2019-08-28 12:07:00 +00:00
|
|
|
.hash_tree_root()
|
|
|
|
.data,
|
|
|
|
domain = get_domain(
|
|
|
|
state,
|
|
|
|
DOMAIN_RANDAO,
|
2019-12-16 18:08:50 +00:00
|
|
|
message_epoch = block_slot.compute_epoch_at_slot(),
|
2019-08-28 12:07:00 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2019-12-16 18:08:50 +00:00
|
|
|
signedBlock.signature = bls_sign(
|
2019-08-28 12:07:00 +00:00
|
|
|
key = privkey,
|
2019-12-16 18:08:50 +00:00
|
|
|
msg = signedBlock.message.hash_tree_root().data,
|
2019-08-28 12:07:00 +00:00
|
|
|
domain = get_domain(
|
|
|
|
state,
|
|
|
|
DOMAIN_BEACON_PROPOSER,
|
2019-12-16 18:08:50 +00:00
|
|
|
message_epoch = block_slot.compute_epoch_at_slot(),
|
2019-08-28 12:07:00 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
proc signMockBlock*(
|
|
|
|
state: BeaconState,
|
2019-12-16 18:08:50 +00:00
|
|
|
signedBlock: var SignedBeaconBlock
|
2019-08-28 12:07:00 +00:00
|
|
|
) =
|
|
|
|
|
|
|
|
var emptyCache = get_empty_per_epoch_cache()
|
2019-12-04 10:49:59 +00:00
|
|
|
let proposer_index =
|
2019-12-16 18:08:50 +00:00
|
|
|
if signedBlock.message.slot == state.slot:
|
2019-12-04 10:49:59 +00:00
|
|
|
get_beacon_proposer_index(state, emptyCache)
|
|
|
|
else:
|
|
|
|
# Stub to get proposer index of future slot
|
|
|
|
# Note: this relies on ``let`` deep-copying the state
|
|
|
|
# i.e. BeaconState should have value semantics
|
|
|
|
# and not contain ref objects or pointers
|
|
|
|
var stubState = state
|
2019-12-16 18:08:50 +00:00
|
|
|
process_slots(stub_state, signedBlock.message.slot)
|
2019-12-04 10:49:59 +00:00
|
|
|
get_beacon_proposer_index(stub_state, emptyCache)
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2019-12-04 10:49:59 +00:00
|
|
|
# In tests, just let this throw if appropriate
|
2019-12-16 18:08:50 +00:00
|
|
|
signMockBlockImpl(state, signedBlock, proposer_index.get)
|
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 =
|
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
|
|
|
|
2019-12-16 18:08:50 +00:00
|
|
|
result.message.slot = slot
|
|
|
|
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)
|
|
|
|
|
|
|
|
proc applyEmptyBlock*(state: var BeaconState) =
|
|
|
|
## Do a state transition with an empty signed block
|
|
|
|
## on the current slot
|
2019-12-16 18:08:50 +00:00
|
|
|
let signedBlock = mockBlock(state, state.slot, flags = {})
|
2020-03-05 12:52:10 +00:00
|
|
|
doAssert state_transition(state, signedBlock, {skipStateRootValidation})
|