Skip to content

Commit 984a030

Browse files
ScottyPoiholgerd77
authored andcommitted
blockchain: fix iterator return type, reinstate tests (ethereumjs#1877)
* blockchain: remove 'void' from iterator() return * vm: remove void from vm.runBlockchain() return types * client: adapt VMExecution to stricter blockchain.iterator() call * vm: reintroduce deleted tests for blockchain.getIteratorHead() * vm: correct method name in test
1 parent 6d22f0f commit 984a030

File tree

4 files changed

+13
-7
lines changed

4 files changed

+13
-7
lines changed

packages/blockchain/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export interface BlockchainInterface {
4141
* @param onBlock - Function called on each block with params (block: Block,
4242
* reorg: boolean)
4343
*/
44-
iterator(name: string, onBlock: OnBlock): Promise<void | number>
44+
iterator(name: string, onBlock: OnBlock): Promise<number>
4545
}
4646

4747
/**

packages/client/lib/execution/vmexecution.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class VMExecution extends Execution {
2424

2525
public receiptsManager?: ReceiptsManager
2626
private pendingReceipts?: Map<string, TxReceipt[]>
27-
private vmPromise?: Promise<number | undefined>
27+
private vmPromise?: Promise<number>
2828

2929
/** Number of maximum blocks to run per iteration of {@link VMExecution.run} */
3030
private NUM_BLOCKS_PER_ITERATION = 50
@@ -154,7 +154,6 @@ export class VMExecution extends Execution {
154154
headBlock = undefined
155155
parentState = undefined
156156
errorBlock = undefined
157-
158157
this.vmPromise = blockchain.iterator(
159158
'vm',
160159
async (block: Block, reorg: boolean) => {
@@ -242,15 +241,15 @@ export class VMExecution extends Execution {
242241
},
243242
this.NUM_BLOCKS_PER_ITERATION
244243
)
245-
numExecuted = (await this.vmPromise) as number
244+
numExecuted = await this.vmPromise
246245

247246
if (errorBlock) {
248247
await this.chain.blockchain.setIteratorHead('vm', (errorBlock as Block).header.parentHash)
249248
return 0
250249
}
251250

252251
const endHeadBlock = await this.vm.blockchain.getIteratorHead('vm')
253-
if (numExecuted > 0) {
252+
if (numExecuted && numExecuted > 0) {
254253
const firstNumber = startHeadBlock.header.number
255254
const firstHash = short(startHeadBlock.hash())
256255
const lastNumber = endHeadBlock.header.number

packages/vm/src/runBlockchain.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default async function runBlockchain(
99
this: VM,
1010
blockchain?: Blockchain,
1111
maxBlocks?: number
12-
): Promise<void | number> {
12+
): Promise<number> {
1313
let headBlock: Block
1414
let parentState: Buffer
1515

packages/vm/tests/api/runBlockchain.spec.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ tape('runBlockchain', (t) => {
101101

102102
await vm.runBlockchain()
103103

104+
const head = await vm.blockchain.getIteratorHead()
105+
st.equal(head.hash().toString('hex'), testData.blocks[0].blockHeader.hash.slice(2))
106+
104107
st.end()
105108
})
106109

@@ -138,12 +141,16 @@ tape('runBlockchain', (t) => {
138141
await blockchain.putBlock(b2)
139142
await blockchain.putBlock(b3)
140143

144+
let head = await blockchain.getIteratorHead()
145+
st.deepEqual(head.hash(), genesisBlock.hash(), 'Iterator head should still be at genesis')
146+
141147
try {
142148
await vm.runBlockchain()
143149
st.fail('should have returned error')
144150
} catch (e: any) {
145151
st.equal(e.message, 'test')
146-
152+
head = await blockchain.getIteratorHead()
153+
st.deepEqual(head.hash(), b2.hash(), 'should have removed invalid block from head')
147154
st.end()
148155
}
149156
})

0 commit comments

Comments
 (0)