2021-03-26 06:52:01 +00:00
|
|
|
# beacon_chain
|
2022-01-29 01:05:39 +00:00
|
|
|
# Copyright (c) 2018-2022 Status Research & Development GmbH
|
2021-03-26 06:52:01 +00:00
|
|
|
# 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.
|
|
|
|
|
2022-07-29 10:53:42 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
2020-04-24 07:16:11 +00:00
|
|
|
|
2018-11-23 23:58:49 +00:00
|
|
|
import
|
2021-12-16 14:57:16 +00:00
|
|
|
std/math,
|
2021-01-21 17:42:57 +00:00
|
|
|
chronos, chronicles,
|
2022-01-11 10:01:54 +00:00
|
|
|
./spec/beacon_time
|
2019-03-22 15:49:37 +00:00
|
|
|
|
2020-08-27 07:34:12 +00:00
|
|
|
from times import Time, getTime, fromUnix, `<`, `-`, inNanoseconds
|
2018-11-23 23:58:49 +00:00
|
|
|
|
2021-03-30 07:40:28 +00:00
|
|
|
export chronos.Duration, Moment, now
|
|
|
|
|
2018-11-23 23:58:49 +00:00
|
|
|
type
|
2019-03-22 15:49:37 +00:00
|
|
|
BeaconClock* = object
|
|
|
|
## The beacon clock represents time as it passes on a beacon chain. Beacon
|
|
|
|
## time is locked to unix time, starting at a particular offset set during
|
|
|
|
## beacon chain instantiation.
|
|
|
|
##
|
|
|
|
## Time on the beacon chain determines what actions should be taken and
|
|
|
|
## which blocks are valid - in particular, blocks are not valid if they
|
|
|
|
## come from the future as seen from the local clock.
|
|
|
|
##
|
2022-05-31 11:15:31 +00:00
|
|
|
## https://github.com/ethereum/consensus-specs/blob/v1.2.0-rc.1/specs/phase0/fork-choice.md#fork-choice
|
2019-03-22 15:49:37 +00:00
|
|
|
##
|
|
|
|
# TODO consider NTP and network-adjusted timestamps as outlined here:
|
|
|
|
# https://ethresear.ch/t/network-adjusted-timestamps/4187
|
|
|
|
genesis: Time
|
|
|
|
|
2021-08-19 10:45:31 +00:00
|
|
|
GetBeaconTimeFn* = proc(): BeaconTime {.gcsafe, raises: [Defect].}
|
2021-03-11 10:10:57 +00:00
|
|
|
|
2020-05-27 17:06:28 +00:00
|
|
|
proc init*(T: type BeaconClock, genesis_time: uint64): T =
|
2021-03-04 09:13:23 +00:00
|
|
|
# ~290 billion years into the future
|
|
|
|
doAssert genesis_time <= high(int64).uint64
|
|
|
|
|
2019-03-22 15:49:37 +00:00
|
|
|
let
|
2020-05-27 17:06:28 +00:00
|
|
|
unixGenesis = fromUnix(genesis_time.int64)
|
2019-03-22 15:49:37 +00:00
|
|
|
# GENESIS_SLOT offsets slot time, but to simplify calculations, we apply that
|
|
|
|
# offset to genesis instead of applying it at every time conversion
|
|
|
|
unixGenesisOffset = times.seconds(int(GENESIS_SLOT * SECONDS_PER_SLOT))
|
|
|
|
|
|
|
|
T(genesis: unixGenesis - unixGenesisOffset)
|
2018-11-23 23:58:49 +00:00
|
|
|
|
2019-03-22 15:49:37 +00:00
|
|
|
func toBeaconTime*(c: BeaconClock, t: Time): BeaconTime =
|
2022-01-11 10:01:54 +00:00
|
|
|
BeaconTime(ns_since_genesis: inNanoseconds(t - c.genesis))
|
2018-11-23 23:58:49 +00:00
|
|
|
|
2019-08-16 11:16:56 +00:00
|
|
|
func toSlot*(c: BeaconClock, t: Time): tuple[afterGenesis: bool, slot: Slot] =
|
2019-03-22 15:49:37 +00:00
|
|
|
c.toBeaconTime(t).toSlot()
|
2018-11-23 23:58:49 +00:00
|
|
|
|
2019-03-22 15:49:37 +00:00
|
|
|
proc now*(c: BeaconClock): BeaconTime =
|
|
|
|
## Current time, in slots - this may end up being less than GENESIS_SLOT(!)
|
|
|
|
toBeaconTime(c, getTime())
|
2018-11-23 23:58:49 +00:00
|
|
|
|
2022-05-31 10:45:37 +00:00
|
|
|
func getBeaconTimeFn*(c: BeaconClock): GetBeaconTimeFn =
|
|
|
|
return proc(): BeaconTime = c.now()
|
|
|
|
|
2019-03-22 15:49:37 +00:00
|
|
|
proc fromNow*(c: BeaconClock, t: BeaconTime): tuple[inFuture: bool, offset: Duration] =
|
|
|
|
let now = c.now()
|
2020-08-27 07:34:12 +00:00
|
|
|
if t > now:
|
2022-01-11 10:01:54 +00:00
|
|
|
(true, chronos.nanoseconds((t - now).nanoseconds))
|
2019-03-22 15:49:37 +00:00
|
|
|
else:
|
2022-01-11 10:01:54 +00:00
|
|
|
(false, chronos.nanoseconds((now - t).nanoseconds))
|
2018-11-23 23:58:49 +00:00
|
|
|
|
2019-03-22 15:49:37 +00:00
|
|
|
proc fromNow*(c: BeaconClock, slot: Slot): tuple[inFuture: bool, offset: Duration] =
|
2022-01-11 10:01:54 +00:00
|
|
|
c.fromNow(slot.start_beacon_time())
|
2018-11-23 23:58:49 +00:00
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
proc durationToNextSlot*(c: BeaconClock): Duration =
|
|
|
|
let (afterGenesis, slot) = c.now().toSlot()
|
|
|
|
if afterGenesis:
|
2021-12-20 11:21:17 +00:00
|
|
|
c.fromNow(slot + 1'u64).offset
|
2021-07-13 11:15:07 +00:00
|
|
|
else:
|
|
|
|
c.fromNow(Slot(0)).offset
|
|
|
|
|
|
|
|
proc durationToNextEpoch*(c: BeaconClock): Duration =
|
|
|
|
let (afterGenesis, slot) = c.now().toSlot()
|
|
|
|
if afterGenesis:
|
2022-01-11 10:01:54 +00:00
|
|
|
c.fromNow((slot.epoch + 1).start_slot()).offset
|
2021-07-13 11:15:07 +00:00
|
|
|
else:
|
2022-01-11 10:01:54 +00:00
|
|
|
c.fromNow(Epoch(0).start_slot()).offset
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2019-11-21 09:15:10 +00:00
|
|
|
func saturate*(d: tuple[inFuture: bool, offset: Duration]): Duration =
|
2019-03-22 15:49:37 +00:00
|
|
|
if d.inFuture: d.offset else: seconds(0)
|
2018-11-23 23:58:49 +00:00
|
|
|
|
2022-01-11 10:01:54 +00:00
|
|
|
proc sleepAsync*(t: TimeDiff): Future[void] =
|
|
|
|
sleepAsync(chronos.nanoseconds(
|
|
|
|
if t.nanoseconds < 0: 0'i64 else: t.nanoseconds))
|
2018-11-23 23:58:49 +00:00
|
|
|
|
2019-08-16 11:16:56 +00:00
|
|
|
func shortLog*(d: Duration): string =
|
2020-11-24 23:39:52 +00:00
|
|
|
$d
|
2019-08-16 11:16:56 +00:00
|
|
|
|
2020-11-11 13:39:36 +00:00
|
|
|
func toFloatSeconds*(d: Duration): float =
|
|
|
|
float(milliseconds(d)) / 1000.0
|
|
|
|
|
2021-12-16 14:57:16 +00:00
|
|
|
func fromFloatSeconds*(T: type Duration, f: float): Duration =
|
|
|
|
case classify(f)
|
|
|
|
of fcNormal:
|
|
|
|
if f >= float(int64.high() div 1_000_000_000): InfiniteDuration
|
|
|
|
elif f <= 0: ZeroDuration
|
|
|
|
else: nanoseconds(int64(f * 1_000_000_000))
|
|
|
|
of fcSubnormal, fcZero, fcNegZero, fcNan, fcNegInf: ZeroDuration
|
|
|
|
of fcInf: InfiniteDuration
|
|
|
|
|
2021-01-26 12:08:29 +00:00
|
|
|
chronicles.formatIt Duration: $it
|