pytest: handle case where funding tx is not tx #1.

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>
This commit is contained in:
Rusty Russell
2021-04-07 17:19:42 +09:30
committed by Christian Decker
parent c0a40b37a7
commit 402f7f90c0

View File

@@ -742,18 +742,20 @@ class LightningNode(object):
# 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']))
return '{}x1x{}'.format(self.bitcoin.rpc.getblockcount(), outnum)
for i, txid in enumerate(self.bitcoin.rpc.getblock(blockid)['tx']):
if txid == res['txid']:
txnum = i
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']]
@@ -855,10 +857,14 @@ class LightningNode(object):
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]
# Hacky way to find our output.
scid = "{}x1x{}".format(self.bitcoin.rpc.getblockcount(),
for i, txid in enumerate(self.bitcoin.rpc.getblock(blockid)['tx']):
if txid == res['txid']:
txnum = i
scid = "{}x{}x{}".format(self.bitcoin.rpc.getblockcount(),
txnum,
get_tx_p2wsh_outnum(self.bitcoin, res['tx'], amount))
if wait_for_active: