Skip to content

Commit

Permalink
Merge 2474817 into e6c559f
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Jul 1, 2024
2 parents e6c559f + 2474817 commit d94a6e1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
48 changes: 24 additions & 24 deletions packages/beacon-node/src/chain/prepareNextSlot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,26 +115,6 @@ export class PrepareNextSlotScheduler {
RegenCaller.precomputeEpoch
);

// assuming there is no reorg, it caches the checkpoint state & helps avoid doing a full state transition in the next slot
// + when gossip block comes, we need to validate and run state transition
// + if next slot is a skipped slot, it'd help getting target checkpoint state faster to validate attestations
if (isEpochTransition) {
this.metrics?.precomputeNextEpochTransition.count.inc({result: "success"}, 1);
const previousHits = this.chain.regen.updatePreComputedCheckpoint(headRoot, nextEpoch);
if (previousHits === 0) {
this.metrics?.precomputeNextEpochTransition.waste.inc();
}
this.metrics?.precomputeNextEpochTransition.hits.set(previousHits ?? 0);
this.logger.verbose("Completed PrepareNextSlotScheduler epoch transition", {
nextEpoch,
headSlot,
prepareSlot,
previousHits,
});

precomputeEpochTransitionTimer?.();
}

if (isExecutionStateType(prepareState)) {
const proposerIndex = prepareState.epochCtx.getBeaconProposer(prepareSlot);
const feeRecipient = this.chain.beaconProposerCache.get(proposerIndex);
Expand Down Expand Up @@ -198,7 +178,7 @@ export class PrepareNextSlotScheduler {
});
}

this.computeStateHashTreeRoot(updatedPrepareState);
this.computeStateHashTreeRoot(updatedPrepareState, isEpochTransition);

// If emitPayloadAttributes is true emit a SSE payloadAttributes event
if (this.chain.opts.emitPayloadAttributes === true) {
Expand All @@ -213,7 +193,27 @@ export class PrepareNextSlotScheduler {
this.chain.emitter.emit(routes.events.EventType.payloadAttributes, {data, version: fork});
}
} else {
this.computeStateHashTreeRoot(prepareState);
this.computeStateHashTreeRoot(prepareState, isEpochTransition);
}

// assuming there is no reorg, it caches the checkpoint state & helps avoid doing a full state transition in the next slot
// + when gossip block comes, we need to validate and run state transition
// + if next slot is a skipped slot, it'd help getting target checkpoint state faster to validate attestations
if (isEpochTransition) {
this.metrics?.precomputeNextEpochTransition.count.inc({result: "success"}, 1);
const previousHits = this.chain.regen.updatePreComputedCheckpoint(headRoot, nextEpoch);
if (previousHits === 0) {
this.metrics?.precomputeNextEpochTransition.waste.inc();
}
this.metrics?.precomputeNextEpochTransition.hits.set(previousHits ?? 0);
this.logger.verbose("Completed PrepareNextSlotScheduler epoch transition", {
nextEpoch,
headSlot,
prepareSlot,
previousHits,
});

precomputeEpochTransitionTimer?.();
}
} catch (e) {
if (!isErrorAborted(e) && !isQueueErrorAborted(e)) {
Expand All @@ -223,11 +223,11 @@ export class PrepareNextSlotScheduler {
}
};

computeStateHashTreeRoot(state: CachedBeaconStateAllForks): void {
computeStateHashTreeRoot(state: CachedBeaconStateAllForks, isEpochTransition: boolean): void {
// cache HashObjects for faster hashTreeRoot() later, especially for computeNewStateRoot() if we need to produce a block at slot 0 of epoch
// see https://github.com/ChainSafe/lodestar/issues/6194
const hashTreeRootTimer = this.metrics?.stateHashTreeRootTime.startTimer({
source: StateHashTreeRootSource.prepareNextSlot,
source: isEpochTransition ? StateHashTreeRootSource.prepareNextEpoch : StateHashTreeRootSource.prepareNextSlot,
});
state.hashTreeRoot();
hashTreeRootTimer?.();
Expand Down
1 change: 1 addition & 0 deletions packages/state-transition/src/stateTransition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export enum StateHashTreeRootSource {
stateTransition = "state_transition",
blockTransition = "block_transition",
prepareNextSlot = "prepare_next_slot",
prepareNextEpoch = "prepare_next_epoch",
computeNewStateRoot = "compute_new_state_root",
}

Expand Down

0 comments on commit d94a6e1

Please sign in to comment.