From 4474de857327fdd06d0a62f774fda4c282f42a6d Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Sun, 6 Mar 2016 17:50:14 +0300 Subject: [PATCH] Fix nChangePosRet in CreateTransaction Closes #716 --- src/wallet/wallet.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index ca0a63a65..6c5e3639c 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -2947,6 +2947,7 @@ bool CWallet::CreateTransaction(const vector& vecSend, CWalletTx& wt } const CAmount nChange = nValueIn - nValueToSelect; + CTxOut newTxOut; if (nChange > 0) { @@ -2987,7 +2988,7 @@ bool CWallet::CreateTransaction(const vector& vecSend, CWalletTx& wt scriptChange = GetScriptForDestination(vchPubKey.GetID()); } - CTxOut newTxOut(nChange, scriptChange); + newTxOut = CTxOut(nChange, scriptChange); // We do not move dust-change to fees, because the sender would end up paying more than requested. // This would be against the purpose of the all-inclusive feature. @@ -3045,6 +3046,20 @@ bool CWallet::CreateTransaction(const vector& vecSend, CWalletTx& wt sort(txNew.vin.begin(), txNew.vin.end()); sort(txNew.vout.begin(), txNew.vout.end()); + // If there was change output added before, we must update its position now + if (nChangePosRet != -1) { + int i = 0; + BOOST_FOREACH(const CTxOut& txOut, txNew.vout) + { + if (txOut == newTxOut) + { + nChangePosRet = i; + break; + } + i++; + } + } + // Sign int nIn = 0; CTransaction txNewConst(txNew);