Skip to content

Commit

Permalink
engine code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanio committed Oct 16, 2021
1 parent 58ff02c commit 824b6eb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 84 deletions.
49 changes: 36 additions & 13 deletions packages/client/lib/rpc/modules/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,7 @@ export class Engine {
throw EngineError.ActionNotAllowed
}

let [payloadId]: any = params

payloadId = toType(payloadId, TypeOutput.Number)
const payloadId = toType(params[0], TypeOutput.Number)
const payload = this.pendingPayloads.get(payloadId)

if (!payload) {
Expand Down Expand Up @@ -493,16 +491,18 @@ export class Engine {
* @returns None or an error
*/
async consensusValidated(params: [{ blockHash: string; status: string }]) {
const { blockHash, status }: any = params[0]
const { status } = params[0]
let { blockHash } = params[0]
blockHash = blockHash.slice(2)

const block = this.validBlocks.get(blockHash.slice(2))
const block = this.validBlocks.get(blockHash)

if (!block && status === Status.VALID) {
throw EngineError.UnknownHeader
}

if (block && status === Status.INVALID) {
this.validBlocks.delete(block.hash().toString('hex'))
this.validBlocks.delete(blockHash)
}

return null
Expand All @@ -518,9 +518,14 @@ export class Engine {
* @returns None or an error
*/
async forkchoiceUpdated(params: [{ headBlockHash: string; finalizedBlockHash: string }]) {
const { headBlockHash, finalizedBlockHash } = params[0]

const headBlock = this.validBlocks.get(headBlockHash.slice(2))
let { headBlockHash, finalizedBlockHash } = params[0]
headBlockHash = headBlockHash.slice(2)
finalizedBlockHash = finalizedBlockHash.slice(2)

/*
* Process head block
*/
const headBlock = this.validBlocks.get(headBlockHash)
if (!headBlock) {
throw EngineError.UnknownHeader
}
Expand All @@ -536,21 +541,39 @@ export class Engine {

await this.chain.putBlocks([...parentBlocks, headBlock], true)

this.synchronizer.syncTargetHeight = headBlock.header.number
if (
!this.synchronizer.syncTargetHeight ||
this.synchronizer.syncTargetHeight.lt(headBlock.header.number)
) {
this.synchronizer.syncTargetHeight = headBlock.header.number
}

if (finalizedBlockHash.slice(2) === '0'.repeat(64)) {
/*
* Process finalized block
*/
if (finalizedBlockHash === '0'.repeat(64)) {
// All zeros means no finalized block yet
} else {
const finalizedBlock = this.validBlocks.get(finalizedBlockHash.slice(2))
let finalizedBlock
finalizedBlock = this.validBlocks.get(finalizedBlockHash)
if (!finalizedBlock) {
throw EngineError.UnknownHeader
try {
finalizedBlock = await this.chain.getBlock(Buffer.from(finalizedBlockHash, 'hex'))
} catch (error) {
throw EngineError.UnknownHeader
}
}

if (!this.chain.mergeFirstFinalizedBlock) {
this.chain.mergeFirstFinalizedBlock = finalizedBlock
}
this.chain.mergeLastFinalizedBlock = finalizedBlock

this.validBlocks.delete(finalizedBlockHash)
}

this.validBlocks.delete(headBlockHash)

return null
}
}

This file was deleted.

This file was deleted.

0 comments on commit 824b6eb

Please sign in to comment.