Bitcoin ABC 0.30.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 <consensus/amount.h>
10#include <random.h>
11
13static constexpr Amount MIN_CHANGE{COIN / 100};
16
18public:
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];
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
42
48
49 bool operator<(const CInputCoin &rhs) const {
50 return outpoint < rhs.outpoint;
51 }
52
53 bool operator!=(const CInputCoin &rhs) const {
54 return outpoint != rhs.outpoint;
55 }
56
57 bool operator==(const CInputCoin &rhs) const {
58 return outpoint == rhs.outpoint;
59 }
60};
61
63 const int conf_mine;
68 const int conf_theirs;
71 const bool m_include_partial_groups{false};
72
73 CoinEligibilityFilter(int conf_mine_, int conf_theirs_)
74 : conf_mine(conf_mine_), conf_theirs(conf_theirs_) {}
75 CoinEligibilityFilter(int conf_mine_, int conf_theirs_,
76 bool include_partial_groups)
77 : conf_mine(conf_mine_), conf_theirs(conf_theirs_),
78 m_include_partial_groups(include_partial_groups) {}
79};
80
82 std::vector<CInputCoin> m_outputs;
83 bool m_from_me{true};
85 int m_depth{999};
91
93 OutputGroup(const CFeeRate &effective_feerate,
94 const CFeeRate &long_term_feerate)
95 : m_effective_feerate(effective_feerate),
96 m_long_term_feerate(long_term_feerate) {}
97
98 void Insert(const CInputCoin &output, int depth, bool from_me,
99 bool positive_only);
100 bool
101 EligibleForSpending(const CoinEligibilityFilter &eligibility_filter) const;
102};
103
104bool 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
110bool KnapsackSolver(const Amount nTargetValue, std::vector<OutputGroup> &groups,
111 std::set<CInputCoin> &setCoinsRet, Amount &nValueRet);
112
113#endif // BITCOIN_WALLET_COINSELECTION_H
static constexpr Amount COIN
Definition: amount.h:144
Fee rate in satoshis per kilobyte: Amount / kB.
Definition: feerate.h:21
int m_input_bytes
Pre-computed estimated size of this output as a fully-signed input in a transaction.
Definition: coinselection.h:47
bool operator==(const CInputCoin &rhs) const
Definition: coinselection.h:57
Amount m_fee
Definition: coinselection.h:40
CInputCoin(const CTransactionRef &tx, unsigned int i)
Definition: coinselection.h:19
Amount m_long_term_fee
Definition: coinselection.h:41
CInputCoin(const CTransactionRef &tx, unsigned int i, int input_bytes)
Definition: coinselection.h:32
CTxOut txout
Definition: coinselection.h:38
Amount effective_value
Definition: coinselection.h:39
bool operator!=(const CInputCoin &rhs) const
Definition: coinselection.h:53
bool operator<(const CInputCoin &rhs) const
Definition: coinselection.h:49
COutPoint outpoint
Definition: coinselection.h:37
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:20
An output of a transaction.
Definition: transaction.h:128
Amount nValue
Definition: transaction.h:130
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
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)
This is the Branch and Bound Coin Selection algorithm designed by Murch.
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:315
Definition: amount.h:19
static constexpr Amount zero() noexcept
Definition: amount.h:32
CoinEligibilityFilter(int conf_mine_, int conf_theirs_, bool include_partial_groups)
Definition: coinselection.h:75
const int conf_theirs
Minimum number of confirmations for outputs received from a different wallet.
Definition: coinselection.h:68
CoinEligibilityFilter(int conf_mine_, int conf_theirs_)
Definition: coinselection.h:73
const bool m_include_partial_groups
Include partial destination groups when avoid_reuse and there are full groups.
Definition: coinselection.h:71
std::vector< CInputCoin > m_outputs
Definition: coinselection.h:82
Amount m_value
Definition: coinselection.h:84
CFeeRate m_long_term_feerate
Definition: coinselection.h:90
Amount long_term_fee
Definition: coinselection.h:89
CFeeRate m_effective_feerate
Definition: coinselection.h:88
void Insert(const CInputCoin &output, int depth, bool from_me, bool positive_only)
OutputGroup(const CFeeRate &effective_feerate, const CFeeRate &long_term_feerate)
Definition: coinselection.h:93
Amount effective_value
Definition: coinselection.h:86
bool EligibleForSpending(const CoinEligibilityFilter &eligibility_filter) const