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

Function test_blockchaintrack

tests/test_misc.py:1384–1425  ·  view source on GitHub ↗

Check that we track the blockchain correctly across reorgs

(node_factory, bitcoind)

Source from the content-addressed store, hash-verified

1382
1383
1384def test_blockchaintrack(node_factory, bitcoind):
1385 """Check that we track the blockchain correctly across reorgs
1386 """
1387 l1 = node_factory.get_node(random_hsm=True)
1388 addr = l1.rpc.newaddr(addresstype='all')['p2tr']
1389
1390 ######################################################################
1391 # First failure scenario: rollback on startup doesn't work,
1392 # and we try to add a block twice when rescanning:
1393 l1.restart()
1394
1395 height = bitcoind.rpc.getblockcount() # 101
1396
1397 # At height 111 we receive an incoming payment
1398 hashes = bitcoind.generate_block(9) # 102-110
1399 bitcoind.rpc.sendtoaddress(addr, 1)
1400 time.sleep(1) # mempool is still unpredictable
1401 bitcoind.generate_block(1)
1402
1403 l1.daemon.wait_for_log(r'Owning output.* CONFIRMED')
1404 outputs = l1.rpc.listfunds()['outputs']
1405 assert len(outputs) == 1
1406
1407 ######################################################################
1408 # Second failure scenario: perform a 20 block reorg
1409 bitcoind.generate_block(10)
1410 l1.daemon.wait_for_log('Adding block {}: '.format(height + 20))
1411
1412 # Now reorg out with a longer fork of 21 blocks
1413 bitcoind.rpc.invalidateblock(hashes[0])
1414 bitcoind.wait_for_log(r'InvalidChainFound: invalid block=.* height={}'
1415 .format(height + 1))
1416 hashes = bitcoind.generate_block(30)
1417 time.sleep(1)
1418
1419 bitcoind.rpc.getblockcount()
1420 l1.daemon.wait_for_log('Adding block {}: '.format(height + 30))
1421
1422 # Our funds got reorged out, we should not have any funds that are confirmed
1423 # NOTE: sendtoaddress() sets locktime=103 and the reorg at 102 invalidates that tx
1424 # and deletes it from mempool
1425 assert [o for o in l1.rpc.listfunds()['outputs'] if o['status'] != "unconfirmed"] == []
1426
1427
1428@pytest.mark.openchannel('v1')

Callers

nothing calls this directly

Calls 6

restartMethod · 0.80
generate_blockMethod · 0.80
wait_for_logMethod · 0.80
get_nodeMethod · 0.45
newaddrMethod · 0.45
listfundsMethod · 0.45

Tested by

no test coverage detected