/Users/brunogarcia/projects/bitcoin-core-dev/src/rpc/util.cpp
Line | Count | Source |
1 | | // Copyright (c) 2017-present The Bitcoin Core developers |
2 | | // Distributed under the MIT software license, see the accompanying |
3 | | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
4 | | |
5 | | #include <chain.h> |
6 | | #include <common/args.h> |
7 | | #include <common/messages.h> |
8 | | #include <common/types.h> |
9 | | #include <consensus/amount.h> |
10 | | #include <core_io.h> |
11 | | #include <key_io.h> |
12 | | #include <node/types.h> |
13 | | #include <outputtype.h> |
14 | | #include <pow.h> |
15 | | #include <rpc/util.h> |
16 | | #include <script/descriptor.h> |
17 | | #include <script/interpreter.h> |
18 | | #include <script/signingprovider.h> |
19 | | #include <script/solver.h> |
20 | | #include <tinyformat.h> |
21 | | #include <uint256.h> |
22 | | #include <univalue.h> |
23 | | #include <util/check.h> |
24 | | #include <util/result.h> |
25 | | #include <util/strencodings.h> |
26 | | #include <util/string.h> |
27 | | #include <util/translation.h> |
28 | | |
29 | | #include <algorithm> |
30 | | #include <iterator> |
31 | | #include <string_view> |
32 | | #include <tuple> |
33 | | #include <utility> |
34 | | |
35 | | using common::PSBTError; |
36 | | using common::PSBTErrorString; |
37 | | using common::TransactionErrorString; |
38 | | using node::TransactionError; |
39 | | using util::Join; |
40 | | using util::SplitString; |
41 | | using util::TrimString; |
42 | | |
43 | | const std::string UNIX_EPOCH_TIME = "UNIX epoch time"; |
44 | | const std::string EXAMPLE_ADDRESS[2] = {"bc1q09vm5lfy0j5reeulh4x5752q25uqqvz34hufdl", "bc1q02ad21edsxd23d32dfgqqsz4vv4nmtfzuklhy3"}; |
45 | | |
46 | | std::string GetAllOutputTypes() |
47 | 0 | { |
48 | 0 | std::vector<std::string> ret; |
49 | 0 | using U = std::underlying_type_t<TxoutType>; |
50 | 0 | for (U i = (U)TxoutType::NONSTANDARD; i <= (U)TxoutType::WITNESS_UNKNOWN; ++i) { |
51 | 0 | ret.emplace_back(GetTxnOutputType(static_cast<TxoutType>(i))); |
52 | 0 | } |
53 | 0 | return Join(ret, ", "); |
54 | 0 | } |
55 | | |
56 | | void RPCTypeCheckObj(const UniValue& o, |
57 | | const std::map<std::string, UniValueType>& typesExpected, |
58 | | bool fAllowNull, |
59 | | bool fStrict) |
60 | 0 | { |
61 | 0 | for (const auto& t : typesExpected) { |
62 | 0 | const UniValue& v = o.find_value(t.first); |
63 | 0 | if (!fAllowNull && v.isNull()) |
64 | 0 | throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Missing %s", t.first));Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
65 | | |
66 | 0 | if (!(t.second.typeAny || v.type() == t.second.type || (fAllowNull && v.isNull()))) |
67 | 0 | throw JSONRPCError(RPC_TYPE_ERROR, strprintf("JSON value of type %s for field %s is not of expected type %s", uvTypeName(v.type()), t.first, uvTypeName(t.second.type)));Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
68 | 0 | } |
69 | | |
70 | 0 | if (fStrict) |
71 | 0 | { |
72 | 0 | for (const std::string& k : o.getKeys()) |
73 | 0 | { |
74 | 0 | if (typesExpected.count(k) == 0) |
75 | 0 | { |
76 | 0 | std::string err = strprintf("Unexpected key %s", k);Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
77 | 0 | throw JSONRPCError(RPC_TYPE_ERROR, err); |
78 | 0 | } |
79 | 0 | } |
80 | 0 | } |
81 | 0 | } |
82 | | |
83 | | int ParseVerbosity(const UniValue& arg, int default_verbosity, bool allow_bool) |
84 | 0 | { |
85 | 0 | if (!arg.isNull()) { |
86 | 0 | if (arg.isBool()) { |
87 | 0 | if (!allow_bool) { |
88 | 0 | throw JSONRPCError(RPC_TYPE_ERROR, "Verbosity was boolean but only integer allowed"); |
89 | 0 | } |
90 | 0 | return arg.get_bool(); // true = 1 |
91 | 0 | } else { |
92 | 0 | return arg.getInt<int>(); |
93 | 0 | } |
94 | 0 | } |
95 | 0 | return default_verbosity; |
96 | 0 | } |
97 | | |
98 | | CAmount AmountFromValue(const UniValue& value, int decimals) |
99 | 0 | { |
100 | 0 | if (!value.isNum() && !value.isStr()) |
101 | 0 | throw JSONRPCError(RPC_TYPE_ERROR, "Amount is not a number or string"); |
102 | 0 | CAmount amount; |
103 | 0 | if (!ParseFixedPoint(value.getValStr(), decimals, &amount)) |
104 | 0 | throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount"); |
105 | 0 | if (!MoneyRange(amount)) |
106 | 0 | throw JSONRPCError(RPC_TYPE_ERROR, "Amount out of range"); |
107 | 0 | return amount; |
108 | 0 | } |
109 | | |
110 | | CFeeRate ParseFeeRate(const UniValue& json) |
111 | 0 | { |
112 | 0 | CAmount val{AmountFromValue(json)}; |
113 | 0 | if (val >= COIN) throw JSONRPCError(RPC_INVALID_PARAMETER, "Fee rates larger than or equal to 1BTC/kvB are not accepted"); |
114 | 0 | return CFeeRate{val}; |
115 | 0 | } |
116 | | |
117 | | uint256 ParseHashV(const UniValue& v, std::string_view name) |
118 | 0 | { |
119 | 0 | const std::string& strHex(v.get_str()); |
120 | 0 | if (auto rv{uint256::FromHex(strHex)}) return *rv; |
121 | 0 | if (auto expected_len{uint256::size() * 2}; strHex.length() != expected_len) { |
122 | 0 | throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be of length %d (not %d, for '%s')", name, expected_len, strHex.length(), strHex));Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
123 | 0 | } |
124 | 0 | throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be hexadecimal string (not '%s')", name, strHex));Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
125 | 0 | } |
126 | | uint256 ParseHashO(const UniValue& o, std::string_view strKey) |
127 | 0 | { |
128 | 0 | return ParseHashV(o.find_value(strKey), strKey); |
129 | 0 | } |
130 | | std::vector<unsigned char> ParseHexV(const UniValue& v, std::string_view name) |
131 | 0 | { |
132 | 0 | std::string strHex; |
133 | 0 | if (v.isStr()) |
134 | 0 | strHex = v.get_str(); |
135 | 0 | if (!IsHex(strHex)) |
136 | 0 | throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be hexadecimal string (not '%s')", name, strHex));Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
137 | 0 | return ParseHex(strHex); |
138 | 0 | } |
139 | | std::vector<unsigned char> ParseHexO(const UniValue& o, std::string_view strKey) |
140 | 0 | { |
141 | 0 | return ParseHexV(o.find_value(strKey), strKey); |
142 | 0 | } |
143 | | |
144 | | namespace { |
145 | | |
146 | | /** |
147 | | * Quote an argument for shell. |
148 | | * |
149 | | * @note This is intended for help, not for security-sensitive purposes. |
150 | | */ |
151 | | std::string ShellQuote(const std::string& s) |
152 | 0 | { |
153 | 0 | std::string result; |
154 | 0 | result.reserve(s.size() * 2); |
155 | 0 | for (const char ch: s) { |
156 | 0 | if (ch == '\'') { |
157 | 0 | result += "'\''"; |
158 | 0 | } else { |
159 | 0 | result += ch; |
160 | 0 | } |
161 | 0 | } |
162 | 0 | return "'" + result + "'"; |
163 | 0 | } |
164 | | |
165 | | /** |
166 | | * Shell-quotes the argument if it needs quoting, else returns it literally, to save typing. |
167 | | * |
168 | | * @note This is intended for help, not for security-sensitive purposes. |
169 | | */ |
170 | | std::string ShellQuoteIfNeeded(const std::string& s) |
171 | 0 | { |
172 | 0 | for (const char ch: s) { |
173 | 0 | if (ch == ' ' || ch == '\'' || ch == '"') { |
174 | 0 | return ShellQuote(s); |
175 | 0 | } |
176 | 0 | } |
177 | | |
178 | 0 | return s; |
179 | 0 | } |
180 | | |
181 | | } |
182 | | |
183 | | std::string HelpExampleCli(const std::string& methodname, const std::string& args) |
184 | 0 | { |
185 | 0 | return "> bitcoin-cli " + methodname + " " + args + "\n"; |
186 | 0 | } |
187 | | |
188 | | std::string HelpExampleCliNamed(const std::string& methodname, const RPCArgList& args) |
189 | 0 | { |
190 | 0 | std::string result = "> bitcoin-cli -named " + methodname; |
191 | 0 | for (const auto& argpair: args) { |
192 | 0 | const auto& value = argpair.second.isStr() |
193 | 0 | ? argpair.second.get_str() |
194 | 0 | : argpair.second.write(); |
195 | 0 | result += " " + argpair.first + "=" + ShellQuoteIfNeeded(value); |
196 | 0 | } |
197 | 0 | result += "\n"; |
198 | 0 | return result; |
199 | 0 | } |
200 | | |
201 | | std::string HelpExampleRpc(const std::string& methodname, const std::string& args) |
202 | 0 | { |
203 | 0 | return "> curl --user myusername --data-binary '{\"jsonrpc\": \"2.0\", \"id\": \"curltest\", " |
204 | 0 | "\"method\": \"" + methodname + "\", \"params\": [" + args + "]}' -H 'content-type: application/json' http://127.0.0.1:8332/\n"; |
205 | 0 | } |
206 | | |
207 | | std::string HelpExampleRpcNamed(const std::string& methodname, const RPCArgList& args) |
208 | 0 | { |
209 | 0 | UniValue params(UniValue::VOBJ); |
210 | 0 | for (const auto& param: args) { |
211 | 0 | params.pushKV(param.first, param.second); |
212 | 0 | } |
213 | |
|
214 | 0 | return "> curl --user myusername --data-binary '{\"jsonrpc\": \"2.0\", \"id\": \"curltest\", " |
215 | 0 | "\"method\": \"" + methodname + "\", \"params\": " + params.write() + "}' -H 'content-type: application/json' http://127.0.0.1:8332/\n"; |
216 | 0 | } |
217 | | |
218 | | // Converts a hex string to a public key if possible |
219 | | CPubKey HexToPubKey(const std::string& hex_in) |
220 | 0 | { |
221 | 0 | if (!IsHex(hex_in)) { |
222 | 0 | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Pubkey \"" + hex_in + "\" must be a hex string"); |
223 | 0 | } |
224 | 0 | if (hex_in.length() != 66 && hex_in.length() != 130) { |
225 | 0 | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Pubkey \"" + hex_in + "\" must have a length of either 33 or 65 bytes"); |
226 | 0 | } |
227 | 0 | CPubKey vchPubKey(ParseHex(hex_in)); |
228 | 0 | if (!vchPubKey.IsFullyValid()) { |
229 | 0 | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Pubkey \"" + hex_in + "\" must be cryptographically valid."); |
230 | 0 | } |
231 | 0 | return vchPubKey; |
232 | 0 | } |
233 | | |
234 | | // Creates a multisig address from a given list of public keys, number of signatures required, and the address type |
235 | | CTxDestination AddAndGetMultisigDestination(const int required, const std::vector<CPubKey>& pubkeys, OutputType type, FlatSigningProvider& keystore, CScript& script_out) |
236 | 0 | { |
237 | | // Gather public keys |
238 | 0 | if (required < 1) { |
239 | 0 | throw JSONRPCError(RPC_INVALID_PARAMETER, "a multisignature address must require at least one key to redeem"); |
240 | 0 | } |
241 | 0 | if ((int)pubkeys.size() < required) { |
242 | 0 | throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("not enough keys supplied (got %u keys, but need at least %d to redeem)", pubkeys.size(), required));Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
243 | 0 | } |
244 | 0 | if (pubkeys.size() > MAX_PUBKEYS_PER_MULTISIG) { |
245 | 0 | throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Number of keys involved in the multisignature address creation > %d\nReduce the number", MAX_PUBKEYS_PER_MULTISIG));Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
246 | 0 | } |
247 | | |
248 | 0 | script_out = GetScriptForMultisig(required, pubkeys); |
249 | | |
250 | | // Check if any keys are uncompressed. If so, the type is legacy |
251 | 0 | for (const CPubKey& pk : pubkeys) { |
252 | 0 | if (!pk.IsCompressed()) { |
253 | 0 | type = OutputType::LEGACY; |
254 | 0 | break; |
255 | 0 | } |
256 | 0 | } |
257 | |
|
258 | 0 | if (type == OutputType::LEGACY && script_out.size() > MAX_SCRIPT_ELEMENT_SIZE) { |
259 | 0 | throw JSONRPCError(RPC_INVALID_PARAMETER, (strprintf("redeemScript exceeds size limit: %d > %d", script_out.size(), MAX_SCRIPT_ELEMENT_SIZE)));Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
260 | 0 | } |
261 | | |
262 | | // Make the address |
263 | 0 | CTxDestination dest = AddAndGetDestinationForScript(keystore, script_out, type); |
264 | |
|
265 | 0 | return dest; |
266 | 0 | } |
267 | | |
268 | | class DescribeAddressVisitor |
269 | | { |
270 | | public: |
271 | | explicit DescribeAddressVisitor() = default; |
272 | | |
273 | | UniValue operator()(const CNoDestination& dest) const |
274 | 0 | { |
275 | 0 | return UniValue(UniValue::VOBJ); |
276 | 0 | } |
277 | | |
278 | | UniValue operator()(const PubKeyDestination& dest) const |
279 | 0 | { |
280 | 0 | return UniValue(UniValue::VOBJ); |
281 | 0 | } |
282 | | |
283 | | UniValue operator()(const PKHash& keyID) const |
284 | 0 | { |
285 | 0 | UniValue obj(UniValue::VOBJ); |
286 | 0 | obj.pushKV("isscript", false); |
287 | 0 | obj.pushKV("iswitness", false); |
288 | 0 | return obj; |
289 | 0 | } |
290 | | |
291 | | UniValue operator()(const ScriptHash& scriptID) const |
292 | 0 | { |
293 | 0 | UniValue obj(UniValue::VOBJ); |
294 | 0 | obj.pushKV("isscript", true); |
295 | 0 | obj.pushKV("iswitness", false); |
296 | 0 | return obj; |
297 | 0 | } |
298 | | |
299 | | UniValue operator()(const WitnessV0KeyHash& id) const |
300 | 0 | { |
301 | 0 | UniValue obj(UniValue::VOBJ); |
302 | 0 | obj.pushKV("isscript", false); |
303 | 0 | obj.pushKV("iswitness", true); |
304 | 0 | obj.pushKV("witness_version", 0); |
305 | 0 | obj.pushKV("witness_program", HexStr(id)); |
306 | 0 | return obj; |
307 | 0 | } |
308 | | |
309 | | UniValue operator()(const WitnessV0ScriptHash& id) const |
310 | 0 | { |
311 | 0 | UniValue obj(UniValue::VOBJ); |
312 | 0 | obj.pushKV("isscript", true); |
313 | 0 | obj.pushKV("iswitness", true); |
314 | 0 | obj.pushKV("witness_version", 0); |
315 | 0 | obj.pushKV("witness_program", HexStr(id)); |
316 | 0 | return obj; |
317 | 0 | } |
318 | | |
319 | | UniValue operator()(const WitnessV1Taproot& tap) const |
320 | 0 | { |
321 | 0 | UniValue obj(UniValue::VOBJ); |
322 | 0 | obj.pushKV("isscript", true); |
323 | 0 | obj.pushKV("iswitness", true); |
324 | 0 | obj.pushKV("witness_version", 1); |
325 | 0 | obj.pushKV("witness_program", HexStr(tap)); |
326 | 0 | return obj; |
327 | 0 | } |
328 | | |
329 | | UniValue operator()(const PayToAnchor& anchor) const |
330 | 0 | { |
331 | 0 | UniValue obj(UniValue::VOBJ); |
332 | 0 | obj.pushKV("isscript", true); |
333 | 0 | obj.pushKV("iswitness", true); |
334 | 0 | return obj; |
335 | 0 | } |
336 | | |
337 | | UniValue operator()(const WitnessUnknown& id) const |
338 | 0 | { |
339 | 0 | UniValue obj(UniValue::VOBJ); |
340 | 0 | obj.pushKV("iswitness", true); |
341 | 0 | obj.pushKV("witness_version", id.GetWitnessVersion()); |
342 | 0 | obj.pushKV("witness_program", HexStr(id.GetWitnessProgram())); |
343 | 0 | return obj; |
344 | 0 | } |
345 | | }; |
346 | | |
347 | | UniValue DescribeAddress(const CTxDestination& dest) |
348 | 0 | { |
349 | 0 | return std::visit(DescribeAddressVisitor(), dest); |
350 | 0 | } |
351 | | |
352 | | /** |
353 | | * Returns a sighash value corresponding to the passed in argument. |
354 | | * |
355 | | * @pre The sighash argument should be string or null. |
356 | | */ |
357 | | std::optional<int> ParseSighashString(const UniValue& sighash) |
358 | 0 | { |
359 | 0 | if (sighash.isNull()) { |
360 | 0 | return std::nullopt; |
361 | 0 | } |
362 | 0 | const auto result{SighashFromStr(sighash.get_str())}; |
363 | 0 | if (!result) { |
364 | 0 | throw JSONRPCError(RPC_INVALID_PARAMETER, util::ErrorString(result).original); |
365 | 0 | } |
366 | 0 | return result.value(); |
367 | 0 | } |
368 | | |
369 | | unsigned int ParseConfirmTarget(const UniValue& value, unsigned int max_target) |
370 | 0 | { |
371 | 0 | const int target{value.getInt<int>()}; |
372 | 0 | const unsigned int unsigned_target{static_cast<unsigned int>(target)}; |
373 | 0 | if (target < 1 || unsigned_target > max_target) { |
374 | 0 | throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid conf_target, must be between %u and %u", 1, max_target));Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
375 | 0 | } |
376 | 0 | return unsigned_target; |
377 | 0 | } |
378 | | |
379 | | RPCErrorCode RPCErrorFromPSBTError(PSBTError err) |
380 | 0 | { |
381 | 0 | switch (err) { |
382 | 0 | case PSBTError::UNSUPPORTED: |
383 | 0 | return RPC_INVALID_PARAMETER; |
384 | 0 | case PSBTError::SIGHASH_MISMATCH: |
385 | 0 | return RPC_DESERIALIZATION_ERROR; |
386 | 0 | default: break; |
387 | 0 | } |
388 | 0 | return RPC_TRANSACTION_ERROR; |
389 | 0 | } |
390 | | |
391 | | RPCErrorCode RPCErrorFromTransactionError(TransactionError terr) |
392 | 0 | { |
393 | 0 | switch (terr) { |
394 | 0 | case TransactionError::MEMPOOL_REJECTED: |
395 | 0 | return RPC_TRANSACTION_REJECTED; |
396 | 0 | case TransactionError::ALREADY_IN_UTXO_SET: |
397 | 0 | return RPC_VERIFY_ALREADY_IN_UTXO_SET; |
398 | 0 | default: break; |
399 | 0 | } |
400 | 0 | return RPC_TRANSACTION_ERROR; |
401 | 0 | } |
402 | | |
403 | | UniValue JSONRPCPSBTError(PSBTError err) |
404 | 0 | { |
405 | 0 | return JSONRPCError(RPCErrorFromPSBTError(err), PSBTErrorString(err).original); |
406 | 0 | } |
407 | | |
408 | | UniValue JSONRPCTransactionError(TransactionError terr, const std::string& err_string) |
409 | 0 | { |
410 | 0 | if (err_string.length() > 0) { |
411 | 0 | return JSONRPCError(RPCErrorFromTransactionError(terr), err_string); |
412 | 0 | } else { |
413 | 0 | return JSONRPCError(RPCErrorFromTransactionError(terr), TransactionErrorString(terr).original); |
414 | 0 | } |
415 | 0 | } |
416 | | |
417 | | /** |
418 | | * A pair of strings that can be aligned (through padding) with other Sections |
419 | | * later on |
420 | | */ |
421 | | struct Section { |
422 | | Section(const std::string& left, const std::string& right) |
423 | 0 | : m_left{left}, m_right{right} {} |
424 | | std::string m_left; |
425 | | const std::string m_right; |
426 | | }; |
427 | | |
428 | | /** |
429 | | * Keeps track of RPCArgs by transforming them into sections for the purpose |
430 | | * of serializing everything to a single string |
431 | | */ |
432 | | struct Sections { |
433 | | std::vector<Section> m_sections; |
434 | | size_t m_max_pad{0}; |
435 | | |
436 | | void PushSection(const Section& s) |
437 | 0 | { |
438 | 0 | m_max_pad = std::max(m_max_pad, s.m_left.size()); |
439 | 0 | m_sections.push_back(s); |
440 | 0 | } |
441 | | |
442 | | /** |
443 | | * Recursive helper to translate an RPCArg into sections |
444 | | */ |
445 | | // NOLINTNEXTLINE(misc-no-recursion) |
446 | | void Push(const RPCArg& arg, const size_t current_indent = 5, const OuterType outer_type = OuterType::NONE) |
447 | 0 | { |
448 | 0 | const auto indent = std::string(current_indent, ' '); |
449 | 0 | const auto indent_next = std::string(current_indent + 2, ' '); |
450 | 0 | const bool push_name{outer_type == OuterType::OBJ}; // Dictionary keys must have a name |
451 | 0 | const bool is_top_level_arg{outer_type == OuterType::NONE}; // True on the first recursion |
452 | |
|
453 | 0 | switch (arg.m_type) { |
454 | 0 | case RPCArg::Type::STR_HEX: |
455 | 0 | case RPCArg::Type::STR: |
456 | 0 | case RPCArg::Type::NUM: |
457 | 0 | case RPCArg::Type::AMOUNT: |
458 | 0 | case RPCArg::Type::RANGE: |
459 | 0 | case RPCArg::Type::BOOL: |
460 | 0 | case RPCArg::Type::OBJ_NAMED_PARAMS: { |
461 | 0 | if (is_top_level_arg) return; // Nothing more to do for non-recursive types on first recursion |
462 | 0 | auto left = indent; |
463 | 0 | if (arg.m_opts.type_str.size() != 0 && push_name) { |
464 | 0 | left += "\"" + arg.GetName() + "\": " + arg.m_opts.type_str.at(0); |
465 | 0 | } else { |
466 | 0 | left += push_name ? arg.ToStringObj(/*oneline=*/false) : arg.ToString(/*oneline=*/false); |
467 | 0 | } |
468 | 0 | left += ","; |
469 | 0 | PushSection({left, arg.ToDescriptionString(/*is_named_arg=*/push_name)}); |
470 | 0 | break; |
471 | 0 | } |
472 | 0 | case RPCArg::Type::OBJ: |
473 | 0 | case RPCArg::Type::OBJ_USER_KEYS: { |
474 | 0 | const auto right = is_top_level_arg ? "" : arg.ToDescriptionString(/*is_named_arg=*/push_name); |
475 | 0 | PushSection({indent + (push_name ? "\"" + arg.GetName() + "\": " : "") + "{", right}); |
476 | 0 | for (const auto& arg_inner : arg.m_inner) { |
477 | 0 | Push(arg_inner, current_indent + 2, OuterType::OBJ); |
478 | 0 | } |
479 | 0 | if (arg.m_type != RPCArg::Type::OBJ) { |
480 | 0 | PushSection({indent_next + "...", ""}); |
481 | 0 | } |
482 | 0 | PushSection({indent + "}" + (is_top_level_arg ? "" : ","), ""}); |
483 | 0 | break; |
484 | 0 | } |
485 | 0 | case RPCArg::Type::ARR: { |
486 | 0 | auto left = indent; |
487 | 0 | left += push_name ? "\"" + arg.GetName() + "\": " : ""; |
488 | 0 | left += "["; |
489 | 0 | const auto right = is_top_level_arg ? "" : arg.ToDescriptionString(/*is_named_arg=*/push_name); |
490 | 0 | PushSection({left, right}); |
491 | 0 | for (const auto& arg_inner : arg.m_inner) { |
492 | 0 | Push(arg_inner, current_indent + 2, OuterType::ARR); |
493 | 0 | } |
494 | 0 | PushSection({indent_next + "...", ""}); |
495 | 0 | PushSection({indent + "]" + (is_top_level_arg ? "" : ","), ""}); |
496 | 0 | break; |
497 | 0 | } |
498 | 0 | } // no default case, so the compiler can warn about missing cases |
499 | 0 | } |
500 | | |
501 | | /** |
502 | | * Concatenate all sections with proper padding |
503 | | */ |
504 | | std::string ToString() const |
505 | 0 | { |
506 | 0 | std::string ret; |
507 | 0 | const size_t pad = m_max_pad + 4; |
508 | 0 | for (const auto& s : m_sections) { |
509 | | // The left part of a section is assumed to be a single line, usually it is the name of the JSON struct or a |
510 | | // brace like {, }, [, or ] |
511 | 0 | CHECK_NONFATAL(s.m_left.find('\n') == std::string::npos);Line | Count | Source | 110 | 0 | inline_check_non_fatal(condition, std::source_location::current(), #condition) |
|
512 | 0 | if (s.m_right.empty()) { |
513 | 0 | ret += s.m_left; |
514 | 0 | ret += "\n"; |
515 | 0 | continue; |
516 | 0 | } |
517 | | |
518 | 0 | std::string left = s.m_left; |
519 | 0 | left.resize(pad, ' '); |
520 | 0 | ret += left; |
521 | | |
522 | | // Properly pad after newlines |
523 | 0 | std::string right; |
524 | 0 | size_t begin = 0; |
525 | 0 | size_t new_line_pos = s.m_right.find_first_of('\n'); |
526 | 0 | while (true) { |
527 | 0 | right += s.m_right.substr(begin, new_line_pos - begin); |
528 | 0 | if (new_line_pos == std::string::npos) { |
529 | 0 | break; //No new line |
530 | 0 | } |
531 | 0 | right += "\n" + std::string(pad, ' '); |
532 | 0 | begin = s.m_right.find_first_not_of(' ', new_line_pos + 1); |
533 | 0 | if (begin == std::string::npos) { |
534 | 0 | break; // Empty line |
535 | 0 | } |
536 | 0 | new_line_pos = s.m_right.find_first_of('\n', begin + 1); |
537 | 0 | } |
538 | 0 | ret += right; |
539 | 0 | ret += "\n"; |
540 | 0 | } |
541 | 0 | return ret; |
542 | 0 | } |
543 | | }; |
544 | | |
545 | | RPCHelpMan::RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples) |
546 | 0 | : RPCHelpMan{std::move(name), std::move(description), std::move(args), std::move(results), std::move(examples), nullptr} {}Unexecuted instantiation: RPCHelpMan::RPCHelpMan(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>, std::__1::vector<RPCArg, std::__1::allocator<RPCArg>>, RPCResults, RPCExamples) Unexecuted instantiation: RPCHelpMan::RPCHelpMan(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>, std::__1::vector<RPCArg, std::__1::allocator<RPCArg>>, RPCResults, RPCExamples) |
547 | | |
548 | | RPCHelpMan::RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples, RPCMethodImpl fun) |
549 | 0 | : m_name{std::move(name)}, |
550 | 0 | m_fun{std::move(fun)}, |
551 | 0 | m_description{std::move(description)}, |
552 | 0 | m_args{std::move(args)}, |
553 | 0 | m_results{std::move(results)}, |
554 | 0 | m_examples{std::move(examples)} |
555 | 0 | { |
556 | | // Map of parameter names and types just used to check whether the names are |
557 | | // unique. Parameter names always need to be unique, with the exception that |
558 | | // there can be pairs of POSITIONAL and NAMED parameters with the same name. |
559 | 0 | enum ParamType { POSITIONAL = 1, NAMED = 2, NAMED_ONLY = 4 }; |
560 | 0 | std::map<std::string, int> param_names; |
561 | |
|
562 | 0 | for (const auto& arg : m_args) { |
563 | 0 | std::vector<std::string> names = SplitString(arg.m_names, '|'); |
564 | | // Should have unique named arguments |
565 | 0 | for (const std::string& name : names) { |
566 | 0 | auto& param_type = param_names[name]; |
567 | 0 | CHECK_NONFATAL(!(param_type & POSITIONAL)); Line | Count | Source | 110 | 0 | inline_check_non_fatal(condition, std::source_location::current(), #condition) |
|
568 | 0 | CHECK_NONFATAL(!(param_type & NAMED_ONLY)); Line | Count | Source | 110 | 0 | inline_check_non_fatal(condition, std::source_location::current(), #condition) |
|
569 | 0 | param_type |= POSITIONAL; |
570 | 0 | } |
571 | 0 | if (arg.m_type == RPCArg::Type::OBJ_NAMED_PARAMS) { |
572 | 0 | for (const auto& inner : arg.m_inner) { |
573 | 0 | std::vector<std::string> inner_names = SplitString(inner.m_names, '|'); |
574 | 0 | for (const std::string& inner_name : inner_names) { |
575 | 0 | auto& param_type = param_names[inner_name]; |
576 | 0 | CHECK_NONFATAL(!(param_type & POSITIONAL) || inner.m_opts.also_positional); Line | Count | Source | 110 | 0 | inline_check_non_fatal(condition, std::source_location::current(), #condition) |
|
577 | 0 | CHECK_NONFATAL(!(param_type & NAMED)); Line | Count | Source | 110 | 0 | inline_check_non_fatal(condition, std::source_location::current(), #condition) |
|
578 | 0 | CHECK_NONFATAL(!(param_type & NAMED_ONLY)); Line | Count | Source | 110 | 0 | inline_check_non_fatal(condition, std::source_location::current(), #condition) |
|
579 | 0 | param_type |= inner.m_opts.also_positional ? NAMED : NAMED_ONLY; |
580 | 0 | } |
581 | 0 | } |
582 | 0 | } |
583 | | // Default value type should match argument type only when defined |
584 | 0 | if (arg.m_fallback.index() == 2) { |
585 | 0 | const RPCArg::Type type = arg.m_type; |
586 | 0 | switch (std::get<RPCArg::Default>(arg.m_fallback).getType()) { |
587 | 0 | case UniValue::VOBJ: |
588 | 0 | CHECK_NONFATAL(type == RPCArg::Type::OBJ); Line | Count | Source | 110 | 0 | inline_check_non_fatal(condition, std::source_location::current(), #condition) |
|
589 | 0 | break; |
590 | 0 | case UniValue::VARR: |
591 | 0 | CHECK_NONFATAL(type == RPCArg::Type::ARR); Line | Count | Source | 110 | 0 | inline_check_non_fatal(condition, std::source_location::current(), #condition) |
|
592 | 0 | break; |
593 | 0 | case UniValue::VSTR: |
594 | 0 | CHECK_NONFATAL(type == RPCArg::Type::STR || type == RPCArg::Type::STR_HEX || type == RPCArg::Type::AMOUNT); Line | Count | Source | 110 | 0 | inline_check_non_fatal(condition, std::source_location::current(), #condition) |
|
595 | 0 | break; |
596 | 0 | case UniValue::VNUM: |
597 | 0 | CHECK_NONFATAL(type == RPCArg::Type::NUM || type == RPCArg::Type::AMOUNT || type == RPCArg::Type::RANGE); Line | Count | Source | 110 | 0 | inline_check_non_fatal(condition, std::source_location::current(), #condition) |
|
598 | 0 | break; |
599 | 0 | case UniValue::VBOOL: |
600 | 0 | CHECK_NONFATAL(type == RPCArg::Type::BOOL); Line | Count | Source | 110 | 0 | inline_check_non_fatal(condition, std::source_location::current(), #condition) |
|
601 | 0 | break; |
602 | 0 | case UniValue::VNULL: |
603 | | // Null values are accepted in all arguments |
604 | 0 | break; |
605 | 0 | default: |
606 | 0 | NONFATAL_UNREACHABLE(); Line | Count | Source | 131 | 0 | throw NonFatalCheckError { "Unreachable code reached (non-fatal)", std::source_location::current() } |
|
607 | 0 | break; |
608 | 0 | } |
609 | 0 | } |
610 | 0 | } |
611 | 0 | } |
612 | | |
613 | | std::string RPCResults::ToDescriptionString() const |
614 | 0 | { |
615 | 0 | std::string result; |
616 | 0 | for (const auto& r : m_results) { |
617 | 0 | if (r.m_type == RPCResult::Type::ANY) continue; // for testing only |
618 | 0 | if (r.m_cond.empty()) { |
619 | 0 | result += "\nResult:\n"; |
620 | 0 | } else { |
621 | 0 | result += "\nResult (" + r.m_cond + "):\n"; |
622 | 0 | } |
623 | 0 | Sections sections; |
624 | 0 | r.ToSections(sections); |
625 | 0 | result += sections.ToString(); |
626 | 0 | } |
627 | 0 | return result; |
628 | 0 | } |
629 | | |
630 | | std::string RPCExamples::ToDescriptionString() const |
631 | 0 | { |
632 | 0 | return m_examples.empty() ? m_examples : "\nExamples:\n" + m_examples; |
633 | 0 | } |
634 | | |
635 | | UniValue RPCHelpMan::HandleRequest(const JSONRPCRequest& request) const |
636 | 0 | { |
637 | 0 | if (request.mode == JSONRPCRequest::GET_ARGS) { |
638 | 0 | return GetArgMap(); |
639 | 0 | } |
640 | | /* |
641 | | * Check if the given request is valid according to this command or if |
642 | | * the user is asking for help information, and throw help when appropriate. |
643 | | */ |
644 | 0 | if (request.mode == JSONRPCRequest::GET_HELP || !IsValidNumArgs(request.params.size())) { |
645 | 0 | throw HelpResult{ToString()}; |
646 | 0 | } |
647 | 0 | UniValue arg_mismatch{UniValue::VOBJ}; |
648 | 0 | for (size_t i{0}; i < m_args.size(); ++i) { |
649 | 0 | const auto& arg{m_args.at(i)}; |
650 | 0 | UniValue match{arg.MatchesType(request.params[i])}; |
651 | 0 | if (!match.isTrue()) { |
652 | 0 | arg_mismatch.pushKV(strprintf("Position %s (%s)", i + 1, arg.m_names), std::move(match));Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
653 | 0 | } |
654 | 0 | } |
655 | 0 | if (!arg_mismatch.empty()) { |
656 | 0 | throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Wrong type passed:\n%s", arg_mismatch.write(4)));Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
657 | 0 | } |
658 | 0 | CHECK_NONFATAL(m_req == nullptr); Line | Count | Source | 110 | 0 | inline_check_non_fatal(condition, std::source_location::current(), #condition) |
|
659 | 0 | m_req = &request; |
660 | 0 | UniValue ret = m_fun(*this, request); |
661 | 0 | m_req = nullptr; |
662 | 0 | if (gArgs.GetBoolArg("-rpcdoccheck", DEFAULT_RPC_DOC_CHECK)) { |
663 | 0 | UniValue mismatch{UniValue::VARR}; |
664 | 0 | for (const auto& res : m_results.m_results) { |
665 | 0 | UniValue match{res.MatchesType(ret)}; |
666 | 0 | if (match.isTrue()) { |
667 | 0 | mismatch.setNull(); |
668 | 0 | break; |
669 | 0 | } |
670 | 0 | mismatch.push_back(std::move(match)); |
671 | 0 | } |
672 | 0 | if (!mismatch.isNull()) { |
673 | 0 | std::string explain{ |
674 | 0 | mismatch.empty() ? "no possible results defined" : |
675 | 0 | mismatch.size() == 1 ? mismatch[0].write(4) : |
676 | 0 | mismatch.write(4)}; |
677 | 0 | throw std::runtime_error{ |
678 | 0 | STR_INTERNAL_BUG(strprintf("RPC call \"%s\" returned incorrect type:\n%s", m_name, explain)),Line | Count | Source | 96 | 0 | #define STR_INTERNAL_BUG(msg) StrFormatInternalBug((msg), std::source_location::current()) |
|
679 | 0 | }; |
680 | 0 | } |
681 | 0 | } |
682 | 0 | return ret; |
683 | 0 | } |
684 | | |
685 | | using CheckFn = void(const RPCArg&); |
686 | | static const UniValue* DetailMaybeArg(CheckFn* check, const std::vector<RPCArg>& params, const JSONRPCRequest* req, size_t i) |
687 | 0 | { |
688 | 0 | CHECK_NONFATAL(i < params.size()); Line | Count | Source | 110 | 0 | inline_check_non_fatal(condition, std::source_location::current(), #condition) |
|
689 | 0 | const UniValue& arg{CHECK_NONFATAL(req)->params[i]};Line | Count | Source | 110 | 0 | inline_check_non_fatal(condition, std::source_location::current(), #condition) |
|
690 | 0 | const RPCArg& param{params.at(i)}; |
691 | 0 | if (check) check(param); |
692 | |
|
693 | 0 | if (!arg.isNull()) return &arg; |
694 | 0 | if (!std::holds_alternative<RPCArg::Default>(param.m_fallback)) return nullptr; |
695 | 0 | return &std::get<RPCArg::Default>(param.m_fallback); |
696 | 0 | } |
697 | | |
698 | | static void CheckRequiredOrDefault(const RPCArg& param) |
699 | 0 | { |
700 | | // Must use `Arg<Type>(key)` to get the argument or its default value. |
701 | 0 | const bool required{ |
702 | 0 | std::holds_alternative<RPCArg::Optional>(param.m_fallback) && RPCArg::Optional::NO == std::get<RPCArg::Optional>(param.m_fallback), |
703 | 0 | }; |
704 | 0 | CHECK_NONFATAL(required || std::holds_alternative<RPCArg::Default>(param.m_fallback)); Line | Count | Source | 110 | 0 | inline_check_non_fatal(condition, std::source_location::current(), #condition) |
|
705 | 0 | } |
706 | | |
707 | | #define TMPL_INST(check_param, ret_type, return_code) \ |
708 | | template <> \ |
709 | | ret_type RPCHelpMan::ArgValue<ret_type>(size_t i) const \ |
710 | 0 | { \ |
711 | 0 | const UniValue* maybe_arg{ \ |
712 | 0 | DetailMaybeArg(check_param, m_args, m_req, i), \ |
713 | 0 | }; \ |
714 | 0 | return return_code \ |
715 | 0 | } \ Unexecuted instantiation: UniValue const* RPCHelpMan::ArgValue<UniValue const*>(unsigned long) const Unexecuted instantiation: std::__1::optional<double> RPCHelpMan::ArgValue<std::__1::optional<double>>(unsigned long) const Unexecuted instantiation: std::__1::optional<bool> RPCHelpMan::ArgValue<std::__1::optional<bool>>(unsigned long) const Unexecuted instantiation: std::__1::optional<long long> RPCHelpMan::ArgValue<std::__1::optional<long long>>(unsigned long) const Unexecuted instantiation: std::__1::optional<std::__1::basic_string_view<char, std::__1::char_traits<char>>> RPCHelpMan::ArgValue<std::__1::optional<std::__1::basic_string_view<char, std::__1::char_traits<char>>>>(unsigned long) const Unexecuted instantiation: UniValue const& RPCHelpMan::ArgValue<UniValue const&>(unsigned long) const Unexecuted instantiation: bool RPCHelpMan::ArgValue<bool>(unsigned long) const Unexecuted instantiation: int RPCHelpMan::ArgValue<int>(unsigned long) const Unexecuted instantiation: unsigned long long RPCHelpMan::ArgValue<unsigned long long>(unsigned long) const Unexecuted instantiation: unsigned int RPCHelpMan::ArgValue<unsigned int>(unsigned long) const Unexecuted instantiation: std::__1::basic_string_view<char, std::__1::char_traits<char>> RPCHelpMan::ArgValue<std::__1::basic_string_view<char, std::__1::char_traits<char>>>(unsigned long) const |
716 | | void force_semicolon(ret_type) |
717 | | |
718 | | // Optional arg (without default). Can also be called on required args, if needed. |
719 | | TMPL_INST(nullptr, const UniValue*, maybe_arg;); |
720 | | TMPL_INST(nullptr, std::optional<double>, maybe_arg ? std::optional{maybe_arg->get_real()} : std::nullopt;); |
721 | | TMPL_INST(nullptr, std::optional<bool>, maybe_arg ? std::optional{maybe_arg->get_bool()} : std::nullopt;); |
722 | | TMPL_INST(nullptr, std::optional<int64_t>, maybe_arg ? std::optional{maybe_arg->getInt<int64_t>()} : std::nullopt;); |
723 | | TMPL_INST(nullptr, std::optional<std::string_view>, maybe_arg ? std::optional<std::string_view>{maybe_arg->get_str()} : std::nullopt;); |
724 | | |
725 | | // Required arg or optional arg with default value. |
726 | | TMPL_INST(CheckRequiredOrDefault, const UniValue&, *CHECK_NONFATAL(maybe_arg);); |
727 | | TMPL_INST(CheckRequiredOrDefault, bool, CHECK_NONFATAL(maybe_arg)->get_bool();); |
728 | | TMPL_INST(CheckRequiredOrDefault, int, CHECK_NONFATAL(maybe_arg)->getInt<int>();); |
729 | | TMPL_INST(CheckRequiredOrDefault, uint64_t, CHECK_NONFATAL(maybe_arg)->getInt<uint64_t>();); |
730 | | TMPL_INST(CheckRequiredOrDefault, uint32_t, CHECK_NONFATAL(maybe_arg)->getInt<uint32_t>();); |
731 | | TMPL_INST(CheckRequiredOrDefault, std::string_view, CHECK_NONFATAL(maybe_arg)->get_str();); |
732 | | |
733 | | bool RPCHelpMan::IsValidNumArgs(size_t num_args) const |
734 | 0 | { |
735 | 0 | size_t num_required_args = 0; |
736 | 0 | for (size_t n = m_args.size(); n > 0; --n) { |
737 | 0 | if (!m_args.at(n - 1).IsOptional()) { |
738 | 0 | num_required_args = n; |
739 | 0 | break; |
740 | 0 | } |
741 | 0 | } |
742 | 0 | return num_required_args <= num_args && num_args <= m_args.size(); |
743 | 0 | } |
744 | | |
745 | | std::vector<std::pair<std::string, bool>> RPCHelpMan::GetArgNames() const |
746 | 0 | { |
747 | 0 | std::vector<std::pair<std::string, bool>> ret; |
748 | 0 | ret.reserve(m_args.size()); |
749 | 0 | for (const auto& arg : m_args) { |
750 | 0 | if (arg.m_type == RPCArg::Type::OBJ_NAMED_PARAMS) { |
751 | 0 | for (const auto& inner : arg.m_inner) { |
752 | 0 | ret.emplace_back(inner.m_names, /*named_only=*/true); |
753 | 0 | } |
754 | 0 | } |
755 | 0 | ret.emplace_back(arg.m_names, /*named_only=*/false); |
756 | 0 | } |
757 | 0 | return ret; |
758 | 0 | } |
759 | | |
760 | | size_t RPCHelpMan::GetParamIndex(std::string_view key) const |
761 | 0 | { |
762 | 0 | auto it{std::find_if( |
763 | 0 | m_args.begin(), m_args.end(), [&key](const auto& arg) { return arg.GetName() == key;} |
764 | 0 | )}; |
765 | |
|
766 | 0 | CHECK_NONFATAL(it != m_args.end()); // TODO: ideally this is checked at compile time Line | Count | Source | 110 | 0 | inline_check_non_fatal(condition, std::source_location::current(), #condition) |
|
767 | 0 | return std::distance(m_args.begin(), it); |
768 | 0 | } |
769 | | |
770 | | std::string RPCHelpMan::ToString() const |
771 | 0 | { |
772 | 0 | std::string ret; |
773 | | |
774 | | // Oneline summary |
775 | 0 | ret += m_name; |
776 | 0 | bool was_optional{false}; |
777 | 0 | for (const auto& arg : m_args) { |
778 | 0 | if (arg.m_opts.hidden) break; // Any arg that follows is also hidden |
779 | 0 | const bool optional = arg.IsOptional(); |
780 | 0 | ret += " "; |
781 | 0 | if (optional) { |
782 | 0 | if (!was_optional) ret += "( "; |
783 | 0 | was_optional = true; |
784 | 0 | } else { |
785 | 0 | if (was_optional) ret += ") "; |
786 | 0 | was_optional = false; |
787 | 0 | } |
788 | 0 | ret += arg.ToString(/*oneline=*/true); |
789 | 0 | } |
790 | 0 | if (was_optional) ret += " )"; |
791 | | |
792 | | // Description |
793 | 0 | CHECK_NONFATAL(!m_description.starts_with('\n')); // Historically \n was required, but reject it for new code.Line | Count | Source | 110 | 0 | inline_check_non_fatal(condition, std::source_location::current(), #condition) |
|
794 | 0 | ret += "\n\n" + TrimString(m_description) + "\n"; |
795 | | |
796 | | // Arguments |
797 | 0 | Sections sections; |
798 | 0 | Sections named_only_sections; |
799 | 0 | for (size_t i{0}; i < m_args.size(); ++i) { |
800 | 0 | const auto& arg = m_args.at(i); |
801 | 0 | if (arg.m_opts.hidden) break; // Any arg that follows is also hidden |
802 | | |
803 | | // Push named argument name and description |
804 | 0 | sections.m_sections.emplace_back(util::ToString(i + 1) + ". " + arg.GetFirstName(), arg.ToDescriptionString(/*is_named_arg=*/true)); |
805 | 0 | sections.m_max_pad = std::max(sections.m_max_pad, sections.m_sections.back().m_left.size()); |
806 | | |
807 | | // Recursively push nested args |
808 | 0 | sections.Push(arg); |
809 | | |
810 | | // Push named-only argument sections |
811 | 0 | if (arg.m_type == RPCArg::Type::OBJ_NAMED_PARAMS) { |
812 | 0 | for (const auto& arg_inner : arg.m_inner) { |
813 | 0 | named_only_sections.PushSection({arg_inner.GetFirstName(), arg_inner.ToDescriptionString(/*is_named_arg=*/true)}); |
814 | 0 | named_only_sections.Push(arg_inner); |
815 | 0 | } |
816 | 0 | } |
817 | 0 | } |
818 | |
|
819 | 0 | if (!sections.m_sections.empty()) ret += "\nArguments:\n"; |
820 | 0 | ret += sections.ToString(); |
821 | 0 | if (!named_only_sections.m_sections.empty()) ret += "\nNamed Arguments:\n"; |
822 | 0 | ret += named_only_sections.ToString(); |
823 | | |
824 | | // Result |
825 | 0 | ret += m_results.ToDescriptionString(); |
826 | | |
827 | | // Examples |
828 | 0 | ret += m_examples.ToDescriptionString(); |
829 | |
|
830 | 0 | return ret; |
831 | 0 | } |
832 | | |
833 | | UniValue RPCHelpMan::GetArgMap() const |
834 | 0 | { |
835 | 0 | UniValue arr{UniValue::VARR}; |
836 | |
|
837 | 0 | auto push_back_arg_info = [&arr](const std::string& rpc_name, int pos, const std::string& arg_name, const RPCArg::Type& type) { |
838 | 0 | UniValue map{UniValue::VARR}; |
839 | 0 | map.push_back(rpc_name); |
840 | 0 | map.push_back(pos); |
841 | 0 | map.push_back(arg_name); |
842 | 0 | map.push_back(type == RPCArg::Type::STR || |
843 | 0 | type == RPCArg::Type::STR_HEX); |
844 | 0 | arr.push_back(std::move(map)); |
845 | 0 | }; |
846 | |
|
847 | 0 | for (int i{0}; i < int(m_args.size()); ++i) { |
848 | 0 | const auto& arg = m_args.at(i); |
849 | 0 | std::vector<std::string> arg_names = SplitString(arg.m_names, '|'); |
850 | 0 | for (const auto& arg_name : arg_names) { |
851 | 0 | push_back_arg_info(m_name, i, arg_name, arg.m_type); |
852 | 0 | if (arg.m_type == RPCArg::Type::OBJ_NAMED_PARAMS) { |
853 | 0 | for (const auto& inner : arg.m_inner) { |
854 | 0 | std::vector<std::string> inner_names = SplitString(inner.m_names, '|'); |
855 | 0 | for (const std::string& inner_name : inner_names) { |
856 | 0 | push_back_arg_info(m_name, i, inner_name, inner.m_type); |
857 | 0 | } |
858 | 0 | } |
859 | 0 | } |
860 | 0 | } |
861 | 0 | } |
862 | 0 | return arr; |
863 | 0 | } |
864 | | |
865 | | static std::optional<UniValue::VType> ExpectedType(RPCArg::Type type) |
866 | 0 | { |
867 | 0 | using Type = RPCArg::Type; |
868 | 0 | switch (type) { |
869 | 0 | case Type::STR_HEX: |
870 | 0 | case Type::STR: { |
871 | 0 | return UniValue::VSTR; |
872 | 0 | } |
873 | 0 | case Type::NUM: { |
874 | 0 | return UniValue::VNUM; |
875 | 0 | } |
876 | 0 | case Type::AMOUNT: { |
877 | | // VNUM or VSTR, checked inside AmountFromValue() |
878 | 0 | return std::nullopt; |
879 | 0 | } |
880 | 0 | case Type::RANGE: { |
881 | | // VNUM or VARR, checked inside ParseRange() |
882 | 0 | return std::nullopt; |
883 | 0 | } |
884 | 0 | case Type::BOOL: { |
885 | 0 | return UniValue::VBOOL; |
886 | 0 | } |
887 | 0 | case Type::OBJ: |
888 | 0 | case Type::OBJ_NAMED_PARAMS: |
889 | 0 | case Type::OBJ_USER_KEYS: { |
890 | 0 | return UniValue::VOBJ; |
891 | 0 | } |
892 | 0 | case Type::ARR: { |
893 | 0 | return UniValue::VARR; |
894 | 0 | } |
895 | 0 | } // no default case, so the compiler can warn about missing cases |
896 | 0 | NONFATAL_UNREACHABLE(); Line | Count | Source | 131 | 0 | throw NonFatalCheckError { "Unreachable code reached (non-fatal)", std::source_location::current() } |
|
897 | 0 | } |
898 | | |
899 | | UniValue RPCArg::MatchesType(const UniValue& request) const |
900 | 0 | { |
901 | 0 | if (m_opts.skip_type_check) return true; |
902 | 0 | if (IsOptional() && request.isNull()) return true; |
903 | 0 | const auto exp_type{ExpectedType(m_type)}; |
904 | 0 | if (!exp_type) return true; // nothing to check |
905 | | |
906 | 0 | if (*exp_type != request.getType()) { |
907 | 0 | return strprintf("JSON value of type %s is not of expected type %s", uvTypeName(request.getType()), uvTypeName(*exp_type));Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
908 | 0 | } |
909 | 0 | return true; |
910 | 0 | } |
911 | | |
912 | | std::string RPCArg::GetFirstName() const |
913 | 0 | { |
914 | 0 | return m_names.substr(0, m_names.find('|')); |
915 | 0 | } |
916 | | |
917 | | std::string RPCArg::GetName() const |
918 | 0 | { |
919 | 0 | CHECK_NONFATAL(std::string::npos == m_names.find('|'));Line | Count | Source | 110 | 0 | inline_check_non_fatal(condition, std::source_location::current(), #condition) |
|
920 | 0 | return m_names; |
921 | 0 | } |
922 | | |
923 | | bool RPCArg::IsOptional() const |
924 | 0 | { |
925 | 0 | if (m_fallback.index() != 0) { |
926 | 0 | return true; |
927 | 0 | } else { |
928 | 0 | return RPCArg::Optional::NO != std::get<RPCArg::Optional>(m_fallback); |
929 | 0 | } |
930 | 0 | } |
931 | | |
932 | | std::string RPCArg::ToDescriptionString(bool is_named_arg) const |
933 | 0 | { |
934 | 0 | std::string ret; |
935 | 0 | ret += "("; |
936 | 0 | if (m_opts.type_str.size() != 0) { |
937 | 0 | ret += m_opts.type_str.at(1); |
938 | 0 | } else { |
939 | 0 | switch (m_type) { |
940 | 0 | case Type::STR_HEX: |
941 | 0 | case Type::STR: { |
942 | 0 | ret += "string"; |
943 | 0 | break; |
944 | 0 | } |
945 | 0 | case Type::NUM: { |
946 | 0 | ret += "numeric"; |
947 | 0 | break; |
948 | 0 | } |
949 | 0 | case Type::AMOUNT: { |
950 | 0 | ret += "numeric or string"; |
951 | 0 | break; |
952 | 0 | } |
953 | 0 | case Type::RANGE: { |
954 | 0 | ret += "numeric or array"; |
955 | 0 | break; |
956 | 0 | } |
957 | 0 | case Type::BOOL: { |
958 | 0 | ret += "boolean"; |
959 | 0 | break; |
960 | 0 | } |
961 | 0 | case Type::OBJ: |
962 | 0 | case Type::OBJ_NAMED_PARAMS: |
963 | 0 | case Type::OBJ_USER_KEYS: { |
964 | 0 | ret += "json object"; |
965 | 0 | break; |
966 | 0 | } |
967 | 0 | case Type::ARR: { |
968 | 0 | ret += "json array"; |
969 | 0 | break; |
970 | 0 | } |
971 | 0 | } // no default case, so the compiler can warn about missing cases |
972 | 0 | } |
973 | 0 | if (m_fallback.index() == 1) { |
974 | 0 | ret += ", optional, default=" + std::get<RPCArg::DefaultHint>(m_fallback); |
975 | 0 | } else if (m_fallback.index() == 2) { |
976 | 0 | ret += ", optional, default=" + std::get<RPCArg::Default>(m_fallback).write(); |
977 | 0 | } else { |
978 | 0 | switch (std::get<RPCArg::Optional>(m_fallback)) { |
979 | 0 | case RPCArg::Optional::OMITTED: { |
980 | 0 | if (is_named_arg) ret += ", optional"; // Default value is "null" in dicts. Otherwise, |
981 | | // nothing to do. Element is treated as if not present and has no default value |
982 | 0 | break; |
983 | 0 | } |
984 | 0 | case RPCArg::Optional::NO: { |
985 | 0 | ret += ", required"; |
986 | 0 | break; |
987 | 0 | } |
988 | 0 | } // no default case, so the compiler can warn about missing cases |
989 | 0 | } |
990 | 0 | ret += ")"; |
991 | 0 | if (m_type == Type::OBJ_NAMED_PARAMS) ret += " Options object that can be used to pass named arguments, listed below."; |
992 | 0 | ret += m_description.empty() ? "" : " " + m_description; |
993 | 0 | return ret; |
994 | 0 | } |
995 | | |
996 | | // NOLINTNEXTLINE(misc-no-recursion) |
997 | | void RPCResult::ToSections(Sections& sections, const OuterType outer_type, const int current_indent) const |
998 | 0 | { |
999 | | // Indentation |
1000 | 0 | const std::string indent(current_indent, ' '); |
1001 | 0 | const std::string indent_next(current_indent + 2, ' '); |
1002 | | |
1003 | | // Elements in a JSON structure (dictionary or array) are separated by a comma |
1004 | 0 | const std::string maybe_separator{outer_type != OuterType::NONE ? "," : ""}; |
1005 | | |
1006 | | // The key name if recursed into a dictionary |
1007 | 0 | const std::string maybe_key{ |
1008 | 0 | outer_type == OuterType::OBJ ? |
1009 | 0 | "\"" + this->m_key_name + "\" : " : |
1010 | 0 | ""}; |
1011 | | |
1012 | | // Format description with type |
1013 | 0 | const auto Description = [&](const std::string& type) { |
1014 | 0 | return "(" + type + (this->m_optional ? ", optional" : "") + ")" + |
1015 | 0 | (this->m_description.empty() ? "" : " " + this->m_description); |
1016 | 0 | }; |
1017 | |
|
1018 | 0 | switch (m_type) { |
1019 | 0 | case Type::ELISION: { |
1020 | | // If the inner result is empty, use three dots for elision |
1021 | 0 | sections.PushSection({indent + "..." + maybe_separator, m_description}); |
1022 | 0 | return; |
1023 | 0 | } |
1024 | 0 | case Type::ANY: { |
1025 | 0 | NONFATAL_UNREACHABLE(); // Only for testing Line | Count | Source | 131 | 0 | throw NonFatalCheckError { "Unreachable code reached (non-fatal)", std::source_location::current() } |
|
1026 | 0 | } |
1027 | 0 | case Type::NONE: { |
1028 | 0 | sections.PushSection({indent + "null" + maybe_separator, Description("json null")}); |
1029 | 0 | return; |
1030 | 0 | } |
1031 | 0 | case Type::STR: { |
1032 | 0 | sections.PushSection({indent + maybe_key + "\"str\"" + maybe_separator, Description("string")}); |
1033 | 0 | return; |
1034 | 0 | } |
1035 | 0 | case Type::STR_AMOUNT: { |
1036 | 0 | sections.PushSection({indent + maybe_key + "n" + maybe_separator, Description("numeric")}); |
1037 | 0 | return; |
1038 | 0 | } |
1039 | 0 | case Type::STR_HEX: { |
1040 | 0 | sections.PushSection({indent + maybe_key + "\"hex\"" + maybe_separator, Description("string")}); |
1041 | 0 | return; |
1042 | 0 | } |
1043 | 0 | case Type::NUM: { |
1044 | 0 | sections.PushSection({indent + maybe_key + "n" + maybe_separator, Description("numeric")}); |
1045 | 0 | return; |
1046 | 0 | } |
1047 | 0 | case Type::NUM_TIME: { |
1048 | 0 | sections.PushSection({indent + maybe_key + "xxx" + maybe_separator, Description("numeric")}); |
1049 | 0 | return; |
1050 | 0 | } |
1051 | 0 | case Type::BOOL: { |
1052 | 0 | sections.PushSection({indent + maybe_key + "true|false" + maybe_separator, Description("boolean")}); |
1053 | 0 | return; |
1054 | 0 | } |
1055 | 0 | case Type::ARR_FIXED: |
1056 | 0 | case Type::ARR: { |
1057 | 0 | sections.PushSection({indent + maybe_key + "[", Description("json array")}); |
1058 | 0 | for (const auto& i : m_inner) { |
1059 | 0 | i.ToSections(sections, OuterType::ARR, current_indent + 2); |
1060 | 0 | } |
1061 | 0 | CHECK_NONFATAL(!m_inner.empty()); Line | Count | Source | 110 | 0 | inline_check_non_fatal(condition, std::source_location::current(), #condition) |
|
1062 | 0 | if (m_type == Type::ARR && m_inner.back().m_type != Type::ELISION) { |
1063 | 0 | sections.PushSection({indent_next + "...", ""}); |
1064 | 0 | } else { |
1065 | | // Remove final comma, which would be invalid JSON |
1066 | 0 | sections.m_sections.back().m_left.pop_back(); |
1067 | 0 | } |
1068 | 0 | sections.PushSection({indent + "]" + maybe_separator, ""}); |
1069 | 0 | return; |
1070 | 0 | } |
1071 | 0 | case Type::OBJ_DYN: |
1072 | 0 | case Type::OBJ: { |
1073 | 0 | if (m_inner.empty()) { |
1074 | 0 | sections.PushSection({indent + maybe_key + "{}", Description("empty JSON object")}); |
1075 | 0 | return; |
1076 | 0 | } |
1077 | 0 | sections.PushSection({indent + maybe_key + "{", Description("json object")}); |
1078 | 0 | for (const auto& i : m_inner) { |
1079 | 0 | i.ToSections(sections, OuterType::OBJ, current_indent + 2); |
1080 | 0 | } |
1081 | 0 | if (m_type == Type::OBJ_DYN && m_inner.back().m_type != Type::ELISION) { |
1082 | | // If the dictionary keys are dynamic, use three dots for continuation |
1083 | 0 | sections.PushSection({indent_next + "...", ""}); |
1084 | 0 | } else { |
1085 | | // Remove final comma, which would be invalid JSON |
1086 | 0 | sections.m_sections.back().m_left.pop_back(); |
1087 | 0 | } |
1088 | 0 | sections.PushSection({indent + "}" + maybe_separator, ""}); |
1089 | 0 | return; |
1090 | 0 | } |
1091 | 0 | } // no default case, so the compiler can warn about missing cases |
1092 | 0 | NONFATAL_UNREACHABLE(); Line | Count | Source | 131 | 0 | throw NonFatalCheckError { "Unreachable code reached (non-fatal)", std::source_location::current() } |
|
1093 | 0 | } |
1094 | | |
1095 | | static std::optional<UniValue::VType> ExpectedType(RPCResult::Type type) |
1096 | 0 | { |
1097 | 0 | using Type = RPCResult::Type; |
1098 | 0 | switch (type) { |
1099 | 0 | case Type::ELISION: |
1100 | 0 | case Type::ANY: { |
1101 | 0 | return std::nullopt; |
1102 | 0 | } |
1103 | 0 | case Type::NONE: { |
1104 | 0 | return UniValue::VNULL; |
1105 | 0 | } |
1106 | 0 | case Type::STR: |
1107 | 0 | case Type::STR_HEX: { |
1108 | 0 | return UniValue::VSTR; |
1109 | 0 | } |
1110 | 0 | case Type::NUM: |
1111 | 0 | case Type::STR_AMOUNT: |
1112 | 0 | case Type::NUM_TIME: { |
1113 | 0 | return UniValue::VNUM; |
1114 | 0 | } |
1115 | 0 | case Type::BOOL: { |
1116 | 0 | return UniValue::VBOOL; |
1117 | 0 | } |
1118 | 0 | case Type::ARR_FIXED: |
1119 | 0 | case Type::ARR: { |
1120 | 0 | return UniValue::VARR; |
1121 | 0 | } |
1122 | 0 | case Type::OBJ_DYN: |
1123 | 0 | case Type::OBJ: { |
1124 | 0 | return UniValue::VOBJ; |
1125 | 0 | } |
1126 | 0 | } // no default case, so the compiler can warn about missing cases |
1127 | 0 | NONFATAL_UNREACHABLE(); Line | Count | Source | 131 | 0 | throw NonFatalCheckError { "Unreachable code reached (non-fatal)", std::source_location::current() } |
|
1128 | 0 | } |
1129 | | |
1130 | | // NOLINTNEXTLINE(misc-no-recursion) |
1131 | | UniValue RPCResult::MatchesType(const UniValue& result) const |
1132 | 0 | { |
1133 | 0 | if (m_skip_type_check) { |
1134 | 0 | return true; |
1135 | 0 | } |
1136 | | |
1137 | 0 | const auto exp_type = ExpectedType(m_type); |
1138 | 0 | if (!exp_type) return true; // can be any type, so nothing to check |
1139 | | |
1140 | 0 | if (*exp_type != result.getType()) { |
1141 | 0 | return strprintf("returned type is %s, but declared as %s in doc", uvTypeName(result.getType()), uvTypeName(*exp_type));Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
1142 | 0 | } |
1143 | | |
1144 | 0 | if (UniValue::VARR == result.getType()) { |
1145 | 0 | UniValue errors(UniValue::VOBJ); |
1146 | 0 | for (size_t i{0}; i < result.get_array().size(); ++i) { |
1147 | | // If there are more results than documented, reuse the last doc_inner. |
1148 | 0 | const RPCResult& doc_inner{m_inner.at(std::min(m_inner.size() - 1, i))}; |
1149 | 0 | UniValue match{doc_inner.MatchesType(result.get_array()[i])}; |
1150 | 0 | if (!match.isTrue()) errors.pushKV(strprintf("%d", i), std::move(match));Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
1151 | 0 | } |
1152 | 0 | if (errors.empty()) return true; // empty result array is valid |
1153 | 0 | return errors; |
1154 | 0 | } |
1155 | | |
1156 | 0 | if (UniValue::VOBJ == result.getType()) { |
1157 | 0 | if (!m_inner.empty() && m_inner.at(0).m_type == Type::ELISION) return true; |
1158 | 0 | UniValue errors(UniValue::VOBJ); |
1159 | 0 | if (m_type == Type::OBJ_DYN) { |
1160 | 0 | const RPCResult& doc_inner{m_inner.at(0)}; // Assume all types are the same, randomly pick the first |
1161 | 0 | for (size_t i{0}; i < result.get_obj().size(); ++i) { |
1162 | 0 | UniValue match{doc_inner.MatchesType(result.get_obj()[i])}; |
1163 | 0 | if (!match.isTrue()) errors.pushKV(result.getKeys()[i], std::move(match)); |
1164 | 0 | } |
1165 | 0 | if (errors.empty()) return true; // empty result obj is valid |
1166 | 0 | return errors; |
1167 | 0 | } |
1168 | 0 | std::set<std::string> doc_keys; |
1169 | 0 | for (const auto& doc_entry : m_inner) { |
1170 | 0 | doc_keys.insert(doc_entry.m_key_name); |
1171 | 0 | } |
1172 | 0 | std::map<std::string, UniValue> result_obj; |
1173 | 0 | result.getObjMap(result_obj); |
1174 | 0 | for (const auto& result_entry : result_obj) { |
1175 | 0 | if (doc_keys.find(result_entry.first) == doc_keys.end()) { |
1176 | 0 | errors.pushKV(result_entry.first, "key returned that was not in doc"); |
1177 | 0 | } |
1178 | 0 | } |
1179 | |
|
1180 | 0 | for (const auto& doc_entry : m_inner) { |
1181 | 0 | const auto result_it{result_obj.find(doc_entry.m_key_name)}; |
1182 | 0 | if (result_it == result_obj.end()) { |
1183 | 0 | if (!doc_entry.m_optional) { |
1184 | 0 | errors.pushKV(doc_entry.m_key_name, "key missing, despite not being optional in doc"); |
1185 | 0 | } |
1186 | 0 | continue; |
1187 | 0 | } |
1188 | 0 | UniValue match{doc_entry.MatchesType(result_it->second)}; |
1189 | 0 | if (!match.isTrue()) errors.pushKV(doc_entry.m_key_name, std::move(match)); |
1190 | 0 | } |
1191 | 0 | if (errors.empty()) return true; |
1192 | 0 | return errors; |
1193 | 0 | } |
1194 | | |
1195 | 0 | return true; |
1196 | 0 | } |
1197 | | |
1198 | | void RPCResult::CheckInnerDoc() const |
1199 | 0 | { |
1200 | 0 | if (m_type == Type::OBJ) { |
1201 | | // May or may not be empty |
1202 | 0 | return; |
1203 | 0 | } |
1204 | | // Everything else must either be empty or not |
1205 | 0 | const bool inner_needed{m_type == Type::ARR || m_type == Type::ARR_FIXED || m_type == Type::OBJ_DYN}; |
1206 | 0 | CHECK_NONFATAL(inner_needed != m_inner.empty()); Line | Count | Source | 110 | 0 | inline_check_non_fatal(condition, std::source_location::current(), #condition) |
|
1207 | 0 | } |
1208 | | |
1209 | | // NOLINTNEXTLINE(misc-no-recursion) |
1210 | | std::string RPCArg::ToStringObj(const bool oneline) const |
1211 | 0 | { |
1212 | 0 | std::string res; |
1213 | 0 | res += "\""; |
1214 | 0 | res += GetFirstName(); |
1215 | 0 | if (oneline) { |
1216 | 0 | res += "\":"; |
1217 | 0 | } else { |
1218 | 0 | res += "\": "; |
1219 | 0 | } |
1220 | 0 | switch (m_type) { |
1221 | 0 | case Type::STR: |
1222 | 0 | return res + "\"str\""; |
1223 | 0 | case Type::STR_HEX: |
1224 | 0 | return res + "\"hex\""; |
1225 | 0 | case Type::NUM: |
1226 | 0 | return res + "n"; |
1227 | 0 | case Type::RANGE: |
1228 | 0 | return res + "n or [n,n]"; |
1229 | 0 | case Type::AMOUNT: |
1230 | 0 | return res + "amount"; |
1231 | 0 | case Type::BOOL: |
1232 | 0 | return res + "bool"; |
1233 | 0 | case Type::ARR: |
1234 | 0 | res += "["; |
1235 | 0 | for (const auto& i : m_inner) { |
1236 | 0 | res += i.ToString(oneline) + ","; |
1237 | 0 | } |
1238 | 0 | return res + "...]"; |
1239 | 0 | case Type::OBJ: |
1240 | 0 | case Type::OBJ_NAMED_PARAMS: |
1241 | 0 | case Type::OBJ_USER_KEYS: |
1242 | | // Currently unused, so avoid writing dead code |
1243 | 0 | NONFATAL_UNREACHABLE(); Line | Count | Source | 131 | 0 | throw NonFatalCheckError { "Unreachable code reached (non-fatal)", std::source_location::current() } |
|
1244 | 0 | } // no default case, so the compiler can warn about missing cases |
1245 | 0 | NONFATAL_UNREACHABLE(); Line | Count | Source | 131 | 0 | throw NonFatalCheckError { "Unreachable code reached (non-fatal)", std::source_location::current() } |
|
1246 | 0 | } |
1247 | | |
1248 | | // NOLINTNEXTLINE(misc-no-recursion) |
1249 | | std::string RPCArg::ToString(const bool oneline) const |
1250 | 0 | { |
1251 | 0 | if (oneline && !m_opts.oneline_description.empty()) { |
1252 | 0 | if (m_opts.oneline_description[0] == '\"' && m_type != Type::STR_HEX && m_type != Type::STR && gArgs.GetBoolArg("-rpcdoccheck", DEFAULT_RPC_DOC_CHECK)) { |
1253 | 0 | throw std::runtime_error{ |
1254 | 0 | STR_INTERNAL_BUG(strprintf("non-string RPC arg \"%s\" quotes oneline_description:\n%s",Line | Count | Source | 96 | 0 | #define STR_INTERNAL_BUG(msg) StrFormatInternalBug((msg), std::source_location::current()) |
|
1255 | 0 | m_names, m_opts.oneline_description) |
1256 | 0 | )}; |
1257 | 0 | } |
1258 | 0 | return m_opts.oneline_description; |
1259 | 0 | } |
1260 | | |
1261 | 0 | switch (m_type) { |
1262 | 0 | case Type::STR_HEX: |
1263 | 0 | case Type::STR: { |
1264 | 0 | return "\"" + GetFirstName() + "\""; |
1265 | 0 | } |
1266 | 0 | case Type::NUM: |
1267 | 0 | case Type::RANGE: |
1268 | 0 | case Type::AMOUNT: |
1269 | 0 | case Type::BOOL: { |
1270 | 0 | return GetFirstName(); |
1271 | 0 | } |
1272 | 0 | case Type::OBJ: |
1273 | 0 | case Type::OBJ_NAMED_PARAMS: |
1274 | 0 | case Type::OBJ_USER_KEYS: { |
1275 | | // NOLINTNEXTLINE(misc-no-recursion) |
1276 | 0 | const std::string res = Join(m_inner, ",", [&](const RPCArg& i) { return i.ToStringObj(oneline); }); |
1277 | 0 | if (m_type == Type::OBJ) { |
1278 | 0 | return "{" + res + "}"; |
1279 | 0 | } else { |
1280 | 0 | return "{" + res + ",...}"; |
1281 | 0 | } |
1282 | 0 | } |
1283 | 0 | case Type::ARR: { |
1284 | 0 | std::string res; |
1285 | 0 | for (const auto& i : m_inner) { |
1286 | 0 | res += i.ToString(oneline) + ","; |
1287 | 0 | } |
1288 | 0 | return "[" + res + "...]"; |
1289 | 0 | } |
1290 | 0 | } // no default case, so the compiler can warn about missing cases |
1291 | 0 | NONFATAL_UNREACHABLE(); Line | Count | Source | 131 | 0 | throw NonFatalCheckError { "Unreachable code reached (non-fatal)", std::source_location::current() } |
|
1292 | 0 | } |
1293 | | |
1294 | | static std::pair<int64_t, int64_t> ParseRange(const UniValue& value) |
1295 | 0 | { |
1296 | 0 | if (value.isNum()) { |
1297 | 0 | return {0, value.getInt<int64_t>()}; |
1298 | 0 | } |
1299 | 0 | if (value.isArray() && value.size() == 2 && value[0].isNum() && value[1].isNum()) { |
1300 | 0 | int64_t low = value[0].getInt<int64_t>(); |
1301 | 0 | int64_t high = value[1].getInt<int64_t>(); |
1302 | 0 | if (low > high) throw JSONRPCError(RPC_INVALID_PARAMETER, "Range specified as [begin,end] must not have begin after end"); |
1303 | 0 | return {low, high}; |
1304 | 0 | } |
1305 | 0 | throw JSONRPCError(RPC_INVALID_PARAMETER, "Range must be specified as end or as [begin,end]"); |
1306 | 0 | } |
1307 | | |
1308 | | std::pair<int64_t, int64_t> ParseDescriptorRange(const UniValue& value) |
1309 | 0 | { |
1310 | 0 | int64_t low, high; |
1311 | 0 | std::tie(low, high) = ParseRange(value); |
1312 | 0 | if (low < 0) { |
1313 | 0 | throw JSONRPCError(RPC_INVALID_PARAMETER, "Range should be greater or equal than 0"); |
1314 | 0 | } |
1315 | 0 | if ((high >> 31) != 0) { |
1316 | 0 | throw JSONRPCError(RPC_INVALID_PARAMETER, "End of range is too high"); |
1317 | 0 | } |
1318 | 0 | if (high >= low + 1000000) { |
1319 | 0 | throw JSONRPCError(RPC_INVALID_PARAMETER, "Range is too large"); |
1320 | 0 | } |
1321 | 0 | return {low, high}; |
1322 | 0 | } |
1323 | | |
1324 | | std::vector<CScript> EvalDescriptorStringOrObject(const UniValue& scanobject, FlatSigningProvider& provider, const bool expand_priv) |
1325 | 0 | { |
1326 | 0 | std::string desc_str; |
1327 | 0 | std::pair<int64_t, int64_t> range = {0, 1000}; |
1328 | 0 | if (scanobject.isStr()) { |
1329 | 0 | desc_str = scanobject.get_str(); |
1330 | 0 | } else if (scanobject.isObject()) { |
1331 | 0 | const UniValue& desc_uni{scanobject.find_value("desc")}; |
1332 | 0 | if (desc_uni.isNull()) throw JSONRPCError(RPC_INVALID_PARAMETER, "Descriptor needs to be provided in scan object"); |
1333 | 0 | desc_str = desc_uni.get_str(); |
1334 | 0 | const UniValue& range_uni{scanobject.find_value("range")}; |
1335 | 0 | if (!range_uni.isNull()) { |
1336 | 0 | range = ParseDescriptorRange(range_uni); |
1337 | 0 | } |
1338 | 0 | } else { |
1339 | 0 | throw JSONRPCError(RPC_INVALID_PARAMETER, "Scan object needs to be either a string or an object"); |
1340 | 0 | } |
1341 | | |
1342 | 0 | std::string error; |
1343 | 0 | auto descs = Parse(desc_str, provider, error); |
1344 | 0 | if (descs.empty()) { |
1345 | 0 | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error); |
1346 | 0 | } |
1347 | 0 | if (!descs.at(0)->IsRange()) { |
1348 | 0 | range.first = 0; |
1349 | 0 | range.second = 0; |
1350 | 0 | } |
1351 | 0 | std::vector<CScript> ret; |
1352 | 0 | for (int i = range.first; i <= range.second; ++i) { |
1353 | 0 | for (const auto& desc : descs) { |
1354 | 0 | std::vector<CScript> scripts; |
1355 | 0 | if (!desc->Expand(i, provider, scripts, provider)) { |
1356 | 0 | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Cannot derive script without private keys: '%s'", desc_str));Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
1357 | 0 | } |
1358 | 0 | if (expand_priv) { |
1359 | 0 | desc->ExpandPrivate(/*pos=*/i, provider, /*out=*/provider); |
1360 | 0 | } |
1361 | 0 | std::move(scripts.begin(), scripts.end(), std::back_inserter(ret)); |
1362 | 0 | } |
1363 | 0 | } |
1364 | 0 | return ret; |
1365 | 0 | } |
1366 | | |
1367 | | /** Convert a vector of bilingual strings to a UniValue::VARR containing their original untranslated values. */ |
1368 | | [[nodiscard]] static UniValue BilingualStringsToUniValue(const std::vector<bilingual_str>& bilingual_strings) |
1369 | 0 | { |
1370 | 0 | CHECK_NONFATAL(!bilingual_strings.empty()); Line | Count | Source | 110 | 0 | inline_check_non_fatal(condition, std::source_location::current(), #condition) |
|
1371 | 0 | UniValue result{UniValue::VARR}; |
1372 | 0 | for (const auto& s : bilingual_strings) { |
1373 | 0 | result.push_back(s.original); |
1374 | 0 | } |
1375 | 0 | return result; |
1376 | 0 | } |
1377 | | |
1378 | | void PushWarnings(const UniValue& warnings, UniValue& obj) |
1379 | 0 | { |
1380 | 0 | if (warnings.empty()) return; |
1381 | 0 | obj.pushKV("warnings", warnings); |
1382 | 0 | } |
1383 | | |
1384 | | void PushWarnings(const std::vector<bilingual_str>& warnings, UniValue& obj) |
1385 | 0 | { |
1386 | 0 | if (warnings.empty()) return; |
1387 | 0 | obj.pushKV("warnings", BilingualStringsToUniValue(warnings)); |
1388 | 0 | } |
1389 | | |
1390 | 0 | std::vector<RPCResult> ScriptPubKeyDoc() { |
1391 | 0 | return |
1392 | 0 | { |
1393 | 0 | {RPCResult::Type::STR, "asm", "Disassembly of the output script"}, |
1394 | 0 | {RPCResult::Type::STR, "desc", "Inferred descriptor for the output"}, |
1395 | 0 | {RPCResult::Type::STR_HEX, "hex", "The raw output script bytes, hex-encoded"}, |
1396 | 0 | {RPCResult::Type::STR, "address", /*optional=*/true, "The Bitcoin address (only if a well-defined address exists)"}, |
1397 | 0 | {RPCResult::Type::STR, "type", "The type (one of: " + GetAllOutputTypes() + ")"}, |
1398 | 0 | }; |
1399 | 0 | } |
1400 | | |
1401 | | uint256 GetTarget(const CBlockIndex& blockindex, const uint256 pow_limit) |
1402 | 0 | { |
1403 | 0 | arith_uint256 target{*CHECK_NONFATAL(DeriveTarget(blockindex.nBits, pow_limit))};Line | Count | Source | 110 | 0 | inline_check_non_fatal(condition, std::source_location::current(), #condition) |
|
1404 | 0 | return ArithToUint256(target); |
1405 | 0 | } |