Bitcoin Core  0.20.99
P2P Digital Currency
coinselection.h
Go to the documentation of this file.
1 // Copyright (c) 2017-2018 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>
10 #include <random.h>
11 
12 class CFeeRate;
13 
15 static constexpr CAmount MIN_CHANGE{COIN / 100};
18 
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];
30  effective_value = txout.nValue;
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 
61 {
62  const int conf_mine;
63  const int conf_theirs;
64  const uint64_t max_ancestors;
65  const uint64_t max_descendants;
66 
67  CoinEligibilityFilter(int conf_mine, int conf_theirs, uint64_t max_ancestors) : conf_mine(conf_mine), conf_theirs(conf_theirs), max_ancestors(max_ancestors), max_descendants(max_ancestors) {}
68  CoinEligibilityFilter(int conf_mine, int conf_theirs, uint64_t max_ancestors, uint64_t max_descendants) : conf_mine(conf_mine), conf_theirs(conf_theirs), max_ancestors(max_ancestors), max_descendants(max_descendants) {}
69 };
70 
72 {
73  std::vector<CInputCoin> m_outputs;
74  bool m_from_me{true};
75  CAmount m_value{0};
76  int m_depth{999};
77  size_t m_ancestors{0};
78  size_t m_descendants{0};
80  CAmount fee{0};
81  CAmount long_term_fee{0};
82 
84  OutputGroup(std::vector<CInputCoin>&& outputs, bool from_me, CAmount value, int depth, size_t ancestors, size_t descendants)
85  : m_outputs(std::move(outputs))
86  , m_from_me(from_me)
87  , m_value(value)
88  , m_depth(depth)
89  , m_ancestors(ancestors)
90  , m_descendants(descendants)
91  {}
92  OutputGroup(const CInputCoin& output, int depth, bool from_me, size_t ancestors, size_t descendants) : OutputGroup() {
93  Insert(output, depth, from_me, ancestors, descendants);
94  }
95  void Insert(const CInputCoin& output, int depth, bool from_me, size_t ancestors, size_t descendants);
96  std::vector<CInputCoin>::iterator Discard(const CInputCoin& output);
97  bool EligibleForSpending(const CoinEligibilityFilter& eligibility_filter) const;
98 
100  void SetFees(const CFeeRate effective_feerate, const CFeeRate long_term_feerate);
101  OutputGroup GetPositiveOnlyGroup();
102 };
103 
104 bool SelectCoinsBnB(std::vector<OutputGroup>& utxo_pool, const CAmount& target_value, const CAmount& cost_of_change, std::set<CInputCoin>& out_set, CAmount& value_ret, CAmount not_input_fees);
105 
106 // Original coin selection algorithm as a fallback
107 bool KnapsackSolver(const CAmount& nTargetValue, std::vector<OutputGroup>& groups, std::set<CInputCoin>& setCoinsRet, CAmount& nValueRet);
108 
109 #endif // BITCOIN_WALLET_COINSELECTION_H
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:395
bool SelectCoinsBnB(std::vector< OutputGroup > &utxo_pool, const CAmount &target_value, const CAmount &cost_of_change, std::set< CInputCoin > &out_set, CAmount &value_ret, CAmount not_input_fees)
bool KnapsackSolver(const CAmount &nTargetValue, std::vector< OutputGroup > &groups, std::set< CInputCoin > &setCoinsRet, CAmount &nValueRet)
COutPoint outpoint
Definition: coinselection.h:38
static const CAmount COIN
Definition: amount.h:14
static constexpr CAmount MIN_CHANGE
target minimum change amount
Definition: coinselection.h:15
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
Definition: coinselection.h:65
CAmount effective_value
Definition: coinselection.h:40
CoinEligibilityFilter(int conf_mine, int conf_theirs, uint64_t max_ancestors)
Definition: coinselection.h:67
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
CAmount m_long_term_fee
Definition: coinselection.h:42
OutputGroup(std::vector< CInputCoin > &&outputs, bool from_me, CAmount value, int depth, size_t ancestors, size_t descendants)
Definition: coinselection.h:84
const uint64_t max_ancestors
Definition: coinselection.h:64
std::vector< CInputCoin > m_outputs
Definition: coinselection.h:73
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
static const CAmount MIN_FINAL_CHANGE
final minimum change amount after paying for fees
Definition: coinselection.h:17
bool operator<(const CInputCoin &rhs) const
Definition: coinselection.h:47
bool operator!=(const CInputCoin &rhs) const
Definition: coinselection.h:51
CoinEligibilityFilter(int conf_mine, int conf_theirs, uint64_t max_ancestors, uint64_t max_descendants)
Definition: coinselection.h:68
OutputGroup(const CInputCoin &output, int depth, bool from_me, size_t ancestors, size_t descendants)
Definition: coinselection.h:92
CInputCoin(const CTransactionRef &tx, unsigned int i, int input_bytes)
Definition: coinselection.h:33
CAmount m_fee
Definition: coinselection.h:41
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:29
CInputCoin(const CTransactionRef &tx, unsigned int i)
Definition: coinselection.h:21
CTxOut txout
Definition: coinselection.h:39
bool operator==(const CInputCoin &rhs) const
Definition: coinselection.h:55