Skip to content

Commit

Permalink
partial bitcoin#17331: Use effective values throughout coin selection
Browse files Browse the repository at this point in the history
includes:
- 1bf4a62
- af5867c
- d97d25d
- cc3f14b

This pull request logically reverts portions of dash#3668 but
breaks BIP69 sorting, which will be fixed in an upcoming commit. It
has been disabled for now.
  • Loading branch information
kwvg committed Feb 9, 2025
1 parent 66fe2d4 commit 9e9e66f
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 199 deletions.
14 changes: 7 additions & 7 deletions src/wallet/coinselection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ bool SelectCoinsBnB(std::vector<OutputGroup>& utxo_pool, const CAmount& target_v

std::vector<bool> curr_selection; // select the utxo at this index
curr_selection.reserve(utxo_pool.size());
CAmount actual_target = not_input_fees + target_value;
CAmount selection_target = not_input_fees + target_value;

// Calculate curr_available_value
CAmount curr_available_value = 0;
Expand All @@ -81,7 +81,7 @@ bool SelectCoinsBnB(std::vector<OutputGroup>& utxo_pool, const CAmount& target_v
assert(utxo.effective_value > 0);
curr_available_value += utxo.effective_value;
}
if (curr_available_value < actual_target) {
if (curr_available_value < selection_target) {
return false;
}

Expand All @@ -96,12 +96,12 @@ bool SelectCoinsBnB(std::vector<OutputGroup>& utxo_pool, const CAmount& target_v
for (size_t i = 0; i < TOTAL_TRIES; ++i) {
// Conditions for starting a backtrack
bool backtrack = false;
if (curr_value + curr_available_value < actual_target || // Cannot possibly reach target with the amount remaining in the curr_available_value.
curr_value > actual_target + cost_of_change || // Selected value is out of range, go back and try other branch
if (curr_value + curr_available_value < selection_target || // Cannot possibly reach target with the amount remaining in the curr_available_value.
curr_value > selection_target + cost_of_change || // Selected value is out of range, go back and try other branch
(curr_waste > best_waste && (utxo_pool.at(0).fee - utxo_pool.at(0).long_term_fee) > 0)) { // Don't select things which we know will be more wasteful if the waste is increasing
backtrack = true;
} else if (curr_value >= actual_target) { // Selected value is within range
curr_waste += (curr_value - actual_target); // This is the excess value which is added to the waste for the below comparison
} else if (curr_value >= selection_target) { // Selected value is within range
curr_waste += (curr_value - selection_target); // This is the excess value which is added to the waste for the below comparison
// Adding another UTXO after this check could bring the waste down if the long term fee is higher than the current fee.
// However we are not going to explore that because this optimization for the waste is only done when we have hit our target
// value. Adding any more UTXOs will be just burning the UTXO; it will go entirely to fees. Thus we aren't going to
Expand All @@ -114,7 +114,7 @@ bool SelectCoinsBnB(std::vector<OutputGroup>& utxo_pool, const CAmount& target_v
break;
}
}
curr_waste -= (curr_value - actual_target); // Remove the excess value as we will be selecting different coins now
curr_waste -= (curr_value - selection_target); // Remove the excess value as we will be selecting different coins now
backtrack = true;
}

Expand Down
Loading

0 comments on commit 9e9e66f

Please sign in to comment.