Skip to content

Commit

Permalink
Client: Fcu handles if headBlockHash is an old block (#1820)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbrzn authored Mar 28, 2022
1 parent 739f839 commit a87bccd
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 27 deletions.
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)
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)
})

0 comments on commit a87bccd

Please sign in to comment.