wallet, refactor: return out-params of CreateTransaction() as optional struct#20640
pull
theStack
wants to merge
2
commits into
bitcoin:master
from
theStack:202012-refactor-wallet-createtransaction-return_out_params_in_optstruct
changing
7
files
+83−75
theStack
commented at 4:03 am on December 13, 2020:
member
The method CWallet::CreateTransaction currently returns several values in the form of out-parameters:
the actual newly created transaction (CTransactionRef& tx)
its required fee (CAmount& nFeeRate)
the position of the change output (int& nChangePosInOut) – as the name suggests, this is both an in- and out-param
By returning these values in an optional structure (which returns no value a.k.a. std::nullopt if an error occured), the interfaces is shorter, cleaner (requested change position is now in-param and can be passed by value) and callers don’t have to create dummy variables for results that they are not interested in.
Note that the names of the replaced out-variables were kept in CreateTransactionInternal to keep the diff minimal. Also, the fee calculation data (FeeCalculation& fee_calc_out) would be another candidate to put into the structure, but FeeCalculation is currently an opaque data type in the wallet interface and I think it should stay that way.
As a potential follow-up, I think it would make sense to also do the same refactoring for CWallet::FundTransaction, which has a very similar parameter structure.
#24649 (wallet: do not count wallet utxos as external by S3RK)
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.
in
src/wallet/feebumper.cpp:220
in
8e9266fb7eoutdated
219- CAmount fee_ret;
220- int change_pos_in_out = -1; // No requested location for change
221 bilingual_str fail_reason;
222 FeeCalculation fee_calc_out;
223- if (!wallet.CreateTransaction(recipients, tx_new, fee_ret, change_pos_in_out, fail_reason, new_coin_control, fee_calc_out, false)) {
224+ auto txr = wallet.CreateTransaction(recipients, /* No requested location for change */ -1, fail_reason, new_coin_control, fee_calc_out, false);
While it’s arguably not, strictly speaking, part of the proposed change, the comment about the change position parameter that’s being maintained here is a bit misleading. This is a bit nit-picky, so bare with me please, but I think it’s more appropriate to:
Use a const instead of inline comment in order to convey the meaning. e.g. const int RANDOM_CHANGE_POSITION = -1). This value is used more than once, so another reason to use a const.
Convey that passing -1 means “actively set the position to a random one” rather than “no requested location” which doesn’t really let the reader know what’s going to happen.
theStack
commented at 7:26 pm on December 27, 2020:
Thanks a lot for reviewing, good point! I probably was a bit over-enthusiastic in getting rid of as many variables as possible before the call to CreateTransaction ;-) Introducing a RANDOM_CHANGE_POSITION constant makes a lot of sense to me, see latest force-push of the second commit. Not sure yet if putting the constant into the header is a good idea, as it is only used for one single method and hence many modules that don’t need the constant would see it as well. Probably a candidate for a follow-up PR?
in
src/wallet/wallet.cpp:3132
in
8e9266fb7eoutdated
pox
commented at 5:36 am on December 19, 2020:
contributor
ACK not tested.
theStack force-pushed
on Dec 27, 2020
theStack
commented at 7:29 pm on December 27, 2020:
member
Force-pushed with the proposed changes by @pox (see #20640 (review)), introducing new constants RANDOM_CHANGE_POSITION to be used as second parameter for CWallet::CreateTransaction.
in
src/wallet/feebumper.cpp:221
in
5865c38c6aoutdated
220- int change_pos_in_out = -1; // No requested location for change
221+ constexpr int RANDOM_CHANGE_POSITION = -1;
222 bilingual_str fail_reason;
223 FeeCalculation fee_calc_out;
224- if (!wallet.CreateTransaction(recipients, tx_new, fee_ret, change_pos_in_out, fail_reason, new_coin_control, fee_calc_out, false)) {
225+ auto txr = wallet.CreateTransaction(recipients, RANDOM_CHANGE_POSITION, fail_reason, new_coin_control, fee_calc_out, false);
practicalswift
commented at 10:50 pm on December 27, 2020:
Nit: Using auto here obscures the fact that txr is an optional. I think being explicit about the optionality is better in order to avoid mistakes in the future.
Applies throughout this PR.
theStack
commented at 8:47 pm on December 30, 2020:
Thanks for reviewing! Agree that being explicit is better here. Done.
stackman27 approved
stackman27
commented at 4:01 am on December 28, 2020:
contributor
ACK Code review and built successful. Also, definitely a good idea to put FeeCalculation& fee_calc_out into this structure
pox
commented at 5:02 am on December 28, 2020:
contributor
reACK5865c38
🙏
theStack force-pushed
on Dec 30, 2020
theStack
commented at 8:52 pm on December 30, 2020:
member
Force-pushed with changes as suggested by practicalswift, using explicit datatype std::optional<CreatedTransactionalResult> rather instead of auto for storing return values of CreateTransaction{Internal}(). Should be straight-forward to re-review, see range-diff.
DrahtBot added the label
Needs rebase
on Feb 19, 2021
theStack force-pushed
on Mar 1, 2021
theStack
commented at 0:11 am on March 1, 2021:
member
Rebased on master.
DrahtBot removed the label
Needs rebase
on Mar 1, 2021
DrahtBot added the label
Needs rebase
on Mar 10, 2021
theStack force-pushed
on Mar 31, 2021
theStack
commented at 6:38 pm on March 31, 2021:
member
Rebased on master and changed code to return uninitialized values explicitely via return std::nullopt rather than return {} (see #21498, #21415 (comment) for further explanation why this is preferred).
DrahtBot removed the label
Needs rebase
on Mar 31, 2021
DrahtBot added the label
Needs rebase
on May 25, 2021
theStack force-pushed
on May 29, 2021
theStack
commented at 12:17 pm on May 29, 2021:
member
Rebased on master.
DrahtBot removed the label
Needs rebase
on May 29, 2021
DrahtBot added the label
Needs rebase
on May 30, 2021
theStack force-pushed
on Jun 9, 2021
theStack
commented at 3:44 pm on June 9, 2021:
member
Rebased on master again. (This time it was a bit more complex, as the involved methods have moved from wallet/wallet.cpp to wallet/spend.cpp).
DrahtBot removed the label
Needs rebase
on Jun 9, 2021
DrahtBot added the label
Needs rebase
on Sep 3, 2021
theStack force-pushed
on Sep 10, 2021
theStack force-pushed
on Sep 10, 2021
DrahtBot removed the label
Needs rebase
on Sep 10, 2021
theStack
commented at 8:31 pm on September 10, 2021:
member
Rebased on master.
DrahtBot added the label
Needs rebase
on Oct 4, 2021
theStack force-pushed
on Oct 29, 2021
theStack
commented at 0:08 am on October 29, 2021:
member
Rebased on master again.
DrahtBot removed the label
Needs rebase
on Oct 29, 2021
DrahtBot added the label
Needs rebase
on Dec 8, 2021
fanquake
commented at 10:15 am on March 22, 2022:
member
achow101
commented at 2:25 pm on March 22, 2022:
member
Concept ACK
theStack force-pushed
on Mar 22, 2022
theStack
commented at 3:15 pm on March 22, 2022:
member
Rebased on master.
DrahtBot removed the label
Needs rebase
on Mar 22, 2022
DrahtBot added the label
Needs rebase
on Mar 23, 2022
theStack force-pushed
on Mar 31, 2022
theStack
commented at 11:14 pm on March 31, 2022:
member
Rebased on master again.
DrahtBot removed the label
Needs rebase
on Apr 1, 2022
laanwj
commented at 5:51 pm on April 21, 2022:
member
Concept ACK. Returning things is much cleaner for out-only arguments than using mutable arguments (which would ideally be reserved for inout-arguments only).
furszy
commented at 11:15 pm on April 21, 2022:
member
First look, concept ACK.
Wasn’t aware of this PR and touched the same piece of code in #24845.
-> PR where I introduced two generic versions of the optional return object called OperationResult and CallResult<T> (basically classes that encapsulate the function result: the result object, or in case of failure, the failure reason), which later can be used everywhere in the sources to cleanup similar boilerplate code where we pass a lot of ref args with a single return optional struct like the one introduced here.
So, combining both PRs, we will just return a CallResult<CreatedTransactionResult> which will cleanup the code further.
Will do a deeper code review soon.
in
src/wallet/spend.h:102
in
5238022161outdated
96@@ -95,9 +97,9 @@ struct CreatedTransactionResult
97 /**
98 * Create a new transaction paying the recipients with a set of coins
99 * selected by SelectCoins(); Also create the change output, when needed
100- * @note passing nChangePosInOut as -1 will result in setting a random position
101+ * @note passing nChangePos as -1 will result in setting a random position
102 */
103-bool CreateTransaction(CWallet& wallet, const std::vector<CRecipient>& vecSend, CTransactionRef& tx, CAmount& nFeeRet, int& nChangePosInOut, bilingual_str& error, const CCoinControl& coin_control, FeeCalculation& fee_calc_out, bool sign = true);
104+std::optional<CreatedTransactionResult> CreateTransaction(CWallet& wallet, const std::vector<CRecipient>& vecSend, int nChangePos, bilingual_str& error, const CCoinControl& coin_control, FeeCalculation& fee_calc_out, bool sign = true);
Optional: I stumble a bit on past tense in the Class name here. I think I’d prefer either CreateTransactionResult to emphasize it’s the result of a function called CreateTransaction…, or maybe just either CreatedTransaction or TransactionResult, since having both “result” and “created” feels a bit redundant.
theStack force-pushed
on May 16, 2022
theStack
commented at 3:57 pm on May 16, 2022:
member
Rebased on master. Thanks to @furszy for getting the ball rolling on this PR again by pinging me up on IRC (I was planning to wait for #25003, but it seems that this will not be merged soon, as it was suggested to remove the affected tracepoint completely).
DrahtBot removed the label
Needs rebase
on May 16, 2022
Xekyo
commented at 5:45 pm on May 16, 2022:
member
ACK4c5ceb040cf50d24201903a9200fb23be88d96fb
achow101
commented at 8:00 pm on May 16, 2022:
member
re-ACK4c5ceb040cf50d24201903a9200fb23be88d96fb
w0xlt approved
w0xlt
commented at 1:43 am on May 17, 2022:
contributor
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-12-18 12:12 UTC
This site is hosted by @0xB10C More mirrored repositories can be found on mirror.b10c.me