2020-04-24 07:16:11 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2018-11-23 23:58:49 +00:00
|
|
|
import
|
2021-01-21 17:42:57 +00:00
|
|
|
chronos, chronicles,
|
2020-04-15 07:59:47 +00:00
|
|
|
spec/datatypes
|
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
|
|
|
|
|
|
|
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.
|
|
|
|
##
|
2020-11-09 14:18:55 +00:00
|
|
|
## https://github.com/ethereum/eth2.0-specs/blob/v1.0.0/specs/phase0/fork-choice.md#fork-choice
|
2019-03-22 15:49:37 +00:00
|
|
|
##
|
|
|
|
# TODO replace time in chronos with a proper unit type, then this code can
|
|
|
|
# follow:
|
|
|
|
# https://github.com/status-im/nim-chronos/issues/15
|
|
|
|
# TODO consider NTP and network-adjusted timestamps as outlined here:
|
|
|
|
# https://ethresear.ch/t/network-adjusted-timestamps/4187
|
|
|
|
genesis: Time
|
|
|
|
|
2020-08-27 07:34:12 +00:00
|
|
|
BeaconTime* = distinct Duration ## Nanoseconds from beacon genesis time
|
2019-03-22 15:49:37 +00:00
|
|
|
|
2020-05-27 17:06:28 +00:00
|
|
|
proc init*(T: type BeaconClock, genesis_time: uint64): T =
|
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
|
|
|
|
2020-05-27 17:06:28 +00:00
|
|
|
proc init*(T: type BeaconClock, state: BeaconState): T =
|
|
|
|
## Initialize time from a beacon state. The genesis time of a beacon state is
|
|
|
|
## constant throughout its lifetime, so the state from any slot will do,
|
|
|
|
## including the genesis state.
|
|
|
|
BeaconClock.init(state.genesis_time)
|
|
|
|
|
2019-08-16 11:20:46 +00:00
|
|
|
template `<`*(a, b: BeaconTime): bool =
|
2020-08-27 07:34:12 +00:00
|
|
|
Duration(a) < Duration(b)
|
2019-08-16 11:20:46 +00:00
|
|
|
|
|
|
|
template `<=`*(a, b: BeaconTime): bool =
|
2020-08-27 07:34:12 +00:00
|
|
|
Duration(a) <= Duration(b)
|
|
|
|
|
|
|
|
template `+`*(t: BeaconTime, offset: Duration): BeaconTime =
|
|
|
|
BeaconTime(Duration(t) + offset)
|
|
|
|
|
|
|
|
template `-`*(t: BeaconTime, offset: Duration): BeaconTime =
|
2020-11-24 23:39:52 +00:00
|
|
|
BeaconTime(nanoseconds(nanoseconds(Duration(t)) - nanoseconds(offset)))
|
2020-08-27 07:34:12 +00:00
|
|
|
|
|
|
|
template `-`*(a, b: BeaconTime): Duration =
|
2020-11-24 23:39:52 +00:00
|
|
|
nanoseconds(nanoseconds(Duration(a)) - nanoseconds(Duration(b)))
|
2019-08-16 11:20:46 +00:00
|
|
|
|
2019-08-16 11:16:56 +00:00
|
|
|
func toSlot*(t: BeaconTime): tuple[afterGenesis: bool, slot: Slot] =
|
2020-08-27 07:34:12 +00:00
|
|
|
let ti = seconds(Duration(t))
|
2019-08-16 11:16:56 +00:00
|
|
|
if ti >= 0:
|
|
|
|
(true, Slot(uint64(ti) div SECONDS_PER_SLOT))
|
|
|
|
else:
|
|
|
|
(false, Slot(uint64(-ti) div SECONDS_PER_SLOT))
|
2018-11-23 23:58:49 +00:00
|
|
|
|
2020-05-22 17:04:52 +00:00
|
|
|
func slotOrZero*(time: BeaconTime): Slot =
|
|
|
|
let exSlot = time.toSlot
|
|
|
|
if exSlot.afterGenesis: exSlot.slot
|
|
|
|
else: Slot(0)
|
|
|
|
|
2019-03-22 15:49:37 +00:00
|
|
|
func toBeaconTime*(c: BeaconClock, t: Time): BeaconTime =
|
2020-08-27 07:34:12 +00:00
|
|
|
BeaconTime(nanoseconds(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
|
|
|
|
2020-08-27 07:34:12 +00:00
|
|
|
func toBeaconTime*(s: Slot, offset = Duration()): BeaconTime =
|
|
|
|
BeaconTime(seconds(int64(uint64(s) * SECONDS_PER_SLOT)) + offset)
|
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
|
|
|
|
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:
|
|
|
|
(true, t - now)
|
2019-03-22 15:49:37 +00:00
|
|
|
else:
|
2020-08-27 07:34:12 +00:00
|
|
|
(false, now - t)
|
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] =
|
|
|
|
c.fromNow(slot.toBeaconTime())
|
2018-11-23 23:58:49 +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-03-22 15:49:37 +00:00
|
|
|
proc addTimer*(fromNow: Duration, cb: CallbackFunc, udata: pointer = nil) =
|
2020-04-24 07:16:11 +00:00
|
|
|
try:
|
|
|
|
discard setTimer(Moment.now() + fromNow, cb, udata)
|
|
|
|
except Exception as e:
|
|
|
|
# TODO https://github.com/status-im/nim-chronos/issues/94
|
|
|
|
# shouldn't happen because we should have initialized chronos by now
|
|
|
|
# https://github.com/nim-lang/Nim/issues/10288 - sigh
|
|
|
|
raiseAssert e.msg
|
2018-11-23 23:58:49 +00:00
|
|
|
|
2021-01-21 17:42:57 +00:00
|
|
|
func humaneStr*(a: Duration): string {.inline.} =
|
|
|
|
## Returns a "humane" string representation of the duration that uses
|
|
|
|
## the most appropriate unit type depending on the actual Duration.
|
|
|
|
var v = a.nanoseconds
|
|
|
|
let days = v div nanoseconds(Day)
|
|
|
|
if days > 0:
|
|
|
|
if days >= 14:
|
|
|
|
return $(v div nanoseconds(Week)) & " weeks"
|
|
|
|
elif days >= 2:
|
|
|
|
return $days & " days"
|
|
|
|
|
|
|
|
let hours = v div nanoseconds(Hour)
|
|
|
|
if hours > 0:
|
|
|
|
if hours >= 2:
|
|
|
|
result = $hours & " hours"
|
|
|
|
else:
|
|
|
|
result = "1 hour"
|
|
|
|
v = v mod nanoseconds(Hour)
|
|
|
|
|
|
|
|
let minutes = v div nanoseconds(Minute)
|
|
|
|
if minutes > 0:
|
|
|
|
if hours > 0:
|
|
|
|
result.add " "
|
|
|
|
if minutes >= 2:
|
|
|
|
result.add $minutes
|
|
|
|
result.add " minutes"
|
|
|
|
else:
|
|
|
|
result.add "1 minute"
|
|
|
|
v = v mod nanoseconds(Minute)
|
|
|
|
|
|
|
|
let
|
|
|
|
isMoreThanAMinute = minutes > 0 or hours > 0
|
|
|
|
seconds = v div nanoseconds(Second)
|
|
|
|
|
|
|
|
if seconds > 0:
|
|
|
|
if isMoreThanAMinute:
|
|
|
|
result.add " "
|
|
|
|
if seconds >= 2:
|
|
|
|
result.add $seconds
|
|
|
|
result.add " seconds"
|
|
|
|
else:
|
|
|
|
result.add "1 second"
|
|
|
|
else:
|
|
|
|
result = $seconds
|
|
|
|
v = v mod nanoseconds(Second)
|
|
|
|
let milliseconds = v div nanoseconds(Millisecond)
|
|
|
|
if milliseconds > 0:
|
|
|
|
let millisecondsStr = $milliseconds
|
|
|
|
result.add "."
|
|
|
|
for _ in millisecondsStr.len ..< 3: result.add "0"
|
|
|
|
result.add millisecondsStr
|
|
|
|
elif seconds == 1:
|
|
|
|
return "1 second"
|
|
|
|
result.add " seconds"
|
|
|
|
return
|
|
|
|
|
|
|
|
if isMoreThanAMinute:
|
|
|
|
return
|
|
|
|
|
|
|
|
let milliseconds = v div nanoseconds(Millisecond)
|
|
|
|
if milliseconds >= 2:
|
|
|
|
return $milliseconds & " milliseconds"
|
|
|
|
|
|
|
|
let microseconds = v div nanoseconds(Microsecond)
|
|
|
|
if microseconds >= 2:
|
|
|
|
return $microseconds & " microseconds"
|
|
|
|
|
|
|
|
let nanoseconds = v div nanoseconds(Nanosecond)
|
|
|
|
if nanoseconds == 1:
|
|
|
|
return "1 nanosecond"
|
|
|
|
|
|
|
|
return $nanoseconds & " nanoseconds"
|
|
|
|
|
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
|
|
|
|
|
2020-08-27 07:34:12 +00:00
|
|
|
func `$`*(v: BeaconTime): string = $Duration(v)
|
2021-01-21 17:42:57 +00:00
|
|
|
func shortLog*(v: BeaconTime): string = humaneStr Duration(v)
|
|
|
|
|
|
|
|
chronicles.formatIt Duration: humaneStr(it)
|
|
|
|
chronicles.formatIt BeaconTime: humaneStr(Duration it)
|
|
|
|
|