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