2018-11-23 23:58:49 +00:00
|
|
|
import
|
2019-02-06 17:56:04 +00:00
|
|
|
chronos,
|
2019-03-22 15:49:37 +00:00
|
|
|
spec/[datatypes]
|
|
|
|
|
|
|
|
from times import Time, getTime, fromUnix, `<`, `-`
|
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.
|
|
|
|
##
|
initial 0.9.0 spec sync (#509)
* rename compute_epoch_of_slot(...) to compute_epoch_at_slot(...)
* remove some unnecessary imports; remove some crosslink-related code and tests; complete renaming of compute_epoch_of_slot(...) to compute_epoch_at_slot(...)
* rm more transfer-related code and tests; rm more unnecessary strutils imports
* rm remaining unused imports
* remove useless get_empty_per_epoch_cache(...)/compute_start_slot_of_epoch(...) calls
* rename compute_start_slot_of_epoch(...) to compute_start_slot_at_epoch(...)
* rename ACTIVATION_EXIT_DELAY to MAX_SEED_LOOKAHEAD
* update domain types to 0.9.0
* mark AttesterSlashing, IndexedAttestation, AttestationDataAndCustodyBit, DepositData, BeaconBlockHeader, Fork, integer_squareroot(...), and process_voluntary_exit(...) as 0.9.0
* mark increase_balance(...), decrease_balance(...), get_block_root(...), CheckPoint, Deposit, PendingAttestation, HistoricalBatch, is_active_validator(...), and is_slashable_attestation_data(...) as 0.9.0
* mark compute_activation_exit_epoch(...), bls_verify(...), Validator, get_active_validator_indices(...), get_current_epoch(...), get_total_active_balance(...), and get_previous_epoch(...) as 0.9.0
* mark get_block_root_at_slot(...), ProposerSlashing, get_domain(...), VoluntaryExit, mainnet preset Gwei values, minimal preset max operations, process_block_header(...), and is_slashable_validator(...) as 0.9.0
* mark makeWithdrawalCredentials(...), get_validator_churn_limit(...), get_total_balance(...), is_valid_indexed_attestation(...), bls_aggregate_pubkeys(...), initial genesis value/constants, Attestation, get_randao_mix(...), mainnet preset max operations per block constants, minimal preset Gwei values and time parameters, process_eth1_data(...), get_shuffled_seq(...), compute_committee(...), and process_slots(...) as 0.9.0; partially update get_indexed_attestation(...) to 0.9.0 by removing crosslink refs and associated tests
* mark initiate_validator_exit(...), process_registry_updates(...), BeaconBlock, Eth1Data, compute_domain(...), process_randao(...), process_attester_slashing(...), get_base_reward(...), and process_slot(...) as 0.9.0
2019-10-30 19:41:19 +00:00
|
|
|
## https://github.com/ethereum/eth2.0-specs/blob/v0.9.0/specs/core/0_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
|
|
|
|
|
|
|
|
BeaconTime* = distinct int64 ## Seconds from beacon genesis time
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
let
|
|
|
|
unixGenesis = fromUnix(state.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))
|
|
|
|
|
|
|
|
T(genesis: unixGenesis - unixGenesisOffset)
|
2018-11-23 23:58:49 +00:00
|
|
|
|
2019-08-16 11:20:46 +00:00
|
|
|
template `<`*(a, b: BeaconTime): bool =
|
|
|
|
int64(a) < int64(b)
|
|
|
|
|
|
|
|
template `<=`*(a, b: BeaconTime): bool =
|
|
|
|
int64(a) <= int64(b)
|
|
|
|
|
2019-08-16 11:16:56 +00:00
|
|
|
func toSlot*(t: BeaconTime): tuple[afterGenesis: bool, slot: Slot] =
|
|
|
|
let ti = t.int64
|
|
|
|
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
|
|
|
|
2019-03-22 15:49:37 +00:00
|
|
|
func toBeaconTime*(c: BeaconClock, t: Time): BeaconTime =
|
2019-08-16 11:16:56 +00:00
|
|
|
BeaconTime(times.seconds(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
|
|
|
func toBeaconTime*(s: Slot, offset = chronos.seconds(0)): BeaconTime =
|
|
|
|
BeaconTime(int64(uint64(s) * SECONDS_PER_SLOT) + seconds(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()
|
2018-11-23 23:58:49 +00:00
|
|
|
|
2019-03-22 15:49:37 +00:00
|
|
|
if int64(t) > int64(now):
|
|
|
|
(true, seconds(int64(t) - int64(now)))
|
|
|
|
else:
|
|
|
|
(false, seconds(int64(now) - int64(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-03-22 15:49:37 +00:00
|
|
|
proc saturate*(d: tuple[inFuture: bool, offset: Duration]): Duration =
|
|
|
|
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) =
|
|
|
|
addTimer(Moment.now() + fromNow, cb, udata)
|
2018-11-23 23:58:49 +00:00
|
|
|
|
2019-08-16 11:16:56 +00:00
|
|
|
func shortLog*(d: Duration): string =
|
2019-03-22 15:49:37 +00:00
|
|
|
let dd = int64(d.milliseconds())
|
|
|
|
if dd < 1000:
|
|
|
|
$dd & "ms"
|
|
|
|
elif dd < 60 * 1000:
|
|
|
|
$(dd div 1000) & "s"
|
|
|
|
elif dd < 60 * 60 * 1000:
|
|
|
|
let secs = dd div 1000
|
|
|
|
var tmp = $(secs div 60) & "m"
|
|
|
|
if (let frac = secs mod 60; frac > 0):
|
|
|
|
tmp &= $frac & "s"
|
|
|
|
tmp
|
|
|
|
else:
|
|
|
|
let mins = dd div 60 * 1000
|
|
|
|
var tmp = $(mins div 60) & "h"
|
|
|
|
if (let frac = mins mod 60; frac > 0):
|
|
|
|
tmp &= $frac & "m"
|
|
|
|
tmp
|
2019-08-16 11:16:56 +00:00
|
|
|
|
2019-08-16 11:20:46 +00:00
|
|
|
func shortLog*(v: BeaconTime): int64 = v.int64
|