UNPKG

@lodestar/beacon-node

Version:

A Typescript implementation of the beacon chain

101 lines 4.11 kB
import EventEmitter from "node:events"; import type { StrictEventEmitter } from "strict-event-emitter-types"; import { ChainForkConfig } from "@lodestar/config"; import type { Epoch, Slot } from "@lodestar/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 `SLOT_DURATION_MS` milliseconds. */ 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 `SLOT_DURATION_MS * SLOTS_PER_EPOCH` milliseconds. */ 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 * - `SLOT_DURATION_MS` - # of milliseconds 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; /** * Return milliseconds from a slot to either toMs or now. */ msFromSlot(slot: Slot, toMs?: 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 * * Spec: phase0/p2p-interface.md - gossip validation uses `current_time + MAXIMUM_GOSSIP_CLOCK_DISPARITY < message_time` * to reject future messages (strict `<`), so the boundary (exactly equal) is accepted, hence `<=` here. */ 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. * * Uses `<=` for disparity checks because the spec rejects with strict `<` * (phase0/p2p-interface.md), meaning the boundary (exactly equal) is accepted. */ isCurrentSlotGivenGossipDisparity(slot: Slot): boolean; waitForSlot(slot: Slot): Promise<void>; secFromSlot(slot: Slot, toSec?: number): number; msFromSlot(slot: Slot, toMs?: number): number; private onNextSlot; private msUntilNextSlot; } //# sourceMappingURL=clock.d.ts.map