From ce7435cf1ef36109595be9a3a3955afdff1d63e4 Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Fri, 9 Mar 2018 17:21:27 -0500 Subject: [PATCH] Move output eligibility to a separate function --- src/wallet/wallet.cpp | 28 ++++++++++++++++------------ src/wallet/wallet.h | 3 +++ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index e797a63dd83..639e00f2bc2 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -2484,6 +2484,20 @@ static void ApproximateBestSubset(const std::vector& vValue, const C } } +bool CWallet::OutputEligibleForSpending(const COutput& output, const int nConfMine, const int nConfTheirs, const uint64_t nMaxAncestors) const +{ + if (!output.fSpendable) + return false; + + if (output.nDepth < (output.tx->IsFromMe(ISMINE_ALL) ? nConfMine : nConfTheirs)) + return false; + + if (!mempool.TransactionWithinChainLimit(output.tx->GetHash(), nMaxAncestors)) + return false; + + return true; +} + bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMine, const int nConfTheirs, const uint64_t nMaxAncestors, std::vector vCoins, std::set& setCoinsRet, CAmount& nValueRet) const { @@ -2499,20 +2513,10 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMin for (const COutput &output : vCoins) { - if (!output.fSpendable) + if (!OutputEligibleForSpending(output, nConfMine, nConfTheirs, nMaxAncestors)) continue; - const CWalletTx *pcoin = output.tx; - - if (output.nDepth < (pcoin->IsFromMe(ISMINE_ALL) ? nConfMine : nConfTheirs)) - continue; - - if (!mempool.TransactionWithinChainLimit(pcoin->GetHash(), nMaxAncestors)) - continue; - - int i = output.i; - - CInputCoin coin = CInputCoin(pcoin, i); + CInputCoin coin = CInputCoin(output.tx, output.i); if (coin.txout.nValue == nTargetValue) { diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index a2fab3c88ea..d2389ae2b6d 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -1190,6 +1190,9 @@ public: * This function will automatically add the necessary scripts to the wallet. */ CTxDestination AddAndGetDestinationForScript(const CScript& script, OutputType); + + /** Whether a given output is spendable by this wallet */ + bool OutputEligibleForSpending(const COutput& output, const int nConfMine, const int nConfTheirs, const uint64_t nMaxAncestors) const; }; /** A key allocated from the key pool. */