Bitcoin Core  29.1.0
P2P Digital Currency
coinselection.h
Go to the documentation of this file.
1 // Copyright (c) 2017-2022 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 <consensus/amount.h>
9 #include <consensus/consensus.h>
10 #include <outputtype.h>
11 #include <policy/feerate.h>
12 #include <primitives/transaction.h>
13 #include <random.h>
14 #include <util/check.h>
15 #include <util/insert.h>
16 #include <util/result.h>
17 
18 #include <optional>
19 
20 
21 namespace wallet {
23 static constexpr CAmount CHANGE_LOWER{50000};
25 static constexpr CAmount CHANGE_UPPER{1000000};
26 
28 struct COutput {
29 private:
31  std::optional<CAmount> effective_value;
32 
34  std::optional<CAmount> fee;
35 
36 public:
39 
42 
48  int depth;
49 
52 
54  bool spendable;
55 
57  bool solvable;
58 
64  bool safe;
65 
67  int64_t time;
68 
70  bool from_me;
71 
74 
77 
78  COutput(const COutPoint& outpoint, const CTxOut& txout, int depth, int input_bytes, bool spendable, bool solvable, bool safe, int64_t time, bool from_me, const std::optional<CFeeRate> feerate = std::nullopt)
79  : outpoint{outpoint},
80  txout{txout},
81  depth{depth},
85  safe{safe},
86  time{time},
88  {
89  if (feerate) {
90  // base fee without considering potential unconfirmed ancestors
91  fee = input_bytes < 0 ? 0 : feerate.value().GetFee(input_bytes);
92  effective_value = txout.nValue - fee.value();
93  }
94  }
95 
96  COutput(const COutPoint& outpoint, const CTxOut& txout, int depth, int input_bytes, bool spendable, bool solvable, bool safe, int64_t time, bool from_me, const CAmount fees)
98  {
99  // if input_bytes is unknown, then fees should be 0, if input_bytes is known, then the fees should be a positive integer or 0 (input_bytes known and fees = 0 only happens in the tests)
100  assert((input_bytes < 0 && fees == 0) || (input_bytes > 0 && fees >= 0));
101  fee = fees;
102  effective_value = txout.nValue - fee.value();
103  }
104 
105  std::string ToString() const;
106 
107  bool operator<(const COutput& rhs) const
108  {
109  return outpoint < rhs.outpoint;
110  }
111 
112  void ApplyBumpFee(CAmount bump_fee)
113  {
114  assert(bump_fee >= 0);
115  ancestor_bump_fees = bump_fee;
116  assert(fee);
117  *fee += bump_fee;
118  // Note: assert(effective_value - bump_fee == nValue - fee.value());
119  effective_value = txout.nValue - fee.value();
120  }
121 
122  CAmount GetFee() const
123  {
124  assert(fee.has_value());
125  return fee.value();
126  }
127 
129  {
130  assert(effective_value.has_value());
131  return effective_value.value();
132  }
133 
134  bool HasEffectiveValue() const { return effective_value.has_value(); }
135 };
136 
178  std::optional<int> m_max_tx_weight{std::nullopt};
179 
181  CAmount min_change_target, CFeeRate effective_feerate,
182  CFeeRate long_term_feerate, CFeeRate discard_feerate, int tx_noinputs_size, bool avoid_partial,
183  std::optional<int> max_tx_weight = std::nullopt)
184  : rng_fast{rng_fast},
187  m_min_change_target(min_change_target),
188  m_effective_feerate(effective_feerate),
189  m_long_term_feerate(long_term_feerate),
190  m_discard_feerate(discard_feerate),
192  m_avoid_partial_spends(avoid_partial),
193  m_max_tx_weight(max_tx_weight)
194  {
195  }
197  : rng_fast{rng_fast} {}
198 };
199 
204 {
207  const int conf_mine;
209  const int conf_theirs;
211  const uint64_t max_ancestors;
213  const uint64_t max_descendants;
215  const bool m_include_partial_groups{false};
216 
217  CoinEligibilityFilter() = delete;
221 
222  bool operator<(const CoinEligibilityFilter& other) const {
224  < std::tie(other.conf_mine, other.conf_theirs, other.max_ancestors, other.max_descendants, other.m_include_partial_groups);
225  }
226 };
227 
230 {
232  std::vector<std::shared_ptr<COutput>> m_outputs;
236  bool m_from_me{true};
240  int m_depth{999};
243  size_t m_ancestors{0};
245  size_t m_descendants{0};
260  int m_weight{0};
261 
262  OutputGroup() = default;
266  {}
267 
268  void Insert(const std::shared_ptr<COutput>& output, size_t ancestors, size_t descendants);
269  bool EligibleForSpending(const CoinEligibilityFilter& eligibility_filter) const;
270  CAmount GetSelectionAmount() const;
271 };
272 
273 struct Groups {
274  // Stores 'OutputGroup' containing only positive UTXOs (value > 0).
275  std::vector<OutputGroup> positive_group;
276  // Stores 'OutputGroup' which may contain both positive and negative UTXOs.
277  std::vector<OutputGroup> mixed_group;
278 };
279 
282 {
283  // Maps output type to output groups.
284  std::map<OutputType, Groups> groups_by_type;
285  // All inserted groups, no type distinction.
287 
288  // Based on the insert flag; appends group to the 'mixed_group' and, if value > 0, to the 'positive_group'.
289  // This affects both; the groups filtered by type and the overall groups container.
290  void Push(const OutputGroup& group, OutputType type, bool insert_positive, bool insert_mixed);
291  // Different output types count
292  size_t TypesCount() { return groups_by_type.size(); }
293 };
294 
295 typedef std::map<CoinEligibilityFilter, OutputGroupTypeMap> FilteredOutputGroups;
296 
311 [[nodiscard]] CAmount GenerateChangeTarget(const CAmount payment_value, const CAmount change_fee, FastRandomContext& rng);
312 
313 enum class SelectionAlgorithm : uint8_t
314 {
315  BNB = 0,
316  KNAPSACK = 1,
317  SRD = 2,
318  CG = 3,
319  MANUAL = 4,
320 };
321 
322 std::string GetAlgorithmName(const SelectionAlgorithm algo);
323 
325 {
326 private:
328  std::set<std::shared_ptr<COutput>> m_selected_inputs;
334  bool m_use_effective{false};
336  std::optional<CAmount> m_waste;
338  bool m_algo_completed{true};
342  int m_weight{0};
345 
346  template<typename T>
347  void InsertInputs(const T& inputs)
348  {
349  // Store sum of combined input sets to check that the results have no shared UTXOs
350  const size_t expected_count = m_selected_inputs.size() + inputs.size();
352  if (m_selected_inputs.size() != expected_count) {
353  throw std::runtime_error(STR_INTERNAL_BUG("Shared UTXOs among selection results"));
354  }
355  }
356 
357 public:
358  explicit SelectionResult(const CAmount target, SelectionAlgorithm algo)
359  : m_target(target), m_algo(algo) {}
360 
361  SelectionResult() = delete;
362 
364  [[nodiscard]] CAmount GetSelectedValue() const;
365 
366  [[nodiscard]] CAmount GetSelectedEffectiveValue() const;
367 
368  [[nodiscard]] CAmount GetTotalBumpFees() const;
369 
370  void Clear();
371 
372  void AddInput(const OutputGroup& group);
373  void AddInputs(const std::set<std::shared_ptr<COutput>>& inputs, bool subtract_fee_outputs);
374 
376  void SetBumpFeeDiscount(const CAmount discount);
377 
390  void RecalculateWaste(const CAmount min_viable_change, const CAmount change_cost, const CAmount change_fee);
391  [[nodiscard]] CAmount GetWaste() const;
392 
394  void SetAlgoCompleted(bool algo_completed);
395 
397  bool GetAlgoCompleted() const;
398 
400  void SetSelectionsEvaluated(size_t attempts);
401 
403  size_t GetSelectionsEvaluated() const ;
404 
411  void Merge(const SelectionResult& other);
412 
414  const std::set<std::shared_ptr<COutput>>& GetInputSet() const;
416  std::vector<std::shared_ptr<COutput>> GetShuffledInputVector() const;
417 
418  bool operator<(SelectionResult other) const;
419 
437  CAmount GetChange(const CAmount min_viable_change, const CAmount change_fee) const;
438 
439  CAmount GetTarget() const { return m_target; }
440 
441  SelectionAlgorithm GetAlgo() const { return m_algo; }
442 
443  int GetWeight() const { return m_weight; }
444 };
445 
446 util::Result<SelectionResult> SelectCoinsBnB(std::vector<OutputGroup>& utxo_pool, const CAmount& selection_target, const CAmount& cost_of_change,
447  int max_selection_weight);
448 
449 util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& utxo_pool, const CAmount& selection_target, CAmount change_target, int max_selection_weight);
450 
460 util::Result<SelectionResult> SelectCoinsSRD(const std::vector<OutputGroup>& utxo_pool, CAmount target_value, CAmount change_fee, FastRandomContext& rng,
461  int max_selection_weight);
462 
463 // Original coin selection algorithm as a fallback
464 util::Result<SelectionResult> KnapsackSolver(std::vector<OutputGroup>& groups, const CAmount& nTargetValue,
465  CAmount change_target, FastRandomContext& rng, int max_selection_weight);
466 } // namespace wallet
467 
468 #endif // BITCOIN_WALLET_COINSELECTION_H
CAmount nValue
Definition: transaction.h:152
COutput(const COutPoint &outpoint, const CTxOut &txout, int depth, int input_bytes, bool spendable, bool solvable, bool safe, int64_t time, bool from_me, const CAmount fees)
Definition: coinselection.h:96
bool m_subtract_fee_outputs
Indicate that we are subtracting the fee from outputs.
void RecalculateWaste(const CAmount min_viable_change, const CAmount change_cost, const CAmount change_fee)
Calculates and stores the waste for this result given the cost of change and the opportunity cost of ...
COutPoint outpoint
The outpoint identifying this UTXO.
Definition: coinselection.h:38
bool solvable
Whether we know how to spend this output, ignoring the lack of keys.
Definition: coinselection.h:57
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...
CAmount GetWaste() const
FastRandomContext & rng_fast
Randomness to use in the context of coin selection.
util::Result< SelectionResult > KnapsackSolver(std::vector< OutputGroup > &groups, const CAmount &nTargetValue, CAmount change_target, FastRandomContext &rng, int max_selection_weight)
CAmount m_value
The total value of the UTXOs in sum.
COutput(const COutPoint &outpoint, const CTxOut &txout, int depth, int input_bytes, bool spendable, bool solvable, bool safe, int64_t time, bool from_me, const std::optional< CFeeRate > feerate=std::nullopt)
Definition: coinselection.h:78
CAmount min_viable_change
Minimum amount for creating a change output.
CAmount GetEffectiveValue() const
std::map< OutputType, Groups > groups_by_type
Stores several &#39;Groups&#39; whose were mapped by output type.
assert(!tx.IsCoinBase())
CAmount fee
The fee to spend these UTXOs at the effective feerate.
const uint64_t max_descendants
Maximum number of descendants that a single UTXO in the OutputGroup may have.
size_t m_ancestors
The aggregated count of unconfirmed ancestors of all UTXOs in this group.
int tx_noinputs_size
Size of the transaction before coin selection, consisting of the header and recipient output(s)...
void AddInput(const OutputGroup &group)
const uint64_t max_ancestors
Maximum number of unconfirmed ancestors aggregated across all UTXOs in an OutputGroup.
int64_t time
The time of the transaction containing this output as determined by CWalletTx::nTimeSmart.
Definition: coinselection.h:67
void AddInputs(const std::set< std::shared_ptr< COutput >> &inputs, bool subtract_fee_outputs)
int m_weight
Total weight of the UTXOs in this group.
static constexpr CAmount CHANGE_LOWER
lower bound for randomly-chosen target change amount
Definition: coinselection.h:23
CoinSelectionParams(FastRandomContext &rng_fast)
void ApplyBumpFee(CAmount bump_fee)
CoinEligibilityFilter(int conf_mine, int conf_theirs, uint64_t max_ancestors)
bool spendable
Whether we have the private keys to spend this output.
Definition: coinselection.h:54
void insert(Tdst &dst, const Tsrc &src)
Simplification of std insertion.
Definition: insert.h:14
bool operator<(SelectionResult other) const
bool m_use_effective
Whether the input values for calculations should be the effective value (true) or normal value (false...
void SetSelectionsEvaluated(size_t attempts)
Record the number of selections that were evaluated.
CAmount GetChange(const CAmount min_viable_change, const CAmount change_fee) const
Get the amount for the change output after paying needed fees.
CoinEligibilityFilter(int conf_mine, int conf_theirs, uint64_t max_ancestors, uint64_t max_descendants, bool include_partial)
OutputType
Definition: outputtype.h:17
CoinSelectionParams(FastRandomContext &rng_fast, int change_output_size, int change_spend_size, CAmount min_change_target, CFeeRate effective_feerate, CFeeRate long_term_feerate, CFeeRate discard_feerate, int tx_noinputs_size, bool avoid_partial, std::optional< int > max_tx_weight=std::nullopt)
size_t m_selections_evaluated
The count of selections that were evaluated by this coin selection attempt.
util::Result< SelectionResult > CoinGrinder(std::vector< OutputGroup > &utxo_pool, const CAmount &selection_target, CAmount change_target, int max_selection_weight)
CAmount effective_value
The value of the UTXOs after deducting the cost of spending them at the effective feerate...
int change_output_size
Size of a change output in bytes, determined by the output type.
void Push(const OutputGroup &group, OutputType type, bool insert_positive, bool insert_mixed)
SelectionAlgorithm
CTxOut txout
The output itself.
Definition: coinselection.h:41
CAmount GetTarget() const
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
CAmount bump_fee_group_discount
How much individual inputs overestimated the bump fees for the shared ancestry.
bool safe
Whether this output is considered safe to spend.
Definition: coinselection.h:64
bool operator<(const CoinEligibilityFilter &other) const
CFeeRate m_effective_feerate
The targeted feerate of the transaction being built.
std::string ToString() const
CAmount long_term_fee
The fee required to spend this output at the consolidation feerate.
Definition: coinselection.h:73
CAmount GetTotalBumpFees() const
bool EligibleForSpending(const CoinEligibilityFilter &eligibility_filter) const
CoinEligibilityFilter(int conf_mine, int conf_theirs, uint64_t max_ancestors, uint64_t max_descendants)
CAmount m_min_change_target
Mininmum change to target in Knapsack solver and CoinGrinder: select coins to cover the payment and a...
size_t m_descendants
The maximum count of descendants of a single UTXO in this output group.
bool from_me
Whether the transaction containing this output is sent from the owning wallet.
Definition: coinselection.h:70
size_t GetSelectionsEvaluated() const
Get selections_evaluated.
std::vector< OutputGroup > positive_group
CAmount GetSelectedEffectiveValue() const
std::optional< CAmount > fee
The fee required to spend this output at the transaction&#39;s target feerate and to bump its unconfirmed...
Definition: coinselection.h:34
std::set< std::shared_ptr< COutput > > m_selected_inputs
Set of inputs selected by the algorithm to use in the transaction.
util::Result< SelectionResult > SelectCoinsSRD(const std::vector< OutputGroup > &utxo_pool, CAmount target_value, CAmount change_fee, FastRandomContext &rng, int max_selection_weight)
Select coins by Single Random Draw.
SelectionAlgorithm GetAlgo() const
std::optional< CAmount > m_waste
The computed waste.
int depth
Depth in block chain.
Definition: coinselection.h:48
Fast randomness source.
Definition: random.h:376
CFeeRate m_long_term_feerate
The feerate estimate used to estimate an upper bound on what should be sufficient to spend the change...
const std::set< std::shared_ptr< COutput > > & GetInputSet() const
Get m_selected_inputs.
CFeeRate m_long_term_feerate
The feerate for spending a created change output eventually (i.e.
CFeeRate m_discard_feerate
If the cost to spend a change output at the discard feerate exceeds its value, drop it to fees...
CAmount m_cost_of_change
Cost of creating the change output + cost of spending the change output in the future.
bool HasEffectiveValue() const
CAmount GetSelectionAmount() const
SelectionAlgorithm m_algo
The algorithm used to produce this result.
An output of a transaction.
Definition: transaction.h:149
A group of UTXOs paid to the same output script.
void InsertInputs(const T &inputs)
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:28
bool m_avoid_partial_spends
When true, always spend all (up to OUTPUT_GROUP_MAX_ENTRIES) or none of the outputs associated with t...
int m_weight
Total weight of the selected inputs.
Parameters for one iteration of Coin Selection.
std::vector< std::shared_ptr< COutput > > m_outputs
The list of UTXOs contained in this output group.
util::Result< SelectionResult > SelectCoinsBnB(std::vector< OutputGroup > &utxo_pool, const CAmount &selection_target, const CAmount &cost_of_change, int max_selection_weight)
bool m_subtract_fee_outputs
Indicate that we are subtracting the fee from outputs.
Parameters for filtering which OutputGroups we may use in coin selection.
bool m_include_unsafe_inputs
When true, allow unsafe coins to be selected during Coin Selection.
std::vector< OutputGroup > mixed_group
const int conf_mine
Minimum number of confirmations for outputs that we sent to ourselves.
std::optional< int > m_max_tx_weight
The maximum weight for this transaction.
int input_bytes
Pre-computed estimated size of this output as a fully-signed input in a transaction.
Definition: coinselection.h:51
SelectionResult(const CAmount target, SelectionAlgorithm algo)
int m_depth
The minimum number of confirmations the UTXOs in the group have.
OutputGroup(const CoinSelectionParams &params)
std::string GetAlgorithmName(const SelectionAlgorithm algo)
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
Definition: feerate.h:32
void Insert(const std::shared_ptr< COutput > &output, size_t ancestors, size_t descendants)
CAmount GetFee() const
CAmount GetSelectedValue() const
Get the sum of the input values.
#define STR_INTERNAL_BUG(msg)
Definition: check.h:68
CAmount long_term_fee
The fee to spend these UTXOs at the long term feerate.
bool GetAlgoCompleted() const
Get m_algo_completed.
bool m_algo_completed
False if algorithm was cut short by hitting limit of attempts and solution is non-optimal.
bool operator<(const COutput &rhs) const
static constexpr CAmount CHANGE_UPPER
upper bound for randomly-chosen target change amount
Definition: coinselection.h:25
void SetAlgoCompleted(bool algo_completed)
Tracks that algorithm was able to exhaustively search the entire combination space before hitting lim...
bool m_from_me
Whether the UTXOs were sent by the wallet to itself.
std::optional< CAmount > effective_value
The output&#39;s value minus fees required to spend it and bump its unconfirmed ancestors to the target f...
Definition: coinselection.h:31
A UTXO under consideration for use in funding a new transaction.
Definition: coinselection.h:28
std::vector< std::shared_ptr< COutput > > GetShuffledInputVector() const
Get the vector of COutputs that will be used to fill in a CTransaction&#39;s vin.
CAmount GenerateChangeTarget(const CAmount payment_value, const CAmount change_fee, FastRandomContext &rng)
Choose a random change target for each transaction to make it harder to fingerprint the Core wallet b...
const int conf_theirs
Minimum number of confirmations for outputs received from a different wallet.
void SetBumpFeeDiscount(const CAmount discount)
How much individual inputs overestimated the bump fees for shared ancestries.
CAmount ancestor_bump_fees
The fee necessary to bump this UTXO&#39;s ancestor transactions to the target feerate.
Definition: coinselection.h:76
int change_spend_size
Size of the input to spend a change output in virtual bytes.
CAmount m_change_fee
Cost of creating the change output.
void Merge(const SelectionResult &other)
Combines the.
CAmount m_target
The target the algorithm selected for.
std::map< CoinEligibilityFilter, OutputGroupTypeMap > FilteredOutputGroups