Skip to content

Commit

Permalink
pytest: Make LightningNode.fund_channel more resilient
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cdecker authored and rustyrussell committed Oct 6, 2020
1 parent 1563bbc commit 8d8b807
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions contrib/pyln-testing/pyln/testing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,21 +748,31 @@ def fund_channel(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
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)
numfunds = len(self.rpc.listfunds()['outputs'])
self.bitcoin.generate_block(1)
wait_for(lambda: len(self.rpc.listfunds()['outputs']) > numfunds)

# Now go ahead and open a channel
num_tx = len(self.bitcoin.rpc.getrawmempool())
tx = self.rpc.fundchannel(l2.info['id'], amount, announce=announce_channel)['tx']
# Now we should.
wait_for(lambda: has_funds_on_addr(addr))

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)

# Hacky way to find our output.
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:
self.wait_channel_active(scid)
Expand Down

0 comments on commit 8d8b807

Please sign in to comment.