Skip to content

Commit f8e7963

Browse files
committed
wallet: add SelectionResult::Merge
1 parent 06f558e commit f8e7963

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

src/wallet/coinselection.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,16 @@ void SelectionResult::AddInput(const OutputGroup& group)
433433
m_use_effective = !group.m_subtract_fee_outputs;
434434
}
435435

436+
void SelectionResult::Merge(const SelectionResult& other)
437+
{
438+
m_target += other.m_target;
439+
m_use_effective |= other.m_use_effective;
440+
if (m_algo == SelectionAlgorithm::MANUAL) {
441+
m_algo = other.m_algo;
442+
}
443+
util::insert(m_selected_inputs, other.m_selected_inputs);
444+
}
445+
436446
const std::set<COutput>& SelectionResult::GetInputSet() const
437447
{
438448
return m_selected_inputs;

src/wallet/coinselection.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,17 +281,16 @@ struct SelectionResult
281281
private:
282282
/** Set of inputs selected by the algorithm to use in the transaction */
283283
std::set<COutput> m_selected_inputs;
284+
/** The target the algorithm selected for. Equal to the recipient amount plus non-input fees */
285+
CAmount m_target;
286+
/** The algorithm used to produce this result */
287+
SelectionAlgorithm m_algo;
284288
/** Whether the input values for calculations should be the effective value (true) or normal value (false) */
285289
bool m_use_effective{false};
286290
/** The computed waste */
287291
std::optional<CAmount> m_waste;
288292

289293
public:
290-
/** The target the algorithm selected for. Note that this may not be equal to the recipient amount as it can include non-input fees */
291-
const CAmount m_target;
292-
/** The algorithm used to produce this result */
293-
const SelectionAlgorithm m_algo;
294-
295294
explicit SelectionResult(const CAmount target, SelectionAlgorithm algo)
296295
: m_target(target), m_algo(algo) {}
297296

@@ -308,12 +307,18 @@ struct SelectionResult
308307
void ComputeAndSetWaste(CAmount change_cost);
309308
[[nodiscard]] CAmount GetWaste() const;
310309

310+
void Merge(const SelectionResult& other);
311+
311312
/** Get m_selected_inputs */
312313
const std::set<COutput>& GetInputSet() const;
313314
/** Get the vector of COutputs that will be used to fill in a CTransaction's vin */
314315
std::vector<COutput> GetShuffledInputVector() const;
315316

316317
bool operator<(SelectionResult other) const;
318+
319+
CAmount GetTarget() const { return m_target; }
320+
321+
SelectionAlgorithm GetAlgo() const { return m_algo; }
317322
};
318323

319324
std::optional<SelectionResult> SelectCoinsBnB(std::vector<OutputGroup>& utxo_pool, const CAmount& selection_target, const CAmount& cost_of_change);

src/wallet/spend.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ std::optional<SelectionResult> SelectCoins(const CWallet& wallet, CoinsResult& a
667667

668668
// Add preset inputs to result
669669
res->AddInput(preset_inputs);
670-
if (res->m_algo == SelectionAlgorithm::MANUAL) {
670+
if (res->GetAlgo() == SelectionAlgorithm::MANUAL) {
671671
res->ComputeAndSetWaste(coin_selection_params.m_cost_of_change);
672672
}
673673

@@ -890,7 +890,7 @@ static util::Result<CreatedTransactionResult> CreateTransactionInternal(
890890
if (!result) {
891891
return util::Error{_("Insufficient funds")};
892892
}
893-
TRACE5(coin_selection, selected_coins, wallet.GetName().c_str(), GetAlgorithmName(result->m_algo).c_str(), result->m_target, result->GetWaste(), result->GetSelectedValue());
893+
TRACE5(coin_selection, selected_coins, wallet.GetName().c_str(), GetAlgorithmName(result->GetAlgo()).c_str(), result->GetTarget(), result->GetWaste(), result->GetSelectedValue());
894894

895895
// Always make a change output
896896
// We will reduce the fee from this change output later, and remove the output if it is too small.

0 commit comments

Comments
 (0)