-
-
Notifications
You must be signed in to change notification settings - Fork 479
fix: avoid BeaconState commit() clone() in beacon-node #8728
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a96ecd7
fix: avoid BeaconState commit() clone() in beacon-node
twoeths 48f8490
chore: simplify computeBlockRewards()
twoeths 97e8f61
Merge remote-tracking branch 'origin/unstable' into te/avoid_beacon_s…
twoeths f7d9c5c
fix: load states in api where it's necessary
twoeths 42f853f
fix: return finalized Uint8Array state data in getStateByStateRoot
twoeths ab1454f
fix: remove stale comments in queue.ts
twoeths 253f589
fix: remove state.clone() in getProposerDuties api
twoeths File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,21 +79,15 @@ export class QueuedStateRegenerator implements IStateRegenerator { | |
|
|
||
| /** | ||
| * Get a state from block state cache. | ||
| * This is not for block processing so don't transfer cache | ||
| */ | ||
| getStateSync(stateRoot: RootHex): CachedBeaconStateAllForks | null { | ||
| return this.blockStateCache.get(stateRoot, {dontTransferCache: true}); | ||
| return this.blockStateCache.get(stateRoot); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the comment above explicitly notes that we don't transfer cache, either comment or code needs to be adapted |
||
| } | ||
|
|
||
| /** | ||
| * Get state for block processing. | ||
| * By default, do not transfer cache except for the block at clock slot | ||
| * which is usually the gossip block. | ||
| */ | ||
| getPreStateSync( | ||
| block: BeaconBlock, | ||
| opts: StateRegenerationOpts = {dontTransferCache: true} | ||
| ): CachedBeaconStateAllForks | null { | ||
| getPreStateSync(block: BeaconBlock): CachedBeaconStateAllForks | null { | ||
| const parentRoot = toRootHex(block.parentRoot); | ||
| const parentBlock = this.forkChoice.getBlockHex(parentRoot); | ||
| if (!parentBlock) { | ||
|
|
@@ -108,7 +102,7 @@ export class QueuedStateRegenerator implements IStateRegenerator { | |
|
|
||
| // Check the checkpoint cache (if the pre-state is a checkpoint state) | ||
| if (parentEpoch < blockEpoch) { | ||
| const checkpointState = this.checkpointStateCache.getLatest(parentRoot, blockEpoch, opts); | ||
| const checkpointState = this.checkpointStateCache.getLatest(parentRoot, blockEpoch); | ||
| if (checkpointState && computeEpochAtSlot(checkpointState.slot) === blockEpoch) { | ||
| return checkpointState; | ||
| } | ||
|
|
@@ -118,7 +112,7 @@ export class QueuedStateRegenerator implements IStateRegenerator { | |
| // Otherwise the state transition may not be cached and wasted. Queue for regen since the | ||
| // work required will still be significant. | ||
| if (parentEpoch === blockEpoch) { | ||
| const state = this.blockStateCache.get(parentBlock.stateRoot, opts); | ||
| const state = this.blockStateCache.get(parentBlock.stateRoot); | ||
| if (state) { | ||
| return state; | ||
| } | ||
|
|
@@ -132,21 +126,17 @@ export class QueuedStateRegenerator implements IStateRegenerator { | |
| } | ||
|
|
||
| /** | ||
| * Get checkpoint state from cache, this function is not for block processing so don't transfer cache | ||
| * Get checkpoint state from cache | ||
| */ | ||
| getCheckpointStateSync(cp: CheckpointHex): CachedBeaconStateAllForks | null { | ||
| return this.checkpointStateCache.get(cp, {dontTransferCache: true}); | ||
| return this.checkpointStateCache.get(cp); | ||
| } | ||
|
|
||
| /** | ||
| * Get state closest to head, this function is not for block processing so don't transfer cache | ||
| * Get state closest to head | ||
| */ | ||
| getClosestHeadState(head: ProtoBlock): CachedBeaconStateAllForks | null { | ||
| const opts = {dontTransferCache: true}; | ||
| return ( | ||
| this.checkpointStateCache.getLatest(head.blockRoot, Infinity, opts) || | ||
| this.blockStateCache.get(head.stateRoot, opts) | ||
| ); | ||
| return this.checkpointStateCache.getLatest(head.blockRoot, Infinity) || this.blockStateCache.get(head.stateRoot); | ||
| } | ||
|
|
||
| pruneOnCheckpoint(finalizedEpoch: Epoch, justifiedEpoch: Epoch, headStateRoot: RootHex): void { | ||
|
|
@@ -181,10 +171,7 @@ export class QueuedStateRegenerator implements IStateRegenerator { | |
| maybeHeadStateRoot, | ||
| }; | ||
| const headState = | ||
| newHeadStateRoot === maybeHeadStateRoot | ||
| ? maybeHeadState | ||
| : // maybeHeadState was already in block state cache so we don't transfer the cache | ||
| this.blockStateCache.get(newHeadStateRoot, {dontTransferCache: true}); | ||
| newHeadStateRoot === maybeHeadStateRoot ? maybeHeadState : this.blockStateCache.get(newHeadStateRoot); | ||
|
|
||
| if (headState) { | ||
| this.blockStateCache.setHeadState(headState); | ||
|
|
@@ -199,9 +186,7 @@ export class QueuedStateRegenerator implements IStateRegenerator { | |
|
|
||
| // 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( | ||
| this.regen.getState(newHeadStateRoot, RegenCaller.processBlock, allowDiskReload).then( | ||
| (headStateRegen) => this.blockStateCache.setHeadState(headStateRegen), | ||
| (e) => this.logger.error("Error on head state regen", logCtx, e) | ||
| ); | ||
|
|
@@ -224,7 +209,7 @@ export class QueuedStateRegenerator implements IStateRegenerator { | |
| this.metrics?.regenFnCallTotal.inc({caller: rCaller, entrypoint: RegenFnName.getPreState}); | ||
|
|
||
| // First attempt to fetch the state from caches before queueing | ||
| const cachedState = this.getPreStateSync(block, opts); | ||
| const cachedState = this.getPreStateSync(block); | ||
|
|
||
| if (cachedState !== null) { | ||
| return cachedState; | ||
|
|
@@ -243,7 +228,7 @@ export class QueuedStateRegenerator implements IStateRegenerator { | |
| this.metrics?.regenFnCallTotal.inc({caller: rCaller, entrypoint: RegenFnName.getCheckpointState}); | ||
|
|
||
| // First attempt to fetch the state from cache before queueing | ||
| const checkpointState = this.checkpointStateCache.get(toCheckpointHex(cp), opts); | ||
| const checkpointState = this.checkpointStateCache.get(toCheckpointHex(cp)); | ||
| if (checkpointState) { | ||
| return checkpointState; | ||
| } | ||
|
|
@@ -271,22 +256,18 @@ export class QueuedStateRegenerator implements IStateRegenerator { | |
| return this.jobQueue.push({key: "getBlockSlotState", args: [blockRoot, slot, opts, rCaller]}); | ||
| } | ||
|
|
||
| async getState( | ||
| stateRoot: RootHex, | ||
| rCaller: RegenCaller, | ||
| opts: StateRegenerationOpts = {dontTransferCache: true} | ||
| ): Promise<CachedBeaconStateAllForks> { | ||
| async getState(stateRoot: RootHex, rCaller: RegenCaller): Promise<CachedBeaconStateAllForks> { | ||
| this.metrics?.regenFnCallTotal.inc({caller: rCaller, entrypoint: RegenFnName.getState}); | ||
|
|
||
| // First attempt to fetch the state from cache before queueing | ||
| const state = this.blockStateCache.get(stateRoot, opts); | ||
| const state = this.blockStateCache.get(stateRoot); | ||
| if (state) { | ||
| return state; | ||
| } | ||
|
|
||
| // The state is not immediately available in the cache, enqueue the job | ||
| this.metrics?.regenFnQueuedTotal.inc({caller: rCaller, entrypoint: RegenFnName.getState}); | ||
| return this.jobQueue.push({key: "getState", args: [stateRoot, rCaller, opts]}); | ||
| return this.jobQueue.push({key: "getState", args: [stateRoot, rCaller]}); | ||
| } | ||
|
|
||
| private jobQueueProcessor = async (regenRequest: RegenRequest): Promise<CachedBeaconStateAllForks> => { | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how is this change related to the other changes in the PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dontTransferCacheis used silently inside this call, so I make it explicit. There is nothing changed here.see
lodestar/packages/state-transition/src/stateTransition.ts
Line 100 in 3932675