Bitcoin Core 32.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 {
27const std::map<FeeReason, std::string>& FeeReasonMap()
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 return fee_reason_strings;
37}
38
39std::string StringForFeeReason(FeeReason reason)
40{
41 const auto& fee_reason_strings = FeeReasonMap();
42 auto reason_string = fee_reason_strings.find(reason);
43
44 if (reason_string == fee_reason_strings.end()) return "Unknown";
45
46 return reason_string->second;
47}
48
49std::string FeeReasons(const std::string& delimiter)
50{
51 return Join(FeeReasonMap(), delimiter, [](const auto& reason) { return reason.second; });
52}
53
54const std::vector<std::pair<std::string, FeeEstimateMode>>& FeeModeMap()
55{
56 static const std::vector<std::pair<std::string, FeeEstimateMode>> FEE_MODES = {
57 {"unset", FeeEstimateMode::UNSET},
58 {"economical", FeeEstimateMode::ECONOMICAL},
59 {"conservative", FeeEstimateMode::CONSERVATIVE},
60 };
61 return FEE_MODES;
62}
63
64std::string FeeModeInfo(const std::pair<std::string, FeeEstimateMode>& mode, std::string& default_info)
65{
66 switch (mode.second) {
68 return strprintf("%s means no mode set (%s). \n", mode.first, default_info);
70 return strprintf("%s mode potentially returns a lower fee rate estimate.\n", mode.first);
72 return strprintf("%s potentially returns a higher fee rate estimate.\n", mode.first);
73 } // no default case, so the compiler can warn about missing cases
74 assert(false);
75}
76
77std::string FeeModesDetail(std::string default_info)
78{
79 std::string info;
80 for (const auto& fee_mode : FeeModeMap()) {
81 info += FeeModeInfo(fee_mode, default_info);
82 }
83 return strprintf("%s \n%s", FeeModes(", "), info);
84}
85
86std::string FeeModes(const std::string& delimiter)
87{
88 return Join(FeeModeMap(), delimiter, [&](const std::pair<std::string, FeeEstimateMode>& i) { return i.first; });
89}
90
92{
93 return "Invalid estimate_mode parameter, must be one of: \"" + FeeModes("\", \"") + "\"";
94}
95
96bool FeeModeFromString(std::string_view mode_string, FeeEstimateMode& fee_estimate_mode)
97{
98 auto searchkey = ToUpper(mode_string);
99 for (const auto& pair : FeeModeMap()) {
100 if (ToUpper(pair.first) == searchkey) {
101 fee_estimate_mode = pair.second;
102 return true;
103 }
104 }
105 return false;
106}
107
109{
110 switch (err) {
112 return Untranslated("Inputs missing or spent");
114 return Untranslated("Specified sighash value does not match value stored in PSBT");
116 return Untranslated("External signer not found");
118 return Untranslated("External signer failed to sign");
120 return Untranslated("Signer does not support PSBT");
122 return Untranslated("Input needs additional signatures or other data");
124 return Untranslated("The transaction cannot be valid");
125 } // no default case, so the compiler can warn about missing cases
126 assert(false);
127}
128
130{
131 switch (err) {
132 case TransactionError::OK:
133 return Untranslated("No error");
134 case TransactionError::MISSING_INPUTS:
135 return Untranslated("Inputs missing or spent");
136 case TransactionError::ALREADY_IN_UTXO_SET:
137 return Untranslated("Transaction outputs already in utxo set");
138 case TransactionError::MEMPOOL_REJECTED:
139 return Untranslated("Transaction rejected by mempool");
140 case TransactionError::MEMPOOL_ERROR:
141 return Untranslated("Mempool internal error");
142 case TransactionError::MAX_FEE_EXCEEDED:
143 return Untranslated("Fee exceeds maximum configured by user (maxtxfee)");
144 case TransactionError::MAX_FEE_RATE_EXCEEDED:
145 return Untranslated("Fee rate exceeds maximum configured by user (maxfeerate)");
146 case TransactionError::MAX_BURN_EXCEEDED:
147 return Untranslated("Unspendable output exceeds maximum configured by user (maxburnamount)");
148 case TransactionError::INVALID_PACKAGE:
149 return Untranslated("Transaction rejected due to invalid package");
150 case TransactionError::PRIVATE_BROADCAST_FULL:
151 return Untranslated("Private broadcast queue is full");
152 } // no default case, so the compiler can warn about missing cases
153 assert(false);
154}
155
156bilingual_str ResolveErrMsg(const std::string& optname, const std::string& strBind)
157{
158 return strprintf(_("Cannot resolve -%s address: '%s'"), optname, strBind);
159}
160
161bilingual_str InvalidPortErrMsg(const std::string& optname, const std::string& invalid_value)
162{
163 return strprintf(_("Invalid port specified in %s: '%s'"), optname, invalid_value);
164}
165
166bilingual_str AmountHighWarn(const std::string& optname)
167{
168 return strprintf(_("%s is set very high!"), optname);
169}
170
171bilingual_str AmountErrMsg(const std::string& optname, const std::string& strValue)
172{
173 return strprintf(_("Invalid amount for -%s=<amount>: '%s'"), optname, strValue);
174}
175} // 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
const std::map< FeeReason, std::string > & FeeReasonMap()
Definition: messages.cpp:27
PSBTError
Definition: types.h:19
bilingual_str PSBTErrorString(PSBTError err)
Definition: messages.cpp:108
std::string FeeModesDetail(std::string default_info)
Definition: messages.cpp:77
std::string FeeModeInfo(const std::pair< std::string, FeeEstimateMode > &mode, std::string &default_info)
Definition: messages.cpp:64
bilingual_str TransactionErrorString(const TransactionError err)
Definition: messages.cpp:129
std::string FeeReasons(const std::string &delimiter)
Definition: messages.cpp:49
std::string FeeModes(const std::string &delimiter)
Definition: messages.cpp:86
bilingual_str AmountErrMsg(const std::string &optname, const std::string &strValue)
Definition: messages.cpp:171
bool FeeModeFromString(std::string_view mode_string, FeeEstimateMode &fee_estimate_mode)
Definition: messages.cpp:96
std::string InvalidEstimateModeErrorMessage()
Definition: messages.cpp:91
bilingual_str ResolveErrMsg(const std::string &optname, const std::string &strBind)
Definition: messages.cpp:156
const std::vector< std::pair< std::string, FeeEstimateMode > > & FeeModeMap()
Definition: messages.cpp:54
std::string StringForFeeReason(FeeReason reason)
Definition: messages.cpp:39
bilingual_str AmountHighWarn(const std::string &optname)
Definition: messages.cpp:166
bilingual_str InvalidPortErrMsg(const std::string &optname, const std::string &invalid_value)
Definition: messages.cpp:161
TransactionError
Definition: types.h:19
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
Definition: string.h:209
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:25
@ FEE_RATE_ESTIMATOR
FeeEstimateMode
Definition: fees.h:18
@ 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())