2021-03-26 06:52:01 +00:00
|
|
|
# beacon_chain
|
2024-01-06 06:18:28 +00:00
|
|
|
# Copyright (c) 2018-2024 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.
|
|
|
|
|
2023-01-20 14:14:37 +00:00
|
|
|
{.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,
|
2024-01-06 14:26:56 +00:00
|
|
|
results,
|
2023-08-10 12:52:49 +00:00
|
|
|
chronos/timer, chronicles,
|
2022-01-11 10:01:54 +00:00
|
|
|
./spec/beacon_time
|
2019-03-22 15:49:37 +00:00
|
|
|
|
2024-01-06 14:26:56 +00:00
|
|
|
from std/times import Time, getTime, fromUnix, toUnix, `<`, `-`, inNanoseconds
|
2018-11-23 23:58:49 +00:00
|
|
|
|
2023-08-23 16:39:57 +00:00
|
|
|
export timer.Duration, Moment, now, beacon_time
|
2021-03-30 07:40:28 +00:00
|
|
|
|
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.
|
|
|
|
##
|
2024-08-21 12:25:19 +00:00
|
|
|
## https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.5/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
|
|
|
|
|
2023-08-25 09:29:07 +00:00
|
|
|
GetBeaconTimeFn* = proc(): BeaconTime {.gcsafe, raises: [].}
|
2021-03-11 10:10:57 +00:00
|
|
|
|
2024-01-06 14:26:56 +00:00
|
|
|
proc init*(T: type BeaconClock, genesis_time: uint64): Opt[T] =
|
|
|
|
# Since we'll be converting beacon time differences to nanoseconds,
|
|
|
|
# the time can't be outrageously far from now
|
2024-01-16 01:26:18 +00:00
|
|
|
if genesis_time > (getTime().toUnix().uint64 + 100'u64 * 365'u64 * 24'u64 *
|
|
|
|
60'u64 * 60'u64) or
|
2024-01-06 14:26:56 +00:00
|
|
|
genesis_time < GENESIS_SLOT * SECONDS_PER_SLOT:
|
|
|
|
Opt.none(BeaconClock)
|
|
|
|
else:
|
|
|
|
let
|
|
|
|
unixGenesis = fromUnix(genesis_time.int64)
|
|
|
|
# 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))
|
2021-03-04 09:13:23 +00:00
|
|
|
|
2024-01-06 14:26:56 +00:00
|
|
|
Opt.some 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:
|
2023-08-10 12:52:49 +00:00
|
|
|
(true, nanoseconds((t - now).nanoseconds))
|
2019-03-22 15:49:37 +00:00
|
|
|
else:
|
2023-08-10 12:52:49 +00:00
|
|
|
(false, 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 =
|
2023-04-17 21:31:54 +00:00
|
|
|
let
|
|
|
|
currentTime = c.now()
|
|
|
|
currentSlot = currentTime.toSlot()
|
|
|
|
|
|
|
|
if currentSlot.afterGenesis:
|
|
|
|
let nextSlot = currentSlot.slot + 1
|
2023-08-10 12:52:49 +00:00
|
|
|
nanoseconds(
|
2023-04-17 21:31:54 +00:00
|
|
|
(nextSlot.start_beacon_time() - currentTime).nanoseconds)
|
2021-07-13 11:15:07 +00:00
|
|
|
else:
|
2023-04-17 21:31:54 +00:00
|
|
|
# absoluteTime = BeaconTime(-currentTime.ns_since_genesis).
|
|
|
|
let
|
|
|
|
absoluteTime = Slot(0).start_beacon_time() +
|
|
|
|
(Slot(0).start_beacon_time() - currentTime)
|
|
|
|
timeToNextSlot = absoluteTime - currentSlot.slot.start_beacon_time()
|
2023-08-10 12:52:49 +00:00
|
|
|
nanoseconds(timeToNextSlot.nanoseconds)
|
2021-07-13 11:15:07 +00:00
|
|
|
|
|
|
|
proc durationToNextEpoch*(c: BeaconClock): Duration =
|
2023-04-17 21:31:54 +00:00
|
|
|
let
|
|
|
|
currentTime = c.now()
|
|
|
|
currentSlot = currentTime.toSlot()
|
|
|
|
|
|
|
|
if currentSlot.afterGenesis:
|
|
|
|
let nextEpochSlot = (currentSlot.slot.epoch() + 1).start_slot()
|
2023-08-10 12:52:49 +00:00
|
|
|
nanoseconds(
|
2023-04-17 21:31:54 +00:00
|
|
|
(nextEpochSlot.start_beacon_time() - currentTime).nanoseconds)
|
2021-07-13 11:15:07 +00:00
|
|
|
else:
|
2023-04-17 21:31:54 +00:00
|
|
|
# absoluteTime = BeaconTime(-currentTime.ns_since_genesis).
|
|
|
|
let
|
|
|
|
absoluteTime = Slot(0).start_beacon_time() +
|
|
|
|
(Slot(0).start_beacon_time() - currentTime)
|
|
|
|
timeToNextEpoch = absoluteTime -
|
|
|
|
currentSlot.slot.epoch().start_slot().start_beacon_time()
|
2023-08-10 12:52:49 +00:00
|
|
|
nanoseconds(timeToNextEpoch.nanoseconds)
|
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
|
|
|
|
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
|
2024-02-19 10:00:11 +00:00
|
|
|
|
|
|
|
const MinSignificantProcessingDuration* = 250.millis
|