nimbus-eth2/tests/test_spec.nim
Jacek Sieka 805e85e1ff
time: spring cleaning (#3262)
Time in the beacon chain is expressed relative to the genesis time -
this PR creates a `beacon_time` module that collects helpers and
utilities for dealing the time units - the new module does not deal with
actual wall time (that's remains in `beacon_clock`).

Collecting the time related stuff in one place makes it easier to find,
avoids some circular imports and allows more easily identifying the code
actually needs wall time to operate.

* move genesis-time-related functionality into `spec/beacon_time`
* avoid using `chronos.Duration` for time differences - it does not
support negative values (such as when something happens earlier than it
should)
* saturate conversions between `FAR_FUTURE_XXX`, so as to avoid
overflows
* fix delay reporting in validator client so it uses the expected
deadline of the slot, not "closest wall slot"
* simplify looping over the slots of an epoch
* `compute_start_slot_at_epoch` -> `start_slot`
* `compute_epoch_at_slot` -> `epoch`

A follow-up PR will (likely) introduce saturating arithmetic for the
time units - this is merely code moves, renames and fixing of small
bugs.
2022-01-11 11:01:54 +01:00

77 lines
3.1 KiB
Nim

# beacon_chain
# Copyright (c) 2018-2021 Status Research & Development GmbH
# Licensed and distributed under either of
# * 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).
# at your option. This file may not be copied, modified, or distributed except according to those terms.
{.used.}
# Test for spec functions and helpers outside of the EF test vectors - mainly
# helpers that extend or make the spec functions usable outside of the state
# transition functions
import
unittest2,
../beacon_chain/spec/datatypes/phase0,
../beacon_chain/spec/[beaconstate, state_transition],
./testutil, ./testblockutil
suite "Beacon state" & preset():
test "Smoke test initialize_beacon_state_from_eth1" & preset():
let state = newClone(initialize_beacon_state_from_eth1(
defaultRuntimeConfig, Eth2Digest(), 0,
makeInitialDeposits(SLOTS_PER_EPOCH, {}), {}))
check: state.validators.lenu64 == SLOTS_PER_EPOCH
test "latest_block_root":
var
cfg = defaultRuntimeConfig
state = (ref ForkedHashedBeaconState)(
kind: BeaconStateFork.Phase0,
phase0Data: initialize_hashed_beacon_state_from_eth1(
defaultRuntimeConfig, Eth2Digest(), 0,
makeInitialDeposits(SLOTS_PER_EPOCH, {}), {skipBlsValidation}))
genBlock = get_initial_beacon_block(state[])
cache: StateCache
info: ForkedEpochInfo
check: # Works for genesis block
state[].phase0Data.latest_block_root() == genBlock.root
process_slots(cfg, state[], Slot 1, cache, info, {})
state[].phase0Data.latest_block_root() == genBlock.root
let blck = addTestBlock(
state[], cache, nextSlot = false, flags = {skipBlsValidation}).phase0Data
check: # Works for random blocks
state[].phase0Data.latest_block_root() == blck.root
process_slots(cfg, state[], Slot 2, cache, info, {})
state[].phase0Data.latest_block_root() == blck.root
test "get_beacon_proposer_index":
var
cfg = defaultRuntimeConfig
state = (ref ForkedHashedBeaconState)(
kind: BeaconStateFork.Phase0,
phase0Data: initialize_hashed_beacon_state_from_eth1(
defaultRuntimeConfig, Eth2Digest(), 0,
makeInitialDeposits(SLOTS_PER_EPOCH, {}), {skipBlsValidation}))
cache: StateCache
info: ForkedEpochInfo
check:
get_beacon_proposer_index(state[].phase0Data.data, cache, Slot 1).isSome()
get_beacon_proposer_index(
state[].phase0Data.data, cache, Epoch(1).start_slot()).isNone()
get_beacon_proposer_index(
state[].phase0Data.data, cache, Epoch(2).start_slot()).isNone()
check:
process_slots(cfg, state[], Epoch(1).start_slot(), cache, info, {})
get_beacon_proposer_index(state[].phase0Data.data, cache, Slot 1).isNone()
get_beacon_proposer_index(
state[].phase0Data.data, cache, Epoch(1).start_slot()).isSome()
get_beacon_proposer_index(
state[].phase0Data.data, cache, Epoch(2).start_slot()).isNone()