10 #include <boost/optional.hpp> 63 static const size_t TOTAL_TRIES = 100000;
70 std::vector<bool> curr_selection;
71 curr_selection.reserve(utxo_pool.size());
72 CAmount actual_target = not_input_fees + target_value;
75 CAmount curr_available_value = 0;
78 assert(utxo.effective_value > 0);
79 curr_available_value += utxo.effective_value;
81 if (curr_available_value < actual_target) {
86 std::sort(utxo_pool.begin(), utxo_pool.end(),
descending);
89 std::vector<bool> best_selection;
93 for (
size_t i = 0; i < TOTAL_TRIES; ++i) {
95 bool backtrack =
false;
96 if (curr_value + curr_available_value < actual_target ||
97 curr_value > actual_target + cost_of_change ||
98 (curr_waste > best_waste && (utxo_pool.at(0).fee - utxo_pool.at(0).long_term_fee) > 0)) {
100 }
else if (curr_value >= actual_target) {
101 curr_waste += (curr_value - actual_target);
106 if (curr_waste <= best_waste) {
107 best_selection = curr_selection;
108 best_selection.resize(utxo_pool.size());
109 best_waste = curr_waste;
111 curr_waste -= (curr_value - actual_target);
118 while (!curr_selection.empty() && !curr_selection.back()) {
119 curr_selection.pop_back();
120 curr_available_value += utxo_pool.at(curr_selection.size()).effective_value;
123 if (curr_selection.empty()) {
128 curr_selection.back() =
false;
129 OutputGroup& utxo = utxo_pool.at(curr_selection.size() - 1);
133 OutputGroup& utxo = utxo_pool.at(curr_selection.size());
140 if (!curr_selection.empty() && !curr_selection.back() &&
141 utxo.
effective_value == utxo_pool.at(curr_selection.size() - 1).effective_value &&
142 utxo.
fee == utxo_pool.at(curr_selection.size() - 1).fee) {
143 curr_selection.push_back(
false);
146 curr_selection.push_back(
true);
154 if (best_selection.empty()) {
160 for (
size_t i = 0; i < best_selection.size(); ++i) {
161 if (best_selection.at(i)) {
163 value_ret += utxo_pool.at(i).m_value;
170 static void ApproximateBestSubset(
const std::vector<OutputGroup>& groups,
const CAmount& nTotalLower,
const CAmount& nTargetValue,
171 std::vector<char>& vfBest,
CAmount& nBest,
int iterations = 1000)
173 std::vector<char> vfIncluded;
175 vfBest.assign(groups.size(),
true);
180 for (
int nRep = 0; nRep < iterations && nBest != nTargetValue; nRep++)
182 vfIncluded.assign(groups.size(),
false);
184 bool fReachedTarget =
false;
185 for (
int nPass = 0; nPass < 2 && !fReachedTarget; nPass++)
187 for (
unsigned int i = 0; i < groups.size(); i++)
195 if (nPass == 0 ? insecure_rand.
randbool() : !vfIncluded[i])
197 nTotal += groups[i].m_value;
198 vfIncluded[i] =
true;
199 if (nTotal >= nTargetValue)
201 fReachedTarget =
true;
207 nTotal -= groups[i].m_value;
208 vfIncluded[i] =
false;
222 boost::optional<OutputGroup> lowest_larger;
223 std::vector<OutputGroup> applicable_groups;
226 random_shuffle(groups.begin(), groups.end(),
GetRandInt);
229 if (group.m_value == nTargetValue) {
231 nValueRet += group.m_value;
233 }
else if (group.m_value < nTargetValue + MIN_CHANGE) {
234 applicable_groups.push_back(group);
235 nTotalLower += group.m_value;
236 }
else if (!lowest_larger || group.m_value < lowest_larger->m_value) {
237 lowest_larger = group;
241 if (nTotalLower == nTargetValue) {
242 for (
const auto& group : applicable_groups) {
244 nValueRet += group.m_value;
249 if (nTotalLower < nTargetValue) {
250 if (!lowest_larger)
return false;
252 nValueRet += lowest_larger->m_value;
257 std::sort(applicable_groups.begin(), applicable_groups.end(),
descending);
258 std::vector<char> vfBest;
261 ApproximateBestSubset(applicable_groups, nTotalLower, nTargetValue, vfBest, nBest);
262 if (nBest != nTargetValue && nTotalLower >= nTargetValue + MIN_CHANGE) {
263 ApproximateBestSubset(applicable_groups, nTotalLower, nTargetValue + MIN_CHANGE, vfBest, nBest);
269 ((nBest != nTargetValue && nBest < nTargetValue + MIN_CHANGE) || lowest_larger->m_value <= nBest)) {
271 nValueRet += lowest_larger->m_value;
273 for (
unsigned int i = 0; i < applicable_groups.size(); i++) {
275 util::insert(setCoinsRet, applicable_groups[i].m_outputs);
276 nValueRet += applicable_groups[i].m_value;
282 for (
unsigned int i = 0; i < applicable_groups.size(); i++) {
bool SelectCoinsBnB(std::vector< OutputGroup > &utxo_pool, const CAmount &target_value, const CAmount &cost_of_change, std::set< CInputCoin > &out_set, CAmount &value_ret, CAmount not_input_fees)
void insert(Tdst &dst, const Tsrc &src)
Simplification of std insertion.
const uint64_t max_descendants
std::vector< CInputCoin >::iterator Discard(const CInputCoin &output)
int64_t CAmount
Amount in satoshis (Can be negative)
void Insert(const CInputCoin &output, int depth, bool from_me, size_t ancestors, size_t descendants)
const uint64_t max_ancestors
std::vector< CInputCoin > m_outputs
std::string FormatMoney(const CAmount &n)
Money parsing/formatting utilities.
bool randbool()
Generate a random boolean.
bool EligibleForSpending(const CoinEligibilityFilter &eligibility_filter) const
bool KnapsackSolver(const CAmount &nTargetValue, std::vector< OutputGroup > &groups, std::set< CInputCoin > &setCoinsRet, CAmount &nValueRet)