UNPKG

@lodestar/beacon-node

Version:

A Typescript implementation of the beacon chain

90 lines 3.51 kB
import EventEmitter from "node:events"; import { ChainForkConfig } from "@lodestar/config"; import type { Epoch, Slot } from "@lodestar/types"; import type { StrictEventEmitter } from "strict-event-emitter-types"; export declare enum ClockEvent { /** * This event signals the start of a new slot, and that subsequent calls to `clock.currentSlot` will equal `slot`. * This event is guaranteed to be emitted every `SECONDS_PER_SLOT` seconds. */ slot = "clock:slot", /** * This event signals the start of a new epoch, and that subsequent calls to `clock.currentEpoch` will return `epoch`. * This event is guaranteed to be emitted every `SECONDS_PER_SLOT * SLOTS_PER_EPOCH` seconds. */ epoch = "clock:epoch" } export type ClockEvents = { [ClockEvent.slot]: (slot: Slot) => void; [ClockEvent.epoch]: (epoch: Epoch) => void; }; /** * Tracks the current chain time, measured in `Slot`s and `Epoch`s * * The time is dependent on: * - `state.genesisTime` - the genesis time * - `SECONDS_PER_SLOT` - # of seconds per slot * - `SLOTS_PER_EPOCH` - # of slots per epoch */ export type IClock = StrictEventEmitter<EventEmitter, ClockEvents> & { readonly genesisTime: Slot; readonly currentSlot: Slot; /** * If it's too close to next slot, maxCurrentSlot = currentSlot + 1 */ readonly currentSlotWithGossipDisparity: Slot; readonly currentEpoch: Epoch; /** Returns the slot if the internal clock were advanced by `toleranceSec`. */ slotWithFutureTolerance(toleranceSec: number): Slot; /** Returns the slot if the internal clock were reversed by `toleranceSec`. */ slotWithPastTolerance(toleranceSec: number): Slot; /** * Check if a slot is current slot given MAXIMUM_GOSSIP_CLOCK_DISPARITY. */ isCurrentSlotGivenGossipDisparity(slot: Slot): boolean; /** * Returns a promise that waits until at least `slot` is reached * Resolves when the current slot >= `slot` * Rejects if the clock is aborted */ waitForSlot(slot: Slot): Promise<void>; /** * Return second from a slot to either toSec or now. */ secFromSlot(slot: Slot, toSec?: number): number; }; /** * A local clock, the clock time is assumed to be trusted */ export declare class Clock extends EventEmitter implements IClock { readonly genesisTime: number; private readonly config; private timeoutId; private readonly signal; private _currentSlot; constructor({ config, genesisTime, signal }: { config: ChainForkConfig; genesisTime: number; signal: AbortSignal; }); get currentSlot(): Slot; /** * If it's too close to next slot given MAXIMUM_GOSSIP_CLOCK_DISPARITY, return currentSlot + 1. * Otherwise return currentSlot */ get currentSlotWithGossipDisparity(): Slot; get currentEpoch(): Epoch; /** Returns the slot if the internal clock were advanced by `toleranceSec`. */ slotWithFutureTolerance(toleranceSec: number): Slot; /** Returns the slot if the internal clock were reversed by `toleranceSec`. */ slotWithPastTolerance(toleranceSec: number): Slot; /** * Check if a slot is current slot given MAXIMUM_GOSSIP_CLOCK_DISPARITY. */ isCurrentSlotGivenGossipDisparity(slot: Slot): boolean; waitForSlot(slot: Slot): Promise<void>; secFromSlot(slot: Slot, toSec?: number): number; private onNextSlot; private msUntilNextSlot; } //# sourceMappingURL=clock.d.ts.map