Bitcoin ABC  0.23.2
P2P Digital Currency
coinselection.h
Go to the documentation of this file.
1 // Copyright (c) 2017 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 
13 static constexpr Amount MIN_CHANGE{COIN / 100};
15 static const Amount MIN_FINAL_CHANGE = MIN_CHANGE / 2;
16 
17 class CInputCoin {
18 public:
19  CInputCoin(const CTransactionRef &tx, unsigned int i) {
20  if (!tx) {
21  throw std::invalid_argument("tx should not be null");
22  }
23  if (i >= tx->vout.size()) {
24  throw std::out_of_range("The output index is out of range");
25  }
26 
27  outpoint = COutPoint(tx->GetId(), i);
28  txout = tx->vout[i];
29  effective_value = txout.nValue;
30  }
31 
32  CInputCoin(const CTransactionRef &tx, unsigned int i, int input_bytes)
33  : CInputCoin(tx, i) {
34  m_input_bytes = input_bytes;
35  }
36 
40 
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  const int conf_mine;
62  const int conf_theirs;
63  const uint64_t max_ancestors;
64  const uint64_t max_descendants;
65 
66  CoinEligibilityFilter(int conf_mine_, int conf_theirs_,
67  uint64_t max_ancestors_)
68  : conf_mine(conf_mine_), conf_theirs(conf_theirs_),
69  max_ancestors(max_ancestors_), max_descendants(max_ancestors_) {}
70  CoinEligibilityFilter(int conf_mine_, int conf_theirs_,
71  uint64_t max_ancestors_, uint64_t max_descendants_)
72  : conf_mine(conf_mine_), conf_theirs(conf_theirs_),
73  max_ancestors(max_ancestors_), max_descendants(max_descendants_) {}
74 };
75 
76 struct OutputGroup {
77  std::vector<CInputCoin> m_outputs;
78  bool m_from_me{true};
79  Amount m_value = Amount::zero();
80  int m_depth{999};
81  size_t m_ancestors{0};
82  size_t m_descendants{0};
85  Amount long_term_fee = Amount::zero();
86 
88  OutputGroup(std::vector<CInputCoin> &&outputs, bool from_me, Amount value,
89  int depth, size_t ancestors, size_t descendants)
90  : m_outputs(std::move(outputs)), m_from_me(from_me), m_value(value),
91  m_depth(depth), m_ancestors(ancestors), m_descendants(descendants) {}
92  OutputGroup(const CInputCoin &output, int depth, bool from_me,
93  size_t ancestors, size_t descendants)
94  : OutputGroup() {
95  Insert(output, depth, from_me, ancestors, descendants);
96  }
97  void Insert(const CInputCoin &output, int depth, bool from_me,
98  size_t ancestors, size_t descendants);
99  std::vector<CInputCoin>::iterator Discard(const CInputCoin &output);
100  bool
101  EligibleForSpending(const CoinEligibilityFilter &eligibility_filter) const;
102 };
103 
104 bool SelectCoinsBnB(std::vector<OutputGroup> &utxo_pool,
105  const Amount &target_value, const Amount &cost_of_change,
106  std::set<CInputCoin> &out_set, Amount &value_ret,
107  const Amount not_input_fees);
108 
109 // Original coin selection algorithm as a fallback
110 bool KnapsackSolver(const Amount nTargetValue, std::vector<OutputGroup> &groups,
111  std::set<CInputCoin> &setCoinsRet, Amount &nValueRet);
112 
113 #endif // BITCOIN_WALLET_COINSELECTION_H
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:319
Amount effective_value
Definition: coinselection.h:39
static constexpr Amount zero()
Definition: amount.h:35
OutputGroup(std::vector< CInputCoin > &&outputs, bool from_me, Amount value, int depth, size_t ancestors, size_t descendants)
Definition: coinselection.h:88
COutPoint outpoint
Definition: coinselection.h:37
Definition: amount.h:17
CoinEligibilityFilter(int conf_mine_, int conf_theirs_, uint64_t max_ancestors_, uint64_t max_descendants_)
Definition: coinselection.h:70
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:64
bool KnapsackSolver(const Amount nTargetValue, std::vector< OutputGroup > &groups, std::set< CInputCoin > &setCoinsRet, Amount &nValueRet)
bool SelectCoinsBnB(std::vector< OutputGroup > &utxo_pool, const Amount &target_value, const Amount &cost_of_change, std::set< CInputCoin > &out_set, Amount &value_ret, const Amount not_input_fees)
const uint64_t max_ancestors
Definition: coinselection.h:63
static constexpr Amount COIN
Definition: amount.h:148
std::vector< CInputCoin > m_outputs
Definition: coinselection.h:77
An output of a transaction.
Definition: transaction.h:130
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
bool operator<(const CInputCoin &rhs) const
Definition: coinselection.h:47
bool operator!=(const CInputCoin &rhs) const
Definition: coinselection.h:51
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:32
static constexpr Amount MIN_CHANGE
target minimum change amount
Definition: coinselection.h:13
static const Amount MIN_FINAL_CHANGE
final minimum change amount after paying for fees
Definition: coinselection.h:15
CInputCoin(const CTransactionRef &tx, unsigned int i)
Definition: coinselection.h:19
CTxOut txout
Definition: coinselection.h:38
bool operator==(const CInputCoin &rhs) const
Definition: coinselection.h:55
CoinEligibilityFilter(int conf_mine_, int conf_theirs_, uint64_t max_ancestors_)
Definition: coinselection.h:66