Skip to content

Commit 68b48f6

Browse files
committed
Add RPC tests for new wallet label APIs
1 parent aff7927 commit 68b48f6

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
# vv Tests less than 2m vv
6565
'wallet.py',
6666
'wallet-accounts.py',
67+
'wallet-labels.py',
6768
'p2p-segwit.py',
6869
'wallet-dump.py',
6970
'listtransactions.py',

test/functional/wallet-labels.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2017 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+
"""Test the wallet label API"""
6+
7+
from test_framework.test_framework import BitcoinTestFramework
8+
from test_framework.util import assert_equal
9+
10+
11+
class Label:
12+
def __init__(self, name):
13+
self.name = name
14+
# Current default "label address" associated with this label.
15+
self.label_address = None
16+
# List of all addresses assigned with this label
17+
self.addresses = []
18+
19+
def add_address(self, address, is_label_address):
20+
assert_equal(address not in self.addresses, True)
21+
if is_label_address:
22+
self.label_address = address
23+
self.addresses.append(address)
24+
25+
def verify(self, node):
26+
if self.label_address is not None:
27+
assert self.label_address in self.addresses
28+
assert_equal(node.getlabeladdress(self.name), self.label_address)
29+
30+
for address in self.addresses:
31+
assert_equal(node.getaccount(address), self.name)
32+
33+
assert_equal(
34+
set(node.getaddressesbyaccount(self.name)), set(self.addresses))
35+
36+
37+
def overwrite_label(node, old_label, address_idx, is_label_address, new_label):
38+
address = old_label.addresses[address_idx]
39+
assert_equal(is_label_address, address == old_label.label_address)
40+
41+
node.setlabel(address, new_label.name)
42+
43+
if old_label.name != new_label.name:
44+
del old_label.addresses[address_idx]
45+
new_label.add_address(address, False)
46+
47+
# Calling setlabel on an address which was previously the default
48+
# "label address" of a different label should cause that label to no
49+
# longer have any default "label address" at all, and for
50+
# getlabeladdress to return a brand new address.
51+
if is_label_address:
52+
new_address = node.getlabeladdress(old_label.name)
53+
assert_equal(new_address not in new_label.addresses, True)
54+
old_label.add_address(new_address, True)
55+
56+
old_label.verify(node)
57+
new_label.verify(node)
58+
59+
60+
class WalletLabelsTest(BitcoinTestFramework):
61+
def set_test_params(self):
62+
self.setup_clean_chain = True
63+
self.num_nodes = 1
64+
65+
def run_test(self):
66+
node = self.nodes[0]
67+
amount_to_send = 1.0
68+
labels = [Label(name) for name in ("a", "b", "c", "d", "e")]
69+
70+
# Check that there's no UTXO on the node, and generate a spendable
71+
# balance.
72+
assert_equal(len(node.listunspent()), 0)
73+
node.generate(101)
74+
assert_equal(node.getbalance(), 50)
75+
76+
# Create an address for each label and make sure subsequent label API
77+
# calls recognize the association.
78+
for label in labels:
79+
label.add_address(node.getlabeladdress(label.name), True)
80+
label.verify(node)
81+
82+
# Check all labels are returned by listlabels.
83+
assert_equal(
84+
sorted(node.listaccounts().keys()),
85+
[""] + [label.name for label in labels])
86+
87+
# Send a transaction to each address, and make sure this forces
88+
# getlabeladdress to generate new unused addresses.
89+
for label in labels:
90+
node.sendtoaddress(label.label_address, amount_to_send)
91+
label.add_address(node.getlabeladdress(label.name), True)
92+
label.verify(node)
93+
94+
# Mine block to confirm sends, then check the amounts received.
95+
node.generate(1)
96+
for label in labels:
97+
assert_equal(
98+
node.getreceivedbyaddress(label.addresses[0]), amount_to_send)
99+
assert_equal(node.getreceivedbylabel(label.name), amount_to_send)
100+
101+
# Check that setlabel can add a label to a new unused address.
102+
for label in labels:
103+
address = node.getlabeladdress("")
104+
node.setlabel(address, label.name)
105+
label.add_address(address, False)
106+
label.verify(node)
107+
108+
# Check that setlabel can safely overwrite the label of an address with
109+
# a different label.
110+
overwrite_label(node, labels[0], 0, False, labels[1])
111+
112+
# Check that setlabel can safely overwrite the label of an address
113+
# which is the default "label address" of a different label.
114+
overwrite_label(node, labels[0], 0, True, labels[1])
115+
116+
# Check that setlabel can safely overwrite the label of an address
117+
# which already has the same label, effectively performing a no-op.
118+
overwrite_label(node, labels[2], 0, False, labels[2])
119+
120+
121+
if __name__ == '__main__':
122+
WalletLabelsTest().main()

0 commit comments

Comments
 (0)