Skip to content

Commit

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

Make calling setlabel on an address that already has the label a no-op.

Bug and fix from Russell Yanofsky <russ@yanofsky.org> in
bitcoin#7729 (comment)
  • Loading branch information
jnewbery committed Nov 30, 2017
1 parent 5ee1c2e commit b6753cc
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 @@ -299,7 +299,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 == GetLabelAddress(pwallet, old_label)) {
if (old_label != label && dest == GetLabelAddress(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 b6753cc

Please sign in to comment.