Bitcoin Core  0.18.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 
13 static constexpr CAmount MIN_CHANGE{COIN / 100};
15 static const CAmount MIN_FINAL_CHANGE = MIN_CHANGE/2;
16 
17 class CInputCoin {
18 public:
19  CInputCoin(const CTransactionRef& tx, unsigned int i)
20  {
21  if (!tx)
22  throw std::invalid_argument("tx should not be null");
23  if (i >= tx->vout.size())
24  throw std::out_of_range("The output index is out of range");
25 
26  outpoint = COutPoint(tx->GetHash(), i);
27  txout = tx->vout[i];
28  effective_value = txout.nValue;
29  }
30 
31  CInputCoin(const CTransactionRef& tx, unsigned int i, int input_bytes) : CInputCoin(tx, i)
32  {
33  m_input_bytes = input_bytes;
34  }
35 
39 
41  int m_input_bytes{-1};
42 
43  bool operator<(const CInputCoin& rhs) const {
44  return outpoint < rhs.outpoint;
45  }
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 
57 {
58  const int conf_mine;
59  const int conf_theirs;
60  const uint64_t max_ancestors;
61  const uint64_t max_descendants;
62 
63  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) {}
64  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) {}
65 };
66 
68 {
69  std::vector<CInputCoin> m_outputs;
70  bool m_from_me{true};
71  CAmount m_value{0};
72  int m_depth{999};
73  size_t m_ancestors{0};
74  size_t m_descendants{0};
76  CAmount fee{0};
77  CAmount long_term_fee{0};
78 
80  OutputGroup(std::vector<CInputCoin>&& outputs, bool from_me, CAmount value, int depth, size_t ancestors, size_t descendants)
81  : m_outputs(std::move(outputs))
82  , m_from_me(from_me)
83  , m_value(value)
84  , m_depth(depth)
85  , m_ancestors(ancestors)
86  , m_descendants(descendants)
87  {}
88  OutputGroup(const CInputCoin& output, int depth, bool from_me, size_t ancestors, size_t descendants) : OutputGroup() {
89  Insert(output, depth, from_me, ancestors, descendants);
90  }
91  void Insert(const CInputCoin& output, int depth, bool from_me, size_t ancestors, size_t descendants);
92  std::vector<CInputCoin>::iterator Discard(const CInputCoin& output);
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
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:408
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:36
int m_input_bytes
Pre-computed estimated size of this output as a fully-signed input in a transaction.
Definition: coinselection.h:41
const uint64_t max_descendants
Definition: coinselection.h:61
CAmount effective_value
Definition: coinselection.h:38
CoinEligibilityFilter(int conf_mine, int conf_theirs, uint64_t max_ancestors)
Definition: coinselection.h:63
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
OutputGroup(std::vector< CInputCoin > &&outputs, bool from_me, CAmount value, int depth, size_t ancestors, size_t descendants)
Definition: coinselection.h:80
const uint64_t max_ancestors
Definition: coinselection.h:60
std::vector< CInputCoin > m_outputs
Definition: coinselection.h:69
An output of a transaction.
Definition: transaction.h:133
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:18
bool operator<(const CInputCoin &rhs) const
Definition: coinselection.h:43
bool operator!=(const CInputCoin &rhs) const
Definition: coinselection.h:47
CoinEligibilityFilter(int conf_mine, int conf_theirs, uint64_t max_ancestors, uint64_t max_descendants)
Definition: coinselection.h:64
OutputGroup(const CInputCoin &output, int depth, bool from_me, size_t ancestors, size_t descendants)
Definition: coinselection.h:88
CInputCoin(const CTransactionRef &tx, unsigned int i, int input_bytes)
Definition: coinselection.h:31
CInputCoin(const CTransactionRef &tx, unsigned int i)
Definition: coinselection.h:19
CTxOut txout
Definition: coinselection.h:37
bool operator==(const CInputCoin &rhs) const
Definition: coinselection.h:51