@@ -369,6 +369,62 @@ describe('DB Service', function() {
369
369
} ) ;
370
370
} ) ;
371
371
372
+ describe ( '#getBlockHashesByTimestamp' , function ( ) {
373
+ it ( 'should get the correct block hashes' , function ( done ) {
374
+ var db = new DB ( baseConfig ) ;
375
+ var readStream = new EventEmitter ( ) ;
376
+ db . store = {
377
+ createReadStream : sinon . stub ( ) . returns ( readStream )
378
+ } ;
379
+
380
+ var block1 = {
381
+ hash : '00000000050a6d07f583beba2d803296eb1e9d4980c4a20f206c584e89a4f02b' ,
382
+ timestamp : 1441911909
383
+ } ;
384
+
385
+ var block2 = {
386
+ hash : '000000000383752a55a0b2891ce018fd0fdc0b6352502772b034ec282b4a1bf6' ,
387
+ timestamp : 1441913112
388
+ } ;
389
+
390
+ db . getBlockHashesByTimestamp ( 1441911000 , 1441914000 , function ( err , hashes ) {
391
+ should . not . exist ( err ) ;
392
+ hashes . should . deep . equal ( [ block1 . hash , block2 . hash ] ) ;
393
+ done ( ) ;
394
+ } ) ;
395
+
396
+ readStream . emit ( 'data' , {
397
+ key : 'blk-' + block1 . timestamp ,
398
+ value : block1 . hash
399
+ } ) ;
400
+
401
+ readStream . emit ( 'data' , {
402
+ key : 'blk-' + block2 . timestamp ,
403
+ value : block2 . hash
404
+ } ) ;
405
+
406
+ readStream . emit ( 'close' ) ;
407
+ } ) ;
408
+
409
+ it ( 'should give an error if the stream has an error' , function ( done ) {
410
+ var db = new DB ( baseConfig ) ;
411
+ var readStream = new EventEmitter ( ) ;
412
+ db . store = {
413
+ createReadStream : sinon . stub ( ) . returns ( readStream )
414
+ } ;
415
+
416
+ db . getBlockHashesByTimestamp ( 1441911000 , 1441914000 , function ( err , hashes ) {
417
+ should . exist ( err ) ;
418
+ err . message . should . equal ( 'error' ) ;
419
+ done ( ) ;
420
+ } ) ;
421
+
422
+ readStream . emit ( 'error' , new Error ( 'error' ) ) ;
423
+
424
+ readStream . emit ( 'close' ) ;
425
+ } ) ;
426
+ } ) ;
427
+
372
428
describe ( '#getPrevHash' , function ( ) {
373
429
it ( 'should return prevHash from bitcoind' , function ( done ) {
374
430
var db = new DB ( baseConfig ) ;
@@ -650,10 +706,22 @@ describe('DB Service', function() {
650
706
batch : sinon . stub ( ) . callsArg ( 1 )
651
707
} ;
652
708
709
+ var block = {
710
+ hash : '00000000000000000d0aaf93e464ddeb503655a0750f8b9c6eed0bdf0ccfc863' ,
711
+ header : {
712
+ timestamp : 1441906365
713
+ }
714
+ } ;
715
+
653
716
it ( 'should call blockHandler in all services and perform operations' , function ( done ) {
654
- db . runAllBlockHandlers ( ' block' , true , function ( err ) {
717
+ db . runAllBlockHandlers ( block , true , function ( err ) {
655
718
should . not . exist ( err ) ;
656
- db . store . batch . args [ 0 ] [ 0 ] . should . deep . equal ( [ 'op1' , 'op2' , 'op3' , 'op4' , 'op5' ] ) ;
719
+ var blockOp = {
720
+ type : 'put' ,
721
+ key : 'blk-1441906365' ,
722
+ value : '00000000000000000d0aaf93e464ddeb503655a0750f8b9c6eed0bdf0ccfc863'
723
+ }
724
+ db . store . batch . args [ 0 ] [ 0 ] . should . deep . equal ( [ blockOp , 'op1' , 'op2' , 'op3' , 'op4' , 'op5' ] ) ;
657
725
done ( ) ;
658
726
} ) ;
659
727
} ) ;
@@ -663,7 +731,7 @@ describe('DB Service', function() {
663
731
Service3 . prototype . blockHandler = sinon . stub ( ) . callsArgWith ( 2 , new Error ( 'error' ) ) ;
664
732
db . node . services . service3 = new Service3 ( ) ;
665
733
666
- db . runAllBlockHandlers ( ' block' , true , function ( err ) {
734
+ db . runAllBlockHandlers ( block , true , function ( err ) {
667
735
should . exist ( err ) ;
668
736
done ( ) ;
669
737
} ) ;
@@ -675,7 +743,7 @@ describe('DB Service', function() {
675
743
service3 : new Service3 ( )
676
744
} ;
677
745
678
- db . runAllBlockHandlers ( ' block' , true , function ( err ) {
746
+ db . runAllBlockHandlers ( block , true , function ( err ) {
679
747
should . not . exist ( err ) ;
680
748
done ( ) ;
681
749
} ) ;
@@ -688,7 +756,7 @@ describe('DB Service', function() {
688
756
} ;
689
757
690
758
( function ( ) {
691
- db . runAllBlockHandlers ( ' block' , true , function ( err ) {
759
+ db . runAllBlockHandlers ( block , true , function ( err ) {
692
760
should . not . exist ( err ) ;
693
761
} ) ;
694
762
} ) . should . throw ( 'bitcore.ErrorInvalidArgument' ) ;
@@ -701,7 +769,7 @@ describe('DB Service', function() {
701
769
db . node = { } ;
702
770
db . node . services = { } ;
703
771
var methods = db . getAPIMethods ( ) ;
704
- methods . length . should . equal ( 5 ) ;
772
+ methods . length . should . equal ( 6 ) ;
705
773
} ) ;
706
774
} ) ;
707
775
0 commit comments