Bitcoin Core 22.99.0
P2P Digital Currency
coinselector_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2017-2020 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <consensus/amount.h>
6#include <node/context.h>
8#include <random.h>
10#include <util/translation.h>
11#include <wallet/coincontrol.h>
13#include <wallet/spend.h>
15#include <wallet/wallet.h>
16
17#include <boost/test/unit_test.hpp>
18#include <random>
19
21
22// how many times to run all the tests to have a chance to catch errors that only show up with particular random shuffles
23#define RUN_TESTS 100
24
25// some tests fail 1% of the time due to bad luck.
26// we repeat those tests this many times and only complain if all iterations of the test fail
27#define RANDOM_REPEATS 5
28
29typedef std::set<CInputCoin> CoinSet;
30
34
35static void add_coin(const CAmount& nValue, int nInput, std::vector<CInputCoin>& set)
36{
38 tx.vout.resize(nInput + 1);
39 tx.vout[nInput].nValue = nValue;
40 set.emplace_back(MakeTransactionRef(tx), nInput);
41}
42
43static void add_coin(const CAmount& nValue, int nInput, CoinSet& set, CAmount fee = 0, CAmount long_term_fee = 0)
44{
46 tx.vout.resize(nInput + 1);
47 tx.vout[nInput].nValue = nValue;
48 CInputCoin coin(MakeTransactionRef(tx), nInput);
49 coin.effective_value = nValue - fee;
50 coin.m_fee = fee;
51 coin.m_long_term_fee = long_term_fee;
52 set.insert(coin);
53}
54
55static 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)
56{
57 static int nextLockTime = 0;
59 tx.nLockTime = nextLockTime++; // so all transactions get different hashes
60 tx.vout.resize(nInput + 1);
61 tx.vout[nInput].nValue = nValue;
62 if (spendable) {
63 CTxDestination dest;
65 const bool destination_ok = wallet.GetNewDestination(OutputType::BECH32, "", dest, error);
66 assert(destination_ok);
67 tx.vout[nInput].scriptPubKey = GetScriptForDestination(dest);
68 }
69 if (fIsFromMe) {
70 // IsFromMe() returns (GetDebit() > 0), and GetDebit() is 0 if vin.empty(),
71 // so stop vin being empty, and cache a non-zero Debit to fake out IsFromMe()
72 tx.vin.resize(1);
73 }
74 uint256 txid = tx.GetHash();
75
76 LOCK(wallet.cs_wallet);
77 auto ret = wallet.mapWallet.emplace(std::piecewise_construct, std::forward_as_tuple(txid), std::forward_as_tuple(MakeTransactionRef(std::move(tx))));
78 assert(ret.second);
79 CWalletTx& wtx = (*ret.first).second;
80 if (fIsFromMe)
81 {
83 wtx.m_is_cache_empty = false;
84 }
85 COutput output(wallet, wtx, nInput, nAge, true /* spendable */, true /* solvable */, true /* safe */);
86 coins.push_back(output);
87}
88
89static bool equal_sets(CoinSet a, CoinSet b)
90{
91 std::pair<CoinSet::iterator, CoinSet::iterator> ret = mismatch(a.begin(), a.end(), b.begin());
92 return ret.first == a.end() && ret.second == b.end();
93}
94
95static CAmount make_hard_case(int utxos, std::vector<CInputCoin>& utxo_pool)
96{
97 utxo_pool.clear();
98 CAmount target = 0;
99 for (int i = 0; i < utxos; ++i) {
100 target += (CAmount)1 << (utxos+i);
101 add_coin((CAmount)1 << (utxos+i), 2*i, utxo_pool);
102 add_coin(((CAmount)1 << (utxos+i)) + ((CAmount)1 << (utxos-1-i)), 2*i + 1, utxo_pool);
103 }
104 return target;
105}
106
107inline std::vector<OutputGroup>& GroupCoins(const std::vector<CInputCoin>& coins)
108{
109 static std::vector<OutputGroup> static_groups;
110 static_groups.clear();
111 for (auto& coin : coins) {
112 static_groups.emplace_back();
113 static_groups.back().Insert(coin, 0, true, 0, 0, false);
114 }
115 return static_groups;
116}
117
118inline std::vector<OutputGroup>& GroupCoins(const std::vector<COutput>& coins)
119{
120 static std::vector<OutputGroup> static_groups;
121 static_groups.clear();
122 for (auto& coin : coins) {
123 static_groups.emplace_back();
124 static_groups.back().Insert(coin.GetInputCoin(), coin.nDepth, coin.tx->m_amounts[CWalletTx::DEBIT].m_cached[ISMINE_SPENDABLE] && coin.tx->m_amounts[CWalletTx::DEBIT].m_value[ISMINE_SPENDABLE] == 1 /* HACK: we can't figure out the is_me flag so we use the conditions defined above; perhaps set safe to false for !fIsFromMe in add_coin() */, 0, 0, false);
125 }
126 return static_groups;
127}
128
129inline std::vector<OutputGroup>& KnapsackGroupOutputs(const std::vector<COutput>& coins, CWallet& wallet, const CoinEligibilityFilter& filter)
130{
131 CoinSelectionParams coin_selection_params(/* change_output_size= */ 0,
132 /* change_spend_size= */ 0, /* effective_feerate= */ CFeeRate(0),
133 /* long_term_feerate= */ CFeeRate(0), /* discard_feerate= */ CFeeRate(0),
134 /* tx_noinputs_size= */ 0, /* avoid_partial= */ false);
135 static std::vector<OutputGroup> static_groups;
136 static_groups = GroupOutputs(wallet, coins, coin_selection_params, filter, /* positive_only */false);
137 return static_groups;
138}
139
140// Branch and bound coin selection tests
141BOOST_AUTO_TEST_CASE(bnb_search_test)
142{
143 // Setup
144 std::vector<CInputCoin> utxo_pool;
145 CoinSet selection;
146 CoinSet actual_selection;
147 CAmount value_ret = 0;
148
150 // Known Outcome tests //
152
153 // Empty utxo pool
154 BOOST_CHECK(!SelectCoinsBnB(GroupCoins(utxo_pool), 1 * CENT, 0.5 * CENT, selection, value_ret));
155 selection.clear();
156
157 // Add utxos
158 add_coin(1 * CENT, 1, utxo_pool);
159 add_coin(2 * CENT, 2, utxo_pool);
160 add_coin(3 * CENT, 3, utxo_pool);
161 add_coin(4 * CENT, 4, utxo_pool);
162
163 // Select 1 Cent
164 add_coin(1 * CENT, 1, actual_selection);
165 BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 1 * CENT, 0.5 * CENT, selection, value_ret));
166 BOOST_CHECK(equal_sets(selection, actual_selection));
167 BOOST_CHECK_EQUAL(value_ret, 1 * CENT);
168 actual_selection.clear();
169 selection.clear();
170
171 // Select 2 Cent
172 add_coin(2 * CENT, 2, actual_selection);
173 BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 2 * CENT, 0.5 * CENT, selection, value_ret));
174 BOOST_CHECK(equal_sets(selection, actual_selection));
175 BOOST_CHECK_EQUAL(value_ret, 2 * CENT);
176 actual_selection.clear();
177 selection.clear();
178
179 // Select 5 Cent
180 add_coin(4 * CENT, 4, actual_selection);
181 add_coin(1 * CENT, 1, actual_selection);
182 BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 5 * CENT, 0.5 * CENT, selection, value_ret));
183 BOOST_CHECK(equal_sets(selection, actual_selection));
184 BOOST_CHECK_EQUAL(value_ret, 5 * CENT);
185 actual_selection.clear();
186 selection.clear();
187
188 // Select 11 Cent, not possible
189 BOOST_CHECK(!SelectCoinsBnB(GroupCoins(utxo_pool), 11 * CENT, 0.5 * CENT, selection, value_ret));
190 actual_selection.clear();
191 selection.clear();
192
193 // Cost of change is greater than the difference between target value and utxo sum
194 add_coin(1 * CENT, 1, actual_selection);
195 BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 0.9 * CENT, 0.5 * CENT, selection, value_ret));
196 BOOST_CHECK_EQUAL(value_ret, 1 * CENT);
197 BOOST_CHECK(equal_sets(selection, actual_selection));
198 actual_selection.clear();
199 selection.clear();
200
201 // Cost of change is less than the difference between target value and utxo sum
202 BOOST_CHECK(!SelectCoinsBnB(GroupCoins(utxo_pool), 0.9 * CENT, 0, selection, value_ret));
203 actual_selection.clear();
204 selection.clear();
205
206 // Select 10 Cent
207 add_coin(5 * CENT, 5, utxo_pool);
208 add_coin(5 * CENT, 5, actual_selection);
209 add_coin(4 * CENT, 4, actual_selection);
210 add_coin(1 * CENT, 1, actual_selection);
211 BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 10 * CENT, 0.5 * CENT, selection, value_ret));
212 BOOST_CHECK(equal_sets(selection, actual_selection));
213 BOOST_CHECK_EQUAL(value_ret, 10 * CENT);
214 actual_selection.clear();
215 selection.clear();
216
217 // Negative effective value
218 // Select 10 Cent but have 1 Cent not be possible because too small
219 add_coin(5 * CENT, 5, actual_selection);
220 add_coin(3 * CENT, 3, actual_selection);
221 add_coin(2 * CENT, 2, actual_selection);
222 BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 10 * CENT, 5000, selection, value_ret));
223 BOOST_CHECK_EQUAL(value_ret, 10 * CENT);
224 // FIXME: this test is redundant with the above, because 1 Cent is selected, not "too small"
225 // BOOST_CHECK(equal_sets(selection, actual_selection));
226
227 // Select 0.25 Cent, not possible
228 BOOST_CHECK(!SelectCoinsBnB(GroupCoins(utxo_pool), 0.25 * CENT, 0.5 * CENT, selection, value_ret));
229 actual_selection.clear();
230 selection.clear();
231
232 // Iteration exhaustion test
233 CAmount target = make_hard_case(17, utxo_pool);
234 BOOST_CHECK(!SelectCoinsBnB(GroupCoins(utxo_pool), target, 0, selection, value_ret)); // Should exhaust
235 target = make_hard_case(14, utxo_pool);
236 BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), target, 0, selection, value_ret)); // Should not exhaust
237
238 // Test same value early bailout optimization
239 utxo_pool.clear();
240 add_coin(7 * CENT, 7, actual_selection);
241 add_coin(7 * CENT, 7, actual_selection);
242 add_coin(7 * CENT, 7, actual_selection);
243 add_coin(7 * CENT, 7, actual_selection);
244 add_coin(2 * CENT, 7, actual_selection);
245 add_coin(7 * CENT, 7, utxo_pool);
246 add_coin(7 * CENT, 7, utxo_pool);
247 add_coin(7 * CENT, 7, utxo_pool);
248 add_coin(7 * CENT, 7, utxo_pool);
249 add_coin(2 * CENT, 7, utxo_pool);
250 for (int i = 0; i < 50000; ++i) {
251 add_coin(5 * CENT, 7, utxo_pool);
252 }
253 BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 30 * CENT, 5000, selection, value_ret));
254 BOOST_CHECK_EQUAL(value_ret, 30 * CENT);
255 BOOST_CHECK(equal_sets(selection, actual_selection));
256
258 // Behavior tests //
260 // Select 1 Cent with pool of only greater than 5 Cent
261 utxo_pool.clear();
262 for (int i = 5; i <= 20; ++i) {
263 add_coin(i * CENT, i, utxo_pool);
264 }
265 // Run 100 times, to make sure it is never finding a solution
266 for (int i = 0; i < 100; ++i) {
267 BOOST_CHECK(!SelectCoinsBnB(GroupCoins(utxo_pool), 1 * CENT, 2 * CENT, selection, value_ret));
268 }
269
270 // Make sure that effective value is working in AttemptSelection when BnB is used
271 CoinSelectionParams coin_selection_params_bnb(/* change_output_size= */ 0,
272 /* change_spend_size= */ 0, /* effective_feerate= */ CFeeRate(3000),
273 /* long_term_feerate= */ CFeeRate(1000), /* discard_feerate= */ CFeeRate(1000),
274 /* tx_noinputs_size= */ 0, /* avoid_partial= */ false);
275 {
276 std::unique_ptr<CWallet> wallet = std::make_unique<CWallet>(m_node.chain.get(), "", CreateMockWalletDatabase());
277 wallet->LoadWallet();
278 LOCK(wallet->cs_wallet);
279 wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
280 wallet->SetupDescriptorScriptPubKeyMans();
281
282 std::vector<COutput> coins;
283 CoinSet setCoinsRet;
284 CAmount nValueRet;
285
286 add_coin(coins, *wallet, 1);
287 coins.at(0).nInputBytes = 40; // Make sure that it has a negative effective value. The next check should assert if this somehow got through. Otherwise it will fail
288 BOOST_CHECK(!SelectCoinsBnB(GroupCoins(coins), 1 * CENT, coin_selection_params_bnb.m_cost_of_change, setCoinsRet, nValueRet));
289
290 // Test fees subtracted from output:
291 coins.clear();
292 add_coin(coins, *wallet, 1 * CENT);
293 coins.at(0).nInputBytes = 40;
294 coin_selection_params_bnb.m_subtract_fee_outputs = true;
295 BOOST_CHECK(SelectCoinsBnB(GroupCoins(coins), 1 * CENT, coin_selection_params_bnb.m_cost_of_change, setCoinsRet, nValueRet));
296 BOOST_CHECK_EQUAL(nValueRet, 1 * CENT);
297 }
298
299 {
300 std::unique_ptr<CWallet> wallet = std::make_unique<CWallet>(m_node.chain.get(), "", CreateMockWalletDatabase());
301 wallet->LoadWallet();
302 LOCK(wallet->cs_wallet);
303 wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
304 wallet->SetupDescriptorScriptPubKeyMans();
305
306 std::vector<COutput> coins;
307 CoinSet setCoinsRet;
308 CAmount nValueRet;
309
310 add_coin(coins, *wallet, 5 * CENT, 6 * 24, false, 0, true);
311 add_coin(coins, *wallet, 3 * CENT, 6 * 24, false, 0, true);
312 add_coin(coins, *wallet, 2 * CENT, 6 * 24, false, 0, true);
313 CCoinControl coin_control;
314 coin_control.fAllowOtherInputs = true;
315 coin_control.Select(COutPoint(coins.at(0).tx->GetHash(), coins.at(0).i));
316 coin_selection_params_bnb.m_effective_feerate = CFeeRate(0);
317 BOOST_CHECK(SelectCoins(*wallet, coins, 10 * CENT, setCoinsRet, nValueRet, coin_control, coin_selection_params_bnb));
318 }
319}
320
321BOOST_AUTO_TEST_CASE(knapsack_solver_test)
322{
323 std::unique_ptr<CWallet> wallet = std::make_unique<CWallet>(m_node.chain.get(), "", CreateMockWalletDatabase());
324 wallet->LoadWallet();
325 LOCK(wallet->cs_wallet);
326 wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
327 wallet->SetupDescriptorScriptPubKeyMans();
328
329 CoinSet setCoinsRet, setCoinsRet2;
330 CAmount nValueRet;
331 std::vector<COutput> coins;
332
333 // test multiple times to allow for differences in the shuffle order
334 for (int i = 0; i < RUN_TESTS; i++)
335 {
336 coins.clear();
337
338 // with an empty wallet we can't even pay one cent
339 BOOST_CHECK(!KnapsackSolver(1 * CENT, KnapsackGroupOutputs(coins, *wallet, filter_standard), setCoinsRet, nValueRet));
340
341 add_coin(coins, *wallet, 1*CENT, 4); // add a new 1 cent coin
342
343 // with a new 1 cent coin, we still can't find a mature 1 cent
344 BOOST_CHECK(!KnapsackSolver(1 * CENT, KnapsackGroupOutputs(coins, *wallet, filter_standard), setCoinsRet, nValueRet));
345
346 // but we can find a new 1 cent
347 BOOST_CHECK(KnapsackSolver(1 * CENT, KnapsackGroupOutputs(coins, *wallet, filter_confirmed), setCoinsRet, nValueRet));
348 BOOST_CHECK_EQUAL(nValueRet, 1 * CENT);
349
350 add_coin(coins, *wallet, 2*CENT); // add a mature 2 cent coin
351
352 // we can't make 3 cents of mature coins
353 BOOST_CHECK(!KnapsackSolver(3 * CENT, KnapsackGroupOutputs(coins, *wallet, filter_standard), setCoinsRet, nValueRet));
354
355 // we can make 3 cents of new coins
356 BOOST_CHECK(KnapsackSolver(3 * CENT, KnapsackGroupOutputs(coins, *wallet, filter_confirmed), setCoinsRet, nValueRet));
357 BOOST_CHECK_EQUAL(nValueRet, 3 * CENT);
358
359 add_coin(coins, *wallet, 5*CENT); // add a mature 5 cent coin,
360 add_coin(coins, *wallet, 10*CENT, 3, true); // a new 10 cent coin sent from one of our own addresses
361 add_coin(coins, *wallet, 20*CENT); // and a mature 20 cent coin
362
363 // now we have new: 1+10=11 (of which 10 was self-sent), and mature: 2+5+20=27. total = 38
364
365 // we can't make 38 cents only if we disallow new coins:
366 BOOST_CHECK(!KnapsackSolver(38 * CENT, KnapsackGroupOutputs(coins, *wallet, filter_standard), setCoinsRet, nValueRet));
367 // we can't even make 37 cents if we don't allow new coins even if they're from us
368 BOOST_CHECK(!KnapsackSolver(38 * CENT, KnapsackGroupOutputs(coins, *wallet, filter_standard_extra), setCoinsRet, nValueRet));
369 // but we can make 37 cents if we accept new coins from ourself
370 BOOST_CHECK(KnapsackSolver(37 * CENT, KnapsackGroupOutputs(coins, *wallet, filter_standard), setCoinsRet, nValueRet));
371 BOOST_CHECK_EQUAL(nValueRet, 37 * CENT);
372 // and we can make 38 cents if we accept all new coins
373 BOOST_CHECK(KnapsackSolver(38 * CENT, KnapsackGroupOutputs(coins, *wallet, filter_confirmed), setCoinsRet, nValueRet));
374 BOOST_CHECK_EQUAL(nValueRet, 38 * CENT);
375
376 // try making 34 cents from 1,2,5,10,20 - we can't do it exactly
377 BOOST_CHECK(KnapsackSolver(34 * CENT, KnapsackGroupOutputs(coins, *wallet, filter_confirmed), setCoinsRet, nValueRet));
378 BOOST_CHECK_EQUAL(nValueRet, 35 * CENT); // but 35 cents is closest
379 BOOST_CHECK_EQUAL(setCoinsRet.size(), 3U); // the best should be 20+10+5. it's incredibly unlikely the 1 or 2 got included (but possible)
380
381 // when we try making 7 cents, the smaller coins (1,2,5) are enough. We should see just 2+5
382 BOOST_CHECK(KnapsackSolver(7 * CENT, KnapsackGroupOutputs(coins, *wallet, filter_confirmed), setCoinsRet, nValueRet));
383 BOOST_CHECK_EQUAL(nValueRet, 7 * CENT);
384 BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U);
385
386 // when we try making 8 cents, the smaller coins (1,2,5) are exactly enough.
387 BOOST_CHECK(KnapsackSolver(8 * CENT, KnapsackGroupOutputs(coins, *wallet, filter_confirmed), setCoinsRet, nValueRet));
388 BOOST_CHECK(nValueRet == 8 * CENT);
389 BOOST_CHECK_EQUAL(setCoinsRet.size(), 3U);
390
391 // when we try making 9 cents, no subset of smaller coins is enough, and we get the next bigger coin (10)
392 BOOST_CHECK(KnapsackSolver(9 * CENT, KnapsackGroupOutputs(coins, *wallet, filter_confirmed), setCoinsRet, nValueRet));
393 BOOST_CHECK_EQUAL(nValueRet, 10 * CENT);
394 BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
395
396 // now clear out the wallet and start again to test choosing between subsets of smaller coins and the next biggest coin
397 coins.clear();
398
399 add_coin(coins, *wallet, 6*CENT);
400 add_coin(coins, *wallet, 7*CENT);
401 add_coin(coins, *wallet, 8*CENT);
402 add_coin(coins, *wallet, 20*CENT);
403 add_coin(coins, *wallet, 30*CENT); // now we have 6+7+8+20+30 = 71 cents total
404
405 // check that we have 71 and not 72
406 BOOST_CHECK(KnapsackSolver(71 * CENT, KnapsackGroupOutputs(coins, *wallet, filter_confirmed), setCoinsRet, nValueRet));
407 BOOST_CHECK(!KnapsackSolver(72 * CENT, KnapsackGroupOutputs(coins, *wallet, filter_confirmed), setCoinsRet, nValueRet));
408
409 // now try making 16 cents. the best smaller coins can do is 6+7+8 = 21; not as good at the next biggest coin, 20
410 BOOST_CHECK(KnapsackSolver(16 * CENT, KnapsackGroupOutputs(coins, *wallet, filter_confirmed), setCoinsRet, nValueRet));
411 BOOST_CHECK_EQUAL(nValueRet, 20 * CENT); // we should get 20 in one coin
412 BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
413
414 add_coin(coins, *wallet, 5*CENT); // now we have 5+6+7+8+20+30 = 75 cents total
415
416 // now if we try making 16 cents again, the smaller coins can make 5+6+7 = 18 cents, better than the next biggest coin, 20
417 BOOST_CHECK(KnapsackSolver(16 * CENT, KnapsackGroupOutputs(coins, *wallet, filter_confirmed), setCoinsRet, nValueRet));
418 BOOST_CHECK_EQUAL(nValueRet, 18 * CENT); // we should get 18 in 3 coins
419 BOOST_CHECK_EQUAL(setCoinsRet.size(), 3U);
420
421 add_coin(coins, *wallet, 18*CENT); // now we have 5+6+7+8+18+20+30
422
423 // and now if we try making 16 cents again, the smaller coins can make 5+6+7 = 18 cents, the same as the next biggest coin, 18
424 BOOST_CHECK(KnapsackSolver(16 * CENT, KnapsackGroupOutputs(coins, *wallet, filter_confirmed), setCoinsRet, nValueRet));
425 BOOST_CHECK_EQUAL(nValueRet, 18 * CENT); // we should get 18 in 1 coin
426 BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U); // because in the event of a tie, the biggest coin wins
427
428 // now try making 11 cents. we should get 5+6
429 BOOST_CHECK(KnapsackSolver(11 * CENT, KnapsackGroupOutputs(coins, *wallet, filter_confirmed), setCoinsRet, nValueRet));
430 BOOST_CHECK_EQUAL(nValueRet, 11 * CENT);
431 BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U);
432
433 // check that the smallest bigger coin is used
434 add_coin(coins, *wallet, 1*COIN);
435 add_coin(coins, *wallet, 2*COIN);
436 add_coin(coins, *wallet, 3*COIN);
437 add_coin(coins, *wallet, 4*COIN); // now we have 5+6+7+8+18+20+30+100+200+300+400 = 1094 cents
438 BOOST_CHECK(KnapsackSolver(95 * CENT, KnapsackGroupOutputs(coins, *wallet, filter_confirmed), setCoinsRet, nValueRet));
439 BOOST_CHECK_EQUAL(nValueRet, 1 * COIN); // we should get 1 BTC in 1 coin
440 BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
441
442 BOOST_CHECK(KnapsackSolver(195 * CENT, KnapsackGroupOutputs(coins, *wallet, filter_confirmed), setCoinsRet, nValueRet));
443 BOOST_CHECK_EQUAL(nValueRet, 2 * COIN); // we should get 2 BTC in 1 coin
444 BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
445
446 // empty the wallet and start again, now with fractions of a cent, to test small change avoidance
447
448 coins.clear();
449 add_coin(coins, *wallet, MIN_CHANGE * 1 / 10);
450 add_coin(coins, *wallet, MIN_CHANGE * 2 / 10);
451 add_coin(coins, *wallet, MIN_CHANGE * 3 / 10);
452 add_coin(coins, *wallet, MIN_CHANGE * 4 / 10);
453 add_coin(coins, *wallet, MIN_CHANGE * 5 / 10);
454
455 // try making 1 * MIN_CHANGE from the 1.5 * MIN_CHANGE
456 // we'll get change smaller than MIN_CHANGE whatever happens, so can expect MIN_CHANGE exactly
458 BOOST_CHECK_EQUAL(nValueRet, MIN_CHANGE);
459
460 // but if we add a bigger coin, small change is avoided
461 add_coin(coins, *wallet, 1111*MIN_CHANGE);
462
463 // try making 1 from 0.1 + 0.2 + 0.3 + 0.4 + 0.5 + 1111 = 1112.5
464 BOOST_CHECK(KnapsackSolver(1 * MIN_CHANGE, KnapsackGroupOutputs(coins, *wallet, filter_confirmed), setCoinsRet, nValueRet));
465 BOOST_CHECK_EQUAL(nValueRet, 1 * MIN_CHANGE); // we should get the exact amount
466
467 // if we add more small coins:
468 add_coin(coins, *wallet, MIN_CHANGE * 6 / 10);
469 add_coin(coins, *wallet, MIN_CHANGE * 7 / 10);
470
471 // and try again to make 1.0 * MIN_CHANGE
472 BOOST_CHECK(KnapsackSolver(1 * MIN_CHANGE, KnapsackGroupOutputs(coins, *wallet, filter_confirmed), setCoinsRet, nValueRet));
473 BOOST_CHECK_EQUAL(nValueRet, 1 * MIN_CHANGE); // we should get the exact amount
474
475 // run the 'mtgox' test (see https://blockexplorer.com/tx/29a3efd3ef04f9153d47a990bd7b048a4b2d213daaa5fb8ed670fb85f13bdbcf)
476 // they tried to consolidate 10 50k coins into one 500k coin, and ended up with 50k in change
477 coins.clear();
478 for (int j = 0; j < 20; j++)
479 add_coin(coins, *wallet, 50000 * COIN);
480
481 BOOST_CHECK(KnapsackSolver(500000 * COIN, KnapsackGroupOutputs(coins, *wallet, filter_confirmed), setCoinsRet, nValueRet));
482 BOOST_CHECK_EQUAL(nValueRet, 500000 * COIN); // we should get the exact amount
483 BOOST_CHECK_EQUAL(setCoinsRet.size(), 10U); // in ten coins
484
485 // if there's not enough in the smaller coins to make at least 1 * MIN_CHANGE change (0.5+0.6+0.7 < 1.0+1.0),
486 // we need to try finding an exact subset anyway
487
488 // sometimes it will fail, and so we use the next biggest coin:
489 coins.clear();
490 add_coin(coins, *wallet, MIN_CHANGE * 5 / 10);
491 add_coin(coins, *wallet, MIN_CHANGE * 6 / 10);
492 add_coin(coins, *wallet, MIN_CHANGE * 7 / 10);
493 add_coin(coins, *wallet, 1111 * MIN_CHANGE);
494 BOOST_CHECK(KnapsackSolver(1 * MIN_CHANGE, KnapsackGroupOutputs(coins, *wallet, filter_confirmed), setCoinsRet, nValueRet));
495 BOOST_CHECK_EQUAL(nValueRet, 1111 * MIN_CHANGE); // we get the bigger coin
496 BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
497
498 // but sometimes it's possible, and we use an exact subset (0.4 + 0.6 = 1.0)
499 coins.clear();
500 add_coin(coins, *wallet, MIN_CHANGE * 4 / 10);
501 add_coin(coins, *wallet, MIN_CHANGE * 6 / 10);
502 add_coin(coins, *wallet, MIN_CHANGE * 8 / 10);
503 add_coin(coins, *wallet, 1111 * MIN_CHANGE);
505 BOOST_CHECK_EQUAL(nValueRet, MIN_CHANGE); // we should get the exact amount
506 BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U); // in two coins 0.4+0.6
507
508 // test avoiding small change
509 coins.clear();
510 add_coin(coins, *wallet, MIN_CHANGE * 5 / 100);
511 add_coin(coins, *wallet, MIN_CHANGE * 1);
512 add_coin(coins, *wallet, MIN_CHANGE * 100);
513
514 // trying to make 100.01 from these three coins
515 BOOST_CHECK(KnapsackSolver(MIN_CHANGE * 10001 / 100, KnapsackGroupOutputs(coins, *wallet, filter_confirmed), setCoinsRet, nValueRet));
516 BOOST_CHECK_EQUAL(nValueRet, MIN_CHANGE * 10105 / 100); // we should get all coins
517 BOOST_CHECK_EQUAL(setCoinsRet.size(), 3U);
518
519 // but if we try to make 99.9, we should take the bigger of the two small coins to avoid small change
520 BOOST_CHECK(KnapsackSolver(MIN_CHANGE * 9990 / 100, KnapsackGroupOutputs(coins, *wallet, filter_confirmed), setCoinsRet, nValueRet));
521 BOOST_CHECK_EQUAL(nValueRet, 101 * MIN_CHANGE);
522 BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U);
523 }
524
525 // test with many inputs
526 for (CAmount amt=1500; amt < COIN; amt*=10) {
527 coins.clear();
528 // Create 676 inputs (= (old MAX_STANDARD_TX_SIZE == 100000) / 148 bytes per input)
529 for (uint16_t j = 0; j < 676; j++)
530 add_coin(coins, *wallet, amt);
531
532 // We only create the wallet once to save time, but we still run the coin selection RUN_TESTS times.
533 for (int i = 0; i < RUN_TESTS; i++) {
534 BOOST_CHECK(KnapsackSolver(2000, KnapsackGroupOutputs(coins, *wallet, filter_confirmed), setCoinsRet, nValueRet));
535
536 if (amt - 2000 < MIN_CHANGE) {
537 // needs more than one input:
538 uint16_t returnSize = std::ceil((2000.0 + MIN_CHANGE)/amt);
539 CAmount returnValue = amt * returnSize;
540 BOOST_CHECK_EQUAL(nValueRet, returnValue);
541 BOOST_CHECK_EQUAL(setCoinsRet.size(), returnSize);
542 } else {
543 // one input is sufficient:
544 BOOST_CHECK_EQUAL(nValueRet, amt);
545 BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
546 }
547 }
548 }
549
550 // test randomness
551 {
552 coins.clear();
553 for (int i2 = 0; i2 < 100; i2++)
554 add_coin(coins, *wallet, COIN);
555
556 // Again, we only create the wallet once to save time, but we still run the coin selection RUN_TESTS times.
557 for (int i = 0; i < RUN_TESTS; i++) {
558 // picking 50 from 100 coins doesn't depend on the shuffle,
559 // but does depend on randomness in the stochastic approximation code
560 BOOST_CHECK(KnapsackSolver(50 * COIN, GroupCoins(coins), setCoinsRet, nValueRet));
561 BOOST_CHECK(KnapsackSolver(50 * COIN, GroupCoins(coins), setCoinsRet2, nValueRet));
562 BOOST_CHECK(!equal_sets(setCoinsRet, setCoinsRet2));
563
564 int fails = 0;
565 for (int j = 0; j < RANDOM_REPEATS; j++)
566 {
567 // Test that the KnapsackSolver selects randomly from equivalent coins (same value and same input size).
568 // When choosing 1 from 100 identical coins, 1% of the time, this test will choose the same coin twice
569 // which will cause it to fail.
570 // To avoid that issue, run the test RANDOM_REPEATS times and only complain if all of them fail
571 BOOST_CHECK(KnapsackSolver(COIN, GroupCoins(coins), setCoinsRet, nValueRet));
572 BOOST_CHECK(KnapsackSolver(COIN, GroupCoins(coins), setCoinsRet2, nValueRet));
573 if (equal_sets(setCoinsRet, setCoinsRet2))
574 fails++;
575 }
576 BOOST_CHECK_NE(fails, RANDOM_REPEATS);
577 }
578
579 // add 75 cents in small change. not enough to make 90 cents,
580 // then try making 90 cents. there are multiple competing "smallest bigger" coins,
581 // one of which should be picked at random
582 add_coin(coins, *wallet, 5 * CENT);
583 add_coin(coins, *wallet, 10 * CENT);
584 add_coin(coins, *wallet, 15 * CENT);
585 add_coin(coins, *wallet, 20 * CENT);
586 add_coin(coins, *wallet, 25 * CENT);
587
588 for (int i = 0; i < RUN_TESTS; i++) {
589 int fails = 0;
590 for (int j = 0; j < RANDOM_REPEATS; j++)
591 {
592 BOOST_CHECK(KnapsackSolver(90*CENT, GroupCoins(coins), setCoinsRet, nValueRet));
593 BOOST_CHECK(KnapsackSolver(90*CENT, GroupCoins(coins), setCoinsRet2, nValueRet));
594 if (equal_sets(setCoinsRet, setCoinsRet2))
595 fails++;
596 }
597 BOOST_CHECK_NE(fails, RANDOM_REPEATS);
598 }
599 }
600}
601
603{
604 std::unique_ptr<CWallet> wallet = std::make_unique<CWallet>(m_node.chain.get(), "", CreateMockWalletDatabase());
605 wallet->LoadWallet();
606 LOCK(wallet->cs_wallet);
607 wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
608 wallet->SetupDescriptorScriptPubKeyMans();
609
610 CoinSet setCoinsRet;
611 CAmount nValueRet;
612 std::vector<COutput> coins;
613
614 // Test vValue sort order
615 for (int i = 0; i < 1000; i++)
616 add_coin(coins, *wallet, 1000 * COIN);
617 add_coin(coins, *wallet, 3 * COIN);
618
619 BOOST_CHECK(KnapsackSolver(1003 * COIN, KnapsackGroupOutputs(coins, *wallet, filter_standard), setCoinsRet, nValueRet));
620 BOOST_CHECK_EQUAL(nValueRet, 1003 * COIN);
621 BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U);
622}
623
624// Tests that with the ideal conditions, the coin selector will always be able to find a solution that can pay the target value
625BOOST_AUTO_TEST_CASE(SelectCoins_test)
626{
627 std::unique_ptr<CWallet> wallet = std::make_unique<CWallet>(m_node.chain.get(), "", CreateMockWalletDatabase());
628 wallet->LoadWallet();
629 LOCK(wallet->cs_wallet);
630 wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
631 wallet->SetupDescriptorScriptPubKeyMans();
632
633 // Random generator stuff
634 std::default_random_engine generator;
635 std::exponential_distribution<double> distribution (100);
637
638 // Run this test 100 times
639 for (int i = 0; i < 100; ++i)
640 {
641 std::vector<COutput> coins;
642 CAmount balance{0};
643
644 // Make a wallet with 1000 exponentially distributed random inputs
645 for (int j = 0; j < 1000; ++j)
646 {
647 CAmount val = distribution(generator)*10000000;
648 add_coin(coins, *wallet, val);
649 balance += val;
650 }
651
652 // Generate a random fee rate in the range of 100 - 400
653 CFeeRate rate(rand.randrange(300) + 100);
654
655 // Generate a random target value between 1000 and wallet balance
656 CAmount target = rand.randrange(balance - 1000) + 1000;
657
658 // Perform selection
659 CoinSelectionParams cs_params(/* change_output_size= */ 34,
660 /* change_spend_size= */ 148, /* effective_feerate= */ CFeeRate(0),
661 /* long_term_feerate= */ CFeeRate(0), /* discard_feerate= */ CFeeRate(0),
662 /* tx_noinputs_size= */ 0, /* avoid_partial= */ false);
663 CoinSet out_set;
664 CAmount out_value = 0;
665 CCoinControl cc;
666 BOOST_CHECK(SelectCoins(*wallet, coins, target, out_set, out_value, cc, cs_params));
667 BOOST_CHECK_GE(out_value, target);
668 }
669}
670
672{
673 CoinSet selection;
674 const CAmount fee{100};
675 const CAmount change_cost{125};
676 const CAmount fee_diff{40};
677 const CAmount in_amt{3 * COIN};
678 const CAmount target{2 * COIN};
679 const CAmount excess{in_amt - fee * 2 - target};
680
681 // Waste with change is the change cost and difference between fee and long term fee
682 add_coin(1 * COIN, 1, selection, fee, fee - fee_diff);
683 add_coin(2 * COIN, 2, selection, fee, fee - fee_diff);
684 const CAmount waste1 = GetSelectionWaste(selection, change_cost, target);
685 BOOST_CHECK_EQUAL(fee_diff * 2 + change_cost, waste1);
686 selection.clear();
687
688 // Waste without change is the excess and difference between fee and long term fee
689 add_coin(1 * COIN, 1, selection, fee, fee - fee_diff);
690 add_coin(2 * COIN, 2, selection, fee, fee - fee_diff);
691 const CAmount waste_nochange1 = GetSelectionWaste(selection, 0, target);
692 BOOST_CHECK_EQUAL(fee_diff * 2 + excess, waste_nochange1);
693 selection.clear();
694
695 // Waste with change and fee == long term fee is just cost of change
696 add_coin(1 * COIN, 1, selection, fee, fee);
697 add_coin(2 * COIN, 2, selection, fee, fee);
698 BOOST_CHECK_EQUAL(change_cost, GetSelectionWaste(selection, change_cost, target));
699 selection.clear();
700
701 // Waste without change and fee == long term fee is just the excess
702 add_coin(1 * COIN, 1, selection, fee, fee);
703 add_coin(2 * COIN, 2, selection, fee, fee);
704 BOOST_CHECK_EQUAL(excess, GetSelectionWaste(selection, 0, target));
705 selection.clear();
706
707 // Waste will be greater when fee is greater, but long term fee is the same
708 add_coin(1 * COIN, 1, selection, fee * 2, fee - fee_diff);
709 add_coin(2 * COIN, 2, selection, fee * 2, fee - fee_diff);
710 const CAmount waste2 = GetSelectionWaste(selection, change_cost, target);
711 BOOST_CHECK_GT(waste2, waste1);
712 selection.clear();
713
714 // Waste with change is the change cost and difference between fee and long term fee
715 // With long term fee greater than fee, waste should be less than when long term fee is less than fee
716 add_coin(1 * COIN, 1, selection, fee, fee + fee_diff);
717 add_coin(2 * COIN, 2, selection, fee, fee + fee_diff);
718 const CAmount waste3 = GetSelectionWaste(selection, change_cost, target);
719 BOOST_CHECK_EQUAL(fee_diff * -2 + change_cost, waste3);
720 BOOST_CHECK_LT(waste3, waste1);
721 selection.clear();
722
723 // Waste without change is the excess and difference between fee and long term fee
724 // With long term fee greater than fee, waste should be less than when long term fee is less than fee
725 add_coin(1 * COIN, 1, selection, fee, fee + fee_diff);
726 add_coin(2 * COIN, 2, selection, fee, fee + fee_diff);
727 const CAmount waste_nochange2 = GetSelectionWaste(selection, 0, target);
728 BOOST_CHECK_EQUAL(fee_diff * -2 + excess, waste_nochange2);
729 BOOST_CHECK_LT(waste_nochange2, waste_nochange1);
730 selection.clear();
731
732 // No Waste when fee == long_term_fee, no change, and no excess
733 add_coin(1 * COIN, 1, selection, fee, fee);
734 add_coin(2 * COIN, 2, selection, fee, fee);
735 const CAmount exact_target{in_amt - fee * 2};
736 BOOST_CHECK_EQUAL(0, GetSelectionWaste(selection, /* change_cost */ 0, exact_target));
737 selection.clear();
738
739 // No Waste when (fee - long_term_fee) == (-cost_of_change), and no excess
740 const CAmount new_change_cost{fee_diff * 2};
741 add_coin(1 * COIN, 1, selection, fee, fee + fee_diff);
742 add_coin(2 * COIN, 2, selection, fee, fee + fee_diff);
743 BOOST_CHECK_EQUAL(0, GetSelectionWaste(selection, new_change_cost, target));
744 selection.clear();
745
746 // No Waste when (fee - long_term_fee) == (-excess), no change cost
747 const CAmount new_target{in_amt - fee * 2 - fee_diff * 2};
748 add_coin(1 * COIN, 1, selection, fee, fee + fee_diff);
749 add_coin(2 * COIN, 2, selection, fee, fee + fee_diff);
750 BOOST_CHECK_EQUAL(0, GetSelectionWaste(selection, /* change cost */ 0, new_target));
751}
752
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition: amount.h:15
NodeContext m_node
Definition: bitcoin-gui.cpp:36
Coin Control Features.
Definition: coincontrol.h:29
void Select(const COutPoint &output)
Definition: coincontrol.h:91
bool fAllowOtherInputs
If false, allows unselected inputs, but requires all selected inputs be used.
Definition: coincontrol.h:40
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:30
A UTXO under consideration for use in funding a new transaction.
Definition: coinselection.h:21
CAmount m_fee
Definition: coinselection.h:55
CAmount m_long_term_fee
Definition: coinselection.h:56
CAmount effective_value
Definition: coinselection.h:54
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:27
Definition: spend.h:17
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:229
A transaction with a bunch of additional info that only the owner cares about.
Definition: transaction.h:47
CachableAmount m_amounts[AMOUNTTYPE_ENUM_ELEMENTS]
Definition: transaction.h:105
bool m_is_cache_empty
This flag is true if all m_amounts caches are empty.
Definition: transaction.h:112
Fast randomness source.
Definition: random.h:120
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
Definition: random.h:190
256-bit opaque blob.
Definition: uint256.h:124
std::set< CInputCoin > CoinSet
bool KnapsackSolver(const CAmount &nTargetValue, std::vector< OutputGroup > &groups, std::set< CInputCoin > &setCoinsRet, CAmount &nValueRet)
bool SelectCoinsBnB(std::vector< OutputGroup > &utxo_pool, const CAmount &selection_target, const CAmount &cost_of_change, std::set< CInputCoin > &out_set, CAmount &value_ret)
static void ApproximateBestSubset(const std::vector< OutputGroup > &groups, const CAmount &nTotalLower, const CAmount &nTargetValue, std::vector< char > &vfBest, CAmount &nBest, int iterations=1000)
CAmount GetSelectionWaste(const std::set< CInputCoin > &inputs, CAmount change_cost, CAmount target, bool use_effective_value)
Compute the waste for this result given the cost of change and the opportunity cost of spending these...
static constexpr CAmount MIN_CHANGE
target minimum change amount
Definition: coinselection.h:16
static const CoinEligibilityFilter filter_standard(1, 6, 0)
std::vector< OutputGroup > & KnapsackGroupOutputs(const std::vector< COutput > &coins, CWallet &wallet, const CoinEligibilityFilter &filter)
static void add_coin(const CAmount &nValue, int nInput, std::vector< CInputCoin > &set)
static CAmount make_hard_case(int utxos, std::vector< CInputCoin > &utxo_pool)
BOOST_AUTO_TEST_CASE(bnb_search_test)
static const CoinEligibilityFilter filter_confirmed(1, 1, 0)
static bool equal_sets(CoinSet a, CoinSet b)
std::set< CInputCoin > CoinSet
std::vector< OutputGroup > & GroupCoins(const std::vector< CInputCoin > &coins)
static const CoinEligibilityFilter filter_standard_extra(6, 6, 0)
#define RANDOM_REPEATS
#define RUN_TESTS
BOOST_AUTO_TEST_SUITE_END()
@ ISMINE_SPENDABLE
Definition: ismine.h:42
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:387
static constexpr CAmount CENT
Definition: setup_common.h:71
std::vector< OutputGroup > GroupOutputs(const CWallet &wallet, const std::vector< COutput > &outputs, const CoinSelectionParams &coin_sel_params, const CoinEligibilityFilter &filter, bool positive_only)
Definition: spend.cpp:290
bool SelectCoins(const CWallet &wallet, const std::vector< COutput > &vAvailableCoins, const CAmount &nTargetValue, std::set< CInputCoin > &setCoinsRet, CAmount &nValueRet, const CCoinControl &coin_control, CoinSelectionParams &coin_selection_params)
Select a set of coins such that nValueRet >= nTargetValue and at least all coins from coin_control ar...
Definition: spend.cpp:430
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:310
std::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:157
A mutable version of CTransaction.
Definition: transaction.h:345
uint256 GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:63
std::vector< CTxOut > vout
Definition: transaction.h:347
std::vector< CTxIn > vin
Definition: transaction.h:346
void Set(isminefilter filter, CAmount value)
Definition: ismine.h:63
Parameters for filtering which OutputGroups we may use in coin selection.
Parameters for one iteration of Coin Selection.
Definition: coinselection.h:76
bool m_subtract_fee_outputs
Indicate that we are subtracting the fee from outputs.
Definition: coinselection.h:96
CAmount m_cost_of_change
Cost of creating the change output + cost of spending the change output in the future.
Definition: coinselection.h:84
CFeeRate m_effective_feerate
The targeted feerate of the transaction being built.
Definition: coinselection.h:86
std::unique_ptr< interfaces::Chain > chain
Definition: context.h:50
Testing setup and teardown for wallet.
Bilingual messages:
Definition: translation.h:16
#define LOCK(cs)
Definition: sync.h:226
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
assert(!tx.IsCoinBase())
std::unique_ptr< WalletDatabase > CreateMockWalletDatabase()
Return object for accessing temporary in-memory database.
Definition: walletdb.cpp:1189
@ WALLET_FLAG_DESCRIPTORS
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition: walletutil.h:65