Skip to content

Commit

Permalink
wallet: Update BnB upper bound to be consistent with tx building
Browse files Browse the repository at this point in the history
Use min_viable_change instead of cost_of_change as the upper limit of excess allowed when computing a changeless BnB solution.

This prevents a corner case where dust threshold > change_fee results in some changeless BnB solutions not being considered, despite change being dropped during tx building.

h/t @S3RK for identifying this issue in bitcoin#26466 .
  • Loading branch information
remyers committed Sep 16, 2024
1 parent 17bc70f commit 7df6376
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions src/wallet/spend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1119,19 +1119,15 @@ static util::Result<CreatedTransactionResult> CreateTransactionInternal(

coin_selection_params.m_min_change_target = GenerateChangeTarget(std::floor(recipients_sum / vecSend.size()), coin_selection_params.m_change_fee, rng_fast);

if (coin_control.m_max_excess) {
coin_selection_params.m_max_excess = std::max(coin_control.m_max_excess.value(), coin_selection_params.m_cost_of_change);
} else {
coin_selection_params.m_max_excess = coin_selection_params.m_cost_of_change;
}

// The smallest change amount should be:
// 1. at least equal to dust threshold
// 2. at least 1 sat greater than fees to spend it at m_discard_feerate
const auto dust = GetDustThreshold(change_prototype_txout, coin_selection_params.m_discard_feerate);
const auto change_spend_fee = coin_selection_params.m_discard_feerate.GetFee(coin_selection_params.change_spend_size);
coin_selection_params.min_viable_change = std::max(change_spend_fee + 1, dust);

coin_selection_params.m_max_excess = std::max(coin_control.m_max_excess.value_or(0), coin_selection_params.min_viable_change);

// If set, do not add any excess from a changeless transaction to fees
coin_selection_params.m_add_excess_to_recipient_position = coin_control.m_add_excess_to_recipient_position;

Expand Down

0 comments on commit 7df6376

Please sign in to comment.