Bitcoin Core  29.1.0
P2P Digital Currency
coincontrol.cpp
Go to the documentation of this file.
1 // Copyright (c) 2018-2021 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 #include <wallet/coincontrol.h>
6 
7 #include <common/args.h>
8 
9 namespace wallet {
11 {
13 }
14 
16 {
17  return !m_selected.empty();
18 }
19 
20 bool CCoinControl::IsSelected(const COutPoint& outpoint) const
21 {
22  return m_selected.count(outpoint) > 0;
23 }
24 
25 bool CCoinControl::IsExternalSelected(const COutPoint& outpoint) const
26 {
27  const auto it = m_selected.find(outpoint);
28  return it != m_selected.end() && it->second.HasTxOut();
29 }
30 
31 std::optional<CTxOut> CCoinControl::GetExternalOutput(const COutPoint& outpoint) const
32 {
33  const auto it = m_selected.find(outpoint);
34  if (it == m_selected.end() || !it->second.HasTxOut()) {
35  return std::nullopt;
36  }
37  return it->second.GetTxOut();
38 }
39 
41 {
42  auto& input = m_selected[outpoint];
43  input.SetPosition(m_selection_pos);
45  return input;
46 }
47 void CCoinControl::UnSelect(const COutPoint& outpoint)
48 {
49  m_selected.erase(outpoint);
50 }
51 
53 {
54  m_selected.clear();
55 }
56 
57 std::vector<COutPoint> CCoinControl::ListSelected() const
58 {
59  std::vector<COutPoint> outpoints;
60  std::transform(m_selected.begin(), m_selected.end(), std::back_inserter(outpoints),
61  [](const std::map<COutPoint, PreselectedInput>::value_type& pair) {
62  return pair.first;
63  });
64  return outpoints;
65 }
66 
67 void CCoinControl::SetInputWeight(const COutPoint& outpoint, int64_t weight)
68 {
69  m_selected[outpoint].SetInputWeight(weight);
70 }
71 
72 std::optional<int64_t> CCoinControl::GetInputWeight(const COutPoint& outpoint) const
73 {
74  const auto it = m_selected.find(outpoint);
75  return it != m_selected.end() ? it->second.GetInputWeight() : std::nullopt;
76 }
77 
78 std::optional<uint32_t> CCoinControl::GetSequence(const COutPoint& outpoint) const
79 {
80  const auto it = m_selected.find(outpoint);
81  return it != m_selected.end() ? it->second.GetSequence() : std::nullopt;
82 }
83 
84 std::pair<std::optional<CScript>, std::optional<CScriptWitness>> CCoinControl::GetScripts(const COutPoint& outpoint) const
85 {
86  const auto it = m_selected.find(outpoint);
87  return it != m_selected.end() ? m_selected.at(outpoint).GetScripts() : std::make_pair(std::nullopt, std::nullopt);
88 }
89 
91 {
92  m_txout = txout;
93 }
94 
96 {
97  assert(m_txout.has_value());
98  return m_txout.value();
99 }
100 
102 {
103  return m_txout.has_value();
104 }
105 
107 {
108  m_weight = weight;
109 }
110 
111 std::optional<int64_t> PreselectedInput::GetInputWeight() const
112 {
113  return m_weight;
114 }
115 
117 {
119 }
120 
121 std::optional<uint32_t> PreselectedInput::GetSequence() const
122 {
123  return m_sequence;
124 }
125 
127 {
129 }
130 
132 {
133  m_script_witness = script_wit;
134 }
135 
137 {
138  return m_script_sig.has_value() || m_script_witness.has_value();
139 }
140 
141 std::pair<std::optional<CScript>, std::optional<CScriptWitness>> PreselectedInput::GetScripts() const
142 {
143  return {m_script_sig, m_script_witness};
144 }
145 
146 void PreselectedInput::SetPosition(unsigned int pos)
147 {
148  m_pos = pos;
149 }
150 
151 std::optional<unsigned int> PreselectedInput::GetPosition() const
152 {
153  return m_pos;
154 }
155 } // namespace wallet
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.
assert(!tx.IsCoinBase())
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 SetTxOut(const CTxOut &txout)
Set the previous output for this input.
Definition: coincontrol.cpp:90
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: args.cpp:507
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< 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
std::optional< unsigned int > m_pos
The position in the inputs vector for this input.
Definition: coincontrol.h:41
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
ArgsManager gArgs
Definition: args.cpp:42
std::optional< int64_t > m_weight
The input weight for spending this input.
Definition: coincontrol.h:33
void UnSelect(const COutPoint &outpoint)
Unselects the given output.
Definition: coincontrol.cpp:47
CTxOut GetTxOut() const
Retrieve the previous output for this input.
Definition: coincontrol.cpp:95
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:414
std::optional< int64_t > GetInputWeight(const COutPoint &outpoint) const
Returns the input weight.
Definition: coincontrol.cpp:72
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.
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
bool IsSelected(const COutPoint &outpoint) const
Returns true if the given output is pre-selected.
Definition: coincontrol.cpp:20
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
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.