Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wallet, refactor: return out-params of CreateTransaction() as optional struct #20640

Conversation

theStack
Copy link
Contributor

@theStack theStack commented Dec 13, 2020

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.

Suggested by laanwj in #20588 (comment).

@theStack theStack force-pushed the 202012-refactor-wallet-createtransaction-return_out_params_in_optstruct branch from 6dc88c6 to 8e9266f Compare December 13, 2020 04:33
@DrahtBot
Copy link
Contributor

DrahtBot commented Dec 13, 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:

  • #25003 (tracing: fix coin_selection:aps_create_tx_internal calling logic by theStack)
  • #24897 ([Draft / POC] Silent Payments by w0xlt)
  • #24845 (wallet: createTransaction, return proper error description for "too-long-mempool-chain" + introduce generic Result classes by furszy)
  • #24752 (wallet: increase BnB upper limit by S3RK)
  • #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.

Copy link
Contributor

@pox pox left a comment

Choose a reason for hiding this comment

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

ACK not tested.

src/wallet/feebumper.cpp Outdated Show resolved Hide resolved
src/wallet/wallet.cpp Outdated Show resolved Hide resolved
@theStack theStack force-pushed the 202012-refactor-wallet-createtransaction-return_out_params_in_optstruct branch from 8e9266f to 5865c38 Compare December 27, 2020 19:17
@theStack
Copy link
Contributor Author

Force-pushed with the proposed changes by @pox (see #20640 (comment)), introducing new constants RANDOM_CHANGE_POSITION to be used as second parameter for CWallet::CreateTransaction.

Copy link
Contributor

@stackman27 stackman27 left a comment

Choose a reason for hiding this comment

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

ACK Code review and built successful. Also, definitely a good idea to put FeeCalculation& fee_calc_out into this structure

@pox
Copy link
Contributor

pox commented Dec 28, 2020

reACK 5865c38
🙏

@theStack theStack force-pushed the 202012-refactor-wallet-createtransaction-return_out_params_in_optstruct branch from 5865c38 to c32e2b1 Compare December 30, 2020 20:46
@theStack
Copy link
Contributor Author

theStack commented Dec 30, 2020

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.

@theStack
Copy link
Contributor Author

theStack commented Mar 1, 2021

Rebased on master.

@theStack
Copy link
Contributor Author

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

@theStack theStack force-pushed the 202012-refactor-wallet-createtransaction-return_out_params_in_optstruct branch from dc1c6d8 to 5238022 Compare March 31, 2022 23:11
@theStack
Copy link
Contributor Author

Rebased on master again.

@laanwj
Copy link
Member

laanwj commented Apr 21, 2022

Concept ACK. Returning things is much cleaner for out-only arguments than using mutable arguments (which would ideally be reserved for inout-arguments only).

Copy link
Member

@furszy furszy left a comment

Choose a reason for hiding this comment

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

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.

Copy link
Member

@achow101 achow101 left a comment

Choose a reason for hiding this comment

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

ACK 5238022

*/
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);
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);
Copy link
Member

Choose a reason for hiding this comment

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

In 5238022 "wallet: CreateTransaction(): return out-params as (optional) struct"

nit: new variables should be snake_case.

Perhaps we should consider changing this to std::optional<int> and get rid of using -1 as a magic number.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nit: new variables should be snake_case.

Thanks, renamed nChangePos to change_pos in both functions CreateTransaction and CreateTransactionInternal in the latest rebase.

Perhaps we should consider changing this to std::optional and get rid of using -1 as a magic number.

Good idea, I think this would make a good follow-up PR candidate.

@theStack theStack force-pushed the 202012-refactor-wallet-createtransaction-return_out_params_in_optstruct branch from 5238022 to 4c5ceb0 Compare May 16, 2022 15:49
@theStack
Copy link
Contributor Author

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

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 4c5ceb0

@@ -82,6 +82,16 @@ std::optional<SelectionResult> AttemptSelection(const CWallet& wallet, const CAm
std::optional<SelectionResult> SelectCoins(const CWallet& wallet, const std::vector<COutput>& vAvailableCoins, const CAmount& nTargetValue, const CCoinControl& coin_control,
const CoinSelectionParams& coin_selection_params) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);

struct CreatedTransactionResult
Copy link
Contributor

Choose a reason for hiding this comment

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

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.

@achow101
Copy link
Member

re-ACK 4c5ceb0

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.

crACK 4c5ceb0

@fanquake fanquake merged commit 1ab389b into bitcoin:master May 17, 2022
@theStack theStack deleted the 202012-refactor-wallet-createtransaction-return_out_params_in_optstruct branch May 17, 2022 10:30
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request May 28, 2022
@bitcoin bitcoin locked and limited conversation to collaborators May 17, 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.

None yet