Skip to content

Commit 152b7fb

Browse files
committed
[tests] Add a (failing) test for waitforblockheight
Demonstrates the presence of a bug in in `validation.cpp:InvalidateBlock` which will update `rpc/blockchain.cpp:latestblock` erroneously.
1 parent d09968f commit 152b7fb

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

test/functional/rpc_blockchain.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@
3232
assert_is_hex_string,
3333
assert_is_hash_string,
3434
)
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+
3547

3648
class BlockchainTest(BitcoinTestFramework):
3749
def set_test_params(self):
@@ -46,6 +58,7 @@ def run_test(self):
4658
self._test_getdifficulty()
4759
self._test_getnetworkhashps()
4860
self._test_stopatheight()
61+
self._test_waitforblockheight()
4962
assert self.nodes[0].verifychain(4, 0)
5063

5164
def _test_getblockchaininfo(self):
@@ -227,6 +240,50 @@ def _test_stopatheight(self):
227240
self.start_node(0)
228241
assert_equal(self.nodes[0].getblockcount(), 207)
229242

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+
230287

231288
if __name__ == '__main__':
232289
BlockchainTest().main()

0 commit comments

Comments
 (0)