Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: more log on updateHeadState() #6997

Merged
merged 3 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/beacon-node/src/chain/blocks/importBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ export async function importBlock(

if (newHead.blockRoot !== oldHead.blockRoot) {
// Set head state as strong reference
this.regen.updateHeadState(newHead.stateRoot, postState);
this.regen.updateHeadState(newHead, postState);

this.emitter.emit(routes.events.EventType.head, {
block: newHead.blockRoot,
Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/src/chain/regen/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface IStateRegenerator extends IStateRegeneratorInternal {
pruneOnFinalized(finalizedEpoch: Epoch): void;
processState(blockRootHex: RootHex, postState: CachedBeaconStateAllForks): void;
addCheckpointState(cp: phase0.Checkpoint, item: CachedBeaconStateAllForks): void;
updateHeadState(newHeadStateRoot: RootHex, maybeHeadState: CachedBeaconStateAllForks): void;
updateHeadState(newHead: ProtoBlock, maybeHeadState: CachedBeaconStateAllForks): void;
updatePreComputedCheckpoint(rootHex: RootHex, epoch: Epoch): number | null;
}

Expand Down
36 changes: 24 additions & 12 deletions packages/beacon-node/src/chain/regen/queued.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,28 +165,40 @@ export class QueuedStateRegenerator implements IStateRegenerator {
this.checkpointStateCache.add(cp, item);
}

updateHeadState(newHeadStateRoot: RootHex, maybeHeadState: CachedBeaconStateAllForks): void {
// the resulting state will be added to block state cache so we transfer the cache in this flow
const cloneOpts = {dontTransferCache: true};
updateHeadState(newHead: ProtoBlock, maybeHeadState: CachedBeaconStateAllForks): void {
const {stateRoot: newHeadStateRoot, blockRoot: newHeadBlockRoot, slot: newHeadSlot} = newHead;
const maybeHeadStateRoot = toHexString(maybeHeadState.hashTreeRoot());
const logCtx = {
newHeadSlot,
newHeadBlockRoot,
newHeadStateRoot,
maybeHeadSlot: maybeHeadState.slot,
maybeHeadStateRoot,
};
const headState =
newHeadStateRoot === toHexString(maybeHeadState.hashTreeRoot())
newHeadStateRoot === maybeHeadStateRoot
? maybeHeadState
: this.blockStateCache.get(newHeadStateRoot, cloneOpts);
: // maybeHeadState was already in block state cache so we don't transfer the cache
this.blockStateCache.get(newHeadStateRoot, {dontTransferCache: true});

if (headState) {
this.blockStateCache.setHeadState(headState);
} else {
// Trigger regen on head change if necessary
this.logger.warn("Head state not available, triggering regen", {stateRoot: newHeadStateRoot});
// it's important to reload state to regen head state here
const allowDiskReload = true;
// head has changed, so the existing cached head state is no longer useful. Set strong reference to null to free
// up memory for regen step below. During regen, node won't be functional but eventually head will be available
// for legacy StateContextCache only
this.logger.warn("Head state not available, triggering regen", logCtx);
// for the old BlockStateCacheImpl only
// - head has changed, so the existing cached head state is no longer useful. Set strong reference to null to free
// up memory for regen step below. During regen, node won't be functional but eventually head will be available
// for the new FIFOBlockStateCache, this has no affect
this.blockStateCache.setHeadState(null);

// for the new FIFOBlockStateCache, it's important to reload state to regen head state here if needed
const allowDiskReload = true;
// transfer cache here because we want to regen state asap
const cloneOpts = {dontTransferCache: false};
this.regen.getState(newHeadStateRoot, RegenCaller.processBlock, cloneOpts, allowDiskReload).then(
(headStateRegen) => this.blockStateCache.setHeadState(headStateRegen),
(e) => this.logger.error("Error on head state regen", {}, e)
(e) => this.logger.error("Error on head state regen", logCtx, e)
);
}
}
Expand Down
Loading