2019-02-21 04:52:38 +01:00
|
|
|
from fixtures import * # noqa: F401,F403
|
2019-08-05 13:52:29 +02:00
|
|
|
from utils import wait_for
|
|
|
|
|
|
|
|
|
2019-02-21 04:52:38 +01:00
|
|
|
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)
|
2019-08-05 13:52:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
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)
|
2019-08-07 12:01:18 +02:00
|
|
|
wait_for(lambda: len(l1.rpc.listfunds()['outputs']) == 1)
|
2019-08-05 13:52:29 +02:00
|
|
|
|
|
|
|
# Now send the needle we will go looking for later:
|
2019-08-07 12:01:18 +02:00
|
|
|
bitcoind.rpc.sendtoaddress('bcrt1qtwxd8wg5eanumk86vfeujvp48hfkgannf77evggzct048wggsrxsum2pmm', 0.00031337)
|
2019-08-05 13:52:29 +02:00
|
|
|
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)
|
|
|
|
|
2019-08-07 12:01:18 +02:00
|
|
|
# Make sure we have backfilled the block
|
2019-08-05 13:52:29 +02:00
|
|
|
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)
|
2019-08-07 12:01:18 +02:00
|
|
|
|
|
|
|
# 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)
|