Coverage Report

Created: 2026-09-01 13:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/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
8
#include <common/types.h>
9
#include <node/types.h>
10
#include <tinyformat.h>
11
#include <util/check.h>
12
#include <util/fees.h>
13
#include <util/strencodings.h>
14
#include <util/string.h>
15
#include <util/translation.h>
16
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
1.17k
{
29
1.17k
    static const std::map<FeeReason, std::string> fee_reason_strings = {
30
1.17k
        {FeeReason::FEE_RATE_ESTIMATOR, "Fee Rate Estimator"},
31
1.17k
        {FeeReason::MEMPOOL_MIN, "Mempool Min Fee"},
32
1.17k
        {FeeReason::USER_SPECIFIED, "User Specified Fee"},
33
1.17k
        {FeeReason::FALLBACK, "Fallback fee"},
34
1.17k
        {FeeReason::REQUIRED, "Minimum Required Fee"},
35
1.17k
    };
36
1.17k
    auto reason_string = fee_reason_strings.find(reason);
37
38
1.17k
    if (reason_string == fee_reason_strings.end()) return "Unknown";
  Branch (38:9): [True: 0, False: 1.17k]
39
40
1.17k
    return reason_string->second;
41
1.17k
}
42
43
const std::vector<std::pair<std::string, FeeEstimateMode>>& FeeModeMap()
44
1.72k
{
45
1.72k
    static const std::vector<std::pair<std::string, FeeEstimateMode>> FEE_MODES = {
46
1.72k
        {"unset", FeeEstimateMode::UNSET},
47
1.72k
        {"economical", FeeEstimateMode::ECONOMICAL},
48
1.72k
        {"conservative", FeeEstimateMode::CONSERVATIVE},
49
1.72k
    };
50
1.72k
    return FEE_MODES;
51
1.72k
}
52
53
std::string FeeModeInfo(const std::pair<std::string, FeeEstimateMode>& mode, std::string& default_info)
54
153
{
55
153
    switch (mode.second) {
  Branch (55:13): [True: 0, False: 153]
56
51
        case FeeEstimateMode::UNSET:
  Branch (56:9): [True: 51, False: 102]
57
51
            return strprintf("%s means no mode set (%s). \n", mode.first, default_info);
58
51
        case FeeEstimateMode::ECONOMICAL:
  Branch (58:9): [True: 51, False: 102]
59
51
            return strprintf("%s mode potentially returns a lower fee rate estimate.\n", mode.first);
60
51
        case FeeEstimateMode::CONSERVATIVE:
  Branch (60:9): [True: 51, False: 102]
61
51
            return strprintf("%s potentially returns a higher fee rate estimate.\n", mode.first);
62
153
    } // no default case, so the compiler can warn about missing cases
63
153
    assert(false);
  Branch (63:5): [Folded - Ignored]
64
0
}
65
66
std::string FeeModesDetail(std::string default_info)
67
51
{
68
51
    std::string info;
69
153
    for (const auto& fee_mode : FeeModeMap()) {
  Branch (69:31): [True: 153, False: 51]
70
153
        info += FeeModeInfo(fee_mode, default_info);
71
153
    }
72
51
    return strprintf("%s \n%s", FeeModes(", "), info);
73
51
}
74
75
std::string FeeModes(const std::string& delimiter)
76
51
{
77
153
    return Join(FeeModeMap(), delimiter, [&](const std::pair<std::string, FeeEstimateMode>& i) { return i.first; });
78
51
}
79
80
std::string InvalidEstimateModeErrorMessage()
81
0
{
82
0
    return "Invalid estimate_mode parameter, must be one of: \"" + FeeModes("\", \"") + "\"";
83
0
}
84
85
bool FeeModeFromString(std::string_view mode_string, FeeEstimateMode& fee_estimate_mode)
86
1.61k
{
87
1.61k
    auto searchkey = ToUpper(mode_string);
88
4.85k
    for (const auto& pair : FeeModeMap()) {
  Branch (88:27): [True: 4.85k, False: 1.61k]
89
4.85k
        if (ToUpper(pair.first) == searchkey) {
  Branch (89:13): [True: 3, False: 4.85k]
90
3
            fee_estimate_mode = pair.second;
91
3
            return true;
92
3
        }
93
4.85k
    }
94
1.61k
    return false;
95
1.61k
}
96
97
bilingual_str PSBTErrorString(PSBTError err)
98
9
{
99
9
    switch (err) {
  Branch (99:13): [True: 0, False: 9]
100
0
        case PSBTError::MISSING_INPUTS:
  Branch (100:9): [True: 0, False: 9]
101
0
            return Untranslated("Inputs missing or spent");
102
9
        case PSBTError::SIGHASH_MISMATCH:
  Branch (102:9): [True: 9, False: 0]
103
9
            return Untranslated("Specified sighash value does not match value stored in PSBT");
104
0
        case PSBTError::EXTERNAL_SIGNER_NOT_FOUND:
  Branch (104:9): [True: 0, False: 9]
105
0
            return Untranslated("External signer not found");
106
0
        case PSBTError::EXTERNAL_SIGNER_FAILED:
  Branch (106:9): [True: 0, False: 9]
107
0
            return Untranslated("External signer failed to sign");
108
0
        case PSBTError::UNSUPPORTED:
  Branch (108:9): [True: 0, False: 9]
109
0
            return Untranslated("Signer does not support PSBT");
110
0
        case PSBTError::INCOMPLETE:
  Branch (110:9): [True: 0, False: 9]
111
0
            return Untranslated("Input needs additional signatures or other data");
112
0
        case PSBTError::INVALID_TX:
  Branch (112:9): [True: 0, False: 9]
113
0
            return Untranslated("The transaction cannot be valid");
114
9
    } // no default case, so the compiler can warn about missing cases
115
9
    assert(false);
  Branch (115:5): [Folded - Ignored]
116
0
}
117
118
bilingual_str TransactionErrorString(const TransactionError err)
119
408
{
120
408
    switch (err) {
  Branch (120:13): [True: 0, False: 408]
121
0
        case TransactionError::OK:
  Branch (121:9): [True: 0, False: 408]
122
0
            return Untranslated("No error");
123
40
        case TransactionError::MISSING_INPUTS:
  Branch (123:9): [True: 40, False: 368]
124
40
            return Untranslated("Inputs missing or spent");
125
26
        case TransactionError::ALREADY_IN_UTXO_SET:
  Branch (125:9): [True: 26, False: 382]
126
26
            return Untranslated("Transaction outputs already in utxo set");
127
24
        case TransactionError::MEMPOOL_REJECTED:
  Branch (127:9): [True: 24, False: 384]
128
24
            return Untranslated("Transaction rejected by mempool");
129
18
        case TransactionError::MEMPOOL_ERROR:
  Branch (129:9): [True: 18, False: 390]
130
18
            return Untranslated("Mempool internal error");
131
284
        case TransactionError::MAX_FEE_EXCEEDED:
  Branch (131:9): [True: 284, False: 124]
132
284
            return Untranslated("Fee exceeds maximum configured by user (e.g. -maxtxfee, maxfeerate)");
133
16
        case TransactionError::MAX_BURN_EXCEEDED:
  Branch (133:9): [True: 16, False: 392]
134
16
            return Untranslated("Unspendable output exceeds maximum configured by user (maxburnamount)");
135
0
        case TransactionError::INVALID_PACKAGE:
  Branch (135:9): [True: 0, False: 408]
136
0
            return Untranslated("Transaction rejected due to invalid package");
137
0
        case TransactionError::PRIVATE_BROADCAST_FULL:
  Branch (137:9): [True: 0, False: 408]
138
0
            return Untranslated("Private broadcast queue is full");
139
408
    } // no default case, so the compiler can warn about missing cases
140
408
    assert(false);
  Branch (140:5): [Folded - Ignored]
141
0
}
142
143
bilingual_str ResolveErrMsg(const std::string& optname, const std::string& strBind)
144
1.93k
{
145
1.93k
    return strprintf(_("Cannot resolve -%s address: '%s'"), optname, strBind);
146
1.93k
}
147
148
bilingual_str InvalidPortErrMsg(const std::string& optname, const std::string& invalid_value)
149
0
{
150
0
    return strprintf(_("Invalid port specified in %s: '%s'"), optname, invalid_value);
151
0
}
152
153
bilingual_str AmountHighWarn(const std::string& optname)
154
1.61k
{
155
1.61k
    return strprintf(_("%s is set very high!"), optname);
156
1.61k
}
157
158
bilingual_str AmountErrMsg(const std::string& optname, const std::string& strValue)
159
1.61k
{
160
1.61k
    return strprintf(_("Invalid amount for -%s=<amount>: '%s'"), optname, strValue);
161
1.61k
}
162
} // namespace common