MCPcopy Create free account
hub / github.com/ElementsProject/lightning / test_bwatch_reorg_2_blocks

Function test_bwatch_reorg_2_blocks

tests/test_plugin.py:5516–5574  ·  view source on GitHub ↗

Test bwatch handles a 2-block reorg correctly

(node_factory, bitcoind)

Source from the content-addressed store, hash-verified

5514
5515@pytest.mark.slow_test
5516def test_bwatch_reorg_2_blocks(node_factory, bitcoind):
5517 """Test bwatch handles a 2-block reorg correctly"""
5518 l1 = node_factory.get_node(options=BWATCH_OPTS)
5519
5520 # Wait for bwatch to initialize
5521 wait_bwatch_caught_up(l1)
5522
5523 # Mine some blocks
5524 bitcoind.generate_block(5)
5525 expected_height = bitcoind.rpc.getblockcount()
5526 l1.daemon.wait_for_log(rf'Added block {expected_height} to history')
5527
5528 # Get the hash of block to reorg from (2 blocks back)
5529 height = bitcoind.rpc.getblockcount()
5530 old_block1_hash = bitcoind.rpc.getblockhash(height - 1)
5531 old_block2_hash = bitcoind.rpc.getblockhash(height)
5532 common_ancestor_hash = bitcoind.rpc.getblockhash(height - 2)
5533
5534 # Invalidate to cause 2-block reorg
5535 bitcoind.rpc.invalidateblock(old_block1_hash)
5536
5537 # Mine longer chain
5538 bitcoind.generate_block(4)
5539 time.sleep(2)
5540
5541 # bwatch should detect and handle the reorg
5542 l1.daemon.wait_for_log(r'Reorg detected', timeout=60)
5543 # Wait for bwatch to finish processing the new chain
5544 l1.daemon.wait_for_log(r'No block change')
5545
5546 # Verify bwatch's block history matches bitcoind's new chain
5547 new_height = bitcoind.rpc.getblockcount()
5548
5549 # Check that bwatch has correct number of blocks (from its start height, not genesis)
5550 initial_height = 101 # regtest starts at this height
5551 expected_blocks = new_height - initial_height + 1
5552
5553 # Wait for datastore to be fully updated
5554 wait_for(lambda: len(l1.rpc.listdatastore(['bwatch', 'block_history'])['datastore']) == expected_blocks, timeout=60)
5555
5556 ds = l1.rpc.listdatastore(['bwatch', 'block_history'])
5557
5558 # Verify the common ancestor is still present with correct hash
5559 ancestor_entry = next((e for e in ds['datastore']
5560 if e['key'][-1] == f"{height - 2:010d}"), None)
5561 assert ancestor_entry is not None
5562 assert reverse_bitcoin_hash(common_ancestor_hash) in str(ancestor_entry['hex'])
5563
5564 # Verify the old block hashes are not in datastore
5565 for entry in ds['datastore']:
5566 assert reverse_bitcoin_hash(old_block1_hash) not in str(entry['hex'])
5567 assert reverse_bitcoin_hash(old_block2_hash) not in str(entry['hex'])
5568
5569 # Verify the new tip matches bitcoind
5570 new_tip_hash = bitcoind.rpc.getblockhash(new_height)
5571 tip_entry = next((e for e in ds['datastore']
5572 if e['key'][-1] == f"{new_height:010d}"), None)
5573 assert tip_entry is not None

Callers

nothing calls this directly

Calls 8

wait_forFunction · 0.90
wait_bwatch_caught_upFunction · 0.85
nextFunction · 0.85
reverse_bitcoin_hashFunction · 0.85
generate_blockMethod · 0.80
wait_for_logMethod · 0.80
listdatastoreMethod · 0.80
get_nodeMethod · 0.45

Tested by

no test coverage detected