Bitcoin Core  26.99.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_inputs.empty();
18 }
19 
20 bool CCoinControl::IsSelected(const COutPoint& output) const
21 {
22  return m_selected_inputs.count(output) > 0;
23 }
24 
26 {
27  return m_external_txouts.count(output) > 0;
28 }
29 
30 std::optional<CTxOut> CCoinControl::GetExternalOutput(const COutPoint& outpoint) const
31 {
32  const auto ext_it = m_external_txouts.find(outpoint);
33  if (ext_it == m_external_txouts.end()) {
34  return std::nullopt;
35  }
36 
37  return std::make_optional(ext_it->second);
38 }
39 
40 void CCoinControl::Select(const COutPoint& output)
41 {
42  m_selected_inputs.insert(output);
43 }
44 
45 void CCoinControl::SelectExternal(const COutPoint& outpoint, const CTxOut& txout)
46 {
47  m_selected_inputs.insert(outpoint);
48  m_external_txouts.emplace(outpoint, txout);
49 }
50 
51 void CCoinControl::UnSelect(const COutPoint& output)
52 {
53  m_selected_inputs.erase(output);
54 }
55 
57 {
58  m_selected_inputs.clear();
59 }
60 
61 std::vector<COutPoint> CCoinControl::ListSelected() const
62 {
63  return {m_selected_inputs.begin(), m_selected_inputs.end()};
64 }
65 
66 void CCoinControl::SetInputWeight(const COutPoint& outpoint, int64_t weight)
67 {
68  m_input_weights[outpoint] = weight;
69 }
70 
71 bool CCoinControl::HasInputWeight(const COutPoint& outpoint) const
72 {
73  return m_input_weights.count(outpoint) > 0;
74 }
75 
76 int64_t CCoinControl::GetInputWeight(const COutPoint& outpoint) const
77 {
78  auto it = m_input_weights.find(outpoint);
79  assert(it != m_input_weights.end());
80  return it->second;
81 }
82 } // namespace wallet
ArgsManager gArgs
Definition: args.cpp:41
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: args.cpp:505
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:29
An output of a transaction.
Definition: transaction.h:150
bool HasInputWeight(const COutPoint &outpoint) const
Returns true if the input weight is set.
Definition: coincontrol.cpp:71
std::optional< CTxOut > GetExternalOutput(const COutPoint &outpoint) const
Returns the external output for the given outpoint if it exists.
Definition: coincontrol.cpp:30
int64_t GetInputWeight(const COutPoint &outpoint) const
Returns the input weight.
Definition: coincontrol.cpp:76
void Select(const COutPoint &output)
Lock-in the given output for spending.
Definition: coincontrol.cpp:40
void SelectExternal(const COutPoint &outpoint, const CTxOut &txout)
Lock-in the given output as an external input for spending because it is not in the wallet.
Definition: coincontrol.cpp:45
std::map< COutPoint, CTxOut > m_external_txouts
Map of external inputs to include in the transaction These are not in the wallet, so we need to track...
Definition: coincontrol.h:121
bool IsSelected(const COutPoint &output) const
Returns true if the given output is pre-selected.
Definition: coincontrol.cpp:20
std::set< COutPoint > m_selected_inputs
Selected inputs (inputs that will be used, regardless of whether they're optimal or not)
Definition: coincontrol.h:118
void UnSelectAll()
Unselects all outputs.
Definition: coincontrol.cpp:56
std::map< COutPoint, int64_t > m_input_weights
Map of COutPoints to the maximum weight for that input.
Definition: coincontrol.h:123
bool HasSelected() const
Returns true if there are pre-selected inputs.
Definition: coincontrol.cpp:15
void SetInputWeight(const COutPoint &outpoint, int64_t weight)
Set an input's weight.
Definition: coincontrol.cpp:66
bool m_avoid_partial_spends
Avoid partial use of funds sent to a given address.
Definition: coincontrol.h:51
bool IsExternalSelected(const COutPoint &output) const
Returns true if the given output is selected as an external input.
Definition: coincontrol.cpp:25
std::vector< COutPoint > ListSelected() const
List the selected inputs.
Definition: coincontrol.cpp:61
void UnSelect(const COutPoint &output)
Unselects the given output.
Definition: coincontrol.cpp:51
static constexpr bool DEFAULT_AVOIDPARTIALSPENDS
Default for -avoidpartialspends.
Definition: coincontrol.h:25
assert(!tx.IsCoinBase())