Skip to content

Conversation

achow101
Copy link
Member

@achow101 achow101 commented Mar 6, 2023

Instead of storing and passing around fixed strings for the purpose of an address, use an enum.

@DrahtBot
Copy link
Contributor

DrahtBot commented Mar 6, 2023

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK josibake
Stale ACK aureleoules, ryanofsky, Xekyo

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #bitcoin-core/gui/723 (Add warnings for non-active addresses in receive tab and address book by pinheadmz)
  • #bitcoin-core/gui/650 (Add Import to Wallet GUI by KolbyML)
  • #27286 (wallet: Keep track of the wallet's own transaction outputs in memory by achow101)
  • #27224 (refactor: Remove CAddressBookData::destdata by ryanofsky)
  • #26840 (refactor: importpubkey, importprivkey, importaddress, importmulti, and importdescriptors rpc by KolbyML)
  • #26836 (wallet: finish addressbook encapsulation by furszy)
  • #25620 (wallet: Introduce AddressBookManager by furszy)
  • #25297 (wallet: group independent db writes on single batched db transaction by furszy)
  • #24914 (wallet: Load database records in a particular order by achow101)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@DrahtBot DrahtBot added the Wallet label Mar 6, 2023
Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nits

Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice change, and I started reviewing 44a2ae1, but it looks like there are a lot of functions taking optional<AddressPurpose> values that should more properly take AddressPurpose values for simplicity and better type checking. (Basically NotifyAddressBookChanged and all the functions it calls). I think this might stem from not having separate SetAddressBook and UpdateAddressBook functions, so I want to look into this before reviewing more of the PR. (Having separate functions could help because Set function could make purpose a required parameter, while the Update function could let it be it optional).

@ryanofsky
Copy link
Contributor

I made some changes to this in a branch that could be adopted https://github.com/ryanofsky/bitcoin/commits/review.27217.1-edit

  • Tries to split commits and add a lot of comments so this is easier to review
  • Make enum a public wallet type instead of putting it in interfaces/ so wallet/ code does not depend on interfaces/wallet code and interfaces::Wallet::AddressPurpose becomes just wallet::AddressPurpose
  • Replaced std::optional<AddressPurpose> with plain AddressPurpose in many places where purpose is known or can be derived from IsMine, to simplify code and avoid bugs.
  • Dropped AddressPurpose::UNKNOWN to avoid defining an enum value that doesn't have any corresponding value in the wallet database.
  • Added AddressPurpse::REFUND enum value to be able to load old wallets that may have refund addresses without errors or warnings.
  • Changed assert(0) failure when loading wallets with unrecognized purpose strings with a warning.

@achow101 achow101 force-pushed the address-purpose-enums branch from 44a2ae1 to db521d3 Compare March 15, 2023 17:10
@achow101
Copy link
Member Author

I made some changes to this in a branch that could be adopted https://github.com/ryanofsky/bitcoin/commits/review.27217.1-edit

I've adopted these changes.

Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review ACK db521d3. Thanks for adopting suggested changes. I left some new suggestions, but they are not important and can be ignored.

I think this PR should be straightforward for others to review now, and nice because it should demystify the purpose field and wallet address book as a whole.


