LCOV - code coverage report
Current view: top level - src/wallet/test/fuzz - coinselection.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 100.0 % 242 242
Test Date: 2024-09-01 05:20:30 Functions: 100.0 % 11 11
Branches: 58.8 % 558 328

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 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 <policy/feerate.h>
       6                 :             : #include <policy/policy.h>
       7                 :             : #include <primitives/transaction.h>
       8                 :             : #include <test/fuzz/FuzzedDataProvider.h>
       9                 :             : #include <test/fuzz/fuzz.h>
      10                 :             : #include <test/fuzz/util.h>
      11                 :             : #include <test/util/setup_common.h>
      12                 :             : #include <wallet/coinselection.h>
      13                 :             : 
      14                 :             : #include <numeric>
      15                 :             : #include <vector>
      16                 :             : 
      17                 :             : namespace wallet {
      18                 :             : 
      19                 :      682199 : static void AddCoin(const CAmount& value, int n_input, int n_input_bytes, int locktime, std::vector<COutput>& coins, CFeeRate fee_rate)
      20                 :             : {
      21                 :      682199 :     CMutableTransaction tx;
      22         [ +  - ]:      682199 :     tx.vout.resize(n_input + 1);
      23                 :      682199 :     tx.vout[n_input].nValue = value;
      24                 :      682199 :     tx.nLockTime = locktime; // all transactions get different hashes
      25   [ +  -  +  -  :      682199 :     coins.emplace_back(COutPoint(tx.GetHash(), n_input), tx.vout.at(n_input), /*depth=*/0, n_input_bytes, /*spendable=*/true, /*solvable=*/true, /*safe=*/true, /*time=*/0, /*from_me=*/true, fee_rate);
             +  -  +  - ]
      26                 :      682199 : }
      27                 :             : 
      28                 :             : // Randomly distribute coins to instances of OutputGroup
      29                 :        3014 : static void GroupCoins(FuzzedDataProvider& fuzzed_data_provider, const std::vector<COutput>& coins, const CoinSelectionParams& coin_params, bool positive_only, std::vector<OutputGroup>& output_groups)
      30                 :             : {
      31                 :        3014 :     auto output_group = OutputGroup(coin_params);
      32                 :        3014 :     bool valid_outputgroup{false};
      33         [ +  + ]:     1159883 :     for (auto& coin : coins) {
      34   [ +  +  +  -  :     1156869 :         if (!positive_only || (positive_only && coin.GetEffectiveValue() > 0)) {
             +  -  +  + ]
      35   [ +  -  +  - ]:     1083974 :             output_group.Insert(std::make_shared<COutput>(coin), /*ancestors=*/0, /*descendants=*/0);
      36                 :     1083974 :         }
      37                 :             :         // If positive_only was specified, nothing was inserted, leading to an empty output group
      38                 :             :         // that would be invalid for the BnB algorithm
      39   [ +  +  +  - ]:     1156869 :         valid_outputgroup = !positive_only || output_group.GetSelectionAmount() > 0;
      40   [ +  +  +  -  :     1156869 :         if (valid_outputgroup && fuzzed_data_provider.ConsumeBool()) {
                   +  + ]
      41         [ +  - ]:      562453 :             output_groups.push_back(output_group);
      42         [ +  - ]:      562453 :             output_group = OutputGroup(coin_params);
      43                 :      562453 :             valid_outputgroup = false;
      44                 :      562453 :         }
      45                 :     1156869 :     }
      46   [ +  +  +  - ]:        3014 :     if (valid_outputgroup) output_groups.push_back(output_group);
      47                 :        3014 : }
      48                 :             : 
      49                 :        3084 : static CAmount CreateCoins(FuzzedDataProvider& fuzzed_data_provider, std::vector<COutput>& utxo_pool, CoinSelectionParams& coin_params, int& next_locktime)
      50                 :             : {
      51                 :        3084 :     CAmount total_balance{0};
      52   [ +  +  +  + ]:      550328 :     LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000)
      53                 :             :     {
      54                 :      547244 :         const int n_input{fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 10)};
      55                 :      547244 :         const int n_input_bytes{fuzzed_data_provider.ConsumeIntegralInRange<int>(41, 10000)};
      56                 :      547244 :         const CAmount amount{fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(1, MAX_MONEY)};
      57         [ +  + ]:      547244 :         if (total_balance + amount >= MAX_MONEY) {
      58                 :         481 :             break;
      59                 :             :         }
      60                 :      546763 :         AddCoin(amount, n_input, n_input_bytes, ++next_locktime, utxo_pool, coin_params.m_effective_feerate);
      61                 :      546763 :         total_balance += amount;
      62         [ +  + ]:      547244 :     }
      63                 :             : 
      64                 :        6168 :     return total_balance;
      65                 :        3084 : }
      66                 :             : 
      67                 :         151 : static SelectionResult ManualSelection(std::vector<COutput>& utxos, const CAmount& total_amount, const bool& subtract_fee_outputs)
      68                 :             : {
      69                 :         151 :     SelectionResult result(total_amount, SelectionAlgorithm::MANUAL);
      70                 :         151 :     std::set<std::shared_ptr<COutput>> utxo_pool;
      71         [ +  + ]:       11411 :     for (const auto& utxo : utxos) {
      72   [ +  -  +  - ]:       11260 :         utxo_pool.insert(std::make_shared<COutput>(utxo));
      73                 :       11260 :     }
      74         [ +  - ]:         151 :     result.AddInputs(utxo_pool, subtract_fee_outputs);
      75                 :         151 :     return result;
      76         [ +  - ]:         151 : }
      77                 :             : 
      78                 :             : // Returns true if the result contains an error and the message is not empty
      79         [ +  - ]:        1205 : static bool HasErrorMsg(const util::Result<SelectionResult>& res) { return !util::ErrorString(res).empty(); }
      80                 :             : 
      81   [ +  -  +  - ]:         961 : FUZZ_TARGET(coin_grinder)
      82                 :             : {
      83                 :         958 :     FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
      84                 :         958 :     std::vector<COutput> utxo_pool;
      85                 :             : 
      86         [ +  - ]:         958 :     const CAmount target{fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(1, MAX_MONEY)};
      87                 :             : 
      88                 :         958 :     FastRandomContext fast_random_context{ConsumeUInt256(fuzzed_data_provider)};
      89         [ +  - ]:         958 :     CoinSelectionParams coin_params{fast_random_context};
      90         [ +  - ]:         958 :     coin_params.m_subtract_fee_outputs = fuzzed_data_provider.ConsumeBool();
      91         [ +  - ]:         958 :     coin_params.m_long_term_feerate = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
      92         [ +  - ]:         958 :     coin_params.m_effective_feerate = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
      93         [ +  - ]:         958 :     coin_params.change_output_size = fuzzed_data_provider.ConsumeIntegralInRange<int>(10, 1000);
      94         [ +  - ]:         958 :     coin_params.change_spend_size = fuzzed_data_provider.ConsumeIntegralInRange<int>(10, 1000);
      95   [ +  -  +  - ]:         958 :     coin_params.m_cost_of_change= coin_params.m_effective_feerate.GetFee(coin_params.change_output_size) + coin_params.m_long_term_feerate.GetFee(coin_params.change_spend_size);
      96         [ +  - ]:         958 :     coin_params.m_change_fee = coin_params.m_effective_feerate.GetFee(coin_params.change_output_size);
      97                 :             :     // For other results to be comparable to SRD, we must align the change_target with SRD’s hardcoded behavior
      98                 :         958 :     coin_params.m_min_change_target = CHANGE_LOWER + coin_params.m_change_fee;
      99                 :             : 
     100                 :             :     // Create some coins
     101                 :         958 :     CAmount total_balance{0};
     102                 :         958 :     CAmount max_spendable{0};
     103                 :         958 :     int next_locktime{0};
     104   [ -  +  +  +  :      133962 :     LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000)
                   +  + ]
     105                 :             :     {
     106         [ +  - ]:      133004 :         const int n_input{fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 10)};
     107         [ +  - ]:      133004 :         const int n_input_bytes{fuzzed_data_provider.ConsumeIntegralInRange<int>(41, 10000)};
     108         [ +  - ]:      133004 :         const CAmount amount{fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(1, MAX_MONEY)};
     109         [ +  + ]:      133004 :         if (total_balance + amount >= MAX_MONEY) {
     110                 :         487 :             break;
     111                 :             :         }
     112         [ +  - ]:      132517 :         AddCoin(amount, n_input, n_input_bytes, ++next_locktime, utxo_pool, coin_params.m_effective_feerate);
     113                 :      132517 :         total_balance += amount;
     114         [ +  - ]:      132517 :         CAmount eff_value = amount - coin_params.m_effective_feerate.GetFee(n_input_bytes);
     115                 :      132517 :         max_spendable += eff_value;
     116         [ +  + ]:      133004 :     }
     117                 :             : 
     118                 :         958 :     std::vector<OutputGroup> group_pos;
     119         [ +  - ]:         958 :     GroupCoins(fuzzed_data_provider, utxo_pool, coin_params, /*positive_only=*/true, group_pos);
     120                 :             : 
     121                 :             :     // Run coinselection algorithms
     122         [ +  - ]:         958 :     auto result_cg = CoinGrinder(group_pos, target, coin_params.m_min_change_target, MAX_STANDARD_TX_WEIGHT);
     123   [ +  +  +  -  :         958 :     if (target + coin_params.m_min_change_target > max_spendable || HasErrorMsg(result_cg)) return; // We only need to compare algorithms if CoinGrinder has a solution
                   +  + ]
     124         [ +  - ]:         854 :     assert(result_cg);
     125   [ +  -  +  -  :         854 :     if (!result_cg->GetAlgoCompleted()) return; // Bail out if CoinGrinder solution is not optimal
                   +  + ]
     126                 :             : 
     127         [ +  - ]:         851 :     auto result_srd = SelectCoinsSRD(group_pos, target, coin_params.m_change_fee, fast_random_context, MAX_STANDARD_TX_WEIGHT);
     128   [ +  +  +  -  :         851 :     if (result_srd && result_srd->GetChange(CHANGE_LOWER, coin_params.m_change_fee) > 0) { // exclude any srd solutions that don’t have change, err on excluding
             +  -  +  - ]
     129   [ +  -  +  -  :         799 :         assert(result_srd->GetWeight() >= result_cg->GetWeight());
          +  -  +  -  +  
                      - ]
     130                 :         799 :     }
     131                 :             : 
     132         [ +  - ]:         851 :     auto result_knapsack = KnapsackSolver(group_pos, target, coin_params.m_min_change_target, fast_random_context, MAX_STANDARD_TX_WEIGHT);
     133   [ +  +  +  -  :         851 :     if (result_knapsack && result_knapsack->GetChange(CHANGE_LOWER, coin_params.m_change_fee) > 0) { // exclude any knapsack solutions that don’t have change, err on excluding
             +  -  +  + ]
     134   [ +  -  +  -  :         736 :         assert(result_knapsack->GetWeight() >= result_cg->GetWeight());
          +  -  +  -  +  
                      - ]
     135                 :         736 :     }
     136         [ -  + ]:         958 : }
     137                 :             : 
     138   [ +  -  +  - ]:         314 : FUZZ_TARGET(coin_grinder_is_optimal)
     139                 :             : {
     140                 :         311 :     FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
     141                 :             : 
     142                 :         311 :     FastRandomContext fast_random_context{ConsumeUInt256(fuzzed_data_provider)};
     143         [ +  - ]:         311 :     CoinSelectionParams coin_params{fast_random_context};
     144                 :         311 :     coin_params.m_subtract_fee_outputs = false;
     145                 :             :     // Set effective feerate up to MAX_MONEY sats per 1'000'000 vB (2'100'000'000 sat/vB = 21'000 BTC/kvB).
     146         [ +  - ]:         311 :     coin_params.m_effective_feerate = CFeeRate{ConsumeMoney(fuzzed_data_provider, MAX_MONEY), 1'000'000};
     147                 :         311 :     coin_params.m_min_change_target = ConsumeMoney(fuzzed_data_provider);
     148                 :             : 
     149                 :             :     // Create some coins
     150                 :         311 :     CAmount max_spendable{0};
     151                 :         311 :     int next_locktime{0};
     152                 :             :     static constexpr unsigned max_output_groups{16};
     153                 :         311 :     std::vector<OutputGroup> group_pos;
     154   [ -  +  +  +  :        3230 :     LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), max_output_groups)
                   +  + ]
     155                 :             :     {
     156                 :             :         // With maximum m_effective_feerate and n_input_bytes = 1'000'000, input_fee <= MAX_MONEY.
     157         [ -  + ]:        2919 :         const int n_input_bytes{fuzzed_data_provider.ConsumeIntegralInRange<int>(1, 1'000'000)};
     158                 :             :         // Only make UTXOs with positive effective value
     159         [ -  + ]:        2919 :         const CAmount input_fee = coin_params.m_effective_feerate.GetFee(n_input_bytes);
     160                 :             :         // Ensure that each UTXO has at least an effective value of 1 sat
     161         [ -  + ]:        2919 :         const CAmount eff_value{fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(1, MAX_MONEY + group_pos.size() - max_spendable - max_output_groups)};
     162                 :        2919 :         const CAmount amount{eff_value + input_fee};
     163                 :        2919 :         std::vector<COutput> temp_utxo_pool;
     164                 :             : 
     165         [ -  + ]:        2919 :         AddCoin(amount, /*n_input=*/0, n_input_bytes, ++next_locktime, temp_utxo_pool, coin_params.m_effective_feerate);
     166                 :        2919 :         max_spendable += eff_value;
     167                 :             : 
     168         [ -  + ]:        2919 :         auto output_group = OutputGroup(coin_params);
     169   [ +  -  +  -  :        2919 :         output_group.Insert(std::make_shared<COutput>(temp_utxo_pool.at(0)), /*ancestors=*/0, /*descendants=*/0);
                   -  + ]
     170         [ +  - ]:        2919 :         group_pos.push_back(output_group);
     171                 :        2919 :     }
     172                 :         311 :     size_t num_groups = group_pos.size();
     173         [ +  - ]:         311 :     assert(num_groups <= max_output_groups);
     174                 :             : 
     175                 :             :     // Only choose targets below max_spendable
     176   [ +  -  +  - ]:         311 :     const CAmount target{fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(1, std::max(CAmount{1}, max_spendable - coin_params.m_min_change_target))};
     177                 :             : 
     178                 :             :     // Brute force optimal solution
     179                 :         311 :     CAmount best_amount{MAX_MONEY};
     180                 :         311 :     int best_weight{std::numeric_limits<int>::max()};
     181         [ +  + ]:     6554347 :     for (uint32_t pattern = 1; (pattern >> num_groups) == 0; ++pattern) {
     182                 :     6554036 :         CAmount subset_amount{0};
     183                 :     6554036 :         int subset_weight{0};
     184         [ +  + ]:   109805375 :         for (unsigned i = 0; i < num_groups; ++i) {
     185         [ +  + ]:   103251339 :             if ((pattern >> i) & 1) {
     186         [ -  + ]:    51627129 :                 subset_amount += group_pos[i].GetSelectionAmount();
     187                 :    51627129 :                 subset_weight += group_pos[i].m_weight;
     188                 :    51627129 :             }
     189                 :   103251339 :         }
     190   [ +  +  +  +  :     6554036 :         if ((subset_amount >= target + coin_params.m_min_change_target) && (subset_weight < best_weight || (subset_weight == best_weight && subset_amount < best_amount))) {
             +  +  +  + ]
     191                 :        1839 :             best_weight = subset_weight;
     192                 :        1839 :             best_amount = subset_amount;
     193                 :        1839 :         }
     194                 :     6554036 :     }
     195                 :             : 
     196         [ +  + ]:         311 :     if (best_weight < std::numeric_limits<int>::max()) {
     197                 :             :         // Sufficient funds and acceptable weight: CoinGrinder should find at least one solution
     198         [ +  - ]:         270 :         int high_max_selection_weight = fuzzed_data_provider.ConsumeIntegralInRange<int>(best_weight, std::numeric_limits<int>::max());
     199                 :             : 
     200         [ +  - ]:         270 :         auto result_cg = CoinGrinder(group_pos, target, coin_params.m_min_change_target, high_max_selection_weight);
     201         [ +  - ]:         270 :         assert(result_cg);
     202   [ +  -  +  -  :         270 :         assert(result_cg->GetWeight() <= high_max_selection_weight);
                   +  - ]
     203   [ +  -  +  -  :         270 :         assert(result_cg->GetSelectedEffectiveValue() >= target + coin_params.m_min_change_target);
                   +  - ]
     204   [ +  -  +  -  :         270 :         assert(best_weight < result_cg->GetWeight() || (best_weight == result_cg->GetWeight() && best_amount <= result_cg->GetSelectedEffectiveValue()));
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     205   [ +  -  +  -  :         270 :         if (result_cg->GetAlgoCompleted()) {
                   +  - ]
     206                 :             :             // If CoinGrinder exhausted the search space, it must return the optimal solution
     207   [ +  -  +  -  :         270 :             assert(best_weight == result_cg->GetWeight());
                   +  - ]
     208   [ +  -  +  -  :         270 :             assert(best_amount == result_cg->GetSelectedEffectiveValue());
                   +  - ]
     209                 :         270 :         }
     210                 :         270 :     }
     211                 :             : 
     212                 :             :     // CoinGrinder cannot ever find a better solution than the brute-forced best, or there is none in the first place
     213         [ +  - ]:         311 :     int low_max_selection_weight = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, best_weight - 1);
     214         [ +  - ]:         311 :     auto result_cg = CoinGrinder(group_pos, target, coin_params.m_min_change_target, low_max_selection_weight);
     215                 :             :     // Max_weight should have been exceeded, or there were insufficient funds
     216         [ +  - ]:         311 :     assert(!result_cg);
     217                 :         311 : }
     218                 :             : 
     219   [ +  -  +  - ]:        1031 : FUZZ_TARGET(coinselection)
     220                 :             : {
     221                 :        1028 :     FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
     222                 :        1028 :     std::vector<COutput> utxo_pool;
     223                 :             : 
     224         [ +  - ]:        1028 :     const CFeeRate long_term_fee_rate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
     225         [ +  - ]:        1028 :     const CFeeRate effective_fee_rate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
     226                 :             :     // Discard feerate must be at least dust relay feerate
     227   [ +  -  +  - ]:        1028 :     const CFeeRate discard_fee_rate{fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(DUST_RELAY_TX_FEE, COIN)};
     228         [ +  - ]:        1028 :     const CAmount target{fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(1, MAX_MONEY)};
     229         [ +  - ]:        1028 :     const bool subtract_fee_outputs{fuzzed_data_provider.ConsumeBool()};
     230                 :             : 
     231                 :        1028 :     FastRandomContext fast_random_context{ConsumeUInt256(fuzzed_data_provider)};
     232         [ +  - ]:        1028 :     CoinSelectionParams coin_params{fast_random_context};
     233                 :        1028 :     coin_params.m_subtract_fee_outputs = subtract_fee_outputs;
     234                 :        1028 :     coin_params.m_long_term_feerate = long_term_fee_rate;
     235                 :        1028 :     coin_params.m_effective_feerate = effective_fee_rate;
     236         [ +  - ]:        1028 :     coin_params.change_output_size = fuzzed_data_provider.ConsumeIntegralInRange(1, MAX_SCRIPT_SIZE);
     237         [ +  - ]:        1028 :     coin_params.m_change_fee = effective_fee_rate.GetFee(coin_params.change_output_size);
     238                 :        1028 :     coin_params.m_discard_feerate = discard_fee_rate;
     239         [ +  - ]:        1028 :     coin_params.change_spend_size = fuzzed_data_provider.ConsumeIntegralInRange<int>(41, 1000);
     240         [ +  - ]:        1028 :     const auto change_spend_fee{coin_params.m_discard_feerate.GetFee(coin_params.change_spend_size)};
     241                 :        1028 :     coin_params.m_cost_of_change = coin_params.m_change_fee + change_spend_fee;
     242   [ +  -  +  -  :        1028 :     CScript change_out_script = CScript() << std::vector<unsigned char>(coin_params.change_output_size, OP_TRUE);
                   +  - ]
     243   [ +  -  +  -  :        1028 :     const auto dust{GetDustThreshold(CTxOut{/*nValueIn=*/0, change_out_script}, coin_params.m_discard_feerate)};
                   +  - ]
     244         [ +  - ]:        1028 :     coin_params.min_viable_change = std::max(change_spend_fee + 1, dust);
     245                 :             : 
     246                 :        1028 :     int next_locktime{0};
     247         [ +  - ]:        1028 :     CAmount total_balance{CreateCoins(fuzzed_data_provider, utxo_pool, coin_params, next_locktime)};
     248                 :             : 
     249                 :        1028 :     std::vector<OutputGroup> group_pos;
     250         [ +  - ]:        1028 :     GroupCoins(fuzzed_data_provider, utxo_pool, coin_params, /*positive_only=*/true, group_pos);
     251                 :        1028 :     std::vector<OutputGroup> group_all;
     252         [ +  - ]:        1028 :     GroupCoins(fuzzed_data_provider, utxo_pool, coin_params, /*positive_only=*/false, group_all);
     253                 :             : 
     254         [ +  + ]:      219091 :     for (const OutputGroup& group : group_all) {
     255   [ +  -  +  -  :      218063 :         const CoinEligibilityFilter filter(fuzzed_data_provider.ConsumeIntegral<int>(), fuzzed_data_provider.ConsumeIntegral<int>(), fuzzed_data_provider.ConsumeIntegral<uint64_t>());
             +  -  +  - ]
     256         [ +  - ]:      218063 :         (void)group.EligibleForSpending(filter);
     257                 :      218063 :     }
     258                 :             : 
     259         [ +  - ]:        1028 :     int max_selection_weight = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, std::numeric_limits<int>::max());
     260                 :             : 
     261                 :             :     // Run coinselection algorithms
     262   [ +  +  +  -  :        1690 :     auto result_bnb = coin_params.m_subtract_fee_outputs ? util::Error{Untranslated("BnB disabled when SFFO is enabled")} :
          +  -  +  -  +  
          +  +  +  +  +  
          +  +  #  #  #  
             #  #  #  #  
                      # ]
     263         [ +  - ]:         662 :                       SelectCoinsBnB(group_pos, target, coin_params.m_cost_of_change, max_selection_weight);
     264         [ +  + ]:        1028 :     if (result_bnb) {
     265   [ +  -  +  -  :         128 :         assert(result_bnb->GetChange(coin_params.min_viable_change, coin_params.m_change_fee) == 0);
                   +  - ]
     266   [ +  -  +  -  :         128 :         assert(result_bnb->GetSelectedValue() >= target);
                   +  - ]
     267   [ +  -  +  -  :         128 :         assert(result_bnb->GetWeight() <= max_selection_weight);
                   +  - ]
     268   [ +  -  +  - ]:         128 :         (void)result_bnb->GetShuffledInputVector();
     269   [ +  -  +  - ]:         128 :         (void)result_bnb->GetInputSet();
     270                 :         128 :     }
     271                 :             : 
     272         [ +  - ]:        1028 :     auto result_srd = SelectCoinsSRD(group_pos, target, coin_params.m_change_fee, fast_random_context, max_selection_weight);
     273         [ +  + ]:        1028 :     if (result_srd) {
     274   [ +  -  +  -  :         313 :         assert(result_srd->GetSelectedValue() >= target);
                   +  - ]
     275   [ +  -  +  -  :         313 :         assert(result_srd->GetChange(CHANGE_LOWER, coin_params.m_change_fee) > 0); // Demonstrate that SRD creates change of at least CHANGE_LOWER
                   +  - ]
     276   [ +  -  +  -  :         313 :         assert(result_srd->GetWeight() <= max_selection_weight);
                   +  - ]
     277   [ +  -  +  - ]:         313 :         result_srd->RecalculateWaste(coin_params.min_viable_change, coin_params.m_cost_of_change, coin_params.m_change_fee);
     278   [ +  -  +  - ]:         313 :         (void)result_srd->GetShuffledInputVector();
     279   [ +  -  +  - ]:         313 :         (void)result_srd->GetInputSet();
     280                 :         313 :     }
     281                 :             : 
     282         [ +  - ]:        1028 :     CAmount change_target{GenerateChangeTarget(target, coin_params.m_change_fee, fast_random_context)};
     283         [ +  - ]:        1028 :     auto result_knapsack = KnapsackSolver(group_all, target, change_target, fast_random_context, max_selection_weight);
     284         [ +  + ]:        1028 :     if (result_knapsack) {
     285   [ +  -  +  -  :         346 :         assert(result_knapsack->GetSelectedValue() >= target);
                   +  - ]
     286   [ +  -  +  -  :         346 :         assert(result_knapsack->GetWeight() <= max_selection_weight);
                   +  - ]
     287   [ +  -  +  - ]:         346 :         result_knapsack->RecalculateWaste(coin_params.min_viable_change, coin_params.m_cost_of_change, coin_params.m_change_fee);
     288   [ +  -  +  - ]:         346 :         (void)result_knapsack->GetShuffledInputVector();
     289   [ +  -  +  - ]:         346 :         (void)result_knapsack->GetInputSet();
     290                 :         346 :     }
     291                 :             : 
     292                 :             :     // If the total balance is sufficient for the target and we are not using
     293                 :             :     // effective values, Knapsack should always find a solution (unless the selection exceeded the max tx weight).
     294   [ +  +  +  +  :        1028 :     if (total_balance >= target && subtract_fee_outputs && !HasErrorMsg(result_knapsack)) {
             +  -  +  + ]
     295         [ +  - ]:         142 :         assert(result_knapsack);
     296                 :         142 :     }
     297                 :             : 
     298                 :        1028 :     std::vector<COutput> utxos;
     299                 :        1028 :     std::vector<util::Result<SelectionResult>> results;
     300         [ +  - ]:        1028 :     results.emplace_back(std::move(result_srd));
     301         [ +  - ]:        1028 :     results.emplace_back(std::move(result_knapsack));
     302         [ +  - ]:        1028 :     results.emplace_back(std::move(result_bnb));
     303         [ +  - ]:        1028 :     CAmount new_total_balance{CreateCoins(fuzzed_data_provider, utxos, coin_params, next_locktime)};
     304         [ +  + ]:        1028 :     if (new_total_balance > 0) {
     305                 :         239 :         std::set<std::shared_ptr<COutput>> new_utxo_pool;
     306         [ +  + ]:       23566 :         for (const auto& utxo : utxos) {
     307   [ +  -  +  - ]:       23327 :             new_utxo_pool.insert(std::make_shared<COutput>(utxo));
     308                 :       23327 :         }
     309         [ +  + ]:         956 :         for (auto& result : results) {
     310         [ +  + ]:         717 :             if (!result) continue;
     311   [ +  -  +  - ]:         470 :             const auto weight{result->GetWeight()};
     312   [ +  -  +  - ]:         470 :             result->AddInputs(new_utxo_pool, subtract_fee_outputs);
     313   [ +  -  +  -  :         470 :             assert(result->GetWeight() > weight);
                   -  + ]
     314         [ +  + ]:         717 :         }
     315                 :         239 :     }
     316                 :             : 
     317                 :        1028 :     std::vector<COutput> manual_inputs;
     318         [ +  - ]:        1028 :     CAmount manual_balance{CreateCoins(fuzzed_data_provider, manual_inputs, coin_params, next_locktime)};
     319         [ +  + ]:        1028 :     if (manual_balance == 0) return;
     320         [ +  - ]:         151 :     auto manual_selection{ManualSelection(manual_inputs, manual_balance, coin_params.m_subtract_fee_outputs)};
     321         [ +  + ]:         604 :     for (auto& result : results) {
     322         [ +  + ]:         453 :         if (!result) continue;
     323   [ +  -  +  - ]:         278 :         const CAmount old_target{result->GetTarget()};
     324   [ +  -  +  -  :         278 :         const std::set<std::shared_ptr<COutput>> input_set{result->GetInputSet()};
                   +  - ]
     325   [ +  -  +  - ]:         278 :         const int old_weight{result->GetWeight()};
     326   [ +  -  +  - ]:         278 :         result->Merge(manual_selection);
     327   [ +  -  +  -  :         278 :         assert(result->GetInputSet().size() == input_set.size() + manual_inputs.size());
                   -  + ]
     328   [ +  -  +  -  :         278 :         assert(result->GetTarget() == old_target + manual_selection.GetTarget());
             +  -  -  + ]
     329   [ +  -  +  -  :         278 :         assert(result->GetWeight() == old_weight + manual_selection.GetWeight());
             +  -  -  + ]
     330         [ +  + ]:         453 :     }
     331                 :        1028 : }
     332                 :             : 
     333                 :             : } // namespace wallet
        

Generated by: LCOV version 2.0-1