Test that block history is correctly rolled back during reorg
(node_factory, bitcoind)
| 5679 | |
| 5680 | @pytest.mark.slow_test |
| 5681 | def test_bwatch_block_history_rollback(node_factory, bitcoind): |
| 5682 | """Test that block history is correctly rolled back during reorg""" |
| 5683 | l1 = node_factory.get_node(options=BWATCH_OPTS) |
| 5684 | |
| 5685 | # Wait for bwatch to fully sync |
| 5686 | wait_bwatch_caught_up(l1) |
| 5687 | |
| 5688 | # Mine blocks |
| 5689 | bitcoind.generate_block(5) |
| 5690 | expected_height = bitcoind.rpc.getblockcount() |
| 5691 | |
| 5692 | # Wait for bwatch to process the new blocks |
| 5693 | l1.daemon.wait_for_log(rf'Added block {expected_height} to history', timeout=60) |
| 5694 | |
| 5695 | # Check block history exists in datastore |
| 5696 | initial_height = 101 # regtest starts at this height |
| 5697 | expected_initial_blocks = expected_height - initial_height + 1 |
| 5698 | wait_for(lambda: len(l1.rpc.listdatastore(['bwatch', 'block_history'])['datastore']) == expected_initial_blocks, timeout=60) |
| 5699 | |
| 5700 | ds = l1.rpc.listdatastore(['bwatch', 'block_history']) |
| 5701 | initial_count = len(ds['datastore']) |
| 5702 | # bwatch stores blocks from its start height (101), so we should have at least 5 new blocks |
| 5703 | assert initial_count >= 5 |
| 5704 | |
| 5705 | # Cause a 3-block reorg - save hashes of blocks that will be reorged |
| 5706 | height = bitcoind.rpc.getblockcount() |
| 5707 | common_ancestor_height = height - 3 |
| 5708 | common_ancestor_hash = bitcoind.rpc.getblockhash(common_ancestor_height) |
| 5709 | old_hashes = [bitcoind.rpc.getblockhash(h) |
| 5710 | for h in range(common_ancestor_height + 1, height + 1)] |
| 5711 | |
| 5712 | reorg_from_hash = bitcoind.rpc.getblockhash( |
| 5713 | common_ancestor_height + 1) |
| 5714 | bitcoind.rpc.invalidateblock(reorg_from_hash) |
| 5715 | bitcoind.generate_block(5) |
| 5716 | |
| 5717 | # Verify reorg was handled |
| 5718 | l1.daemon.wait_for_log(r'Reorg detected', timeout=60) |
| 5719 | # Wait for bwatch to finish syncing to the new chain |
| 5720 | l1.daemon.wait_for_log(r'No block change') |
| 5721 | |
| 5722 | # Verify block history after reorg |
| 5723 | ds = l1.rpc.listdatastore(['bwatch', 'block_history']) |
| 5724 | new_height = bitcoind.rpc.getblockcount() |
| 5725 | |
| 5726 | # Should have correct number of blocks (from its start height, not genesis) |
| 5727 | initial_height = 101 # regtest starts at this height |
| 5728 | expected_blocks = new_height - initial_height + 1 |
| 5729 | assert len(ds['datastore']) == expected_blocks |
| 5730 | |
| 5731 | # Verify the common ancestor is still present with correct hash |
| 5732 | ancestor_entry = next((e for e in ds['datastore'] |
| 5733 | if e['key'][-1] == f"{common_ancestor_height:010d}"), |
| 5734 | None) |
| 5735 | assert ancestor_entry is not None |
| 5736 | assert reverse_bitcoin_hash(common_ancestor_hash) in str(ancestor_entry['hex']) |
| 5737 | |
| 5738 | # Verify old block hashes are not present |
nothing calls this directly
no test coverage detected