Skip to content

Conversation

kallewoof
Copy link
Contributor

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

kallewoof added 12 commits March 2, 2020 17:26
The method checks the chain tip for the best block, and calls SyncWithValidationInterfaceQueue() (a standalone function) if necessary.
This method simply checks if HD is or can be enabled and does not require mutability.
This method returns the sum of the key pool sizes. It does no modification.
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.
This method is the to-disk equivalent of serialize methods which are also const.
This method does a simple check and no modifications.
* 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.
The method checks the oldest key time for key pools and returns the oldest. It does no modifications.
* 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
@fanquake fanquake added the Wallet label Mar 2, 2020
@promag
Copy link
Contributor

promag commented Mar 2, 2020

Concept ACK.

@DrahtBot
Copy link
Contributor

DrahtBot commented Mar 2, 2020

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

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.

@maflcko
Copy link
Member

maflcko commented Mar 2, 2020

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.

@promag
Copy link
Contributor

promag commented Mar 2, 2020

@MarcoFalke that would be a larger change. I don't mind this PR as it is and do that in other PR.

@kallewoof
Copy link
Contributor Author

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.

@promag
Copy link
Contributor

promag commented Mar 2, 2020

Unless some script-fu master sorts some scripted diff.

@practicalswift
Copy link
Contributor

Concept ACK

const CWallet& would be even better :)

@kallewoof
Copy link
Contributor Author

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: kallewoof/bitcoin@2002-const-fixes...kallewoof:2002-const-fixes-depointered (diff vs this PR)

@Empact
Copy link
Contributor

Empact commented Mar 3, 2020

ACK 79facb1

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

@kallewoof
Copy link
Contributor Author

@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.

@promag
Copy link
Contributor

promag commented Mar 3, 2020

d9b0ebc has a typo in commit message.

@kallewoof
Copy link
Contributor Author

@promag What typo? "ivar" is a common way to refer to instance variables where I'm from.

@promag
Copy link
Contributor

promag commented Mar 4, 2020

@kallewoof TIL!

ACK 79facb1.

@fjahr
Copy link
Contributor

fjahr commented Mar 4, 2020

ACK 79facb1

@laanwj
Copy link
Member

laanwj commented Mar 6, 2020

ACK 79facb1

@fanquake fanquake merged commit 4d80274 into bitcoin:master Mar 6, 2020
@kallewoof kallewoof deleted the 2002-const-fixes branch March 7, 2020 02:09
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Mar 7, 2020
… possible

79facb1 wallet: use constant CWallets in rpcwallet.cpp (Karl-Johan Alm)
d9b0ebc wallet: make ReserveDestination pwallet ivar const (Karl-Johan Alm)
57c569e wallet: make BackupWallet() const (Karl-Johan Alm)
df3a818 wallet: make getters const (Karl-Johan Alm)
227b9dd wallet/spkm: make GetOldestKeyPoolTime() const (Karl-Johan Alm)
22d329a wallet: use constant CWallets in rpcdump.cpp (Karl-Johan Alm)
7b3587b wallet/db: make IsDummy() const (Karl-Johan Alm)
d366795 wallet/db: make Backup() const (Karl-Johan Alm)
8cd0b86 wallet: make CanGetAddresses() const (Karl-Johan Alm)
037fa77 wallet: make KeypoolCountExternalKeys() const (Karl-Johan Alm)
ddc9355 wallet: make CanGenerateKeys() const (Karl-Johan Alm)
dc2d065 make BlockUntilSyncedToCurrentChain() const (Karl-Johan Alm)

Pull request description:

  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:

  > &lt;sipa&gt; sounds good to me; this is the sort of change that as long as it compiles, the behavior shouldn't change
  > &lt;sipa&gt; 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)
  > &lt;sipa&gt; CWallet objects aren't copied or moved though

ACKs for top commit:
  laanwj:
    ACK 79facb1
  Empact:
    ACK bitcoin@79facb1
  promag:
    ACK 79facb1.
  fjahr:
    ACK 79facb1

Tree-SHA512: 80a80c1a52f0f788d0ccb268b53bc0f46c796643a3c5a22b55bbbde4ffa6c7e347784e5e53b1e488a3b4e14399e31d5be9417ad5b6319c74a462609e9b1a98e8
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Oct 25, 2020
Summary:
 * make BlockUntilSyncedToCurrentChain() const

The method checks the chain tip for the best block, and calls SyncWithValidationInterfaceQueue() (a standalone function) if necessary.

 * wallet: make CanGenerateKeys() const

This method simply checks if HD is or can be enabled and does not require mutability.

 * wallet: make KeypoolCountExternalKeys() const

This method returns the sum of the key pool sizes. It does no modification.

 * 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.

 * wallet/db: make IsDummy() const

This method does a simple check and no modifications.

 * wallet/db: make Backup() const

This method is the to-disk equivalent of serialize methods which are also const.

 * 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.

 * wallet/spkm: make GetOldestKeyPoolTime() const

The method checks the oldest key time for key pools and returns the oldest. It does no modifications.

 * wallet: make getters const

 * wallet: make BackupWallet() const

 * wallet: make ReserveDestination pwallet ivar const

 * 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

This is a backport of Core [[bitcoin/bitcoin#18241 | PR18241]]

Test Plan:
  ninja all check-all

Reviewers: #bitcoin_abc, PiRK

Reviewed By: PiRK

Differential Revision: https://reviews.bitcoinabc.org/D8115
sidhujag pushed a commit to syscoin-core/syscoin that referenced this pull request Nov 10, 2020
… possible

79facb1 wallet: use constant CWallets in rpcwallet.cpp (Karl-Johan Alm)
d9b0ebc wallet: make ReserveDestination pwallet ivar const (Karl-Johan Alm)
57c569e wallet: make BackupWallet() const (Karl-Johan Alm)
df3a818 wallet: make getters const (Karl-Johan Alm)
227b9dd wallet/spkm: make GetOldestKeyPoolTime() const (Karl-Johan Alm)
22d329a wallet: use constant CWallets in rpcdump.cpp (Karl-Johan Alm)
7b3587b wallet/db: make IsDummy() const (Karl-Johan Alm)
d366795 wallet/db: make Backup() const (Karl-Johan Alm)
8cd0b86 wallet: make CanGetAddresses() const (Karl-Johan Alm)
037fa77 wallet: make KeypoolCountExternalKeys() const (Karl-Johan Alm)
ddc9355 wallet: make CanGenerateKeys() const (Karl-Johan Alm)
dc2d065 make BlockUntilSyncedToCurrentChain() const (Karl-Johan Alm)

Pull request description:

  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:

  > &lt;sipa&gt; sounds good to me; this is the sort of change that as long as it compiles, the behavior shouldn't change
  > &lt;sipa&gt; 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)
  > &lt;sipa&gt; CWallet objects aren't copied or moved though

ACKs for top commit:
  laanwj:
    ACK 79facb1
  Empact:
    ACK bitcoin@79facb1
  promag:
    ACK 79facb1.
  fjahr:
    ACK 79facb1

Tree-SHA512: 80a80c1a52f0f788d0ccb268b53bc0f46c796643a3c5a22b55bbbde4ffa6c7e347784e5e53b1e488a3b4e14399e31d5be9417ad5b6319c74a462609e9b1a98e8
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Feb 15, 2022
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.

9 participants