Bitcoin ABC  0.24.7
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 
12 class CFeeRate;
13 
15 static constexpr Amount MIN_CHANGE{COIN / 100};
17 static const Amount MIN_FINAL_CHANGE = MIN_CHANGE / 2;
18 
19 class CInputCoin {
20 public:
21  CInputCoin(const CTransactionRef &tx, unsigned int i) {
22  if (!tx) {
23  throw std::invalid_argument("tx should not be null");
24  }
25  if (i >= tx->vout.size()) {
26  throw std::out_of_range("The output index is out of range");
27  }
28 
29  outpoint = COutPoint(tx->GetId(), i);
30  txout = tx->vout[i];
32  }
33 
34  CInputCoin(const CTransactionRef &tx, unsigned int i, int input_bytes)
35  : CInputCoin(tx, i) {
36  m_input_bytes = input_bytes;
37  }
38 
44 
49  int m_input_bytes{-1};
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  bool operator==(const CInputCoin &rhs) const {
60  return outpoint == rhs.outpoint;
61  }
62 };
63 
65  const int conf_mine;
66  const int conf_theirs;
67  const uint64_t max_ancestors;
68  const uint64_t max_descendants;
69 
70  CoinEligibilityFilter(int conf_mine_, int conf_theirs_,
71  uint64_t max_ancestors_)
72  : conf_mine(conf_mine_), conf_theirs(conf_theirs_),
73  max_ancestors(max_ancestors_), max_descendants(max_ancestors_) {}
74  CoinEligibilityFilter(int conf_mine_, int conf_theirs_,
75  uint64_t max_ancestors_, uint64_t max_descendants_)
76  : conf_mine(conf_mine_), conf_theirs(conf_theirs_),
77  max_ancestors(max_ancestors_), max_descendants(max_descendants_) {}
78 };
79 
80 struct OutputGroup {
81  std::vector<CInputCoin> m_outputs;
82  bool m_from_me{true};
84  int m_depth{999};
85  size_t m_ancestors{0};
86  size_t m_descendants{0};
90 
92  OutputGroup(std::vector<CInputCoin> &&outputs, bool from_me, Amount value,
93  int depth, size_t ancestors, size_t descendants)
94  : m_outputs(std::move(outputs)), m_from_me(from_me), m_value(value),
95  m_depth(depth), m_ancestors(ancestors), m_descendants(descendants) {}
96  OutputGroup(const CInputCoin &output, int depth, bool from_me,
97  size_t ancestors, size_t descendants)
98  : OutputGroup() {
99  Insert(output, depth, from_me, ancestors, descendants);
100  }
101  void Insert(const CInputCoin &output, int depth, bool from_me,
102  size_t ancestors, size_t descendants);
103  std::vector<CInputCoin>::iterator Discard(const CInputCoin &output);
104  bool
105  EligibleForSpending(const CoinEligibilityFilter &eligibility_filter) const;
106 
109  void SetFees(const CFeeRate effective_feerate,
110  const CFeeRate long_term_feerate);
112 };
113 
114 bool SelectCoinsBnB(std::vector<OutputGroup> &utxo_pool,
115  const Amount &target_value, const Amount &cost_of_change,
116  std::set<CInputCoin> &out_set, Amount &value_ret,
117  const Amount not_input_fees);
118 
119 // Original coin selection algorithm as a fallback
120 bool KnapsackSolver(const Amount nTargetValue, std::vector<OutputGroup> &groups,
121  std::set<CInputCoin> &setCoinsRet, Amount &nValueRet);
122 
123 #endif // BITCOIN_WALLET_COINSELECTION_H
MIN_CHANGE
static constexpr Amount MIN_CHANGE
target minimum change amount
Definition: coinselection.h:15
CInputCoin::m_long_term_fee
Amount m_long_term_fee
Definition: coinselection.h:43
OutputGroup::m_depth
int m_depth
Definition: coinselection.h:84
CTxOut::nValue
Amount nValue
Definition: transaction.h:132
OutputGroup::fee
Amount fee
Definition: coinselection.h:88
CoinEligibilityFilter::max_descendants
const uint64_t max_descendants
Definition: coinselection.h:68
OutputGroup
Definition: coinselection.h:80
OutputGroup::OutputGroup
OutputGroup()
Definition: coinselection.h:91
transaction.h
CoinEligibilityFilter::conf_theirs
const int conf_theirs
Definition: coinselection.h:66
CInputCoin::operator!=
bool operator!=(const CInputCoin &rhs) const
Definition: coinselection.h:55
CoinEligibilityFilter::max_ancestors
const uint64_t max_ancestors
Definition: coinselection.h:67
CoinEligibilityFilter::CoinEligibilityFilter
CoinEligibilityFilter(int conf_mine_, int conf_theirs_, uint64_t max_ancestors_)
Definition: coinselection.h:70
KnapsackSolver
bool KnapsackSolver(const Amount nTargetValue, std::vector< OutputGroup > &groups, std::set< CInputCoin > &setCoinsRet, Amount &nValueRet)
Definition: coinselection.cpp:255
CoinEligibilityFilter
Definition: coinselection.h:64
OutputGroup::m_outputs
std::vector< CInputCoin > m_outputs
Definition: coinselection.h:81
CFeeRate
Fee rate in satoshis per kilobyte: Amount / kB.
Definition: feerate.h:21
COIN
static constexpr Amount COIN
Definition: amount.h:154
Amount::zero
static constexpr Amount zero()
Definition: amount.h:42
CInputCoin::CInputCoin
CInputCoin(const CTransactionRef &tx, unsigned int i)
Definition: coinselection.h:21
SelectCoinsBnB
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)
Definition: coinselection.cpp:72
OutputGroup::OutputGroup
OutputGroup(const CInputCoin &output, int depth, bool from_me, size_t ancestors, size_t descendants)
Definition: coinselection.h:96
CInputCoin::outpoint
COutPoint outpoint
Definition: coinselection.h:39
random.h
CTxOut
An output of a transaction.
Definition: transaction.h:130
CoinEligibilityFilter::CoinEligibilityFilter
CoinEligibilityFilter(int conf_mine_, int conf_theirs_, uint64_t max_ancestors_, uint64_t max_descendants_)
Definition: coinselection.h:74
OutputGroup::long_term_fee
Amount long_term_fee
Definition: coinselection.h:89
CInputCoin::operator==
bool operator==(const CInputCoin &rhs) const
Definition: coinselection.h:59
CInputCoin::m_fee
Amount m_fee
Definition: coinselection.h:42
CInputCoin::txout
CTxOut txout
Definition: coinselection.h:40
OutputGroup::Discard
std::vector< CInputCoin >::iterator Discard(const CInputCoin &output)
Definition: coinselection.cpp:370
CInputCoin::m_input_bytes
int m_input_bytes
Pre-computed estimated size of this output as a fully-signed input in a transaction.
Definition: coinselection.h:49
Amount
Definition: amount.h:19
CInputCoin
Definition: coinselection.h:19
CoinEligibilityFilter::conf_mine
const int conf_mine
Definition: coinselection.h:65
OutputGroup::m_value
Amount m_value
Definition: coinselection.h:83
MIN_FINAL_CHANGE
static const Amount MIN_FINAL_CHANGE
final minimum change amount after paying for fees
Definition: coinselection.h:17
CInputCoin::operator<
bool operator<(const CInputCoin &rhs) const
Definition: coinselection.h:51
CTransactionRef
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:319
OutputGroup::SetFees
void SetFees(const CFeeRate effective_feerate, const CFeeRate long_term_feerate)
Update the OutputGroup's fee, long_term_fee, and effective_value based on the given feerates.
Definition: coinselection.cpp:393
OutputGroup::m_from_me
bool m_from_me
Definition: coinselection.h:82
OutputGroup::effective_value
Amount effective_value
Definition: coinselection.h:87
CInputCoin::CInputCoin
CInputCoin(const CTransactionRef &tx, unsigned int i, int input_bytes)
Definition: coinselection.h:34
COutPoint
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
OutputGroup::OutputGroup
OutputGroup(std::vector< CInputCoin > &&outputs, bool from_me, Amount value, int depth, size_t ancestors, size_t descendants)
Definition: coinselection.h:92
OutputGroup::m_descendants
size_t m_descendants
Definition: coinselection.h:86
OutputGroup::Insert
void Insert(const CInputCoin &output, int depth, bool from_me, size_t ancestors, size_t descendants)
Definition: coinselection.cpp:350
OutputGroup::m_ancestors
size_t m_ancestors
Definition: coinselection.h:85
OutputGroup::GetPositiveOnlyGroup
OutputGroup GetPositiveOnlyGroup()
Definition: coinselection.cpp:415
amount.h
CInputCoin::effective_value
Amount effective_value
Definition: coinselection.h:41
OutputGroup::EligibleForSpending
bool EligibleForSpending(const CoinEligibilityFilter &eligibility_filter) const
Definition: coinselection.cpp:385