Skip to content
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: Fcu handles if headBlockHash is an old block #1820

Merged
merged 1 commit into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 24 additions & 26 deletions packages/client/lib/rpc/modules/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,27 +534,29 @@ export class Engine {

const vmHeadHash = this.chain.headers.latest!.hash()
if (!vmHeadHash.equals(headBlock.hash())) {
let parentBlocks
try {
parentBlocks = await recursivelyFindParents(
vmHeadHash,
headBlock.header.parentHash,
this.validBlocks,
this.chain
)
} catch (error) {
const latestValidHash = await validHash(
headBlock.header.parentHash,
this.validBlocks,
this.chain
)
const payloadStatus = { status: Status.SYNCING, latestValidHash, validationError: null }
const response = { payloadStatus, payloadId: null }
this.connectionManager.lastForkchoiceUpdate({
state: params[0],
response,
})
return response
let parentBlocks: Block[] = []
if (this.chain.headers.latest?.number.lt(headBlock.header.number)) {
try {
parentBlocks = await recursivelyFindParents(
vmHeadHash,
headBlock.header.parentHash,
this.validBlocks,
this.chain
)
} catch (error) {
const latestValidHash = await validHash(
headBlock.header.parentHash,
this.validBlocks,
this.chain
)
const payloadStatus = { status: Status.SYNCING, latestValidHash, validationError: null }
const response = { payloadStatus, payloadId: null }
this.connectionManager.lastForkchoiceUpdate({
state: params[0],
response,
})
return response
}
}

const blocks = [...parentBlocks, headBlock]
Expand Down Expand Up @@ -624,11 +626,7 @@ export class Engine {
return response
}

const latestValidHash = await validHash(
headBlock.header.parentHash,
this.validBlocks,
this.chain
)
const latestValidHash = await validHash(headBlock.header.hash(), this.validBlocks, this.chain)
const payloadStatus = { status: Status.VALID, latestValidHash, validationError: null }
const response = { payloadStatus, payloadId: null }
this.connectionManager.lastForkchoiceUpdate({
Expand Down
44 changes: 43 additions & 1 deletion packages/client/test/rpc/engine/forkchoiceUpdatedV1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ tape(`${method}: call with valid fork choice state without payload attributes`,
const req = params(method, [validForkChoiceState])
const expectRes = (res: any) => {
t.equal(res.body.result.payloadStatus.status, 'VALID')
t.equal(res.body.result.payloadStatus.latestValidHash, null)
t.equal(res.body.result.payloadStatus.latestValidHash, validForkChoiceState.headBlockHash)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been changed because I noticed that when fcu returns VALID it also returns the latestValidHash (Point 7 of specs)

t.equal(res.body.result.payloadStatus.validationError, null)
t.equal(res.body.result.payloadId, null)
}
Expand Down Expand Up @@ -281,3 +281,45 @@ tape(`${method}: invalid safe block hash`, async (t) => {

await baseRequest(t, server, req, 200, expectRes)
})

tape(`${method}: latest block after reorg`, async (t) => {
const { server } = await setupChain(genesisJSON, 'post-merge', { engine: true })
let req = params(method, [validForkChoiceState])
let expectRes = (res: any) => {
t.equal(res.body.result.payloadStatus.status, 'VALID')
}
await baseRequest(t, server, req, 200, expectRes, false)

for (let i = 0; i < 3; i++) {
const req = params('engine_newPayloadV1', [blocks[i]])
const expectRes = (res: any) => {
t.equal(res.body.result.status, 'VALID')
}
await baseRequest(t, server, req, 200, expectRes, false)
}

req = params(method, [
{
...validForkChoiceState,
headBlockHash: blocks[2].blockHash,
safeBlockHash: blocks[0].blockHash,
},
])
expectRes = (res: any) => {
t.equal(res.body.result.payloadStatus.status, 'VALID')
}
await baseRequest(t, server, req, 200, expectRes, false)

req = params(method, [
{
headBlockHash: blocks[1].blockHash,
safeBlockHash: blocks[2].blockHash,
finalizedBlockHash: blocks[2].blockHash,
},
])

expectRes = (res: any) => {
t.equal(res.body.result.payloadStatus.latestValidHash, blocks[1].blockHash)
}
await baseRequest(t, server, req, 200, expectRes)
})