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

Function test_bwatch_reorg_1_block

tests/test_plugin.py:5453–5512  ·  view source on GitHub ↗

Test bwatch handles a 1-block reorg correctly

(node_factory, bitcoind)

Source from the content-addressed store, hash-verified

5451
5452
5453def test_bwatch_reorg_1_block(node_factory, bitcoind):
5454 """Test bwatch handles a 1-block reorg correctly"""
5455 l1 = node_factory.get_node(options=BWATCH_OPTS)
5456
5457 # Wait for bwatch to initialize and sync to initial tip
5458 wait_bwatch_caught_up(l1)
5459
5460 # Mine a few blocks to establish history
5461 bitcoind.generate_block(5)
5462 # Wait for bwatch to fully catch up (important: bwatch must have the block
5463 # that will be reorged, otherwise it can't detect the reorg)
5464 l1.daemon.wait_for_log(r'No block change')
5465
5466 # Get the actual number of blocks bwatch has stored before reorg
5467 ds_before = l1.rpc.listdatastore(['bwatch', 'block_history'])
5468 blocks_before = len(ds_before['datastore'])
5469
5470 # Get the hash of the last block (the one we'll reorg out)
5471 height = bitcoind.rpc.getblockcount()
5472 old_block_hash = bitcoind.rpc.getblockhash(height)
5473 common_ancestor_hash = bitcoind.rpc.getblockhash(height - 1)
5474
5475 # Invalidate the last block (1-block reorg)
5476 bitcoind.rpc.invalidateblock(old_block_hash)
5477
5478 # Mine 2 new blocks on the new chain
5479 bitcoind.generate_block(2)
5480
5481 # bwatch should detect and handle the reorg
5482 l1.daemon.wait_for_log(r'Reorg detected', timeout=60)
5483 # Persisting rolled-forward blocks is async; don't assert immediately after
5484 # "Reorg detected" (see test_bwatch_reorg_2_blocks).
5485 l1.daemon.wait_for_log(r'No block change', timeout=60)
5486
5487 # Verify bwatch's block history matches bitcoind's new chain
5488 new_height = bitcoind.rpc.getblockcount()
5489 # After reorg: removed 1 old block, added 2 new blocks, so net +1
5490 wait_for(lambda: len(l1.rpc.listdatastore(['bwatch', 'block_history'])['datastore'])
5491 == blocks_before + 1,
5492 timeout=60)
5493
5494 ds = l1.rpc.listdatastore(['bwatch', 'block_history'])
5495
5496 # Verify the common ancestor is still present with correct hash
5497 ancestor_entry = next((e for e in ds['datastore']
5498 if e['key'][-1] == f"{height - 1:010d}"), None)
5499 assert ancestor_entry is not None
5500 # Decode the block record and verify hash matches
5501 assert reverse_bitcoin_hash(common_ancestor_hash) in str(ancestor_entry['hex'])
5502
5503 # Verify the old block hash is not in datastore
5504 for entry in ds['datastore']:
5505 assert reverse_bitcoin_hash(old_block_hash) not in str(entry['hex'])
5506
5507 # Verify the new tip matches bitcoind
5508 new_tip_hash = bitcoind.rpc.getblockhash(new_height)
5509 tip_entry = next((e for e in ds['datastore']
5510 if e['key'][-1] == f"{new_height:010d}"), 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