Skip to content

Commit 33f5fc3

Browse files
committed
test: add rpc getaddressinfo labels test coverage
1 parent 0f3539a commit 33f5fc3

File tree

6 files changed

+58
-22
lines changed

6 files changed

+58
-22
lines changed

test/functional/test_framework/wallet_util.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ def get_multisig(node):
8888
p2sh_p2wsh_script=CScript([OP_HASH160, witness_script, OP_EQUAL]).hex(),
8989
p2sh_p2wsh_addr=script_to_p2sh_p2wsh(script_code))
9090

91+
def labels_value(name="", purpose="receive"):
92+
"""Generate a getaddressinfo labels array from a name and purpose.
93+
Often used as the value of a labels kwarg for calling test_address below."""
94+
return [{"name": name, "purpose": purpose}]
95+
9196
def test_address(node, address, **kwargs):
9297
"""Get address info for `address` and test whether the returned values are as expected."""
9398
addr_info = node.getaddressinfo(address)

test/functional/wallet_basic.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
connect_nodes,
1616
wait_until,
1717
)
18+
from test_framework.wallet_util import (
19+
labels_value,
20+
test_address,
21+
)
1822

1923

2024
class WalletTest(BitcoinTestFramework):
@@ -390,7 +394,7 @@ def run_test(self):
390394
for label in [u'рыба', u'𝅘𝅥𝅯']:
391395
addr = self.nodes[0].getnewaddress()
392396
self.nodes[0].setlabel(addr, label)
393-
assert_equal(self.nodes[0].getaddressinfo(addr)['label'], label)
397+
test_address(self.nodes[0], addr, label=label, labels=labels_value(name=label))
394398
assert label in self.nodes[0].listlabels()
395399
self.nodes[0].rpc.ensure_ascii = True # restore to default
396400

test/functional/wallet_import_with_label.py

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

1313
from test_framework.test_framework import BitcoinTestFramework
14-
from test_framework.wallet_util import test_address
14+
from test_framework.wallet_util import (
15+
labels_value,
16+
test_address,
17+
)
1518

1619

1720
class ImportWithLabel(BitcoinTestFramework):
@@ -36,7 +39,8 @@ def run_test(self):
3639
address,
3740
iswatchonly=True,
3841
ismine=False,
39-
label=label)
42+
label=label,
43+
labels=labels_value(name=label))
4044

4145
self.log.info(
4246
"Import the watch-only address's private key without a "
@@ -47,7 +51,8 @@ def run_test(self):
4751

4852
test_address(self.nodes[1],
4953
address,
50-
label=label)
54+
label=label,
55+
labels=labels_value(name=label))
5156

5257
self.log.info(
5358
"Test importaddress without label and importprivkey with label."
@@ -59,7 +64,8 @@ def run_test(self):
5964
address2,
6065
iswatchonly=True,
6166
ismine=False,
62-
label="")
67+
label="",
68+
labels=labels_value())
6369

6470
self.log.info(
6571
"Import the watch-only address's private key with a "
@@ -71,7 +77,8 @@ def run_test(self):
7177

7278
test_address(self.nodes[1],
7379
address2,
74-
label=label2)
80+
label=label2,
81+
labels=labels_value(name=label2))
7582

7683
self.log.info("Test importaddress with label and importprivkey with label.")
7784
self.log.info("Import a watch-only address with a label.")
@@ -82,7 +89,8 @@ def run_test(self):
8289
address3,
8390
iswatchonly=True,
8491
ismine=False,
85-
label=label3_addr)
92+
label=label3_addr,
93+
labels=labels_value(name=label3_addr))
8694

8795
self.log.info(
8896
"Import the watch-only address's private key with a "
@@ -94,7 +102,8 @@ def run_test(self):
94102

95103
test_address(self.nodes[1],
96104
address3,
97-
label=label3_priv)
105+
label=label3_priv,
106+
labels=labels_value(name=label3_priv))
98107

99108
self.log.info(
100109
"Test importprivkey won't label new dests with the same "
@@ -109,6 +118,7 @@ def run_test(self):
109118
iswatchonly=True,
110119
ismine=False,
111120
label=label4_addr,
121+
labels=labels_value(name=label4_addr),
112122
embedded=None)
113123

114124
self.log.info(
@@ -123,10 +133,13 @@ def run_test(self):
123133

124134
test_address(self.nodes[1],
125135
embedded_addr,
126-
label="")
136+
label="",
137+
labels=labels_value())
138+
127139
test_address(self.nodes[1],
128140
address4,
129-
label=label4_addr)
141+
label=label4_addr,
142+
labels=labels_value(name=label4_addr))
130143

131144
self.stop_nodes()
132145

test/functional/wallet_importmulti.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from test_framework.wallet_util import (
3030
get_key,
3131
get_multisig,
32+
labels_value,
3233
test_address,
3334
)
3435

