diff --git a/.gitignore b/.gitignore index f7a52e17aca7..a0deed473c4a 100644 --- a/.gitignore +++ b/.gitignore @@ -77,5 +77,3 @@ packages/cli/.git-data.json dictionary.dic temp/ - -checkpoint_states/ diff --git a/packages/beacon-node/src/chain/chain.ts b/packages/beacon-node/src/chain/chain.ts index f2ea3d45a376..cbb4b59af041 100644 --- a/packages/beacon-node/src/chain/chain.ts +++ b/packages/beacon-node/src/chain/chain.ts @@ -85,6 +85,7 @@ import {InMemoryCheckpointStateCache} from "./stateCache/stateContextCheckpoints import {FIFOBlockStateCache} from "./stateCache/fifoBlockStateCache.js"; import {PersistentCheckpointStateCache} from "./stateCache/persistentCheckpointsCache.js"; import {CHECKPOINT_STATES_FOLDER, FileCPStateDatastore} from "./stateCache/datastore/file.js"; +import {DbCPStateDatastore} from "./stateCache/datastore/db.js"; /** * Arbitrary constants, blobs and payloads should be consumed immediately in the same slot @@ -242,6 +243,7 @@ export class BeaconChain implements IBeaconChain { this.pubkey2index = cachedState.epochCtx.pubkey2index; this.index2pubkey = cachedState.epochCtx.index2pubkey; + const fileDataStore = opts.nHistoricalStatesFileDataStore ?? false; const stateCache = this.opts.nHistoricalStates ? new FIFOBlockStateCache(this.opts, {metrics}) : new StateContextCache({metrics}); @@ -254,9 +256,9 @@ export class BeaconChain implements IBeaconChain { shufflingCache: this.shufflingCache, getHeadState: this.getHeadState.bind(this), bufferPool: new BufferPool(anchorState.type.tree_serializedSize(anchorState.node), metrics), - // datastore: new DbCPStateDatastore(this.db), - // TODO: add new flag - datastore: new FileCPStateDatastore(CHECKPOINT_STATES_FOLDER), + datastore: fileDataStore + ? new FileCPStateDatastore(CHECKPOINT_STATES_FOLDER) + : new DbCPStateDatastore(this.db), }, this.opts ) diff --git a/packages/beacon-node/src/chain/options.ts b/packages/beacon-node/src/chain/options.ts index de38f0d0f4ce..53aef52a19ad 100644 --- a/packages/beacon-node/src/chain/options.ts +++ b/packages/beacon-node/src/chain/options.ts @@ -36,6 +36,7 @@ export type IChainOptions = BlockProcessOpts & broadcastValidationStrictness?: string; minSameMessageSignatureSetsToBatch: number; nHistoricalStates?: boolean; + nHistoricalStatesFileDataStore?: boolean; }; export type BlockProcessOpts = { @@ -109,6 +110,7 @@ export const defaultChainOptions: IChainOptions = { // since this batch attestation work is designed to work with useWorker=true, make this the lowest value minSameMessageSignatureSetsToBatch: 2, nHistoricalStates: false, + nHistoricalStatesFileDataStore: false, maxBlockStates: DEFAULT_MAX_BLOCK_STATES, maxCPStateEpochsInMemory: DEFAULT_MAX_CP_STATE_EPOCHS_IN_MEMORY, }; diff --git a/packages/beacon-node/test/e2e/chain/stateCache/nHistoricalStates.test.ts b/packages/beacon-node/test/e2e/chain/stateCache/nHistoricalStates.test.ts index 368d70b05a4f..96f688facfcf 100644 --- a/packages/beacon-node/test/e2e/chain/stateCache/nHistoricalStates.test.ts +++ b/packages/beacon-node/test/e2e/chain/stateCache/nHistoricalStates.test.ts @@ -1,5 +1,4 @@ -import fs from "node:fs"; -import {describe, it, afterEach, beforeEach, expect} from "vitest"; +import {describe, it, afterEach, expect} from "vitest"; import {Gauge, Histogram} from "prom-client"; import {ChainConfig} from "@lodestar/config"; import {Slot, phase0} from "@lodestar/types"; @@ -14,7 +13,6 @@ import {ChainEvent, ReorgEventData} from "../../../../src/chain/emitter.js"; import {ReorgedForkChoice} from "../../../utils/mocks/forkchoice.js"; import {connect} from "../../../utils/network.js"; import {CacheItemType} from "../../../../src/chain/stateCache/types.js"; -import {CHECKPOINT_STATES_FOLDER} from "../../../../src/chain/stateCache/datastore/file.js"; /** * Test different reorg scenarios to make sure the StateCache implementations are correct. @@ -28,10 +26,6 @@ describe( SECONDS_PER_SLOT: 2, }; - beforeEach(async () => { - await fs.promises.rm(CHECKPOINT_STATES_FOLDER, {recursive: true, force: true}); - }); - const afterEachCallbacks: (() => Promise | void)[] = []; afterEach(async () => { while (afterEachCallbacks.length > 0) { diff --git a/packages/cli/src/options/beaconNodeOptions/chain.ts b/packages/cli/src/options/beaconNodeOptions/chain.ts index 3beb9db2ed2d..331619846b33 100644 --- a/packages/cli/src/options/beaconNodeOptions/chain.ts +++ b/packages/cli/src/options/beaconNodeOptions/chain.ts @@ -27,6 +27,7 @@ export type ChainArgs = { "chain.minSameMessageSignatureSetsToBatch"?: number; "chain.maxShufflingCacheEpochs"?: number; "chain.nHistoricalStates"?: boolean; + "chain.nHistoricalStatesFileDataStore"?: boolean; "chain.maxBlockStates"?: number; "chain.maxCPStateEpochsInMemory"?: number; }; @@ -57,6 +58,8 @@ export function parseArgs(args: ChainArgs): IBeaconNodeOptions["chain"] { args["chain.minSameMessageSignatureSetsToBatch"] ?? defaultOptions.chain.minSameMessageSignatureSetsToBatch, maxShufflingCacheEpochs: args["chain.maxShufflingCacheEpochs"] ?? defaultOptions.chain.maxShufflingCacheEpochs, nHistoricalStates: args["chain.nHistoricalStates"] ?? defaultOptions.chain.nHistoricalStates, + nHistoricalStatesFileDataStore: + args["chain.nHistoricalStatesFileDataStore"] ?? defaultOptions.chain.nHistoricalStatesFileDataStore, maxBlockStates: args["chain.maxBlockStates"] ?? defaultOptions.chain.maxBlockStates, maxCPStateEpochsInMemory: args["chain.maxCPStateEpochsInMemory"] ?? defaultOptions.chain.maxCPStateEpochsInMemory, }; @@ -228,6 +231,14 @@ Will double processing times. Use only for debugging purposes.", group: "chain", }, + "chain.nHistoricalStatesFileDataStore": { + hidden: true, + description: "Use fs to store checkpoint state for PersistentCheckpointStateCache or not", + type: "boolean", + default: defaultOptions.chain.nHistoricalStatesFileDataStore, + group: "chain", + }, + "chain.maxBlockStates": { hidden: true, description: "Max block states to cache in memory, used for FIFOBlockStateCache", diff --git a/packages/cli/test/unit/options/beaconNodeOptions.test.ts b/packages/cli/test/unit/options/beaconNodeOptions.test.ts index 4bc6c99e0d0a..35247d1e165f 100644 --- a/packages/cli/test/unit/options/beaconNodeOptions.test.ts +++ b/packages/cli/test/unit/options/beaconNodeOptions.test.ts @@ -37,6 +37,7 @@ describe("options / beaconNodeOptions", () => { "chain.minSameMessageSignatureSetsToBatch": 32, "chain.maxShufflingCacheEpochs": 100, "chain.nHistoricalStates": true, + "chain.nHistoricalStatesFileDataStore": true, "chain.maxBlockStates": 100, "chain.maxCPStateEpochsInMemory": 100, emitPayloadAttributes: false, @@ -143,6 +144,7 @@ describe("options / beaconNodeOptions", () => { minSameMessageSignatureSetsToBatch: 32, maxShufflingCacheEpochs: 100, nHistoricalStates: true, + nHistoricalStatesFileDataStore: true, maxBlockStates: 100, maxCPStateEpochsInMemory: 100, },