Skip to content

Commit

Permalink
[wallet] Make setlabel idempotent
Browse files Browse the repository at this point in the history
Prevent setlabel calls on an address that already has the same label, and which
is the already the default "label address" of that label, from generating a
new default address.

This way calling setlabel on an address that already has the specified label is
a no-op.

Change suggested by Russell Yanofsky <russ@yanofsky.org> in
bitcoin#7729 (comment)
  • Loading branch information
jnewbery committed Mar 19, 2018
1 parent dd425a0 commit 39f5cf8
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ UniValue setlabel(const JSONRPCRequest& request)
// and if we wouldn't do this, the record would stick around forever.
if (pwallet->mapAddressBook.count(dest)) {
std::string old_label = pwallet->mapAddressBook[dest].name;
if (dest == GetLabelDestination(pwallet, old_label)) {
if (old_label != label && dest == GetLabelDestination(pwallet, old_label)) {
DeleteLabel(*pwallet, old_label);
}
}
Expand Down
6 changes: 2 additions & 4 deletions test/functional/wallet_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,7 @@ def run_test(self):
change_label(node, labels[2].addresses[0], labels[2], labels[2])

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

class Label:
Expand Down Expand Up @@ -210,7 +208,7 @@ def change_label(node, address, old_label, new_label):
# address of a different label should reset the receiving address of
# the old label, causing getlabeladdress to return a brand new
# address.
if address == old_label.receive_address:
if old_label.name != new_label.name and address == old_label.receive_address:
new_address = node.getlabeladdress(old_label.name)
assert_equal(new_address not in old_label.addresses, True)
assert_equal(new_address not in new_label.addresses, True)
Expand Down

0 comments on commit 39f5cf8

Please sign in to comment.