Skip to content

Commit a133e27

Browse files
authored
client: small engine updates (#1902)
* simplify txpool, fix runExecution, allow safe block to also be zeros during transition * fix beacon sync skeleton fill with new vmexecution.run loop param
1 parent 0b7cc1b commit a133e27

File tree

11 files changed

+141
-162
lines changed

11 files changed

+141
-162
lines changed

packages/client/lib/blockchain/chain.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@ export class Chain {
8383
public chainDB: LevelUp
8484
public blockchain: Blockchain
8585
public opened: boolean
86-
public terminalPoWBlock: Block | undefined
87-
public transitionPoSBlock: Block | undefined
88-
public lastFinalizedBlockHash: Buffer | undefined
8986

9087
private _headers: ChainHeaders = {
9188
latest: null,

packages/client/lib/execution/vmexecution.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export class VMExecution extends Execution {
115115
* @param blocks Array of blocks to save pending receipts and set the last block as the head
116116
*/
117117
async setHead(blocks: Block[]): Promise<void> {
118+
await this.chain.blockchain.setIteratorHead('vm', blocks[blocks.length - 1].hash())
118119
await this.chain.putBlocks(blocks, true)
119120
for (const block of blocks) {
120121
const receipts = this.pendingReceipts?.get(block.hash().toString('hex'))
@@ -123,19 +124,15 @@ export class VMExecution extends Execution {
123124
this.pendingReceipts?.delete(block.hash().toString('hex'))
124125
}
125126
}
126-
const head = blocks[blocks.length - 1]
127-
await this.chain.blockchain.setIteratorHead('vm', head.hash())
128127
}
129128

130129
/**
131130
* Runs the VM execution
132-
*
131+
* @param loop Whether to continue iterating until vm head equals chain head (default: true)
133132
* @returns number of blocks executed
134133
*/
135-
async run(): Promise<number> {
136-
if (this.running) {
137-
return 0
138-
}
134+
async run(loop = true): Promise<number> {
135+
if (this.running) return 0
139136
this.running = true
140137
let numExecuted: number | undefined
141138

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

150147
while (
151-
(numExecuted === undefined || numExecuted === this.NUM_BLOCKS_PER_ITERATION) &&
148+
(numExecuted === undefined || (loop && numExecuted === this.NUM_BLOCKS_PER_ITERATION)) &&
152149
!startHeadBlock.hash().equals(canonicalHead.hash())
153150
) {
154151
let txCounter = 0
@@ -159,11 +156,9 @@ export class VMExecution extends Execution {
159156
this.vmPromise = blockchain.iterator(
160157
'vm',
161158
async (block: Block, reorg: boolean) => {
162-
if (errorBlock) {
163-
return
164-
}
159+
if (errorBlock) return
165160
// determine starting state for block run
166-
// if we are just starting or if a chain re-org has happened
161+
// if we are just starting or if a chain reorg has happened
167162
if (!headBlock || reorg) {
168163
const parentBlock = await blockchain.getBlock(block.header.parentHash)
169164
parentState = parentBlock.header.stateRoot
@@ -278,7 +273,7 @@ export class VMExecution extends Execution {
278273
canonicalHead = await this.vm.blockchain.getLatestBlock()
279274
}
280275
this.running = false
281-
return numExecuted as number
276+
return numExecuted ?? 0
282277
}
283278

284279
/**

packages/client/lib/rpc/modules/engine.ts

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -536,23 +536,6 @@ export class Engine {
536536
}
537537
}
538538

539-
if (safeBlockHash !== headBlockHash) {
540-
try {
541-
await this.chain.getBlock(toBuffer(safeBlockHash))
542-
} catch (error) {
543-
const message = 'safe block hash not available'
544-
this.connectionManager.lastForkchoiceUpdate({
545-
state: params[0],
546-
response: undefined,
547-
error: message,
548-
})
549-
throw {
550-
code: INVALID_PARAMS,
551-
message,
552-
}
553-
}
554-
}
555-
556539
const vmHeadHash = this.chain.headers.latest!.hash()
557540
if (!vmHeadHash.equals(headBlock.hash())) {
558541
let parentBlocks: Block[] = []
@@ -601,19 +584,34 @@ export class Engine {
601584
}
602585

603586
/*
604-
* Process finalized block
605-
* All zeros means no finalized block yet which is okay
587+
* Process safe and finalized block
588+
* Allowed to have zero value while transition block is finalizing
606589
*/
607-
const zeroHash = zeros(32)
608-
const finalizedHash = toBuffer(finalizedBlockHash)
609-
if (!finalizedHash.equals(zeroHash)) {
590+
const zeroBlockHash = zeros(32)
591+
const safe = toBuffer(safeBlockHash)
592+
if (!safe.equals(headBlock.hash()) && !safe.equals(zeroBlockHash)) {
593+
try {
594+
await this.chain.getBlock(safe)
595+
} catch (error) {
596+
const message = 'safe block not available'
597+
this.connectionManager.lastForkchoiceUpdate({
598+
state: params[0],
599+
response: undefined,
600+
error: message,
601+
})
602+
throw {
603+
code: INVALID_PARAMS,
604+
message,
605+
}
606+
}
607+
}
608+
const finalized = toBuffer(finalizedBlockHash)
609+
if (!finalized.equals(zeroBlockHash)) {
610610
try {
611-
this.chain.lastFinalizedBlockHash = (
612-
await this.chain.getBlock(toBuffer(finalizedBlockHash))
613-
).hash()
611+
await this.chain.getBlock(finalized)
614612
} catch (error) {
615613
throw {
616-
message: 'finalized block hash not available',
614+
message: 'finalized block not available',
617615
code: INVALID_PARAMS,
618616
}
619617
}

0 commit comments

Comments
 (0)