Skip to content

Commit 42f5ce4

Browse files
committed
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.
1 parent f646275 commit 42f5ce4

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/wallet/wallet.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2537,6 +2537,18 @@ bool CWallet::CreateTransaction(const vector<CRecipient>& vecSend, CWalletTx& wt
25372537
if (nFeeRet >= nFeeNeeded)
25382538
break; // Done, enough fee included.
25392539

2540+
// Try to reduce change to include necessary fee
2541+
if (nChangePosInOut != -1 && nSubtractFeeFromAmount == 0) {
2542+
CAmount additionalFeeNeeded = nFeeNeeded - nFeeRet;
2543+
vector<CTxOut>::iterator change_position = txNew.vout.begin()+nChangePosInOut;
2544+
// Only reduce change if remaining amount is still a large enough output.
2545+
if (change_position->nValue >= MIN_FINAL_CHANGE + additionalFeeNeeded) {
2546+
change_position->nValue -= additionalFeeNeeded;
2547+
nFeeRet += additionalFeeNeeded;
2548+
break; // Done, able to increase fee from change
2549+
}
2550+
}
2551+
25402552
// Include more fee and try again.
25412553
nFeeRet = nFeeNeeded;
25422554
continue;

src/wallet/wallet.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ static const CAmount DEFAULT_TRANSACTION_FEE = 0;
4848
static const CAmount DEFAULT_FALLBACK_FEE = 20000;
4949
//! -mintxfee default
5050
static const CAmount DEFAULT_TRANSACTION_MINFEE = 1000;
51-
//! minimum change amount
51+
//! target minimum change amount
5252
static const CAmount MIN_CHANGE = CENT;
53+
//! final minimum change amount after paying for fees
54+
static const CAmount MIN_FINAL_CHANGE = MIN_CHANGE/2;
5355
//! Default for -spendzeroconfchange
5456
static const bool DEFAULT_SPEND_ZEROCONF_CHANGE = true;
5557
//! Default for -sendfreetransactions

0 commit comments

Comments
 (0)