Skip to content

Commit 189e0ef

Browse files
laanwjjnewbery
authored andcommitted
[wallet] [rpc] introduce 'label' API for wallet
Add label API to wallet RPC. This is one step towards #3816 ("Remove bolt-on account system") although it doesn't actually remove anything yet. These initially mirror the account functions, with the following differences: - These functions aren't DEPRECATED in the help - Help mentions 'label' instead of accounts. In the language used, labels are associated with addresses, instead of addresses associated with labels. (unlike with accounts.) - Labels have no balance - No balances in `listlabels` - `listlabels` has no minconf or watchonly argument - Like in the GUI, labels can be set on any address, not just receiving addreses - Unlike accounts, labels can be deleted. Being unable to delete them is a common annoyance (see #1231). Currently only by reassigning all addresses using `setlabel`, but an explicit call `deletelabel` which assigns all address to the default label may make sense. Thanks to Pierre Rochard for test fixes.
1 parent cf8073f commit 189e0ef

File tree

8 files changed

+209
-36
lines changed

8 files changed

+209
-36
lines changed

src/rpc/client.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
5151
{ "listreceivedbylabel", 0, "minconf" },
5252
{ "listreceivedbylabel", 1, "include_empty" },
5353
{ "listreceivedbylabel", 2, "include_watchonly" },
54+
{ "getlabeladdress", 1, "force" },
5455
{ "getbalance", 1, "minconf" },
5556
{ "getbalance", 2, "include_watchonly" },
5657
{ "getblockhash", 0, "height" },

src/wallet/rpcwallet.cpp

Lines changed: 174 additions & 27 deletions
Large diffs are not rendered by default.

src/wallet/wallet.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3640,6 +3640,12 @@ std::set<CTxDestination> CWallet::GetLabelAddresses(const std::string& label) co
36403640
return result;
36413641
}
36423642

3643+
void CWallet::DeleteLabel(const std::string& label)
3644+
{
3645+
WalletBatch batch(*database);
3646+
batch.EraseAccount(label);
3647+
}
3648+
36433649
bool CReserveKey::GetReservedKey(CPubKey& pubkey, bool internal)
36443650
{
36453651
if (nIndex == -1)

src/wallet/wallet.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ class CWalletKey
549549
};
550550

551551
/**
552-
* Internal transfers.
552+
* DEPRECATED Internal transfers.
553553
* Database key is acentry<account><counter>.
554554
*/
555555
class CAccountingEntry
@@ -989,6 +989,7 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
989989
std::map<CTxDestination, CAmount> GetAddressBalances();
990990

991991
std::set<CTxDestination> GetLabelAddresses(const std::string& label) const;
992+
void DeleteLabel(const std::string& label);
992993

993994
isminetype IsMine(const CTxIn& txin) const;
994995
/**
@@ -1184,7 +1185,7 @@ class CReserveKey final : public CReserveScript
11841185

11851186

11861187
/**
1187-
* Account information.
1188+
* DEPRECATED Account information.
11881189
* Stored in wallet with key "acc"+string account name.
11891190
*/
11901191
class CAccount

src/wallet/walletdb.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ bool WalletBatch::WriteAccount(const std::string& strAccount, const CAccount& ac
161161
return WriteIC(std::make_pair(std::string("acc"), strAccount), account);
162162
}
163163

164+
bool WalletBatch::EraseAccount(const std::string& strAccount)
165+
{
166+
return EraseIC(std::make_pair(std::string("acc"), strAccount));
167+
}
168+
164169
bool WalletBatch::WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry)
165170
{
166171
return WriteIC(std::make_pair(std::string("acentry"), std::make_pair(acentry.strAccount, nAccEntryNum)), acentry);

src/wallet/walletdb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ class WalletBatch
204204
bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry);
205205
bool ReadAccount(const std::string& strAccount, CAccount& account);
206206
bool WriteAccount(const std::string& strAccount, const CAccount& account);
207+
bool EraseAccount(const std::string& strAccount);
207208

208209
/// Write destination data key,value tuple to database
209210
bool WriteDestData(const std::string &address, const std::string &key, const std::string &value);