WalletAddress(CTxDestination dest, wallet::isminetype is_mine, std::string name, std::string purpose)
: dest(std::move(dest)), is_mine(is_mine), name(std::move(name)), purpose(std::move(purpose))
WalletAddress(CTxDestination dest, wallet::isminetype is_mine, wallet::AddressPurpose purpose, std::string name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In commit "wallet: Replace use of purpose strings with an enum" (db521d3)

Note for reviewers: in a few places in this PR, struct order or argument order is changed so the AddressPurpose field follows IsMine or IsChange fields. This is done because the fields hold overlapping information, and in most cases new code should rely on the other fields instead of AddressPurpose.

@@ -677,7 +677,7 @@ RPCHelpMan getaddressesbylabel()
// std::set in O(log(N))), UniValue::__pushKV is used instead,
// which currently is O(1).
UniValue value(UniValue::VOBJ);
value.pushKV("purpose", _purpose);
value.pushKV("purpose", _purpose ? PurposeToString(*_purpose) : "unknown");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In commit "wallet: Replace use of purpose strings with an enum" (db521d3)

Note for reviewers: this is preserving previous behavior. Default value for CAddressBook::purpose field previously was "unknown" when a purpose was not recorded, and now it is null which is translated to "unknown"

@achow101 achow101 force-pushed the address-purpose-enums branch 2 times, most recently from a797f99 to 0d18142 Compare March 17, 2023 21:17
Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review ACK 0d18142. Suggested a few comment/whitespace tweaks, but this is also fine as-is.

@achow101 achow101 force-pushed the address-purpose-enums branch from 0d18142 to a48010f Compare March 20, 2023 16:57
Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review ACK a48010f. Just comment and whitespace changes since last review

Copy link
Contributor

@aureleoules aureleoules left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK a48010f, looks good to me.

@achow101 achow101 force-pushed the address-purpose-enums branch from a48010f to 98821a7 Compare April 10, 2023 14:40
Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review ACK 98821a7. Just suggested changes since last review, the main one being changing translateTransactionType to use a switch statement.

This had 3 reviews before, so it would be nice to have reacks and hopefully merge this soon.

Copy link
Contributor

@murchandamus murchandamus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 98821a7

@achow101 achow101 force-pushed the address-purpose-enums branch from 98821a7 to 635c50a Compare April 11, 2023 00:44
@josibake
Copy link
Member

ACK 635c50a

@DrahtBot DrahtBot requested review from ryanofsky and murchandamus and removed request for josibake April 11, 2023 09:14
@murchandamus
Copy link
Contributor

reACK 635c50a

but appears to need rebase

@DrahtBot DrahtBot removed the request for review from murchandamus April 11, 2023 19:24
ryanofsky and others added 4 commits April 11, 2023 15:52
Move isminetype and isminefilter there this commit, add WalletPurpose type next
commit.
Instead of storing and passing around fixed strings for the purpose of
an address, use an enum.

This also rationalizes the CAddressBookData struct, documenting all fields and
making them public, and simplifying the representation to avoid bugs like
bitcoin#26761 (comment) and make
it not possible to invalid address data like change addresses with labels.

Co-authored-by: Ryan Ofsky <ryan@ofsky.org>
@achow101 achow101 force-pushed the address-purpose-enums branch from 635c50a to 18fc71a Compare April 11, 2023 20:01
@josibake
Copy link
Member

reACK 18fc71a

@DrahtBot DrahtBot requested a review from murchandamus April 12, 2023 08:45
@fanquake fanquake merged commit cae0608 into bitcoin:master Apr 12, 2023
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Apr 12, 2023
bool invoked = QMetaObject::invokeMethod(walletmodel, "updateAddressBook",
Q_ARG(QString, strAddress),
Q_ARG(QString, strLabel),
Q_ARG(bool, isMine),
Q_ARG(QString, strPurpose),
Q_ARG(wallet::AddressPurpose, purpose),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change appears to trigger this assert when executing "getnewaddress" from the Console:
Assertion failed: (invoked), function NotifyAddressBookChanged, file walletmodel.cpp, line 392.

debug.log shows this error message:
GUI: QMetaMethod::invoke: Unable to handle unregistered datatype 'wallet::AddressPurpose'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, opened an issue here to follow up: bitcoin-core/gui#725.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hebasto added a commit to bitcoin-core/gui that referenced this pull request Apr 13, 2023
a45b544 qt: Register `wallet::AddressPurpose` type (Hennadii Stepanov)

Pull request description:

  This PR is a follow up of bitcoin/bitcoin#27217.

  Fixes #725.

ACKs for top commit:
  achow101:
    ACK a45b544
  furszy:
    Tested ACK a45b544

Tree-SHA512: c670f4bf56442613d3fe038b0ba21acfcd4c69aa5340072e9a77d83f5fab1bf2facd87a9e1f42d88f496d277b27b79e7090444d59a9b9e71f3b486e171daa669
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Apr 13, 2023
a45b544 qt: Register `wallet::AddressPurpose` type (Hennadii Stepanov)

Pull request description:

  This PR is a follow up of bitcoin#27217.

  Fixes bitcoin#725.

ACKs for top commit:
  achow101:
    ACK a45b544
  furszy:
    Tested ACK a45b544

Tree-SHA512: c670f4bf56442613d3fe038b0ba21acfcd4c69aa5340072e9a77d83f5fab1bf2facd87a9e1f42d88f496d277b27b79e7090444d59a9b9e71f3b486e171daa669
@bitcoin bitcoin locked and limited conversation to collaborators Apr 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants