nimbus-eth2/tests/test_forks.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

101 lines
3.1 KiB
Nim

import
unittest2,
stew/byteutils,
../beacon_chain/spec/[forks, helpers],
../beacon_chain/spec/datatypes/[phase0, altair, merge]
{.used.}
template testHashedBeaconState(T: type, s: Slot) =
let
state = (ref T)()
state[].slot = s
let
bytes = SSZ.encode(state[])
forked = (ref ForkedHashedBeaconState)()
forked[] = readSszForkedHashedBeaconState(cfg, bytes)
check:
forked.kind == T.toFork()
template testTrustedSignedBeaconBlock(T: type, s: Slot) =
let
blck = (ref T)()
blck[].message.slot = s
let
bytes = SSZ.encode(blck[])
forked = (ref ForkedSignedBeaconBlock)()
forked[] = readSszForkedSignedBeaconBlock(cfg, bytes)
check:
forked.kind == T.toFork()
suite "Forked SSZ readers":
var
cfg = defaultRuntimeConfig
cfg.ALTAIR_FORK_EPOCH = Epoch(1)
cfg.MERGE_FORK_EPOCH = Epoch(2)
test "load phase0 state":
testHashedBeaconState(phase0.BeaconState, 0.Slot)
expect(SszError):
testHashedBeaconState(altair.BeaconState, 0.Slot)
expect(SszError):
testHashedBeaconState(merge.BeaconState, 0.Slot)
test "load altair state":
testHashedBeaconState(altair.BeaconState, cfg.ALTAIR_FORK_EPOCH.start_slot)
expect(SszError):
testHashedBeaconState(phase0.BeaconState, cfg.ALTAIR_FORK_EPOCH.start_slot)
expect(SszError):
testHashedBeaconState(merge.BeaconState, cfg.ALTAIR_FORK_EPOCH.start_slot)
test "load merge state":
testHashedBeaconState(merge.BeaconState, cfg.MERGE_FORK_EPOCH.start_slot)
expect(SszError):
testHashedBeaconState(phase0.BeaconState, cfg.MERGE_FORK_EPOCH.start_slot)
expect(SszError):
testHashedBeaconState(altair.BeaconState, cfg.MERGE_FORK_EPOCH.start_slot)
test "should raise on unknown data":
let
bytes = SSZ.encode(AttestationData())
expect(SszError):
discard newClone(readSszForkedHashedBeaconState(cfg, bytes))
test "load phase0 block":
testTrustedSignedBeaconBlock(phase0.TrustedSignedBeaconBlock, 0.Slot)
expect(SszError):
testTrustedSignedBeaconBlock(altair.TrustedSignedBeaconBlock, 0.Slot)
expect(SszError):
testTrustedSignedBeaconBlock(merge.TrustedSignedBeaconBlock, 0.Slot)
test "load altair block":
testTrustedSignedBeaconBlock(altair.TrustedSignedBeaconBlock, cfg.ALTAIR_FORK_EPOCH.start_slot)
expect(SszError):
testTrustedSignedBeaconBlock(phase0.TrustedSignedBeaconBlock, cfg.ALTAIR_FORK_EPOCH.start_slot)
expect(SszError):
testTrustedSignedBeaconBlock(merge.TrustedSignedBeaconBlock, cfg.ALTAIR_FORK_EPOCH.start_slot)
test "load merge block":
testTrustedSignedBeaconBlock(merge.TrustedSignedBeaconBlock, cfg.MERGE_FORK_EPOCH.start_slot)
expect(SszError):
testTrustedSignedBeaconBlock(phase0.TrustedSignedBeaconBlock, cfg.MERGE_FORK_EPOCH.start_slot)
expect(SszError):
testTrustedSignedBeaconBlock(altair.TrustedSignedBeaconBlock, cfg.MERGE_FORK_EPOCH.start_slot)
test "should raise on unknown data":
let
bytes = SSZ.encode(AttestationData())
expect(SszError):
discard newClone(readSszForkedSignedBeaconBlock(cfg, bytes))