import {Epoch, Root, Slot, phase0} from "@lodestar/types"; import {IBeaconStateView} from "../stateView/interface.js"; /** * Cache to prevent accessing the state tree to fetch block roots repeteadly. * In normal network conditions the same root is read multiple times, specially the target. */ export class RootCache { readonly currentJustifiedCheckpoint: phase0.Checkpoint; readonly previousJustifiedCheckpoint: phase0.Checkpoint; private readonly blockRootEpochCache = new Map(); private readonly blockRootSlotCache = new Map(); constructor(private readonly state: IBeaconStateView) { this.currentJustifiedCheckpoint = state.currentJustifiedCheckpoint; this.previousJustifiedCheckpoint = state.previousJustifiedCheckpoint; } getBlockRoot(epoch: Epoch): Root { let root = this.blockRootEpochCache.get(epoch); if (!root) { root = this.state.getBlockRootAtEpoch(epoch); this.blockRootEpochCache.set(epoch, root); } return root; } getBlockRootAtSlot(slot: Slot): Root { let root = this.blockRootSlotCache.get(slot); if (!root) { root = this.state.getBlockRootAtSlot(slot); this.blockRootSlotCache.set(slot, root); } return root; } }