|
34 | 34 | """ |
35 | 35 | from shutil import rmtree |
36 | 36 |
|
37 | | -from test_framework.messages import tx_from_hex |
| 37 | +from test_framework.messages import ( |
| 38 | + CBlock, |
| 39 | + from_hex, |
| 40 | + msg_block, |
| 41 | + msg_headers, |
| 42 | + tx_from_hex, |
| 43 | +) |
| 44 | +from test_framework.p2p import P2PDataStore |
38 | 45 | from test_framework.test_framework import BitcoinTestFramework |
39 | 46 | from test_framework.util import ( |
40 | 47 | assert_equal, |
@@ -304,19 +311,56 @@ def run_test(self): |
304 | 311 | self.log.info("-- Testing all indexes + reindex") |
305 | 312 | assert_equal(n2.getblockcount(), START_HEIGHT) |
306 | 313 |
|
| 314 | + # Connect a peer and start syncing the IBD chain before the snapshot |
| 315 | + # is loaded. |
| 316 | + peer = n2.add_outbound_p2p_connection(P2PDataStore(), p2p_idx=0, connection_type="outbound-full-relay") |
| 317 | + # Sending an IBD block before loading the dump so that the peer has a |
| 318 | + # last common block set. |
| 319 | + block_hash = n0.getblockhash(START_HEIGHT + 1) |
| 320 | + block_hex = n0.getblock(blockhash=block_hash, verbosity=0) |
| 321 | + block = from_hex(CBlock(), block_hex) |
| 322 | + |
| 323 | + # TODO: REMOVE DEBUG MESSAGE CHECK |
| 324 | + with n2.assert_debug_log(expected_msgs=["[FindNextBlocksToDownload] HERE"]): |
| 325 | + peer.send_and_ping(msg_headers([block])) |
| 326 | + peer.wait_until(lambda: int(block_hash, 16) in peer.getdata_requests) |
| 327 | + peer.send_and_ping(msg_block(block)) |
| 328 | + |
307 | 329 | self.log.info(f"Loading snapshot into third node from {dump_output['path']}") |
308 | 330 | loaded = n2.loadtxoutset(dump_output['path']) |
309 | 331 | assert_equal(loaded['coins_loaded'], SNAPSHOT_BASE_HEIGHT) |
310 | 332 | assert_equal(loaded['base_height'], SNAPSHOT_BASE_HEIGHT) |
311 | 333 |
|
312 | 334 | normal, snapshot = n2.getchainstates()['chainstates'] |
313 | | - assert_equal(normal['blocks'], START_HEIGHT) |
| 335 | + assert_equal(normal['blocks'], START_HEIGHT + 1) |
314 | 336 | assert_equal(normal.get('snapshot_blockhash'), None) |
315 | 337 | assert_equal(normal['validated'], True) |
316 | 338 | assert_equal(snapshot['blocks'], SNAPSHOT_BASE_HEIGHT) |
317 | 339 | assert_equal(snapshot['snapshot_blockhash'], dump_output['base_hash']) |
318 | 340 | assert_equal(snapshot['validated'], False) |
319 | 341 |
|
| 342 | + # Send all available headers to node 2 to trigger another getdata |
| 343 | + # request |
| 344 | + blocks = [] |
| 345 | + for i in range(START_HEIGHT, FINAL_HEIGHT): |
| 346 | + block_hash = n0.getblockhash(i) |
| 347 | + block_hex = n0.getblock(blockhash=block_hash, verbosity=0) |
| 348 | + block = from_hex(CBlock(), block_hex) |
| 349 | + blocks.append(block) |
| 350 | + |
| 351 | + # TODO: REMOVE DEBUG MESSAGE CHECK |
| 352 | + with n2.assert_debug_log(expected_msgs=["[FindNextBlocksToDownload] HERE"]): |
| 353 | + peer.send_and_ping(msg_headers(blocks)) |
| 354 | + |
| 355 | + # Node 2 should request the blocks following the snapshot next. It |
| 356 | + # should not ask for blocks below the snapshot (IBD blocks). |
| 357 | + assert int(n0.getblockhash(SNAPSHOT_BASE_HEIGHT + 1), 16) in peer.getdata_requests |
| 358 | + assert int(n0.getblockhash(START_HEIGHT + 2), 16) not in peer.getdata_requests |
| 359 | + assert int(n0.getblockhash(SNAPSHOT_BASE_HEIGHT - 1), 16) not in peer.getdata_requests |
| 360 | + |
| 361 | + # Disconnect to not interfere with the rest of the test |
| 362 | + peer.peer_disconnect() |
| 363 | + |
320 | 364 | self.connect_nodes(0, 2) |
321 | 365 | self.wait_until(lambda: n2.getchainstates()['chainstates'][-1]['blocks'] == FINAL_HEIGHT) |
322 | 366 | self.sync_blocks() |
|
0 commit comments