Test bwatch handles a longer reorg (5+ blocks)
(node_factory, bitcoind)
| 5575 | |
| 5576 | |
| 5577 | def test_bwatch_reorg_long_chain(node_factory, bitcoind): |
| 5578 | """Test bwatch handles a longer reorg (5+ blocks)""" |
| 5579 | l1 = node_factory.get_node(options=BWATCH_OPTS) |
| 5580 | |
| 5581 | # Wait for bwatch to initialize and sync to initial tip |
| 5582 | wait_bwatch_caught_up(l1) |
| 5583 | |
| 5584 | # Mine 10 blocks and wait for bwatch to fully catch up |
| 5585 | bitcoind.generate_block(10) |
| 5586 | expected_height = bitcoind.rpc.getblockcount() |
| 5587 | l1.daemon.wait_for_log(rf'Added block {expected_height} to history') |
| 5588 | |
| 5589 | # Reorg 5 blocks - save hashes of blocks that will be reorged out |
| 5590 | height = bitcoind.rpc.getblockcount() |
| 5591 | common_ancestor_height = height - 5 |
| 5592 | common_ancestor_hash = bitcoind.rpc.getblockhash(common_ancestor_height) |
| 5593 | old_hashes = [bitcoind.rpc.getblockhash(h) |
| 5594 | for h in range(common_ancestor_height + 1, height + 1)] |
| 5595 | |
| 5596 | reorg_from_hash = bitcoind.rpc.getblockhash( |
| 5597 | common_ancestor_height + 1) |
| 5598 | bitcoind.rpc.invalidateblock(reorg_from_hash) |
| 5599 | |
| 5600 | # Mine longer replacement chain |
| 5601 | bitcoind.generate_block(8) |
| 5602 | time.sleep(3) |
| 5603 | |
| 5604 | # Should handle the deep reorg |
| 5605 | l1.daemon.wait_for_log(r'Reorg detected') |
| 5606 | |
| 5607 | # Verify bwatch's block history matches bitcoind's new chain |
| 5608 | new_height = bitcoind.rpc.getblockcount() |
| 5609 | |
| 5610 | # Wait for bwatch to fully sync to the new chain height |
| 5611 | # bwatch stores blocks from initial height (101), not genesis |
| 5612 | initial_height = 101 # regtest starts at this height |
| 5613 | expected_blocks = new_height - initial_height + 1 |
| 5614 | wait_for(lambda: len(l1.rpc.listdatastore(['bwatch', 'block_history'])['datastore']) == expected_blocks) |
| 5615 | |
| 5616 | ds = l1.rpc.listdatastore(['bwatch', 'block_history']) |
| 5617 | |
| 5618 | # Check that bwatch has correct number of blocks |
| 5619 | assert len(ds['datastore']) == expected_blocks |
| 5620 | |
| 5621 | # Verify the common ancestor is still present with correct hash |
| 5622 | ancestor_entry = next((e for e in ds['datastore'] |
| 5623 | if e['key'][-1] == f"{common_ancestor_height:010d}"), |
| 5624 | None) |
| 5625 | assert ancestor_entry is not None |
| 5626 | assert reverse_bitcoin_hash(common_ancestor_hash) in str(ancestor_entry['hex']) |
| 5627 | |
| 5628 | # Verify none of the old block hashes are in datastore |
| 5629 | for entry in ds['datastore']: |
| 5630 | for old_hash in old_hashes: |
| 5631 | assert reverse_bitcoin_hash(old_hash) not in str(entry['hex']) |
| 5632 | |
| 5633 | # Verify the new tip matches bitcoind |
| 5634 | new_tip_hash = bitcoind.rpc.getblockhash(new_height) |
nothing calls this directly
no test coverage detected