[Wallet] Prevent CInputCoin to be in a null state

This commit is contained in:
NicolasDorier 2017-04-08 03:41:27 +00:00
parent f597dcb7c8
commit c37e32af0d
2 changed files with 17 additions and 21 deletions

View file

@ -2085,7 +2085,7 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMin
nValueRet = 0; nValueRet = 0;
// List of values less than target // List of values less than target
CInputCoin coinLowestLarger; boost::optional<CInputCoin> coinLowestLarger;
std::vector<CInputCoin> vValue; std::vector<CInputCoin> vValue;
CAmount nTotalLower = 0; CAmount nTotalLower = 0;
@ -2119,7 +2119,7 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMin
vValue.push_back(coin); vValue.push_back(coin);
nTotalLower += coin.txout.nValue; nTotalLower += coin.txout.nValue;
} }
else if (coinLowestLarger.IsNull() || coin.txout.nValue < coinLowestLarger.txout.nValue) else if (!coinLowestLarger || coin.txout.nValue < coinLowestLarger->txout.nValue)
{ {
coinLowestLarger = coin; coinLowestLarger = coin;
} }
@ -2137,10 +2137,10 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMin
if (nTotalLower < nTargetValue) if (nTotalLower < nTargetValue)
{ {
if (coinLowestLarger.IsNull()) if (!coinLowestLarger)
return false; return false;
setCoinsRet.insert(coinLowestLarger); setCoinsRet.insert(coinLowestLarger.get());
nValueRet += coinLowestLarger.txout.nValue; nValueRet += coinLowestLarger->txout.nValue;
return true; return true;
} }
@ -2156,11 +2156,11 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMin
// If we have a bigger coin and (either the stochastic approximation didn't find a good solution, // If we have a bigger coin and (either the stochastic approximation didn't find a good solution,
// or the next bigger coin is closer), return the bigger coin // or the next bigger coin is closer), return the bigger coin
if (!coinLowestLarger.IsNull() && if (coinLowestLarger &&
((nBest != nTargetValue && nBest < nTargetValue + MIN_CHANGE) || coinLowestLarger.txout.nValue <= nBest)) ((nBest != nTargetValue && nBest < nTargetValue + MIN_CHANGE) || coinLowestLarger->txout.nValue <= nBest))
{ {
setCoinsRet.insert(coinLowestLarger); setCoinsRet.insert(coinLowestLarger.get());
nValueRet += coinLowestLarger.txout.nValue; nValueRet += coinLowestLarger->txout.nValue;
} }
else { else {
for (unsigned int i = 0; i < vValue.size(); i++) for (unsigned int i = 0; i < vValue.size(); i++)

View file

@ -477,21 +477,17 @@ public:
class CInputCoin { class CInputCoin {
public: public:
CInputCoin()
{
}
CInputCoin(const CWalletTx* walletTx, unsigned int i) CInputCoin(const CWalletTx* walletTx, unsigned int i)
{ {
if (walletTx != nullptr && i < walletTx->tx->vout.size()) if (!walletTx)
{ throw std::invalid_argument("walletTx should not be null");
if (i >= walletTx->tx->vout.size())
throw std::out_of_range("The output index is out of range");
outpoint = COutPoint(walletTx->GetHash(), i); outpoint = COutPoint(walletTx->GetHash(), i);
txout = walletTx->tx->vout[i]; txout = walletTx->tx->vout[i];
} }
}
bool IsNull() const
{
return outpoint.IsNull() && txout.IsNull();
}
COutPoint outpoint; COutPoint outpoint;
CTxOut txout; CTxOut txout;