Skip to content

Commit 9b3370d

Browse files
committed
Merge #12892: [wallet] [rpc] introduce 'label' API for wallet
41ba061 [docs] Add release notes for wallet 'label' API. (John Newbery) 189e0ef [wallet] [rpc] introduce 'label' API for wallet (Wladimir J. van der Laan) Pull request description: 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. Tree-SHA512: 45cc313c68ad529ce3a15c02181d2ab0083a7e14fe824e2cde34972713fecce512e3d4b9aa46db5355f2baa857c44b234d4fe9709225bc23c7ebbc0e03febbf5
2 parents 0a8054e + 41ba061 commit 9b3370d

File tree

10 files changed

+241
-46
lines changed

10 files changed

+241
-46
lines changed

doc/release-notes-pr12892.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'label' API for wallet
2+
----------------------
3+
4+
A new 'label' API has been introduced for the wallet. This is intended as a
5+
replacement for the deprecated 'account' API.
6+
7+
The label RPC methods mirror the account functionality, with the following functional differences:
8+
9+
- Labels can be set on any address, not just receiving addresses. This functionality was previously only available through the GUI.
10+
- Labels can be deleted by reassigning all addresses using the `setlabel` RPC method.
11+
- There isn't support for sending transactions _from_ a label, or for determining which label a transaction was sent from.
12+
- Labels do not have a balance.
13+
14+
Here are the changes to RPC methods:
15+
16+
| Deprecated Method | New Method | Notes |
17+
| :---------------------- | :-------------------- | :-----------|
18+
| `getaccount` | `getaddressinfo` | `getaddressinfo` returns a json object with address information instead of just the name of the account as a string. |
19+
| `getaccountaddress` | `getlabeladdress` | `getlabeladdress` throws an error by default if the label does not already exist, but provides a `force` option for compatibility with existing applications. |
20+
| `getaddressesbyaccount` | `getaddressesbylabel` | `getaddressesbylabel` returns a json object with the addresses as keys, instead of a list of strings. |
21+
| `getreceivedbyaccount` | `getreceivedbylabel` | _no change in behavior_ |
22+
| `listaccounts` | `listlabels` | `listlabels` does not return a balance or accept `minconf` and `watchonly` arguments. |
23+
| `listreceivedbyaccount` | `listreceivedbylabel` | Both methods return new `label` fields, along with `account` fields for backward compatibility. |
24+
| `move` | n/a | _no replacement_ |
25+
| `sendfrom` | n/a | _no replacement_ |
26+
| `setaccount` | `setlabel` | Both methods now: <ul><li>allow assigning labels to any address, instead of raising an error if the address is not receiving address.<li>delete the previous label associated with an address when the final address using that label is reassigned to a different label, instead of making an implicit `getaccountaddress` call to ensure the previous label still has a receiving address. |
27+
28+
| Changed Method | Notes |
29+
| :--------------------- | :------ |
30+
| `addmultisigaddress` | Renamed `account` named parameter to `label`. Still accepts `account` for backward compatibility. |
31+
| `getnewaddress` | Renamed `account` named parameter to `label`. Still accepts `account` for backward compatibility. |
32+
| `listunspent` | Returns new `label` fields, along with `account` fields for backward compatibility. |

doc/release-notes.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,6 @@ RPC changes
6363

6464
- The `createrawtransaction` RPC will now accept an array or dictionary (kept for compatibility) for the `outputs` parameter. This means the order of transaction outputs can be specified by the client.
6565
- The `fundrawtransaction` RPC will reject the previously deprecated `reserveChangeKey` option.
66-
- Wallet `getnewaddress` and `addmultisigaddress` RPC `account` named
67-
parameters have been renamed to `label` with no change in behavior.
68-
- Wallet `getlabeladdress`, `getreceivedbylabel`, `listreceivedbylabel`, and
69-
`setlabel` RPCs have been added to replace `getaccountaddress`,
70-
`getreceivedbyaccount`, `listreceivedbyaccount`, and `setaccount` RPCs,
71-
which are now deprecated. There is no change in behavior between the
72-
new RPCs and deprecated RPCs.
73-
- Wallet `listreceivedbylabel`, `listreceivedbyaccount` and `listunspent` RPCs
74-
add `label` fields to returned JSON objects that previously only had
75-
`account` fields.
7666
- `sendmany` now shuffles outputs to improve privacy, so any previously expected behavior with regards to output ordering can no longer be relied upon.
7767
- The new RPC `testmempoolaccept` can be used to test acceptance of a transaction to the mempool without adding it.
7868

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)