Simplify "bool x = y ? true : false" to "bool x = y"
[bitcoinplatinum.git] / src / rpc / misc.cpp
blob76aa7ec992a488e67c40d83b0c5a630c4ec30571
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #include "base58.h"
7 #include "chain.h"
8 #include "clientversion.h"
9 #include "init.h"
10 #include "validation.h"
11 #include "httpserver.h"
12 #include "net.h"
13 #include "netbase.h"
14 #include "rpc/blockchain.h"
15 #include "rpc/server.h"
16 #include "timedata.h"
17 #include "util.h"
18 #include "utilstrencodings.h"
19 #ifdef ENABLE_WALLET
20 #include "wallet/rpcwallet.h"
21 #include "wallet/wallet.h"
22 #include "wallet/walletdb.h"
23 #endif
25 #include <stdint.h>
26 #ifdef HAVE_MALLOC_INFO
27 #include <malloc.h>
28 #endif
30 #include <boost/assign/list_of.hpp>
32 #include <univalue.h>
34 /**
35 * @note Do not add or change anything in the information returned by this
36 * method. `getinfo` exists for backwards-compatibility only. It combines
37 * information from wildly different sources in the program, which is a mess,
38 * and is thus planned to be deprecated eventually.
40 * Based on the source of the information, new information should be added to:
41 * - `getblockchaininfo`,
42 * - `getnetworkinfo` or
43 * - `getwalletinfo`
45 * Or alternatively, create a specific query method for the information.
46 **/
47 UniValue getinfo(const JSONRPCRequest& request)
49 if (request.fHelp || request.params.size() != 0)
50 throw std::runtime_error(
51 "getinfo\n"
52 "\nDEPRECATED. Returns an object containing various state info.\n"
53 "\nResult:\n"
54 "{\n"
55 " \"version\": xxxxx, (numeric) the server version\n"
56 " \"protocolversion\": xxxxx, (numeric) the protocol version\n"
57 " \"walletversion\": xxxxx, (numeric) the wallet version\n"
58 " \"balance\": xxxxxxx, (numeric) the total bitcoin balance of the wallet\n"
59 " \"blocks\": xxxxxx, (numeric) the current number of blocks processed in the server\n"
60 " \"timeoffset\": xxxxx, (numeric) the time offset\n"
61 " \"connections\": xxxxx, (numeric) the number of connections\n"
62 " \"proxy\": \"host:port\", (string, optional) the proxy used by the server\n"
63 " \"difficulty\": xxxxxx, (numeric) the current difficulty\n"
64 " \"testnet\": true|false, (boolean) if the server is using testnet or not\n"
65 " \"keypoololdest\": xxxxxx, (numeric) the timestamp (seconds since Unix epoch) of the oldest pre-generated key in the key pool\n"
66 " \"keypoolsize\": xxxx, (numeric) how many new keys are pre-generated\n"
67 " \"unlocked_until\": ttt, (numeric) the timestamp in seconds since epoch (midnight Jan 1 1970 GMT) that the wallet is unlocked for transfers, or 0 if the wallet is locked\n"
68 " \"paytxfee\": x.xxxx, (numeric) the transaction fee set in " + CURRENCY_UNIT + "/kB\n"
69 " \"relayfee\": x.xxxx, (numeric) minimum relay fee for transactions in " + CURRENCY_UNIT + "/kB\n"
70 " \"errors\": \"...\" (string) any error messages\n"
71 "}\n"
72 "\nExamples:\n"
73 + HelpExampleCli("getinfo", "")
74 + HelpExampleRpc("getinfo", "")
77 #ifdef ENABLE_WALLET
78 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
80 LOCK2(cs_main, pwallet ? &pwallet->cs_wallet : NULL);
81 #else
82 LOCK(cs_main);
83 #endif
85 proxyType proxy;
86 GetProxy(NET_IPV4, proxy);
88 UniValue obj(UniValue::VOBJ);
89 obj.push_back(Pair("version", CLIENT_VERSION));
90 obj.push_back(Pair("protocolversion", PROTOCOL_VERSION));
91 #ifdef ENABLE_WALLET
92 if (pwallet) {
93 obj.push_back(Pair("walletversion", pwallet->GetVersion()));
94 obj.push_back(Pair("balance", ValueFromAmount(pwallet->GetBalance())));
96 #endif
97 obj.push_back(Pair("blocks", (int)chainActive.Height()));
98 obj.push_back(Pair("timeoffset", GetTimeOffset()));
99 if(g_connman)
100 obj.push_back(Pair("connections", (int)g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL)));
101 obj.push_back(Pair("proxy", (proxy.IsValid() ? proxy.proxy.ToStringIPPort() : std::string())));
102 obj.push_back(Pair("difficulty", (double)GetDifficulty()));
103 obj.push_back(Pair("testnet", Params().NetworkIDString() == CBaseChainParams::TESTNET));
104 #ifdef ENABLE_WALLET
105 if (pwallet) {
106 obj.push_back(Pair("keypoololdest", pwallet->GetOldestKeyPoolTime()));
107 obj.push_back(Pair("keypoolsize", (int)pwallet->GetKeyPoolSize()));
109 if (pwallet && pwallet->IsCrypted()) {
110 obj.push_back(Pair("unlocked_until", pwallet->nRelockTime));
112 obj.push_back(Pair("paytxfee", ValueFromAmount(payTxFee.GetFeePerK())));
113 #endif
114 obj.push_back(Pair("relayfee", ValueFromAmount(::minRelayTxFee.GetFeePerK())));
115 obj.push_back(Pair("errors", GetWarnings("statusbar")));
116 return obj;
119 #ifdef ENABLE_WALLET
120 class DescribeAddressVisitor : public boost::static_visitor<UniValue>
122 public:
123 CWallet * const pwallet;
125 DescribeAddressVisitor(CWallet *_pwallet) : pwallet(_pwallet) {}
127 UniValue operator()(const CNoDestination &dest) const { return UniValue(UniValue::VOBJ); }
129 UniValue operator()(const CKeyID &keyID) const {
130 UniValue obj(UniValue::VOBJ);
131 CPubKey vchPubKey;
132 obj.push_back(Pair("isscript", false));
133 if (pwallet && pwallet->GetPubKey(keyID, vchPubKey)) {
134 obj.push_back(Pair("pubkey", HexStr(vchPubKey)));
135 obj.push_back(Pair("iscompressed", vchPubKey.IsCompressed()));
137 return obj;
140 UniValue operator()(const CScriptID &scriptID) const {
141 UniValue obj(UniValue::VOBJ);
142 CScript subscript;
143 obj.push_back(Pair("isscript", true));
144 if (pwallet && pwallet->GetCScript(scriptID, subscript)) {
145 std::vector<CTxDestination> addresses;
146 txnouttype whichType;
147 int nRequired;
148 ExtractDestinations(subscript, whichType, addresses, nRequired);
149 obj.push_back(Pair("script", GetTxnOutputType(whichType)));
150 obj.push_back(Pair("hex", HexStr(subscript.begin(), subscript.end())));
151 UniValue a(UniValue::VARR);
152 BOOST_FOREACH(const CTxDestination& addr, addresses)
153 a.push_back(CBitcoinAddress(addr).ToString());
154 obj.push_back(Pair("addresses", a));
155 if (whichType == TX_MULTISIG)
156 obj.push_back(Pair("sigsrequired", nRequired));
158 return obj;
161 #endif
163 UniValue validateaddress(const JSONRPCRequest& request)
165 if (request.fHelp || request.params.size() != 1)
166 throw std::runtime_error(
167 "validateaddress \"address\"\n"
168 "\nReturn information about the given bitcoin address.\n"
169 "\nArguments:\n"
170 "1. \"address\" (string, required) The bitcoin address to validate\n"
171 "\nResult:\n"
172 "{\n"
173 " \"isvalid\" : true|false, (boolean) If the address is valid or not. If not, this is the only property returned.\n"
174 " \"address\" : \"address\", (string) The bitcoin address validated\n"
175 " \"scriptPubKey\" : \"hex\", (string) The hex encoded scriptPubKey generated by the address\n"
176 " \"ismine\" : true|false, (boolean) If the address is yours or not\n"
177 " \"iswatchonly\" : true|false, (boolean) If the address is watchonly\n"
178 " \"isscript\" : true|false, (boolean) If the key is a script\n"
179 " \"pubkey\" : \"publickeyhex\", (string) The hex value of the raw public key\n"
180 " \"iscompressed\" : true|false, (boolean) If the address is compressed\n"
181 " \"account\" : \"account\" (string) DEPRECATED. The account associated with the address, \"\" is the default account\n"
182 " \"timestamp\" : timestamp, (number, optional) The creation time of the key if available in seconds since epoch (Jan 1 1970 GMT)\n"
183 " \"hdkeypath\" : \"keypath\" (string, optional) The HD keypath if the key is HD and available\n"
184 " \"hdmasterkeyid\" : \"<hash160>\" (string, optional) The Hash160 of the HD master pubkey\n"
185 "}\n"
186 "\nExamples:\n"
187 + HelpExampleCli("validateaddress", "\"1PSSGeFHDnKNxiEyFrD1wcEaHr9hrQDDWc\"")
188 + HelpExampleRpc("validateaddress", "\"1PSSGeFHDnKNxiEyFrD1wcEaHr9hrQDDWc\"")
191 #ifdef ENABLE_WALLET
192 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
194 LOCK2(cs_main, pwallet ? &pwallet->cs_wallet : NULL);
195 #else
196 LOCK(cs_main);
197 #endif
199 CBitcoinAddress address(request.params[0].get_str());
200 bool isValid = address.IsValid();
202 UniValue ret(UniValue::VOBJ);
203 ret.push_back(Pair("isvalid", isValid));
204 if (isValid)
206 CTxDestination dest = address.Get();
207 std::string currentAddress = address.ToString();
208 ret.push_back(Pair("address", currentAddress));
210 CScript scriptPubKey = GetScriptForDestination(dest);
211 ret.push_back(Pair("scriptPubKey", HexStr(scriptPubKey.begin(), scriptPubKey.end())));
213 #ifdef ENABLE_WALLET
214 isminetype mine = pwallet ? IsMine(*pwallet, dest) : ISMINE_NO;
215 ret.push_back(Pair("ismine", bool(mine & ISMINE_SPENDABLE)));
216 ret.push_back(Pair("iswatchonly", bool(mine & ISMINE_WATCH_ONLY)));
217 UniValue detail = boost::apply_visitor(DescribeAddressVisitor(pwallet), dest);
218 ret.pushKVs(detail);
219 if (pwallet && pwallet->mapAddressBook.count(dest)) {
220 ret.push_back(Pair("account", pwallet->mapAddressBook[dest].name));
222 CKeyID keyID;
223 if (pwallet) {
224 const auto& meta = pwallet->mapKeyMetadata;
225 auto it = address.GetKeyID(keyID) ? meta.find(keyID) : meta.end();
226 if (it == meta.end()) {
227 it = meta.find(CScriptID(scriptPubKey));
229 if (it != meta.end()) {
230 ret.push_back(Pair("timestamp", it->second.nCreateTime));
231 if (!it->second.hdKeypath.empty()) {
232 ret.push_back(Pair("hdkeypath", it->second.hdKeypath));
233 ret.push_back(Pair("hdmasterkeyid", it->second.hdMasterKeyID.GetHex()));
237 #endif
239 return ret;
242 // Needed even with !ENABLE_WALLET, to pass (ignored) pointers around
243 class CWallet;
246 * Used by addmultisigaddress / createmultisig:
248 CScript _createmultisig_redeemScript(CWallet * const pwallet, const UniValue& params)
250 int nRequired = params[0].get_int();
251 const UniValue& keys = params[1].get_array();
253 // Gather public keys
254 if (nRequired < 1)
255 throw std::runtime_error("a multisignature address must require at least one key to redeem");
256 if ((int)keys.size() < nRequired)
257 throw std::runtime_error(
258 strprintf("not enough keys supplied "
259 "(got %u keys, but need at least %d to redeem)", keys.size(), nRequired));
260 if (keys.size() > 16)
261 throw std::runtime_error("Number of addresses involved in the multisignature address creation > 16\nReduce the number");
262 std::vector<CPubKey> pubkeys;
263 pubkeys.resize(keys.size());
264 for (unsigned int i = 0; i < keys.size(); i++)
266 const std::string& ks = keys[i].get_str();
267 #ifdef ENABLE_WALLET
268 // Case 1: Bitcoin address and we have full public key:
269 CBitcoinAddress address(ks);
270 if (pwallet && address.IsValid()) {
271 CKeyID keyID;
272 if (!address.GetKeyID(keyID))
273 throw std::runtime_error(
274 strprintf("%s does not refer to a key",ks));
275 CPubKey vchPubKey;
276 if (!pwallet->GetPubKey(keyID, vchPubKey)) {
277 throw std::runtime_error(
278 strprintf("no full public key for address %s",ks));
280 if (!vchPubKey.IsFullyValid())
281 throw std::runtime_error(" Invalid public key: "+ks);
282 pubkeys[i] = vchPubKey;
285 // Case 2: hex public key
286 else
287 #endif
288 if (IsHex(ks))
290 CPubKey vchPubKey(ParseHex(ks));
291 if (!vchPubKey.IsFullyValid())
292 throw std::runtime_error(" Invalid public key: "+ks);
293 pubkeys[i] = vchPubKey;
295 else
297 throw std::runtime_error(" Invalid public key: "+ks);
300 CScript result = GetScriptForMultisig(nRequired, pubkeys);
302 if (result.size() > MAX_SCRIPT_ELEMENT_SIZE)
303 throw std::runtime_error(
304 strprintf("redeemScript exceeds size limit: %d > %d", result.size(), MAX_SCRIPT_ELEMENT_SIZE));
306 return result;
309 UniValue createmultisig(const JSONRPCRequest& request)
311 #ifdef ENABLE_WALLET
312 CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
313 #else
314 CWallet * const pwallet = NULL;
315 #endif
317 if (request.fHelp || request.params.size() < 2 || request.params.size() > 2)
319 std::string msg = "createmultisig nrequired [\"key\",...]\n"
320 "\nCreates a multi-signature address with n signature of m keys required.\n"
321 "It returns a json object with the address and redeemScript.\n"
323 "\nArguments:\n"
324 "1. nrequired (numeric, required) The number of required signatures out of the n keys or addresses.\n"
325 "2. \"keys\" (string, required) A json array of keys which are bitcoin addresses or hex-encoded public keys\n"
326 " [\n"
327 " \"key\" (string) bitcoin address or hex-encoded public key\n"
328 " ,...\n"
329 " ]\n"
331 "\nResult:\n"
332 "{\n"
333 " \"address\":\"multisigaddress\", (string) The value of the new multisig address.\n"
334 " \"redeemScript\":\"script\" (string) The string value of the hex-encoded redemption script.\n"
335 "}\n"
337 "\nExamples:\n"
338 "\nCreate a multisig address from 2 addresses\n"
339 + HelpExampleCli("createmultisig", "2 \"[\\\"16sSauSf5pF2UkUwvKGq4qjNRzBZYqgEL5\\\",\\\"171sgjn4YtPu27adkKGrdDwzRTxnRkBfKV\\\"]\"") +
340 "\nAs a json rpc call\n"
341 + HelpExampleRpc("createmultisig", "2, \"[\\\"16sSauSf5pF2UkUwvKGq4qjNRzBZYqgEL5\\\",\\\"171sgjn4YtPu27adkKGrdDwzRTxnRkBfKV\\\"]\"")
343 throw std::runtime_error(msg);
346 // Construct using pay-to-script-hash:
347 CScript inner = _createmultisig_redeemScript(pwallet, request.params);
348 CScriptID innerID(inner);
349 CBitcoinAddress address(innerID);
351 UniValue result(UniValue::VOBJ);
352 result.push_back(Pair("address", address.ToString()));
353 result.push_back(Pair("redeemScript", HexStr(inner.begin(), inner.end())));
355 return result;
358 UniValue verifymessage(const JSONRPCRequest& request)
360 if (request.fHelp || request.params.size() != 3)
361 throw std::runtime_error(
362 "verifymessage \"address\" \"signature\" \"message\"\n"
363 "\nVerify a signed message\n"
364 "\nArguments:\n"
365 "1. \"address\" (string, required) The bitcoin address to use for the signature.\n"
366 "2. \"signature\" (string, required) The signature provided by the signer in base 64 encoding (see signmessage).\n"
367 "3. \"message\" (string, required) The message that was signed.\n"
368 "\nResult:\n"
369 "true|false (boolean) If the signature is verified or not.\n"
370 "\nExamples:\n"
371 "\nUnlock the wallet for 30 seconds\n"
372 + HelpExampleCli("walletpassphrase", "\"mypassphrase\" 30") +
373 "\nCreate the signature\n"
374 + HelpExampleCli("signmessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"my message\"") +
375 "\nVerify the signature\n"
376 + HelpExampleCli("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"signature\" \"my message\"") +
377 "\nAs json rpc\n"
378 + HelpExampleRpc("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\", \"signature\", \"my message\"")
381 LOCK(cs_main);
383 std::string strAddress = request.params[0].get_str();
384 std::string strSign = request.params[1].get_str();
385 std::string strMessage = request.params[2].get_str();
387 CBitcoinAddress addr(strAddress);
388 if (!addr.IsValid())
389 throw JSONRPCError(RPC_TYPE_ERROR, "Invalid address");
391 CKeyID keyID;
392 if (!addr.GetKeyID(keyID))
393 throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to key");
395 bool fInvalid = false;
396 std::vector<unsigned char> vchSig = DecodeBase64(strSign.c_str(), &fInvalid);
398 if (fInvalid)
399 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Malformed base64 encoding");
401 CHashWriter ss(SER_GETHASH, 0);
402 ss << strMessageMagic;
403 ss << strMessage;
405 CPubKey pubkey;
406 if (!pubkey.RecoverCompact(ss.GetHash(), vchSig))
407 return false;
409 return (pubkey.GetID() == keyID);
412 UniValue signmessagewithprivkey(const JSONRPCRequest& request)
414 if (request.fHelp || request.params.size() != 2)
415 throw std::runtime_error(
416 "signmessagewithprivkey \"privkey\" \"message\"\n"
417 "\nSign a message with the private key of an address\n"
418 "\nArguments:\n"
419 "1. \"privkey\" (string, required) The private key to sign the message with.\n"
420 "2. \"message\" (string, required) The message to create a signature of.\n"
421 "\nResult:\n"
422 "\"signature\" (string) The signature of the message encoded in base 64\n"
423 "\nExamples:\n"
424 "\nCreate the signature\n"
425 + HelpExampleCli("signmessagewithprivkey", "\"privkey\" \"my message\"") +
426 "\nVerify the signature\n"
427 + HelpExampleCli("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"signature\" \"my message\"") +
428 "\nAs json rpc\n"
429 + HelpExampleRpc("signmessagewithprivkey", "\"privkey\", \"my message\"")
432 std::string strPrivkey = request.params[0].get_str();
433 std::string strMessage = request.params[1].get_str();
435 CBitcoinSecret vchSecret;
436 bool fGood = vchSecret.SetString(strPrivkey);
437 if (!fGood)
438 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");
439 CKey key = vchSecret.GetKey();
440 if (!key.IsValid())
441 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Private key outside allowed range");
443 CHashWriter ss(SER_GETHASH, 0);
444 ss << strMessageMagic;
445 ss << strMessage;
447 std::vector<unsigned char> vchSig;
448 if (!key.SignCompact(ss.GetHash(), vchSig))
449 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Sign failed");
451 return EncodeBase64(&vchSig[0], vchSig.size());
454 UniValue setmocktime(const JSONRPCRequest& request)
456 if (request.fHelp || request.params.size() != 1)
457 throw std::runtime_error(
458 "setmocktime timestamp\n"
459 "\nSet the local time to given timestamp (-regtest only)\n"
460 "\nArguments:\n"
461 "1. timestamp (integer, required) Unix seconds-since-epoch timestamp\n"
462 " Pass 0 to go back to using the system time."
465 if (!Params().MineBlocksOnDemand())
466 throw std::runtime_error("setmocktime for regression testing (-regtest mode) only");
468 // For now, don't change mocktime if we're in the middle of validation, as
469 // this could have an effect on mempool time-based eviction, as well as
470 // IsCurrentForFeeEstimation() and IsInitialBlockDownload().
471 // TODO: figure out the right way to synchronize around mocktime, and
472 // ensure all call sites of GetTime() are accessing this safely.
473 LOCK(cs_main);
475 RPCTypeCheck(request.params, boost::assign::list_of(UniValue::VNUM));
476 SetMockTime(request.params[0].get_int64());
478 return NullUniValue;
481 static UniValue RPCLockedMemoryInfo()
483 LockedPool::Stats stats = LockedPoolManager::Instance().stats();
484 UniValue obj(UniValue::VOBJ);
485 obj.push_back(Pair("used", uint64_t(stats.used)));
486 obj.push_back(Pair("free", uint64_t(stats.free)));
487 obj.push_back(Pair("total", uint64_t(stats.total)));
488 obj.push_back(Pair("locked", uint64_t(stats.locked)));
489 obj.push_back(Pair("chunks_used", uint64_t(stats.chunks_used)));
490 obj.push_back(Pair("chunks_free", uint64_t(stats.chunks_free)));
491 return obj;
494 #ifdef HAVE_MALLOC_INFO
495 static std::string RPCMallocInfo()
497 char *ptr = nullptr;
498 size_t size = 0;
499 FILE *f = open_memstream(&ptr, &size);
500 if (f) {
501 malloc_info(0, f);
502 fclose(f);
503 if (ptr) {
504 std::string rv(ptr, size);
505 free(ptr);
506 return rv;
509 return "";
511 #endif
513 UniValue getmemoryinfo(const JSONRPCRequest& request)
515 /* Please, avoid using the word "pool" here in the RPC interface or help,
516 * as users will undoubtedly confuse it with the other "memory pool"
518 if (request.fHelp || request.params.size() > 1)
519 throw std::runtime_error(
520 "getmemoryinfo (\"mode\")\n"
521 "Returns an object containing information about memory usage.\n"
522 "Arguments:\n"
523 "1. \"mode\" determines what kind of information is returned. This argument is optional, the default mode is \"stats\".\n"
524 " - \"stats\" returns general statistics about memory usage in the daemon.\n"
525 " - \"mallocinfo\" returns an XML string describing low-level heap state (only available if compiled with glibc 2.10+).\n"
526 "\nResult (mode \"stats\"):\n"
527 "{\n"
528 " \"locked\": { (json object) Information about locked memory manager\n"
529 " \"used\": xxxxx, (numeric) Number of bytes used\n"
530 " \"free\": xxxxx, (numeric) Number of bytes available in current arenas\n"
531 " \"total\": xxxxxxx, (numeric) Total number of bytes managed\n"
532 " \"locked\": xxxxxx, (numeric) Amount of bytes that succeeded locking. If this number is smaller than total, locking pages failed at some point and key data could be swapped to disk.\n"
533 " \"chunks_used\": xxxxx, (numeric) Number allocated chunks\n"
534 " \"chunks_free\": xxxxx, (numeric) Number unused chunks\n"
535 " }\n"
536 "}\n"
537 "\nResult (mode \"mallocinfo\"):\n"
538 "\"<malloc version=\"1\">...\"\n"
539 "\nExamples:\n"
540 + HelpExampleCli("getmemoryinfo", "")
541 + HelpExampleRpc("getmemoryinfo", "")
544 std::string mode = (request.params.size() < 1 || request.params[0].isNull()) ? "stats" : request.params[0].get_str();
545 if (mode == "stats") {
546 UniValue obj(UniValue::VOBJ);
547 obj.push_back(Pair("locked", RPCLockedMemoryInfo()));
548 return obj;
549 } else if (mode == "mallocinfo") {
550 #ifdef HAVE_MALLOC_INFO
551 return RPCMallocInfo();
552 #else
553 throw JSONRPCError(RPC_INVALID_PARAMETER, "mallocinfo is only available when compiled with glibc 2.10+");
554 #endif
555 } else {
556 throw JSONRPCError(RPC_INVALID_PARAMETER, "unknown mode " + mode);
560 uint32_t getCategoryMask(UniValue cats) {
561 cats = cats.get_array();
562 uint32_t mask = 0;
563 for (unsigned int i = 0; i < cats.size(); ++i) {
564 uint32_t flag = 0;
565 std::string cat = cats[i].get_str();
566 if (!GetLogCategory(&flag, &cat)) {
567 throw JSONRPCError(RPC_INVALID_PARAMETER, "unknown logging category " + cat);
569 mask |= flag;
571 return mask;
574 UniValue logging(const JSONRPCRequest& request)
576 if (request.fHelp || request.params.size() > 2) {
577 throw std::runtime_error(
578 "logging [include,...] <exclude>\n"
579 "Gets and sets the logging configuration.\n"
580 "When called without an argument, returns the list of categories that are currently being debug logged.\n"
581 "When called with arguments, adds or removes categories from debug logging.\n"
582 "The valid logging categories are: " + ListLogCategories() + "\n"
583 "libevent logging is configured on startup and cannot be modified by this RPC during runtime."
584 "Arguments:\n"
585 "1. \"include\" (array of strings) add debug logging for these categories.\n"
586 "2. \"exclude\" (array of strings) remove debug logging for these categories.\n"
587 "\nResult: <categories> (string): a list of the logging categories that are active.\n"
588 "\nExamples:\n"
589 + HelpExampleCli("logging", "\"[\\\"all\\\"]\" \"[\\\"http\\\"]\"")
590 + HelpExampleRpc("logging", "[\"all\"], \"[libevent]\"")
594 uint32_t originalLogCategories = logCategories;
595 if (request.params.size() > 0 && request.params[0].isArray()) {
596 logCategories |= getCategoryMask(request.params[0]);
599 if (request.params.size() > 1 && request.params[1].isArray()) {
600 logCategories &= ~getCategoryMask(request.params[1]);
603 // Update libevent logging if BCLog::LIBEVENT has changed.
604 // If the library version doesn't allow it, UpdateHTTPServerLogging() returns false,
605 // in which case we should clear the BCLog::LIBEVENT flag.
606 // Throw an error if the user has explicitly asked to change only the libevent
607 // flag and it failed.
608 uint32_t changedLogCategories = originalLogCategories ^ logCategories;
609 if (changedLogCategories & BCLog::LIBEVENT) {
610 if (!UpdateHTTPServerLogging(logCategories & BCLog::LIBEVENT)) {
611 logCategories &= ~BCLog::LIBEVENT;
612 if (changedLogCategories == BCLog::LIBEVENT) {
613 throw JSONRPCError(RPC_INVALID_PARAMETER, "libevent logging cannot be updated when using libevent before v2.1.1.");
618 UniValue result(UniValue::VOBJ);
619 std::vector<CLogCategoryActive> vLogCatActive = ListActiveLogCategories();
620 for (const auto& logCatActive : vLogCatActive) {
621 result.pushKV(logCatActive.category, logCatActive.active);
624 return result;
627 UniValue echo(const JSONRPCRequest& request)
629 if (request.fHelp)
630 throw std::runtime_error(
631 "echo|echojson \"message\" ...\n"
632 "\nSimply echo back the input arguments. This command is for testing.\n"
633 "\nThe difference between echo and echojson is that echojson has argument conversion enabled in the client-side table in"
634 "bitcoin-cli and the GUI. There is no server-side difference."
637 return request.params;
640 static const CRPCCommand commands[] =
641 { // category name actor (function) okSafeMode
642 // --------------------- ------------------------ ----------------------- ----------
643 { "control", "getinfo", &getinfo, true, {} }, /* uses wallet if enabled */
644 { "control", "getmemoryinfo", &getmemoryinfo, true, {"mode"} },
645 { "util", "validateaddress", &validateaddress, true, {"address"} }, /* uses wallet if enabled */
646 { "util", "createmultisig", &createmultisig, true, {"nrequired","keys"} },
647 { "util", "verifymessage", &verifymessage, true, {"address","signature","message"} },
648 { "util", "signmessagewithprivkey", &signmessagewithprivkey, true, {"privkey","message"} },
650 /* Not shown in help */
651 { "hidden", "setmocktime", &setmocktime, true, {"timestamp"}},
652 { "hidden", "echo", &echo, true, {"arg0","arg1","arg2","arg3","arg4","arg5","arg6","arg7","arg8","arg9"}},
653 { "hidden", "echojson", &echo, true, {"arg0","arg1","arg2","arg3","arg4","arg5","arg6","arg7","arg8","arg9"}},
654 { "hidden", "logging", &logging, true, {"include", "exclude"}},
657 void RegisterMiscRPCCommands(CRPCTable &t)
659 for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++)
660 t.appendCommand(commands[vcidx].name, &commands[vcidx]);