nimbus-eth2/tests/mocking/mock_blocks.nim
Dustin Brody 63e621c27d
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

106 lines
3.3 KiB
Nim

# beacon_chain
# Copyright (c) 2018-2019 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0).
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import
# 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,
blck: var BeaconBlock,
proposer_index: ValidatorIndex
) =
doAssert state.slot <= blck.slot
let privkey = MockPrivKeys[proposer_index]
blck.body.randao_reveal = bls_sign(
key = privkey,
msg = blck.slot
.compute_epoch_at_slot()
.hash_tree_root()
.data,
domain = get_domain(
state,
DOMAIN_RANDAO,
message_epoch = blck.slot.compute_epoch_at_slot(),
)
)
blck.signature = bls_sign(
key = privkey,
msg = blck.signing_root().data,
domain = get_domain(
state,
DOMAIN_BEACON_PROPOSER,
message_epoch = blck.slot.compute_epoch_at_slot(),
)
)
proc signMockBlock*(
state: BeaconState,
blck: var BeaconBlock,
proposer_index: ValidatorIndex
) =
signMockBlockImpl(state, blck, proposer_index)
proc signMockBlock*(
state: BeaconState,
blck: var BeaconBlock
) =
var proposer_index: ValidatorIndex
var emptyCache = get_empty_per_epoch_cache()
if blck.slot == state.slot:
proposer_index = 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
process_slots(stub_state, blck.slot)
proposer_index = get_beacon_proposer_index(stub_state, emptyCache)
signMockBlockImpl(state, blck, proposer_index)
proc mockBlock*(
state: BeaconState,
slot: Slot,
flags: UpdateFlags = {}): BeaconBlock =
## Mock a BeaconBlock for the specific slot
## Add skipValidation if block should not be signed
result.slot = slot
result.body.eth1_data.deposit_count = state.eth1_deposit_index
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()
result.parent_root = previous_block_header.signing_root()
if skipValidation notin flags:
signMockBlock(state, result)
proc mockBlockForNextSlot*(state: BeaconState, flags: UpdateFlags = {}): BeaconBlock =
mockBlock(state, state.slot + 1, flags)
proc applyEmptyBlock*(state: var BeaconState) =
## Do a state transition with an empty signed block
## on the current slot
let blck = mockBlock(state, state.slot, flags = {})
# TODO: we only need to skip verifyStateRoot validation
# processBlock validation should work
doAssert state_transition(state, blck, {skipValidation})