Skip to content

Commit 72cad28

Browse files
committed
wallet: calculate and store min_viable_change
1 parent e3210a7 commit 72cad28

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/wallet/coinselection.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ struct CoinSelectionParams {
123123
/** Mininmum change to target in Knapsack solver: select coins to cover the payment and
124124
* at least this value of change. */
125125
CAmount m_min_change_target{0};
126+
/** Minimum amount for creating a change output.
127+
* If change budget is smaller than min_change then we forgo creation of change output.
128+
*/
129+
CAmount min_viable_change{0};
126130
/** Cost of creating the change output. */
127131
CAmount m_change_fee{0};
128132
/** Cost of creating the change output + cost of spending the change output in the future. */

src/wallet/spend.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,13 @@ static util::Result<CreatedTransactionResult> CreateTransactionInternal(
855855

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

858+
// The smallest change amount should be:
859+
// 1. at least equal to dust threshold
860+
// 2. at least 1 sat greater than fees to spend it at m_discard_feerate
861+
const auto dust = GetDustThreshold(change_prototype_txout, coin_selection_params.m_discard_feerate);
862+
const auto change_spend_fee = coin_selection_params.m_discard_feerate.GetFee(coin_selection_params.change_spend_size);
863+
coin_selection_params.min_viable_change = std::max(change_spend_fee + 1, dust);
864+
858865
// vouts to the payees
859866
if (!coin_selection_params.m_subtract_fee_outputs) {
860867
coin_selection_params.tx_noinputs_size = 10; // Static vsize overhead + outputs vsize. 4 nVersion, 4 nLocktime, 1 input count, 1 witness overhead (dummy, flag, stack size)

0 commit comments

Comments
 (0)