Bitcoin Core  28.1.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>
11 #include <primitives/transaction.h>
12 #include <script/keyorigin.h>
13 #include <script/signingprovider.h>
14 
15 #include <algorithm>
16 #include <map>
17 #include <optional>
18 #include <set>
19 
20 namespace wallet {
21 const int DEFAULT_MIN_DEPTH = 0;
22 const int DEFAULT_MAX_DEPTH = 9999999;
23 
25 static constexpr bool DEFAULT_AVOIDPARTIALSPENDS = false;
26 
28 {
29 private:
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 
43 public:
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 {
82 public:
86  std::optional<OutputType> m_change_type;
91  bool m_allow_other_inputs = true;
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;
105  bool m_avoid_address_reuse = false;
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 
183 private:
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::optional< unsigned int > GetSelectionPos(const COutPoint &outpoint) const
Definition: coincontrol.h:174
std::optional< CScriptWitness > m_script_witness
The scriptWitness for this input.
Definition: coincontrol.h:39
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:103
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:99
std::optional< uint32_t > m_locktime
Locktime.
Definition: coincontrol.h:115
std::optional< uint32_t > GetSequence() const
Retrieve the sequence for this input.
bool HasSelectedOrder() const
Definition: coincontrol.h:169
CTxDestination destChange
Custom change destination, if not set an address is generated.
Definition: coincontrol.h:84
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:119
bool fAllowWatchOnly
Includes watch only addresses which are solvable.
Definition: coincontrol.h:93
FlatSigningProvider m_external_provider
SigningProvider that has pubkeys and scripts to do spend size estimation for external inputs...
Definition: coincontrol.h:113
std::optional< uint32_t > m_version
Version.
Definition: coincontrol.h:117
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:86
std::optional< uint32_t > m_sequence
The sequence number for this input.
Definition: coincontrol.h:35
void SetScriptSig(const CScript &script)
Set the scriptSig for this input.
static constexpr bool DEFAULT_AVOIDPARTIALSPENDS
Default for -avoidpartialspends.
Definition: coincontrol.h:25
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:185
unsigned int m_selection_pos
Definition: coincontrol.h:186
FeeEstimateMode
Definition: feerate.h:21
std::optional< unsigned int > m_pos
The position in the inputs vector for this input.
Definition: coincontrol.h:41
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&#39;s weight.
Definition: coincontrol.cpp:67
void UnSelectAll()
Unselects all outputs.
Definition: coincontrol.cpp:52
An output of a transaction.
Definition: transaction.h:149
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:105
std::optional< int64_t > m_weight
The input weight for spending this input.
Definition: coincontrol.h:33
const int DEFAULT_MAX_DEPTH
Definition: coincontrol.h:22
const int DEFAULT_MIN_DEPTH
Definition: coincontrol.h:21
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:109
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:413
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:88
FeeEstimateMode m_fee_mode
Fee estimation mode to control arguments to estimateSmartFee.
Definition: coincontrol.h:107
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
Definition: addresstype.h:140
std::optional< CScript > m_script_sig
The scriptSig for this input.
Definition: coincontrol.h:37
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:91
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.
uint64_t sequence
std::optional< unsigned int > GetPosition() const
Retrieve the position of this input.
int m_max_depth
Maximum chain depth value for coin availability.
Definition: coincontrol.h:111
std::optional< bool > m_signal_bip125_rbf
Override the wallet&#39;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< CFeeRate > m_feerate
Override the wallet&#39;s m_pay_tx_fee if set.
Definition: coincontrol.h:97
std::optional< CTxOut > m_txout
The previous output being spent by this input.
Definition: coincontrol.h:31
std::vector< COutPoint > ListSelected() const
List the selected inputs.
Definition: coincontrol.cpp:57
Coin Control Features.
Definition: coincontrol.h:80
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.