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

Function test_block_backfill

tests/test_db.py:38–100  ·  view source on GitHub ↗

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 chann

(node_factory, bitcoind, chainparams)

Source from the content-addressed store, hash-verified

36
37@unittest.skipIf(TEST_NETWORK != 'regtest', "Address is network specific")
38def test_block_backfill(node_factory, bitcoind, chainparams):
39 """Test whether we backfill data from the blockchain correctly.
40
41 For normal operation we will process any block after the initial start
42 height, or rescan height, but for gossip we actually also need to backfill
43 the blocks we skipped initially. We do so on-demand, whenever we see a
44 channel_announcement referencing a blockheight we haven't processed yet,
45 we fetch the entire block, extract P2WSH outputs and ask `bitcoin
46 gettxout` for each of them. We then store the block header in the `blocks`
47 table and the unspent outputs in the `utxoset` table.
48
49 The test consist of two nodes opening a channel at height X, and an
50 unrelated P2WSH transaction being sent at the same height (will be used to
51 check for completeness of the backfill). Then a second node starts at
52 height X+100 and connect to one of the nodes. It should not have the block
53 in its DB before connecting. After connecting it should sync the gossip,
54 triggering a backfill of block X, and all associated P2WSH outputs.
55
56 """
57 # Need to manually open the channels later since otherwise we can't have a
58 # tx in the same block (`line_graph` with `fundchannel=True` generates
59 # blocks).
60 l1, l2 = node_factory.line_graph(2, fundchannel=False)
61
62 # Get some funds to l1
63 addr = l1.rpc.newaddr()['p2tr']
64 bitcoind.rpc.sendtoaddress(addr, 1)
65 bitcoind.generate_block(1)
66 wait_for(lambda: len(l1.rpc.listfunds()['outputs']) == 1)
67
68 # Now send the needle we will go looking for later:
69 bitcoind.rpc.sendtoaddress('bcrt1qtwxd8wg5eanumk86vfeujvp48hfkgannf77evggzct048wggsrxsum2pmm', 0.00031337)
70 l1.rpc.fundchannel(l2.info['id'], 10**6, announce=True)
71 wait_for(lambda: len(bitcoind.rpc.getrawmempool()) == 2)
72
73 # Confirm and get some distance between the funding and the l3 wallet birth date
74 bitcoind.generate_block(100)
75 wait_for(lambda: len(l1.rpc.listnodes()['nodes']) == 2)
76
77 # Start the tester node, and connect it to l1. l0 should sync the gossip
78 # and call out to `bitcoind` to backfill the block.
79 l3 = node_factory.get_node()
80 heights = [r['height'] for r in l3.db_query("SELECT height FROM blocks")]
81 assert(103 not in heights)
82
83 l3.rpc.connect(l1.info['id'], 'localhost', l1.port)
84
85 # Make sure we have backfilled the block
86 wait_for(lambda: len(l3.rpc.listnodes()['nodes']) == 2)
87 heights = [r['height'] for r in l3.db_query("SELECT height FROM blocks")]
88 assert(103 in heights)
89
90 # Make sure we also have the needle we added to the haystack above
91 assert(31337 in [r['satoshis'] for r in l3.db_query("SELECT satoshis FROM utxoset")])
92
93 # Make sure that l3 doesn't ask for more gossip and get a reply about
94 # the closed channel (hence Bad gossip msgs in log).
95 l3.daemon.wait_for_log('seeker: state = NORMAL')

Callers

nothing calls this directly

Calls 14

wait_forFunction · 0.90
only_oneFunction · 0.90
line_graphMethod · 0.80
generate_blockMethod · 0.80
db_queryMethod · 0.80
wait_for_logMethod · 0.80
newaddrMethod · 0.45
listfundsMethod · 0.45
fundchannelMethod · 0.45
listnodesMethod · 0.45
get_nodeMethod · 0.45
connectMethod · 0.45

Tested by

no test coverage detected