Bitcoin Core  21.99.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 
18 class CInputCoin {
19 public:
20  CInputCoin(const CTransactionRef& tx, unsigned int i)
21  {
22  if (!tx)
23  throw std::invalid_argument("tx should not be null");
24  if (i >= tx->vout.size())
25  throw std::out_of_range("The output index is out of range");
26 
27  outpoint = COutPoint(tx->GetHash(), i);
28  txout = tx->vout[i];
30  }
31 
32  CInputCoin(const CTransactionRef& tx, unsigned int i, int input_bytes) : CInputCoin(tx, i)
33  {
34  m_input_bytes = input_bytes;
35  }
36 
42 
44  int m_input_bytes{-1};
45 
46  bool operator<(const CInputCoin& rhs) const {
47  return outpoint < rhs.outpoint;
48  }
49 
50  bool operator!=(const CInputCoin& rhs) const {
51  return outpoint != rhs.outpoint;
52  }
53 
54  bool operator==(const CInputCoin& rhs) const {
55  return outpoint == rhs.outpoint;
56  }
57 };
58 
60 {
61  const int conf_mine;
62  const int conf_theirs;
63  const uint64_t max_ancestors;
64  const uint64_t max_descendants;
65  const bool m_include_partial_groups{false};
66 
70 };
71 
73 {
74  std::vector<CInputCoin> m_outputs;
75  bool m_from_me{true};
77  int m_depth{999};
78  size_t m_ancestors{0};
79  size_t m_descendants{0};
85 
87  OutputGroup(const CFeeRate& effective_feerate, const CFeeRate& long_term_feerate) :
88  m_effective_feerate(effective_feerate),
89  m_long_term_feerate(long_term_feerate)
90  {}
91 
92  void Insert(const CInputCoin& output, int depth, bool from_me, size_t ancestors, size_t descendants, bool positive_only);
93  bool EligibleForSpending(const CoinEligibilityFilter& eligibility_filter) const;
94 };
95 
96 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);
97 
98 // Original coin selection algorithm as a fallback
99 bool KnapsackSolver(const CAmount& nTargetValue, std::vector<OutputGroup>& groups, std::set<CInputCoin>& setCoinsRet, CAmount& nValueRet);
100 
101 #endif // BITCOIN_WALLET_COINSELECTION_H
OutputGroup::Insert
void Insert(const CInputCoin &output, int depth, bool from_me, size_t ancestors, size_t descendants, bool positive_only)
Definition: coinselection.cpp:304
OutputGroup::m_depth
int m_depth
Definition: coinselection.h:77
feerate.h
OutputGroup::OutputGroup
OutputGroup(const CFeeRate &effective_feerate, const CFeeRate &long_term_feerate)
Definition: coinselection.h:87
CoinEligibilityFilter::max_descendants
const uint64_t max_descendants
Definition: coinselection.h:64
MIN_CHANGE
static constexpr CAmount MIN_CHANGE
target minimum change amount
Definition: coinselection.h:14
OutputGroup
Definition: coinselection.h:72
OutputGroup::OutputGroup
OutputGroup()
Definition: coinselection.h:86
transaction.h
CoinEligibilityFilter::conf_theirs
const int conf_theirs
Definition: coinselection.h:62
CoinEligibilityFilter::m_include_partial_groups
const bool m_include_partial_groups
Definition: coinselection.h:65
CInputCoin::operator!=
bool operator!=(const CInputCoin &rhs) const
Definition: coinselection.h:50
CoinEligibilityFilter::CoinEligibilityFilter
CoinEligibilityFilter(int conf_mine, int conf_theirs, uint64_t max_ancestors)
Include partial destination groups when avoid_reuse and there are full groups.
Definition: coinselection.h:67
OutputGroup::m_long_term_feerate
CFeeRate m_long_term_feerate
Definition: coinselection.h:84
CoinEligibilityFilter::max_ancestors
const uint64_t max_ancestors
Definition: coinselection.h:63
OutputGroup::effective_value
CAmount effective_value
Definition: coinselection.h:80
CInputCoin::effective_value
CAmount effective_value
Definition: coinselection.h:39
CInputCoin::m_fee
CAmount m_fee
Definition: coinselection.h:40
CoinEligibilityFilter::CoinEligibilityFilter
CoinEligibilityFilter(int conf_mine, int conf_theirs, uint64_t max_ancestors, uint64_t max_descendants)
Definition: coinselection.h:68
CoinEligibilityFilter
Definition: coinselection.h:59
CTransactionRef
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:386
OutputGroup::m_outputs
std::vector< CInputCoin > m_outputs
Definition: coinselection.h:74
CFeeRate
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:29
OutputGroup::m_effective_feerate
CFeeRate m_effective_feerate
Definition: coinselection.h:82
CTxOut::nValue
CAmount nValue
Definition: transaction.h:131
CInputCoin::CInputCoin
CInputCoin(const CTransactionRef &tx, unsigned int i)
Definition: coinselection.h:20
CInputCoin::outpoint
COutPoint outpoint
Definition: coinselection.h:37
random.h
OutputGroup::long_term_fee
CAmount long_term_fee
Definition: coinselection.h:83
CTxOut
An output of a transaction.
Definition: transaction.h:128
MIN_FINAL_CHANGE
static const CAmount MIN_FINAL_CHANGE
final minimum change amount after paying for fees
Definition: coinselection.h:16
CInputCoin::operator==
bool operator==(const CInputCoin &rhs) const
Definition: coinselection.h:54
CInputCoin::txout
CTxOut txout
Definition: coinselection.h:38
OutputGroup::m_value
CAmount m_value
Definition: coinselection.h:76
CAmount
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
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:44
CoinEligibilityFilter::CoinEligibilityFilter
CoinEligibilityFilter(int conf_mine, int conf_theirs, uint64_t max_ancestors, uint64_t max_descendants, bool include_partial)
Definition: coinselection.h:69
OutputGroup::fee
CAmount fee
Definition: coinselection.h:81
CInputCoin
Definition: coinselection.h:18
CoinEligibilityFilter::conf_mine
const int conf_mine
Definition: coinselection.h:61
CInputCoin::operator<
bool operator<(const CInputCoin &rhs) const
Definition: coinselection.h:46
OutputGroup::m_from_me
bool m_from_me
Definition: coinselection.h:75
COIN
static const CAmount COIN
Definition: amount.h:14
CInputCoin::CInputCoin
CInputCoin(const CTransactionRef &tx, unsigned int i, int input_bytes)
Definition: coinselection.h:32
COutPoint
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:26
OutputGroup::m_descendants
size_t m_descendants
Definition: coinselection.h:79
SelectCoinsBnB
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)
Definition: coinselection.cpp:66
OutputGroup::m_ancestors
size_t m_ancestors
Definition: coinselection.h:78
amount.h
CInputCoin::m_long_term_fee
CAmount m_long_term_fee
Definition: coinselection.h:41
KnapsackSolver
bool KnapsackSolver(const CAmount &nTargetValue, std::vector< OutputGroup > &groups, std::set< CInputCoin > &setCoinsRet, CAmount &nValueRet)
Definition: coinselection.cpp:220
OutputGroup::EligibleForSpending
bool EligibleForSpending(const CoinEligibilityFilter &eligibility_filter) const
Definition: coinselection.cpp:336