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
wallet, refactor: return out-params of CreateTransaction() as optional struct #20640
Conversation
6dc88c6
to
8e9266f
Compare
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ConflictsReviewers, 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK not tested.
8e9266f
to
5865c38
Compare
Force-pushed with the proposed changes by @pox (see #20640 (comment)), introducing new constants |
There was a problem hiding this 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
reACK 5865c38 |
5865c38
to
c32e2b1
Compare
Force-pushed with changes as suggested by practicalswift, using explicit datatype |
c32e2b1
to
3a07695
Compare
Rebased on master. |
3a07695
to
5832404
Compare
Rebased on master and changed code to return uninitialized values explicitely via |
dc1c6d8
to
5238022
Compare
Rebased on master again. |
Concept ACK. Returning things is much cleaner for out-only arguments than using mutable arguments (which would ideally be reserved for inout-arguments only). |
There was a problem hiding this 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 5238022
src/wallet/spend.h
Outdated
*/ | ||
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
5238022
to
4c5ceb0
Compare
There was a problem hiding this 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 |
There was a problem hiding this comment.
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.
re-ACK 4c5ceb0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
crACK 4c5ceb0
The method
CWallet::CreateTransaction
currently returns several values in the form of out-parameters:CTransactionRef& tx
)CAmount& nFeeRate
)int& nChangePosInOut
) -- as the name suggests, this is both an in- and out-paramBy 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, butFeeCalculation
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).