Bitcoin Core  31.0.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>
11 #include <primitives/transaction.h>
12 #include <script/keyorigin.h>
13 #include <script/signingprovider.h>
14 #include <util/fees.h>
15 
16 #include <algorithm>
17 #include <map>
18 #include <optional>
19 #include <set>
20 
21 namespace wallet {
22 const int DEFAULT_MIN_DEPTH = 0;
23 const int DEFAULT_MAX_DEPTH = 9999999;
24 
26 
28 static constexpr bool DEFAULT_AVOIDPARTIALSPENDS = false;
29 
31 {
32 private:
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 
46 public:
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 {
85 public:
89  std::optional<OutputType> m_change_type;
94  bool m_allow_other_inputs = true;
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;
106  bool m_avoid_address_reuse = false;
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 
184 private:
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::optional< unsigned int > GetSelectionPos(const COutPoint &outpoint) const
Definition: coincontrol.h:175
std::optional< CScriptWitness > m_script_witness
The scriptWitness for this input.
Definition: coincontrol.h:42
std::optional< CTxOut > GetExternalOutput(const COutPoint &outpoint) const
Returns the external output for the given outpoint if it exists.
Definition: coincontrol.cpp:31
bool m_avoid_partial_spends
Avoid partial use of funds sent to a given address.
Definition: coincontrol.h:104
std::optional< int64_t > GetInputWeight() const
Retrieve the input weight for this input.
std::optional< unsigned int > m_confirm_target
Override the default confirmation target if set.
Definition: coincontrol.h:100
std::optional< uint32_t > m_locktime
Locktime.
Definition: coincontrol.h:118
std::optional< uint32_t > GetSequence() const
Retrieve the sequence for this input.
bool HasSelectedOrder() const
Definition: coincontrol.h:170
CTxDestination destChange
Custom change destination, if not set an address is generated.
Definition: coincontrol.h:87
bool HasScripts() const
Return whether either the scriptSig or scriptWitness are set for this input.
void SetTxOut(const CTxOut &txout)
Set the previous output for this input.
Definition: coincontrol.cpp:90
std::optional< int > m_max_tx_weight
Caps weight of resulting tx.
Definition: coincontrol.h:120
FlatSigningProvider m_external_provider
SigningProvider that has pubkeys and scripts to do spend size estimation for external inputs...
Definition: coincontrol.h:114
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
bool HasSelected() const
Returns true if there are pre-selected inputs.
Definition: coincontrol.cpp:15
std::optional< OutputType > m_change_type
Override the default change type if set, ignored if destChange is set.
Definition: coincontrol.h:89
std::optional< uint32_t > m_sequence
The sequence number for this input.
Definition: coincontrol.h:38
static const uint32_t CURRENT_VERSION
Definition: transaction.h:284
uint32_t m_version
Version.
Definition: coincontrol.h:116
void SetScriptSig(const CScript &script)
Set the scriptSig for this input.
static constexpr bool DEFAULT_AVOIDPARTIALSPENDS
Default for -avoidpartialspends.
Definition: coincontrol.h:28
std::map< COutPoint, PreselectedInput > m_selected
Selected inputs (inputs that will be used, regardless of whether they&#39;re optimal or not) ...
Definition: coincontrol.h:186
unsigned int m_selection_pos
Definition: coincontrol.h:187
const int DEFAULT_WALLET_TX_VERSION
Definition: coincontrol.h:25
std::optional< unsigned int > m_pos
The position in the inputs vector for this input.
Definition: coincontrol.h:44
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&#39;s weight.
Definition: coincontrol.cpp:67
void UnSelectAll()
Unselects all outputs.
Definition: coincontrol.cpp:52
An output of a transaction.
Definition: transaction.h:139
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:28
bool m_avoid_address_reuse
Forbids inclusion of dirty (previously used) addresses.
Definition: coincontrol.h:106
std::optional< int64_t > m_weight
The input weight for spending this input.
Definition: coincontrol.h:36
const int DEFAULT_MAX_DEPTH
Definition: coincontrol.h:23
const int DEFAULT_MIN_DEPTH
Definition: coincontrol.h:22
void UnSelect(const COutPoint &outpoint)
Unselects the given output.
Definition: coincontrol.cpp:47
int m_min_depth
Minimum chain depth value for coin availability.
Definition: coincontrol.h:110
CTxOut GetTxOut() const
Retrieve the previous output for this input.
Definition: coincontrol.cpp:95
Use default settings based on other criteria.
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:404
std::optional< int64_t > GetInputWeight(const COutPoint &outpoint) const
Returns the input weight.
Definition: coincontrol.cpp:72
bool m_include_unsafe_inputs
If false, only safe inputs will be used.
Definition: coincontrol.h:91
FeeEstimateMode m_fee_mode
Fee estimation mode to control arguments to estimateSmartFee.
Definition: coincontrol.h:108
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
Definition: addresstype.h:143
std::optional< CScript > m_script_sig
The scriptSig for this input.
Definition: coincontrol.h:40
FeeEstimateMode
Definition: fees.h:9
void SetPosition(unsigned int pos)
Store the position of this input.
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
void SetSequence(uint32_t sequence)
Set the sequence for this input.
bool IsExternalSelected(const COutPoint &outpoint) const
Returns true if the given output is selected as an external input.
Definition: coincontrol.cpp:25
void SetInputWeight(int64_t weight)
Set the weight for this input.
std::optional< unsigned int > GetPosition() const
Retrieve the position of this input.
uint64_t sequence
int m_max_depth
Maximum chain depth value for coin availability.
Definition: coincontrol.h:112
std::optional< bool > m_signal_bip125_rbf
Override the wallet&#39;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< CFeeRate > m_feerate
Override the wallet&#39;s fee rate if set.
Definition: coincontrol.h:98
std::optional< CTxOut > m_txout
The previous output being spent by this input.
Definition: coincontrol.h:34
std::vector< COutPoint > ListSelected() const
List the selected inputs.
Definition: coincontrol.cpp:57
Coin Control Features.
Definition: coincontrol.h:83
PreselectedInput & Select(const COutPoint &outpoint)
Lock-in the given output for spending.
Definition: coincontrol.cpp:40
std::pair< std::optional< CScript >, std::optional< CScriptWitness > > GetScripts() const
Retrieve both the scriptSig and the scriptWitness.
std::optional< uint32_t > GetSequence(const COutPoint &outpoint) const
Retrieve the sequence for an input.
Definition: coincontrol.cpp:78
void SetScriptWitness(const CScriptWitness &script_wit)
Set the scriptWitness for this input.
bool HasTxOut() const
Return whether the previous output is set for this input.