Skip to content

Conversation

@achow101
Copy link
Member

While working on coin selection code, it occurred to me that CInputCoin is really a subset of COutput and the conversion of a COutput to a CInputCoin does not appear to be all that useful. So this PR adds fields that are present in CInputCoin to COutput and replaces the usage of CInputCoin with COutput.

COutput is also moved to coinselection.h. As part of this move, the usage of CWalletTx is removed from COutput. It is instead replaced by storing a COutPoint and the CTxOut rather than the entire CWalletTx as coin selection does not really need the full CWalletTx. The CWalletTx was only used for figuring out whether the transaction containing the output was from the current wallet, and for the transaction's time. These are now parameters to COutput's constructor.

Copy link
Member

@Sjors Sjors left a comment

Choose a reason for hiding this comment

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

Concept ACK

Reviewed the first 3 commits up to 95338d3b4bf3be2492cc654029ebfb28bedda42d, got a bit confused after that, as I think some of 864b4ed accidentally landed in 6d54822.

While you're touching this, it would be helpful to document the COutput class (now in coinselection.h) and how it relates to CWalletTx and COutputEntry (which is in receive.h, but used in the listtransactions RPC). For example, I was somewhat confused to see that CWalletTx does not in fact contain a list of COutputs.

95338d3b4bf3be2492cc654029ebfb28bedda42d: do you want to move AvailableCoins, ListCoins, etc from spend.h to coinselection.h too? Those all use COutput. Or is that more involved?

Copy link
Member

@Sjors Sjors Jan 18, 2022

Choose a reason for hiding this comment

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

864b4edf130afe2b77142ca343022b3de6d3fec5: this is not set and not used (in this commit)

Copy link
Member Author

Choose a reason for hiding this comment

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

It is used later, but I didn't want that commit to be too big.

Copy link
Member

Choose a reason for hiding this comment

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

06cf38fce16f327579b008ee8fb78fa851cc8fce: it compiles fine now. But ideally you would move some more code from d840d84f547e911ec88f098d1ece7ed86f100cbc into this commit to set and use them. Not sure if that's practical.

@achow101
Copy link
Member Author

I've split up a few commits and introduced the variables added to COutput in more separate commits.

do you want to move AvailableCoins, ListCoins, etc from spend.h to coinselection.h too? Those all use COutput. Or is that more involved?

No. COutput is only being moved to avoid a circular dependency.

@achow101 achow101 force-pushed the consolidate-coutput-cinputcoin branch from efa72a5 to 2278038 Compare January 19, 2022 16:58
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.

Concept ACK. I need this for some wallet stuff.

@DrahtBot
Copy link
Contributor

DrahtBot commented Jan 22, 2022

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #24644 (wallet: add tracepoints and algorithm information to coin selection by achow101)
  • #24494 (wallet: generate random change target for each tx for better privacy by glozow)
  • #24128 (wallet: BIP 326 sequence based anti-fee-snipe for taproot inputs by MarcoFalke)

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.

@kiminuo
Copy link
Contributor

kiminuo commented Jan 22, 2022

