|
5 | 5 | #include <wallet/coinselection.h> |
6 | 6 |
|
7 | 7 | #include <policy/feerate.h> |
| 8 | +#include <util/check.h> |
8 | 9 | #include <util/system.h> |
9 | 10 | #include <util/moneystr.h> |
10 | 11 |
|
| 12 | +#include <numeric> |
11 | 13 | #include <optional> |
12 | 14 |
|
13 | 15 | // Descending order comparator |
@@ -168,6 +170,30 @@ bool SelectCoinsBnB(std::vector<OutputGroup>& utxo_pool, const CAmount& selectio |
168 | 170 | return true; |
169 | 171 | } |
170 | 172 |
|
| 173 | +std::optional<std::pair<std::set<CInputCoin>, CAmount>> SelectCoinsSRD(const std::vector<OutputGroup>& utxo_pool, CAmount target_value) |
| 174 | +{ |
| 175 | + std::set<CInputCoin> out_set; |
| 176 | + CAmount value_ret = 0; |
| 177 | + |
| 178 | + std::vector<size_t> indexes; |
| 179 | + indexes.resize(utxo_pool.size()); |
| 180 | + std::iota(indexes.begin(), indexes.end(), 0); |
| 181 | + Shuffle(indexes.begin(), indexes.end(), FastRandomContext()); |
| 182 | + |
| 183 | + CAmount selected_eff_value = 0; |
| 184 | + for (const size_t i : indexes) { |
| 185 | + const OutputGroup& group = utxo_pool.at(i); |
| 186 | + Assume(group.GetSelectionAmount() > 0); |
| 187 | + selected_eff_value += group.GetSelectionAmount(); |
| 188 | + value_ret += group.m_value; |
| 189 | + util::insert(out_set, group.m_outputs); |
| 190 | + if (selected_eff_value >= target_value) { |
| 191 | + return std::make_pair(out_set, value_ret); |
| 192 | + } |
| 193 | + } |
| 194 | + return std::nullopt; |
| 195 | +} |
| 196 | + |
171 | 197 | static void ApproximateBestSubset(const std::vector<OutputGroup>& groups, const CAmount& nTotalLower, const CAmount& nTargetValue, |
172 | 198 | std::vector<char>& vfBest, CAmount& nBest, int iterations = 1000) |
173 | 199 | { |
|
0 commit comments