Skip to content

Commit

Permalink
feat: nHistoricalStatesFileDataStore flag
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Jan 5, 2024
1 parent efeaced commit 6caee55
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 12 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,3 @@ packages/cli/.git-data.json
dictionary.dic

temp/

checkpoint_states/
8 changes: 5 additions & 3 deletions packages/beacon-node/src/chain/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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});
Expand All @@ -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
)
Expand Down
2 changes: 2 additions & 0 deletions packages/beacon-node/src/chain/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export type IChainOptions = BlockProcessOpts &
broadcastValidationStrictness?: string;
minSameMessageSignatureSetsToBatch: number;
nHistoricalStates?: boolean;
nHistoricalStatesFileDataStore?: boolean;
};

export type BlockProcessOpts = {
Expand Down Expand Up @@ -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,
};
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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.
Expand All @@ -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<unknown> | void)[] = [];
afterEach(async () => {
while (afterEachCallbacks.length > 0) {
Expand Down
11 changes: 11 additions & 0 deletions packages/cli/src/options/beaconNodeOptions/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down Expand Up @@ -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,
};
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/test/unit/options/beaconNodeOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -143,6 +144,7 @@ describe("options / beaconNodeOptions", () => {
minSameMessageSignatureSetsToBatch: 32,
maxShufflingCacheEpochs: 100,
nHistoricalStates: true,
nHistoricalStatesFileDataStore: true,
maxBlockStates: 100,
maxCPStateEpochsInMemory: 100,
},
Expand Down

0 comments on commit 6caee55

Please sign in to comment.