Just read commits up to 1f475858590a50a8a3f37a02df2c2d7b803f9834 (wallet: Remove CWalletTx from COutput's constructor) and so far I have not found any obvious issue. Nice PR.

Copy link
Contributor

@promag promag left a comment

Choose a reason for hiding this comment

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

Light code review ACK 2278038d3f9c2488421cf7ed1131ac77a6f22380.

nit, drop COutput forward declaration in wallet.h

@fanquake fanquake requested a review from instagibbs January 25, 2022 03:00
@instagibbs
Copy link
Member

concept ACK

Copy link
Member

@Sjors Sjors left a comment

Choose a reason for hiding this comment

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

Reviewed everything except d840d84f547e911ec88f098d1ece7ed86f100cbc, which still gives me a bit of a headache, but will look at it later.

Copy link
Member

Choose a reason for hiding this comment

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

06cf38fce16f327579b008ee8fb78fa851cc8fce: it compiles fine now. But ideally you would move some more code from d840d84f547e911ec88f098d1ece7ed86f100cbc into this commit to set and use them. Not sure if that's practical.

@achow101 achow101 force-pushed the consolidate-coutput-cinputcoin branch from 2278038 to 3f5ed18 Compare January 25, 2022 16:34
Copy link
Member

@Sjors Sjors left a comment

Choose a reason for hiding this comment

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

utACK 3f5ed1858eba99253153f6b692625af075107f60

Copy link
Member

@instagibbs instagibbs left a comment

Choose a reason for hiding this comment

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

ACK 3f5ed1858eba99253153f6b692625af075107f60

@murchandamus
Copy link
Contributor

Concept ACK

Copy link
Contributor

@kallewoof kallewoof left a comment

Choose a reason for hiding this comment

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

Concept/utACK 3f5ed1858eba99253153f6b692625af075107f60

@bitcoin bitcoin deleted a comment from taveechaimekwan Feb 8, 2022
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 3f5ed1858eba99253153f6b692625af075107f60. All my suggestions are minor, and you can ignore them. This is a nice simplification and the changes are generally pretty easy to follow. Main feedback would be to drop the struct m_ prefixes (Ugly!)

@Sjors
Copy link
Member

Sjors commented Mar 17, 2022

CI seems a bit unhappy

Update the member variables to match the new style

-BEGIN VERIFY SCRIPT-
sed -i 's/fSpendableIn/spendable/' $(git grep -l "fSpendableIn")
sed -i 's/fSpendable/spendable/' $(git grep -l "fSpendable")
sed -i 's/fSolvableIn/solvable/' $(git grep -l "fSolvableIn")
sed -i 's/fSolvable/solvable/' $(git grep -l "fSolvable")
sed -i 's/fSafeIn/safe/' $(git grep -l "fSafeIn")
sed -i 's/fSafe/safe/' $(git grep -l "fSafe")
sed -i 's/nInputBytes/input_bytes/' $(git grep -l "nInputBytes")
sed -i 's/nDepthIn/depth/' $(git grep -l "nDepthIn" src/wallet src/bench)
sed -i 's/nDepth/depth/' src/wallet/spend.h
sed -i 's/\.nDepth/.depth/' $(git grep -l "\.nDepth" src/wallet/)
sed -i 's/nDepth, FormatMoney/depth, FormatMoney/' src/wallet/spend.cpp
-END VERIFY SCRIPT-
Instead of determining whether the containing transaction is from the
wallet dynamically as needed, just pass it in to COutput and store it.
The transaction ownership isn't going to change.
@achow101 achow101 force-pushed the consolidate-coutput-cinputcoin branch from 98812f1 to 1a3e984 Compare March 17, 2022 15:55
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 1a3e984a09343ac1d804f6106a52c9cf527f7dba

Nice simplification and easy review!

Comment on lines 114 to 67
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 "coinselection: Add effective value and fees to COutput" (03245baac1b8a9bef2cad09942cdd6a618e3b7ef)

Note (just for understanding): These new members come from the existing CInputCoin struct and are added here unchanged so COutput can replace CInputCoin later

Comment on lines +53 to +54
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 "coinselection: Use COutput instead of CInputCoin" (f2cf89eefefec98aa6f5f7a8d3f6f0abc0e0d526)

Note: parameter isn't actually changing here, obsolete comment is just being brought up to date with current code. (At first I thought this change was more significant than it really is)

Copy link
Member

@Sjors Sjors left a comment

Choose a reason for hiding this comment

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

re-utACK 1a3e984a09343ac1d804f6106a52c9cf527f7dba

Copy link
Member

@glozow glozow left a comment

Choose a reason for hiding this comment

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

light code review ACK 1a3e984a09343ac1d804f6106a52c9cf527f7dba, just a few nits

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, thanks for doing, I'm glad to see CInputCoin go away. Got some nits and questions.

std::vector<COutput> coins;
for (const auto& wtx : wtxs) {
coins.emplace_back(wallet, *wtx, 0 /* iIn */, 6 * 24 /* depth */, true /* spendable */, true /* solvable */, true /* safe */);
coins.emplace_back(wallet, *wtx, /*iIn=*/ 0, /*depth=*/ 6 * 24, /*spendable=*/ true, /*solvable=*/ true, /*safe=*/ true, /*use_max_sig_in=*/ false);
Copy link
Contributor

Choose a reason for hiding this comment

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

In 4602295 I missed at first that you added explicit values for use_max_sig_in in a few places while removing the default value. The style amendment first made me think that the default value removal was the only code change. In the future, perhaps consider splitting style edits and functional code changes.

Copy link
Contributor

Choose a reason for hiding this comment

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

Probably for a follow-up, but I think

COutput could have a function that takes a feerate, and sets the COutput.effective_value, or perhaps we could even have COutput take a feerate in the constructor.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll leave that for a followup.

Copy link
Contributor

@w0xlt w0xlt 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 1a3e984

Instead of having a pointer to the CWalletTx in COutput, we can just
store the COutPoint and the CTxOut as those are the only things we need
from the CWalletTx. Other things CWalletTx used to provide were time and
fIsFromMe but these are also being stored by COutput.
@achow101 achow101 force-pushed the consolidate-coutput-cinputcoin branch from 1a3e984 to 6c05fa5 Compare March 23, 2022 18:55
Also rename setPresetCoins to preset_coins
It is no longer needed as everything it was doing is now done by COutput
These operators are used only by the tests in std::mismatch. As
std::mismatch can take a binary predicate, we can use a lambda that
achieves the same instead.
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 049003f, just adding comments and removing == operators since last review

Copy link
Contributor

@w0xlt w0xlt left a comment

Choose a reason for hiding this comment

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

reACK 049003f

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.

reACK 049003f

@fanquake fanquake merged commit 3740cdd into bitcoin:master Mar 24, 2022
fanquake added a commit to bitcoin-core/gui that referenced this pull request Mar 25, 2022
…ake COutput a struct

fab287c Clarify that COutput is a struct, not a class (MarcoFalke)
fa61cdf wallet: Fix coinselection include (MarcoFalke)

Pull request description:

  * Fix include (see commit message)
  * `{}`-init, see bitcoin/bitcoin#24091 (comment)
  * `struct`, see bitcoin/bitcoin#24091 (comment)

ACKs for top commit:
  theStack:
    Code-review ACK fab287c

Tree-SHA512: dd2cfb9c06a92295dbd8fbb6d56afcf00ebda2a0440e301d392cd183d1b9cd87626311d539e302a9e6c6521d69d6183c74a51934e3fc16e64a5dcaba60c7e3ce
@bitcoin bitcoin locked and limited conversation to collaborators Jun 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.