Bitcoin Core 28.99.0
P2P Digital Currency
coincontrol.h
Go to the documentation of this file.
1// Copyright (c) 2011-2022 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>
10#include <policy/fees.h>
12#include <script/keyorigin.h>
14
15#include <algorithm>
16#include <map>
17#include <optional>
18#include <set>
19
20namespace wallet {
21const int DEFAULT_MIN_DEPTH = 0;
22const int DEFAULT_MAX_DEPTH = 9999999;
23
25static constexpr bool DEFAULT_AVOIDPARTIALSPENDS = false;
26
28{
29private:
31 std::optional<CTxOut> m_txout;
33 std::optional<int64_t> m_weight;
35 std::optional<uint32_t> m_sequence;
37 std::optional<CScript> m_script_sig;
39 std::optional<CScriptWitness> m_script_witness;
41 std::optional<unsigned int> m_pos;
42
43public:
48 void SetTxOut(const CTxOut& txout);
50 CTxOut GetTxOut() const;
52 bool HasTxOut() const;
53
55 void SetInputWeight(int64_t weight);
57 std::optional<int64_t> GetInputWeight() const;
58
60 void SetSequence(uint32_t sequence);
62 std::optional<uint32_t> GetSequence() const;
63
65 void SetScriptSig(const CScript& script);
67 void SetScriptWitness(const CScriptWitness& script_wit);
69 bool HasScripts() const;
71 std::pair<std::optional<CScript>, std::optional<CScriptWitness>> GetScripts() const;
72
74 void SetPosition(unsigned int pos);
76 std::optional<unsigned int> GetPosition() const;
77};
78
81{
82public:
86 std::optional<OutputType> m_change_type;
93 bool fAllowWatchOnly = false;
95 bool fOverrideFeeRate = false;
97 std::optional<CFeeRate> m_feerate;
99 std::optional<unsigned int> m_confirm_target;
101 std::optional<bool> m_signal_bip125_rbf;
115 std::optional<uint32_t> m_locktime;
117 std::optional<uint32_t> m_version;
119 std::optional<int> m_max_tx_weight{std::nullopt};
120
121 CCoinControl();
122
126 bool HasSelected() const;
130 bool IsSelected(const COutPoint& outpoint) const;
134 bool IsExternalSelected(const COutPoint& outpoint) const;
138 std::optional<CTxOut> GetExternalOutput(const COutPoint& outpoint) const;
143 PreselectedInput& Select(const COutPoint& outpoint);
147 void UnSelect(const COutPoint& outpoint);
151 void UnSelectAll();
155 std::vector<COutPoint> ListSelected() const;
159 void SetInputWeight(const COutPoint& outpoint, int64_t weight);
163 std::optional<int64_t> GetInputWeight(const COutPoint& outpoint) const;
165 std::optional<uint32_t> GetSequence(const COutPoint& outpoint) const;
167 std::pair<std::optional<CScript>, std::optional<CScriptWitness>> GetScripts(const COutPoint& outpoint) const;
168
169 bool HasSelectedOrder() const
170 {
171 return m_selection_pos > 0;
172 }
173
174 std::optional<unsigned int> GetSelectionPos(const COutPoint& outpoint) const
175 {
176 const auto it = m_selected.find(outpoint);
177 if (it == m_selected.end()) {
178 return std::nullopt;
179 }
180 return it->second.GetPosition();
181 }
182
183private:
185 std::map<COutPoint, PreselectedInput> m_selected;
186 unsigned int m_selection_pos{0};
187};
188} // namespace wallet
189
190#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:140
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:415
An output of a transaction.
Definition: transaction.h:150
Coin Control Features.
Definition: coincontrol.h:81
FeeEstimateMode m_fee_mode
Fee estimation mode to control arguments to estimateSmartFee.
Definition: coincontrol.h:107
std::optional< bool > m_signal_bip125_rbf
Override the wallet's m_signal_rbf if set.
Definition: coincontrol.h:101
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:99
std::map< COutPoint, PreselectedInput > m_selected
Selected inputs (inputs that will be used, regardless of whether they're optimal or not)
Definition: coincontrol.h:185
std::optional< int > m_max_tx_weight
Caps weight of resulting tx.
Definition: coincontrol.h:119
std::optional< OutputType > m_change_type
Override the default change type if set, ignored if destChange is set.
Definition: coincontrol.h:86
bool m_avoid_address_reuse
Forbids inclusion of dirty (previously used) addresses.
Definition: coincontrol.h:105
bool HasSelectedOrder() const
Definition: coincontrol.h:169
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:174
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:109
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:91
int m_max_depth
Maximum chain depth value for coin availability.
Definition: coincontrol.h:111
bool fOverrideFeeRate
Override automatic min/max checks on fee, m_feerate must be set if true.
Definition: coincontrol.h:95
void SetInputWeight(const COutPoint &outpoint, int64_t weight)
Set an input's weight.
Definition: coincontrol.cpp:67
std::optional< uint32_t > m_version
Version.
Definition: coincontrol.h:117
std::optional< CFeeRate > m_feerate
Override the wallet's m_pay_tx_fee if set.
Definition: coincontrol.h:97
bool m_include_unsafe_inputs
If false, only safe inputs will be used.
Definition: coincontrol.h:88
bool m_avoid_partial_spends
Avoid partial use of funds sent to a given address.
Definition: coincontrol.h:103
bool fAllowWatchOnly
Includes watch only addresses which are solvable.
Definition: coincontrol.h:93
unsigned int m_selection_pos
Definition: coincontrol.h:186
FlatSigningProvider m_external_provider
SigningProvider that has pubkeys and scripts to do spend size estimation for external inputs.
Definition: coincontrol.h:113
CTxDestination destChange
Custom change destination, if not set an address is generated.
Definition: coincontrol.h:84
std::vector< COutPoint > ListSelected() const
List the selected inputs.
Definition: coincontrol.cpp:57
std::optional< uint32_t > m_locktime
Locktime.
Definition: coincontrol.h:115
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:41
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:35
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:37
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:31
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:33
std::optional< CScriptWitness > m_script_witness
The scriptWitness for this input.
Definition: coincontrol.h:39
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.
FeeEstimateMode
Definition: feerate.h:21
@ UNSET
Use default settings based on other criteria.
uint64_t sequence
static constexpr bool DEFAULT_AVOIDPARTIALSPENDS
Default for -avoidpartialspends.
Definition: coincontrol.h:25
const int DEFAULT_MIN_DEPTH
Definition: coincontrol.h:21
const int DEFAULT_MAX_DEPTH
Definition: coincontrol.h:22