LCOV - code coverage report
Current view: top level - src/wallet/test - coinselector_tests.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 0 844 0.0 %
Date: 2023-10-05 12:38:51 Functions: 0 96 0.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2017-2022 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>
       7                 :            : #include <policy/policy.h>
       8                 :            : #include <primitives/transaction.h>
       9                 :            : #include <random.h>
      10                 :            : #include <test/util/setup_common.h>
      11                 :            : #include <util/translation.h>
      12                 :            : #include <wallet/coincontrol.h>
      13                 :            : #include <wallet/coinselection.h>
      14                 :            : #include <wallet/spend.h>
      15                 :            : #include <wallet/test/util.h>
      16                 :            : #include <wallet/test/wallet_test_fixture.h>
      17                 :          0 : #include <wallet/wallet.h>
      18                 :          0 : 
      19                 :            : #include <algorithm>
      20                 :            : #include <boost/test/unit_test.hpp>
      21                 :            : #include <random>
      22                 :            : 
      23                 :            : namespace wallet {
      24                 :          0 : BOOST_FIXTURE_TEST_SUITE(coinselector_tests, WalletTestingSetup)
      25                 :            : 
      26                 :            : // how many times to run all the tests to have a chance to catch errors that only show up with particular random shuffles
      27                 :          0 : #define RUN_TESTS 100
      28                 :            : 
      29                 :            : // some tests fail 1% of the time due to bad luck.
      30                 :            : // we repeat those tests this many times and only complain if all iterations of the test fail
      31                 :            : #define RANDOM_REPEATS 5
      32                 :            : 
      33                 :            : typedef std::set<std::shared_ptr<COutput>> CoinSet;
      34                 :            : 
      35                 :          0 : static const CoinEligibilityFilter filter_standard(1, 6, 0);
      36                 :          0 : static const CoinEligibilityFilter filter_confirmed(1, 1, 0);
      37                 :          0 : static const CoinEligibilityFilter filter_standard_extra(6, 6, 0);
      38                 :            : static int nextLockTime = 0;
      39                 :            : 
      40                 :          0 : static void add_coin(const CAmount& nValue, int nInput, std::vector<COutput>& set)
      41                 :            : {
      42                 :          0 :     CMutableTransaction tx;
      43                 :          0 :     tx.vout.resize(nInput + 1);
      44                 :          0 :     tx.vout[nInput].nValue = nValue;
      45                 :          0 :     tx.nLockTime = nextLockTime++;        // so all transactions get different hashes
      46                 :          0 :     set.emplace_back(COutPoint(tx.GetHash(), nInput), tx.vout.at(nInput), /*depth=*/ 1, /*input_bytes=*/ -1, /*spendable=*/ true, /*solvable=*/ true, /*safe=*/ true, /*time=*/ 0, /*from_me=*/ false, /*fees=*/ 0);
      47                 :          0 : }
      48                 :            : 
      49                 :          0 : static void add_coin(const CAmount& nValue, int nInput, SelectionResult& result)
      50                 :            : {
      51                 :          0 :     CMutableTransaction tx;
      52                 :          0 :     tx.vout.resize(nInput + 1);
      53                 :          0 :     tx.vout[nInput].nValue = nValue;
      54                 :          0 :     tx.nLockTime = nextLockTime++;        // so all transactions get different hashes
      55                 :          0 :     COutput output(COutPoint(tx.GetHash(), nInput), tx.vout.at(nInput), /*depth=*/ 1, /*input_bytes=*/ -1, /*spendable=*/ true, /*solvable=*/ true, /*safe=*/ true, /*time=*/ 0, /*from_me=*/ false, /*fees=*/ 0);
      56                 :          0 :     OutputGroup group;
      57                 :          0 :     group.Insert(std::make_shared<COutput>(output), /*ancestors=*/ 0, /*descendants=*/ 0);
      58                 :          0 :     result.AddInput(group);
      59                 :          0 : }
      60                 :            : 
      61                 :          0 : static void add_coin(const CAmount& nValue, int nInput, SelectionResult& result, CAmount fee, CAmount long_term_fee)
      62                 :            : {
      63                 :          0 :     CMutableTransaction tx;
      64                 :          0 :     tx.vout.resize(nInput + 1);
      65                 :          0 :     tx.vout[nInput].nValue = nValue;
      66                 :          0 :     tx.nLockTime = nextLockTime++;        // so all transactions get different hashes
      67                 :          0 :     std::shared_ptr<COutput> coin = std::make_shared<COutput>(COutPoint(tx.GetHash(), nInput), tx.vout.at(nInput), /*depth=*/ 1, /*input_bytes=*/ 148, /*spendable=*/ true, /*solvable=*/ true, /*safe=*/ true, /*time=*/ 0, /*from_me=*/ false, fee);
      68                 :          0 :     OutputGroup group;
      69                 :          0 :     group.Insert(coin, /*ancestors=*/ 0, /*descendants=*/ 0);
      70                 :          0 :     coin->long_term_fee = long_term_fee; // group.Insert() will modify long_term_fee, so we need to set it afterwards
      71                 :          0 :     result.AddInput(group);
      72                 :          0 : }
      73                 :            : 
      74                 :          0 : static void add_coin(CoinsResult& available_coins, CWallet& wallet, const CAmount& nValue, CFeeRate feerate = CFeeRate(0), int nAge = 6*24, bool fIsFromMe = false, int nInput =0, bool spendable = false, int custom_size = 0)
      75                 :            : {
      76                 :          0 :     CMutableTransaction tx;
      77                 :          0 :     tx.nLockTime = nextLockTime++;        // so all transactions get different hashes
      78                 :          0 :     tx.vout.resize(nInput + 1);
      79                 :          0 :     tx.vout[nInput].nValue = nValue;
      80                 :          0 :     if (spendable) {
      81                 :          0 :         tx.vout[nInput].scriptPubKey = GetScriptForDestination(*Assert(wallet.GetNewDestination(OutputType::BECH32, "")));
      82                 :          0 :     }
      83                 :          0 :     uint256 txid = tx.GetHash();
      84                 :            : 
      85                 :          0 :     LOCK(wallet.cs_wallet);
      86                 :          0 :     auto ret = wallet.mapWallet.emplace(std::piecewise_construct, std::forward_as_tuple(txid), std::forward_as_tuple(MakeTransactionRef(std::move(tx)), TxStateInactive{}));
      87                 :          0 :     assert(ret.second);
      88                 :          0 :     CWalletTx& wtx = (*ret.first).second;
      89                 :          0 :     const auto& txout = wtx.tx->vout.at(nInput);
      90                 :          0 :     available_coins.Add(OutputType::BECH32, {COutPoint(wtx.GetHash(), nInput), txout, nAge, custom_size == 0 ? CalculateMaximumSignedInputSize(txout, &wallet, /*coin_control=*/nullptr) : custom_size, /*spendable=*/ true, /*solvable=*/ true, /*safe=*/ true, wtx.GetTxTime(), fIsFromMe, feerate});
      91                 :          0 : }
      92                 :            : 
      93                 :            : // Helpers
      94                 :          0 : std::optional<SelectionResult> KnapsackSolver(std::vector<OutputGroup>& groups, const CAmount& nTargetValue,
      95                 :            :                                               CAmount change_target, FastRandomContext& rng)
      96                 :            : {
      97                 :          0 :     auto res{KnapsackSolver(groups, nTargetValue, change_target, rng, MAX_STANDARD_TX_WEIGHT)};
      98                 :          0 :     return res ? std::optional<SelectionResult>(*res) : std::nullopt;
      99                 :          0 : }
     100                 :            : 
     101                 :          0 : std::optional<SelectionResult> SelectCoinsBnB(std::vector<OutputGroup>& utxo_pool, const CAmount& selection_target, const CAmount& cost_of_change)
     102                 :            : {
     103                 :          0 :     auto res{SelectCoinsBnB(utxo_pool, selection_target, cost_of_change, MAX_STANDARD_TX_WEIGHT)};
     104                 :          0 :     return res ? std::optional<SelectionResult>(*res) : std::nullopt;
     105                 :          0 : }
     106                 :            : 
     107                 :            : /** Check if SelectionResult a is equivalent to SelectionResult b.
     108                 :            :  * Equivalent means same input values, but maybe different inputs (i.e. same value, different prevout) */
     109                 :          0 : static bool EquivalentResult(const SelectionResult& a, const SelectionResult& b)
     110                 :            : {
     111                 :          0 :     std::vector<CAmount> a_amts;
     112                 :          0 :     std::vector<CAmount> b_amts;
     113                 :          0 :     for (const auto& coin : a.GetInputSet()) {
     114                 :          0 :         a_amts.push_back(coin->txout.nValue);
     115                 :            :     }
     116                 :          0 :     for (const auto& coin : b.GetInputSet()) {
     117                 :          0 :         b_amts.push_back(coin->txout.nValue);
     118                 :            :     }
     119                 :          0 :     std::sort(a_amts.begin(), a_amts.end());
     120                 :          0 :     std::sort(b_amts.begin(), b_amts.end());
     121                 :            : 
     122                 :          0 :     std::pair<std::vector<CAmount>::iterator, std::vector<CAmount>::iterator> ret = std::mismatch(a_amts.begin(), a_amts.end(), b_amts.begin());
     123                 :          0 :     return ret.first == a_amts.end() && ret.second == b_amts.end();
     124                 :          0 : }
     125                 :            : 
     126                 :            : /** Check if this selection is equal to another one. Equal means same inputs (i.e same value and prevout) */
     127                 :          0 : static bool EqualResult(const SelectionResult& a, const SelectionResult& b)
     128                 :            : {
     129                 :          0 :     std::pair<CoinSet::iterator, CoinSet::iterator> ret = std::mismatch(a.GetInputSet().begin(), a.GetInputSet().end(), b.GetInputSet().begin(),
     130                 :          0 :         [](const std::shared_ptr<COutput>& a, const std::shared_ptr<COutput>& b) {
     131                 :          0 :             return a->outpoint == b->outpoint;
     132                 :            :         });
     133                 :          0 :     return ret.first == a.GetInputSet().end() && ret.second == b.GetInputSet().end();
     134                 :            : }
     135                 :            : 
     136                 :          0 : static CAmount make_hard_case(int utxos, std::vector<COutput>& utxo_pool)
     137                 :            : {
     138                 :          0 :     utxo_pool.clear();
     139                 :          0 :     CAmount target = 0;
     140                 :          0 :     for (int i = 0; i < utxos; ++i) {
     141                 :          0 :         target += CAmount{1} << (utxos+i);
     142                 :          0 :         add_coin(CAmount{1} << (utxos+i), 2*i, utxo_pool);
     143                 :          0 :         add_coin((CAmount{1} << (utxos+i)) + (CAmount{1} << (utxos-1-i)), 2*i + 1, utxo_pool);
     144                 :          0 :     }
     145                 :          0 :     return target;
     146                 :            : }
     147                 :            : 
     148                 :          0 : inline std::vector<OutputGroup>& GroupCoins(const std::vector<COutput>& available_coins, bool subtract_fee_outputs = false)
     149                 :            : {
     150                 :          0 :     static std::vector<OutputGroup> static_groups;
     151                 :          0 :     static_groups.clear();
     152                 :          0 :     for (auto& coin : available_coins) {
     153                 :          0 :         static_groups.emplace_back();
     154                 :          0 :         OutputGroup& group = static_groups.back();
     155                 :          0 :         group.Insert(std::make_shared<COutput>(coin), /*ancestors=*/ 0, /*descendants=*/ 0);
     156                 :          0 :         group.m_subtract_fee_outputs = subtract_fee_outputs;
     157                 :            :     }
     158                 :          0 :     return static_groups;
     159                 :          0 : }
     160                 :            : 
     161                 :          0 : inline std::vector<OutputGroup>& KnapsackGroupOutputs(const CoinsResult& available_coins, CWallet& wallet, const CoinEligibilityFilter& filter)
     162                 :            : {
     163                 :          0 :     FastRandomContext rand{};
     164                 :          0 :     CoinSelectionParams coin_selection_params{
     165                 :          0 :         rand,
     166                 :          0 :         /*change_output_size=*/ 0,
     167                 :          0 :         /*change_spend_size=*/ 0,
     168                 :          0 :         /*min_change_target=*/ CENT,
     169                 :          0 :         /*effective_feerate=*/ CFeeRate(0),
     170                 :          0 :         /*long_term_feerate=*/ CFeeRate(0),
     171                 :          0 :         /*discard_feerate=*/ CFeeRate(0),
     172                 :            :         /*tx_noinputs_size=*/ 0,
     173                 :            :         /*avoid_partial=*/ false,
     174                 :            :     };
     175                 :          0 :     static OutputGroupTypeMap static_groups;
     176                 :          0 :     static_groups = GroupOutputs(wallet, available_coins, coin_selection_params, {{filter}})[filter];
     177                 :            :     return static_groups.all_groups.mixed_group;
     178                 :          0 : }
     179                 :            : 
     180                 :          0 : static std::unique_ptr<CWallet> NewWallet(const node::NodeContext& m_node, const std::string& wallet_name = "")
     181                 :            : {
     182                 :          0 :     std::unique_ptr<CWallet> wallet = std::make_unique<CWallet>(m_node.chain.get(), wallet_name, CreateMockableWalletDatabase());
     183                 :          0 :     BOOST_CHECK(wallet->LoadWallet() == DBErrors::LOAD_OK);
     184                 :          0 :     LOCK(wallet->cs_wallet);
     185                 :          0 :     wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
     186                 :          0 :     wallet->SetupDescriptorScriptPubKeyMans();
     187                 :          0 :     return wallet;
     188                 :          0 : }
     189                 :            : 
     190                 :            : // Branch and bound coin selection tests
     191                 :          0 : BOOST_AUTO_TEST_CASE(bnb_search_test)
     192                 :            : {
     193                 :          0 :     FastRandomContext rand{};
     194                 :            :     // Setup
     195                 :          0 :     std::vector<COutput> utxo_pool;
     196                 :          0 :     SelectionResult expected_result(CAmount(0), SelectionAlgorithm::BNB);
     197                 :            : 
     198                 :            :     /////////////////////////
     199                 :            :     // Known Outcome tests //
     200                 :            :     /////////////////////////
     201                 :            : 
     202                 :            :     // Empty utxo pool
     203                 :          0 :     BOOST_CHECK(!SelectCoinsBnB(GroupCoins(utxo_pool), 1 * CENT, 0.5 * CENT));
     204                 :            : 
     205                 :            :     // Add utxos
     206                 :          0 :     add_coin(1 * CENT, 1, utxo_pool);
     207                 :          0 :     add_coin(2 * CENT, 2, utxo_pool);
     208                 :          0 :     add_coin(3 * CENT, 3, utxo_pool);
     209                 :          0 :     add_coin(4 * CENT, 4, utxo_pool);
     210                 :            : 
     211                 :            :     // Select 1 Cent
     212                 :          0 :     add_coin(1 * CENT, 1, expected_result);
     213                 :          0 :     const auto result1 = SelectCoinsBnB(GroupCoins(utxo_pool), 1 * CENT, 0.5 * CENT);
     214                 :          0 :     BOOST_CHECK(result1);
     215                 :          0 :     BOOST_CHECK(EquivalentResult(expected_result, *result1));
     216                 :          0 :     BOOST_CHECK_EQUAL(result1->GetSelectedValue(), 1 * CENT);
     217                 :          0 :     expected_result.Clear();
     218                 :            : 
     219                 :            :     // Select 2 Cent
     220                 :          0 :     add_coin(2 * CENT, 2, expected_result);
     221                 :          0 :     const auto result2 = SelectCoinsBnB(GroupCoins(utxo_pool), 2 * CENT, 0.5 * CENT);
     222                 :          0 :     BOOST_CHECK(result2);
     223                 :          0 :     BOOST_CHECK(EquivalentResult(expected_result, *result2));
     224                 :          0 :     BOOST_CHECK_EQUAL(result2->GetSelectedValue(), 2 * CENT);
     225                 :          0 :     expected_result.Clear();
     226                 :            : 
     227                 :          0 :     // Select 5 Cent
     228                 :          0 :     add_coin(3 * CENT, 3, expected_result);
     229                 :          0 :     add_coin(2 * CENT, 2, expected_result);
     230                 :          0 :     const auto result3 = SelectCoinsBnB(GroupCoins(utxo_pool), 5 * CENT, 0.5 * CENT);
     231                 :          0 :     BOOST_CHECK(result3);
     232                 :          0 :     BOOST_CHECK(EquivalentResult(expected_result, *result3));
     233                 :          0 :     BOOST_CHECK_EQUAL(result3->GetSelectedValue(), 5 * CENT);
     234                 :          0 :     expected_result.Clear();
     235                 :            : 
     236                 :            :     // Select 11 Cent, not possible
     237                 :          0 :     BOOST_CHECK(!SelectCoinsBnB(GroupCoins(utxo_pool), 11 * CENT, 0.5 * CENT));
     238                 :          0 :     expected_result.Clear();
     239                 :            : 
     240                 :            :     // Cost of change is greater than the difference between target value and utxo sum
     241                 :          0 :     add_coin(1 * CENT, 1, expected_result);
     242                 :          0 :     const auto result4 = SelectCoinsBnB(GroupCoins(utxo_pool), 0.9 * CENT, 0.5 * CENT);
     243                 :          0 :     BOOST_CHECK(result4);
     244                 :          0 :     BOOST_CHECK_EQUAL(result4->GetSelectedValue(), 1 * CENT);
     245                 :          0 :     BOOST_CHECK(EquivalentResult(expected_result, *result4));
     246                 :          0 :     expected_result.Clear();
     247                 :            : 
     248                 :            :     // Cost of change is less than the difference between target value and utxo sum
     249                 :          0 :     BOOST_CHECK(!SelectCoinsBnB(GroupCoins(utxo_pool), 0.9 * CENT, 0));
     250                 :          0 :     expected_result.Clear();
     251                 :            : 
     252                 :            :     // Select 10 Cent
     253                 :          0 :     add_coin(5 * CENT, 5, utxo_pool);
     254                 :          0 :     add_coin(4 * CENT, 4, expected_result);
     255                 :          0 :     add_coin(3 * CENT, 3, expected_result);
     256                 :          0 :     add_coin(2 * CENT, 2, expected_result);
     257                 :          0 :     add_coin(1 * CENT, 1, expected_result);
     258                 :          0 :     const auto result5 = SelectCoinsBnB(GroupCoins(utxo_pool), 10 * CENT, 0.5 * CENT);
     259                 :          0 :     BOOST_CHECK(result5);
     260                 :          0 :     BOOST_CHECK(EquivalentResult(expected_result, *result5));
     261                 :          0 :     BOOST_CHECK_EQUAL(result5->GetSelectedValue(), 10 * CENT);
     262                 :          0 :     expected_result.Clear();
     263                 :            : 
     264                 :          0 :     // Select 0.25 Cent, not possible
     265                 :          0 :     BOOST_CHECK(!SelectCoinsBnB(GroupCoins(utxo_pool), 0.25 * CENT, 0.5 * CENT));
     266                 :          0 :     expected_result.Clear();
     267                 :            : 
     268                 :            :     // Iteration exhaustion test
     269                 :          0 :     CAmount target = make_hard_case(17, utxo_pool);
     270                 :          0 :     BOOST_CHECK(!SelectCoinsBnB(GroupCoins(utxo_pool), target, 1)); // Should exhaust
     271                 :          0 :     target = make_hard_case(14, utxo_pool);
     272                 :          0 :     const auto result7 = SelectCoinsBnB(GroupCoins(utxo_pool), target, 1); // Should not exhaust
     273                 :          0 :     BOOST_CHECK(result7);
     274                 :            : 
     275                 :            :     // Test same value early bailout optimization
     276                 :          0 :     utxo_pool.clear();
     277                 :          0 :     add_coin(7 * CENT, 7, expected_result);
     278                 :          0 :     add_coin(7 * CENT, 7, expected_result);
     279                 :          0 :     add_coin(7 * CENT, 7, expected_result);
     280                 :          0 :     add_coin(7 * CENT, 7, expected_result);
     281                 :          0 :     add_coin(2 * CENT, 7, expected_result);
     282                 :          0 :     add_coin(7 * CENT, 7, utxo_pool);
     283                 :          0 :     add_coin(7 * CENT, 7, utxo_pool);
     284                 :          0 :     add_coin(7 * CENT, 7, utxo_pool);
     285                 :          0 :     add_coin(7 * CENT, 7, utxo_pool);
     286                 :          0 :     add_coin(2 * CENT, 7, utxo_pool);
     287                 :          0 :     for (int i = 0; i < 50000; ++i) {
     288                 :          0 :         add_coin(5 * CENT, 7, utxo_pool);
     289                 :          0 :     }
     290                 :          0 :     const auto result8 = SelectCoinsBnB(GroupCoins(utxo_pool), 30 * CENT, 5000);
     291                 :          0 :     BOOST_CHECK(result8);
     292                 :          0 :     BOOST_CHECK_EQUAL(result8->GetSelectedValue(), 30 * CENT);
     293                 :          0 :     BOOST_CHECK(EquivalentResult(expected_result, *result8));
     294                 :            : 
     295                 :            :     ////////////////////
     296                 :            :     // Behavior tests //
     297                 :            :     ////////////////////
     298                 :            :     // Select 1 Cent with pool of only greater than 5 Cent
     299                 :          0 :     utxo_pool.clear();
     300                 :          0 :     for (int i = 5; i <= 20; ++i) {
     301                 :          0 :         add_coin(i * CENT, i, utxo_pool);
     302                 :          0 :     }
     303                 :            :     // Run 100 times, to make sure it is never finding a solution
     304                 :          0 :     for (int i = 0; i < 100; ++i) {
     305                 :          0 :         BOOST_CHECK(!SelectCoinsBnB(GroupCoins(utxo_pool), 1 * CENT, 2 * CENT));
     306                 :          0 :     }
     307                 :            : 
     308                 :            :     // Make sure that effective value is working in AttemptSelection when BnB is used
     309                 :          0 :     CoinSelectionParams coin_selection_params_bnb{
     310                 :            :         rand,
     311                 :            :         /*change_output_size=*/ 31,
     312                 :            :         /*change_spend_size=*/ 68,
     313                 :            :         /*min_change_target=*/ 0,
     314                 :          0 :         /*effective_feerate=*/ CFeeRate(3000),
     315                 :          0 :         /*long_term_feerate=*/ CFeeRate(1000),
     316                 :          0 :         /*discard_feerate=*/ CFeeRate(1000),
     317                 :            :         /*tx_noinputs_size=*/ 0,
     318                 :            :         /*avoid_partial=*/ false,
     319                 :            :     };
     320                 :          0 :     coin_selection_params_bnb.m_change_fee = coin_selection_params_bnb.m_effective_feerate.GetFee(coin_selection_params_bnb.change_output_size);
     321                 :          0 :     coin_selection_params_bnb.m_cost_of_change = coin_selection_params_bnb.m_effective_feerate.GetFee(coin_selection_params_bnb.change_spend_size) + coin_selection_params_bnb.m_change_fee;
     322                 :          0 :     coin_selection_params_bnb.min_viable_change = coin_selection_params_bnb.m_effective_feerate.GetFee(coin_selection_params_bnb.change_spend_size);
     323                 :          0 :     coin_selection_params_bnb.m_subtract_fee_outputs = true;
     324                 :            : 
     325                 :            :     {
     326                 :          0 :         std::unique_ptr<CWallet> wallet = NewWallet(m_node);
     327                 :            : 
     328                 :          0 :         CoinsResult available_coins;
     329                 :            : 
     330                 :          0 :         add_coin(available_coins, *wallet, 1, coin_selection_params_bnb.m_effective_feerate);
     331                 :          0 :         available_coins.All().at(0).input_bytes = 40; // Make sure that it has a negative effective value. The next check should assert if this somehow got through. Otherwise it will fail
     332                 :          0 :         BOOST_CHECK(!SelectCoinsBnB(GroupCoins(available_coins.All()), 1 * CENT, coin_selection_params_bnb.m_cost_of_change));
     333                 :            : 
     334                 :            :         // Test fees subtracted from output:
     335                 :          0 :         available_coins.Clear();
     336                 :          0 :         add_coin(available_coins, *wallet, 1 * CENT, coin_selection_params_bnb.m_effective_feerate);
     337                 :          0 :         available_coins.All().at(0).input_bytes = 40;
     338                 :          0 :         const auto result9 = SelectCoinsBnB(GroupCoins(available_coins.All()), 1 * CENT, coin_selection_params_bnb.m_cost_of_change);
     339                 :          0 :         BOOST_CHECK(result9);
     340                 :          0 :         BOOST_CHECK_EQUAL(result9->GetSelectedValue(), 1 * CENT);
     341                 :          0 :     }
     342                 :            : 
     343                 :            :     {
     344                 :          0 :         std::unique_ptr<CWallet> wallet = NewWallet(m_node);
     345                 :            : 
     346                 :          0 :         CoinsResult available_coins;
     347                 :            : 
     348                 :          0 :         add_coin(available_coins, *wallet, 5 * CENT, coin_selection_params_bnb.m_effective_feerate, 6 * 24, false, 0, true);
     349                 :          0 :         add_coin(available_coins, *wallet, 3 * CENT, coin_selection_params_bnb.m_effective_feerate, 6 * 24, false, 0, true);
     350                 :          0 :         add_coin(available_coins, *wallet, 2 * CENT, coin_selection_params_bnb.m_effective_feerate, 6 * 24, false, 0, true);
     351                 :          0 :         CCoinControl coin_control;
     352                 :          0 :         coin_control.m_allow_other_inputs = true;
     353                 :          0 :         COutput select_coin = available_coins.All().at(0);
     354                 :          0 :         coin_control.Select(select_coin.outpoint);
     355                 :          0 :         PreSelectedInputs selected_input;
     356                 :          0 :         selected_input.Insert(select_coin, coin_selection_params_bnb.m_subtract_fee_outputs);
     357                 :          0 :         available_coins.Erase({available_coins.coins[OutputType::BECH32].begin()->outpoint});
     358                 :          0 :         coin_selection_params_bnb.m_effective_feerate = CFeeRate(0);
     359                 :          0 :         LOCK(wallet->cs_wallet);
     360                 :          0 :         const auto result10 = SelectCoins(*wallet, available_coins, selected_input, 10 * CENT, coin_control, coin_selection_params_bnb);
     361                 :          0 :         BOOST_CHECK(result10);
     362                 :          0 :     }
     363                 :            :     {
     364                 :          0 :         std::unique_ptr<CWallet> wallet = NewWallet(m_node);
     365                 :          0 :         LOCK(wallet->cs_wallet); // Every 'SelectCoins' call requires it
     366                 :            : 
     367                 :          0 :         CoinsResult available_coins;
     368                 :            : 
     369                 :            :         // single coin should be selected when effective fee > long term fee
     370                 :          0 :         coin_selection_params_bnb.m_effective_feerate = CFeeRate(5000);
     371                 :          0 :         coin_selection_params_bnb.m_long_term_feerate = CFeeRate(3000);
     372                 :            : 
     373                 :          0 :         add_coin(available_coins, *wallet, 10 * CENT, coin_selection_params_bnb.m_effective_feerate, 6 * 24, false, 0, true);
     374                 :          0 :         add_coin(available_coins, *wallet, 9 * CENT, coin_selection_params_bnb.m_effective_feerate, 6 * 24, false, 0, true);
     375                 :          0 :         add_coin(available_coins, *wallet, 1 * CENT, coin_selection_params_bnb.m_effective_feerate, 6 * 24, false, 0, true);
     376                 :            : 
     377                 :          0 :         expected_result.Clear();
     378                 :          0 :         add_coin(10 * CENT, 2, expected_result);
     379                 :          0 :         CCoinControl coin_control;
     380                 :          0 :         const auto result11 = SelectCoins(*wallet, available_coins, /*pre_set_inputs=*/{}, 10 * CENT, coin_control, coin_selection_params_bnb);
     381                 :          0 :         BOOST_CHECK(EquivalentResult(expected_result, *result11));
     382                 :          0 :         available_coins.Clear();
     383                 :            : 
     384                 :            :         // more coins should be selected when effective fee < long term fee
     385                 :          0 :         coin_selection_params_bnb.m_effective_feerate = CFeeRate(3000);
     386                 :          0 :         coin_selection_params_bnb.m_long_term_feerate = CFeeRate(5000);
     387                 :            : 
     388                 :          0 :         add_coin(available_coins, *wallet, 10 * CENT, coin_selection_params_bnb.m_effective_feerate, 6 * 24, false, 0, true);
     389                 :          0 :         add_coin(available_coins, *wallet, 9 * CENT, coin_selection_params_bnb.m_effective_feerate, 6 * 24, false, 0, true);
     390                 :          0 :         add_coin(available_coins, *wallet, 1 * CENT, coin_selection_params_bnb.m_effective_feerate, 6 * 24, false, 0, true);
     391                 :            : 
     392                 :          0 :         expected_result.Clear();
     393                 :          0 :         add_coin(9 * CENT, 2, expected_result);
     394                 :          0 :         add_coin(1 * CENT, 2, expected_result);
     395                 :          0 :         const auto result12 = SelectCoins(*wallet, available_coins, /*pre_set_inputs=*/{}, 10 * CENT, coin_control, coin_selection_params_bnb);
     396                 :          0 :         BOOST_CHECK(EquivalentResult(expected_result, *result12));
     397                 :          0 :         available_coins.Clear();
     398                 :            : 
     399                 :            :         // pre selected coin should be selected even if disadvantageous
     400                 :          0 :         coin_selection_params_bnb.m_effective_feerate = CFeeRate(5000);
     401                 :          0 :         coin_selection_params_bnb.m_long_term_feerate = CFeeRate(3000);
     402                 :            : 
     403                 :          0 :         add_coin(available_coins, *wallet, 10 * CENT, coin_selection_params_bnb.m_effective_feerate, 6 * 24, false, 0, true);
     404                 :          0 :         add_coin(available_coins, *wallet, 9 * CENT, coin_selection_params_bnb.m_effective_feerate, 6 * 24, false, 0, true);
     405                 :          0 :         add_coin(available_coins, *wallet, 1 * CENT, coin_selection_params_bnb.m_effective_feerate, 6 * 24, false, 0, true);
     406                 :            : 
     407                 :          0 :         expected_result.Clear();
     408                 :          0 :         add_coin(9 * CENT, 2, expected_result);
     409                 :          0 :         add_coin(1 * CENT, 2, expected_result);
     410                 :          0 :         coin_control.m_allow_other_inputs = true;
     411                 :          0 :         COutput select_coin = available_coins.All().at(1); // pre select 9 coin
     412                 :          0 :         coin_control.Select(select_coin.outpoint);
     413                 :          0 :         PreSelectedInputs selected_input;
     414                 :          0 :         selected_input.Insert(select_coin, coin_selection_params_bnb.m_subtract_fee_outputs);
     415                 :          0 :         available_coins.Erase({(++available_coins.coins[OutputType::BECH32].begin())->outpoint});
     416                 :          0 :         const auto result13 = SelectCoins(*wallet, available_coins, selected_input, 10 * CENT, coin_control, coin_selection_params_bnb);
     417                 :          0 :         BOOST_CHECK(EquivalentResult(expected_result, *result13));
     418                 :          0 :     }
     419                 :            : 
     420                 :            :     {
     421                 :            :         // Test bnb max weight exceeded
     422                 :            :         // Inputs set [10, 9, 8, 5, 3, 1], Selection Target = 16 and coin 5 exceeding the max weight.
     423                 :            : 
     424                 :          0 :         std::unique_ptr<CWallet> wallet = NewWallet(m_node);
     425                 :            : 
     426                 :          0 :         CoinsResult available_coins;
     427                 :          0 :         add_coin(available_coins, *wallet, 10 * CENT, coin_selection_params_bnb.m_effective_feerate, 6 * 24, false, 0, true);
     428                 :          0 :         add_coin(available_coins, *wallet, 9 * CENT, coin_selection_params_bnb.m_effective_feerate, 6 * 24, false, 0, true);
     429                 :          0 :         add_coin(available_coins, *wallet, 8 * CENT, coin_selection_params_bnb.m_effective_feerate, 6 * 24, false, 0, true);
     430                 :          0 :         add_coin(available_coins, *wallet, 5 * CENT, coin_selection_params_bnb.m_effective_feerate, 6 * 24, false, 0, true, /*custom_size=*/MAX_STANDARD_TX_WEIGHT);
     431                 :          0 :         add_coin(available_coins, *wallet, 3 * CENT, coin_selection_params_bnb.m_effective_feerate, 6 * 24, false, 0, true);
     432                 :          0 :         add_coin(available_coins, *wallet, 1 * CENT, coin_selection_params_bnb.m_effective_feerate, 6 * 24, false, 0, true);
     433                 :            : 
     434                 :          0 :         CAmount selection_target = 16 * CENT;
     435                 :          0 :         const auto& no_res = SelectCoinsBnB(GroupCoins(available_coins.All(), /*subtract_fee_outputs*/true),
     436                 :          0 :                                             selection_target, /*cost_of_change=*/0, MAX_STANDARD_TX_WEIGHT);
     437                 :          0 :         BOOST_REQUIRE(!no_res);
     438                 :          0 :         BOOST_CHECK(util::ErrorString(no_res).original.find("The inputs size exceeds the maximum weight") != std::string::npos);
     439                 :            : 
     440                 :            :         // Now add same coin value with a good size and check that it gets selected
     441                 :          0 :         add_coin(available_coins, *wallet, 5 * CENT, coin_selection_params_bnb.m_effective_feerate, 6 * 24, false, 0, true);
     442                 :          0 :         const auto& res = SelectCoinsBnB(GroupCoins(available_coins.All(), /*subtract_fee_outputs*/true), selection_target, /*cost_of_change=*/0);
     443                 :            : 
     444                 :          0 :         expected_result.Clear();
     445                 :          0 :         add_coin(8 * CENT, 2, expected_result);
     446                 :          0 :         add_coin(5 * CENT, 2, expected_result);
     447                 :          0 :         add_coin(3 * CENT, 2, expected_result);
     448                 :          0 :         BOOST_CHECK(EquivalentResult(expected_result, *res));
     449                 :          0 :     }
     450                 :          0 : }
     451                 :            : 
     452                 :          0 : BOOST_AUTO_TEST_CASE(knapsack_solver_test)
     453                 :            : {
     454                 :          0 :     FastRandomContext rand{};
     455                 :          0 :     const auto temp1{[&rand](std::vector<OutputGroup>& g, const CAmount& v, CAmount c) { return KnapsackSolver(g, v, c, rand); }};
     456                 :          0 :     const auto KnapsackSolver{temp1};
     457                 :          0 :     std::unique_ptr<CWallet> wallet = NewWallet(m_node);
     458                 :            : 
     459                 :          0 :     CoinsResult available_coins;
     460                 :            : 
     461                 :            :     // test multiple times to allow for differences in the shuffle order
     462                 :          0 :     for (int i = 0; i < RUN_TESTS; i++)
     463                 :            :     {
     464                 :          0 :         available_coins.Clear();
     465                 :            : 
     466                 :            :         // with an empty wallet we can't even pay one cent
     467                 :          0 :         BOOST_CHECK(!KnapsackSolver(KnapsackGroupOutputs(available_coins, *wallet, filter_standard), 1 * CENT, CENT));
     468                 :            : 
     469                 :          0 :         add_coin(available_coins, *wallet, 1*CENT, CFeeRate(0), 4);        // add a new 1 cent coin
     470                 :            : 
     471                 :            :         // with a new 1 cent coin, we still can't find a mature 1 cent
     472                 :          0 :         BOOST_CHECK(!KnapsackSolver(KnapsackGroupOutputs(available_coins, *wallet, filter_standard), 1 * CENT, CENT));
     473                 :            : 
     474                 :            :         // but we can find a new 1 cent
     475                 :          0 :         const auto result1 = KnapsackSolver(KnapsackGroupOutputs(available_coins, *wallet, filter_confirmed), 1 * CENT, CENT);
     476                 :          0 :         BOOST_CHECK(result1);
     477                 :          0 :         BOOST_CHECK_EQUAL(result1->GetSelectedValue(), 1 * CENT);
     478                 :            : 
     479                 :          0 :         add_coin(available_coins, *wallet, 2*CENT);           // add a mature 2 cent coin
     480                 :            : 
     481                 :            :         // we can't make 3 cents of mature coins
     482                 :          0 :         BOOST_CHECK(!KnapsackSolver(KnapsackGroupOutputs(available_coins, *wallet, filter_standard), 3 * CENT, CENT));
     483                 :            : 
     484                 :            :         // we can make 3 cents of new coins
     485                 :          0 :         const auto result2 = KnapsackSolver(KnapsackGroupOutputs(available_coins, *wallet, filter_confirmed), 3 * CENT, CENT);
     486                 :          0 :         BOOST_CHECK(result2);
     487                 :          0 :         BOOST_CHECK_EQUAL(result2->GetSelectedValue(), 3 * CENT);
     488                 :            : 
     489                 :          0 :         add_coin(available_coins, *wallet, 5*CENT);           // add a mature 5 cent coin,
     490                 :          0 :         add_coin(available_coins, *wallet, 10*CENT, CFeeRate(0), 3, true); // a new 10 cent coin sent from one of our own addresses
     491                 :          0 :         add_coin(available_coins, *wallet, 20*CENT);          // and a mature 20 cent coin
     492                 :            : 
     493                 :            :         // now we have new: 1+10=11 (of which 10 was self-sent), and mature: 2+5+20=27.  total = 38
     494                 :            : 
     495                 :            :         // we can't make 38 cents only if we disallow new coins:
     496                 :          0 :         BOOST_CHECK(!KnapsackSolver(KnapsackGroupOutputs(available_coins, *wallet, filter_standard), 38 * CENT, CENT));
     497                 :            :         // we can't even make 37 cents if we don't allow new coins even if they're from us
     498                 :          0 :         BOOST_CHECK(!KnapsackSolver(KnapsackGroupOutputs(available_coins, *wallet, filter_standard_extra), 38 * CENT, CENT));
     499                 :            :         // but we can make 37 cents if we accept new coins from ourself
     500                 :          0 :         const auto result3 = KnapsackSolver(KnapsackGroupOutputs(available_coins, *wallet, filter_standard), 37 * CENT, CENT);
     501                 :          0 :         BOOST_CHECK(result3);
     502                 :          0 :         BOOST_CHECK_EQUAL(result3->GetSelectedValue(), 37 * CENT);
     503                 :            :         // and we can make 38 cents if we accept all new coins
     504                 :          0 :         const auto result4 = KnapsackSolver(KnapsackGroupOutputs(available_coins, *wallet, filter_confirmed), 38 * CENT, CENT);
     505                 :          0 :         BOOST_CHECK(result4);
     506                 :          0 :         BOOST_CHECK_EQUAL(result4->GetSelectedValue(), 38 * CENT);
     507                 :            : 
     508                 :            :         // try making 34 cents from 1,2,5,10,20 - we can't do it exactly
     509                 :          0 :         const auto result5 = KnapsackSolver(KnapsackGroupOutputs(available_coins, *wallet, filter_confirmed), 34 * CENT, CENT);
     510                 :          0 :         BOOST_CHECK(result5);
     511                 :          0 :         BOOST_CHECK_EQUAL(result5->GetSelectedValue(), 35 * CENT);       // but 35 cents is closest
     512                 :          0 :         BOOST_CHECK_EQUAL(result5->GetInputSet().size(), 3U);     // the best should be 20+10+5.  it's incredibly unlikely the 1 or 2 got included (but possible)
     513                 :            : 
     514                 :            :         // when we try making 7 cents, the smaller coins (1,2,5) are enough.  We should see just 2+5
     515                 :          0 :         const auto result6 = KnapsackSolver(KnapsackGroupOutputs(available_coins, *wallet, filter_confirmed), 7 * CENT, CENT);
     516                 :          0 :         BOOST_CHECK(result6);
     517                 :          0 :         BOOST_CHECK_EQUAL(result6->GetSelectedValue(), 7 * CENT);
     518                 :          0 :         BOOST_CHECK_EQUAL(result6->GetInputSet().size(), 2U);
     519                 :            : 
     520                 :            :         // when we try making 8 cents, the smaller coins (1,2,5) are exactly enough.
     521                 :          0 :         const auto result7 = KnapsackSolver(KnapsackGroupOutputs(available_coins, *wallet, filter_confirmed), 8 * CENT, CENT);
     522                 :          0 :         BOOST_CHECK(result7);
     523                 :          0 :         BOOST_CHECK(result7->GetSelectedValue() == 8 * CENT);
     524                 :          0 :         BOOST_CHECK_EQUAL(result7->GetInputSet().size(), 3U);
     525                 :            : 
     526                 :            :         // when we try making 9 cents, no subset of smaller coins is enough, and we get the next bigger coin (10)
     527                 :          0 :         const auto result8 = KnapsackSolver(KnapsackGroupOutputs(available_coins, *wallet, filter_confirmed), 9 * CENT, CENT);
     528                 :          0 :         BOOST_CHECK(result8);
     529                 :          0 :         BOOST_CHECK_EQUAL(result8->GetSelectedValue(), 10 * CENT);
     530                 :          0 :         BOOST_CHECK_EQUAL(result8->GetInputSet().size(), 1U);
     531                 :            : 
     532                 :            :         // now clear out the wallet and start again to test choosing between subsets of smaller coins and the next biggest coin
     533                 :          0 :         available_coins.Clear();
     534                 :            : 
     535                 :          0 :         add_coin(available_coins, *wallet,  6*CENT);
     536                 :          0 :         add_coin(available_coins, *wallet,  7*CENT);
     537                 :          0 :         add_coin(available_coins, *wallet,  8*CENT);
     538                 :          0 :         add_coin(available_coins, *wallet, 20*CENT);
     539                 :          0 :         add_coin(available_coins, *wallet, 30*CENT); // now we have 6+7+8+20+30 = 71 cents total
     540                 :            : 
     541                 :            :         // check that we have 71 and not 72
     542                 :          0 :         const auto result9 = KnapsackSolver(KnapsackGroupOutputs(available_coins, *wallet, filter_confirmed), 71 * CENT, CENT);
     543                 :          0 :         BOOST_CHECK(result9);
     544                 :          0 :         BOOST_CHECK(!KnapsackSolver(KnapsackGroupOutputs(available_coins, *wallet, filter_confirmed), 72 * CENT, CENT));
     545                 :            : 
     546                 :            :         // 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
     547                 :          0 :         const auto result10 = KnapsackSolver(KnapsackGroupOutputs(available_coins, *wallet, filter_confirmed), 16 * CENT, CENT);
     548                 :          0 :         BOOST_CHECK(result10);
     549                 :          0 :         BOOST_CHECK_EQUAL(result10->GetSelectedValue(), 20 * CENT); // we should get 20 in one coin
     550                 :          0 :         BOOST_CHECK_EQUAL(result10->GetInputSet().size(), 1U);
     551                 :            : 
     552                 :          0 :         add_coin(available_coins, *wallet,  5*CENT); // now we have 5+6+7+8+20+30 = 75 cents total
     553                 :            : 
     554                 :            :         // 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
     555                 :          0 :         const auto result11 = KnapsackSolver(KnapsackGroupOutputs(available_coins, *wallet, filter_confirmed), 16 * CENT, CENT);
     556                 :          0 :         BOOST_CHECK(result11);
     557                 :          0 :         BOOST_CHECK_EQUAL(result11->GetSelectedValue(), 18 * CENT); // we should get 18 in 3 coins
     558                 :          0 :         BOOST_CHECK_EQUAL(result11->GetInputSet().size(), 3U);
     559                 :            : 
     560                 :          0 :         add_coin(available_coins, *wallet,  18*CENT); // now we have 5+6+7+8+18+20+30
     561                 :            : 
     562                 :            :         // 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
     563                 :          0 :         const auto result12 = KnapsackSolver(KnapsackGroupOutputs(available_coins, *wallet, filter_confirmed), 16 * CENT, CENT);
     564                 :          0 :         BOOST_CHECK(result12);
     565                 :          0 :         BOOST_CHECK_EQUAL(result12->GetSelectedValue(), 18 * CENT);  // we should get 18 in 1 coin
     566                 :          0 :         BOOST_CHECK_EQUAL(result12->GetInputSet().size(), 1U); // because in the event of a tie, the biggest coin wins
     567                 :            : 
     568                 :            :         // now try making 11 cents.  we should get 5+6
     569                 :          0 :         const auto result13 = KnapsackSolver(KnapsackGroupOutputs(available_coins, *wallet, filter_confirmed), 11 * CENT, CENT);
     570                 :          0 :         BOOST_CHECK(result13);
     571                 :          0 :         BOOST_CHECK_EQUAL(result13->GetSelectedValue(), 11 * CENT);
     572                 :          0 :         BOOST_CHECK_EQUAL(result13->GetInputSet().size(), 2U);
     573                 :            : 
     574                 :            :         // check that the smallest bigger coin is used
     575                 :          0 :         add_coin(available_coins, *wallet,  1*COIN);
     576                 :          0 :         add_coin(available_coins, *wallet,  2*COIN);
     577                 :          0 :         add_coin(available_coins, *wallet,  3*COIN);
     578                 :          0 :         add_coin(available_coins, *wallet,  4*COIN); // now we have 5+6+7+8+18+20+30+100+200+300+400 = 1094 cents
     579                 :          0 :         const auto result14 = KnapsackSolver(KnapsackGroupOutputs(available_coins, *wallet, filter_confirmed), 95 * CENT, CENT);
     580                 :          0 :         BOOST_CHECK(result14);
     581                 :          0 :         BOOST_CHECK_EQUAL(result14->GetSelectedValue(), 1 * COIN);  // we should get 1 BTC in 1 coin
     582                 :          0 :         BOOST_CHECK_EQUAL(result14->GetInputSet().size(), 1U);
     583                 :            : 
     584                 :          0 :         const auto result15 = KnapsackSolver(KnapsackGroupOutputs(available_coins, *wallet, filter_confirmed), 195 * CENT, CENT);
     585                 :          0 :         BOOST_CHECK(result15);
     586                 :          0 :         BOOST_CHECK_EQUAL(result15->GetSelectedValue(), 2 * COIN);  // we should get 2 BTC in 1 coin
     587                 :          0 :         BOOST_CHECK_EQUAL(result15->GetInputSet().size(), 1U);
     588                 :            : 
     589                 :            :         // empty the wallet and start again, now with fractions of a cent, to test small change avoidance
     590                 :            : 
     591                 :          0 :         available_coins.Clear();
     592                 :          0 :         add_coin(available_coins, *wallet, CENT * 1 / 10);
     593                 :          0 :         add_coin(available_coins, *wallet, CENT * 2 / 10);
     594                 :          0 :         add_coin(available_coins, *wallet, CENT * 3 / 10);
     595                 :          0 :         add_coin(available_coins, *wallet, CENT * 4 / 10);
     596                 :          0 :         add_coin(available_coins, *wallet, CENT * 5 / 10);
     597                 :            : 
     598                 :            :         // try making 1 * CENT from the 1.5 * CENT
     599                 :            :         // we'll get change smaller than CENT whatever happens, so can expect CENT exactly
     600                 :          0 :         const auto result16 = KnapsackSolver(KnapsackGroupOutputs(available_coins, *wallet, filter_confirmed), CENT, CENT);
     601                 :          0 :         BOOST_CHECK(result16);
     602                 :          0 :         BOOST_CHECK_EQUAL(result16->GetSelectedValue(), CENT);
     603                 :            : 
     604                 :            :         // but if we add a bigger coin, small change is avoided
     605                 :          0 :         add_coin(available_coins, *wallet, 1111*CENT);
     606                 :            : 
     607                 :            :         // try making 1 from 0.1 + 0.2 + 0.3 + 0.4 + 0.5 + 1111 = 1112.5
     608                 :          0 :         const auto result17 = KnapsackSolver(KnapsackGroupOutputs(available_coins, *wallet, filter_confirmed), 1 * CENT, CENT);
     609                 :          0 :         BOOST_CHECK(result17);
     610                 :          0 :         BOOST_CHECK_EQUAL(result17->GetSelectedValue(), 1 * CENT); // we should get the exact amount
     611                 :            : 
     612                 :            :         // if we add more small coins:
     613                 :          0 :         add_coin(available_coins, *wallet, CENT * 6 / 10);
     614                 :          0 :         add_coin(available_coins, *wallet, CENT * 7 / 10);
     615                 :            : 
     616                 :            :         // and try again to make 1.0 * CENT
     617                 :          0 :         const auto result18 = KnapsackSolver(KnapsackGroupOutputs(available_coins, *wallet, filter_confirmed), 1 * CENT, CENT);
     618                 :          0 :         BOOST_CHECK(result18);
     619                 :          0 :         BOOST_CHECK_EQUAL(result18->GetSelectedValue(), 1 * CENT); // we should get the exact amount
     620                 :            : 
     621                 :            :         // run the 'mtgox' test (see https://blockexplorer.com/tx/29a3efd3ef04f9153d47a990bd7b048a4b2d213daaa5fb8ed670fb85f13bdbcf)
     622                 :            :         // they tried to consolidate 10 50k coins into one 500k coin, and ended up with 50k in change
     623                 :          0 :         available_coins.Clear();
     624                 :          0 :         for (int j = 0; j < 20; j++)
     625                 :          0 :             add_coin(available_coins, *wallet, 50000 * COIN);
     626                 :            : 
     627                 :          0 :         const auto result19 = KnapsackSolver(KnapsackGroupOutputs(available_coins, *wallet, filter_confirmed), 500000 * COIN, CENT);
     628                 :          0 :         BOOST_CHECK(result19);
     629                 :          0 :         BOOST_CHECK_EQUAL(result19->GetSelectedValue(), 500000 * COIN); // we should get the exact amount
     630                 :          0 :         BOOST_CHECK_EQUAL(result19->GetInputSet().size(), 10U); // in ten coins
     631                 :            : 
     632                 :            :         // if there's not enough in the smaller coins to make at least 1 * CENT change (0.5+0.6+0.7 < 1.0+1.0),
     633                 :            :         // we need to try finding an exact subset anyway
     634                 :            : 
     635                 :            :         // sometimes it will fail, and so we use the next biggest coin:
     636                 :          0 :         available_coins.Clear();
     637                 :          0 :         add_coin(available_coins, *wallet, CENT * 5 / 10);
     638                 :          0 :         add_coin(available_coins, *wallet, CENT * 6 / 10);
     639                 :          0 :         add_coin(available_coins, *wallet, CENT * 7 / 10);
     640                 :          0 :         add_coin(available_coins, *wallet, 1111 * CENT);
     641                 :          0 :         const auto result20 = KnapsackSolver(KnapsackGroupOutputs(available_coins, *wallet, filter_confirmed), 1 * CENT, CENT);
     642                 :          0 :         BOOST_CHECK(result20);
     643                 :          0 :         BOOST_CHECK_EQUAL(result20->GetSelectedValue(), 1111 * CENT); // we get the bigger coin
     644                 :          0 :         BOOST_CHECK_EQUAL(result20->GetInputSet().size(), 1U);
     645                 :            : 
     646                 :            :         // but sometimes it's possible, and we use an exact subset (0.4 + 0.6 = 1.0)
     647                 :          0 :         available_coins.Clear();
     648                 :          0 :         add_coin(available_coins, *wallet, CENT * 4 / 10);
     649                 :          0 :         add_coin(available_coins, *wallet, CENT * 6 / 10);
     650                 :          0 :         add_coin(available_coins, *wallet, CENT * 8 / 10);
     651                 :          0 :         add_coin(available_coins, *wallet, 1111 * CENT);
     652                 :          0 :         const auto result21 = KnapsackSolver(KnapsackGroupOutputs(available_coins, *wallet, filter_confirmed), CENT, CENT);
     653                 :          0 :         BOOST_CHECK(result21);
     654                 :          0 :         BOOST_CHECK_EQUAL(result21->GetSelectedValue(), CENT);   // we should get the exact amount
     655                 :          0 :         BOOST_CHECK_EQUAL(result21->GetInputSet().size(), 2U); // in two coins 0.4+0.6
     656                 :            : 
     657                 :            :         // test avoiding small change
     658                 :          0 :         available_coins.Clear();
     659                 :          0 :         add_coin(available_coins, *wallet, CENT * 5 / 100);
     660                 :          0 :         add_coin(available_coins, *wallet, CENT * 1);
     661                 :          0 :         add_coin(available_coins, *wallet, CENT * 100);
     662                 :            : 
     663                 :            :         // trying to make 100.01 from these three coins
     664                 :          0 :         const auto result22 = KnapsackSolver(KnapsackGroupOutputs(available_coins, *wallet, filter_confirmed), CENT * 10001 / 100, CENT);
     665                 :          0 :         BOOST_CHECK(result22);
     666                 :          0 :         BOOST_CHECK_EQUAL(result22->GetSelectedValue(), CENT * 10105 / 100); // we should get all coins
     667                 :          0 :         BOOST_CHECK_EQUAL(result22->GetInputSet().size(), 3U);
     668                 :            : 
     669                 :            :         // but if we try to make 99.9, we should take the bigger of the two small coins to avoid small change
     670                 :          0 :         const auto result23 = KnapsackSolver(KnapsackGroupOutputs(available_coins, *wallet, filter_confirmed), CENT * 9990 / 100, CENT);
     671                 :          0 :         BOOST_CHECK(result23);
     672                 :          0 :         BOOST_CHECK_EQUAL(result23->GetSelectedValue(), 101 * CENT);
     673                 :          0 :         BOOST_CHECK_EQUAL(result23->GetInputSet().size(), 2U);
     674                 :          0 :     }
     675                 :            : 
     676                 :            :     // test with many inputs
     677                 :          0 :     for (CAmount amt=1500; amt < COIN; amt*=10) {
     678                 :          0 :         available_coins.Clear();
     679                 :            :         // Create 676 inputs (=  (old MAX_STANDARD_TX_SIZE == 100000)  / 148 bytes per input)
     680                 :          0 :         for (uint16_t j = 0; j < 676; j++)
     681                 :          0 :             add_coin(available_coins, *wallet, amt);
     682                 :            : 
     683                 :            :         // We only create the wallet once to save time, but we still run the coin selection RUN_TESTS times.
     684                 :          0 :         for (int i = 0; i < RUN_TESTS; i++) {
     685                 :          0 :             const auto result24 = KnapsackSolver(KnapsackGroupOutputs(available_coins, *wallet, filter_confirmed), 2000, CENT);
     686                 :          0 :             BOOST_CHECK(result24);
     687                 :            : 
     688                 :          0 :             if (amt - 2000 < CENT) {
     689                 :            :                 // needs more than one input:
     690                 :          0 :                 uint16_t returnSize = std::ceil((2000.0 + CENT)/amt);
     691                 :          0 :                 CAmount returnValue = amt * returnSize;
     692                 :          0 :                 BOOST_CHECK_EQUAL(result24->GetSelectedValue(), returnValue);
     693                 :          0 :                 BOOST_CHECK_EQUAL(result24->GetInputSet().size(), returnSize);
     694                 :          0 :             } else {
     695                 :            :                 // one input is sufficient:
     696                 :          0 :                 BOOST_CHECK_EQUAL(result24->GetSelectedValue(), amt);
     697                 :          0 :                 BOOST_CHECK_EQUAL(result24->GetInputSet().size(), 1U);
     698                 :            :             }
     699                 :          0 :         }
     700                 :          0 :     }
     701                 :            : 
     702                 :            :     // test randomness
     703                 :            :     {
     704                 :          0 :         available_coins.Clear();
     705                 :          0 :         for (int i2 = 0; i2 < 100; i2++)
     706                 :          0 :             add_coin(available_coins, *wallet, COIN);
     707                 :            : 
     708                 :            :         // Again, we only create the wallet once to save time, but we still run the coin selection RUN_TESTS times.
     709                 :          0 :         for (int i = 0; i < RUN_TESTS; i++) {
     710                 :            :             // picking 50 from 100 coins doesn't depend on the shuffle,
     711                 :            :             // but does depend on randomness in the stochastic approximation code
     712                 :          0 :             const auto result25 = KnapsackSolver(GroupCoins(available_coins.All()), 50 * COIN, CENT);
     713                 :          0 :             BOOST_CHECK(result25);
     714                 :          0 :             const auto result26 = KnapsackSolver(GroupCoins(available_coins.All()), 50 * COIN, CENT);
     715                 :          0 :             BOOST_CHECK(result26);
     716                 :          0 :             BOOST_CHECK(!EqualResult(*result25, *result26));
     717                 :            : 
     718                 :          0 :             int fails = 0;
     719                 :          0 :             for (int j = 0; j < RANDOM_REPEATS; j++)
     720                 :            :             {
     721                 :            :                 // Test that the KnapsackSolver selects randomly from equivalent coins (same value and same input size).
     722                 :            :                 // When choosing 1 from 100 identical coins, 1% of the time, this test will choose the same coin twice
     723                 :            :                 // which will cause it to fail.
     724                 :            :                 // To avoid that issue, run the test RANDOM_REPEATS times and only complain if all of them fail
     725                 :          0 :                 const auto result27 = KnapsackSolver(GroupCoins(available_coins.All()), COIN, CENT);
     726                 :          0 :                 BOOST_CHECK(result27);
     727                 :          0 :                 const auto result28 = KnapsackSolver(GroupCoins(available_coins.All()), COIN, CENT);
     728                 :          0 :                 BOOST_CHECK(result28);
     729                 :          0 :                 if (EqualResult(*result27, *result28))
     730                 :          0 :                     fails++;
     731                 :          0 :             }
     732                 :          0 :             BOOST_CHECK_NE(fails, RANDOM_REPEATS);
     733                 :          0 :         }
     734                 :            : 
     735                 :            :         // add 75 cents in small change.  not enough to make 90 cents,
     736                 :            :         // then try making 90 cents.  there are multiple competing "smallest bigger" coins,
     737                 :            :         // one of which should be picked at random
     738                 :          0 :         add_coin(available_coins, *wallet, 5 * CENT);
     739                 :          0 :         add_coin(available_coins, *wallet, 10 * CENT);
     740                 :          0 :         add_coin(available_coins, *wallet, 15 * CENT);
     741                 :          0 :         add_coin(available_coins, *wallet, 20 * CENT);
     742                 :          0 :         add_coin(available_coins, *wallet, 25 * CENT);
     743                 :            : 
     744                 :          0 :         for (int i = 0; i < RUN_TESTS; i++) {
     745                 :          0 :             int fails = 0;
     746                 :          0 :             for (int j = 0; j < RANDOM_REPEATS; j++)
     747                 :            :             {
     748                 :          0 :                 const auto result29 = KnapsackSolver(GroupCoins(available_coins.All()), 90 * CENT, CENT);
     749                 :          0 :                 BOOST_CHECK(result29);
     750                 :          0 :                 const auto result30 = KnapsackSolver(GroupCoins(available_coins.All()), 90 * CENT, CENT);
     751                 :          0 :                 BOOST_CHECK(result30);
     752                 :          0 :                 if (EqualResult(*result29, *result30))
     753                 :          0 :                     fails++;
     754                 :          0 :             }
     755                 :          0 :             BOOST_CHECK_NE(fails, RANDOM_REPEATS);
     756                 :          0 :         }
     757                 :            :     }
     758                 :          0 : }
     759                 :            : 
     760                 :          0 : BOOST_AUTO_TEST_CASE(ApproximateBestSubset)
     761                 :            : {
     762                 :          0 :     FastRandomContext rand{};
     763                 :          0 :     std::unique_ptr<CWallet> wallet = NewWallet(m_node);
     764                 :            : 
     765                 :          0 :     CoinsResult available_coins;
     766                 :            : 
     767                 :            :     // Test vValue sort order
     768                 :          0 :     for (int i = 0; i < 1000; i++)
     769                 :          0 :         add_coin(available_coins, *wallet, 1000 * COIN);
     770                 :          0 :     add_coin(available_coins, *wallet, 3 * COIN);
     771                 :            : 
     772                 :          0 :     const auto result = KnapsackSolver(KnapsackGroupOutputs(available_coins, *wallet, filter_standard), 1003 * COIN, CENT, rand);
     773                 :          0 :     BOOST_CHECK(result);
     774                 :          0 :     BOOST_CHECK_EQUAL(result->GetSelectedValue(), 1003 * COIN);
     775                 :          0 :     BOOST_CHECK_EQUAL(result->GetInputSet().size(), 2U);
     776                 :          0 : }
     777                 :            : 
     778                 :            : // Tests that with the ideal conditions, the coin selector will always be able to find a solution that can pay the target value
     779                 :          0 : BOOST_AUTO_TEST_CASE(SelectCoins_test)
     780                 :            : {
     781                 :          0 :     std::unique_ptr<CWallet> wallet = NewWallet(m_node);
     782                 :          0 :     LOCK(wallet->cs_wallet); // Every 'SelectCoins' call requires it
     783                 :            : 
     784                 :            :     // Random generator stuff
     785                 :          0 :     std::default_random_engine generator;
     786                 :          0 :     std::exponential_distribution<double> distribution (100);
     787                 :          0 :     FastRandomContext rand;
     788                 :            : 
     789                 :            :     // Run this test 100 times
     790                 :          0 :     for (int i = 0; i < 100; ++i)
     791                 :            :     {
     792                 :          0 :         CoinsResult available_coins;
     793                 :          0 :         CAmount balance{0};
     794                 :            : 
     795                 :            :         // Make a wallet with 1000 exponentially distributed random inputs
     796                 :          0 :         for (int j = 0; j < 1000; ++j)
     797                 :            :         {
     798                 :          0 :             CAmount val = distribution(generator)*10000000;
     799                 :          0 :             add_coin(available_coins, *wallet, val);
     800                 :          0 :             balance += val;
     801                 :          0 :         }
     802                 :            : 
     803                 :            :         // Generate a random fee rate in the range of 100 - 400
     804                 :          0 :         CFeeRate rate(rand.randrange(300) + 100);
     805                 :            : 
     806                 :            :         // Generate a random target value between 1000 and wallet balance
     807                 :          0 :         CAmount target = rand.randrange(balance - 1000) + 1000;
     808                 :            : 
     809                 :            :         // Perform selection
     810                 :          0 :         CoinSelectionParams cs_params{
     811                 :            :             rand,
     812                 :            :             /*change_output_size=*/ 34,
     813                 :            :             /*change_spend_size=*/ 148,
     814                 :            :             /*min_change_target=*/ CENT,
     815                 :          0 :             /*effective_feerate=*/ CFeeRate(0),
     816                 :          0 :             /*long_term_feerate=*/ CFeeRate(0),
     817                 :          0 :             /*discard_feerate=*/ CFeeRate(0),
     818                 :            :             /*tx_noinputs_size=*/ 0,
     819                 :            :             /*avoid_partial=*/ false,
     820                 :            :         };
     821                 :          0 :         cs_params.m_cost_of_change = 1;
     822                 :          0 :         cs_params.min_viable_change = 1;
     823                 :          0 :         CCoinControl cc;
     824                 :          0 :         const auto result = SelectCoins(*wallet, available_coins, /*pre_set_inputs=*/{}, target, cc, cs_params);
     825                 :          0 :         BOOST_CHECK(result);
     826                 :          0 :         BOOST_CHECK_GE(result->GetSelectedValue(), target);
     827                 :          0 :     }
     828                 :          0 : }
     829                 :            : 
     830                 :          0 : BOOST_AUTO_TEST_CASE(waste_test)
     831                 :            : {
     832                 :          0 :     const CAmount fee{100};
     833                 :          0 :     const CAmount change_cost{125};
     834                 :          0 :     const CAmount fee_diff{40};
     835                 :          0 :     const CAmount in_amt{3 * COIN};
     836                 :          0 :     const CAmount target{2 * COIN};
     837                 :          0 :     const CAmount excess{in_amt - fee * 2 - target};
     838                 :            : 
     839                 :            :     // The following tests that the waste is calculated correctly in various scenarios.
     840                 :            :     // ComputeAndSetWaste will first determine the size of the change output. We don't really
     841                 :            :     // care about the change and just want to use the variant that always includes the change_cost,
     842                 :            :     // so min_viable_change and change_fee are set to 0 to ensure that.
     843                 :            :     {
     844                 :            :         // Waste with change is the change cost and difference between fee and long term fee
     845                 :          0 :         SelectionResult selection1{target, SelectionAlgorithm::MANUAL};
     846                 :          0 :         add_coin(1 * COIN, 1, selection1, fee, fee - fee_diff);
     847                 :          0 :         add_coin(2 * COIN, 2, selection1, fee, fee - fee_diff);
     848                 :          0 :         selection1.ComputeAndSetWaste(/*min_viable_change=*/0, change_cost, /*change_fee=*/0);
     849                 :          0 :         BOOST_CHECK_EQUAL(fee_diff * 2 + change_cost, selection1.GetWaste());
     850                 :            : 
     851                 :            :         // Waste will be greater when fee is greater, but long term fee is the same
     852                 :          0 :         SelectionResult selection2{target, SelectionAlgorithm::MANUAL};
     853                 :          0 :         add_coin(1 * COIN, 1, selection2, fee * 2, fee - fee_diff);
     854                 :          0 :         add_coin(2 * COIN, 2, selection2, fee * 2, fee - fee_diff);
     855                 :          0 :         selection2.ComputeAndSetWaste(/*min_viable_change=*/0, change_cost, /*change_fee=*/0);
     856                 :          0 :         BOOST_CHECK_GT(selection2.GetWaste(), selection1.GetWaste());
     857                 :            : 
     858                 :            :         // Waste with change is the change cost and difference between fee and long term fee
     859                 :            :         // With long term fee greater than fee, waste should be less than when long term fee is less than fee
     860                 :          0 :         SelectionResult selection3{target, SelectionAlgorithm::MANUAL};
     861                 :          0 :         add_coin(1 * COIN, 1, selection3, fee, fee + fee_diff);
     862                 :          0 :         add_coin(2 * COIN, 2, selection3, fee, fee + fee_diff);
     863                 :          0 :         selection3.ComputeAndSetWaste(/*min_viable_change=*/0, change_cost, /*change_fee=*/0);
     864                 :          0 :         BOOST_CHECK_EQUAL(fee_diff * -2 + change_cost, selection3.GetWaste());
     865                 :          0 :         BOOST_CHECK_LT(selection3.GetWaste(), selection1.GetWaste());
     866                 :          0 :     }
     867                 :            : 
     868                 :            :     {
     869                 :            :         // Waste without change is the excess and difference between fee and long term fee
     870                 :          0 :         SelectionResult selection_nochange1{target, SelectionAlgorithm::MANUAL};
     871                 :          0 :         add_coin(1 * COIN, 1, selection_nochange1, fee, fee - fee_diff);
     872                 :          0 :         add_coin(2 * COIN, 2, selection_nochange1, fee, fee - fee_diff);
     873                 :          0 :         selection_nochange1.ComputeAndSetWaste(/*min_viable_change=*/0, /*change_cost=*/0, /*change_fee=*/0);
     874                 :          0 :         BOOST_CHECK_EQUAL(fee_diff * 2 + excess, selection_nochange1.GetWaste());
     875                 :            : 
     876                 :            :         // Waste without change is the excess and difference between fee and long term fee
     877                 :            :         // With long term fee greater than fee, waste should be less than when long term fee is less than fee
     878                 :          0 :         SelectionResult selection_nochange2{target, SelectionAlgorithm::MANUAL};
     879                 :          0 :         add_coin(1 * COIN, 1, selection_nochange2, fee, fee + fee_diff);
     880                 :          0 :         add_coin(2 * COIN, 2, selection_nochange2, fee, fee + fee_diff);
     881                 :          0 :         selection_nochange2.ComputeAndSetWaste(/*min_viable_change=*/0, /*change_cost=*/0, /*change_fee=*/0);
     882                 :          0 :         BOOST_CHECK_EQUAL(fee_diff * -2 + excess, selection_nochange2.GetWaste());
     883                 :          0 :         BOOST_CHECK_LT(selection_nochange2.GetWaste(), selection_nochange1.GetWaste());
     884                 :          0 :     }
     885                 :            : 
     886                 :            :     {
     887                 :            :         // Waste with change and fee == long term fee is just cost of change
     888                 :          0 :         SelectionResult selection{target, SelectionAlgorithm::MANUAL};
     889                 :          0 :         add_coin(1 * COIN, 1, selection, fee, fee);
     890                 :          0 :         add_coin(2 * COIN, 2, selection, fee, fee);
     891                 :          0 :         selection.ComputeAndSetWaste(/*min_viable_change=*/0, change_cost, /*change_fee=*/0);
     892                 :          0 :         BOOST_CHECK_EQUAL(change_cost, selection.GetWaste());
     893                 :          0 :     }
     894                 :            : 
     895                 :            :     {
     896                 :            :         // Waste without change and fee == long term fee is just the excess
     897                 :          0 :         SelectionResult selection{target, SelectionAlgorithm::MANUAL};
     898                 :          0 :         add_coin(1 * COIN, 1, selection, fee, fee);
     899                 :          0 :         add_coin(2 * COIN, 2, selection, fee, fee);
     900                 :          0 :         selection.ComputeAndSetWaste(/*min_viable_change=*/0, /*change_cost=*/0, /*change_fee=*/0);
     901                 :          0 :         BOOST_CHECK_EQUAL(excess, selection.GetWaste());
     902                 :          0 :     }
     903                 :            : 
     904                 :            :     {
     905                 :            :         // No Waste when fee == long_term_fee, no change, and no excess
     906                 :          0 :         const CAmount exact_target{in_amt - fee * 2};
     907                 :          0 :         SelectionResult selection{exact_target, SelectionAlgorithm::MANUAL};
     908                 :          0 :         add_coin(1 * COIN, 1, selection, fee, fee);
     909                 :          0 :         add_coin(2 * COIN, 2, selection, fee, fee);
     910                 :          0 :         selection.ComputeAndSetWaste(/*min_viable_change=*/0, /*change_cost=*/0, /*change_fee=*/0);
     911                 :          0 :         BOOST_CHECK_EQUAL(0, selection.GetWaste());
     912                 :          0 :     }
     913                 :            : 
     914                 :            :     {
     915                 :            :         // No Waste when (fee - long_term_fee) == (-cost_of_change), and no excess
     916                 :          0 :         SelectionResult selection{target, SelectionAlgorithm::MANUAL};
     917                 :          0 :         const CAmount new_change_cost{fee_diff * 2};
     918                 :          0 :         add_coin(1 * COIN, 1, selection, fee, fee + fee_diff);
     919                 :          0 :         add_coin(2 * COIN, 2, selection, fee, fee + fee_diff);
     920                 :          0 :         selection.ComputeAndSetWaste(/*min_viable_change=*/0, new_change_cost, /*change_fee=*/0);
     921                 :          0 :         BOOST_CHECK_EQUAL(0, selection.GetWaste());
     922                 :          0 :     }
     923                 :            : 
     924                 :            :     {
     925                 :            :         // No Waste when (fee - long_term_fee) == (-excess), no change cost
     926                 :          0 :         const CAmount new_target{in_amt - fee * 2 - fee_diff * 2};
     927                 :          0 :         SelectionResult selection{new_target, SelectionAlgorithm::MANUAL};
     928                 :          0 :         add_coin(1 * COIN, 1, selection, fee, fee + fee_diff);
     929                 :          0 :         add_coin(2 * COIN, 2, selection, fee, fee + fee_diff);
     930                 :          0 :         selection.ComputeAndSetWaste(/*min_viable_change=*/0, /*change_cost=*/0, /*change_fee=*/0);
     931                 :          0 :         BOOST_CHECK_EQUAL(0, selection.GetWaste());
     932                 :          0 :     }
     933                 :            : 
     934                 :            :     {
     935                 :            :         // Negative waste when the long term fee is greater than the current fee and the selected value == target
     936                 :          0 :         const CAmount exact_target{3 * COIN - 2 * fee};
     937                 :          0 :         SelectionResult selection{exact_target, SelectionAlgorithm::MANUAL};
     938                 :          0 :         const CAmount target_waste1{-2 * fee_diff}; // = (2 * fee) - (2 * (fee + fee_diff))
     939                 :          0 :         add_coin(1 * COIN, 1, selection, fee, fee + fee_diff);
     940                 :          0 :         add_coin(2 * COIN, 2, selection, fee, fee + fee_diff);
     941                 :          0 :         selection.ComputeAndSetWaste(/*min_viable_change=*/0, /*change_cost=*/0, /*change_fee=*/0);
     942                 :          0 :         BOOST_CHECK_EQUAL(target_waste1, selection.GetWaste());
     943                 :          0 :     }
     944                 :            : 
     945                 :            :     {
     946                 :            :         // Negative waste when the long term fee is greater than the current fee and change_cost < - (inputs * (fee - long_term_fee))
     947                 :          0 :         SelectionResult selection{target, SelectionAlgorithm::MANUAL};
     948                 :          0 :         const CAmount large_fee_diff{90};
     949                 :          0 :         const CAmount target_waste2{-2 * large_fee_diff + change_cost}; // = (2 * fee) - (2 * (fee + large_fee_diff)) + change_cost
     950                 :          0 :         add_coin(1 * COIN, 1, selection, fee, fee + large_fee_diff);
     951                 :          0 :         add_coin(2 * COIN, 2, selection, fee, fee + large_fee_diff);
     952                 :          0 :         selection.ComputeAndSetWaste(/*min_viable_change=*/0, change_cost, /*change_fee=*/0);
     953                 :          0 :         BOOST_CHECK_EQUAL(target_waste2, selection.GetWaste());
     954                 :          0 :     }
     955                 :          0 : }
     956                 :            : 
     957                 :            : 
     958                 :          0 : BOOST_AUTO_TEST_CASE(bump_fee_test)
     959                 :            : {
     960                 :          0 :     const CAmount fee{100};
     961                 :          0 :     const CAmount min_viable_change{200};
     962                 :          0 :     const CAmount change_cost{125};
     963                 :          0 :     const CAmount change_fee{35};
     964                 :          0 :     const CAmount fee_diff{40};
     965                 :          0 :     const CAmount target{2 * COIN};
     966                 :            : 
     967                 :            :     {
     968                 :          0 :         SelectionResult selection{target, SelectionAlgorithm::MANUAL};
     969                 :          0 :         add_coin(1 * COIN, 1, selection, /*fee=*/fee, /*long_term_fee=*/fee + fee_diff);
     970                 :          0 :         add_coin(2 * COIN, 2, selection, fee, fee + fee_diff);
     971                 :          0 :         const std::vector<std::shared_ptr<COutput>> inputs = selection.GetShuffledInputVector();
     972                 :            : 
     973                 :          0 :         for (size_t i = 0; i < inputs.size(); ++i) {
     974                 :          0 :             inputs[i]->ApplyBumpFee(20*(i+1));
     975                 :          0 :         }
     976                 :            : 
     977                 :          0 :         selection.ComputeAndSetWaste(min_viable_change, change_cost, change_fee);
     978                 :          0 :         CAmount expected_waste = fee_diff * -2 + change_cost + /*bump_fees=*/60;
     979                 :          0 :         BOOST_CHECK_EQUAL(expected_waste, selection.GetWaste());
     980                 :            : 
     981                 :          0 :         selection.SetBumpFeeDiscount(30);
     982                 :          0 :         selection.ComputeAndSetWaste(min_viable_change, change_cost, change_fee);
     983                 :          0 :         expected_waste = fee_diff * -2 + change_cost + /*bump_fees=*/60 - /*group_discount=*/30;
     984                 :          0 :         BOOST_CHECK_EQUAL(expected_waste, selection.GetWaste());
     985                 :          0 :     }
     986                 :            : 
     987                 :            :     {
     988                 :            :         // Test with changeless transaction
     989                 :            :         //
     990                 :            :         // Bump fees and excess both contribute fully to the waste score,
     991                 :            :         // therefore, a bump fee group discount will not change the waste
     992                 :            :         // score as long as we do not create change in both instances.
     993                 :          0 :         CAmount changeless_target = 3 * COIN - 2 * fee - 100;
     994                 :          0 :         SelectionResult selection{changeless_target, SelectionAlgorithm::MANUAL};
     995                 :          0 :         add_coin(1 * COIN, 1, selection, /*fee=*/fee, /*long_term_fee=*/fee + fee_diff);
     996                 :          0 :         add_coin(2 * COIN, 2, selection, fee, fee + fee_diff);
     997                 :          0 :         const std::vector<std::shared_ptr<COutput>> inputs = selection.GetShuffledInputVector();
     998                 :            : 
     999                 :          0 :         for (size_t i = 0; i < inputs.size(); ++i) {
    1000                 :          0 :             inputs[i]->ApplyBumpFee(20*(i+1));
    1001                 :          0 :         }
    1002                 :            : 
    1003                 :          0 :         selection.ComputeAndSetWaste(min_viable_change, change_cost, change_fee);
    1004                 :          0 :         CAmount expected_waste = fee_diff * -2 + /*bump_fees=*/60 + /*excess = 100 - bump_fees*/40;
    1005                 :          0 :         BOOST_CHECK_EQUAL(expected_waste, selection.GetWaste());
    1006                 :            : 
    1007                 :          0 :         selection.SetBumpFeeDiscount(30);
    1008                 :          0 :         selection.ComputeAndSetWaste(min_viable_change, change_cost, change_fee);
    1009                 :          0 :         expected_waste = fee_diff * -2 + /*bump_fees=*/60 - /*group_discount=*/30 + /*excess = 100 - bump_fees + group_discount*/70;
    1010                 :          0 :         BOOST_CHECK_EQUAL(expected_waste, selection.GetWaste());
    1011                 :          0 :     }
    1012                 :          0 : }
    1013                 :            : 
    1014                 :          0 : BOOST_AUTO_TEST_CASE(effective_value_test)
    1015                 :            : {
    1016                 :          0 :     const int input_bytes = 148;
    1017                 :          0 :     const CFeeRate feerate(1000);
    1018                 :          0 :     const CAmount nValue = 10000;
    1019                 :          0 :     const int nInput = 0;
    1020                 :            : 
    1021                 :          0 :     CMutableTransaction tx;
    1022                 :          0 :     tx.vout.resize(1);
    1023                 :          0 :     tx.vout[nInput].nValue = nValue;
    1024                 :            : 
    1025                 :            :     // standard case, pass feerate in constructor
    1026                 :          0 :     COutput output1(COutPoint(tx.GetHash(), nInput), tx.vout.at(nInput), /*depth=*/ 1, input_bytes, /*spendable=*/ true, /*solvable=*/ true, /*safe=*/ true, /*time=*/ 0, /*from_me=*/ false, feerate);
    1027                 :          0 :     const CAmount expected_ev1 = 9852; // 10000 - 148
    1028                 :          0 :     BOOST_CHECK_EQUAL(output1.GetEffectiveValue(), expected_ev1);
    1029                 :            : 
    1030                 :            :     // input bytes unknown (input_bytes = -1), pass feerate in constructor
    1031                 :          0 :     COutput output2(COutPoint(tx.GetHash(), nInput), tx.vout.at(nInput), /*depth=*/ 1, /*input_bytes=*/ -1, /*spendable=*/ true, /*solvable=*/ true, /*safe=*/ true, /*time=*/ 0, /*from_me=*/ false, feerate);
    1032                 :          0 :     BOOST_CHECK_EQUAL(output2.GetEffectiveValue(), nValue); // The effective value should be equal to the absolute value if input_bytes is -1
    1033                 :            : 
    1034                 :            :     // negative effective value, pass feerate in constructor
    1035                 :          0 :     COutput output3(COutPoint(tx.GetHash(), nInput), tx.vout.at(nInput), /*depth=*/ 1, input_bytes, /*spendable=*/ true, /*solvable=*/ true, /*safe=*/ true, /*time=*/ 0, /*from_me=*/ false, CFeeRate(100000));
    1036                 :          0 :     const CAmount expected_ev3 = -4800; // 10000 - 14800
    1037                 :          0 :     BOOST_CHECK_EQUAL(output3.GetEffectiveValue(), expected_ev3);
    1038                 :            : 
    1039                 :            :     // standard case, pass fees in constructor
    1040                 :          0 :     const CAmount fees = 148;
    1041                 :          0 :     COutput output4(COutPoint(tx.GetHash(), nInput), tx.vout.at(nInput), /*depth=*/ 1, input_bytes, /*spendable=*/ true, /*solvable=*/ true, /*safe=*/ true, /*time=*/ 0, /*from_me=*/ false, fees);
    1042                 :          0 :     BOOST_CHECK_EQUAL(output4.GetEffectiveValue(), expected_ev1);
    1043                 :            : 
    1044                 :            :     // input bytes unknown (input_bytes = -1), pass fees in constructor
    1045                 :          0 :     COutput output5(COutPoint(tx.GetHash(), nInput), tx.vout.at(nInput), /*depth=*/ 1, /*input_bytes=*/ -1, /*spendable=*/ true, /*solvable=*/ true, /*safe=*/ true, /*time=*/ 0, /*from_me=*/ false, /*fees=*/ 0);
    1046                 :          0 :     BOOST_CHECK_EQUAL(output5.GetEffectiveValue(), nValue); // The effective value should be equal to the absolute value if input_bytes is -1
    1047                 :          0 : }
    1048                 :            : 
    1049                 :            : 
    1050                 :          0 : static util::Result<SelectionResult> SelectCoinsSRD(const CAmount& target,
    1051                 :            :                                                     const CoinSelectionParams& cs_params,
    1052                 :            :                                                     const node::NodeContext& m_node,
    1053                 :            :                                                     int max_weight,
    1054                 :            :                                                     std::function<CoinsResult(CWallet&)> coin_setup)
    1055                 :            : {
    1056                 :          0 :     std::unique_ptr<CWallet> wallet = NewWallet(m_node);
    1057                 :          0 :     CoinEligibilityFilter filter(0, 0, 0); // accept all coins without ancestors
    1058                 :          0 :     Groups group = GroupOutputs(*wallet, coin_setup(*wallet), cs_params, {{filter}})[filter].all_groups;
    1059                 :          0 :     return SelectCoinsSRD(group.positive_group, target, cs_params.m_change_fee, cs_params.rng_fast, max_weight);
    1060                 :          0 : }
    1061                 :            : 
    1062                 :          0 : BOOST_AUTO_TEST_CASE(srd_tests)
    1063                 :            : {
    1064                 :            :     // Test SRD:
    1065                 :            :     // 1) Insufficient funds, select all provided coins and fail.
    1066                 :            :     // 2) Exceeded max weight, coin selection always surpasses the max allowed weight.
    1067                 :            :     // 3) Select coins without surpassing the max weight (some coins surpasses the max allowed weight, some others not)
    1068                 :            : 
    1069                 :          0 :     FastRandomContext rand;
    1070                 :          0 :     CoinSelectionParams dummy_params{ // Only used to provide the 'avoid_partial' flag.
    1071                 :            :             rand,
    1072                 :            :             /*change_output_size=*/34,
    1073                 :            :             /*change_spend_size=*/68,
    1074                 :            :             /*min_change_target=*/CENT,
    1075                 :          0 :             /*effective_feerate=*/CFeeRate(0),
    1076                 :          0 :             /*long_term_feerate=*/CFeeRate(0),
    1077                 :          0 :             /*discard_feerate=*/CFeeRate(0),
    1078                 :            :             /*tx_noinputs_size=*/10 + 34, // static header size + output size
    1079                 :            :             /*avoid_partial=*/false,
    1080                 :            :     };
    1081                 :            : 
    1082                 :            :     {
    1083                 :            :         // #########################################################
    1084                 :            :         // 1) Insufficient funds, select all provided coins and fail
    1085                 :            :         // #########################################################
    1086                 :          0 :         CAmount target = 49.5L * COIN;
    1087                 :          0 :         int max_weight = 10000; // high enough to not fail for this reason.
    1088                 :          0 :         const auto& res = SelectCoinsSRD(target, dummy_params, m_node, max_weight, [&](CWallet& wallet) {
    1089                 :          0 :             CoinsResult available_coins;
    1090                 :          0 :             for (int j = 0; j < 10; ++j) {
    1091                 :          0 :                 add_coin(available_coins, wallet, CAmount(1 * COIN));
    1092                 :          0 :                 add_coin(available_coins, wallet, CAmount(2 * COIN));
    1093                 :          0 :             }
    1094                 :          0 :             return available_coins;
    1095                 :          0 :         });
    1096                 :          0 :         BOOST_CHECK(!res);
    1097                 :          0 :         BOOST_CHECK(util::ErrorString(res).empty()); // empty means "insufficient funds"
    1098                 :          0 :     }
    1099                 :            : 
    1100                 :            :     {
    1101                 :            :         // ###########################
    1102                 :            :         // 2) Test max weight exceeded
    1103                 :            :         // ###########################
    1104                 :          0 :         CAmount target = 49.5L * COIN;
    1105                 :          0 :         int max_weight = 3000;
    1106                 :          0 :         const auto& res = SelectCoinsSRD(target, dummy_params, m_node, max_weight, [&](CWallet& wallet) {
    1107                 :          0 :             CoinsResult available_coins;
    1108                 :          0 :             for (int j = 0; j < 10; ++j) {
    1109                 :          0 :                 add_coin(available_coins, wallet, CAmount(1 * COIN), CFeeRate(0), 144, false, 0, true);
    1110                 :          0 :                 add_coin(available_coins, wallet, CAmount(2 * COIN), CFeeRate(0), 144, false, 0, true);
    1111                 :          0 :             }
    1112                 :          0 :             return available_coins;
    1113                 :          0 :         });
    1114                 :          0 :         BOOST_CHECK(!res);
    1115                 :          0 :         BOOST_CHECK(util::ErrorString(res).original.find("The inputs size exceeds the maximum weight") != std::string::npos);
    1116                 :          0 :     }
    1117                 :            : 
    1118                 :            :     {
    1119                 :            :         // ################################################################################################################
    1120                 :            :         // 3) Test selection when some coins surpass the max allowed weight while others not. --> must find a good solution
    1121                 :            :         // ################################################################################################################
    1122                 :          0 :         CAmount target = 25.33L * COIN;
    1123                 :          0 :         int max_weight = 10000; // WU
    1124                 :          0 :         const auto& res = SelectCoinsSRD(target, dummy_params, m_node, max_weight, [&](CWallet& wallet) {
    1125                 :          0 :             CoinsResult available_coins;
    1126                 :          0 :             for (int j = 0; j < 60; ++j) { // 60 UTXO --> 19,8 BTC total --> 60 × 272 WU = 16320 WU
    1127                 :          0 :                 add_coin(available_coins, wallet, CAmount(0.33 * COIN), CFeeRate(0), 144, false, 0, true);
    1128                 :          0 :             }
    1129                 :          0 :             for (int i = 0; i < 10; i++) { // 10 UTXO --> 20 BTC total --> 10 × 272 WU = 2720 WU
    1130                 :          0 :                 add_coin(available_coins, wallet, CAmount(2 * COIN), CFeeRate(0), 144, false, 0, true);
    1131                 :          0 :             }
    1132                 :          0 :             return available_coins;
    1133                 :          0 :         });
    1134                 :          0 :         BOOST_CHECK(res);
    1135                 :          0 :     }
    1136                 :          0 : }
    1137                 :            : 
    1138                 :          0 : static util::Result<SelectionResult> select_coins(const CAmount& target, const CoinSelectionParams& cs_params, const CCoinControl& cc, std::function<CoinsResult(CWallet&)> coin_setup, const node::NodeContext& m_node)
    1139                 :            : {
    1140                 :          0 :     std::unique_ptr<CWallet> wallet = NewWallet(m_node);
    1141                 :          0 :     auto available_coins = coin_setup(*wallet);
    1142                 :            : 
    1143                 :          0 :     LOCK(wallet->cs_wallet);
    1144                 :          0 :     auto result = SelectCoins(*wallet, available_coins, /*pre_set_inputs=*/ {}, target, cc, cs_params);
    1145                 :          0 :     if (result) {
    1146                 :          0 :         const auto signedTxSize = 10 + 34 + 68 * result->GetInputSet().size(); // static header size + output size + inputs size (P2WPKH)
    1147                 :          0 :         BOOST_CHECK_LE(signedTxSize * WITNESS_SCALE_FACTOR, MAX_STANDARD_TX_WEIGHT);
    1148                 :            : 
    1149                 :          0 :         BOOST_CHECK_GE(result->GetSelectedValue(), target);
    1150                 :          0 :     }
    1151                 :          0 :     return result;
    1152                 :          0 : }
    1153                 :            : 
    1154                 :          0 : static bool has_coin(const CoinSet& set, CAmount amount)
    1155                 :            : {
    1156                 :          0 :     return std::any_of(set.begin(), set.end(), [&](const auto& coin) { return coin->GetEffectiveValue() == amount; });
    1157                 :            : }
    1158                 :            : 
    1159                 :          0 : BOOST_AUTO_TEST_CASE(check_max_weight)
    1160                 :            : {
    1161                 :          0 :     const CAmount target = 49.5L * COIN;
    1162                 :          0 :     CCoinControl cc;
    1163                 :            : 
    1164                 :          0 :     FastRandomContext rand;
    1165                 :          0 :     CoinSelectionParams cs_params{
    1166                 :            :         rand,
    1167                 :            :         /*change_output_size=*/34,
    1168                 :            :         /*change_spend_size=*/68,
    1169                 :            :         /*min_change_target=*/CENT,
    1170                 :          0 :         /*effective_feerate=*/CFeeRate(0),
    1171                 :          0 :         /*long_term_feerate=*/CFeeRate(0),
    1172                 :          0 :         /*discard_feerate=*/CFeeRate(0),
    1173                 :            :         /*tx_noinputs_size=*/10 + 34, // static header size + output size
    1174                 :            :         /*avoid_partial=*/false,
    1175                 :            :     };
    1176                 :            : 
    1177                 :            :     {
    1178                 :            :         // Scenario 1:
    1179                 :            :         // The actor starts with 1x 50.0 BTC and 1515x 0.033 BTC (~100.0 BTC total) unspent outputs
    1180                 :            :         // Then tries to spend 49.5 BTC
    1181                 :            :         // The 50.0 BTC output should be selected, because the transaction would otherwise be too large
    1182                 :            : 
    1183                 :            :         // Perform selection
    1184                 :            : 
    1185                 :          0 :         const auto result = select_coins(
    1186                 :          0 :             target, cs_params, cc, [&](CWallet& wallet) {
    1187                 :          0 :                 CoinsResult available_coins;
    1188                 :          0 :                 for (int j = 0; j < 1515; ++j) {
    1189                 :          0 :                     add_coin(available_coins, wallet, CAmount(0.033 * COIN), CFeeRate(0), 144, false, 0, true);
    1190                 :          0 :                 }
    1191                 :            : 
    1192                 :          0 :                 add_coin(available_coins, wallet, CAmount(50 * COIN), CFeeRate(0), 144, false, 0, true);
    1193                 :          0 :                 return available_coins;
    1194                 :          0 :             },
    1195                 :          0 :             m_node);
    1196                 :            : 
    1197                 :          0 :         BOOST_CHECK(result);
    1198                 :            :         // Verify that only the 50 BTC UTXO was selected
    1199                 :          0 :         const auto& selection_res = result->GetInputSet();
    1200                 :          0 :         BOOST_CHECK(selection_res.size() == 1);
    1201                 :          0 :         BOOST_CHECK((*selection_res.begin())->GetEffectiveValue() == 50 * COIN);
    1202                 :          0 :     }
    1203                 :            : 
    1204                 :            :     {
    1205                 :            :         // Scenario 2:
    1206                 :            : 
    1207                 :            :         // The actor starts with 400x 0.0625 BTC and 2000x 0.025 BTC (75.0 BTC total) unspent outputs
    1208                 :            :         // Then tries to spend 49.5 BTC
    1209                 :            :         // A combination of coins should be selected, such that the created transaction is not too large
    1210                 :            : 
    1211                 :            :         // Perform selection
    1212                 :          0 :         const auto result = select_coins(
    1213                 :          0 :             target, cs_params, cc, [&](CWallet& wallet) {
    1214                 :          0 :                 CoinsResult available_coins;
    1215                 :          0 :                 for (int j = 0; j < 400; ++j) {
    1216                 :          0 :                     add_coin(available_coins, wallet, CAmount(0.0625 * COIN), CFeeRate(0), 144, false, 0, true);
    1217                 :          0 :                 }
    1218                 :          0 :                 for (int j = 0; j < 2000; ++j) {
    1219                 :          0 :                     add_coin(available_coins, wallet, CAmount(0.025 * COIN), CFeeRate(0), 144, false, 0, true);
    1220                 :          0 :                 }
    1221                 :          0 :                 return available_coins;
    1222                 :          0 :             },
    1223                 :          0 :             m_node);
    1224                 :            : 
    1225                 :          0 :         BOOST_CHECK(has_coin(result->GetInputSet(), CAmount(0.0625 * COIN)));
    1226                 :          0 :         BOOST_CHECK(has_coin(result->GetInputSet(), CAmount(0.025 * COIN)));
    1227                 :          0 :     }
    1228                 :            : 
    1229                 :            :     {
    1230                 :            :         // Scenario 3:
    1231                 :            : 
    1232                 :            :         // The actor starts with 1515x 0.033 BTC (49.995 BTC total) unspent outputs
    1233                 :            :         // No results should be returned, because the transaction would be too large
    1234                 :            : 
    1235                 :            :         // Perform selection
    1236                 :          0 :         const auto result = select_coins(
    1237                 :          0 :             target, cs_params, cc, [&](CWallet& wallet) {
    1238                 :          0 :                 CoinsResult available_coins;
    1239                 :          0 :                 for (int j = 0; j < 1515; ++j) {
    1240                 :          0 :                     add_coin(available_coins, wallet, CAmount(0.033 * COIN), CFeeRate(0), 144, false, 0, true);
    1241                 :          0 :                 }
    1242                 :          0 :                 return available_coins;
    1243                 :          0 :             },
    1244                 :          0 :             m_node);
    1245                 :            : 
    1246                 :            :         // No results
    1247                 :            :         // 1515 inputs * 68 bytes = 103,020 bytes
    1248                 :            :         // 103,020 bytes * 4 = 412,080 weight, which is above the MAX_STANDARD_TX_WEIGHT of 400,000
    1249                 :          0 :         BOOST_CHECK(!result);
    1250                 :          0 :     }
    1251                 :          0 : }
    1252                 :            : 
    1253                 :          0 : BOOST_AUTO_TEST_CASE(SelectCoins_effective_value_test)
    1254                 :            : {
    1255                 :            :     // Test that the effective value is used to check whether preset inputs provide sufficient funds when subtract_fee_outputs is not used.
    1256                 :            :     // This test creates a coin whose value is higher than the target but whose effective value is lower than the target.
    1257                 :            :     // The coin is selected using coin control, with m_allow_other_inputs = false. SelectCoins should fail due to insufficient funds.
    1258                 :            : 
    1259                 :          0 :     std::unique_ptr<CWallet> wallet = NewWallet(m_node);
    1260                 :            : 
    1261                 :          0 :     CoinsResult available_coins;
    1262                 :            :     {
    1263                 :          0 :         std::unique_ptr<CWallet> dummyWallet = NewWallet(m_node, /*wallet_name=*/"dummy");
    1264                 :          0 :         add_coin(available_coins, *dummyWallet, 100000); // 0.001 BTC
    1265                 :          0 :     }
    1266                 :            : 
    1267                 :          0 :     CAmount target{99900}; // 0.000999 BTC
    1268                 :            : 
    1269                 :          0 :     FastRandomContext rand;
    1270                 :          0 :     CoinSelectionParams cs_params{
    1271                 :            :         rand,
    1272                 :            :         /*change_output_size=*/34,
    1273                 :            :         /*change_spend_size=*/148,
    1274                 :            :         /*min_change_target=*/1000,
    1275                 :          0 :         /*effective_feerate=*/CFeeRate(3000),
    1276                 :          0 :         /*long_term_feerate=*/CFeeRate(1000),
    1277                 :          0 :         /*discard_feerate=*/CFeeRate(1000),
    1278                 :            :         /*tx_noinputs_size=*/0,
    1279                 :            :         /*avoid_partial=*/false,
    1280                 :            :     };
    1281                 :          0 :     CCoinControl cc;
    1282                 :          0 :     cc.m_allow_other_inputs = false;
    1283                 :          0 :     COutput output = available_coins.All().at(0);
    1284                 :          0 :     cc.SetInputWeight(output.outpoint, 148);
    1285                 :          0 :     cc.SelectExternal(output.outpoint, output.txout);
    1286                 :            : 
    1287                 :          0 :     LOCK(wallet->cs_wallet);
    1288                 :          0 :     const auto preset_inputs = *Assert(FetchSelectedInputs(*wallet, cc, cs_params));
    1289                 :          0 :     available_coins.Erase({available_coins.coins[OutputType::BECH32].begin()->outpoint});
    1290                 :            : 
    1291                 :          0 :     const auto result = SelectCoins(*wallet, available_coins, preset_inputs, target, cc, cs_params);
    1292                 :          0 :     BOOST_CHECK(!result);
    1293                 :          0 : }
    1294                 :            : 
    1295                 :          0 : BOOST_FIXTURE_TEST_CASE(wallet_coinsresult_test, BasicTestingSetup)
    1296                 :            : {
    1297                 :            :     // Test case to verify CoinsResult object sanity.
    1298                 :          0 :     CoinsResult available_coins;
    1299                 :            :     {
    1300                 :          0 :         std::unique_ptr<CWallet> dummyWallet = NewWallet(m_node, /*wallet_name=*/"dummy");
    1301                 :            : 
    1302                 :            :         // Add some coins to 'available_coins'
    1303                 :          0 :         for (int i=0; i<10; i++) {
    1304                 :          0 :             add_coin(available_coins, *dummyWallet, 1 * COIN);
    1305                 :          0 :         }
    1306                 :          0 :     }
    1307                 :            : 
    1308                 :            :     {
    1309                 :            :         // First test case, check that 'CoinsResult::Erase' function works as expected.
    1310                 :            :         // By trying to erase two elements from the 'available_coins' object.
    1311                 :          0 :         std::unordered_set<COutPoint, SaltedOutpointHasher> outs_to_remove;
    1312                 :          0 :         const auto& coins = available_coins.All();
    1313                 :          0 :         for (int i = 0; i < 2; i++) {
    1314                 :          0 :             outs_to_remove.emplace(coins[i].outpoint);
    1315                 :          0 :         }
    1316                 :          0 :         available_coins.Erase(outs_to_remove);
    1317                 :            : 
    1318                 :            :         // Check that the elements were actually removed.
    1319                 :          0 :         const auto& updated_coins = available_coins.All();
    1320                 :          0 :         for (const auto& out: outs_to_remove) {
    1321                 :          0 :             auto it = std::find_if(updated_coins.begin(), updated_coins.end(), [&out](const COutput &coin) {
    1322                 :          0 :                 return coin.outpoint == out;
    1323                 :            :             });
    1324                 :          0 :             BOOST_CHECK(it == updated_coins.end());
    1325                 :            :         }
    1326                 :            :         // And verify that no extra element were removed
    1327                 :          0 :         BOOST_CHECK_EQUAL(available_coins.Size(), 8);
    1328                 :          0 :     }
    1329                 :          0 : }
    1330                 :            : 
    1331                 :          0 : BOOST_AUTO_TEST_SUITE_END()
    1332                 :            : } // namespace wallet

Generated by: LCOV version 1.14