From a7dd11c6dadeb17cf5444dd29f3a63d80ef4b159 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Sun, 24 Jul 2011 16:37:09 +0200 Subject: Fix for small change outputs With the separation of CENT and MIN_TX_FEE, it is now reasonable to create change outputs between 0.01 and 0.0005, as these are spendable according to the policy, even though they require a fee to be paid. Also, when enough fee was already present, everything can go into a change output, without further increasing the fee. --- src/wallet.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'src/wallet.cpp') diff --git a/src/wallet.cpp b/src/wallet.cpp index 2ee918fdab..e9fa5a5207 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -941,9 +941,17 @@ bool CWallet::CreateTransaction(const vector >& vecSend, CW dPriority += (double)nCredit * pcoin.first->GetDepthInMainChain(); } - // Fill a vout back to self with any change - int64 nChange = nValueIn - nTotalValue; - if (nChange >= CENT) + int64 nChange = nValueIn - nValue - nFeeRet; + // if sub-cent change is required, the fee must be raised to at least MIN_TX_FEE + // or until nChange becomes zero + if (nFeeRet < MIN_TX_FEE && nChange > 0 && nChange < CENT) + { + int64 nMoveToFee = min(nChange, MIN_TX_FEE - nFeeRet); + nChange -= nMoveToFee; + nFeeRet += nMoveToFee; + } + + if (nChange > 0) { // Note: We use a new key here to keep it from being obvious which side is the change. // The drawback is that by not reusing a previous key, the change may be lost if a -- cgit v1.2.3