@lodestar/beacon-node
Version:
A Typescript implementation of the beacon chain
93 lines • 3.7 kB
TypeScript
import { routes } from "@lodestar/api";
import { IBeaconStateView } from "@lodestar/state-transition";
import { RootHex } from "@lodestar/types";
import { Metrics } from "../../metrics/index.js";
import { BlockStateCache } from "./types.js";
export type FIFOBlockStateCacheOpts = {
maxBlockStates?: number;
};
/**
* Given `maxSkipSlots` = 32 and `DEFAULT_EARLIEST_PERMISSIBLE_SLOT_DISTANCE` = 32, lodestar doesn't need to
* reload states in order to process a gossip block.
*
* |-----------------------------------------------|-----------------------------------------------|
* maxSkipSlots DEFAULT_EARLIEST_PERMISSIBLE_SLOT_DISTANCE ^
* clock slot
*/
export declare const DEFAULT_MAX_BLOCK_STATES = 64;
/**
* New implementation of BlockStateCache that keeps the most recent n states consistently
* - Maintain a linked list (FIFO) with special handling for head state, which is always the first item in the list
* - Prune per add() instead of per checkpoint so it only keeps n historical states consistently, prune from tail
* - No need to prune per finalized checkpoint
*
* Given this block tree with Block 11 as head:
* ```
Block 10
|
+-----+-----+
| |
Block 11 Block 12
^ |
| |
head Block 13
* ```
* The maintained key order would be: 11 -> 13 -> 12 -> 10, and state 10 will be pruned first.
*/
export declare class FIFOBlockStateCache implements BlockStateCache {
readonly maxStates: number;
private readonly cache;
/**
* Key order to implement FIFO cache
*/
private readonly keyOrder;
private readonly metrics;
constructor(opts: FIFOBlockStateCacheOpts, { metrics }: {
metrics?: Metrics | null;
});
/**
* Set a state as head, happens when importing a block and head block is changed.
*/
setHeadState(item: IBeaconStateView | null): void;
/**
* Get a seed state for state reload, this could be any states. The goal is to have the same
* base merkle tree for all BeaconState objects across application.
* See packages/state-transition/src/util/loadState/loadState.ts for more detail
*/
getSeedState(): IBeaconStateView;
/**
* Get a state from this cache given a state root hex.
*/
get(rootHex: RootHex): IBeaconStateView | null;
/**
* Add a state to this cache.
* @param isHead if true, move it to the head of the list. Otherwise add to the 2nd position.
* In importBlock() steps, normally it'll call add() with isHead = false first. Then call setHeadState() to set the head.
*/
add(item: IBeaconStateView, isHead?: boolean): void;
get size(): number;
/**
* Prune the cache from tail to keep the most recent n states consistently.
* The tail of the list is the oldest state, in case regen adds back the same state,
* it should stay next to head so that it won't be pruned right away.
* The FIFO cache helps with this.
*/
prune(lastAddedKey: string): void;
/**
* No need for this implementation
* This is only to conform to the old api
*/
deleteAllBeforeEpoch(): void;
/**
* ONLY FOR DEBUGGING PURPOSES. For lodestar debug API.
*/
clear(): void;
/** ONLY FOR DEBUGGING PURPOSES. For lodestar debug API */
dumpSummary(): routes.lodestar.StateCacheItem[];
getStates(): IterableIterator<IBeaconStateView>;
/**
* For unit test only.
*/
dumpKeyOrder(): string[];
}
//# sourceMappingURL=fifoBlockStateCache.d.ts.map