Skip to content

Commit

Permalink
pytest: handle case where funding tx is not tx #1.
Browse files Browse the repository at this point in the history
e.g. in test_closing_id we can get a spend from the first (closed) channel
in the same block as the open of the second.  Half the time, we'll choose
the wrong one as scid.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell authored and cdecker committed Apr 12, 2021
1 parent c0a40b3 commit 402f7f9
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions contrib/pyln-testing/pyln/testing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,18 +742,20 @@ def fundbalancedchannel(self, remote_node, total_capacity, announce=True):

# Make sure the fundchannel is confirmed.
num_tx = len(self.bitcoin.rpc.getrawmempool())
tx = self.rpc.fundchannel(remote_node.info['id'], chan_capacity, feerate='slow', minconf=0, announce=announce, push_msat=Millisatoshi(chan_capacity * 500))['tx']
res = self.rpc.fundchannel(remote_node.info['id'], chan_capacity, feerate='slow', minconf=0, announce=announce, push_msat=Millisatoshi(chan_capacity * 500))
wait_for(lambda: len(self.bitcoin.rpc.getrawmempool()) == num_tx + 1)
self.bitcoin.generate_block(1)
blockid = self.bitcoin.generate_block(1)[0]

# Generate the scid.
# NOTE This assumes only the coinbase and the fundchannel is
# confirmed in the block.
outnum = get_tx_p2wsh_outnum(self.bitcoin, tx, total_capacity)
outnum = get_tx_p2wsh_outnum(self.bitcoin, res['tx'], total_capacity)
if outnum is None:
raise ValueError("no outnum found. capacity {} tx {}".format(total_capacity, tx))
raise ValueError("no outnum found. capacity {} tx {}".format(total_capacity, res['tx']))

for i, txid in enumerate(self.bitcoin.rpc.getblock(blockid)['tx']):
if txid == res['txid']:
txnum = i

return '{}x1x{}'.format(self.bitcoin.rpc.getblockcount(), outnum)
return '{}x{}x{}'.format(self.bitcoin.rpc.getblockcount(), txnum, outnum)

def getactivechannels(self):
return [c for c in self.rpc.listchannels()['channels'] if c['active']]
Expand Down Expand Up @@ -855,11 +857,15 @@ def has_funds_on_addr(addr):
announce=announce_channel,
**kwargs)
wait_for(lambda: res['txid'] in self.bitcoin.rpc.getrawmempool())
self.bitcoin.generate_block(1)
blockid = self.bitcoin.generate_block(1)[0]

for i, txid in enumerate(self.bitcoin.rpc.getblock(blockid)['tx']):
if txid == res['txid']:
txnum = i

# Hacky way to find our output.
scid = "{}x1x{}".format(self.bitcoin.rpc.getblockcount(),
get_tx_p2wsh_outnum(self.bitcoin, res['tx'], amount))
scid = "{}x{}x{}".format(self.bitcoin.rpc.getblockcount(),
txnum,
get_tx_p2wsh_outnum(self.bitcoin, res['tx'], amount))

if wait_for_active:
self.wait_channel_active(scid)
Expand Down

0 comments on commit 402f7f9

Please sign in to comment.