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