@@ -348,6 +348,99 @@ tape('blockchain test', (t) => {
348
348
st . end ( )
349
349
} )
350
350
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
+
351
444
t . test ( 'should catch iterator func error' , async ( st ) => {
352
445
const { blockchain, error } = await generateBlockchain ( 25 )
353
446
st . error ( error , 'no error' )
0 commit comments