Coverage Report

Created: 2025-09-19 18:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/bitcoin/src/wallet/coincontrol.h
Line
Count
Source
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
24
const int DEFAULT_WALLET_TX_VERSION = CTransaction::CURRENT_VERSION;
25
26
//! Default for -avoidpartialspends
27
static constexpr bool DEFAULT_AVOIDPARTIALSPENDS = false;
28
29
class PreselectedInput
30
{
31
private:
32
    //! The previous output being spent by this input
33
    std::optional<CTxOut> m_txout;
34
    //! The input weight for spending this input
35
    std::optional<int64_t> m_weight;
36
    //! The sequence number for this input
37
    std::optional<uint32_t> m_sequence;
38
    //! The scriptSig for this input
39
    std::optional<CScript> m_script_sig;
40
    //! The scriptWitness for this input
41
    std::optional<CScriptWitness> m_script_witness;
42
    //! The position in the inputs vector for this input
43
    std::optional<unsigned int> m_pos;
44
45
public:
46
    /**
47
     * Set the previous output for this input.
48
     * Only necessary if the input is expected to be an external input.
49
     */
50
    void SetTxOut(const CTxOut& txout);
51
    /** Retrieve the previous output for this input. */
52
    CTxOut GetTxOut() const;
53
    /** Return whether the previous output is set for this input. */
54
    bool HasTxOut() const;
55
56
    /** Set the weight for this input. */
57
    void SetInputWeight(int64_t weight);
58
    /** Retrieve the input weight for this input. */
59
    std::optional<int64_t> GetInputWeight() const;
60
61
    /** Set the sequence for this input. */
62
    void SetSequence(uint32_t sequence);
63
    /** Retrieve the sequence for this input. */
64
    std::optional<uint32_t> GetSequence() const;
65
66
    /** Set the scriptSig for this input. */
67
    void SetScriptSig(const CScript& script);
68
    /** Set the scriptWitness for this input. */
69
    void SetScriptWitness(const CScriptWitness& script_wit);
70
    /** Return whether either the scriptSig or scriptWitness are set for this input. */
71
    bool HasScripts() const;
72
    /** Retrieve both the scriptSig and the scriptWitness. */
73
    std::pair<std::optional<CScript>, std::optional<CScriptWitness>> GetScripts() const;
74
75
    /** Store the position of this input. */
76
    void SetPosition(unsigned int pos);
77
    /** Retrieve the position of this input. */
78
    std::optional<unsigned int> GetPosition() const;
79
};
80
81
/** Coin Control Features. */
82
class CCoinControl
83
{
84
public:
85
    //! Custom change destination, if not set an address is generated
86
    CTxDestination destChange = CNoDestination();
87
    //! Override the default change type if set, ignored if destChange is set
88
    std::optional<OutputType> m_change_type;
89
    //! If false, only safe inputs will be used
90
    bool m_include_unsafe_inputs = false;
91
    //! If true, the selection process can add extra unselected inputs from the wallet
92
    //! while requires all selected inputs be used
93
    bool m_allow_other_inputs = true;
94
    //! Override automatic min/max checks on fee, m_feerate must be set if true
95
    bool fOverrideFeeRate = false;
96
    //! Override the wallet's m_pay_tx_fee if set
97
    std::optional<CFeeRate> m_feerate;
98
    //! Override the default confirmation target if set
99
    std::optional<unsigned int> m_confirm_target;
100
    //! Override the wallet's m_signal_rbf if set
101
    std::optional<bool> m_signal_bip125_rbf;
102
    //! Avoid partial use of funds sent to a given address
103
    bool m_avoid_partial_spends = DEFAULT_AVOIDPARTIALSPENDS;
104
    //! Forbids inclusion of dirty (previously used) addresses
105
    bool m_avoid_address_reuse = false;
106
    //! Fee estimation mode to control arguments to estimateSmartFee
107
    FeeEstimateMode m_fee_mode = FeeEstimateMode::UNSET;
108
    //! Minimum chain depth value for coin availability
109
    int m_min_depth = DEFAULT_MIN_DEPTH;
110
    //! Maximum chain depth value for coin availability
111
    int m_max_depth = DEFAULT_MAX_DEPTH;
112
    //! SigningProvider that has pubkeys and scripts to do spend size estimation for external inputs
113
    FlatSigningProvider m_external_provider;
114
    //! Version
115
    uint32_t m_version = DEFAULT_WALLET_TX_VERSION;
116
    //! Locktime
117
    std::optional<uint32_t> m_locktime;
118
    //! Caps weight of resulting tx
119
    std::optional<int> m_max_tx_weight{std::nullopt};
120
121
    CCoinControl();
122
123
    /**
124
     * Returns true if there are pre-selected inputs.
125
     */
126
    bool HasSelected() const;
127
    /**
128
     * Returns true if the given output is pre-selected.
129
     */
130
    bool IsSelected(const COutPoint& outpoint) const;
131
    /**
132
     * Returns true if the given output is selected as an external input.
133
     */
134
    bool IsExternalSelected(const COutPoint& outpoint) const;
135
    /**
136
     * Returns the external output for the given outpoint if it exists.
137
     */
138
    std::optional<CTxOut> GetExternalOutput(const COutPoint& outpoint) const;
139
    /**
140
     * Lock-in the given output for spending.
141
     * The output will be included in the transaction even if it's not the most optimal choice.
142
     */
143
    PreselectedInput& Select(const COutPoint& outpoint);
144
    /**
145
     * Unselects the given output.
146
     */
147
    void UnSelect(const COutPoint& outpoint);
148
    /**
149
     * Unselects all outputs.
150
     */
151
    void UnSelectAll();
152
    /**
153
     * List the selected inputs.
154
     */
155
    std::vector<COutPoint> ListSelected() const;
156
    /**
157
     * Set an input's weight.
158
     */
159
    void SetInputWeight(const COutPoint& outpoint, int64_t weight);
160
    /**
161
     * Returns the input weight.
162
     */
163
    std::optional<int64_t> GetInputWeight(const COutPoint& outpoint) const;
164
    /** Retrieve the sequence for an input */
165
    std::optional<uint32_t> GetSequence(const COutPoint& outpoint) const;
166
    /** Retrieves the scriptSig and scriptWitness for an input. */
167
    std::pair<std::optional<CScript>, std::optional<CScriptWitness>> GetScripts(const COutPoint& outpoint) const;
168
169
    bool HasSelectedOrder() const
170
0
    {
171
0
        return m_selection_pos > 0;
172
0
    }
173
174
    std::optional<unsigned int> GetSelectionPos(const COutPoint& outpoint) const
175
0
    {
176
0
        const auto it = m_selected.find(outpoint);
177
0
        if (it == m_selected.end()) {
178
0
            return std::nullopt;
179
0
        }
180
0
        return it->second.GetPosition();
181
0
    }
182
183
private:
184
    //! Selected inputs (inputs that will be used, regardless of whether they're optimal or not)
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