Skip to content

Commit

Permalink
Add skipCreateStateCacheIfAvailable option
Browse files Browse the repository at this point in the history
  • Loading branch information
dapplion committed May 13, 2022
1 parent 4c8fd32 commit 63a0be6
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 15 deletions.
21 changes: 12 additions & 9 deletions packages/lodestar/src/chain/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,18 @@ export class BeaconChain implements IBeaconChain {
const stateCache = new StateContextCache({metrics});
const checkpointStateCache = new CheckpointStateCache({metrics});

// anchorState may already by a CachedBeaconState. If so, don't create the cache again,
// since deserializing all pubkeys takes ~30 seconds for 350k keys (mainnet 2022Q2).
const cachedState = isCachedBeaconState(anchorState)
? anchorState
: createCachedBeaconState(anchorState, {
config,
pubkey2index: new PubkeyIndexMap(),
index2pubkey: [],
});
// anchorState may already by a CachedBeaconState. If so, don't create the cache again, since deserializing all
// pubkeys takes ~30 seconds for 350k keys (mainnet 2022Q2).
// When the BeaconStateCache is created in eth1 genesis builder it may be incorrect. Until we can ensure that
// it's safe to re-use _ANY_ BeaconStateCache, this option is disabled by default and only used in tests.
const cachedState =
isCachedBeaconState(anchorState) && opts.skipCreateStateCacheIfAvailable
? anchorState
: createCachedBeaconState(anchorState, {
config,
pubkey2index: new PubkeyIndexMap(),
index2pubkey: [],
});

// Persist single global instance of state caches
this.pubkey2index = cachedState.epochCtx.pubkey2index;
Expand Down
1 change: 1 addition & 0 deletions packages/lodestar/src/chain/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type IChainOptions = BlockProcessOpts &
blsVerifyAllMultiThread?: boolean;
persistInvalidSszObjects?: boolean;
persistInvalidSszObjectsDir?: string;
skipCreateStateCacheIfAvailable?: boolean;
};

export type BlockProcessOpts = {
Expand Down
6 changes: 0 additions & 6 deletions packages/lodestar/src/node/utils/interop/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,5 @@ export function getInteropState(

state.genesisTime = genesisTime;

// Drop CachedBeaconState cache, it's not properly populated.
// Otherwise BeaconChain won't populate the cache properly and the shufflings and indexes will be wrong.
const stateTB = state as BeaconStateAllForks & Partial<typeof state>;
delete stateTB.epochCtx;
delete stateTB.config;

return state;
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ describe("verify+import blocks - range sync perf test", () => {
proposerBoostEnabled: true,
safeSlotsToImportOptimistically: SAFE_SLOTS_TO_IMPORT_OPTIMISTICALLY,
disableArchiveOnCheckpoint: true,
skipCreateStateCacheIfAvailable: true,
},
{
config: state.config,
Expand Down

0 comments on commit 63a0be6

Please sign in to comment.