mirror of
https://github.com/status-im/nimbus-eth2.git
synced 2025-01-09 22:06:21 +00:00
805e85e1ff
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.
137 lines
4.3 KiB
Nim
137 lines
4.3 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.}
|
|
|
|
import
|
|
chronicles,
|
|
unittest2,
|
|
../beacon_chain/consensus_object_pools/block_dag
|
|
|
|
func `$`(x: BlockRef): string = shortLog(x)
|
|
|
|
suite "BlockRef and helpers":
|
|
test "isAncestorOf sanity":
|
|
let
|
|
s0 = BlockRef(bid: BlockId(slot: Slot(0)))
|
|
s1 = BlockRef(bid: BlockId(slot: Slot(1)), parent: s0)
|
|
s2 = BlockRef(bid: BlockId(slot: Slot(2)), parent: s1)
|
|
|
|
check:
|
|
s0.isAncestorOf(s0)
|
|
s0.isAncestorOf(s1)
|
|
s0.isAncestorOf(s2)
|
|
s1.isAncestorOf(s1)
|
|
s1.isAncestorOf(s2)
|
|
|
|
not s2.isAncestorOf(s0)
|
|
not s2.isAncestorOf(s1)
|
|
not s1.isAncestorOf(s0)
|
|
|
|
test "get_ancestor sanity":
|
|
let
|
|
s0 = BlockRef(bid: BlockId(slot: Slot(0)))
|
|
s1 = BlockRef(bid: BlockId(slot: Slot(1)), parent: s0)
|
|
s2 = BlockRef(bid: BlockId(slot: Slot(2)), parent: s1)
|
|
s4 = BlockRef(bid: BlockId(slot: Slot(4)), parent: s2)
|
|
|
|
check:
|
|
s0.get_ancestor(Slot(0)) == s0
|
|
s0.get_ancestor(Slot(1)) == s0
|
|
|
|
s1.get_ancestor(Slot(0)) == s0
|
|
s1.get_ancestor(Slot(1)) == s1
|
|
|
|
s4.get_ancestor(Slot(0)) == s0
|
|
s4.get_ancestor(Slot(1)) == s1
|
|
s4.get_ancestor(Slot(2)) == s2
|
|
s4.get_ancestor(Slot(3)) == s2
|
|
s4.get_ancestor(Slot(4)) == s4
|
|
|
|
suite "BlockSlot and helpers":
|
|
test "atSlot sanity":
|
|
let
|
|
s0 = BlockRef(bid: BlockId(slot: Slot(0)))
|
|
s1 = BlockRef(bid: BlockId(slot: Slot(1)), parent: s0)
|
|
s2 = BlockRef(bid: BlockId(slot: Slot(2)), parent: s1)
|
|
s4 = BlockRef(bid: BlockId(slot: Slot(4)), parent: s2)
|
|
se1 = BlockRef(bid:
|
|
BlockId(slot: Epoch(1).start_slot()), parent: s2)
|
|
se2 = BlockRef(bid:
|
|
BlockId(slot: Epoch(2).start_slot()), parent: se1)
|
|
|
|
check:
|
|
s0.atSlot(Slot(0)).blck == s0
|
|
s0.atSlot(Slot(0)) == s1.atSlot(Slot(0))
|
|
s1.atSlot(Slot(1)).blck == s1
|
|
|
|
s4.atSlot(Slot(0)).blck == s0
|
|
|
|
s4.atSlot() == s4.atSlot(s4.slot)
|
|
|
|
se2.dependentBlock(s0, Epoch(2)) == se1
|
|
se2.dependentBlock(s0, Epoch(1)) == s2
|
|
se2.dependentBlock(s0, Epoch(0)) == s0
|
|
|
|
se2.prevDependentBlock(s0, Epoch(2)) == s2
|
|
se2.prevDependentBlock(s0, Epoch(1)) == s0
|
|
se2.prevDependentBlock(s0, Epoch(0)) == s0
|
|
|
|
test "parent sanity":
|
|
let
|
|
s0 = BlockRef(bid: BlockId(slot: Slot(0)))
|
|
s00 = BlockSlot(blck: s0, slot: Slot(0))
|
|
s01 = BlockSlot(blck: s0, slot: Slot(1))
|
|
s2 = BlockRef(bid: BlockId(slot: Slot(2)), parent: s0)
|
|
s22 = BlockSlot(blck: s2, slot: Slot(2))
|
|
s24 = BlockSlot(blck: s2, slot: Slot(4))
|
|
|
|
check:
|
|
s00.parent == BlockSlot(blck: nil, slot: Slot(0))
|
|
s01.parent == s00
|
|
s01.parentOrSlot == s00
|
|
s22.parent == s01
|
|
s22.parentOrSlot == BlockSlot(blck: s0, slot: Slot(2))
|
|
s24.parent == BlockSlot(blck: s2, slot: Slot(3))
|
|
s24.parent.parent == s22
|
|
|
|
s22.isProposed()
|
|
not s24.isProposed()
|
|
|
|
suite "BlockId and helpers":
|
|
test "atSlot sanity":
|
|
let
|
|
s0 = BlockRef(bid: BlockId(slot: Slot(0)))
|
|
s1 = BlockRef(bid: BlockId(slot: Slot(1)), parent: s0)
|
|
s2 = BlockRef(bid: BlockId(slot: Slot(2)), parent: s1)
|
|
s4 = BlockRef(bid: BlockId(slot: Slot(4)), parent: s2)
|
|
|
|
check:
|
|
s0.atSlot(Slot(0)).blck == s0
|
|
s0.atSlot(Slot(0)) == s1.atSlot(Slot(0))
|
|
s1.atSlot(Slot(1)).blck == s1
|
|
|
|
s4.atSlot(Slot(0)).blck == s0
|
|
|
|
test "parent sanity":
|
|
let
|
|
s0 = BlockRef(bid: BlockId(slot: Slot(0)))
|
|
s00 = BlockSlot(blck: s0, slot: Slot(0))
|
|
s01 = BlockSlot(blck: s0, slot: Slot(1))
|
|
s2 = BlockRef(bid: BlockId(slot: Slot(2)), parent: s0)
|
|
s22 = BlockSlot(blck: s2, slot: Slot(2))
|
|
s24 = BlockSlot(blck: s2, slot: Slot(4))
|
|
|
|
check:
|
|
s00.parent == BlockSlot(blck: nil, slot: Slot(0))
|
|
s01.parent == s00
|
|
s01.parentOrSlot == s00
|
|
s22.parent == s01
|
|
s22.parentOrSlot == BlockSlot(blck: s0, slot: Slot(2))
|
|
s24.parent == BlockSlot(blck: s2, slot: Slot(3))
|
|
s24.parent.parent == s22
|