/root/bitcoin/src/wallet/coincontrol.cpp
Line | Count | Source (jump to first uncovered line) |
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 { |
10 | | CCoinControl::CCoinControl() |
11 | 0 | { |
12 | 0 | m_avoid_partial_spends = gArgs.GetBoolArg("-avoidpartialspends", DEFAULT_AVOIDPARTIALSPENDS); |
13 | 0 | } |
14 | | |
15 | | bool CCoinControl::HasSelected() const |
16 | 0 | { |
17 | 0 | return !m_selected.empty(); |
18 | 0 | } |
19 | | |
20 | | bool CCoinControl::IsSelected(const COutPoint& outpoint) const |
21 | 0 | { |
22 | 0 | return m_selected.count(outpoint) > 0; |
23 | 0 | } |
24 | | |
25 | | bool CCoinControl::IsExternalSelected(const COutPoint& outpoint) const |
26 | 0 | { |
27 | 0 | const auto it = m_selected.find(outpoint); |
28 | 0 | return it != m_selected.end() && it->second.HasTxOut(); |
29 | 0 | } |
30 | | |
31 | | std::optional<CTxOut> CCoinControl::GetExternalOutput(const COutPoint& outpoint) const |
32 | 0 | { |
33 | 0 | const auto it = m_selected.find(outpoint); |
34 | 0 | if (it == m_selected.end() || !it->second.HasTxOut()) { |
35 | 0 | return std::nullopt; |
36 | 0 | } |
37 | 0 | return it->second.GetTxOut(); |
38 | 0 | } |
39 | | |
40 | | PreselectedInput& CCoinControl::Select(const COutPoint& outpoint) |
41 | 0 | { |
42 | 0 | auto& input = m_selected[outpoint]; |
43 | 0 | input.SetPosition(m_selection_pos); |
44 | 0 | ++m_selection_pos; |
45 | 0 | return input; |
46 | 0 | } |
47 | | void CCoinControl::UnSelect(const COutPoint& outpoint) |
48 | 0 | { |
49 | 0 | m_selected.erase(outpoint); |
50 | 0 | } |
51 | | |
52 | | void CCoinControl::UnSelectAll() |
53 | 0 | { |
54 | 0 | m_selected.clear(); |
55 | 0 | } |
56 | | |
57 | | std::vector<COutPoint> CCoinControl::ListSelected() const |
58 | 0 | { |
59 | 0 | std::vector<COutPoint> outpoints; |
60 | 0 | std::transform(m_selected.begin(), m_selected.end(), std::back_inserter(outpoints), |
61 | 0 | [](const std::map<COutPoint, PreselectedInput>::value_type& pair) { |
62 | 0 | return pair.first; |
63 | 0 | }); |
64 | 0 | return outpoints; |
65 | 0 | } |
66 | | |
67 | | void CCoinControl::SetInputWeight(const COutPoint& outpoint, int64_t weight) |
68 | 0 | { |
69 | 0 | m_selected[outpoint].SetInputWeight(weight); |
70 | 0 | } |
71 | | |
72 | | std::optional<int64_t> CCoinControl::GetInputWeight(const COutPoint& outpoint) const |
73 | 0 | { |
74 | 0 | const auto it = m_selected.find(outpoint); |
75 | 0 | return it != m_selected.end() ? it->second.GetInputWeight() : std::nullopt; |
76 | 0 | } |
77 | | |
78 | | std::optional<uint32_t> CCoinControl::GetSequence(const COutPoint& outpoint) const |
79 | 0 | { |
80 | 0 | const auto it = m_selected.find(outpoint); |
81 | 0 | return it != m_selected.end() ? it->second.GetSequence() : std::nullopt; |
82 | 0 | } |
83 | | |
84 | | std::pair<std::optional<CScript>, std::optional<CScriptWitness>> CCoinControl::GetScripts(const COutPoint& outpoint) const |
85 | 0 | { |
86 | 0 | const auto it = m_selected.find(outpoint); |
87 | 0 | return it != m_selected.end() ? m_selected.at(outpoint).GetScripts() : std::make_pair(std::nullopt, std::nullopt); |
88 | 0 | } |
89 | | |
90 | | void PreselectedInput::SetTxOut(const CTxOut& txout) |
91 | 0 | { |
92 | 0 | m_txout = txout; |
93 | 0 | } |
94 | | |
95 | | CTxOut PreselectedInput::GetTxOut() const |
96 | 0 | { |
97 | 0 | assert(m_txout.has_value()); |
98 | 0 | return m_txout.value(); |
99 | 0 | } |
100 | | |
101 | | bool PreselectedInput::HasTxOut() const |
102 | 0 | { |
103 | 0 | return m_txout.has_value(); |
104 | 0 | } |
105 | | |
106 | | void PreselectedInput::SetInputWeight(int64_t weight) |
107 | 0 | { |
108 | 0 | m_weight = weight; |
109 | 0 | } |
110 | | |
111 | | std::optional<int64_t> PreselectedInput::GetInputWeight() const |
112 | 0 | { |
113 | 0 | return m_weight; |
114 | 0 | } |
115 | | |
116 | | void PreselectedInput::SetSequence(uint32_t sequence) |
117 | 0 | { |
118 | 0 | m_sequence = sequence; |
119 | 0 | } |
120 | | |
121 | | std::optional<uint32_t> PreselectedInput::GetSequence() const |
122 | 0 | { |
123 | 0 | return m_sequence; |
124 | 0 | } |
125 | | |
126 | | void PreselectedInput::SetScriptSig(const CScript& script) |
127 | 0 | { |
128 | 0 | m_script_sig = script; |
129 | 0 | } |
130 | | |
131 | | void PreselectedInput::SetScriptWitness(const CScriptWitness& script_wit) |
132 | 0 | { |
133 | 0 | m_script_witness = script_wit; |
134 | 0 | } |
135 | | |
136 | | bool PreselectedInput::HasScripts() const |
137 | 0 | { |
138 | 0 | return m_script_sig.has_value() || m_script_witness.has_value(); |
139 | 0 | } |
140 | | |
141 | | std::pair<std::optional<CScript>, std::optional<CScriptWitness>> PreselectedInput::GetScripts() const |
142 | 0 | { |
143 | 0 | return {m_script_sig, m_script_witness}; |
144 | 0 | } |
145 | | |
146 | | void PreselectedInput::SetPosition(unsigned int pos) |
147 | 0 | { |
148 | 0 | m_pos = pos; |
149 | 0 | } |
150 | | |
151 | | std::optional<unsigned int> PreselectedInput::GetPosition() const |
152 | 0 | { |
153 | 0 | return m_pos; |
154 | 0 | } |
155 | | } // namespace wallet |