Skip to content

Commit

Permalink
Merge branch 'master' into signature/eip1559
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestognw authored May 27, 2022
2 parents f57dd58 + 2b993aa commit 4953c7b
Show file tree
Hide file tree
Showing 13 changed files with 202 additions and 210 deletions.
3 changes: 0 additions & 3 deletions packages/client/lib/blockchain/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ export class Chain {
public chainDB: LevelUp
public blockchain: Blockchain
public opened: boolean
public terminalPoWBlock: Block | undefined
public transitionPoSBlock: Block | undefined
public lastFinalizedBlockHash: Buffer | undefined

private _headers: ChainHeaders = {
latest: null,
Expand Down
21 changes: 8 additions & 13 deletions packages/client/lib/execution/vmexecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export class VMExecution extends Execution {
* @param blocks Array of blocks to save pending receipts and set the last block as the head
*/
async setHead(blocks: Block[]): Promise<void> {
await this.chain.blockchain.setIteratorHead('vm', blocks[blocks.length - 1].hash())
await this.chain.putBlocks(blocks, true)
for (const block of blocks) {
const receipts = this.pendingReceipts?.get(block.hash().toString('hex'))
Expand All @@ -123,19 +124,15 @@ export class VMExecution extends Execution {
this.pendingReceipts?.delete(block.hash().toString('hex'))
}
}
const head = blocks[blocks.length - 1]
await this.chain.blockchain.setIteratorHead('vm', head.hash())
}

/**
* Runs the VM execution
*
* @param loop Whether to continue iterating until vm head equals chain head (default: true)
* @returns number of blocks executed
*/
async run(): Promise<number> {
if (this.running) {
return 0
}
async run(loop = true): Promise<number> {
if (this.running) return 0
this.running = true
let numExecuted: number | undefined

Expand All @@ -148,7 +145,7 @@ export class VMExecution extends Execution {
let errorBlock: Block | undefined

while (
(numExecuted === undefined || numExecuted === this.NUM_BLOCKS_PER_ITERATION) &&
(numExecuted === undefined || (loop && numExecuted === this.NUM_BLOCKS_PER_ITERATION)) &&
!startHeadBlock.hash().equals(canonicalHead.hash())
) {
let txCounter = 0
Expand All @@ -159,11 +156,9 @@ export class VMExecution extends Execution {
this.vmPromise = blockchain.iterator(
'vm',
async (block: Block, reorg: boolean) => {
if (errorBlock) {
return
}
if (errorBlock) return
// determine starting state for block run
// if we are just starting or if a chain re-org has happened
// if we are just starting or if a chain reorg has happened
if (!headBlock || reorg) {
const parentBlock = await blockchain.getBlock(block.header.parentHash)
parentState = parentBlock.header.stateRoot
Expand Down Expand Up @@ -278,7 +273,7 @@ export class VMExecution extends Execution {
canonicalHead = await this.vm.blockchain.getLatestBlock()
}
this.running = false
return numExecuted as number
return numExecuted ?? 0
}

/**
Expand Down
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 @@ -536,23 +536,6 @@ export class Engine {
}
}

if (safeBlockHash !== headBlockHash) {
try {
await this.chain.getBlock(toBuffer(safeBlockHash))
} catch (error) {
const message = 'safe block hash not available'
this.connectionManager.lastForkchoiceUpdate({
state: params[0],
response: undefined,
error: message,
})
throw {
code: INVALID_PARAMS,
message,
}
}
}

const vmHeadHash = this.chain.headers.latest!.hash()
if (!vmHeadHash.equals(headBlock.hash())) {
let parentBlocks: Block[] = []
Expand Down Expand Up @@ -601,19 +584,34 @@ export class Engine {
}

/*
* Process finalized block
* All zeros means no finalized block yet which is okay
* Process safe and finalized block
* Allowed to have zero value while transition block is finalizing
*/
const zeroHash = zeros(32)
const finalizedHash = toBuffer(finalizedBlockHash)
if (!finalizedHash.equals(zeroHash)) {
const zeroBlockHash = zeros(32)
const safe = toBuffer(safeBlockHash)
if (!safe.equals(headBlock.hash()) && !safe.equals(zeroBlockHash)) {
try {
await this.chain.getBlock(safe)
} catch (error) {
const message = 'safe block not available'
this.connectionManager.lastForkchoiceUpdate({
state: params[0],
response: undefined,
error: message,
})
throw {
code: INVALID_PARAMS,
message,
}
}
}
const finalized = toBuffer(finalizedBlockHash)
if (!finalized.equals(zeroBlockHash)) {
try {
this.chain.lastFinalizedBlockHash = (
await this.chain.getBlock(toBuffer(finalizedBlockHash))
).hash()
await this.chain.getBlock(finalized)
} catch (error) {
throw {
message: 'finalized block hash not available',
message: 'finalized block not available',
code: INVALID_PARAMS,
}
}
Expand Down
Loading

0 comments on commit 4953c7b

Please sign in to comment.