Coverage Report

Created: 2026-04-29 19:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/common/messages.cpp
Line
Count
Source
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
// Copyright (c) 2009-present The Bitcoin Core developers
3
// Distributed under the MIT software license, see the accompanying
4
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6
#include <common/messages.h>
7
#include <common/types.h>
8
#include <node/types.h>
9
#include <policy/fees/block_policy_estimator.h>
10
#include <tinyformat.h>
11
#include <util/fees.h>
12
#include <util/strencodings.h>
13
#include <util/string.h>
14
#include <util/translation.h>
15
16
#include <cassert>
17
#include <map>
18
#include <string>
19
#include <string_view>
20
#include <utility>
21
#include <vector>
22
23
using node::TransactionError;
24
using util::Join;
25
26
namespace common {
27
std::string StringForFeeReason(FeeReason reason)
28
3.52k
{
29
3.52k
    static const std::map<FeeReason, std::string> fee_reason_strings = {
30
3.52k
        {FeeReason::NONE, "None"},
31
3.52k
        {FeeReason::HALF_ESTIMATE, "Half Target 60% Threshold"},
32
3.52k
        {FeeReason::FULL_ESTIMATE, "Target 85% Threshold"},
33
3.52k
        {FeeReason::DOUBLE_ESTIMATE, "Double Target 95% Threshold"},
34
3.52k
        {FeeReason::CONSERVATIVE, "Conservative Double Target longer horizon"},
35
3.52k
        {FeeReason::MEMPOOL_MIN, "Mempool Min Fee"},
36
3.52k
        {FeeReason::FALLBACK, "Fallback fee"},
37
3.52k
        {FeeReason::REQUIRED, "Minimum Required Fee"},
38
3.52k
    };
39
3.52k
    auto reason_string = fee_reason_strings.find(reason);
40
41
3.52k
    if (reason_string == fee_reason_strings.end()) return "Unknown";
42
43
3.52k
    return reason_string->second;
44
3.52k
}
45
46
const std::vector<std::pair<std::string, FeeEstimateMode>>& FeeModeMap()
47
26.8k
{
48
26.8k
    static const std::vector<std::pair<std::string, FeeEstimateMode>> FEE_MODES = {
49
26.8k
        {"unset", FeeEstimateMode::UNSET},
50
26.8k
        {"economical", FeeEstimateMode::ECONOMICAL},
51
26.8k
        {"conservative", FeeEstimateMode::CONSERVATIVE},
52
26.8k
    };
53
26.8k
    return FEE_MODES;
54
26.8k
}
55
56
std::string FeeModeInfo(const std::pair<std::string, FeeEstimateMode>& mode, std::string& default_info)
57
39.9k
{
58
39.9k
    switch (mode.second) {
59
13.3k
        case FeeEstimateMode::UNSET:
60
13.3k
            return strprintf("%s means no mode set (%s). \n", mode.first, default_info);
61
13.3k
        case FeeEstimateMode::ECONOMICAL:
62
13.3k
            return strprintf("%s estimates use a shorter time horizon, making them more\n"
63
13.3k
                   "responsive to short-term drops in the prevailing fee market. This mode\n"
64
13.3k
                   "potentially returns a lower fee rate estimate.\n", mode.first);
65
13.3k
        case FeeEstimateMode::CONSERVATIVE:
66
13.3k
            return strprintf("%s estimates use a longer time horizon, making them\n"
67
13.3k
                   "less responsive to short-term drops in the prevailing fee market. This mode\n"
68
13.3k
                   "potentially returns a higher fee rate estimate.\n", mode.first);
69
39.9k
    } // no default case, so the compiler can warn about missing cases
70
39.9k
    assert(false);
71
0
}
72
73
std::string FeeModesDetail(std::string default_info)
74
13.3k
{
75
13.3k
    std::string info;
76
39.9k
    for (const auto& fee_mode : FeeModeMap()) {
77
39.9k
        info += FeeModeInfo(fee_mode, default_info);
78
39.9k
    }
79
13.3k
    return strprintf("%s \n%s", FeeModes(", "), info);
80
13.3k
}
81
82
std::string FeeModes(const std::string& delimiter)
83
13.3k
{
84
40.0k
    return Join(FeeModeMap(), delimiter, [&](const std::pair<std::string, FeeEstimateMode>& i) { return i.first; });
85
13.3k
}
86
87
std::string InvalidEstimateModeErrorMessage()
88
28
{
89
28
    return "Invalid estimate_mode parameter, must be one of: \"" + FeeModes("\", \"") + "\"";
90
28
}
91
92
bool FeeModeFromString(std::string_view mode_string, FeeEstimateMode& fee_estimate_mode)
93
231
{
94
231
    auto searchkey = ToUpper(mode_string);
95
498
    for (const auto& pair : FeeModeMap()) {
96
498
        if (ToUpper(pair.first) == searchkey) {
97
203
            fee_estimate_mode = pair.second;
98
203
            return true;
99
203
        }
100
498
    }
101
28
    return false;
102
231
}
103
104
bilingual_str PSBTErrorString(PSBTError err)
105
15
{
106
15
    switch (err) {
107
0
        case PSBTError::MISSING_INPUTS:
108
0
            return Untranslated("Inputs missing or spent");
109
14
        case PSBTError::SIGHASH_MISMATCH:
110
14
            return Untranslated("Specified sighash value does not match value stored in PSBT");
111
1
        case PSBTError::EXTERNAL_SIGNER_NOT_FOUND:
112
1
            return Untranslated("External signer not found");
113
0
        case PSBTError::EXTERNAL_SIGNER_FAILED:
114
0
            return Untranslated("External signer failed to sign");
115
0
        case PSBTError::UNSUPPORTED:
116
0
            return Untranslated("Signer does not support PSBT");
117
0
        case PSBTError::INCOMPLETE:
118
0
            return Untranslated("Input needs additional signatures or other data");
119
0
        case PSBTError::OK:
120
0
            return Untranslated("No errors");
121
15
    } // no default case, so the compiler can warn about missing cases
122
15
    assert(false);
123
0
}
124
125
bilingual_str TransactionErrorString(const TransactionError err)
126
30
{
127
30
    switch (err) {
128
0
        case TransactionError::OK:
129
0
            return Untranslated("No error");
130
0
        case TransactionError::MISSING_INPUTS:
131
0
            return Untranslated("Inputs missing or spent");
132
3
        case TransactionError::ALREADY_IN_UTXO_SET:
133
3
            return Untranslated("Transaction outputs already in utxo set");
134
0
        case TransactionError::MEMPOOL_REJECTED:
135
0
            return Untranslated("Transaction rejected by mempool");
136
0
        case TransactionError::MEMPOOL_ERROR:
137
0
            return Untranslated("Mempool internal error");
138
22
        case TransactionError::MAX_FEE_EXCEEDED:
139
22
            return Untranslated("Fee exceeds maximum configured by user (e.g. -maxtxfee, maxfeerate)");
140
5
        case TransactionError::MAX_BURN_EXCEEDED:
141
5
            return Untranslated("Unspendable output exceeds maximum configured by user (maxburnamount)");
142
0
        case TransactionError::INVALID_PACKAGE:
143
0
            return Untranslated("Transaction rejected due to invalid package");
144
30
    } // no default case, so the compiler can warn about missing cases
145
30
    assert(false);
146
0
}
147
148
bilingual_str ResolveErrMsg(const std::string& optname, const std::string& strBind)
149
3
{
150
3
    return strprintf(_("Cannot resolve -%s address: '%s'"), optname, strBind);
151
3
}
152
153
bilingual_str InvalidPortErrMsg(const std::string& optname, const std::string& invalid_value)
154
13
{
155
13
    return strprintf(_("Invalid port specified in %s: '%s'"), optname, invalid_value);
156
13
}
157
158
bilingual_str AmountHighWarn(const std::string& optname)
159
7
{
160
7
    return strprintf(_("%s is set very high!"), optname);
161
7
}
162
163
bilingual_str AmountErrMsg(const std::string& optname, const std::string& strValue)
164
0
{
165
0
    return strprintf(_("Invalid amount for -%s=<amount>: '%s'"), optname, strValue);
166
0
}
167
} // namespace common