Skip to content

Commit fc0f52d

Browse files
committed
Added a test for the pruning of extraneous inputs after ApproximateBestSet
1 parent af9510e commit fc0f52d

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/wallet/test/wallet_tests.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,4 +328,22 @@ BOOST_AUTO_TEST_CASE(coin_selection_tests)
328328
empty_wallet();
329329
}
330330

331+
BOOST_AUTO_TEST_CASE(pruning_in_ApproximateBestSet)
332+
{
333+
CoinSet setCoinsRet;
334+
CAmount nValueRet;
335+
336+
LOCK(wallet.cs_wallet);
337+
338+
empty_wallet();
339+
for (int i = 0; i < 12; i++)
340+
{
341+
add_coin(10*CENT);
342+
}
343+
add_coin(100*CENT);
344+
add_coin(100*CENT);
345+
BOOST_CHECK(wallet.SelectCoinsMinConf(221*CENT, 1, 6, vCoins, setCoinsRet, nValueRet));
346+
BOOST_CHECK_EQUAL(nValueRet, 230*CENT);
347+
}
348+
331349
BOOST_AUTO_TEST_SUITE_END()

src/wallet/wallet.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,7 +1605,7 @@ static void ApproximateBestSubset(vector<pair<CAmount, pair<const CWalletTx*,uns
16051605
bool fReachedTarget = false;
16061606
for (int nPass = 0; nPass < 2 && !fReachedTarget; nPass++)
16071607
{
1608-
for (unsigned int i = 0; i < vValue.size() && !fReachedTarget; i++)
1608+
for (unsigned int i = 0; i < vValue.size(); i++)
16091609
{
16101610
//The solver here uses a randomized algorithm,
16111611
//the randomness serves no real security purpose but is just
@@ -1625,6 +1625,8 @@ static void ApproximateBestSubset(vector<pair<CAmount, pair<const CWalletTx*,uns
16251625
nBest = nTotal;
16261626
vfBest = vfIncluded;
16271627
}
1628+
nTotal -= vValue[i].first;
1629+
vfIncluded[i] = false;
16281630
}
16291631
}
16301632
}
@@ -1634,11 +1636,11 @@ static void ApproximateBestSubset(vector<pair<CAmount, pair<const CWalletTx*,uns
16341636
//Reduces the approximate best subset by removing any inputs that are smaller than the surplus of nTotal beyond nTargetValue.
16351637
for (unsigned int i = 0; i < vValue.size(); i++)
16361638
{
1637-
if (vfBest[i] && (nBest - vValue[i].first) >= nTargetValue )
1638-
{
1639-
vfBest[i] = false;
1640-
nBest -= vValue[i].first;
1641-
}
1639+
if (vfBest[i] && (nBest - vValue[i].first) >= nTargetValue )
1640+
{
1641+
vfBest[i] = false;
1642+
nBest -= vValue[i].first;
1643+
}
16421644
}
16431645
}
16441646

0 commit comments

Comments
 (0)