From 8d8b8077938deff865105b7bb8ad2488a55607ee Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Sat, 19 Sep 2020 19:32:06 +0200 Subject: [PATCH] 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. --- contrib/pyln-testing/pyln/testing/utils.py | 24 +++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/contrib/pyln-testing/pyln/testing/utils.py b/contrib/pyln-testing/pyln/testing/utils.py index 0cc34de15..6a965f128 100644 --- a/contrib/pyln-testing/pyln/testing/utils.py +++ b/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): # 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)