-
Notifications
You must be signed in to change notification settings - Fork 846
client: Add safe and finalized blockoptions to the chain #2585
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
f9ec854
client: Add safe and finalized blockoptions to the chain
g11tech 1313e09
fix tests
g11tech 42b9954
fix more tests
g11tech 49f9601
fix remaining
g11tech dfcd45c
cleanup
g11tech 0e537c3
enhance coverage
g11tech 29db479
unset scheduled goerli timestamp based hfs colliding with test
g11tech 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -161,27 +161,72 @@ export class VMExecution extends Execution { | |
| * Should only be used after {@link VMExecution.runWithoutSetHead} | ||
| * @param blocks Array of blocks to save pending receipts and set the last block as the head | ||
| */ | ||
| async setHead(blocks: Block[]): Promise<void> { | ||
| async setHead( | ||
| blocks: Block[], | ||
| { finalizedBlock, safeBlock }: { finalizedBlock?: Block; safeBlock?: Block } = {} | ||
| ): Promise<void> { | ||
| return this.runWithLock<void>(async () => { | ||
| const vmHeadBlock = blocks[blocks.length - 1] | ||
| if (!(await this.vm.stateManager.hasStateRoot(vmHeadBlock.header.stateRoot))) { | ||
| // If we set blockchain iterator to somewhere where we don't have stateroot | ||
| // execution run will always fail | ||
| const chainPointers: [string, Block | null][] = [ | ||
| ['vmHeadBlock', vmHeadBlock], | ||
| // if safeBlock is not provided, the current safeBlock of chain should be used | ||
| // which is genesisBlock if it has never been set for e.g. | ||
| ['safeBlock', safeBlock ?? this.chain.blocks.safe], | ||
| ['finalizedBlock', finalizedBlock ?? this.chain.blocks.finalized], | ||
| ] | ||
|
|
||
| let isSortedDesc = true | ||
| let lastBlock = vmHeadBlock | ||
| for (const [blockName, block] of chainPointers) { | ||
| if (block === null) { | ||
| continue | ||
| } | ||
| if (!(await this.vm.stateManager.hasStateRoot(block.header.stateRoot))) { | ||
| // If we set blockchain iterator to somewhere where we don't have stateroot | ||
| // execution run will always fail | ||
| throw Error( | ||
| `${blockName}'s stateRoot not found number=${block.header.number} root=${short( | ||
| block.header.stateRoot | ||
| )}` | ||
| ) | ||
| } | ||
| isSortedDesc = isSortedDesc && lastBlock.header.number >= block.header.number | ||
| lastBlock = block | ||
| } | ||
|
|
||
| if (isSortedDesc === false) { | ||
| throw Error( | ||
| `vmHeadBlock's stateRoot not found number=${vmHeadBlock.header.number} root=${short( | ||
| vmHeadBlock.header.stateRoot | ||
| )}` | ||
| `headBlock=${vmHeadBlock?.header.number} should be >= safeBlock=${safeBlock?.header.number} should be >= finalizedBlock=${finalizedBlock?.header.number}` | ||
| ) | ||
| } | ||
| await this.chain.putBlocks(blocks, true) | ||
| // skip emitting the chain update event as we will manually do it | ||
| await this.chain.putBlocks(blocks, true, true) | ||
| for (const block of blocks) { | ||
| const receipts = this.pendingReceipts?.get(block.hash().toString('hex')) | ||
| if (receipts) { | ||
| void this.receiptsManager?.saveReceipts(block, receipts) | ||
| this.pendingReceipts?.delete(block.hash().toString('hex')) | ||
| } | ||
| } | ||
|
|
||
| // check if the head, safe and finalized are now canonical | ||
| for (const [blockName, block] of chainPointers) { | ||
| if (block === null) { | ||
| continue | ||
| } | ||
| const blockByNumber = await this.chain.getBlock(block.header.number) | ||
| if (!blockByNumber.hash().equals(block.hash())) { | ||
|
Contributor
Author
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. canonicalness of head, safe and finalized is now checked here |
||
| throw Error(`${blockName} not in canonical chain`) | ||
| } | ||
| } | ||
| await this.chain.blockchain.setIteratorHead('vm', vmHeadBlock.hash()) | ||
| if (safeBlock !== undefined) { | ||
| await this.chain.blockchain.setIteratorHead('safe', safeBlock.hash()) | ||
| } | ||
| if (finalizedBlock !== undefined) { | ||
| await this.chain.blockchain.setIteratorHead('finalized', finalizedBlock.hash()) | ||
| } | ||
| await this.chain.update(true) | ||
| }) | ||
| } | ||
|
|
||
|
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.