32
32
assert_is_hex_string ,
33
33
assert_is_hash_string ,
34
34
)
35
+ from test_framework .blocktools import (
36
+ create_block ,
37
+ create_coinbase ,
38
+ )
39
+ from test_framework .messages import (
40
+ msg_block ,
41
+ )
42
+ from test_framework .mininode import (
43
+ P2PInterface ,
44
+ network_thread_start ,
45
+ )
46
+
35
47
36
48
class BlockchainTest (BitcoinTestFramework ):
37
49
def set_test_params (self ):
@@ -46,6 +58,7 @@ def run_test(self):
46
58
self ._test_getdifficulty ()
47
59
self ._test_getnetworkhashps ()
48
60
self ._test_stopatheight ()
61
+ self ._test_waitforblockheight ()
49
62
assert self .nodes [0 ].verifychain (4 , 0 )
50
63
51
64
def _test_getblockchaininfo (self ):
@@ -227,6 +240,50 @@ def _test_stopatheight(self):
227
240
self .start_node (0 )
228
241
assert_equal (self .nodes [0 ].getblockcount (), 207 )
229
242
243
+ def _test_waitforblockheight (self ):
244
+ self .log .info ("Test waitforblockheight" )
245
+
246
+ node = self .nodes [0 ]
247
+
248
+ # Start a P2P connection since we'll need to create some blocks.
249
+ node .add_p2p_connection (P2PInterface ())
250
+ network_thread_start ()
251
+ node .p2p .wait_for_verack ()
252
+
253
+ current_height = node .getblock (node .getbestblockhash ())['height' ]
254
+
255
+ # Create a fork somewhere below our current height, invalidate the tip
256
+ # of that fork, and then ensure that waitforblockheight still
257
+ # works as expected.
258
+ #
259
+ # (Previously this was broken based on setting
260
+ # `rpc/blockchain.cpp:latestblock` incorrectly.)
261
+ #
262
+ b20hash = node .getblockhash (20 )
263
+ b20 = node .getblock (b20hash )
264
+
265
+ def solve_and_send_block (prevhash , height , time ):
266
+ b = create_block (prevhash , create_coinbase (height ), time )
267
+ b .solve ()
268
+ node .p2p .send_message (msg_block (b ))
269
+ node .p2p .sync_with_ping ()
270
+ return b
271
+
272
+ b21f = solve_and_send_block (int (b20hash , 16 ), 21 , b20 ['time' ] + 1 )
273
+ b22f = solve_and_send_block (b21f .sha256 , 22 , b21f .nTime + 1 )
274
+
275
+ node .invalidateblock (b22f .hash )
276
+
277
+ def assert_waitforheight (height , timeout = 2 ):
278
+ assert_equal (
279
+ node .waitforblockheight (height , timeout )['height' ],
280
+ current_height )
281
+
282
+ assert_waitforheight (0 )
283
+ assert_waitforheight (current_height - 1 )
284
+ assert_waitforheight (current_height )
285
+ assert_waitforheight (current_height + 1 )
286
+
230
287
231
288
if __name__ == '__main__' :
232
289
BlockchainTest ().main ()
0 commit comments