aboutsummaryrefslogtreecommitdiff
path: root/src/wallet/coinselection.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/wallet/coinselection.cpp')
-rw-r--r--src/wallet/coinselection.cpp59
1 files changed, 23 insertions, 36 deletions
diff --git a/src/wallet/coinselection.cpp b/src/wallet/coinselection.cpp
index 42615b5d42..f1706b6800 100644
--- a/src/wallet/coinselection.cpp
+++ b/src/wallet/coinselection.cpp
@@ -194,7 +194,7 @@ util::Result<SelectionResult> SelectCoinsBnB(std::vector<OutputGroup>& utxo_pool
for (const size_t& i : best_selection) {
result.AddInput(utxo_pool.at(i));
}
- result.ComputeAndSetWaste(cost_of_change, cost_of_change, CAmount{0});
+ result.RecalculateWaste(cost_of_change, cost_of_change, CAmount{0});
assert(best_waste == result.GetWaste());
return result;
@@ -792,35 +792,6 @@ void OutputGroupTypeMap::Push(const OutputGroup& group, OutputType type, bool in
}
}
-CAmount SelectionResult::GetSelectionWaste(CAmount change_cost, CAmount target, bool use_effective_value)
-{
- // This function should not be called with empty inputs as that would mean the selection failed
- assert(!m_selected_inputs.empty());
-
- // Always consider the cost of spending an input now vs in the future.
- CAmount waste = 0;
- for (const auto& coin_ptr : m_selected_inputs) {
- const COutput& coin = *coin_ptr;
- waste += coin.GetFee() - coin.long_term_fee;
- }
- // Bump fee of whole selection may diverge from sum of individual bump fees
- waste -= bump_fee_group_discount;
-
- if (change_cost) {
- // Consider the cost of making change and spending it in the future
- // If we aren't making change, the caller should've set change_cost to 0
- assert(change_cost > 0);
- waste += change_cost;
- } else {
- // When we are not making change (change_cost == 0), consider the excess we are throwing away to fees
- CAmount selected_effective_value = use_effective_value ? GetSelectedEffectiveValue() : GetSelectedValue();
- assert(selected_effective_value >= target);
- waste += selected_effective_value - target;
- }
-
- return waste;
-}
-
CAmount GenerateChangeTarget(const CAmount payment_value, const CAmount change_fee, FastRandomContext& rng)
{
if (payment_value <= CHANGE_LOWER / 2) {
@@ -839,16 +810,32 @@ void SelectionResult::SetBumpFeeDiscount(const CAmount discount)
bump_fee_group_discount = discount;
}
-
-void SelectionResult::ComputeAndSetWaste(const CAmount min_viable_change, const CAmount change_cost, const CAmount change_fee)
+void SelectionResult::RecalculateWaste(const CAmount min_viable_change, const CAmount change_cost, const CAmount change_fee)
{
- const CAmount change = GetChange(min_viable_change, change_fee);
+ // This function should not be called with empty inputs as that would mean the selection failed
+ assert(!m_selected_inputs.empty());
+
+ // Always consider the cost of spending an input now vs in the future.
+ CAmount waste = 0;
+ for (const auto& coin_ptr : m_selected_inputs) {
+ const COutput& coin = *coin_ptr;
+ waste += coin.GetFee() - coin.long_term_fee;
+ }
+ // Bump fee of whole selection may diverge from sum of individual bump fees
+ waste -= bump_fee_group_discount;
- if (change > 0) {
- m_waste = GetSelectionWaste(change_cost, m_target, m_use_effective);
+ if (GetChange(min_viable_change, change_fee)) {
+ // if we have a minimum viable amount after deducting fees, account for
+ // cost of creating and spending change
+ waste += change_cost;
} else {
- m_waste = GetSelectionWaste(0, m_target, m_use_effective);
+ // When we are not making change (GetChange(…) == 0), consider the excess we are throwing away to fees
+ CAmount selected_effective_value = m_use_effective ? GetSelectedEffectiveValue() : GetSelectedValue();
+ assert(selected_effective_value >= m_target);
+ waste += selected_effective_value - m_target;
}
+
+ m_waste = waste;
}
void SelectionResult::SetAlgoCompleted(bool algo_completed)