Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2863,13 +2863,15 @@ static void ApproximateBestSubset(const std::vector<CInputCoin>& vValue, const C

vfBest.assign(vValue.size(), true);
nBest = nTotalLower;
int nBestInputCount = 0;

FastRandomContext insecure_rand;

for (int nRep = 0; nRep < iterations && nBest != nTargetValue; nRep++)
{
vfIncluded.assign(vValue.size(), false);
CAmount nTotal = 0;
int nTotalInputCount = 0;
bool fReachedTarget = false;
for (int nPass = 0; nPass < 2 && !fReachedTarget; nPass++)
{
Expand All @@ -2884,16 +2886,19 @@ static void ApproximateBestSubset(const std::vector<CInputCoin>& vValue, const C
if (nPass == 0 ? insecure_rand.randbool() : !vfIncluded[i])
{
nTotal += vValue[i].txout.nValue;
++nTotalInputCount;
vfIncluded[i] = true;
if (nTotal >= nTargetValue)
{
fReachedTarget = true;
if (nTotal < nBest)
if (nTotal < nBest || (nTotal == nBest && nTotalInputCount < nBestInputCount))
{
nBest = nTotal;
nBestInputCount = nTotalInputCount;
vfBest = vfIncluded;
}
nTotal -= vValue[i].txout.nValue;
--nTotalInputCount;
vfIncluded[i] = false;
}
}
Expand Down Expand Up @@ -3047,7 +3052,7 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMin
CAmount nBest;

ApproximateBestSubset(vValue, nTotalLower, nTargetValue, vfBest, nBest);
if (nBest != nTargetValue && nTotalLower >= nTargetValue + nMinChange)
if (nBest != nTargetValue && nMinChange != 0 && nTotalLower >= nTargetValue + nMinChange)
ApproximateBestSubset(vValue, nTotalLower, nTargetValue + nMinChange, vfBest, nBest);

// If we have a bigger coin and (either the stochastic approximation didn't find a good solution,
Expand Down