Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/coinselection.cpp
// Copyright (c) 2017 The Bitcoin Core developers | // Copyright (c) 2017 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <wallet/coinselection.h> | #include <wallet/coinselection.h> | ||||
#include <optional.h> | |||||
#include <util/moneystr.h> | #include <util/moneystr.h> | ||||
#include <util/system.h> | #include <util/system.h> | ||||
// Descending order comparator | // Descending order comparator | ||||
struct { | struct { | ||||
bool operator()(const OutputGroup &a, const OutputGroup &b) const { | bool operator()(const OutputGroup &a, const OutputGroup &b) const { | ||||
return a.effective_value > b.effective_value; | return a.effective_value > b.effective_value; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 235 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
bool KnapsackSolver(const Amount nTargetValue, std::vector<OutputGroup> &groups, | bool KnapsackSolver(const Amount nTargetValue, std::vector<OutputGroup> &groups, | ||||
std::set<CInputCoin> &setCoinsRet, Amount &nValueRet) { | std::set<CInputCoin> &setCoinsRet, Amount &nValueRet) { | ||||
setCoinsRet.clear(); | setCoinsRet.clear(); | ||||
nValueRet = Amount::zero(); | nValueRet = Amount::zero(); | ||||
// List of values less than target | // List of values less than target | ||||
Optional<OutputGroup> lowest_larger; | std::optional<OutputGroup> lowest_larger; | ||||
std::vector<OutputGroup> applicable_groups; | std::vector<OutputGroup> applicable_groups; | ||||
Amount nTotalLower = Amount::zero(); | Amount nTotalLower = Amount::zero(); | ||||
Shuffle(groups.begin(), groups.end(), FastRandomContext()); | Shuffle(groups.begin(), groups.end(), FastRandomContext()); | ||||
for (const OutputGroup &group : groups) { | for (const OutputGroup &group : groups) { | ||||
if (group.m_value == nTargetValue) { | if (group.m_value == nTargetValue) { | ||||
util::insert(setCoinsRet, group.m_outputs); | util::insert(setCoinsRet, group.m_outputs); | ||||
▲ Show 20 Lines • Show All 118 Lines • Show Last 20 Lines |