Skip to content

Commit 5497bc0

Browse files
blockchain: move loop check
blockchain: add setHead / iterator tests blockchain: remove unnecessary iterator logic
1 parent f16177e commit 5497bc0

File tree

2 files changed

+99
-7
lines changed

2 files changed

+99
-7
lines changed

packages/blockchain/src/index.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ export default class Blockchain implements BlockchainInterface {
736736
*
737737
* @param name - Name of the state root head
738738
* @param onBlock - Function called on each block with params (block, reorg)
739-
* @param maxBlocks - How many blocks to run. By default, run all blocks in the canonical chain.
739+
* @param maxBlocks - How many blocks to run. By default, run all unprocessed blocks in the canonical chain.
740740
*/
741741
async iterator(name: string, onBlock: OnBlock, maxBlocks?: number) {
742742
return this._iterator(name, onBlock, maxBlocks)
@@ -754,12 +754,15 @@ export default class Blockchain implements BlockchainInterface {
754754
return
755755
}
756756

757+
if (maxBlocks && maxBlocks < 0) {
758+
throw 'If maxBlocks is provided, it has to be a non-negative number'
759+
}
760+
757761
const number = await this.dbManager.hashToNumber(blockHash)
758762
const blockNumber = number.addn(1)
759763
let blocksRanCounter = 0
760764

761-
// eslint-disable-next-line no-constant-condition
762-
while (true) {
765+
while (maxBlocks !== blocksRanCounter) {
763766
try {
764767
const block = await this._getBlock(blockNumber)
765768

@@ -773,9 +776,6 @@ export default class Blockchain implements BlockchainInterface {
773776
await onBlock(block, reorg)
774777
blockNumber.iaddn(1)
775778
blocksRanCounter++
776-
if (maxBlocks && blocksRanCounter == maxBlocks) {
777-
break
778-
}
779779
} catch (error) {
780780
if (error.type !== 'NotFoundError') {
781781
throw error
@@ -794,7 +794,6 @@ export default class Blockchain implements BlockchainInterface {
794794
* @param tag - The tag to save the headHash to
795795
* @param headHash - The head hash to save
796796
*/
797-
798797
async setHead(tag: string, headHash: Buffer) {
799798
await this.initAndLock<void>(async () => {
800799
this._heads[tag] = headHash

packages/blockchain/test/index.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,99 @@ tape('blockchain test', (t) => {
348348
st.end()
349349
})
350350

351+
t.test(
352+
'should iterate through maxBlocks blocks if maxBlocks parameter is provided',
353+
async (st) => {
354+
const { blockchain, blocks, error } = await generateBlockchain(25)
355+
st.error(error, 'no error')
356+
let i = 0
357+
await blockchain.iterator(
358+
'test',
359+
(block: Block) => {
360+
if (block.hash().equals(blocks[i + 1].hash())) {
361+
i++
362+
}
363+
},
364+
5
365+
)
366+
st.equals(i, 5)
367+
st.end()
368+
}
369+
)
370+
371+
t.test(
372+
'should iterate through 0 blocks in case 0 maxBlocks parameter is provided',
373+
async (st) => {
374+
const { blockchain, blocks, error } = await generateBlockchain(25)
375+
st.error(error, 'no error')
376+
let i = 0
377+
await blockchain
378+
.iterator(
379+
'test',
380+
(block: Block) => {
381+
if (block.hash().equals(blocks[i + 1].hash())) {
382+
i++
383+
}
384+
},
385+
0
386+
)
387+
.catch(() => {
388+
st.fail('Promise cannot throw when running 0 blocks')
389+
})
390+
st.equals(i, 0)
391+
st.end()
392+
}
393+
)
394+
395+
t.test('should throw on a negative maxBlocks parameter in iterator', async (st) => {
396+
const { blockchain, blocks, error } = await generateBlockchain(25)
397+
st.error(error, 'no error')
398+
let i = 0
399+
await blockchain
400+
.iterator(
401+
'test',
402+
(block: Block) => {
403+
if (block.hash().equals(blocks[i + 1].hash())) {
404+
i++
405+
}
406+
},
407+
-1
408+
)
409+
.catch(() => {
410+
st.end()
411+
})
412+
// Note: if st.end() is not called (Promise did not throw), then this test fails, as it does not end.
413+
})
414+
415+
t.test('should test setHead method', async (st) => {
416+
const { blockchain, blocks, error } = await generateBlockchain(25)
417+
st.error(error, 'no error')
418+
419+
const headBlockIndex = 5
420+
421+
const headHash = blocks[headBlockIndex].hash()
422+
await blockchain.setHead('myHead', headHash)
423+
const currentHeadBlock = await blockchain.getHead('myHead')
424+
425+
st.ok(headHash.equals(currentHeadBlock.hash()), 'head hash equals the provided head hash')
426+
427+
let i = 0
428+
// check that iterator starts from this head block
429+
await blockchain.iterator(
430+
'myHead',
431+
(block: Block) => {
432+
if (block.hash().equals(blocks[headBlockIndex + 1].hash())) {
433+
i++
434+
}
435+
},
436+
5
437+
)
438+
439+
st.equals(i, 1)
440+
441+
st.end()
442+
})
443+
351444
t.test('should catch iterator func error', async (st) => {
352445
const { blockchain, error } = await generateBlockchain(25)
353446
st.error(error, 'no error')

0 commit comments

Comments
 (0)