From 4c57bf002e847f07a666a8755d2e27ce85e75446 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 15 Jan 2019 15:52:22 +1030 Subject: [PATCH] pytest: neaten fund_channel scid extraction. We used to have a bug where decoderawtransaction would fail, fixed in fedcfd661 (pytest: hand 'True' to decoderawtransaction so it doesn't get confused.). So we can remove the fallback decode, and might as well extract the ugliness into a helper function. Signed-off-by: Rusty Russell --- tests/utils.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/tests/utils.py b/tests/utils.py index 87dacddd3..0e2b55fda 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -83,6 +83,18 @@ def wait_channel_quiescent(n1, n2): wait_for(lambda: only_one(only_one(n2.rpc.listpeers(n1.info['id'])['peers'])['channels'])['htlcs'] == []) +def get_tx_p2wsh_outnum(bitcoind, tx, amount): + """Get output number of this tx which is p2wsh of amount""" + decoded = bitcoind.rpc.decoderawtransaction(tx, True) + + for out in decoded['vout']: + if out['scriptPubKey']['type'] == 'witness_v0_scripthash': + if out['value'] == Decimal(amount) / 10**8: + return out['n'] + + return None + + class TailableProc(object): """A monitorable process that we can start, stop and tail. @@ -519,19 +531,8 @@ class LightningNode(object): self.bitcoin.generate_block(1) # Hacky way to find our output. - scid = None - decoded = self.bitcoin.rpc.decoderawtransaction(tx, True) - - for out in decoded['vout']: - if out['scriptPubKey']['type'] == 'witness_v0_scripthash': - if out['value'] == Decimal(amount) / 10**8: - scid = "{}x1x{}".format(self.bitcoin.rpc.getblockcount(), out['n']) - break - - if not scid: - # Intermittent decoding failure. See if it decodes badly twice? - decoded2 = self.bitcoin.rpc.decoderawtransaction(tx) - raise ValueError("Can't find {} payment in {} (1={} 2={})".format(amount, tx, decoded, decoded2)) + scid = "{}x1x{}".format(self.bitcoin.rpc.getblockcount(), + get_tx_p2wsh_outnum(self.bitcoin, tx, amount)) if wait_for_active: # We wait until gossipd sees both local updates, as well as status NORMAL,