Skip to content

Commit

Permalink
Add chain.archiveStateEpochFrequency cli option (#5154)
Browse files Browse the repository at this point in the history
* Add chain.archiveStateEpochFrequency cli option

* Fix test

* Update packages/beacon-node/src/chain/archiver/archiveStates.ts

Co-authored-by: Nico Flaig <[email protected]>

---------

Co-authored-by: Nico Flaig <[email protected]>
  • Loading branch information
wemeetagain and nflaig authored Feb 19, 2023
1 parent 7a40fab commit 3857acc
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 13 deletions.
21 changes: 13 additions & 8 deletions packages/beacon-node/src/chain/archiver/archiveStates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ import {CheckpointWithHex} from "@lodestar/fork-choice";
import {IBeaconDb} from "../../db/index.js";
import {CheckpointStateCache} from "../stateCache/index.js";

/**
* Minimum number of epochs between archived states
*/
export const PERSIST_STATE_EVERY_EPOCHS = 1024;
/**
* Minimum number of epochs between single temp archived states
* These states will be pruned once a new state is persisted
*/
const PERSIST_TEMP_STATE_EVERY_EPOCHS = 32;

export interface StatesArchiverOpts {
/**
* Minimum number of epochs between archived states
*/
archiveStateEpochFrequency: number;
}

/**
* Archives finalized states from active bucket to archive bucket.
*
Expand All @@ -25,7 +28,8 @@ export class StatesArchiver {
constructor(
private readonly checkpointStateCache: CheckpointStateCache,
private readonly db: IBeaconDb,
private readonly logger: ILogger
private readonly logger: ILogger,
private readonly opts: StatesArchiverOpts
) {}

/**
Expand All @@ -43,22 +47,23 @@ export class StatesArchiver {
async maybeArchiveState(finalized: CheckpointWithHex): Promise<void> {
const lastStoredSlot = await this.db.stateArchive.lastKey();
const lastStoredEpoch = computeEpochAtSlot(lastStoredSlot ?? 0);
const {archiveStateEpochFrequency} = this.opts;

if (finalized.epoch - lastStoredEpoch > PERSIST_TEMP_STATE_EVERY_EPOCHS) {
if (finalized.epoch - lastStoredEpoch > Math.min(PERSIST_TEMP_STATE_EVERY_EPOCHS, archiveStateEpochFrequency)) {
await this.archiveState(finalized);

// Only check the current and previous intervals
const minEpoch = Math.max(
0,
(Math.floor(finalized.epoch / PERSIST_STATE_EVERY_EPOCHS) - 1) * PERSIST_STATE_EVERY_EPOCHS
(Math.floor(finalized.epoch / archiveStateEpochFrequency) - 1) * archiveStateEpochFrequency
);

const storedStateSlots = await this.db.stateArchive.keys({
lt: computeStartSlotAtEpoch(finalized.epoch),
gte: computeStartSlotAtEpoch(minEpoch),
});

const statesSlotsToDelete = computeStateSlotsToDelete(storedStateSlots, PERSIST_STATE_EVERY_EPOCHS);
const statesSlotsToDelete = computeStateSlotsToDelete(storedStateSlots, archiveStateEpochFrequency);
if (statesSlotsToDelete.length > 0) {
await this.db.stateArchive.batchDelete(statesSlotsToDelete);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/beacon-node/src/chain/archiver/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import {IBeaconDb} from "../../db/index.js";
import {JobItemQueue} from "../../util/queue/index.js";
import {IBeaconChain} from "../interface.js";
import {ChainEvent} from "../emitter.js";
import {StatesArchiver} from "./archiveStates.js";
import {StatesArchiver, StatesArchiverOpts} from "./archiveStates.js";
import {archiveBlocks} from "./archiveBlocks.js";

const PROCESS_FINALIZED_CHECKPOINT_QUEUE_LEN = 256;

export type ArchiverOpts = {
export type ArchiverOpts = StatesArchiverOpts & {
disableArchiveOnCheckpoint?: boolean;
};

Expand All @@ -30,7 +30,7 @@ export class Archiver {
signal: AbortSignal,
opts: ArchiverOpts
) {
this.statesArchiver = new StatesArchiver(chain.checkpointStateCache, db, logger);
this.statesArchiver = new StatesArchiver(chain.checkpointStateCache, db, logger, opts);
this.jobQueue = new JobItemQueue<[CheckpointWithHex], void>(this.processFinalizedCheckpoint, {
maxLength: PROCESS_FINALIZED_CHECKPOINT_QUEUE_LEN,
signal,
Expand Down
1 change: 1 addition & 0 deletions packages/beacon-node/src/chain/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,5 @@ export const defaultChainOptions: IChainOptions = {
safeSlotsToImportOptimistically: SAFE_SLOTS_TO_IMPORT_OPTIMISTICALLY,
suggestedFeeRecipient: defaultValidatorOptions.suggestedFeeRecipient,
assertCorrectProgressiveBalances: false,
archiveStateEpochFrequency: 1024,
};
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ describe.skip("verify+import blocks - range sync perf test", () => {
disableArchiveOnCheckpoint: true,
suggestedFeeRecipient: defaultValidatorOptions.suggestedFeeRecipient,
skipCreateStateCacheIfAvailable: true,
archiveStateEpochFrequency: 1024,
},
{
config: state.config,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
getStateValidatorIndex,
} from "../../../../../../src/api/impl/beacon/state/utils.js";
import {IBeaconChain} from "../../../../../../src/chain/index.js";
import {PERSIST_STATE_EVERY_EPOCHS} from "../../../../../../src/chain/archiver/archiveStates.js";
import {generateProtoBlock} from "../../../../../utils/typeGenerator.js";
import {generateCachedAltairState, generateCachedState, generateState} from "../../../../../utils/state.js";
import {StubbedBeaconDb} from "../../../../../utils/stub/index.js";
Expand Down Expand Up @@ -91,7 +90,7 @@ describe("beacon state api utils", function () {
});

it("resolve state on unarchived finalized slot", async function () {
const nearestArchiveSlot = PERSIST_STATE_EVERY_EPOCHS * SLOTS_PER_EPOCH;
const nearestArchiveSlot = 1024 * SLOTS_PER_EPOCH;
const finalizedEpoch = 1028;
const requestedSlot = 1026 * SLOTS_PER_EPOCH;

Expand Down
1 change: 1 addition & 0 deletions packages/beacon-node/test/utils/mocks/chain/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export class MockBeaconChain implements IBeaconChain {
proposerBoostEnabled: false,
safeSlotsToImportOptimistically: 0,
suggestedFeeRecipient: "0x0000000000000000000000000000000000000000",
archiveStateEpochFrequency: 1024,
};
readonly anchorStateLatestBlockSlot: Slot;

Expand Down
9 changes: 9 additions & 0 deletions packages/cli/src/options/beaconNodeOptions/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface IChainArgs {
"chain.assertCorrectProgressiveBalances": boolean;
"chain.maxSkipSlots": number;
"safe-slots-to-import-optimistically": number;
"chain.archiveStateEpochFrequency": number;
}

export function parseArgs(args: IChainArgs): IBeaconNodeOptions["chain"] {
Expand All @@ -35,6 +36,7 @@ export function parseArgs(args: IChainArgs): IBeaconNodeOptions["chain"] {
assertCorrectProgressiveBalances: args["chain.assertCorrectProgressiveBalances"],
maxSkipSlots: args["chain.maxSkipSlots"],
safeSlotsToImportOptimistically: args["safe-slots-to-import-optimistically"],
archiveStateEpochFrequency: args["chain.archiveStateEpochFrequency"],
};
}

Expand Down Expand Up @@ -133,4 +135,11 @@ Will double processing times. Use only for debugging purposes.",
defaultDescription: String(defaultOptions.chain.safeSlotsToImportOptimistically),
group: "chain",
},

"chain.archiveStateEpochFrequency": {
hidden: true,
description: "Minimum number of epochs between archived states",
type: "number",
group: "chain",
},
};
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 @@ -29,6 +29,7 @@ describe("options / beaconNodeOptions", () => {
"chain.assertCorrectProgressiveBalances": true,
"chain.maxSkipSlots": 100,
"safe-slots-to-import-optimistically": 256,
"chain.archiveStateEpochFrequency": 1024,

eth1: true,
"eth1.providerUrl": "http://my.node:8545",
Expand Down Expand Up @@ -112,6 +113,7 @@ describe("options / beaconNodeOptions", () => {
suggestedFeeRecipient: "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
assertCorrectProgressiveBalances: true,
maxSkipSlots: 100,
archiveStateEpochFrequency: 1024,
},
eth1: {
enabled: true,
Expand Down

0 comments on commit 3857acc

Please sign in to comment.