Bitcoin Core 30.99.0
P2P Digital Currency
coincontrol.h
Go to the documentation of this file.
1// Copyright (c) 2011-present 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_COINCONTROL_H
6#define BITCOIN_WALLET_COINCONTROL_H
7
8#include <outputtype.h>
9#include <policy/feerate.h>
12#include <script/keyorigin.h>
14#include <util/fees.h>
15
16#include <algorithm>
17#include <map>
18#include <optional>
19#include <set>
20
21namespace wallet {
22const int DEFAULT_MIN_DEPTH = 0;
23const int DEFAULT_MAX_DEPTH = 9999999;
24
26
28static constexpr bool DEFAULT_AVOIDPARTIALSPENDS = false;
29
31{
32private:
34 std::optional<CTxOut> m_txout;
36 std::optional<int64_t> m_weight;
38 std::optional<uint32_t> m_sequence;
40 std::optional<CScript> m_script_sig;
42 std::optional<CScriptWitness> m_script_witness;
44 std::optional<unsigned int> m_pos;
45
46public:
51 void SetTxOut(const CTxOut& txout);
53 CTxOut GetTxOut() const;
55 bool HasTxOut() const;
56
58 void SetInputWeight(int64_t weight);
60 std::optional<int64_t> GetInputWeight() const;
61
63 void SetSequence(uint32_t sequence);
65 std::optional<uint32_t> GetSequence() const;
66
68 void SetScriptSig(const CScript& script);
70 void SetScriptWitness(const CScriptWitness& script_wit);
72 bool HasScripts() const;
74 std::pair<std::optional<CScript>, std::optional<CScriptWitness>> GetScripts() const;
75
77 void SetPosition(unsigned int pos);
79 std::optional<unsigned int> GetPosition() const;
80};
81
84{
85public:
89 std::optional<OutputType> m_change_type;
96 bool fOverrideFeeRate = false;
98 std::optional<CFeeRate> m_feerate;
100 std::optional<unsigned int> m_confirm_target;
102 std::optional<bool> m_signal_bip125_rbf;
118 std::optional<uint32_t> m_locktime;
120 std::optional<int> m_max_tx_weight{std::nullopt};
121
122 CCoinControl();
123
127 bool HasSelected() const;
131 bool IsSelected(const COutPoint& outpoint) const;
135 bool IsExternalSelected(const COutPoint& outpoint) const;
139 std::optional<CTxOut> GetExternalOutput(const COutPoint& outpoint) const;
144 PreselectedInput& Select(const COutPoint& outpoint);
148 void UnSelect(const COutPoint& outpoint);
152 void UnSelectAll();
156 std::vector<COutPoint> ListSelected() const;
160 void SetInputWeight(const COutPoint& outpoint, int64_t weight);
164 std::optional<int64_t> GetInputWeight(const COutPoint& outpoint) const;
166 std::optional<uint32_t> GetSequence(const COutPoint& outpoint) const;
168 std::pair<std::optional<CScript>, std::optional<CScriptWitness>> GetScripts(const COutPoint& outpoint) const;
169
170 bool HasSelectedOrder() const
171 {
172 return m_selection_pos > 0;
173 }
174
175 std::optional<unsigned int> GetSelectionPos(const COutPoint& outpoint) const
176 {
177 const auto it = m_selected.find(outpoint);
178 if (it == m_selected.end()) {
179 return std::nullopt;
180 }
181 return it->second.GetPosition();
182 }
183
184private:
186 std::map<COutPoint, PreselectedInput> m_selected;
187 unsigned int m_selection_pos{0};
188};
189} // namespace wallet
190
191#endif // BITCOIN_WALLET_COINCONTROL_H
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
Definition: addresstype.h:143
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:29
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:405
static const uint32_t CURRENT_VERSION
Definition: transaction.h:284
An output of a transaction.
Definition: transaction.h:140
Coin Control Features.
Definition: coincontrol.h:84
FeeEstimateMode m_fee_mode
Fee estimation mode to control arguments to estimateSmartFee.
Definition: coincontrol.h:108
std::optional< bool > m_signal_bip125_rbf
Override the wallet's m_signal_rbf if set.
Definition: coincontrol.h:102
bool IsSelected(const COutPoint &outpoint) const
Returns true if the given output is pre-selected.
Definition: coincontrol.cpp:20
std::optional< CTxOut > GetExternalOutput(const COutPoint &outpoint) const
Returns the external output for the given outpoint if it exists.
Definition: coincontrol.cpp:31
std::optional< unsigned int > m_confirm_target
Override the default confirmation target if set.
Definition: coincontrol.h:100
std::map< COutPoint, PreselectedInput > m_selected
Selected inputs (inputs that will be used, regardless of whether they're optimal or not)
Definition: coincontrol.h:186
std::optional< int > m_max_tx_weight
Caps weight of resulting tx.
Definition: coincontrol.h:120
std::optional< OutputType > m_change_type
Override the default change type if set, ignored if destChange is set.
Definition: coincontrol.h:89
bool m_avoid_address_reuse
Forbids inclusion of dirty (previously used) addresses.
Definition: coincontrol.h:106
bool HasSelectedOrder() const
Definition: coincontrol.h:170
std::optional< int64_t > GetInputWeight(const COutPoint &outpoint) const
Returns the input weight.
Definition: coincontrol.cpp:72
void UnSelectAll()
Unselects all outputs.
Definition: coincontrol.cpp:52
void UnSelect(const COutPoint &outpoint)
Unselects the given output.
Definition: coincontrol.cpp:47
std::optional< unsigned int > GetSelectionPos(const COutPoint &outpoint) const
Definition: coincontrol.h:175
PreselectedInput & Select(const COutPoint &outpoint)
Lock-in the given output for spending.
Definition: coincontrol.cpp:40
bool HasSelected() const
Returns true if there are pre-selected inputs.
Definition: coincontrol.cpp:15
bool IsExternalSelected(const COutPoint &outpoint) const
Returns true if the given output is selected as an external input.
Definition: coincontrol.cpp:25
std::pair< std::optional< CScript >, std::optional< CScriptWitness > > GetScripts(const COutPoint &outpoint) const
Retrieves the scriptSig and scriptWitness for an input.
Definition: coincontrol.cpp:84
int m_min_depth
Minimum chain depth value for coin availability.
Definition: coincontrol.h:110
bool m_allow_other_inputs
If true, the selection process can add extra unselected inputs from the wallet while requires all sel...
Definition: coincontrol.h:94
int m_max_depth
Maximum chain depth value for coin availability.
Definition: coincontrol.h:112
bool fOverrideFeeRate
Override automatic min/max checks on fee, m_feerate must be set if true.
Definition: coincontrol.h:96
void SetInputWeight(const COutPoint &outpoint, int64_t weight)
Set an input's weight.
Definition: coincontrol.cpp:67
std::optional< CFeeRate > m_feerate
Override the wallet's fee rate if set.
Definition: coincontrol.h:98
bool m_include_unsafe_inputs
If false, only safe inputs will be used.
Definition: coincontrol.h:91
bool m_avoid_partial_spends
Avoid partial use of funds sent to a given address.
Definition: coincontrol.h:104
unsigned int m_selection_pos
Definition: coincontrol.h:187
uint32_t m_version
Version.
Definition: coincontrol.h:116
FlatSigningProvider m_external_provider
SigningProvider that has pubkeys and scripts to do spend size estimation for external inputs.
Definition: coincontrol.h:114
CTxDestination destChange
Custom change destination, if not set an address is generated.
Definition: coincontrol.h:87
std::vector< COutPoint > ListSelected() const
List the selected inputs.
Definition: coincontrol.cpp:57
std::optional< uint32_t > m_locktime
Locktime.
Definition: coincontrol.h:118
std::optional< uint32_t > GetSequence(const COutPoint &outpoint) const
Retrieve the sequence for an input.
Definition: coincontrol.cpp:78
CTxOut GetTxOut() const
Retrieve the previous output for this input.
Definition: coincontrol.cpp:95
std::optional< unsigned int > m_pos
The position in the inputs vector for this input.
Definition: coincontrol.h:44
void SetScriptSig(const CScript &script)
Set the scriptSig for this input.
std::optional< uint32_t > m_sequence
The sequence number for this input.
Definition: coincontrol.h:38
void SetSequence(uint32_t sequence)
Set the sequence for this input.
std::optional< CScript > m_script_sig
The scriptSig for this input.
Definition: coincontrol.h:40
std::optional< uint32_t > GetSequence() const
Retrieve the sequence for this input.
bool HasScripts() const
Return whether either the scriptSig or scriptWitness are set for this input.
void SetScriptWitness(const CScriptWitness &script_wit)
Set the scriptWitness for this input.
std::optional< CTxOut > m_txout
The previous output being spent by this input.
Definition: coincontrol.h:34
std::optional< int64_t > GetInputWeight() const
Retrieve the input weight for this input.
void SetTxOut(const CTxOut &txout)
Set the previous output for this input.
Definition: coincontrol.cpp:90
std::optional< unsigned int > GetPosition() const
Retrieve the position of this input.
void SetInputWeight(int64_t weight)
Set the weight for this input.
std::optional< int64_t > m_weight
The input weight for spending this input.
Definition: coincontrol.h:36
std::optional< CScriptWitness > m_script_witness
The scriptWitness for this input.
Definition: coincontrol.h:42
std::pair< std::optional< CScript >, std::optional< CScriptWitness > > GetScripts() const
Retrieve both the scriptSig and the scriptWitness.
bool HasTxOut() const
Return whether the previous output is set for this input.
void SetPosition(unsigned int pos)
Store the position of this input.
uint64_t sequence
static constexpr bool DEFAULT_AVOIDPARTIALSPENDS
Default for -avoidpartialspends.
Definition: coincontrol.h:28
const int DEFAULT_MIN_DEPTH
Definition: coincontrol.h:22
const int DEFAULT_MAX_DEPTH
Definition: coincontrol.h:23
const int DEFAULT_WALLET_TX_VERSION
Definition: coincontrol.h:25
FeeEstimateMode
Definition: fees.h:9
@ UNSET
Use default settings based on other criteria.