from fixtures import * # noqa: F401,F403 from utils import wait_for def test_db_dangling_peer_fix(node_factory): # This was taken from test_fail_unconfirmed() node. l1 = node_factory.get_node(dbfile='dangling-peer.sqlite3.xz') l2 = node_factory.get_node() # Must match entry in db assert l2.info['id'] == '022d223620a359a47ff7f7ac447c85c46c923da53389221a0054c11c1e3ca31d59' # This time it should work! (Connect *in* since l1 thinks it has UTXOs # it doesn't have). l1.rpc.connect(l2.info['id'], 'localhost', l2.port) # Make sure l2 has register connection l2.daemon.wait_for_log('Handed peer, entering loop') l2.fund_channel(l1, 200000, wait_for_active=True) def test_block_backfill(node_factory, bitcoind): """Test whether we backfill data from the blockchain correctly. For normal operation we will process any block after the initial start height, or rescan height, but for gossip we actually also need to backfill the blocks we skipped initially. We do so on-demand, whenever we see a channel_announcement referencing a blockheight we haven't processed yet, we fetch the entire block, extract P2WSH outputs and ask `bitcoin gettxout` for each of them. We then store the block header in the `blocks` table and the unspent outputs in the `utxoset` table. The test consist of two nodes opening a channel at height X, and an unrelated P2WSH transaction being sent at the same height (will be used to check for completeness of the backfill). Then a second node starts at height X+100 and connect to one of the nodes. It should not have the block in its DB before connecting. After connecting it should sync the gossip, triggering a backfill of block X, and all associated P2WSH outputs. """ # Need to manually open the channels later since otherwise we can't have a # tx in the same block (`line_graph` with `fundchannel=True` generates # blocks). l1, l2 = node_factory.line_graph(2, fundchannel=False) # Get some funds to l1 addr = l1.rpc.newaddr()['bech32'] bitcoind.rpc.sendtoaddress(addr, 1) bitcoind.generate_block(1) wait_for(lambda: len(l1.rpc.listfunds()['outputs']) == 1) # Now send the needle we will go looking for later: bitcoind.rpc.sendtoaddress('bcrt1qtwxd8wg5eanumk86vfeujvp48hfkgannf77evggzct048wggsrxsum2pmm', 0.00031337) l1.rpc.fundchannel(l2.info['id'], 10**6, announce=True) wait_for(lambda: len(bitcoind.rpc.getrawmempool()) == 2) # Confirm and get some distance between the funding and the l3 wallet birth date bitcoind.generate_block(100) wait_for(lambda: len(l1.rpc.listnodes()['nodes']) == 2) # Start the tester node, and connect it to l1. l0 should sync the gossip # and call out to `bitcoind` to backfill the block. l3 = node_factory.get_node() heights = [r['height'] for r in l3.db_query("SELECT height FROM blocks")] assert(103 not in heights) l3.rpc.connect(l1.info['id'], 'localhost', l1.port) # Make sure we have backfilled the block wait_for(lambda: len(l3.rpc.listnodes()['nodes']) == 2) heights = [r['height'] for r in l3.db_query("SELECT height FROM blocks")] assert(103 in heights) # Make sure we also have the needle we added to the haystack above assert(31337 in [r['satoshis'] for r in l3.db_query("SELECT satoshis FROM utxoset")]) # Now close the channel and make sure `l3` cleans up correctly: l1.rpc.close(l2.info['id']) bitcoind.generate_block(1) wait_for(lambda: len(l3.rpc.listchannels()['channels']) == 0)