Skip to content

Commit 94d851d

Browse files
committed
Fix bnb_search_test to use set equivalence for
For BnB, we only want to check that sets are equivalent with their values, whereas in knapsack we care about the outpoints.
1 parent 224e90d commit 94d851d

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

src/wallet/test/coinselector_tests.cpp

+27-8
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ typedef std::set<CInputCoin> CoinSet;
3131
static const CoinEligibilityFilter filter_standard(1, 6, 0);
3232
static const CoinEligibilityFilter filter_confirmed(1, 1, 0);
3333
static const CoinEligibilityFilter filter_standard_extra(6, 6, 0);
34+
static int nextLockTime = 0;
3435

3536
static void add_coin(const CAmount& nValue, int nInput, std::vector<CInputCoin>& set)
3637
{
3738
CMutableTransaction tx;
3839
tx.vout.resize(nInput + 1);
3940
tx.vout[nInput].nValue = nValue;
41+
tx.nLockTime = nextLockTime++; // so all transactions get different hashes
4042
set.emplace_back(MakeTransactionRef(tx), nInput);
4143
}
4244

@@ -45,6 +47,7 @@ static void add_coin(const CAmount& nValue, int nInput, CoinSet& set, CAmount fe
4547
CMutableTransaction tx;
4648
tx.vout.resize(nInput + 1);
4749
tx.vout[nInput].nValue = nValue;
50+
tx.nLockTime = nextLockTime++; // so all transactions get different hashes
4851
CInputCoin coin(MakeTransactionRef(tx), nInput);
4952
coin.effective_value = nValue - fee;
5053
coin.m_fee = fee;
@@ -54,7 +57,6 @@ static void add_coin(const CAmount& nValue, int nInput, CoinSet& set, CAmount fe
5457

5558
static void add_coin(std::vector<COutput>& coins, CWallet& wallet, const CAmount& nValue, int nAge = 6*24, bool fIsFromMe = false, int nInput=0, bool spendable = false)
5659
{
57-
static int nextLockTime = 0;
5860
CMutableTransaction tx;
5961
tx.nLockTime = nextLockTime++; // so all transactions get different hashes
6062
tx.vout.resize(nInput + 1);
@@ -81,6 +83,23 @@ static void add_coin(std::vector<COutput>& coins, CWallet& wallet, const CAmount
8183
coins.push_back(output);
8284
}
8385

86+
static bool equivalent_sets(CoinSet a, CoinSet b)
87+
{
88+
std::vector<CAmount> a_amts;
89+
std::vector<CAmount> b_amts;
90+
for (const auto& coin : a) {
91+
a_amts.push_back(coin.txout.nValue);
92+
}
93+
for (const auto& coin : b) {
94+
b_amts.push_back(coin.txout.nValue);
95+
}
96+
std::sort(a_amts.begin(), a_amts.end());
97+
std::sort(b_amts.begin(), b_amts.end());
98+
99+
std::pair<std::vector<CAmount>::iterator, std::vector<CAmount>::iterator> ret = mismatch(a_amts.begin(), a_amts.end(), b_amts.begin());
100+
return ret.first == a_amts.end() && ret.second == b_amts.end();
101+
}
102+
84103
static bool equal_sets(CoinSet a, CoinSet b)
85104
{
86105
std::pair<CoinSet::iterator, CoinSet::iterator> ret = mismatch(a.begin(), a.end(), b.begin());
@@ -158,15 +177,15 @@ BOOST_AUTO_TEST_CASE(bnb_search_test)
158177
// Select 1 Cent
159178
add_coin(1 * CENT, 1, actual_selection);
160179
BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 1 * CENT, 0.5 * CENT, selection, value_ret));
161-
BOOST_CHECK(equal_sets(selection, actual_selection));
180+
BOOST_CHECK(equivalent_sets(selection, actual_selection));
162181
BOOST_CHECK_EQUAL(value_ret, 1 * CENT);
163182
actual_selection.clear();
164183
selection.clear();
165184

166185
// Select 2 Cent
167186
add_coin(2 * CENT, 2, actual_selection);
168187
BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 2 * CENT, 0.5 * CENT, selection, value_ret));
169-
BOOST_CHECK(equal_sets(selection, actual_selection));
188+
BOOST_CHECK(equivalent_sets(selection, actual_selection));
170189
BOOST_CHECK_EQUAL(value_ret, 2 * CENT);
171190
actual_selection.clear();
172191
selection.clear();
@@ -175,7 +194,7 @@ BOOST_AUTO_TEST_CASE(bnb_search_test)
175194
add_coin(4 * CENT, 4, actual_selection);
176195
add_coin(1 * CENT, 1, actual_selection);
177196
BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 5 * CENT, 0.5 * CENT, selection, value_ret));
178-
BOOST_CHECK(equal_sets(selection, actual_selection));
197+
BOOST_CHECK(equivalent_sets(selection, actual_selection));
179198
BOOST_CHECK_EQUAL(value_ret, 5 * CENT);
180199
actual_selection.clear();
181200
selection.clear();
@@ -189,7 +208,7 @@ BOOST_AUTO_TEST_CASE(bnb_search_test)
189208
add_coin(1 * CENT, 1, actual_selection);
190209
BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 0.9 * CENT, 0.5 * CENT, selection, value_ret));
191210
BOOST_CHECK_EQUAL(value_ret, 1 * CENT);
192-
BOOST_CHECK(equal_sets(selection, actual_selection));
211+
BOOST_CHECK(equivalent_sets(selection, actual_selection));
193212
actual_selection.clear();
194213
selection.clear();
195214

@@ -204,7 +223,7 @@ BOOST_AUTO_TEST_CASE(bnb_search_test)
204223
add_coin(4 * CENT, 4, actual_selection);
205224
add_coin(1 * CENT, 1, actual_selection);
206225
BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 10 * CENT, 0.5 * CENT, selection, value_ret));
207-
BOOST_CHECK(equal_sets(selection, actual_selection));
226+
BOOST_CHECK(equivalent_sets(selection, actual_selection));
208227
BOOST_CHECK_EQUAL(value_ret, 10 * CENT);
209228
actual_selection.clear();
210229
selection.clear();
@@ -217,7 +236,7 @@ BOOST_AUTO_TEST_CASE(bnb_search_test)
217236
BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 10 * CENT, 5000, selection, value_ret));
218237
BOOST_CHECK_EQUAL(value_ret, 10 * CENT);
219238
// FIXME: this test is redundant with the above, because 1 Cent is selected, not "too small"
220-
// BOOST_CHECK(equal_sets(selection, actual_selection));
239+
// BOOST_CHECK(equivalent_sets(selection, actual_selection));
221240

222241
// Select 0.25 Cent, not possible
223242
BOOST_CHECK(!SelectCoinsBnB(GroupCoins(utxo_pool), 0.25 * CENT, 0.5 * CENT, selection, value_ret));
@@ -247,7 +266,7 @@ BOOST_AUTO_TEST_CASE(bnb_search_test)
247266
}
248267
BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 30 * CENT, 5000, selection, value_ret));
249268
BOOST_CHECK_EQUAL(value_ret, 30 * CENT);
250-
BOOST_CHECK(equal_sets(selection, actual_selection));
269+
BOOST_CHECK(equivalent_sets(selection, actual_selection));
251270

252271
////////////////////
253272
// Behavior tests //

0 commit comments

Comments
 (0)