Skip to content

Commit 285fe9f

Browse files
committed
rpc: add test for waitforblock and waitfornewblock
1 parent b94b27c commit 285fe9f

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

test/functional/rpc_blockchain.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ def run_test(self):
9090
self._test_getdifficulty()
9191
self._test_getnetworkhashps()
9292
self._test_stopatheight()
93+
self._test_waitforblock() # also tests waitfornewblock
9394
self._test_waitforblockheight()
9495
self._test_getblock()
9596
self._test_getdeploymentinfo()
@@ -507,6 +508,38 @@ def _test_stopatheight(self):
507508
self.start_node(0)
508509
assert_equal(self.nodes[0].getblockcount(), HEIGHT + 7)
509510

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+
510543
def _test_waitforblockheight(self):
511544
self.log.info("Test waitforblockheight")
512545
node = self.nodes[0]

0 commit comments

Comments
 (0)