Bitcoin Core  22.0.0
P2P Digital Currency
coinselection.h
Go to the documentation of this file.
1 // Copyright (c) 2017-2019 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_WALLET_COINSELECTION_H
6 #define BITCOIN_WALLET_COINSELECTION_H
7 
8 #include <amount.h>
9 #include <policy/feerate.h>
10 #include <primitives/transaction.h>
11 #include <random.h>
12 
14 static constexpr CAmount MIN_CHANGE{COIN / 100};
17 
19 class CInputCoin {
20 public:
21  CInputCoin(const CTransactionRef& tx, unsigned int i)
22  {
23  if (!tx)
24  throw std::invalid_argument("tx should not be null");
25  if (i >= tx->vout.size())
26  throw std::out_of_range("The output index is out of range");
27 
28  outpoint = COutPoint(tx->GetHash(), i);
29  txout = tx->vout[i];
31  }
32 
33  CInputCoin(const CTransactionRef& tx, unsigned int i, int input_bytes) : CInputCoin(tx, i)
34  {
35  m_input_bytes = input_bytes;
36  }
37 
43 
45  int m_input_bytes{-1};
46 
47  bool operator<(const CInputCoin& rhs) const {
48  return outpoint < rhs.outpoint;
49  }
50 
51  bool operator!=(const CInputCoin& rhs) const {
52  return outpoint != rhs.outpoint;
53  }
54 
55  bool operator==(const CInputCoin& rhs) const {
56  return outpoint == rhs.outpoint;
57  }
58 };
59 
62 {
64  size_t change_output_size = 0;
66  size_t change_spend_size = 0;
80  size_t tx_noinputs_size = 0;
82  bool m_subtract_fee_outputs = false;
86  bool m_avoid_partial_spends = false;
87 
89  CFeeRate long_term_feerate, CFeeRate discard_feerate, size_t tx_noinputs_size, bool avoid_partial) :
92  m_effective_feerate(effective_feerate),
93  m_long_term_feerate(long_term_feerate),
94  m_discard_feerate(discard_feerate),
96  m_avoid_partial_spends(avoid_partial)
97  {}
99 };
100 
105 {
108  const int conf_mine;
110  const int conf_theirs;
112  const uint64_t max_ancestors;
114  const uint64_t max_descendants;
116  const bool m_include_partial_groups{false};
117 
121 };
122 
125 {
127  std::vector<CInputCoin> m_outputs;
131  bool m_from_me{true};
135  int m_depth{999};
138  size_t m_ancestors{0};
140  size_t m_descendants{0};
156 
162  {}
163 
164  void Insert(const CInputCoin& output, int depth, bool from_me, size_t ancestors, size_t descendants, bool positive_only);
165  bool EligibleForSpending(const CoinEligibilityFilter& eligibility_filter) const;
166  CAmount GetSelectionAmount() const;
167 };
168 
169 bool SelectCoinsBnB(std::vector<OutputGroup>& utxo_pool, const CAmount& selection_target, const CAmount& cost_of_change, std::set<CInputCoin>& out_set, CAmount& value_ret);
170 
171 // Original coin selection algorithm as a fallback
172 bool KnapsackSolver(const CAmount& nTargetValue, std::vector<OutputGroup>& groups, std::set<CInputCoin>& setCoinsRet, CAmount& nValueRet);
173 
174 #endif // BITCOIN_WALLET_COINSELECTION_H
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:386
CAmount nValue
Definition: transaction.h:131
bool SelectCoinsBnB(std::vector< OutputGroup > &utxo_pool, const CAmount &selection_target, const CAmount &cost_of_change, std::set< CInputCoin > &out_set, CAmount &value_ret)
CoinSelectionParams(size_t change_output_size, size_t change_spend_size, CFeeRate effective_feerate, CFeeRate long_term_feerate, CFeeRate discard_feerate, size_t tx_noinputs_size, bool avoid_partial)
Definition: coinselection.h:88
CFeeRate m_effective_feerate
The targeted feerate of the transaction being built.
Definition: coinselection.h:72
bool m_from_me
Whether the UTXOs were sent by the wallet to itself.
const int conf_theirs
Minimum number of confirmations for outputs received from a different wallet.
bool KnapsackSolver(const CAmount &nTargetValue, std::vector< OutputGroup > &groups, std::set< CInputCoin > &setCoinsRet, CAmount &nValueRet)
COutPoint outpoint
Definition: coinselection.h:38
size_t change_output_size
Size of a change output in bytes, determined by the output type.
Definition: coinselection.h:64
size_t m_descendants
The maximum count of descendants of a single UTXO in this output group.
void Insert(const CInputCoin &output, int depth, bool from_me, size_t ancestors, size_t descendants, bool positive_only)
static const CAmount COIN
Definition: amount.h:14
static constexpr CAmount MIN_CHANGE
target minimum change amount
Definition: coinselection.h:14
int m_input_bytes
Pre-computed estimated size of this output as a fully-signed input in a transaction.
Definition: coinselection.h:45
const uint64_t max_descendants
Maximum number of descendants that a single UTXO in the OutputGroup may have.
CAmount effective_value
Definition: coinselection.h:40
CoinEligibilityFilter(int conf_mine, int conf_theirs, uint64_t max_ancestors)
CoinEligibilityFilter(int conf_mine, int conf_theirs, uint64_t max_ancestors, uint64_t max_descendants, bool include_partial)
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
size_t change_spend_size
Size of the input to spend a change output in virtual bytes.
Definition: coinselection.h:66
CAmount fee
The fee to spend these UTXOs at the effective feerate.
Parameters for filtering which OutputGroups we may use in coin selection.
CAmount m_long_term_fee
Definition: coinselection.h:42
const uint64_t max_ancestors
Maximum number of unconfirmed ancestors aggregated across all UTXOs in an OutputGroup.
bool m_avoid_partial_spends
When true, always spend all (up to OUTPUT_GROUP_MAX_ENTRIES) or none of the outputs associated with t...
Definition: coinselection.h:86
CFeeRate m_effective_feerate
The target feerate of the transaction we&#39;re trying to build.
A group of UTXOs paid to the same output script.
std::vector< CInputCoin > m_outputs
The list of UTXOs contained in this output group.
CAmount long_term_fee
The fee to spend these UTXOs at the long term feerate.
bool m_subtract_fee_outputs
Indicate that we are subtracting the fee from outputs.
Definition: coinselection.h:82
CAmount m_change_fee
Cost of creating the change output.
Definition: coinselection.h:68
A UTXO under consideration for use in funding a new transaction.
Definition: coinselection.h:19
An output of a transaction.
Definition: transaction.h:128
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:26
CAmount m_value
The total value of the UTXOs in sum.
static const CAmount MIN_FINAL_CHANGE
final minimum change amount after paying for fees
Definition: coinselection.h:16
size_t tx_noinputs_size
Size of the transaction before coin selection, consisting of the header and recipient output(s)...
Definition: coinselection.h:80
size_t m_ancestors
The aggregated count of unconfirmed ancestors of all UTXOs in this group.
int m_depth
The minimum number of confirmations the UTXOs in the group have.
const bool m_include_partial_groups
When avoid_reuse=true and there are full groups (OUTPUT_GROUP_MAX_ENTRIES), whether or not to use any...
bool operator<(const CInputCoin &rhs) const
Definition: coinselection.h:47
bool operator!=(const CInputCoin &rhs) const
Definition: coinselection.h:51
OutputGroup(const CoinSelectionParams &params)
CoinEligibilityFilter(int conf_mine, int conf_theirs, uint64_t max_ancestors, uint64_t max_descendants)
CAmount m_cost_of_change
Cost of creating the change output + cost of spending the change output in the future.
Definition: coinselection.h:70
CFeeRate m_discard_feerate
If the cost to spend a change output at the discard feerate exceeds its value, drop it to fees...
Definition: coinselection.h:77
bool EligibleForSpending(const CoinEligibilityFilter &eligibility_filter) const
const int conf_mine
Minimum number of confirmations for outputs that we sent to ourselves.
CInputCoin(const CTransactionRef &tx, unsigned int i, int input_bytes)
Definition: coinselection.h:33
CAmount m_fee
Definition: coinselection.h:41
CAmount effective_value
The value of the UTXOs after deducting the cost of spending them at the effective feerate...
CAmount GetSelectionAmount() const
Parameters for one iteration of Coin Selection.
Definition: coinselection.h:61
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:29
bool m_subtract_fee_outputs
Indicate that we are subtracting the fee from outputs.
CInputCoin(const CTransactionRef &tx, unsigned int i)
Definition: coinselection.h:21
CFeeRate m_long_term_feerate
The feerate estimate used to estimate an upper bound on what should be sufficient to spend the change...
Definition: coinselection.h:75
CTxOut txout
Definition: coinselection.h:39
bool operator==(const CInputCoin &rhs) const
Definition: coinselection.h:55
CFeeRate m_long_term_feerate
The feerate for spending a created change output eventually (i.e.