Browse Source

Try to reduce change output to make needed fee in CreateTransaction

Once we've picked coins and dummy-signed the transaction to calculate fee, if we don't have sufficient fee, then try to meet the fee by reducing change before resorting to picking new coins.
0.14
Alex Morcos 8 years ago
parent
commit
42f5ce4093
  1. 12
      src/wallet/wallet.cpp
  2. 4
      src/wallet/wallet.h

12
src/wallet/wallet.cpp

@ -2537,6 +2537,18 @@ bool CWallet::CreateTransaction(const vector<CRecipient>& vecSend, CWalletTx& wt
if (nFeeRet >= nFeeNeeded) if (nFeeRet >= nFeeNeeded)
break; // Done, enough fee included. break; // Done, enough fee included.
// Try to reduce change to include necessary fee
if (nChangePosInOut != -1 && nSubtractFeeFromAmount == 0) {
CAmount additionalFeeNeeded = nFeeNeeded - nFeeRet;
vector<CTxOut>::iterator change_position = txNew.vout.begin()+nChangePosInOut;
// Only reduce change if remaining amount is still a large enough output.
if (change_position->nValue >= MIN_FINAL_CHANGE + additionalFeeNeeded) {
change_position->nValue -= additionalFeeNeeded;
nFeeRet += additionalFeeNeeded;
break; // Done, able to increase fee from change
}
}
// Include more fee and try again. // Include more fee and try again.
nFeeRet = nFeeNeeded; nFeeRet = nFeeNeeded;
continue; continue;

4
src/wallet/wallet.h

@ -48,8 +48,10 @@ static const CAmount DEFAULT_TRANSACTION_FEE = 0;
static const CAmount DEFAULT_FALLBACK_FEE = 20000; static const CAmount DEFAULT_FALLBACK_FEE = 20000;
//! -mintxfee default //! -mintxfee default
static const CAmount DEFAULT_TRANSACTION_MINFEE = 1000; static const CAmount DEFAULT_TRANSACTION_MINFEE = 1000;
//! minimum change amount //! target minimum change amount
static const CAmount MIN_CHANGE = CENT; static const CAmount MIN_CHANGE = CENT;
//! final minimum change amount after paying for fees
static const CAmount MIN_FINAL_CHANGE = MIN_CHANGE/2;
//! Default for -spendzeroconfchange //! Default for -spendzeroconfchange
static const bool DEFAULT_SPEND_ZEROCONF_CHANGE = true; static const bool DEFAULT_SPEND_ZEROCONF_CHANGE = true;
//! Default for -sendfreetransactions //! Default for -sendfreetransactions

Loading…
Cancel
Save