Skip to content

Commit

Permalink
clear cache is the vmhead not at parent stateroot
Browse files Browse the repository at this point in the history
  • Loading branch information
g11tech committed May 30, 2023
1 parent 36b564b commit 810aa54
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
16 changes: 14 additions & 2 deletions packages/client/src/execution/vmexecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,11 @@ export class VMExecution extends Execution {
let parentState: Uint8Array | undefined
let errorBlock: Block | undefined

// flag for vm to clear statemanager cache on runBlock
// i) If on start of iterator the last run state is not same as the block's parent
// ii) If reorg happens on the block iterator
let clearCache = false

while (
this.started &&
!this.config.shutdown &&
Expand All @@ -305,10 +310,17 @@ export class VMExecution extends Execution {
parentState = headBlock.header.stateRoot

if (reorg) {
clearCache = true
this.config.logger.info(
`Chain reorg happened, set new head to block number=${headBlock.header.number}, clearing state cache for VM execution.`
`VM run: Chain reorged, setting new head to block number=${headBlock.header.number} clearCache=${clearCache}.`
)
} else {
const prevVMStateRoot = await this.vm.stateManager.getStateRoot()
clearCache = !equalsBytes(prevVMStateRoot, parentState)
}
} else {
// Continuation of last vm run, no need to clearCache
clearCache = false
}

// run block, update head if valid
Expand Down Expand Up @@ -356,7 +368,7 @@ export class VMExecution extends Execution {
const result = await this.vm.runBlock({
block,
root: parentState,
clearCache: reorg ? true : false,
clearCache,
skipBlockValidation,
skipHeaderValidation: true,
})
Expand Down
2 changes: 1 addition & 1 deletion packages/vm/src/runBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export async function runBlock(this: VM, opts: RunBlockOpts): Promise<RunBlockRe
// Set state root if provided
if (root) {
if (this.DEBUG) {
debug(`Set provided state root ${bytesToHex(root)}`)
debug(`Set provided state root ${bytesToHex(root)} clearCache=${clearCache}`)
}
await state.setStateRoot(root, clearCache)
}
Expand Down

0 comments on commit 810aa54

Please sign in to comment.