test/functional/wallet_labels.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- sendfrom (with account arguments)
1313
- move (with account arguments)
1414
"""
15+
from collections import defaultdict
1516

1617
from test_framework.test_framework import BitcoinTestFramework
1718
from test_framework.util import assert_equal
@@ -78,9 +79,12 @@ def run_test(self):
7879
# recognize the label/address associations.
7980
labels = [Label(name) for name in ("a", "b", "c", "d", "e")]
8081
for label in labels:
81-
label.add_receive_address(node.getlabeladdress(label.name))
82+
label.add_receive_address(node.getlabeladdress(label=label.name, force=True))
8283
label.verify(node)
8384

85+
# Check all labels are returned by listlabels.
86+
assert_equal(node.listlabels(), [label.name for label in labels])
87+
8488
# Send a transaction to each label, and make sure this forces
8589
# getlabeladdress to generate a new receiving address.
8690
for label in labels:
@@ -115,7 +119,7 @@ def run_test(self):
115119

116120
# Check that setlabel can assign a label to a new unused address.
117121
for label in labels:
118-
address = node.getlabeladdress("")
122+
address = node.getlabeladdress(label="", force=True)
119123
node.setlabel(address, label.name)
120124
label.add_address(address)
121125
label.verify(node)
@@ -128,6 +132,7 @@ def run_test(self):
128132
addresses.append(node.getnewaddress())
129133
multisig_address = node.addmultisigaddress(5, addresses, label.name)['address']
130134
label.add_address(multisig_address)
135+
label.purpose[multisig_address] = "send"
131136
label.verify(node)
132137
node.sendfrom("", multisig_address, 50)
133138
node.generate(101)
@@ -147,9 +152,7 @@ def run_test(self):
147152
change_label(node, labels[2].addresses[0], labels[2], labels[2])
148153

149154
# Check that setlabel can set the label of an address which is
150-
# already the receiving address of the label. It would probably make
151-
# sense for this to be a no-op, but right now it resets the receiving
152-
# address, causing getlabeladdress to return a brand new address.
155+
# already the receiving address of the label. This is a no-op.
153156
change_label(node, labels[2].receive_address, labels[2], labels[2])
154157

155158
class Label:
@@ -160,6 +163,8 @@ def __init__(self, name):
160163
self.receive_address = None
161164
# List of all addresses assigned with this label
162165
self.addresses = []
166+
# Map of address to address purpose
167+
self.purpose = defaultdict(lambda: "receive")
163168

164169
def add_address(self, address):
165170
assert_equal(address not in self.addresses, True)
@@ -175,8 +180,15 @@ def verify(self, node):
175180
assert_equal(node.getlabeladdress(self.name), self.receive_address)
176181

177182
for address in self.addresses:
183+
assert_equal(
184+
node.getaddressinfo(address)['labels'][0],
185+
{"name": self.name,
186+
"purpose": self.purpose[address]})
178187
assert_equal(node.getaccount(address), self.name)
179188

189+
assert_equal(
190+
node.getaddressesbylabel(self.name),
191+
{address: {"purpose": self.purpose[address]} for address in self.addresses})
180192
assert_equal(
181193
set(node.getaddressesbyaccount(self.name)), set(self.addresses))
182194

@@ -192,7 +204,7 @@ def change_label(node, address, old_label, new_label):
192204
# address of a different label should reset the receiving address of
193205
# the old label, causing getlabeladdress to return a brand new
194206
# address.
195-
if address == old_label.receive_address:
207+
if old_label.name != new_label.name and address == old_label.receive_address:
196208
new_address = node.getlabeladdress(old_label.name)
197209
assert_equal(new_address not in old_label.addresses, True)
198210
assert_equal(new_address not in new_label.addresses, True)

test/functional/wallet_listreceivedby.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def run_test(self):
140140
assert_equal(balance, balance_by_label + Decimal("0.1"))
141141

142142
# Create a new label named "mynewlabel" that has a 0 balance
143-
self.nodes[1].getlabeladdress("mynewlabel")
143+
self.nodes[1].getlabeladdress(label="mynewlabel", force=True)
144144
received_by_label_json = [r for r in self.nodes[1].listreceivedbylabel(0, True) if r["label"] == "mynewlabel"][0]
145145

146146
# Test includeempty of listreceivedbylabel

0 commit comments

Comments
 (0)