@@ -121,7 +122,7 @@ def run_test(self):
121122
self.test_importmulti({"scriptPubKey": key.p2pkh_script,
122123
"timestamp": "now",
123124
"internal": True,
124-
"label": "Example label"},
125+
"label": "Unsuccessful labelling for internal addresses"},
125126
success=False,
126127
error_code=-8,
127128
error_message='Internal addresses should not have a label')
@@ -550,25 +551,27 @@ def run_test(self):
550551
self.log.info("Should not import a p2sh-p2wpkh address from descriptor without checksum and private key")
551552
self.test_importmulti({"desc": "sh(wpkh(" + key.pubkey + "))",
552553
"timestamp": "now",
553-
"label": "Descriptor import test",
554+
"label": "Unsuccessful P2SH-P2WPKH descriptor import",
554555
"keys": [key.privkey]},
555556
success=False,
556557
error_code=-5,
557558
error_message="Missing checksum")
558559

559560
# Test importing of a P2SH-P2WPKH address via descriptor + private key
560561
key = get_key(self.nodes[0])
562+
p2sh_p2wpkh_label = "Successful P2SH-P2WPKH descriptor import"
561563
self.log.info("Should import a p2sh-p2wpkh address from descriptor and private key")
562564
self.test_importmulti({"desc": descsum_create("sh(wpkh(" + key.pubkey + "))"),
563565
"timestamp": "now",
564-
"label": "Descriptor import test",
566+
"label": p2sh_p2wpkh_label,
565567
"keys": [key.privkey]},
566568
success=True)
567569
test_address(self.nodes[1],
568570
key.p2sh_p2wpkh_addr,
569571
solvable=True,
570572
ismine=True,
571-
label="Descriptor import test")
573+
label=p2sh_p2wpkh_label,
574+
labels=labels_value(name=p2sh_p2wpkh_label))
572575

573576
# Test ranged descriptor fails if range is not specified
574577
xpriv = "tprv8ZgxMBicQKsPeuVhWwi6wuMQGfPKi9Li5GtX35jVNknACgqe3CY4g5xgkfDDJcmtF7o1QnxWDRYw4H5P26PXq7sbcUkEqeR4fg3Kxp2tigg"
@@ -628,17 +631,19 @@ def run_test(self):
628631

629632
# Test importing of a P2PKH address via descriptor
630633
key = get_key(self.nodes[0])
634+
p2pkh_label = "P2PKH descriptor import"
631635
self.log.info("Should import a p2pkh address from descriptor")
632636
self.test_importmulti({"desc": descsum_create("pkh(" + key.pubkey + ")"),
633637
"timestamp": "now",
634-
"label": "Descriptor import test"},
638+
"label": p2pkh_label},
635639
True,
636640
warnings=["Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag."])
637641
test_address(self.nodes[1],
638642
key.p2pkh_addr,
639643
solvable=True,
640644
ismine=False,
641-
label="Descriptor import test")
645+
label=p2pkh_label,
646+
labels=labels_value(name=p2pkh_label))
642647

643648
# Test import fails if both desc and scriptPubKey are provided
644649
key = get_key(self.nodes[0])

test/functional/wallet_labels.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
from test_framework.test_framework import BitcoinTestFramework
1515
from test_framework.util import assert_equal, assert_raises_rpc_error
16+
from test_framework.wallet_util import (
17+
labels_value,
18+
test_address,
19+
)
1620

1721
class WalletLabelsTest(BitcoinTestFramework):
1822
def set_test_params(self):
@@ -153,11 +157,12 @@ def verify(self, node):
153157
if self.receive_address is not None:
154158
assert self.receive_address in self.addresses
155159
for address in self.addresses:
156-
assert_equal(
157-
node.getaddressinfo(address)['labels'][0],
158-
{"name": self.name,
159-
"purpose": self.purpose[address]})
160-
assert_equal(node.getaddressinfo(address)['label'], self.name)
160+
test_address(
161+
node,
162+
address,
163+
label=self.name,
164+
labels=labels_value(name=self.name, purpose=self.purpose[address])
165+
)
161166
assert self.name in node.listlabels()
162167
assert_equal(
163168
node.getaddressesbylabel(self.name),

test/functional/wallet_listreceivedby.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
assert_equal,
1212
assert_raises_rpc_error,
1313
)
14+
from test_framework.wallet_util import (
15+
labels_value,
16+
test_address,
17+
)
1418

1519

1620
class ReceivedByTest(BitcoinTestFramework):
@@ -127,7 +131,7 @@ def run_test(self):
127131
# set pre-state
128132
label = ''
129133
address = self.nodes[1].getnewaddress()
130-
assert_equal(self.nodes[1].getaddressinfo(address)['label'], label)
134+
test_address(self.nodes[1], address, label=label, labels=labels_value(name=label))
131135
received_by_label_json = [r for r in self.nodes[1].listreceivedbylabel() if r["label"] == label][0]
132136
balance_by_label = self.nodes[1].getreceivedbylabel(label)
133137

0 commit comments

Comments
 (0)