Skip to content

Commit a54c7b9

Browse files
committed
Fix rounding errors in calculation of minimum change size
1 parent 3f726c9 commit a54c7b9

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/wallet/wallet.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2861,13 +2861,15 @@ bool CWallet::CreateTransaction(const std::vector<CRecipient>& vecSend, CWalletT
28612861
// new inputs. We now know we only need the smaller fee
28622862
// (because of reduced tx size) and so we should add a
28632863
// change output. Only try this once.
2864-
CAmount fee_needed_for_change = GetMinimumFee(change_prototype_size, coin_control, ::mempool, ::feeEstimator, nullptr);
2865-
CAmount minimum_value_for_change = GetDustThreshold(change_prototype_txout, discard_rate);
2866-
CAmount max_excess_fee = fee_needed_for_change + minimum_value_for_change;
2867-
if (nFeeRet > nFeeNeeded + max_excess_fee && nChangePosInOut == -1 && nSubtractFeeFromAmount == 0 && pick_new_inputs) {
2868-
pick_new_inputs = false;
2869-
nFeeRet = nFeeNeeded + fee_needed_for_change;
2870-
continue;
2864+
if (nChangePosInOut == -1 && nSubtractFeeFromAmount == 0 && pick_new_inputs) {
2865+
unsigned int tx_size_with_change = nBytes + change_prototype_size + 2; // Add 2 as a buffer in case increasing # of outputs changes compact size
2866+
CAmount fee_needed_with_change = GetMinimumFee(tx_size_with_change, coin_control, ::mempool, ::feeEstimator, nullptr);
2867+
CAmount minimum_value_for_change = GetDustThreshold(change_prototype_txout, discard_rate);
2868+
if (nFeeRet >= fee_needed_with_change + minimum_value_for_change) {
2869+
pick_new_inputs = false;
2870+
nFeeRet = fee_needed_with_change;
2871+
continue;
2872+
}
28712873
}
28722874

28732875
// If we have change output already, just increase it

0 commit comments

Comments
 (0)