2021-12-30 19:36:57 +02:00
// Copyright (c) 2017-2021 The Bitcoin Core developers
2018-03-05 16:29:37 -05:00
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
# include <wallet/coinselection.h>
2018-08-13 16:13:29 -04:00
2021-09-11 10:29:00 +08:00
# include <consensus/amount.h>
2019-11-12 15:06:24 -05:00
# include <policy/feerate.h>
2019-11-06 14:35:11 -05:00
# include <util/check.h>
2018-10-22 15:51:11 -07:00
# include <util/system.h>
# include <util/moneystr.h>
2018-03-05 16:29:37 -05:00
2019-11-06 14:35:11 -05:00
# include <numeric>
2021-03-15 11:59:05 +08:00
# include <optional>
2021-11-12 11:13:29 -05:00
namespace wallet {
2018-03-05 16:29:37 -05:00
// Descending order comparator
struct {
2018-07-19 11:45:26 +09:00
bool operator ( ) ( const OutputGroup & a , const OutputGroup & b ) const
2018-03-05 16:29:37 -05:00
{
2021-04-23 15:14:41 -04:00
return a . GetSelectionAmount ( ) > b . GetSelectionAmount ( ) ;
2018-03-05 16:29:37 -05:00
}
} descending ;
/*
* This is the Branch and Bound Coin Selection algorithm designed by Murch . It searches for an input
* set that can pay for the spending target and does not exceed the spending target by more than the
* cost of creating and spending a change output . The algorithm uses a depth - first search on a binary
* tree . In the binary tree , each node corresponds to the inclusion or the omission of a UTXO . UTXOs
2021-10-11 08:26:07 -04:00
* are sorted by their effective values and the tree is explored deterministically per the inclusion
2018-03-05 16:29:37 -05:00
* branch first . At each node , the algorithm checks whether the selection is within the target range .
* While the selection has not reached the target range , more UTXOs are included . When a selection ' s
* value exceeds the target range , the complete subtree deriving from this selection can be omitted .
* At that point , the last included UTXO is deselected and the corresponding omission branch explored
* instead . The search ends after the complete tree has been searched or after a limited number of tries .
*
* The search continues to search for better solutions after one solution has been found . The best
* solution is chosen by minimizing the waste metric . The waste metric is defined as the cost to
* spend the current inputs at the given fee rate minus the long term expected cost to spend the
2021-10-11 08:26:07 -04:00
* inputs , plus the amount by which the selection exceeds the spending target :
2018-03-05 16:29:37 -05:00
*
* waste = selectionTotal - target + inputs × ( currentFeeRate - longTermFeeRate )
*
* The algorithm uses two additional optimizations . A lookahead keeps track of the total value of
* the unexplored UTXOs . A subtree is not explored if the lookahead indicates that the target range
* cannot be reached . Further , it is unnecessary to test equivalent combinations . This allows us
* to skip testing the inclusion of UTXOs that match the effective value and waste of an omitted
* predecessor .
*
* The Branch and Bound algorithm is described in detail in Murch ' s Master Thesis :
* https : //murch.one/wp-content/uploads/2016/11/erhardt2016coinselection.pdf
*
* @ param const std : : vector < CInputCoin > & utxo_pool The set of UTXOs that we are choosing from .
* These UTXOs will be sorted in descending order by effective value and the CInputCoins '
* values are their effective values .
2019-10-30 18:41:13 -04:00
* @ param const CAmount & selection_target This is the value that we want to select . It is the lower
2018-03-05 16:29:37 -05:00
* bound of the range .
* @ param const CAmount & cost_of_change This is the cost of creating and spending a change output .
2019-10-30 18:41:13 -04:00
* This plus selection_target is the upper bound of the range .
2020-11-16 14:31:45 -05:00
* @ returns The result of this coin selection algorithm , or std : : nullopt
2018-03-05 16:29:37 -05:00
*/
static const size_t TOTAL_TRIES = 100000 ;
2020-11-16 14:31:45 -05:00
std : : optional < SelectionResult > SelectCoinsBnB ( std : : vector < OutputGroup > & utxo_pool , const CAmount & selection_target , const CAmount & cost_of_change )
2018-03-05 16:29:37 -05:00
{
2020-11-16 14:31:45 -05:00
SelectionResult result ( selection_target ) ;
2018-03-05 16:29:37 -05:00
CAmount curr_value = 0 ;
2018-05-07 20:42:49 -04:00
std : : vector < size_t > curr_selection ; // selected utxo indexes
2018-03-05 16:29:37 -05:00
// Calculate curr_available_value
CAmount curr_available_value = 0 ;
2018-07-19 11:45:26 +09:00
for ( const OutputGroup & utxo : utxo_pool ) {
2022-02-28 12:10:16 +00:00
// Assert that this utxo is not negative. It should never be negative,
// effective value calculation should have removed it
2021-04-23 15:14:41 -04:00
assert ( utxo . GetSelectionAmount ( ) > 0 ) ;
curr_available_value + = utxo . GetSelectionAmount ( ) ;
2018-03-05 16:29:37 -05:00
}
2021-05-06 14:12:13 -04:00
if ( curr_available_value < selection_target ) {
2020-11-16 14:31:45 -05:00
return std : : nullopt ;
2018-03-05 16:29:37 -05:00
}
// Sort the utxo_pool
std : : sort ( utxo_pool . begin ( ) , utxo_pool . end ( ) , descending ) ;
CAmount curr_waste = 0 ;
2018-05-07 20:42:49 -04:00
std : : vector < size_t > best_selection ;
2018-03-05 16:29:37 -05:00
CAmount best_waste = MAX_MONEY ;
// Depth First search loop for choosing the UTXOs
2022-02-28 12:07:57 +00:00
for ( size_t curr_try = 0 , utxo_pool_index = 0 ; curr_try < TOTAL_TRIES ; + + curr_try , + + utxo_pool_index ) {
2018-03-05 16:29:37 -05:00
// Conditions for starting a backtrack
bool backtrack = false ;
2022-02-28 12:10:16 +00:00
if ( curr_value + curr_available_value < selection_target | | // Cannot possibly reach target with the amount remaining in the curr_available_value.
curr_value > selection_target + cost_of_change | | // Selected value is out of range, go back and try other branch
2018-03-05 16:29:37 -05:00
( curr_waste > best_waste & & ( utxo_pool . at ( 0 ) . fee - utxo_pool . at ( 0 ) . long_term_fee ) > 0 ) ) { // Don't select things which we know will be more wasteful if the waste is increasing
backtrack = true ;
2021-05-06 14:12:13 -04:00
} else if ( curr_value > = selection_target ) { // Selected value is within range
curr_waste + = ( curr_value - selection_target ) ; // This is the excess value which is added to the waste for the below comparison
2018-03-05 16:29:37 -05:00
// Adding another UTXO after this check could bring the waste down if the long term fee is higher than the current fee.
// However we are not going to explore that because this optimization for the waste is only done when we have hit our target
// value. Adding any more UTXOs will be just burning the UTXO; it will go entirely to fees. Thus we aren't going to
// explore any more UTXOs to avoid burning money like that.
if ( curr_waste < = best_waste ) {
best_selection = curr_selection ;
best_waste = curr_waste ;
2020-03-04 16:35:15 -05:00
if ( best_waste = = 0 ) {
break ;
}
2018-03-05 16:29:37 -05:00
}
2021-05-06 14:12:13 -04:00
curr_waste - = ( curr_value - selection_target ) ; // Remove the excess value as we will be selecting different coins now
2018-03-05 16:29:37 -05:00
backtrack = true ;
}
2022-02-28 12:10:16 +00:00
if ( backtrack ) { // Backtracking, moving backwards
2018-03-05 16:29:37 -05:00
if ( curr_selection . empty ( ) ) { // We have walked back to the first utxo and no branch is untraversed. All solutions searched
break ;
}
2022-02-28 12:10:16 +00:00
// Add omitted UTXOs back to lookahead before traversing the omission branch of last included UTXO.
2018-05-07 20:42:49 -04:00
for ( - - utxo_pool_index ; utxo_pool_index > curr_selection . back ( ) ; - - utxo_pool_index ) {
curr_available_value + = utxo_pool . at ( utxo_pool_index ) . GetSelectionAmount ( ) ;
}
2018-03-05 16:29:37 -05:00
// Output was included on previous iterations, try excluding now.
2018-05-07 20:42:49 -04:00
assert ( utxo_pool_index = = curr_selection . back ( ) ) ;
OutputGroup & utxo = utxo_pool . at ( utxo_pool_index ) ;
2021-04-23 15:14:41 -04:00
curr_value - = utxo . GetSelectionAmount ( ) ;
2018-03-05 16:29:37 -05:00
curr_waste - = utxo . fee - utxo . long_term_fee ;
2018-05-07 20:42:49 -04:00
curr_selection . pop_back ( ) ;
2018-03-05 16:29:37 -05:00
} else { // Moving forwards, continuing down this branch
2018-05-07 20:42:49 -04:00
OutputGroup & utxo = utxo_pool . at ( utxo_pool_index ) ;
2018-03-05 16:29:37 -05:00
// Remove this utxo from the curr_available_value utxo amount
2021-04-23 15:14:41 -04:00
curr_available_value - = utxo . GetSelectionAmount ( ) ;
2018-03-05 16:29:37 -05:00
2018-05-07 20:42:49 -04:00
if ( curr_selection . empty ( ) | |
// The previous index is included and therefore not relevant for exclusion shortcut
( utxo_pool_index - 1 ) = = curr_selection . back ( ) | |
2022-02-28 12:10:16 +00:00
// Avoid searching a branch if the previous UTXO has the same value and same waste and was excluded.
// Since the ratio of fee to long term fee is the same, we only need to check if one of those values match in order to know that the waste is the same.
2018-05-07 20:42:49 -04:00
utxo . GetSelectionAmount ( ) ! = utxo_pool . at ( utxo_pool_index - 1 ) . GetSelectionAmount ( ) | |
utxo . fee ! = utxo_pool . at ( utxo_pool_index - 1 ) . fee )
{
2018-03-05 16:29:37 -05:00
// Inclusion branch first (Largest First Exploration)
2018-05-07 20:42:49 -04:00
curr_selection . push_back ( utxo_pool_index ) ;
2021-04-23 15:14:41 -04:00
curr_value + = utxo . GetSelectionAmount ( ) ;
2018-03-05 16:29:37 -05:00
curr_waste + = utxo . fee - utxo . long_term_fee ;
}
}
}
// Check for solution
if ( best_selection . empty ( ) ) {
2020-11-16 14:31:45 -05:00
return std : : nullopt ;
2018-03-05 16:29:37 -05:00
}
// Set output set
2018-05-07 20:42:49 -04:00
for ( const size_t & i : best_selection ) {
result . AddInput ( utxo_pool . at ( i ) ) ;
2018-03-05 16:29:37 -05:00
}
2022-03-11 12:20:36 +00:00
result . ComputeAndSetWaste ( CAmount { 0 } ) ;
assert ( best_waste = = result . GetWaste ( ) ) ;
2018-03-05 16:29:37 -05:00
2020-11-16 14:31:45 -05:00
return result ;
2018-03-05 16:29:37 -05:00
}
2018-03-09 22:51:39 -05:00
2022-03-14 15:22:42 +01:00
std : : optional < SelectionResult > SelectCoinsSRD ( const std : : vector < OutputGroup > & utxo_pool , CAmount target_value , FastRandomContext & rng )
2019-11-06 14:35:11 -05:00
{
2021-09-27 23:22:34 -04:00
SelectionResult result ( target_value ) ;
2019-11-06 14:35:11 -05:00
std : : vector < size_t > indexes ;
indexes . resize ( utxo_pool . size ( ) ) ;
std : : iota ( indexes . begin ( ) , indexes . end ( ) , 0 ) ;
2022-03-14 15:22:42 +01:00
Shuffle ( indexes . begin ( ) , indexes . end ( ) , rng ) ;
2019-11-06 14:35:11 -05:00
CAmount selected_eff_value = 0 ;
for ( const size_t i : indexes ) {
const OutputGroup & group = utxo_pool . at ( i ) ;
Assume ( group . GetSelectionAmount ( ) > 0 ) ;
selected_eff_value + = group . GetSelectionAmount ( ) ;
2021-09-27 23:22:34 -04:00
result . AddInput ( group ) ;
2019-11-06 14:35:11 -05:00
if ( selected_eff_value > = target_value ) {
2021-09-27 23:22:34 -04:00
return result ;
2019-11-06 14:35:11 -05:00
}
}
return std : : nullopt ;
}
2022-03-14 15:22:42 +01:00
static void ApproximateBestSubset ( FastRandomContext & insecure_rand , const std : : vector < OutputGroup > & groups , const CAmount & nTotalLower , const CAmount & nTargetValue ,
2018-03-09 22:51:39 -05:00
std : : vector < char > & vfBest , CAmount & nBest , int iterations = 1000 )
{
std : : vector < char > vfIncluded ;
2018-07-19 11:45:26 +09:00
vfBest . assign ( groups . size ( ) , true ) ;
2018-03-09 22:51:39 -05:00
nBest = nTotalLower ;
for ( int nRep = 0 ; nRep < iterations & & nBest ! = nTargetValue ; nRep + + )
{
2018-07-19 11:45:26 +09:00
vfIncluded . assign ( groups . size ( ) , false ) ;
2018-03-09 22:51:39 -05:00
CAmount nTotal = 0 ;
bool fReachedTarget = false ;
for ( int nPass = 0 ; nPass < 2 & & ! fReachedTarget ; nPass + + )
{
2018-07-19 11:45:26 +09:00
for ( unsigned int i = 0 ; i < groups . size ( ) ; i + + )
2018-03-09 22:51:39 -05:00
{
//The solver here uses a randomized algorithm,
//the randomness serves no real security purpose but is just
//needed to prevent degenerate behavior and it is important
//that the rng is fast. We do not use a constant random sequence,
//because there may be some privacy improvement by making
//the selection random.
if ( nPass = = 0 ? insecure_rand . randbool ( ) : ! vfIncluded [ i ] )
{
2021-08-11 22:03:56 -04:00
nTotal + = groups [ i ] . GetSelectionAmount ( ) ;
2018-03-09 22:51:39 -05:00
vfIncluded [ i ] = true ;
if ( nTotal > = nTargetValue )
{
fReachedTarget = true ;
if ( nTotal < nBest )
{
nBest = nTotal ;
vfBest = vfIncluded ;
}
2021-08-11 22:03:56 -04:00
nTotal - = groups [ i ] . GetSelectionAmount ( ) ;
2018-03-09 22:51:39 -05:00
vfIncluded [ i ] = false ;
}
}
}
}
}
}
2022-03-14 15:22:42 +01:00
std : : optional < SelectionResult > KnapsackSolver ( std : : vector < OutputGroup > & groups , const CAmount & nTargetValue , FastRandomContext & rng )
2018-03-09 22:51:39 -05:00
{
2020-11-16 15:36:47 -05:00
SelectionResult result ( nTargetValue ) ;
2018-03-09 22:51:39 -05:00
// List of values less than target
2021-03-15 10:41:30 +08:00
std : : optional < OutputGroup > lowest_larger ;
2018-07-19 11:45:26 +09:00
std : : vector < OutputGroup > applicable_groups ;
2018-03-09 22:51:39 -05:00
CAmount nTotalLower = 0 ;
2022-03-14 15:22:42 +01:00
Shuffle ( groups . begin ( ) , groups . end ( ) , rng ) ;
2018-03-09 22:51:39 -05:00
2018-07-19 11:45:26 +09:00
for ( const OutputGroup & group : groups ) {
2021-04-23 15:14:41 -04:00
if ( group . GetSelectionAmount ( ) = = nTargetValue ) {
2020-11-16 15:36:47 -05:00
result . AddInput ( group ) ;
return result ;
2021-04-23 15:14:41 -04:00
} else if ( group . GetSelectionAmount ( ) < nTargetValue + MIN_CHANGE ) {
2018-07-19 11:45:26 +09:00
applicable_groups . push_back ( group ) ;
2021-04-23 15:14:41 -04:00
nTotalLower + = group . GetSelectionAmount ( ) ;
} else if ( ! lowest_larger | | group . GetSelectionAmount ( ) < lowest_larger - > GetSelectionAmount ( ) ) {
2018-07-19 11:45:26 +09:00
lowest_larger = group ;
2018-03-09 22:51:39 -05:00
}
}
2018-07-19 11:45:26 +09:00
if ( nTotalLower = = nTargetValue ) {
for ( const auto & group : applicable_groups ) {
2020-11-16 15:36:47 -05:00
result . AddInput ( group ) ;
2018-03-09 22:51:39 -05:00
}
2020-11-16 15:36:47 -05:00
return result ;
2018-03-09 22:51:39 -05:00
}
2018-07-19 11:45:26 +09:00
if ( nTotalLower < nTargetValue ) {
2020-11-16 15:36:47 -05:00
if ( ! lowest_larger ) return std : : nullopt ;
result . AddInput ( * lowest_larger ) ;
return result ;
2018-03-09 22:51:39 -05:00
}
// Solve subset sum by stochastic approximation
2018-07-19 11:45:26 +09:00
std : : sort ( applicable_groups . begin ( ) , applicable_groups . end ( ) , descending ) ;
2018-03-09 22:51:39 -05:00
std : : vector < char > vfBest ;
CAmount nBest ;
2022-03-14 15:22:42 +01:00
ApproximateBestSubset ( rng , applicable_groups , nTotalLower , nTargetValue , vfBest , nBest ) ;
2018-07-19 11:45:26 +09:00
if ( nBest ! = nTargetValue & & nTotalLower > = nTargetValue + MIN_CHANGE ) {
2022-03-14 15:22:42 +01:00
ApproximateBestSubset ( rng , applicable_groups , nTotalLower , nTargetValue + MIN_CHANGE , vfBest , nBest ) ;
2018-07-19 11:45:26 +09:00
}
2018-03-09 22:51:39 -05:00
// If we have a bigger coin and (either the stochastic approximation didn't find a good solution,
// or the next bigger coin is closer), return the bigger coin
2018-07-19 11:45:26 +09:00
if ( lowest_larger & &
2021-04-23 15:14:41 -04:00
( ( nBest ! = nTargetValue & & nBest < nTargetValue + MIN_CHANGE ) | | lowest_larger - > GetSelectionAmount ( ) < = nBest ) ) {
2020-11-16 15:36:47 -05:00
result . AddInput ( * lowest_larger ) ;
2018-07-19 11:45:26 +09:00
} else {
for ( unsigned int i = 0 ; i < applicable_groups . size ( ) ; i + + ) {
if ( vfBest [ i ] ) {
2020-11-16 15:36:47 -05:00
result . AddInput ( applicable_groups [ i ] ) ;
2018-03-09 22:51:39 -05:00
}
2018-07-19 11:45:26 +09:00
}
2018-03-09 22:51:39 -05:00
if ( LogAcceptCategory ( BCLog : : SELECTCOINS ) ) {
2021-09-28 08:50:02 +02:00
std : : string log_message { " Coin selection best subset: " } ;
2018-07-19 11:45:26 +09:00
for ( unsigned int i = 0 ; i < applicable_groups . size ( ) ; i + + ) {
2018-03-09 22:51:39 -05:00
if ( vfBest [ i ] ) {
2021-09-27 13:11:30 +02:00
log_message + = strprintf ( " %s " , FormatMoney ( applicable_groups [ i ] . m_value ) ) ;
2018-03-09 22:51:39 -05:00
}
}
2021-09-27 13:11:30 +02:00
LogPrint ( BCLog : : SELECTCOINS , " %stotal %s \n " , log_message , FormatMoney ( nBest ) ) ;
2018-03-09 22:51:39 -05:00
}
}
2020-11-16 15:36:47 -05:00
return result ;
2018-03-09 22:51:39 -05:00
}
2018-07-19 11:43:03 +09:00
/******************************************************************************
OutputGroup
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2020-08-31 16:45:39 -04:00
void OutputGroup : : Insert ( const CInputCoin & output , int depth , bool from_me , size_t ancestors , size_t descendants , bool positive_only ) {
// Compute the effective value first
const CAmount coin_fee = output . m_input_bytes < 0 ? 0 : m_effective_feerate . GetFee ( output . m_input_bytes ) ;
const CAmount ev = output . txout . nValue - coin_fee ;
// Filter for positive only here before adding the coin
if ( positive_only & & ev < = 0 ) return ;
2018-07-19 11:43:03 +09:00
m_outputs . push_back ( output ) ;
2020-08-31 15:56:30 -04:00
CInputCoin & coin = m_outputs . back ( ) ;
2020-08-31 16:45:39 -04:00
coin . m_fee = coin_fee ;
2020-08-31 15:56:30 -04:00
fee + = coin . m_fee ;
coin . m_long_term_fee = coin . m_input_bytes < 0 ? 0 : m_long_term_feerate . GetFee ( coin . m_input_bytes ) ;
long_term_fee + = coin . m_long_term_fee ;
2020-08-31 16:45:39 -04:00
coin . effective_value = ev ;
2020-08-31 15:56:30 -04:00
effective_value + = coin . effective_value ;
2018-07-19 11:43:03 +09:00
m_from_me & = from_me ;
2019-11-07 17:19:24 -05:00
m_value + = output . txout . nValue ;
2018-07-19 11:43:03 +09:00
m_depth = std : : min ( m_depth , depth ) ;
2018-07-31 04:42:41 +09:00
// ancestors here express the number of ancestors the new coin will end up having, which is
// the sum, rather than the max; this will overestimate in the cases where multiple inputs
// have common ancestors
m_ancestors + = ancestors ;
// descendants is the count as seen from the top ancestor, not the descendants as seen from the
// coin itself; thus, this value is counted as the max, not the sum
2018-07-19 11:43:03 +09:00
m_descendants = std : : max ( m_descendants , descendants ) ;
}
bool OutputGroup : : EligibleForSpending ( const CoinEligibilityFilter & eligibility_filter ) const
{
return m_depth > = ( m_from_me ? eligibility_filter . conf_mine : eligibility_filter . conf_theirs )
& & m_ancestors < = eligibility_filter . max_ancestors
& & m_descendants < = eligibility_filter . max_descendants ;
}
2021-04-23 15:14:41 -04:00
CAmount OutputGroup : : GetSelectionAmount ( ) const
{
return m_subtract_fee_outputs ? m_value : effective_value ;
}
2021-05-20 19:06:56 -04:00
CAmount GetSelectionWaste ( const std : : set < CInputCoin > & inputs , CAmount change_cost , CAmount target , bool use_effective_value )
{
// This function should not be called with empty inputs as that would mean the selection failed
assert ( ! inputs . empty ( ) ) ;
// Always consider the cost of spending an input now vs in the future.
CAmount waste = 0 ;
CAmount selected_effective_value = 0 ;
for ( const CInputCoin & coin : inputs ) {
waste + = coin . m_fee - coin . m_long_term_fee ;
selected_effective_value + = use_effective_value ? coin . effective_value : coin . txout . nValue ;
}
if ( change_cost ) {
// Consider the cost of making change and spending it in the future
// If we aren't making change, the caller should've set change_cost to 0
assert ( change_cost > 0 ) ;
waste + = change_cost ;
} else {
// When we are not making change (change_cost == 0), consider the excess we are throwing away to fees
assert ( selected_effective_value > = target ) ;
waste + = selected_effective_value - target ;
}
return waste ;
}
2020-11-16 14:24:08 -05:00
void SelectionResult : : ComputeAndSetWaste ( CAmount change_cost )
{
m_waste = GetSelectionWaste ( m_selected_inputs , change_cost , m_target , m_use_effective ) ;
}
CAmount SelectionResult : : GetWaste ( ) const
{
2021-12-13 14:45:48 +01:00
return * Assert ( m_waste ) ;
2020-11-16 14:24:08 -05:00
}
CAmount SelectionResult : : GetSelectedValue ( ) const
{
return std : : accumulate ( m_selected_inputs . cbegin ( ) , m_selected_inputs . cend ( ) , CAmount { 0 } , [ ] ( CAmount sum , const auto & coin ) { return sum + coin . txout . nValue ; } ) ;
}
void SelectionResult : : Clear ( )
{
m_selected_inputs . clear ( ) ;
m_waste . reset ( ) ;
}
void SelectionResult : : AddInput ( const OutputGroup & group )
{
util : : insert ( m_selected_inputs , group . m_outputs ) ;
2020-11-16 14:31:45 -05:00
m_use_effective = ! group . m_subtract_fee_outputs ;
2020-11-16 14:24:08 -05:00
}
const std : : set < CInputCoin > & SelectionResult : : GetInputSet ( ) const
{
return m_selected_inputs ;
}
std : : vector < CInputCoin > SelectionResult : : GetShuffledInputVector ( ) const
{
std : : vector < CInputCoin > coins ( m_selected_inputs . begin ( ) , m_selected_inputs . end ( ) ) ;
Shuffle ( coins . begin ( ) , coins . end ( ) , FastRandomContext ( ) ) ;
return coins ;
}
bool SelectionResult : : operator < ( SelectionResult other ) const
{
2021-12-13 14:45:48 +01:00
Assert ( m_waste . has_value ( ) ) ;
Assert ( other . m_waste . has_value ( ) ) ;
2020-11-16 14:24:08 -05:00
// As this operator is only used in std::min_element, we want the result that has more inputs when waste are equal.
return * m_waste < * other . m_waste | | ( * m_waste = = * other . m_waste & & m_selected_inputs . size ( ) > other . m_selected_inputs . size ( ) ) ;
}
2021-11-12 11:13:29 -05:00
} // namespace wallet