Skip to content

Commit 1cfbb16

Browse files
author
MarcoFalke
committed
Merge #15108: [tests] tidy up wallet_importmulti.py
2d5f1ea [tests] move wallet util functions to wallet_util.py (John Newbery) 6be64ef [tests] tidy up wallet_importmulti.py (John Newbery) Pull request description: Cherry picks un-merged commits from #14952, which "fixes review comments from @ryanofsky here: bitcoin/bitcoin#14886 (review)" Tree-SHA512: 5f389196b0140d013a533d500f1812786a3a5cfb65980e13eaeacc459fddb55f43d05da3ab5e7cc8c997f26c0b667eed081ab6de2d125e631c70a7dd4c06e350
2 parents 0ed279c + 2d5f1ea commit 1cfbb16

File tree

3 files changed

+361
-336
lines changed

3 files changed

+361
-336
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2018 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
"""Useful util functions for testing the wallet"""
6+
from collections import namedtuple
7+
8+
from test_framework.address import (
9+
key_to_p2pkh,
10+
key_to_p2sh_p2wpkh,
11+
key_to_p2wpkh,
12+
script_to_p2sh,
13+
script_to_p2sh_p2wsh,
14+
script_to_p2wsh,
15+
)
16+
from test_framework.script import (
17+
CScript,
18+
OP_0,
19+
OP_2,
20+
OP_3,
21+
OP_CHECKMULTISIG,
22+
OP_CHECKSIG,
23+
OP_DUP,
24+
OP_EQUAL,
25+
OP_EQUALVERIFY,
26+
OP_HASH160,
27+
hash160,
28+
sha256,
29+
)
30+
from test_framework.util import hex_str_to_bytes
31+
32+
Key = namedtuple('Key', ['privkey',
33+
'pubkey',
34+
'p2pkh_script',
35+
'p2pkh_addr',
36+
'p2wpkh_script',
37+
'p2wpkh_addr',
38+
'p2sh_p2wpkh_script',
39+
'p2sh_p2wpkh_redeem_script',
40+
'p2sh_p2wpkh_addr'])
41+
42+
Multisig = namedtuple('Multisig', ['privkeys',
43+
'pubkeys',
44+
'p2sh_script',
45+
'p2sh_addr',
46+
'redeem_script',
47+
'p2wsh_script',
48+
'p2wsh_addr',
49+
'p2sh_p2wsh_script',
50+
'p2sh_p2wsh_addr'])
51+
52+
def get_key(node):
53+
"""Generate a fresh key on node
54+
55+
Returns a named tuple of privkey, pubkey and all address and scripts."""
56+
addr = node.getnewaddress()
57+
pubkey = node.getaddressinfo(addr)['pubkey']
58+
pkh = hash160(hex_str_to_bytes(pubkey))
59+
return Key(privkey=node.dumpprivkey(addr),
60+
pubkey=pubkey,
61+
p2pkh_script=CScript([OP_DUP, OP_HASH160, pkh, OP_EQUALVERIFY, OP_CHECKSIG]).hex(),
62+
p2pkh_addr=key_to_p2pkh(pubkey),
63+
p2wpkh_script=CScript([OP_0, pkh]).hex(),
64+
p2wpkh_addr=key_to_p2wpkh(pubkey),
65+
p2sh_p2wpkh_script=CScript([OP_HASH160, hash160(CScript([OP_0, pkh])), OP_EQUAL]).hex(),
66+
p2sh_p2wpkh_redeem_script=CScript([OP_0, pkh]).hex(),
67+
p2sh_p2wpkh_addr=key_to_p2sh_p2wpkh(pubkey))
68+
69+
def get_multisig(node):
70+
"""Generate a fresh 2-of-3 multisig on node
71+
72+
Returns a named tuple of privkeys, pubkeys and all address and scripts."""
73+
addrs = []
74+
pubkeys = []
75+
for _ in range(3):
76+
addr = node.getaddressinfo(node.getnewaddress())
77+
addrs.append(addr['address'])
78+
pubkeys.append(addr['pubkey'])
79+
script_code = CScript([OP_2] + [hex_str_to_bytes(pubkey) for pubkey in pubkeys] + [OP_3, OP_CHECKMULTISIG])
80+
witness_script = CScript([OP_0, sha256(script_code)])
81+
return Multisig(privkeys=[node.dumpprivkey(addr) for addr in addrs],
82+
pubkeys=pubkeys,
83+
p2sh_script=CScript([OP_HASH160, hash160(script_code), OP_EQUAL]).hex(),
84+
p2sh_addr=script_to_p2sh(script_code),
85+
redeem_script=script_code.hex(),
86+
p2wsh_script=witness_script.hex(),
87+
p2wsh_addr=script_to_p2wsh(script_code),
88+
p2sh_p2wsh_script=CScript([OP_HASH160, witness_script, OP_EQUAL]).hex(),
89+
p2sh_p2wsh_addr=script_to_p2sh_p2wsh(script_code))
90+
91+
def test_address(node, address, **kwargs):
92+
"""Get address info for `address` and test whether the returned values are as expected."""
93+
addr_info = node.getaddressinfo(address)
94+
for key, value in kwargs.items():
95+
if value is None:
96+
if key in addr_info.keys():
97+
raise AssertionError("key {} unexpectedly returned in getaddressinfo.".format(key))
98+
elif addr_info[key] != value:
99+
raise AssertionError("key {} value {} did not match expected value {}".format(key, addr_info[key], value))

