Backport bitcoin#10637 (partial) (#3878)
* Calculate and store the number of bytes required to spend an input
* Store effective value, fee, and long term fee in CInputCoin
Have CInputCOin store effective value information. This includes the effective
value itself, the fee, and the long term fee for the input
* Implement Branch and Bound coin selection in a new file
Create a new file for coin selection logic and implement the BnB algorithm in it.
* Move output eligibility to a separate function
* Use a struct for output eligibility
Instead of specifying 3 parameters, use a struct for those parameters
in order to reduce the number of arguments to SelectCoinsMinConf.
* Remove coinselection.h -> wallet.h circular dependency
Changes CInputCoin to coinselection and to use CTransactionRef in
order to avoid a circular dependency. Also moves other coin selection
specific variables out of wallet.h to coinselectoin.h
* Add tests for the Branch and Bound algorithm
* Move current coin selection algorithm to coinselection.{cpp,h}
Moves the current coin selection algorithm out of SelectCoinsMinConf
and puts it in coinselection.{cpp,h}. The new function, KnapsackSolver,
instead of taking a vector of COutputs, will take a vector of CInputCoins
that is prepared by SelectCoinsMinConf.
* Move original knapsack solver tests to coinselector_tests.cpp
* Add a GetMinimumFeeRate function which is wrapped by GetMinimumFee
* Have SelectCoinsMinConf and SelectCoins use BnB or Knapsack and use it (partial)
Allows SelectCoinsMinConf and SelectCoins be able to switch between
using BnB or Knapsack for choosing coins.
Has SelectCoinsMinConf do the preprocessing necessary to support either
BnB or Knapsack. This includes calculating the filtering the effective
values for each input.
Uses BnB in CreateTransaction to find an exact match for the output.
If BnB fails, it will fallback to the Knapsack solver.
Dash specific note: just always use Knapsack in CreateTransaction.
* Benchmark BnB in the worst case where it exhausts
* Add a test to make sure that negative effective values are filtered
* More of 12747: Fix typos
Co-authored-by: Andrew Chow <achow101-github@achow101.com>
2020-12-18 18:43:48 +01:00
// Copyright (c) 2017 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
# include <wallet/coinselection.h>
# include <util.h>
# include <utilmoneystr.h>
# include <privatesend/privatesend.h>
// Descending order comparator
struct {
bool operator ( ) ( const CInputCoin & a , const CInputCoin & b ) const
{
return a . effective_value > b . effective_value ;
}
} 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
* are sorted by their effective values and the trees is explored deterministically per the inclusion
* 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
* inputs , plus the amount the selection exceeds the spending target :
*
* 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 .
* @ param const CAmount & target_value This is the value that we want to select . It is the lower
* bound of the range .
* @ param const CAmount & cost_of_change This is the cost of creating and spending a change output .
* This plus target_value is the upper bound of the range .
* @ param std : : set < CInputCoin > & out_set - > This is an output parameter for the set of CInputCoins
* that have been selected .
* @ param CAmount & value_ret - > This is an output parameter for the total value of the CInputCoins
* that were selected .
* @ param CAmount not_input_fees - > The fees that need to be paid for the outputs and fixed size
* overhead ( version , locktime , marker and flag )
*/
static const size_t TOTAL_TRIES = 100000 ;
bool SelectCoinsBnB ( std : : vector < CInputCoin > & utxo_pool , const CAmount & target_value , const CAmount & cost_of_change , std : : set < CInputCoin > & out_set , CAmount & value_ret , CAmount not_input_fees )
{
out_set . clear ( ) ;
CAmount curr_value = 0 ;
std : : vector < bool > curr_selection ; // select the utxo at this index
curr_selection . reserve ( utxo_pool . size ( ) ) ;
CAmount actual_target = not_input_fees + target_value ;
// Calculate curr_available_value
CAmount curr_available_value = 0 ;
for ( const CInputCoin & utxo : utxo_pool ) {
// Assert that this utxo is not negative. It should never be negative, effective value calculation should have removed it
assert ( utxo . effective_value > 0 ) ;
curr_available_value + = utxo . effective_value ;
}
if ( curr_available_value < actual_target ) {
return false ;
}
// Sort the utxo_pool
std : : sort ( utxo_pool . begin ( ) , utxo_pool . end ( ) , descending ) ;
CAmount curr_waste = 0 ;
std : : vector < bool > best_selection ;
CAmount best_waste = MAX_MONEY ;
// Depth First search loop for choosing the UTXOs
for ( size_t i = 0 ; i < TOTAL_TRIES ; + + i ) {
// Conditions for starting a backtrack
bool backtrack = false ;
if ( curr_value + curr_available_value < actual_target | | // Cannot possibly reach target with the amount remaining in the curr_available_value.
curr_value > actual_target + cost_of_change | | // Selected value is out of range, go back and try other branch
( 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 ;
} else if ( curr_value > = actual_target ) { // Selected value is within range
curr_waste + = ( curr_value - actual_target ) ; // This is the excess value which is added to the waste for the below comparison
// 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_selection . resize ( utxo_pool . size ( ) ) ;
best_waste = curr_waste ;
}
curr_waste - = ( curr_value - actual_target ) ; // Remove the excess value as we will be selecting different coins now
backtrack = true ;
}
// Backtracking, moving backwards
if ( backtrack ) {
// Walk backwards to find the last included UTXO that still needs to have its omission branch traversed.
while ( ! curr_selection . empty ( ) & & ! curr_selection . back ( ) ) {
curr_selection . pop_back ( ) ;
curr_available_value + = utxo_pool . at ( curr_selection . size ( ) ) . effective_value ;
} ;
if ( curr_selection . empty ( ) ) { // We have walked back to the first utxo and no branch is untraversed. All solutions searched
break ;
}
// Output was included on previous iterations, try excluding now.
curr_selection . back ( ) = false ;
CInputCoin & utxo = utxo_pool . at ( curr_selection . size ( ) - 1 ) ;
curr_value - = utxo . effective_value ;
curr_waste - = utxo . fee - utxo . long_term_fee ;
} else { // Moving forwards, continuing down this branch
CInputCoin & utxo = utxo_pool . at ( curr_selection . size ( ) ) ;
// Remove this utxo from the curr_available_value utxo amount
curr_available_value - = utxo . effective_value ;
// 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.
if ( ! curr_selection . empty ( ) & & ! curr_selection . back ( ) & &
utxo . effective_value = = utxo_pool . at ( curr_selection . size ( ) - 1 ) . effective_value & &
utxo . fee = = utxo_pool . at ( curr_selection . size ( ) - 1 ) . fee ) {
curr_selection . push_back ( false ) ;
} else {
// Inclusion branch first (Largest First Exploration)
curr_selection . push_back ( true ) ;
curr_value + = utxo . effective_value ;
curr_waste + = utxo . fee - utxo . long_term_fee ;
}
}
}
// Check for solution
if ( best_selection . empty ( ) ) {
return false ;
}
// Set output set
value_ret = 0 ;
for ( size_t i = 0 ; i < best_selection . size ( ) ; + + i ) {
if ( best_selection . at ( i ) ) {
out_set . insert ( utxo_pool . at ( i ) ) ;
value_ret + = utxo_pool . at ( i ) . txout . nValue ;
}
}
return true ;
}
static void ApproximateBestSubset ( const std : : vector < CInputCoin > & vValue , const CAmount & nTotalLower , const CAmount & nTargetValue ,
std : : vector < char > & vfBest , CAmount & nBest , int iterations = 1000 )
{
std : : vector < char > vfIncluded ;
vfBest . assign ( vValue . size ( ) , true ) ;
nBest = nTotalLower ;
int nBestInputCount = 0 ;
FastRandomContext insecure_rand ;
for ( int nRep = 0 ; nRep < iterations & & nBest ! = nTargetValue ; nRep + + )
{
vfIncluded . assign ( vValue . size ( ) , false ) ;
CAmount nTotal = 0 ;
int nTotalInputCount = 0 ;
bool fReachedTarget = false ;
for ( int nPass = 0 ; nPass < 2 & & ! fReachedTarget ; nPass + + )
{
for ( unsigned int i = 0 ; i < vValue . size ( ) ; i + + )
{
//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 ] )
{
nTotal + = vValue [ i ] . txout . nValue ;
+ + nTotalInputCount ;
vfIncluded [ i ] = true ;
if ( nTotal > = nTargetValue )
{
fReachedTarget = true ;
if ( nTotal < nBest | | ( nTotal = = nBest & & nTotalInputCount < nBestInputCount ) )
{
nBest = nTotal ;
nBestInputCount = nTotalInputCount ;
vfBest = vfIncluded ;
}
nTotal - = vValue [ i ] . txout . nValue ;
- - nTotalInputCount ;
vfIncluded [ i ] = false ;
}
}
}
}
}
}
int CInputCoin : : Priority ( ) const
{
for ( const auto & d : CPrivateSend : : GetStandardDenominations ( ) ) {
// large denoms have lower value
if ( txout . nValue = = d ) return ( float ) COIN / d * 10000 ;
}
if ( txout . nValue < 1 * COIN ) return 20000 ;
//nondenom return largest first
return - 1 * ( txout . nValue / COIN ) ;
}
struct CompareByPriority
{
bool operator ( ) ( const CInputCoin & coin1 ,
const CInputCoin & coin2 ) const
{
return coin1 . Priority ( ) > coin2 . Priority ( ) ;
}
} ;
// move denoms down
bool less_then_denom ( const CInputCoin & coin1 , const CInputCoin & coin2 )
{
bool found1 = false ;
bool found2 = false ;
for ( const auto & d : CPrivateSend : : GetStandardDenominations ( ) ) // loop through predefined denoms
{
if ( coin1 . txout . nValue = = d ) found1 = true ;
if ( coin2 . txout . nValue = = d ) found2 = true ;
}
return ( ! found1 & & found2 ) ;
}
bool KnapsackSolver ( const CAmount & nTargetValue , std : : vector < CInputCoin > & vCoins , std : : set < CInputCoin > & setCoinsRet , CAmount & nValueRet , bool fFulyMixedOnly , CAmount maxTxFee )
{
setCoinsRet . clear ( ) ;
nValueRet = 0 ;
// List of values less than target
boost : : optional < CInputCoin > coinLowestLarger ;
std : : vector < CInputCoin > vValue ;
CAmount nTotalLower = 0 ;
2021-01-14 20:46:16 +01:00
Shuffle ( vCoins . begin ( ) , vCoins . end ( ) , FastRandomContext ( ) ) ;
Backport bitcoin#10637 (partial) (#3878)
* Calculate and store the number of bytes required to spend an input
* Store effective value, fee, and long term fee in CInputCoin
Have CInputCOin store effective value information. This includes the effective
value itself, the fee, and the long term fee for the input
* Implement Branch and Bound coin selection in a new file
Create a new file for coin selection logic and implement the BnB algorithm in it.
* Move output eligibility to a separate function
* Use a struct for output eligibility
Instead of specifying 3 parameters, use a struct for those parameters
in order to reduce the number of arguments to SelectCoinsMinConf.
* Remove coinselection.h -> wallet.h circular dependency
Changes CInputCoin to coinselection and to use CTransactionRef in
order to avoid a circular dependency. Also moves other coin selection
specific variables out of wallet.h to coinselectoin.h
* Add tests for the Branch and Bound algorithm
* Move current coin selection algorithm to coinselection.{cpp,h}
Moves the current coin selection algorithm out of SelectCoinsMinConf
and puts it in coinselection.{cpp,h}. The new function, KnapsackSolver,
instead of taking a vector of COutputs, will take a vector of CInputCoins
that is prepared by SelectCoinsMinConf.
* Move original knapsack solver tests to coinselector_tests.cpp
* Add a GetMinimumFeeRate function which is wrapped by GetMinimumFee
* Have SelectCoinsMinConf and SelectCoins use BnB or Knapsack and use it (partial)
Allows SelectCoinsMinConf and SelectCoins be able to switch between
using BnB or Knapsack for choosing coins.
Has SelectCoinsMinConf do the preprocessing necessary to support either
BnB or Knapsack. This includes calculating the filtering the effective
values for each input.
Uses BnB in CreateTransaction to find an exact match for the output.
If BnB fails, it will fallback to the Knapsack solver.
Dash specific note: just always use Knapsack in CreateTransaction.
* Benchmark BnB in the worst case where it exhausts
* Add a test to make sure that negative effective values are filtered
* More of 12747: Fix typos
Co-authored-by: Andrew Chow <achow101-github@achow101.com>
2020-12-18 18:43:48 +01:00
int tryDenomStart = 0 ;
CAmount nMinChange = MIN_CHANGE ;
if ( fFulyMixedOnly ) {
// larger denoms first
std : : sort ( vCoins . rbegin ( ) , vCoins . rend ( ) , CompareByPriority ( ) ) ;
// we actually want denoms only, so let's skip "non-denom only" step
tryDenomStart = 1 ;
// no change is allowed
nMinChange = 0 ;
} else {
// move denoms down on the list
// try not to use denominated coins when not needed, save denoms for privatesend
std : : sort ( vCoins . begin ( ) , vCoins . end ( ) , less_then_denom ) ;
}
// try to find nondenom first to prevent unneeded spending of mixed coins
for ( unsigned int tryDenom = tryDenomStart ; tryDenom < 2 ; tryDenom + + )
{
LogPrint ( BCLog : : SELECTCOINS , " tryDenom: %d \n " , tryDenom ) ;
vValue . clear ( ) ;
nTotalLower = 0 ;
for ( const CInputCoin & coin : vCoins )
{
if ( tryDenom = = 0 & & CPrivateSend : : IsDenominatedAmount ( coin . txout . nValue ) ) continue ; // we don't want denom values on first run
if ( coin . txout . nValue = = nTargetValue )
{
setCoinsRet . insert ( coin ) ;
nValueRet + = coin . txout . nValue ;
return true ;
}
else if ( coin . txout . nValue < nTargetValue + nMinChange )
{
vValue . push_back ( coin ) ;
nTotalLower + = coin . txout . nValue ;
}
else if ( ! coinLowestLarger | | coin . txout . nValue < coinLowestLarger - > txout . nValue )
{
coinLowestLarger = coin ;
}
}
if ( nTotalLower = = nTargetValue )
{
for ( const auto & input : vValue )
{
setCoinsRet . insert ( input ) ;
nValueRet + = input . txout . nValue ;
}
return true ;
}
if ( nTotalLower < nTargetValue )
{
if ( ! coinLowestLarger ) // there is no input larger than nTargetValue
{
if ( tryDenom = = 0 )
// we didn't look at denom yet, let's do it
continue ;
else
// we looked at everything possible and didn't find anything, no luck
return false ;
}
setCoinsRet . insert ( coinLowestLarger . get ( ) ) ;
nValueRet + = coinLowestLarger - > txout . nValue ;
// There is no change in PS, so we know the fee beforehand,
// can see if we exceeded the max fee and thus fail quickly.
return fFulyMixedOnly ? ( nValueRet - nTargetValue < = maxTxFee ) : true ;
}
// nTotalLower > nTargetValue
break ;
}
// Solve subset sum by stochastic approximation
std : : sort ( vValue . begin ( ) , vValue . end ( ) , descending ) ;
std : : vector < char > vfBest ;
CAmount nBest ;
ApproximateBestSubset ( vValue , nTotalLower , nTargetValue , vfBest , nBest ) ;
if ( nBest ! = nTargetValue & & nMinChange ! = 0 & & nTotalLower > = nTargetValue + nMinChange )
ApproximateBestSubset ( vValue , nTotalLower , nTargetValue + nMinChange , vfBest , nBest ) ;
// 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
if ( coinLowestLarger & &
( ( nBest ! = nTargetValue & & nBest < nTargetValue + nMinChange ) | | coinLowestLarger - > txout . nValue < = nBest ) )
{
setCoinsRet . insert ( coinLowestLarger . get ( ) ) ;
nValueRet + = coinLowestLarger - > txout . nValue ;
}
else {
std : : string s = " CWallet::SelectCoinsMinConf best subset: " ;
for ( unsigned int i = 0 ; i < vValue . size ( ) ; i + + )
{
if ( vfBest [ i ] )
{
setCoinsRet . insert ( vValue [ i ] ) ;
nValueRet + = vValue [ i ] . txout . nValue ;
s + = FormatMoney ( vValue [ i ] . txout . nValue ) + " " ;
}
}
LogPrint ( BCLog : : SELECTCOINS , " %s - total %s \n " , s , FormatMoney ( nBest ) ) ;
}
// There is no change in PS, so we know the fee beforehand,
// can see if we exceeded the max fee and thus fail quickly.
return fFulyMixedOnly ? ( nValueRet - nTargetValue < = maxTxFee ) : true ;
}