-
Notifications
You must be signed in to change notification settings - Fork 791
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
Client: ensure safe/finalized blocks are part of the canonical chain on forkchoiceUpdated #2577
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0252bbb
client/engine: ensure finalized/safe blocks are in canonical chain
jochem-brouwer 10ceed7
client: engine-api: fix finalized block check
jochem-brouwer 70cad64
Merge branch 'master' into hive-execution-api-fixfcu
jochem-brouwer 2a1609b
client/tests: fix forkchoice updated test
jochem-brouwer 0473e19
Merge branch 'hive-execution-api-fixfcu' of github.com:ethereumjs/eth…
jochem-brouwer 998f99b
client: add fcu tests to check if blocks are part of canonical chain
jochem-brouwer 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 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 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 |
---|---|---|
@@ -1,16 +1,20 @@ | ||
import { Block, BlockHeader } from '@ethereumjs/block' | ||
import { Chain, Common, Hardfork } from '@ethereumjs/common' | ||
import { bufferToHex, zeros } from '@ethereumjs/util' | ||
import * as tape from 'tape' | ||
import * as td from 'testdouble' | ||
|
||
import { INVALID_PARAMS } from '../../../lib/rpc/error-code' | ||
import { blockToExecutionPayload } from '../../../lib/rpc/modules' | ||
import blocks = require('../../testdata/blocks/beacon.json') | ||
import genesisJSON = require('../../testdata/geth-genesis/post-merge.json') | ||
import { baseRequest, baseSetup, params, setupChain } from '../helpers' | ||
import { checkError } from '../util' | ||
|
||
import { batchBlocks } from './newPayloadV1.spec' | ||
|
||
const crypto = require('crypto') | ||
|
||
const method = 'engine_forkchoiceUpdatedV1' | ||
|
||
const originalValidate = BlockHeader.prototype._consensusFormatValidation | ||
|
@@ -27,6 +31,26 @@ const validPayloadAttributes = { | |
suggestedFeeRecipient: '0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b', | ||
} | ||
|
||
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Merge }) | ||
|
||
function createBlock(parentBlock: Block) { | ||
const prevRandao = crypto.randomBytes(32) | ||
const block = Block.fromBlockData( | ||
{ | ||
header: { | ||
parentHash: parentBlock.hash(), | ||
mixHash: prevRandao, | ||
number: parentBlock.header.number + BigInt(1), | ||
stateRoot: parentBlock.header.stateRoot, | ||
timestamp: parentBlock.header.timestamp + BigInt(1), | ||
gasLimit: parentBlock.header.gasLimit, | ||
}, | ||
}, | ||
{ common } | ||
) | ||
return block | ||
} | ||
|
||
export const validPayload = [validForkChoiceState, validPayloadAttributes] | ||
|
||
tape(`${method}: call with invalid head block hash without 0x`, async (t) => { | ||
|
@@ -271,7 +295,93 @@ tape(`${method}: latest block after reorg`, async (t) => { | |
]) | ||
|
||
expectRes = (res: any) => { | ||
t.equal(res.body.result.payloadStatus.latestValidHash, blocks[1].blockHash) | ||
t.equal(res.body.error.code, -32602) | ||
} | ||
await baseRequest(t, server, req, 200, expectRes) | ||
}) | ||
|
||
tape(`${method}: validate safeBlockHash is part of canonical chain`, async (t) => { | ||
const { server, chain } = await setupChain(genesisJSON, 'post-merge', { engine: true }) | ||
|
||
const genesis = await chain.getBlock(BigInt(0)) | ||
|
||
// Build the payload for the canonical chain | ||
const canonical = [genesis] | ||
|
||
for (let i = 0; i < 2; i++) { | ||
canonical.push(createBlock(canonical[canonical.length - 1])) | ||
} | ||
|
||
// Build an alternative payload | ||
const reorg = [genesis] | ||
for (let i = 0; i < 2; i++) { | ||
reorg.push(createBlock(reorg[reorg.length - 1])) | ||
} | ||
|
||
const canonicalPayload = canonical.map( | ||
(e) => blockToExecutionPayload(e, BigInt(0)).executionPayload | ||
) | ||
const reorgPayload = reorg.map((e) => blockToExecutionPayload(e, BigInt(0)).executionPayload) | ||
|
||
await batchBlocks(t, server, canonicalPayload.slice(1)) | ||
await batchBlocks(t, server, reorgPayload.slice(1)) | ||
|
||
// Safe block hash is not in the canonical chain | ||
const req = params(method, [ | ||
{ | ||
headBlockHash: reorgPayload[2].blockHash, | ||
safeBlockHash: canonicalPayload[1].blockHash, | ||
finalizedBlockHash: reorgPayload[1].blockHash, | ||
}, | ||
]) | ||
|
||
const expectRes = (res: any) => { | ||
t.equal(res.body.error.code, -32602) | ||
t.ok(res.body.error.message.includes('Safe')) | ||
t.ok(res.body.error.message.includes('canonical')) | ||
} | ||
await baseRequest(t, server, req, 200, expectRes) | ||
}) | ||
|
||
tape(`${method}: validate finalizedBlockHash is part of canonical chain`, async (t) => { | ||
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. Note: had to copy the previous test, because if |
||
const { server, chain } = await setupChain(genesisJSON, 'post-merge', { engine: true }) | ||
|
||
const genesis = await chain.getBlock(BigInt(0)) | ||
|
||
// Build the payload for the canonical chain | ||
const canonical = [genesis] | ||
|
||
for (let i = 0; i < 2; i++) { | ||
canonical.push(createBlock(canonical[canonical.length - 1])) | ||
} | ||
|
||
// Build an alternative payload | ||
const reorg = [genesis] | ||
for (let i = 0; i < 2; i++) { | ||
reorg.push(createBlock(reorg[reorg.length - 1])) | ||
} | ||
|
||
const canonicalPayload = canonical.map( | ||
(e) => blockToExecutionPayload(e, BigInt(0)).executionPayload | ||
) | ||
const reorgPayload = reorg.map((e) => blockToExecutionPayload(e, BigInt(0)).executionPayload) | ||
|
||
await batchBlocks(t, server, canonicalPayload.slice(1)) | ||
await batchBlocks(t, server, reorgPayload.slice(1)) | ||
|
||
// Finalized block hash is not in the canonical chain | ||
const req = params(method, [ | ||
{ | ||
headBlockHash: reorgPayload[2].blockHash, | ||
safeBlockHash: reorgPayload[1].blockHash, | ||
finalizedBlockHash: canonicalPayload[1].blockHash, | ||
}, | ||
]) | ||
|
||
const expectRes = (res: any) => { | ||
t.equal(res.body.error.code, -32602) | ||
t.ok(res.body.error.message.includes('Finalized')) | ||
t.ok(res.body.error.message.includes('canonical')) | ||
} | ||
await baseRequest(t, server, req, 200, expectRes) | ||
}) | ||
|
This file contains 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
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.
Note; this FCU payload is indeed invalid, because
headBlock
has block number 1, whilesafeBlock
has block number 2 (andfinalBlock
also)