wallet/refactor: refer to CWallet immutably when possible #18241

pull kallewoof wants to merge 12 commits into bitcoin:master from kallewoof:2002-const-fixes changing 12 files +68 −68
  1. kallewoof commented at 10:43 am on March 2, 2020: member

    A lot of places refer to CWallet*’s as CWallet * const, which translates to “an immutable pointer to a mutable CWallet instance”; this is

    1. often not what the author meant, especially as a lot of these places do not at all modify the wallet object, and
    2. confusing, as it tends to suggest that this is a proper way to refer to a constant CWallet instance.

    This PR changes references to wallets to const CWallet* const whenever immutability is expected. This should result in no behavioral changes at all, and improved compile-time error checking.

    Note from irc:

    <sipa> sounds good to me; this is the sort of change that as long as it compiles, the behavior shouldn’t change <sipa> though in general it may lead to introducing automatic copying of objects sometimes (e.g. trying to std::move a const object will work, but generally result in a copy rather than an efficient move) <sipa> CWallet objects aren’t copied or moved though

  2. make BlockUntilSyncedToCurrentChain() const
    The method checks the chain tip for the best block, and calls SyncWithValidationInterfaceQueue() (a standalone function) if necessary.
    dc2d0650fd
  3. wallet: make CanGenerateKeys() const
    This method simply checks if HD is or can be enabled and does not require mutability.
    ddc93557ad
  4. wallet: make KeypoolCountExternalKeys() const
    This method returns the sum of the key pool sizes. It does no modification.
    037fa770eb
  5. wallet: make CanGetAddresses() const
    CWallet::CanGetAddresses() is used to check whether the wallet has available or is able to produce keys for addresses. It uses the ScriptPubKeyMan::CanGetAddresses(), which in turn uses the const KeypoolCountExternalKeys() method, all which do counting and no modifications.
    8cd0b86340
  6. wallet/db: make Backup() const
    This method is the to-disk equivalent of serialize methods which are also const.
    d366795d18
  7. wallet/db: make IsDummy() const
    This method does a simple check and no modifications.
    7b3587b29d
  8. wallet: use constant CWallets in rpcdump.cpp
    * GetWalletAddressesForKey is, as the name implies, immutable; the one change besides the parameter constness is a [] -> .at() change, to a verified-existing key.
    * dumpprivkey and dumpwallet are both similarly immutable, for obvious reasons.
    22d329ad0e
  9. wallet/spkm: make GetOldestKeyPoolTime() const
    The method checks the oldest key time for key pools and returns the oldest. It does no modifications.
    227b9dd2d6
  10. wallet: make getters const df3a818d2a
  11. wallet: make BackupWallet() const 57c569e4d9
  12. wallet: make ReserveDestination pwallet ivar const d9b0ebc1da
  13. wallet: use constant CWallets in rpcwallet.cpp
    * GetAvoidReuseFlag: simply gets the flag, without modifying the wallet
    * ListReceived: helper function to produce lists
    * ListTransactions: produces a list of transactions, without modifications; two cases of map [] -> .at() for verified-existing keys
    * DescribeWalletAddress: generates a description of a given wallet address without changing the wallet
    * The following functions produce a list without making any modifications to the wallet:
      * listaddressgroupings
      * listreceivedbyaddress
      * listreceivedbylabel
      * listtransactions
      * listsinceblock
      * listlockunspent
      * listunspent
      * listlabels
      * getreceivedbyaddress
      * getreceivedbylabel
      * getaddressesbylabel
    * signmessage: uses the wallet to procure a private key for signing, but does no modifications
    * getbalance, getunconfirmedbalance: calculates the wallet balance, without any modifications
    * gettransaction: procures transaction without any modifications
    * backupwallet: makes a backup of the wallet to disk, without changing said wallet
    * getwalletinfo: produces info about wallet without any modifications
    * signrawtransactionwithwallet: modifies incoming transaction on the fly by signing with private key procured from within wallet; no modifications to wallet
    * getaddressinfo: gets information about the given address, with no modifications done to the wallet; one case of [] -> .at() and one ::iterator -> ::const_iterator
    * walletprocesspsbt: processes the given PSBT on the fly, without modifying the wallet
    79facb11e9
  14. fanquake added the label Wallet on Mar 2, 2020
  15. promag commented at 11:01 am on March 2, 2020: member
    Concept ACK.
  16. DrahtBot commented at 11:04 am on March 2, 2020: member

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

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #18278 (interfaces: Describe and follow some code conventions by ryanofsky)
    • #18192 (Bugfix: Wallet: Safely deal with change in the address book by luke-jr)
    • #16378 (The ultimate send RPC by Sjors)
    • #14942 (wallet: Make scan / abort status private to CWallet by Empact)
    • #11413 ([wallet] [rpc] sendtoaddress/sendmany: Add explicit feerate option by kallewoof)

    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.

  17. MarcoFalke commented at 2:44 pm on March 2, 2020: member
    As you are changing this, my preference would be to just use const CWallet& wallet, which has all the same benefits as const CWallet* const, but also can’t be nullptr.
  18. promag commented at 3:09 pm on March 2, 2020: member
    @MarcoFalke that would be a larger change. I don’t mind this PR as it is and do that in other PR.
  19. kallewoof commented at 3:11 pm on March 2, 2020: member
    I love the idea, and started working on it, but as @promag points out, the diff becomes significantly larger. I can make a competing PR if people would like to compare the two, otherwise I think a follow-up PR to go from const CWallet* to const CWallet& might be worthwhile.
  20. promag commented at 3:13 pm on March 2, 2020: member
    Unless some script-fu master sorts some scripted diff.
  21. practicalswift commented at 8:56 pm on March 2, 2020: contributor

    Concept ACK

    const CWallet& would be even better :)

  22. kallewoof force-pushed on Mar 3, 2020
  23. kallewoof commented at 3:49 am on March 3, 2020: member
    I made a new branch and switched to const CWallet&. It builds on top of this one. I can switch to it if people find it worthwhile, but it pushes the diff from 68 to ~330 lines: https://github.com/kallewoof/bitcoin/compare/2002-const-fixes...kallewoof:2002-const-fixes-depointered (diff vs this PR)
  24. Empact commented at 1:21 pm on March 3, 2020: member

    ACK https://github.com/bitcoin/bitcoin/pull/18241/commits/79facb11e92f8b61063f301027dee7c7344eb1be

    Could squash into one or a few commits. I also like moving to references but I don’t mind them arriving over time.

  25. kallewoof commented at 2:17 pm on March 3, 2020: member
    @Empact I tried to justify/analyze each change in each commit, so would prefer not to squash but if people find it hard to review, I can do so.
  26. promag commented at 2:24 pm on March 3, 2020: member
    d9b0ebc1da8758645f6de24a4a557511ef9b5e36 has a typo in commit message.
  27. kallewoof commented at 1:39 am on March 4, 2020: member
    @promag What typo? “ivar” is a common way to refer to instance variables where I’m from.
  28. promag commented at 7:47 am on March 4, 2020: member

    @kallewoof TIL!

    ACK 79facb11e92f8b61063f301027dee7c7344eb1be.

  29. fjahr commented at 10:23 pm on March 4, 2020: member
    ACK 79facb11e92f8b61063f301027dee7c7344eb1be
  30. laanwj commented at 10:39 pm on March 6, 2020: member
    ACK 79facb11e92f8b61063f301027dee7c7344eb1be
  31. fanquake merged this on Mar 6, 2020
  32. fanquake closed this on Mar 6, 2020

  33. kallewoof deleted the branch on Mar 7, 2020
  34. sidhujag referenced this in commit 3098cc8782 on Mar 7, 2020
  35. deadalnix referenced this in commit fceefb59cf on Oct 25, 2020
  36. sidhujag referenced this in commit 7001f43072 on Nov 10, 2020
  37. DrahtBot locked this on Feb 15, 2022

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-11-17 12:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me