test/functional/wallet_import_with_label.py

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"""
1212

1313
from test_framework.test_framework import BitcoinTestFramework
14-
from test_framework.util import assert_equal
14+
from test_framework.wallet_util import test_address
1515

1616

1717
class ImportWithLabel(BitcoinTestFramework):
@@ -32,11 +32,11 @@ def run_test(self):
3232
address = self.nodes[0].getnewaddress()
3333
label = "Test Label"
3434
self.nodes[1].importaddress(address, label)
35-
address_assert = self.nodes[1].getaddressinfo(address)
36-
37-
assert_equal(address_assert["iswatchonly"], True)
38-
assert_equal(address_assert["ismine"], False)
39-
assert_equal(address_assert["label"], label)
35+
test_address(self.nodes[1],
36+
address,
37+
iswatchonly=True,
38+
ismine=False,
39+
label=label)
4040

4141
self.log.info(
4242
"Import the watch-only address's private key without a "
@@ -45,19 +45,21 @@ def run_test(self):
4545
priv_key = self.nodes[0].dumpprivkey(address)
4646
self.nodes[1].importprivkey(priv_key)
4747

48-
assert_equal(label, self.nodes[1].getaddressinfo(address)["label"])
48+
test_address(self.nodes[1],
49+
address,
50+
label=label)
4951

5052
self.log.info(
5153
"Test importaddress without label and importprivkey with label."
5254
)
5355
self.log.info("Import a watch-only address without a label.")
5456
address2 = self.nodes[0].getnewaddress()
5557
self.nodes[1].importaddress(address2)
56-
address_assert2 = self.nodes[1].getaddressinfo(address2)
57-
58-
assert_equal(address_assert2["iswatchonly"], True)
59-
assert_equal(address_assert2["ismine"], False)
60-
assert_equal(address_assert2["label"], "")
58+
test_address(self.nodes[1],
59+
address2,
60+
iswatchonly=True,
61+
ismine=False,
62+
label="")
6163

6264
self.log.info(
6365
"Import the watch-only address's private key with a "
@@ -67,18 +69,20 @@ def run_test(self):
6769
label2 = "Test Label 2"
6870
self.nodes[1].importprivkey(priv_key2, label2)
6971

70-
assert_equal(label2, self.nodes[1].getaddressinfo(address2)["label"])
72+
test_address(self.nodes[1],
73+
address2,
74+
label=label2)
7175

7276
self.log.info("Test importaddress with label and importprivkey with label.")
7377
self.log.info("Import a watch-only address with a label.")
7478
address3 = self.nodes[0].getnewaddress()
7579
label3_addr = "Test Label 3 for importaddress"
7680
self.nodes[1].importaddress(address3, label3_addr)
77-
address_assert3 = self.nodes[1].getaddressinfo(address3)
78-
79-
assert_equal(address_assert3["iswatchonly"], True)
80-
assert_equal(address_assert3["ismine"], False)
81-
assert_equal(address_assert3["label"], label3_addr)
81+
test_address(self.nodes[1],
82+
address3,
83+
iswatchonly=True,
84+
ismine=False,
85+
label=label3_addr)
8286

8387
self.log.info(
8488
"Import the watch-only address's private key with a "
@@ -88,7 +92,9 @@ def run_test(self):
8892
label3_priv = "Test Label 3 for importprivkey"
8993
self.nodes[1].importprivkey(priv_key3, label3_priv)
9094

91-
assert_equal(label3_priv, self.nodes[1].getaddressinfo(address3)["label"])
95+
test_address(self.nodes[1],
96+
address3,
97+
label=label3_priv)
9298

9399
self.log.info(
94100
"Test importprivkey won't label new dests with the same "
@@ -98,15 +104,12 @@ def run_test(self):
98104
address4 = self.nodes[0].getnewaddress()
99105
label4_addr = "Test Label 4 for importaddress"
100106
self.nodes[1].importaddress(address4, label4_addr)
101-
address_assert4 = self.nodes[1].getaddressinfo(address4)
102-
103-
assert_equal(address_assert4["iswatchonly"], True)
104-
assert_equal(address_assert4["ismine"], False)
105-
assert_equal(address_assert4["label"], label4_addr)
106-
107-
self.log.info("Asserts address has no embedded field with dests.")
108-
109-
assert_equal(address_assert4.get("embedded"), None)
107+
test_address(self.nodes[1],
108+
address4,
109+
iswatchonly=True,
110+
ismine=False,
111+
label=label4_addr,
112+
embedded=None)
110113

111114
self.log.info(
112115
"Import the watch-only address's private key without a "
@@ -116,16 +119,14 @@ def run_test(self):
116119
)
117120
priv_key4 = self.nodes[0].dumpprivkey(address4)
118121
self.nodes[1].importprivkey(priv_key4)
119-
address_assert4 = self.nodes[1].getaddressinfo(address4)
120-
121-
assert address_assert4.get("embedded")
122-
123-
bcaddress_assert = self.nodes[1].getaddressinfo(
124-
address_assert4["embedded"]["address"]
125-
)
126-
127-
assert_equal(address_assert4["label"], label4_addr)
128-
assert_equal(bcaddress_assert["label"], "")
122+
embedded_addr = self.nodes[1].getaddressinfo(address4)['embedded']['address']
123+
124+
test_address(self.nodes[1],
125+
embedded_addr,
126+
label="")
127+
test_address(self.nodes[1],
128+
address4,
129+
label=label4_addr)
129130

130131
self.stop_nodes()
131132

0 commit comments

Comments
 (0)