Browse Source

pytest: Make LightningNode.fund_channel more resilient

It was really flaky, especially under `test_mpp_interference_2`, most likely
due to multiple calls to `fund_channel`. This commit looks for the specific
txids in the `listfunds` output and the `getrawmempool` output, avoiding
strange artifacts from multiple calls.
travis-experimental
Christian Decker 4 years ago
committed by Rusty Russell
parent
commit
8d8b807793
  1. 24
      contrib/pyln-testing/pyln/testing/utils.py

24
contrib/pyln-testing/pyln/testing/utils.py

@ -748,21 +748,31 @@ class LightningNode(object):
def fundchannel(self, l2, amount, wait_for_active=True, announce_channel=True): def fundchannel(self, l2, amount, wait_for_active=True, announce_channel=True):
# Give yourself some funds to work with # Give yourself some funds to work with
addr = self.rpc.newaddr()['bech32'] addr = self.rpc.newaddr()['bech32']
def has_funds_on_addr(addr):
"""Check if the given address has funds in the internal wallet.
"""
outs = self.rpc.listfunds()['outputs']
addrs = [o['address'] for o in outs]
return addr in addrs
# We should not have funds on that address yet, we just generated it.
assert(not has_funds_on_addr(addr))
self.bitcoin.rpc.sendtoaddress(addr, (amount + 1000000) / 10**8) self.bitcoin.rpc.sendtoaddress(addr, (amount + 1000000) / 10**8)
numfunds = len(self.rpc.listfunds()['outputs'])
self.bitcoin.generate_block(1) self.bitcoin.generate_block(1)
wait_for(lambda: len(self.rpc.listfunds()['outputs']) > numfunds)
# Now go ahead and open a channel # Now we should.
num_tx = len(self.bitcoin.rpc.getrawmempool()) wait_for(lambda: has_funds_on_addr(addr))
tx = self.rpc.fundchannel(l2.info['id'], amount, announce=announce_channel)['tx']
wait_for(lambda: len(self.bitcoin.rpc.getrawmempool()) == num_tx + 1) # Now go ahead and open a channel
res = self.rpc.fundchannel(l2.info['id'], amount, announce=announce_channel)
wait_for(lambda: res['txid'] in self.bitcoin.rpc.getrawmempool())
self.bitcoin.generate_block(1) self.bitcoin.generate_block(1)
# Hacky way to find our output. # Hacky way to find our output.
scid = "{}x1x{}".format(self.bitcoin.rpc.getblockcount(), scid = "{}x1x{}".format(self.bitcoin.rpc.getblockcount(),
get_tx_p2wsh_outnum(self.bitcoin, tx, amount)) get_tx_p2wsh_outnum(self.bitcoin, res['tx'], amount))
if wait_for_active: if wait_for_active:
self.wait_channel_active(scid) self.wait_channel_active(scid)

Loading…
Cancel
Save