@@ -90,6 +90,7 @@ def run_test(self):
90
90
self ._test_getdifficulty ()
91
91
self ._test_getnetworkhashps ()
92
92
self ._test_stopatheight ()
93
+ self ._test_waitforblock () # also tests waitfornewblock
93
94
self ._test_waitforblockheight ()
94
95
self ._test_getblock ()
95
96
self ._test_getdeploymentinfo ()
@@ -507,6 +508,38 @@ def _test_stopatheight(self):
507
508
self .start_node (0 )
508
509
assert_equal (self .nodes [0 ].getblockcount (), HEIGHT + 7 )
509
510
511
+ def _test_waitforblock (self ):
512
+ self .log .info ("Test waitforblock and waitfornewblock" )
513
+ node = self .nodes [0 ]
514
+
515
+ current_height = node .getblock (node .getbestblockhash ())['height' ]
516
+ current_hash = node .getblock (node .getbestblockhash ())['hash' ]
517
+
518
+ self .log .debug ("Roll the chain back a few blocks and then reconsider it" )
519
+ rollback_height = current_height - 100
520
+ rollback_hash = node .getblockhash (rollback_height )
521
+ rollback_header = node .getblockheader (rollback_hash )
522
+
523
+ node .invalidateblock (rollback_hash )
524
+ assert_equal (node .getblockcount (), rollback_height - 1 )
525
+
526
+ self .log .debug ("waitforblock should return the same block after its timeout" )
527
+ assert_equal (node .waitforblock (blockhash = current_hash , timeout = 1 )['hash' ], rollback_header ['previousblockhash' ])
528
+
529
+ node .reconsiderblock (rollback_hash )
530
+ # The chain has probably already been restored by the time reconsiderblock returns,
531
+ # but poll anyway.
532
+ self .wait_until (lambda : node .waitforblock (blockhash = current_hash , timeout = 100 )['hash' ] == current_hash )
533
+
534
+ # roll back again
535
+ node .invalidateblock (rollback_hash )
536
+ assert_equal (node .getblockcount (), rollback_height - 1 )
537
+
538
+ node .reconsiderblock (rollback_hash )
539
+ # The chain has probably already been restored by the time reconsiderblock returns,
540
+ # but poll anyway.
541
+ self .wait_until (lambda : node .waitfornewblock (timeout = 100 )['hash' ] == current_hash )
542
+
510
543
def _test_waitforblockheight (self ):
511
544
self .log .info ("Test waitforblockheight" )
512
545
node = self .nodes [0 ]
0 commit comments