Bitcoin Core 31.99.0
P2P Digital Currency
messages.cpp
Go to the documentation of this file.
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
24using util::Join;
25
26namespace common {
27std::string StringForFeeReason(FeeReason reason)
28{
29 static const std::map<FeeReason, std::string> fee_reason_strings = {
30 {FeeReason::FEE_RATE_ESTIMATOR, "Fee Rate Estimator"},
31 {FeeReason::MEMPOOL_MIN, "Mempool Min Fee"},
32 {FeeReason::USER_SPECIFIED, "User Specified Fee"},
33 {FeeReason::FALLBACK, "Fallback fee"},
34 {FeeReason::REQUIRED, "Minimum Required Fee"},
35 };
36 auto reason_string = fee_reason_strings.find(reason);
37
38 if (reason_string == fee_reason_strings.end()) return "Unknown";
39
40 return reason_string->second;
41}
42
43const std::vector<std::pair<std::string, FeeEstimateMode>>& FeeModeMap()
44{
45 static const std::vector<std::pair<std::string, FeeEstimateMode>> FEE_MODES = {
46 {"unset", FeeEstimateMode::UNSET},
47 {"economical", FeeEstimateMode::ECONOMICAL},
48 {"conservative", FeeEstimateMode::CONSERVATIVE},
49 };
50 return FEE_MODES;
51}
52
53std::string FeeModeInfo(const std::pair<std::string, FeeEstimateMode>& mode, std::string& default_info)
54{
55 switch (mode.second) {
57 return strprintf("%s means no mode set (%s). \n", mode.first, default_info);
59 return strprintf("%s mode potentially returns a lower fee rate estimate.\n", mode.first);
61 return strprintf("%s potentially returns a higher fee rate estimate.\n", mode.first);
62 } // no default case, so the compiler can warn about missing cases
63 assert(false);
64}
65
66std::string FeeModesDetail(std::string default_info)
67{
68 std::string info;
69 for (const auto& fee_mode : FeeModeMap()) {
70 info += FeeModeInfo(fee_mode, default_info);
71 }
72 return strprintf("%s \n%s", FeeModes(", "), info);
73}
74
75std::string FeeModes(const std::string& delimiter)
76{
77 return Join(FeeModeMap(), delimiter, [&](const std::pair<std::string, FeeEstimateMode>& i) { return i.first; });
78}
79
81{
82 return "Invalid estimate_mode parameter, must be one of: \"" + FeeModes("\", \"") + "\"";
83}
84
85bool FeeModeFromString(std::string_view mode_string, FeeEstimateMode& fee_estimate_mode)
86{
87 auto searchkey = ToUpper(mode_string);
88 for (const auto& pair : FeeModeMap()) {
89 if (ToUpper(pair.first) == searchkey) {
90 fee_estimate_mode = pair.second;
91 return true;
92 }
93 }
94 return false;
95}
96
98{
99 switch (err) {
101 return Untranslated("Inputs missing or spent");
103 return Untranslated("Specified sighash value does not match value stored in PSBT");
105 return Untranslated("External signer not found");
107 return Untranslated("External signer failed to sign");
109 return Untranslated("Signer does not support PSBT");
111 return Untranslated("Input needs additional signatures or other data");
113 return Untranslated("The transaction cannot be valid");
114 } // no default case, so the compiler can warn about missing cases
115 assert(false);
116}
117
119{
120 switch (err) {
121 case TransactionError::OK:
122 return Untranslated("No error");
123 case TransactionError::MISSING_INPUTS:
124 return Untranslated("Inputs missing or spent");
125 case TransactionError::ALREADY_IN_UTXO_SET:
126 return Untranslated("Transaction outputs already in utxo set");
127 case TransactionError::MEMPOOL_REJECTED:
128 return Untranslated("Transaction rejected by mempool");
129 case TransactionError::MEMPOOL_ERROR:
130 return Untranslated("Mempool internal error");
131 case TransactionError::MAX_FEE_EXCEEDED:
132 return Untranslated("Fee exceeds maximum configured by user (e.g. -maxtxfee, maxfeerate)");
133 case TransactionError::MAX_BURN_EXCEEDED:
134 return Untranslated("Unspendable output exceeds maximum configured by user (maxburnamount)");
135 case TransactionError::INVALID_PACKAGE:
136 return Untranslated("Transaction rejected due to invalid package");
137 case TransactionError::PRIVATE_BROADCAST_FULL:
138 return Untranslated("Private broadcast queue is full");
139 } // no default case, so the compiler can warn about missing cases
140 assert(false);
141}
142
143bilingual_str ResolveErrMsg(const std::string& optname, const std::string& strBind)
144{
145 return strprintf(_("Cannot resolve -%s address: '%s'"), optname, strBind);
146}
147
148bilingual_str InvalidPortErrMsg(const std::string& optname, const std::string& invalid_value)
149{
150 return strprintf(_("Invalid port specified in %s: '%s'"), optname, invalid_value);
151}
152
153bilingual_str AmountHighWarn(const std::string& optname)
154{
155 return strprintf(_("%s is set very high!"), optname);
156}
157
158bilingual_str AmountErrMsg(const std::string& optname, const std::string& strValue)
159{
160 return strprintf(_("Invalid amount for -%s=<amount>: '%s'"), optname, strValue);
161}
162} // namespace common
is a home for simple enum and struct type definitions that can be used internally by functions in the...
is a home for simple string functions returning descriptive messages that are used in RPC and GUI int...
Definition: init.cpp:17
PSBTError
Definition: types.h:19
bilingual_str PSBTErrorString(PSBTError err)
Definition: messages.cpp:97
std::string FeeModesDetail(std::string default_info)
Definition: messages.cpp:66
std::string FeeModeInfo(const std::pair< std::string, FeeEstimateMode > &mode, std::string &default_info)
Definition: messages.cpp:53
bilingual_str TransactionErrorString(const TransactionError err)
Definition: messages.cpp:118
std::string FeeModes(const std::string &delimiter)
Definition: messages.cpp:75
bilingual_str AmountErrMsg(const std::string &optname, const std::string &strValue)
Definition: messages.cpp:158
bool FeeModeFromString(std::string_view mode_string, FeeEstimateMode &fee_estimate_mode)
Definition: messages.cpp:85
std::string InvalidEstimateModeErrorMessage()
Definition: messages.cpp:80
bilingual_str ResolveErrMsg(const std::string &optname, const std::string &strBind)
Definition: messages.cpp:143
const std::vector< std::pair< std::string, FeeEstimateMode > > & FeeModeMap()
Definition: messages.cpp:43
std::string StringForFeeReason(FeeReason reason)
Definition: messages.cpp:27
bilingual_str AmountHighWarn(const std::string &optname)
Definition: messages.cpp:153
bilingual_str InvalidPortErrMsg(const std::string &optname, const std::string &invalid_value)
Definition: messages.cpp:148
TransactionError
Definition: types.h:19
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
Definition: string.h:208
is a home for public enum and struct type definitions that are used internally by node code,...
Bilingual messages:
Definition: translation.h:24
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1172
consteval auto _(util::TranslatedLiteral str)
Definition: translation.h:79
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:82
FeeReason
Definition: fees.h:24
@ FEE_RATE_ESTIMATOR
FeeEstimateMode
Definition: fees.h:17
@ CONSERVATIVE
Force Fee rate estimator to return conservative estimates.
@ UNSET
Use default settings based on other criteria.
@ ECONOMICAL
Force Fee rate estimator to return non-conservative estimates.
std::string ToUpper(std::string_view str)
Returns the uppercase equivalent of the given string.
assert(!tx.IsCoinBase())