From 235f78b0c169aa9b7decff124c818ea5de9380f3 Mon Sep 17 00:00:00 2001 From: saizsassin <39055732+saizsassin@users.noreply.github.com> Date: Thu, 10 May 2018 15:33:37 +0530 Subject: [PATCH] FIFO selection wallet.cpp This wallet.cpp version selects the UTXOs based on FIFO i.e, the coins received first will be spent first --- src/wallet/wallet.cpp | 44 ++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 00abd0c6e..a1be221c6 100755 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -2458,9 +2458,10 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMin bool CWallet::SelectCoins(const std::vector& vAvailableCoins, const CAmount& nTargetValue, std::set& setCoinsRet, CAmount& nValueRet, const CCoinControl* coinControl) const { - /*This selectCoins function selects the coins (UTXOs) in FIFO order i.e, the coins that is received first will be sent to spending first*/ +/*This selectCoins function selects the coins (UTXOs) in FIFO order i.e, the coins that is received first will be sent to spending first*/ //get the received time of each coin and store in set time (using set because the content will be automatically stored in ascending order of the data) + std::vector vCoinsTmp(vAvailableCoins); std::vector::iterator it; std::set time; @@ -2489,26 +2490,26 @@ bool CWallet::SelectCoins(const std::vector& vAvailableCoins, const CAm } // calculate value from preset inputs and store them in PresetCoins set - std::vector setPresetCoins; - CAmount nValueFromPresetInputs = 0; + std::vector setPresetCoins; + CAmount nValueFromPresetInputs = 0; setPresetCoins.clear(); - std::vector vPresetInputs; - if (coinControl) - coinControl->ListSelected(vPresetInputs); - for (const COutPoint& outpoint : vPresetInputs) - { - std::map::const_iterator it = mapWallet.find(outpoint.hash); - if (it != mapWallet.end()) - { - const CWalletTx* pcoin = &it->second; - // Clearly invalid input, fail - if (pcoin->tx->vout.size() <= outpoint.n) - return false; - nValueFromPresetInputs += pcoin->tx->vout[outpoint.n].nValue; - setPresetCoins.push_back(CInputCoin(pcoin, outpoint.n)); - } else - return false; // TODO: Allow non-wallet inputs + std::vector vPresetInputs; + if (coinControl) + coinControl->ListSelected(vPresetInputs); + for (const COutPoint& outpoint : vPresetInputs) + { + std::map::const_iterator it = mapWallet.find(outpoint.hash); + if (it != mapWallet.end()) + { + const CWalletTx* pcoin = &it->second; + // Clearly invalid input, fail + if (pcoin->tx->vout.size() <= outpoint.n) + return false; + nValueFromPresetInputs += pcoin->tx->vout[outpoint.n].nValue; + setPresetCoins.push_back(CInputCoin(pcoin, outpoint.n)); + } else + return false; // TODO: Allow non-wallet inputs } @@ -2521,13 +2522,13 @@ bool CWallet::SelectCoins(const std::vector& vAvailableCoins, const CAm ++it; } - //insert all preset Inputs to the setCoinsRet (returning set of selected utxo) + //insert all preset Inputs to the setCoinsRet (returning set of selected utxo) setCoinsRet.clear(); setCoinsRet.insert(setPresetCoins.begin(), setPresetCoins.end()); nValueRet = nValueFromPresetInputs; //return true if total preset input value is greater than required amount - if (nValueFromPresetInputs >= nTargetValue) + if (nValueFromPresetInputs >= nTargetValue) return true; //select UTXOs from vCoins and insert into setCoinsRet (returning set of selected utxo) until required amount is obtained @@ -4372,3 +4373,4 @@ bool CMerkleTx::AcceptToMemoryPool(const CAmount& nAbsurdFee, CValidationState& { return ::AcceptToMemoryPool(mempool, state, tx, true, nullptr, nullptr, false, nAbsurdFee); } +