From b352df417987b4e93d69e800c8ff3097409a5422 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sun, 18 Apr 2021 14:08:22 +0930 Subject: [PATCH] pytest: make it work with latest bitcoind master branch. They seem to have changed the JSON output. Signed-off-by: Rusty Russell --- tests/test_closing.py | 8 ++++---- tests/test_connection.py | 3 ++- tests/test_misc.py | 4 ++-- tests/test_wallet.py | 22 +++++++++++----------- tests/utils.py | 9 +++++++++ 5 files changed, 28 insertions(+), 18 deletions(-) diff --git a/tests/test_closing.py b/tests/test_closing.py index 12745ed9b..94c82584e 100644 --- a/tests/test_closing.py +++ b/tests/test_closing.py @@ -6,7 +6,7 @@ from pyln.testing.utils import SLOW_MACHINE from utils import ( only_one, sync_blockheight, wait_for, DEVELOPER, TIMEOUT, account_balance, first_channel_id, basic_fee, TEST_NETWORK, - EXPERIMENTAL_FEATURES, EXPERIMENTAL_DUAL_FUND, + EXPERIMENTAL_FEATURES, EXPERIMENTAL_DUAL_FUND, scriptpubkey_addr ) import os @@ -372,7 +372,7 @@ def test_closing_specified_destination(node_factory, bitcoind, chainparams): output_num2 = [o for o in outputs if o['txid'] == closetx][0]['output'] output_num1 = 0 if output_num2 == 1 else 1 # Check the another address is addr - assert addr == bitcoind.rpc.gettxout(closetx, output_num1)['scriptPubKey']['addresses'][0] + assert addr == scriptpubkey_addr(bitcoind.rpc.gettxout(closetx, output_num1)['scriptPubKey']) assert 1 == bitcoind.rpc.gettxout(closetx, output_num1)['confirmations'] @@ -2513,7 +2513,7 @@ def test_permfail(node_factory, bitcoind): # Check that the all the addresses match what we generated ourselves: for o in l1.rpc.listfunds()['outputs']: txout = bitcoind.rpc.gettxout(o['txid'], o['output']) - addr = txout['scriptPubKey']['addresses'][0] + addr = scriptpubkey_addr(txout['scriptPubKey']) assert(addr == o['address']) addr = l1.bitcoin.getnewaddress() @@ -2713,7 +2713,7 @@ def test_shutdown_alternate_txid(node_factory, bitcoind): # Gotta figure out which output manually :( tx = bitcoind.rpc.getrawtransaction(txid, 1) for n, out in enumerate(tx['vout']): - if 'addresses' in out['scriptPubKey'] and out['scriptPubKey']['addresses'][0] == addr: + if scriptpubkey_addr(out['scriptPubKey']) == addr: txout = n bitcoind.generate_block(1, wait_for_mempool=1) diff --git a/tests/test_connection.py b/tests/test_connection.py index c0f67bcc0..e47c27a87 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -8,6 +8,7 @@ from utils import ( expected_peer_features, expected_node_features, expected_channel_features, check_coin_moves, first_channel_id, account_balance, basic_fee, + scriptpubkey_addr, EXPERIMENTAL_FEATURES, EXPERIMENTAL_DUAL_FUND ) from pyln.testing.utils import SLOW_MACHINE, VALGRIND @@ -1304,7 +1305,7 @@ def test_funding_close_upfront(node_factory, bitcoind): assert r['type'] == 'mutual' tx = bitcoind.rpc.decoderawtransaction(r['tx']) - addrs = [vout['scriptPubKey']['addresses'][0] for vout in tx['vout']] + addrs = [scriptpubkey_addr(vout['scriptPubKey']) for vout in tx['vout']] bitcoind.generate_block(1, wait_for_mempool=[r['txid']]) sync_blockheight(bitcoind, [l1, l2]) return addrs diff --git a/tests/test_misc.py b/tests/test_misc.py index 36a674496..e85b6c8a3 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -10,7 +10,7 @@ from pyln.testing.utils import ( wait_for, TailableProc, env ) from utils import ( - check_coin_moves, account_balance + check_coin_moves, account_balance, scriptpubkey_addr, ) from ephemeral_port_reserve import reserve from utils import EXPERIMENTAL_FEATURES @@ -483,7 +483,7 @@ def test_bech32_funding(node_factory, chainparams): def is_p2wpkh(output): return output['type'] == 'witness_v0_keyhash' and \ - address == only_one(output['addresses']) + address == scriptpubkey_addr(output) assert any(is_p2wpkh(output['scriptPubKey']) for output in wallettx['vout']) assert only_one(fundingtx['vin'])['txid'] == res['wallettxid'] diff --git a/tests/test_wallet.py b/tests/test_wallet.py index 4e02b2bb7..d94fbaedf 100644 --- a/tests/test_wallet.py +++ b/tests/test_wallet.py @@ -6,7 +6,7 @@ from flaky import flaky # noqa: F401 from pyln.client import RpcError, Millisatoshi from utils import ( only_one, wait_for, sync_blockheight, EXPERIMENTAL_FEATURES, - VALGRIND, check_coin_moves, TailableProc + VALGRIND, check_coin_moves, TailableProc, scriptpubkey_addr, ) import os @@ -288,7 +288,7 @@ def test_txprepare(node_factory, bitcoind, chainparams): for i, o in enumerate(decode['vout']): if i == outnum: assert o['scriptPubKey']['type'] == 'witness_v0_keyhash' - assert o['scriptPubKey']['addresses'] == [addr] + assert scriptpubkey_addr(o['scriptPubKey']) == addr else: assert o['scriptPubKey']['type'] in ['witness_v0_keyhash', 'fee'] @@ -304,7 +304,7 @@ def test_txprepare(node_factory, bitcoind, chainparams): assert decode['vout'][0]['value'] < Decimal(amount * 6) / 10**8 assert decode['vout'][0]['value'] > Decimal(amount * 6) / 10**8 - Decimal(0.0002) assert decode['vout'][0]['scriptPubKey']['type'] == 'witness_v0_keyhash' - assert decode['vout'][0]['scriptPubKey']['addresses'] == [addr] + assert scriptpubkey_addr(decode['vout'][0]['scriptPubKey']) == addr # If I cancel the first one, I can get those first 4 outputs. discard = l1.rpc.txdiscard(prep['txid']) @@ -322,7 +322,7 @@ def test_txprepare(node_factory, bitcoind, chainparams): assert decode['vout'][0]['value'] < Decimal(amount * 4) / 10**8 assert decode['vout'][0]['value'] > Decimal(amount * 4) / 10**8 - Decimal(0.0002) assert decode['vout'][0]['scriptPubKey']['type'] == 'witness_v0_keyhash' - assert decode['vout'][0]['scriptPubKey']['addresses'] == [addr] + assert scriptpubkey_addr(decode['vout'][0]['scriptPubKey']) == addr # Cannot discard twice. with pytest.raises(RpcError, match=r'not an unreleased txid'): @@ -342,7 +342,7 @@ def test_txprepare(node_factory, bitcoind, chainparams): assert decode['vout'][0]['value'] < Decimal(amount * 10) / 10**8 assert decode['vout'][0]['value'] > Decimal(amount * 10) / 10**8 - Decimal(0.0003) assert decode['vout'][0]['scriptPubKey']['type'] == 'witness_v0_keyhash' - assert decode['vout'][0]['scriptPubKey']['addresses'] == [addr] + assert scriptpubkey_addr(decode['vout'][0]['scriptPubKey']) == addr l1.rpc.txdiscard(prep4['txid']) # Try passing in a utxo set @@ -373,12 +373,12 @@ def test_txprepare(node_factory, bitcoind, chainparams): for vout in decode['vout']: if vout['scriptPubKey']['type'] == 'fee': continue - if vout['scriptPubKey']['addresses'] == [addr]: + if scriptpubkey_addr(vout['scriptPubKey']) == addr: changeout = vout assert changeout['value'] == Decimal(amount * 3.5) / 10**8 assert changeout['scriptPubKey']['type'] == 'witness_v0_keyhash' - assert changeout['scriptPubKey']['addresses'] == [addr] + assert scriptpubkey_addr(changeout['scriptPubKey']) == addr # Discard prep4 and get all funds again l1.rpc.txdiscard(prep5['txid']) @@ -407,10 +407,10 @@ def test_txprepare(node_factory, bitcoind, chainparams): changenum = i - 1 assert decode['vout'][outnum1]['scriptPubKey']['type'] == 'witness_v0_keyhash' - assert decode['vout'][outnum1]['scriptPubKey']['addresses'] == [addr] + assert scriptpubkey_addr(decode['vout'][outnum1]['scriptPubKey']) == addr assert decode['vout'][outnum2]['scriptPubKey']['type'] == 'witness_v0_keyhash' - assert decode['vout'][outnum2]['scriptPubKey']['addresses'] == [addr] + assert scriptpubkey_addr(decode['vout'][outnum2]['scriptPubKey']) == addr assert decode['vout'][changenum]['scriptPubKey']['type'] == 'witness_v0_keyhash' @@ -909,7 +909,7 @@ def test_txsend(node_factory, bitcoind, chainparams): wait_for(lambda: len(l1.rpc.listfunds()['outputs']) == 10 - len(decode['vin']) + 1) # Change address should appear in listfunds() - assert decode['vout'][changenum]['scriptPubKey']['addresses'][0] in [f['address'] for f in l1.rpc.listfunds()['outputs']] + assert scriptpubkey_addr(decode['vout'][changenum]['scriptPubKey']) in [f['address'] for f in l1.rpc.listfunds()['outputs']] @unittest.skipIf(TEST_NETWORK != 'regtest', "Fee outputs throw off our output matching logic") @@ -1331,7 +1331,7 @@ def test_repro_4258(node_factory, bitcoind): assert(len(tx['vout']) == 1) o0 = tx['vout'][0] - assert(o0['scriptPubKey']['addresses'] == [addr]) + assert(scriptpubkey_addr(o0['scriptPubKey']) == addr) assert(len(tx['vin']) == 1) i0 = tx['vin'][0] diff --git a/tests/utils.py b/tests/utils.py index 9f8cf046d..48394f1d3 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -143,3 +143,12 @@ def basic_fee(feerate): else: weight = 724 return (weight * feerate) // 1000 + + +def scriptpubkey_addr(scriptpubkey): + if 'addresses' in scriptpubkey: + return scriptpubkey['addresses'][0] + elif 'address' in scriptpubkey: + # Modern bitcoin (at least, git master) + return scriptpubkey['address'] + return None