Files
thehub/libs/server/rpcserver.cpp
T

584 lines
23 KiB
C++
Raw Permalink Normal View History

2017-11-09 19:34:51 +01:00
/*
* This file is part of the Flowee project
* Copyright (C) 2010 Satoshi Nakamoto
* Copyright (C) 2009-2015 The Bitcoin Core developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2011-05-14 20:10:21 +02:00
2013-11-20 14:18:57 +01:00
#include "rpcserver.h"
2013-04-13 00:13:08 -05:00
#include "base58.h"
#include "init.h"
#include "random.h"
#include "sync.h"
2018-02-10 22:48:07 +01:00
#include "UiInterface.h"
2014-01-11 18:14:29 +01:00
#include "util.h"
#include "utilstrencodings.h"
2013-04-13 00:13:08 -05:00
2015-09-04 16:11:34 +02:00
#include <univalue.h>
2015-01-23 07:53:17 +01:00
2011-07-13 11:56:38 +02:00
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
2011-05-14 20:10:21 +02:00
#include <boost/iostreams/concepts.hpp>
2011-08-10 13:53:13 +02:00
#include <boost/shared_ptr.hpp>
#include <boost/signals2/signal.hpp>
2014-08-21 16:11:09 +02:00
#include <boost/thread.hpp>
2015-01-23 07:53:17 +01:00
#include <boost/algorithm/string/case_conv.hpp> // for to_upper()
using namespace RPCServer;
2011-05-14 20:10:21 +02:00
2012-05-13 04:43:24 +00:00
static bool fRPCRunning = false;
2014-10-29 18:08:31 +01:00
static bool fRPCInWarmup = true;
static std::string rpcWarmupStatus("RPC server started");
static CCriticalSection cs_rpcWarmup;
2015-01-23 07:53:17 +01:00
/* Timer-creating functions */
static std::vector<RPCTimerInterface*> timerInterfaces;
/* Map of name to timer.
* @note Can be changed to std::unique_ptr when C++11 */
static std::map<std::string, boost::shared_ptr<RPCTimerBase> > deadlineTimers;
2012-04-14 20:35:58 -04:00
static struct CRPCSignals
{
boost::signals2::signal<void ()> Started;
boost::signals2::signal<void ()> Stopped;
boost::signals2::signal<void (const CRPCCommand&)> PreCommand;
boost::signals2::signal<void (const CRPCCommand&)> PostCommand;
} g_rpcSignals;
void RPCServer::OnStarted(boost::function<void ()> slot)
{
g_rpcSignals.Started.connect(slot);
}
void RPCServer::OnStopped(boost::function<void ()> slot)
{
g_rpcSignals.Stopped.connect(slot);
}
void RPCServer::OnPreCommand(boost::function<void (const CRPCCommand&)> slot)
{
2019-10-10 16:18:53 +02:00
g_rpcSignals.PreCommand.connect(std::bind(slot, std::placeholders::_1));
}
void RPCServer::OnPostCommand(boost::function<void (const CRPCCommand&)> slot)
{
2019-10-10 16:18:53 +02:00
g_rpcSignals.PostCommand.connect(std::bind(slot, std::placeholders::_1));
}
void RPCTypeCheck(const UniValue& params,
2017-08-02 19:53:22 +05:30
const std::list<UniValue::VType>& typesExpected,
bool fAllowNull)
{
unsigned int i = 0;
BOOST_FOREACH(UniValue::VType t, typesExpected)
{
if (params.size() <= i)
break;
const UniValue& v = params[i];
if (!((v.type() == t) || (fAllowNull && (v.isNull()))))
{
2017-08-02 19:53:22 +05:30
std::string err = strprintf("Expected type %s, got %s",
uvTypeName(t), uvTypeName(v.type()));
2012-10-04 09:34:44 +02:00
throw JSONRPCError(RPC_TYPE_ERROR, err);
}
i++;
}
}
void RPCTypeCheckObj(const UniValue& o,
2017-08-02 19:53:22 +05:30
const std::map<std::string, UniValue::VType>& typesExpected,
bool fAllowNull)
{
2017-08-02 19:53:22 +05:30
BOOST_FOREACH(const PAIRTYPE(std::string, UniValue::VType)& t, typesExpected)
{
const UniValue& v = find_value(o, t.first);
if (!fAllowNull && v.isNull())
2014-01-16 16:15:27 +01:00
throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Missing %s", t.first));
if (!((v.type() == t.second) || (fAllowNull && (v.isNull()))))
{
2017-08-02 19:53:22 +05:30
std::string err = strprintf("Expected type %s for %s, got %s",
uvTypeName(t.second), t.first, uvTypeName(v.type()));
2012-10-04 09:34:44 +02:00
throw JSONRPCError(RPC_TYPE_ERROR, err);
}
}
}
CAmount AmountFromValue(const UniValue& value)
2011-05-14 20:10:21 +02:00
{
2015-07-06 11:43:56 +02:00
if (!value.isNum() && !value.isStr())
throw JSONRPCError(RPC_TYPE_ERROR, "Amount is not a number or string");
CAmount amount;
if (!ParseFixedPoint(value.getValStr(), 8, &amount))
2012-10-04 09:34:44 +02:00
throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount");
if (!MoneyRange(amount))
throw JSONRPCError(RPC_TYPE_ERROR, "Amount out of range");
return amount;
2011-05-14 20:10:21 +02:00
}
2015-05-13 21:29:19 +02:00
UniValue ValueFromAmount(const CAmount& amount)
2011-05-14 20:10:21 +02:00
{
bool sign = amount < 0;
int64_t n_abs = (sign ? -amount : amount);
int64_t quotient = n_abs / COIN;
int64_t remainder = n_abs % COIN;
return UniValue(UniValue::VNUM,
strprintf("%s%d.%08d", sign ? "-" : "", quotient, remainder));
2011-05-14 20:10:21 +02:00
}
2017-08-02 19:53:22 +05:30
uint256 ParseHashV(const UniValue& v, std::string strName)
{
2017-08-02 19:53:22 +05:30
std::string strHex;
if (v.isStr())
strHex = v.get_str();
if (!IsHex(strHex)) // Note: IsHex("") is false
throw JSONRPCError(RPC_INVALID_PARAMETER, strName+" must be hexadecimal string (not '"+strHex+"')");
uint256 result;
result.SetHex(strHex);
return result;
}
2017-08-02 19:53:22 +05:30
uint256 ParseHashO(const UniValue& o, std::string strKey)
{
return ParseHashV(find_value(o, strKey), strKey);
}
2017-08-02 19:53:22 +05:30
std::vector<unsigned char> ParseHexV(const UniValue& v, std::string strName)
{
2017-08-02 19:53:22 +05:30
std::string strHex;
if (v.isStr())
strHex = v.get_str();
if (!IsHex(strHex))
throw JSONRPCError(RPC_INVALID_PARAMETER, strName+" must be hexadecimal string (not '"+strHex+"')");
return ParseHex(strHex);
}
2017-08-02 19:53:22 +05:30
std::vector<unsigned char> ParseHexO(const UniValue& o, std::string strKey)
{
return ParseHexV(find_value(o, strKey), strKey);
}
2011-05-14 20:10:21 +02:00
/**
* Note: This interface may still be subject to change.
*/
2011-05-14 20:10:21 +02:00
2015-05-31 15:36:44 +02:00
std::string CRPCTable::help(const std::string& strCommand) const
2011-05-14 20:10:21 +02:00
{
2017-08-02 19:53:22 +05:30
std::string strRet;
std::string category;
std::set<rpcfn_type> setDone;
std::vector<std::pair<std::string, const CRPCCommand*> > vCommands;
2014-07-15 21:38:52 +02:00
2017-08-02 19:53:22 +05:30
for (std::map<std::string, const CRPCCommand*>::const_iterator mi = mapCommands.begin(); mi != mapCommands.end(); ++mi)
vCommands.push_back(std::make_pair(mi->second->category + mi->first, mi->second));
std::sort(vCommands.begin(), vCommands.end());
2014-07-15 21:38:52 +02:00
2017-08-02 19:53:22 +05:30
BOOST_FOREACH(const PAIRTYPE(std::string, const CRPCCommand*)& command, vCommands)
2011-05-14 20:10:21 +02:00
{
2014-07-15 21:38:52 +02:00
const CRPCCommand *pcmd = command.second;
2017-08-02 19:53:22 +05:30
std::string strMethod = pcmd->name;
2011-05-14 20:10:21 +02:00
// We already filter duplicates, but these deprecated screw up the sort order
2017-08-02 19:53:22 +05:30
if (strMethod.find("label") != std::string::npos)
2011-05-14 20:10:21 +02:00
continue;
2014-11-26 16:33:18 +01:00
if ((strCommand != "" || pcmd->category == "hidden") && strMethod != strCommand)
2011-05-14 20:10:21 +02:00
continue;
try
{
2015-05-13 21:29:19 +02:00
UniValue params;
rpcfn_type pfn = pcmd->actor;
2011-05-14 20:10:21 +02:00
if (setDone.insert(pfn).second)
(*pfn)(params, true);
}
2014-12-07 13:29:06 +01:00
catch (const std::exception& e)
2011-05-14 20:10:21 +02:00
{
// Help text is returned in an exception
2017-08-02 19:53:22 +05:30
std::string strHelp = std::string(e.what());
2011-05-14 20:10:21 +02:00
if (strCommand == "")
2014-07-15 21:38:52 +02:00
{
2017-08-02 19:53:22 +05:30
if (strHelp.find('\n') != std::string::npos)
2011-05-14 20:10:21 +02:00
strHelp = strHelp.substr(0, strHelp.find('\n'));
2014-07-15 21:38:52 +02:00
if (category != pcmd->category)
{
if (!category.empty())
strRet += "\n";
category = pcmd->category;
2017-08-02 19:53:22 +05:30
std::string firstLetter = category.substr(0,1);
2014-07-15 21:38:52 +02:00
boost::to_upper(firstLetter);
strRet += "== " + firstLetter + category.substr(1) + " ==\n";
}
}
2011-05-14 20:10:21 +02:00
strRet += strHelp + "\n";
}
}
if (strRet == "")
2014-01-16 16:15:27 +01:00
strRet = strprintf("help: unknown command: %s\n", strCommand);
2011-05-14 20:10:21 +02:00
strRet = strRet.substr(0,strRet.size()-1);
return strRet;
}
UniValue help(const UniValue& params, bool fHelp)
2012-04-18 22:42:17 +02:00
{
if (fHelp || params.size() > 1)
2017-08-02 19:53:22 +05:30
throw std::runtime_error(
2013-10-29 22:29:44 +11:00
"help ( \"command\" )\n"
"\nList all commands, or get help for a specified command.\n"
"\nArguments:\n"
"1. \"command\" (string, optional) The command to get help on\n"
"\nResult:\n"
"\"text\" (string) The help text\n"
);
2012-04-18 22:42:17 +02:00
2017-08-02 19:53:22 +05:30
std::string strCommand;
2012-04-18 22:42:17 +02:00
if (params.size() > 0)
strCommand = params[0].get_str();
return tableRPC.help(strCommand);
}
2011-05-14 20:10:21 +02:00
UniValue stop(const UniValue& params, bool fHelp)
2011-05-14 20:10:21 +02:00
{
2013-01-20 18:50:30 +01:00
// Accept the deprecated and ignored 'detach' boolean argument
if (fHelp || params.size() > 1)
2017-08-02 19:53:22 +05:30
throw std::runtime_error(
"stop\n"
2013-10-29 22:29:44 +11:00
"\nStop Bitcoin server.");
2015-09-24 17:29:22 +02:00
// Event loop will exit after current HTTP requests have been handled, so
// this reply will get back to the client.
StartShutdown();
return "Bitcoin server stopping";
2011-05-14 20:10:21 +02:00
}
/**
* Call Table
*/
static const CRPCCommand vRPCCommands[] =
2015-04-12 17:56:44 +02:00
{ // category name actor (function) okSafeMode
// --------------------- ------------------------ ----------------------- ----------
2014-03-26 12:26:43 +01:00
/* Overall control/query calls */
2015-04-12 17:56:44 +02:00
{ "control", "getinfo", &getinfo, true }, /* uses wallet if enabled */
{ "control", "help", &help, true },
{ "control", "stop", &stop, true },
2014-03-26 12:26:43 +01:00
/* P2P networking */
2015-04-12 17:56:44 +02:00
{ "network", "getnetworkinfo", &getnetworkinfo, true },
{ "network", "addnode", &addnode, true },
2015-06-11 20:20:54 -07:00
{ "network", "disconnectnode", &disconnectnode, true },
2015-04-12 17:56:44 +02:00
{ "network", "getaddednodeinfo", &getaddednodeinfo, true },
{ "network", "getconnectioncount", &getconnectioncount, true },
{ "network", "getnettotals", &getnettotals, true },
{ "network", "getpeerinfo", &getpeerinfo, true },
{ "network", "ping", &ping, true },
{ "network", "setban", &setban, true },
{ "network", "listbanned", &listbanned, true },
{ "network", "clearbanned", &clearbanned, true },
2014-03-26 12:26:43 +01:00
/* Block chain and UTXO */
2015-04-12 17:56:44 +02:00
{ "blockchain", "getblockchaininfo", &getblockchaininfo, true },
{ "blockchain", "getbestblockhash", &getbestblockhash, true },
{ "blockchain", "getblockcount", &getblockcount, true },
{ "blockchain", "getblock", &getblock, true },
{ "blockchain", "getblockhash", &getblockhash, true },
2015-06-05 17:07:17 -02:30
{ "blockchain", "getblockheader", &getblockheader, true },
2015-04-12 17:56:44 +02:00
{ "blockchain", "getchaintips", &getchaintips, true },
{ "blockchain", "getdifficulty", &getdifficulty, true },
{ "blockchain", "getmempoolinfo", &getmempoolinfo, true },
{ "blockchain", "getrawmempool", &getrawmempool, true },
{ "blockchain", "gettxout", &gettxout, true },
{ "blockchain", "verifytxoutproof", &verifytxoutproof, true },
2015-04-12 17:56:44 +02:00
{ "blockchain", "verifychain", &verifychain, true },
2013-11-29 16:04:29 +01:00
2013-12-08 15:26:08 +01:00
/* Mining */
2016-05-10 11:25:39 +01:00
{ "mining", "setcoinbase", &setcoinbase, true },
2015-04-12 17:56:44 +02:00
{ "mining", "getblocktemplate", &getblocktemplate, true },
{ "mining", "getmininginfo", &getmininginfo, true },
{ "mining", "getnetworkhashps", &getnetworkhashps, true },
{ "mining", "prioritisetransaction", &prioritisetransaction, true },
{ "mining", "submitblock", &submitblock, true },
2014-07-15 21:38:52 +02:00
/* Coin generation */
2015-04-12 17:56:44 +02:00
{ "generating", "getgenerate", &getgenerate, true },
{ "generating", "setgenerate", &setgenerate, true },
{ "generating", "generate", &generate, true },
2014-03-26 12:26:43 +01:00
/* Raw transactions */
2015-04-12 17:56:44 +02:00
{ "rawtransactions", "createrawtransaction", &createrawtransaction, true },
{ "rawtransactions", "decoderawtransaction", &decoderawtransaction, true },
{ "rawtransactions", "decodescript", &decodescript, true },
{ "rawtransactions", "getrawtransaction", &getrawtransaction, true },
{ "rawtransactions", "sendrawtransaction", &sendrawtransaction, false },
{ "rawtransactions", "signrawtransaction", &signrawtransaction, false }, /* uses wallet if enabled */
2015-04-24 18:27:30 -07:00
#ifdef ENABLE_WALLET
{ "rawtransactions", "fundrawtransaction", &fundrawtransaction, false },
#endif
2014-03-26 12:26:43 +01:00
/* Utility functions */
2015-04-12 17:56:44 +02:00
{ "util", "createmultisig", &createmultisig, true },
{ "util", "validateaddress", &validateaddress, true }, /* uses wallet if enabled */
{ "util", "verifymessage", &verifymessage, true },
{ "util", "estimatefee", &estimatefee, true },
{ "util", "estimatepriority", &estimatepriority, true },
{ "util", "estimatesmartfee", &estimatesmartfee, true },
{ "util", "estimatesmartpriority", &estimatesmartpriority, true },
2016-05-06 14:35:21 +01:00
{ "util", "createaddress", &createaddress, true },
2013-12-08 15:26:08 +01:00
2014-11-26 16:33:18 +01:00
/* Not shown in help */
2015-04-12 17:56:44 +02:00
{ "hidden", "invalidateblock", &invalidateblock, true },
{ "hidden", "reconsiderblock", &reconsiderblock, true },
{ "hidden", "setmocktime", &setmocktime, true },
#ifdef ENABLE_WALLET
2015-04-12 17:56:44 +02:00
{ "hidden", "resendwallettransactions", &resendwallettransactions, true},
#endif
2014-11-26 16:33:18 +01:00
2013-12-08 15:26:08 +01:00
#ifdef ENABLE_WALLET
/* Wallet */
2015-04-12 17:56:44 +02:00
{ "wallet", "addmultisigaddress", &addmultisigaddress, true },
{ "wallet", "backupwallet", &backupwallet, true },
{ "wallet", "dumpprivkey", &dumpprivkey, true },
{ "wallet", "dumpwallet", &dumpwallet, true },
{ "wallet", "encryptwallet", &encryptwallet, true },
{ "wallet", "getaccountaddress", &getaccountaddress, true },
{ "wallet", "getaccount", &getaccount, true },
{ "wallet", "getaddressesbyaccount", &getaddressesbyaccount, true },
{ "wallet", "getbalance", &getbalance, false },
{ "wallet", "getnewaddress", &getnewaddress, true },
{ "wallet", "getrawchangeaddress", &getrawchangeaddress, true },
{ "wallet", "getreceivedbyaccount", &getreceivedbyaccount, false },
{ "wallet", "getreceivedbyaddress", &getreceivedbyaddress, false },
{ "wallet", "gettransaction", &gettransaction, false },
2016-01-07 16:31:12 -05:00
{ "wallet", "abandontransaction", &abandontransaction, false },
2015-04-12 17:56:44 +02:00
{ "wallet", "getunconfirmedbalance", &getunconfirmedbalance, false },
{ "wallet", "getwalletinfo", &getwalletinfo, false },
{ "wallet", "importprivkey", &importprivkey, true },
{ "wallet", "importwallet", &importwallet, true },
{ "wallet", "importaddress", &importaddress, true },
{ "wallet", "importpubkey", &importpubkey, true },
2015-04-12 17:56:44 +02:00
{ "wallet", "keypoolrefill", &keypoolrefill, true },
{ "wallet", "listaccounts", &listaccounts, false },
{ "wallet", "listaddressgroupings", &listaddressgroupings, false },
{ "wallet", "listlockunspent", &listlockunspent, false },
{ "wallet", "listreceivedbyaccount", &listreceivedbyaccount, false },
{ "wallet", "listreceivedbyaddress", &listreceivedbyaddress, false },
{ "wallet", "listsinceblock", &listsinceblock, false },
{ "wallet", "listtransactions", &listtransactions, false },
{ "wallet", "listunspent", &listunspent, false },
{ "wallet", "lockunspent", &lockunspent, true },
{ "wallet", "move", &movecmd, false },
{ "wallet", "sendfrom", &sendfrom, false },
{ "wallet", "sendmany", &sendmany, false },
{ "wallet", "sendtoaddress", &sendtoaddress, false },
{ "wallet", "setaccount", &setaccount, true },
{ "wallet", "settxfee", &settxfee, true },
{ "wallet", "signmessage", &signmessage, true },
{ "wallet", "walletlock", &walletlock, true },
{ "wallet", "walletpassphrasechange", &walletpassphrasechange, true },
{ "wallet", "walletpassphrase", &walletpassphrase, true },
2013-11-29 16:04:29 +01:00
#endif // ENABLE_WALLET
2011-05-14 20:10:21 +02:00
};
2012-04-18 22:42:17 +02:00
CRPCTable::CRPCTable()
2011-05-14 20:10:21 +02:00
{
unsigned int vcidx;
for (vcidx = 0; vcidx < (sizeof(vRPCCommands) / sizeof(vRPCCommands[0])); vcidx++)
{
const CRPCCommand *pcmd;
2011-05-14 20:10:21 +02:00
pcmd = &vRPCCommands[vcidx];
mapCommands[pcmd->name] = pcmd;
}
}
2011-05-14 20:10:21 +02:00
2015-01-23 07:53:17 +01:00
const CRPCCommand *CRPCTable::operator[](const std::string &name) const
2012-04-18 22:42:17 +02:00
{
2017-08-02 19:53:22 +05:30
std::map<std::string, const CRPCCommand*>::const_iterator it = mapCommands.find(name);
2012-04-18 22:42:17 +02:00
if (it == mapCommands.end())
return NULL;
return (*it).second;
}
2011-05-14 20:10:21 +02:00
2018-08-10 18:07:35 +03:00
void StartRPC()
2011-05-14 20:10:21 +02:00
{
2015-01-23 07:53:17 +01:00
LogPrint("rpc", "Starting RPC\n");
2012-05-13 04:43:24 +00:00
fRPCRunning = true;
g_rpcSignals.Started();
2013-03-06 22:31:26 -05:00
}
2015-01-23 07:53:17 +01:00
void InterruptRPC()
{
2019-06-24 19:49:11 +02:00
if (fRPCRunning)
logInfo(Log::RPC) << "Interrupting RPC";
2015-01-23 07:53:17 +01:00
// Interrupt e.g. running longpolls
2012-05-13 04:43:24 +00:00
fRPCRunning = false;
2015-01-23 07:53:17 +01:00
}
2013-03-06 22:31:26 -05:00
2015-01-23 07:53:17 +01:00
void StopRPC()
{
LogPrint("rpc", "Stopping RPC\n");
deadlineTimers.clear();
g_rpcSignals.Stopped();
2012-04-14 20:35:58 -04:00
}
2012-05-13 04:43:24 +00:00
bool IsRPCRunning()
{
return fRPCRunning;
}
2014-10-29 18:08:31 +01:00
void SetRPCWarmupStatus(const std::string& newStatus)
{
LOCK(cs_rpcWarmup);
rpcWarmupStatus = newStatus;
}
void SetRPCWarmupFinished()
{
LOCK(cs_rpcWarmup);
assert(fRPCInWarmup);
fRPCInWarmup = false;
}
bool RPCIsInWarmup(std::string *outStatus)
{
LOCK(cs_rpcWarmup);
if (outStatus)
*outStatus = rpcWarmupStatus;
return fRPCInWarmup;
}
void JSONRequest::parse(const UniValue& valRequest)
{
// Parse request
if (!valRequest.isObject())
2012-10-04 09:34:44 +02:00
throw JSONRPCError(RPC_INVALID_REQUEST, "Invalid Request object");
const UniValue& request = valRequest.get_obj();
// Parse id now so errors from here on will have the id
id = find_value(request, "id");
// Parse method
2015-05-13 21:29:19 +02:00
UniValue valMethod = find_value(request, "method");
if (valMethod.isNull())
2012-10-04 09:34:44 +02:00
throw JSONRPCError(RPC_INVALID_REQUEST, "Missing method");
if (!valMethod.isStr())
2012-10-04 09:34:44 +02:00
throw JSONRPCError(RPC_INVALID_REQUEST, "Method must be a string");
strMethod = valMethod.get_str();
2014-04-28 12:52:32 +02:00
if (strMethod != "getblocktemplate")
2017-08-31 12:33:47 +02:00
logInfo(Log::RPC) << "Method:" << SanitizeString(strMethod);
// Parse params
2015-05-13 21:29:19 +02:00
UniValue valParams = find_value(request, "params");
if (valParams.isArray())
params = valParams.get_array();
else if (valParams.isNull())
params = UniValue(UniValue::VARR);
else
2012-10-04 09:34:44 +02:00
throw JSONRPCError(RPC_INVALID_REQUEST, "Params must be an array");
}
static UniValue JSONRPCExecOne(const UniValue& req)
{
UniValue rpc_result(UniValue::VOBJ);
JSONRequest jreq;
try {
jreq.parse(req);
2015-05-13 21:29:19 +02:00
UniValue result = tableRPC.execute(jreq.strMethod, jreq.params);
rpc_result = JSONRPCReplyObj(result, NullUniValue, jreq.id);
}
catch (const UniValue& objError)
{
rpc_result = JSONRPCReplyObj(NullUniValue, objError, jreq.id);
}
2014-12-07 13:29:06 +01:00
catch (const std::exception& e)
{
rpc_result = JSONRPCReplyObj(NullUniValue,
2012-10-04 09:34:44 +02:00
JSONRPCError(RPC_PARSE_ERROR, e.what()), jreq.id);
}
return rpc_result;
}
2015-01-23 07:53:17 +01:00
std::string JSONRPCExecBatch(const UniValue& vReq)
{
2015-06-02 11:41:00 +02:00
UniValue ret(UniValue::VARR);
for (unsigned int reqIdx = 0; reqIdx < vReq.size(); reqIdx++)
ret.push_back(JSONRPCExecOne(vReq[reqIdx]));
return ret.write() + "\n";
}
2015-05-13 21:29:19 +02:00
UniValue CRPCTable::execute(const std::string &strMethod, const UniValue &params) const
2012-04-09 21:07:25 +02:00
{
// Return immediately if in warmup
{
LOCK(cs_rpcWarmup);
if (fRPCInWarmup)
throw JSONRPCError(RPC_IN_WARMUP, rpcWarmupStatus);
}
2012-04-09 21:07:25 +02:00
// Find method
const CRPCCommand *pcmd = tableRPC[strMethod];
if (!pcmd)
2012-10-04 09:34:44 +02:00
throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Method not found");
2011-05-14 20:10:21 +02:00
g_rpcSignals.PreCommand(*pcmd);
2012-04-09 21:07:25 +02:00
try
{
// Execute
return pcmd->actor(params, false);
2012-04-09 21:07:25 +02:00
}
2014-12-07 13:29:06 +01:00
catch (const std::exception& e)
2012-04-09 21:07:25 +02:00
{
2012-10-04 09:34:44 +02:00
throw JSONRPCError(RPC_MISC_ERROR, e.what());
2012-04-09 21:07:25 +02:00
}
g_rpcSignals.PostCommand(*pcmd);
2012-04-09 21:07:25 +02:00
}
2011-05-14 20:10:21 +02:00
2015-05-31 15:36:44 +02:00
std::string HelpExampleCli(const std::string& methodname, const std::string& args)
{
return "> bitcoin-cli " + methodname + " " + args + "\n";
}
2015-05-31 15:36:44 +02:00
std::string HelpExampleRpc(const std::string& methodname, const std::string& args)
{
return "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", \"id\":\"curltest\", "
"\"method\": \"" + methodname + "\", \"params\": [" + args + "] }' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n";
}
2015-01-23 07:53:17 +01:00
void RPCRegisterTimerInterface(RPCTimerInterface *iface)
{
timerInterfaces.push_back(iface);
}
void RPCUnregisterTimerInterface(RPCTimerInterface *iface)
{
std::vector<RPCTimerInterface*>::iterator i = std::find(timerInterfaces.begin(), timerInterfaces.end(), iface);
assert(i != timerInterfaces.end());
timerInterfaces.erase(i);
}
void RPCRunLater(const std::string& name, boost::function<void(void)> func, int64_t nSeconds)
{
if (timerInterfaces.empty())
throw JSONRPCError(RPC_INTERNAL_ERROR, "No timer handler registered for RPC");
deadlineTimers.erase(name);
RPCTimerInterface* timerInterface = timerInterfaces.back();
2015-01-23 07:53:17 +01:00
LogPrint("rpc", "queue run of timer %s in %i seconds (using %s)\n", name, nSeconds, timerInterface->Name());
deadlineTimers.insert(std::make_pair(name, boost::shared_ptr<RPCTimerBase>(timerInterface->NewTimer(func, nSeconds*1000))));
2015-01-23 07:53:17 +01:00
}
const CRPCTable tableRPC;