Coverage Report

Created: 2026-04-29 19:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/wallet/coincontrol.cpp
Line
Count
Source
1
// Copyright (c) 2018-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
#include <wallet/coincontrol.h>
6
7
#include <common/args.h>
8
9
namespace wallet {
10
CCoinControl::CCoinControl()
11
2.87k
{
12
2.87k
    m_avoid_partial_spends = gArgs.GetBoolArg("-avoidpartialspends", DEFAULT_AVOIDPARTIALSPENDS);
13
2.87k
}
14
15
bool CCoinControl::HasSelected() const
16
479k
{
17
479k
    return !m_selected.empty();
18
479k
}
19
20
bool CCoinControl::IsSelected(const COutPoint& outpoint) const
21
32.4k
{
22
32.4k
    return m_selected.contains(outpoint);
23
32.4k
}
24
25
bool CCoinControl::IsExternalSelected(const COutPoint& outpoint) const
26
13.1k
{
27
13.1k
    const auto it = m_selected.find(outpoint);
28
13.1k
    return it != m_selected.end() && it->second.HasTxOut();
29
13.1k
}
30
31
std::optional<CTxOut> CCoinControl::GetExternalOutput(const COutPoint& outpoint) const
32
84
{
33
84
    const auto it = m_selected.find(outpoint);
34
84
    if (it == m_selected.end() || !it->second.HasTxOut()) {
35
0
        return std::nullopt;
36
0
    }
37
84
    return it->second.GetTxOut();
38
84
}
39
40
PreselectedInput& CCoinControl::Select(const COutPoint& outpoint)
41
3.24k
{
42
3.24k
    auto& input = m_selected[outpoint];
43
3.24k
    input.SetPosition(m_selection_pos);
44
3.24k
    ++m_selection_pos;
45
3.24k
    return input;
46
3.24k
}
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
1.02k
{
59
1.02k
    std::vector<COutPoint> outpoints;
60
1.02k
    std::transform(m_selected.begin(), m_selected.end(), std::back_inserter(outpoints),
61
11.3k
        [](const std::map<COutPoint, PreselectedInput>::value_type& pair) {
62
11.3k
            return pair.first;
63
11.3k
        });
64
1.02k
    return outpoints;
65
1.02k
}
66
67
void CCoinControl::SetInputWeight(const COutPoint& outpoint, int64_t weight)
68
2.48k
{
69
2.48k
    m_selected[outpoint].SetInputWeight(weight);
70
2.48k
}
71
72
std::optional<int64_t> CCoinControl::GetInputWeight(const COutPoint& outpoint) const
73
20.2k
{
74
20.2k
    const auto it = m_selected.find(outpoint);
75
20.2k
    return it != m_selected.end() ? it->second.GetInputWeight() : std::nullopt;
76
20.2k
}
77
78
std::optional<uint32_t> CCoinControl::GetSequence(const COutPoint& outpoint) const
79
14.5k
{
80
14.5k
    const auto it = m_selected.find(outpoint);
81
14.5k
    return it != m_selected.end() ? it->second.GetSequence() : std::nullopt;
82
14.5k
}
83
84
std::pair<std::optional<CScript>, std::optional<CScriptWitness>> CCoinControl::GetScripts(const COutPoint& outpoint) const
85
14.5k
{
86
14.5k
    const auto it = m_selected.find(outpoint);
87
14.5k
    return it != m_selected.end() ? m_selected.at(outpoint).GetScripts() : std::make_pair(std::nullopt, std::nullopt);
88
14.5k
}
89
90
void PreselectedInput::SetTxOut(const CTxOut& txout)
91
27
{
92
27
    m_txout = txout;
93
27
}
94
95
CTxOut PreselectedInput::GetTxOut() const
96
84
{
97
84
    assert(m_txout.has_value());
98
84
    return m_txout.value();
99
84
}
100
101
bool PreselectedInput::HasTxOut() const
102
2.39k
{
103
2.39k
    return m_txout.has_value();
104
2.39k
}
105
106
void PreselectedInput::SetInputWeight(int64_t weight)
107
2.48k
{
108
2.48k
    m_weight = weight;
109
2.48k
}
110
111
std::optional<int64_t> PreselectedInput::GetInputWeight() const
112
9.24k
{
113
9.24k
    return m_weight;
114
9.24k
}
115
116
void PreselectedInput::SetSequence(uint32_t sequence)
117
2.69k
{
118
2.69k
    m_sequence = sequence;
119
2.69k
}
120
121
std::optional<uint32_t> PreselectedInput::GetSequence() const
122
3.51k
{
123
3.51k
    return m_sequence;
124
3.51k
}
125
126
void PreselectedInput::SetScriptSig(const CScript& script)
127
2.69k
{
128
2.69k
    m_script_sig = script;
129
2.69k
}
130
131
void PreselectedInput::SetScriptWitness(const CScriptWitness& script_wit)
132
2.69k
{
133
2.69k
    m_script_witness = script_wit;
134
2.69k
}
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
3.51k
{
143
3.51k
    return {m_script_sig, m_script_witness};
144
3.51k
}
145
146
void PreselectedInput::SetPosition(unsigned int pos)
147
3.24k
{
148
3.24k
    m_pos = pos;
149
3.24k
}
150
151
std::optional<unsigned int> PreselectedInput::GetPosition() const
152
29.9k
{
153
29.9k
    return m_pos;
154
29.9k
}
155
} // namespace wallet