Fix rounding errors in calculation of minimum change size

This commit is contained in:
Alex Morcos 2017-08-25 14:46:07 -05:00
parent 3f726c99f8
commit a54c7b94f8

View file

@ -2861,14 +2861,16 @@ bool CWallet::CreateTransaction(const std::vector<CRecipient>& vecSend, CWalletT
// new inputs. We now know we only need the smaller fee // new inputs. We now know we only need the smaller fee
// (because of reduced tx size) and so we should add a // (because of reduced tx size) and so we should add a
// change output. Only try this once. // change output. Only try this once.
CAmount fee_needed_for_change = GetMinimumFee(change_prototype_size, coin_control, ::mempool, ::feeEstimator, nullptr); if (nChangePosInOut == -1 && nSubtractFeeFromAmount == 0 && pick_new_inputs) {
unsigned int tx_size_with_change = nBytes + change_prototype_size + 2; // Add 2 as a buffer in case increasing # of outputs changes compact size
CAmount fee_needed_with_change = GetMinimumFee(tx_size_with_change, coin_control, ::mempool, ::feeEstimator, nullptr);
CAmount minimum_value_for_change = GetDustThreshold(change_prototype_txout, discard_rate); CAmount minimum_value_for_change = GetDustThreshold(change_prototype_txout, discard_rate);
CAmount max_excess_fee = fee_needed_for_change + minimum_value_for_change; if (nFeeRet >= fee_needed_with_change + minimum_value_for_change) {
if (nFeeRet > nFeeNeeded + max_excess_fee && nChangePosInOut == -1 && nSubtractFeeFromAmount == 0 && pick_new_inputs) {
pick_new_inputs = false; pick_new_inputs = false;
nFeeRet = nFeeNeeded + fee_needed_for_change; nFeeRet = fee_needed_with_change;
continue; continue;
} }
}
// If we have change output already, just increase it // If we have change output already, just increase it
if (nFeeRet > nFeeNeeded && nChangePosInOut != -1 && nSubtractFeeFromAmount == 0) { if (nFeeRet > nFeeNeeded && nChangePosInOut != -1 && nSubtractFeeFromAmount == 0) {