@@ -369,6 +369,76 @@ describe('DB Service', function() {
369369 } ) ;
370370 } ) ;
371371
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 ( 1441914000 , 1441911000 , function ( err , hashes ) {
391+ should . not . exist ( err ) ;
392+ hashes . should . deep . equal ( [ block2 . hash , block1 . hash ] ) ;
393+ done ( ) ;
394+ } ) ;
395+
396+ readStream . emit ( 'data' , {
397+ key : db . _encodeBlockIndexKey ( block2 . timestamp ) ,
398+ value : db . _encodeBlockIndexValue ( block2 . hash )
399+ } ) ;
400+
401+ readStream . emit ( 'data' , {
402+ key : db . _encodeBlockIndexKey ( block1 . timestamp ) ,
403+ value : db . _encodeBlockIndexValue ( block1 . 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+ it ( 'should give an error if the timestamp is out of range' , function ( done ) {
428+ var db = new DB ( baseConfig ) ;
429+ var readStream = new EventEmitter ( ) ;
430+ db . store = {
431+ createReadStream : sinon . stub ( ) . returns ( readStream )
432+ } ;
433+
434+ db . getBlockHashesByTimestamp ( - 1 , - 5 , function ( err , hashes ) {
435+ should . exist ( err ) ;
436+ err . message . should . equal ( 'Invalid Argument: timestamp out of bounds' ) ;
437+ done ( ) ;
438+ } ) ;
439+ } ) ;
440+ } ) ;
441+
372442 describe ( '#getPrevHash' , function ( ) {
373443 it ( 'should return prevHash from bitcoind' , function ( done ) {
374444 var db = new DB ( baseConfig ) ;
@@ -650,10 +720,22 @@ describe('DB Service', function() {
650720 batch : sinon . stub ( ) . callsArg ( 1 )
651721 } ;
652722
723+ var block = {
724+ hash : '00000000000000000d0aaf93e464ddeb503655a0750f8b9c6eed0bdf0ccfc863' ,
725+ header : {
726+ timestamp : 1441906365
727+ }
728+ } ;
729+
653730 it ( 'should call blockHandler in all services and perform operations' , function ( done ) {
654- db . runAllBlockHandlers ( ' block' , true , function ( err ) {
731+ db . runAllBlockHandlers ( block , true , function ( err ) {
655732 should . not . exist ( err ) ;
656- db . store . batch . args [ 0 ] [ 0 ] . should . deep . equal ( [ 'op1' , 'op2' , 'op3' , 'op4' , 'op5' ] ) ;
733+ var blockOp = {
734+ type : 'put' ,
735+ key : db . _encodeBlockIndexKey ( 1441906365 ) ,
736+ value : db . _encodeBlockIndexValue ( '00000000000000000d0aaf93e464ddeb503655a0750f8b9c6eed0bdf0ccfc863' )
737+ } ;
738+ db . store . batch . args [ 0 ] [ 0 ] . should . deep . equal ( [ blockOp , 'op1' , 'op2' , 'op3' , 'op4' , 'op5' ] ) ;
657739 done ( ) ;
658740 } ) ;
659741 } ) ;
@@ -663,7 +745,7 @@ describe('DB Service', function() {
663745 Service3 . prototype . blockHandler = sinon . stub ( ) . callsArgWith ( 2 , new Error ( 'error' ) ) ;
664746 db . node . services . service3 = new Service3 ( ) ;
665747
666- db . runAllBlockHandlers ( ' block' , true , function ( err ) {
748+ db . runAllBlockHandlers ( block , true , function ( err ) {
667749 should . exist ( err ) ;
668750 done ( ) ;
669751 } ) ;
@@ -675,7 +757,7 @@ describe('DB Service', function() {
675757 service3 : new Service3 ( )
676758 } ;
677759
678- db . runAllBlockHandlers ( ' block' , true , function ( err ) {
760+ db . runAllBlockHandlers ( block , true , function ( err ) {
679761 should . not . exist ( err ) ;
680762 done ( ) ;
681763 } ) ;
@@ -688,7 +770,7 @@ describe('DB Service', function() {
688770 } ;
689771
690772 ( function ( ) {
691- db . runAllBlockHandlers ( ' block' , true , function ( err ) {
773+ db . runAllBlockHandlers ( block , true , function ( err ) {
692774 should . not . exist ( err ) ;
693775 } ) ;
694776 } ) . should . throw ( 'bitcore.ErrorInvalidArgument' ) ;
@@ -701,7 +783,7 @@ describe('DB Service', function() {
701783 db . node = { } ;
702784 db . node . services = { } ;
703785 var methods = db . getAPIMethods ( ) ;
704- methods . length . should . equal ( 5 ) ;
786+ methods . length . should . equal ( 6 ) ;
705787 } ) ;
706788 } ) ;
707789
0 commit comments