Skip to content

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

Merged
fanquake merged 2 commits into
bitcoin:masterfrom
theStack:202012-refactor-wallet-createtransaction-return_out_params_in_optstruct
May 17, 2022
Merged

wallet, refactor: return out-params of CreateTransaction() as optional struct#20640
fanquake merged 2 commits into
bitcoin:masterfrom
theStack:202012-refactor-wallet-createtransaction-return_out_params_in_optstruct

Conversation

@theStack

@theStack theStack commented Dec 13, 2020

Copy link
Copy Markdown
Contributor

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

DrahtBot commented Dec 13, 2020

Copy link
Copy Markdown
Contributor

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.

@pox pox left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ACK not tested.

Comment thread src/wallet/feebumper.cpp Outdated
Comment thread src/wallet/wallet.cpp Outdated
@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
Copy Markdown
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.

Comment thread src/wallet/feebumper.cpp Outdated

@stackman27 stackman27 left a comment

Copy link
Copy Markdown
Contributor

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

pox commented Dec 28, 2020

Copy link
Copy Markdown
Contributor

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

theStack commented Dec 30, 2020

Copy link
Copy Markdown
Contributor Author

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

theStack commented Mar 1, 2021

Copy link
Copy Markdown
Contributor Author

Rebased on master.

@theStack
theStack force-pushed the 202012-refactor-wallet-createtransaction-return_out_params_in_optstruct branch from 3a07695 to 5832404 Compare March 31, 2021 18:35
@theStack

Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

Rebased on master again.

@laanwj

laanwj commented Apr 21, 2022

Copy link
Copy Markdown
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 furszy left a comment

Copy link
Copy Markdown
Member

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.

@achow101 achow101 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ACK 5238022161a1ecd361916c6f425439e7c7604758

Comment thread src/wallet/spend.h Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In 5238022161a1ecd361916c6f425439e7c7604758 "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
Copy Markdown
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
Copy Markdown
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).

@murchandamus murchandamus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ACK 4c5ceb0

Comment thread src/wallet/spend.h Outdated

Copy link
Copy Markdown
Member

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
Copy Markdown
Member

re-ACK 4c5ceb0

@w0xlt w0xlt left a comment

Copy link
Copy Markdown
Contributor

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