17 #include <boost/bind.hpp> 18 #include <boost/signals2/signal.hpp> 19 #include <boost/algorithm/string/classification.hpp> 20 #include <boost/algorithm/string/split.hpp> 23 #include <unordered_map> 26 static bool fRPCRunning =
false;
27 static bool fRPCInWarmup
GUARDED_BY(cs_rpcWarmup) =
true;
28 static std::string rpcWarmupStatus
GUARDED_BY(cs_rpcWarmup) =
"RPC server started";
32 static std::map<std::string, std::unique_ptr<RPCTimerBase> > deadlineTimers;
34 static struct CRPCSignals
36 boost::signals2::signal<void ()> Started;
37 boost::signals2::signal<void ()> Stopped;
38 boost::signals2::signal<void (const CRPCCommand&)> PreCommand;
43 g_rpcSignals.Started.connect(slot);
48 g_rpcSignals.Stopped.connect(slot);
52 const std::list<UniValueType>& typesExpected,
57 if (params.
size() <= i)
61 if (!(fAllowNull && v.
isNull())) {
76 const std::map<std::string, UniValueType>& typesExpected,
80 for (
const auto& t : typesExpected) {
82 if (!fAllowNull && v.
isNull())
85 if (!(t.second.typeAny || v.
type() == t.second.type || (fAllowNull && v.
isNull()))) {
86 std::string err =
strprintf(
"Expected type %s for %s, got %s",
94 for (
const std::string& k : o.
getKeys())
96 if (typesExpected.count(k) == 0)
98 std::string err =
strprintf(
"Unexpected key %s", k);
119 std::string strHex(v.
get_str());
120 if (64 != strHex.length())
151 std::string category;
152 std::set<rpcfn_type> setDone;
153 std::vector<std::pair<std::string, const CRPCCommand*> > vCommands;
156 vCommands.push_back(make_pair(entry.second->category + entry.first, entry.second));
157 sort(vCommands.begin(), vCommands.end());
163 for (
const std::pair<std::string, const CRPCCommand*>& command : vCommands)
166 std::string strMethod = pcmd->
name;
167 if ((strCommand !=
"" || pcmd->
category ==
"hidden") && strMethod != strCommand)
169 jreq.strMethod = strMethod;
173 if (setDone.insert(pfn).second)
176 catch (
const std::exception& e)
179 std::string strHelp = std::string(e.what());
180 if (strCommand ==
"")
182 if (strHelp.find(
'\n') != std::string::npos)
183 strHelp = strHelp.substr(0, strHelp.find(
'\n'));
187 if (!category.empty())
190 strRet +=
"== " +
Capitalize(category) +
" ==\n";
193 strRet += strHelp +
"\n";
197 strRet =
strprintf(
"help: unknown command: %s\n", strCommand);
198 strRet = strRet.substr(0,strRet.size()-1);
205 throw std::runtime_error(
206 "help ( \"command\" )\n" 207 "\nList all commands, or get help for a specified command.\n" 209 "1. \"command\" (string, optional) The command to get help on\n" 211 "\"text\" (string) The help text\n" 214 std::string strCommand;
226 throw std::runtime_error(
228 "\nStop BSHA3 server.");
232 return "BSHA3 server stopping";
238 throw std::runtime_error(
240 "\nReturns the total uptime of the server.\n" 242 "ttt (numeric) The number of seconds that the server has been running\n" 259 {
"control",
"help", &
help, {
"command"} },
260 {
"control",
"stop", &
stop, {} },
261 {
"control",
"uptime", &uptime, {} },
268 for (vcidx = 0; vcidx < (
sizeof(vRPCCommands) /
sizeof(vRPCCommands[0])); vcidx++)
272 pcmd = &vRPCCommands[vcidx];
279 std::map<std::string, const CRPCCommand*>::const_iterator it =
mapCommands.find(
name);
291 std::map<std::string, const CRPCCommand*>::const_iterator it =
mapCommands.find(
name);
303 g_rpcSignals.Started();
316 deadlineTimers.clear();
318 g_rpcSignals.Stopped();
329 rpcWarmupStatus = newStatus;
335 assert(fRPCInWarmup);
336 fRPCInWarmup =
false;
343 *outStatus = rpcWarmupStatus;
361 if (!valMethod.
isStr())
374 else if (valParams.
isNull())
382 const std::vector<std::string> enabled_methods =
gArgs.
GetArgs(
"-deprecatedrpc");
384 return find(enabled_methods.begin(), enabled_methods.end(), method) != enabled_methods.end();
401 catch (
const std::exception& e)
413 for (
unsigned int reqIdx = 0; reqIdx < vReq.
size(); reqIdx++)
431 std::unordered_map<std::string, const UniValue*> argsIn;
432 for (
size_t i=0; i<keys.size(); ++i) {
433 argsIn[keys[i]] = &values[i];
437 for (
const std::string &argNamePattern: argNames) {
438 std::vector<std::string> vargNames;
439 boost::algorithm::split(vargNames, argNamePattern, boost::algorithm::is_any_of(
"|"));
440 auto fr = argsIn.end();
441 for (
const std::string & argName : vargNames) {
442 fr = argsIn.find(argName);
443 if (fr != argsIn.end()) {
447 if (fr != argsIn.end()) {
448 for (
int i = 0; i < hole; ++i) {
462 if (!argsIn.empty()) {
483 g_rpcSignals.PreCommand(*pcmd);
489 return pcmd->
actor(transformNamedArguments(request, pcmd->
argNames));
491 return pcmd->
actor(request);
494 catch (
const std::exception& e)
502 std::vector<std::string> commandList;
503 typedef std::map<std::string, const CRPCCommand*> commandMap;
506 std::back_inserter(commandList),
507 boost::bind(&commandMap::value_type::first,_1) );
511 std::string
HelpExampleCli(
const std::string& methodname,
const std::string& args)
513 return "> bsha3-cli " + methodname +
" " + args +
"\n";
516 std::string
HelpExampleRpc(
const std::string& methodname,
const std::string& args)
518 return "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", \"id\":\"curltest\", " 519 "\"method\": \"" + methodname +
"\", \"params\": [" + args +
"] }' -H 'content-type: text/plain;' http://127.0.0.1:8334/\n";
525 timerInterface = iface;
530 timerInterface = iface;
535 if (timerInterface == iface)
536 timerInterface =
nullptr;
539 void RPCRunLater(
const std::string&
name, std::function<
void()> func, int64_t nSeconds)
543 deadlineTimers.erase(
name);
544 LogPrint(
BCLog::RPC,
"queue run of timer %s in %i seconds (using %s)\n",
name, nSeconds, timerInterface->
Name());
545 deadlineTimers.emplace(
name, std::unique_ptr<RPCTimerBase>(timerInterface->
NewTimer(func, nSeconds*1000)));
551 if (
gArgs.
GetArg(
"-rpcserialversion", DEFAULT_RPC_SERIALIZE_VERSION) == 0)
552 flag |= SERIALIZE_TRANSACTION_NO_WITNESS;
void RPCTypeCheckObj(const UniValue &o, const std::map< std::string, UniValueType > &typesExpected, bool fAllowNull, bool fStrict)
uint256 ParseHashO(const UniValue &o, std::string strKey)
std::vector< unsigned char > ParseHexO(const UniValue &o, std::string strKey)
std::string help(const std::string &name, const JSONRPCRequest &helpreq) const
Note: This interface may still be subject to change.
const std::vector< UniValue > & getValues() const
Bitcoin RPC command dispatcher.
bool IsRPCRunning()
Query whether RPC is running.
void SetRPCWarmupStatus(const std::string &newStatus)
Set the RPC warmup status.
UniValue ret(UniValue::VARR)
int64_t GetStartupTime()
Server/client environment: argument handling, config file parsing, thread wrappers, startup time.
bool MoneyRange(const CAmount &nValue)
void OnStopped(std::function< void()> slot)
void DeleteAuthCookie()
Delete RPC authentication cookie from disk.
const std::string & get_str() const
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
const std::vector< std::string > & getKeys() const
void RPCRunLater(const std::string &name, std::function< void()> func, int64_t nSeconds)
Run func nSeconds from now.
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
const std::string & getValStr() const
bool appendCommand(const std::string &name, const CRPCCommand *pcmd)
Appends a CRPCCommand to the dispatch table.
std::vector< std::string > argNames
Invalid, missing or duplicate parameter.
uint256 ParseHashV(const UniValue &v, std::string strName)
Utilities: convert hex-encoded Values (throws error if not hex).
void RPCUnsetTimerInterface(RPCTimerInterface *iface)
Unset factory function for timers.
const UniValue & find_value(const UniValue &obj, const std::string &name)
int64_t CAmount
Amount in satoshis (Can be negative)
UniValue execute(const JSONRPCRequest &request) const
Execute a method.
const CRPCCommand * operator[](const std::string &name) const
void SetRPCWarmupFinished()
const char * uvTypeName(UniValue::VType t)
bool push_back(const UniValue &val)
uint256 uint256S(const char *str)
bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
Parse number as fixed point according to JSON number syntax.
std::string JSONRPCExecBatch(const JSONRPCRequest &jreq, const UniValue &vReq)
bool IsHex(const std::string &str)
Unexpected type was passed as parameter.
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
General application defined errors.
std::map< std::string, const CRPCCommand * > mapCommands
void RPCTypeCheck(const UniValue ¶ms, const std::list< UniValueType > &typesExpected, bool fAllowNull)
Type-check arguments; throws JSONRPCError if wrong type given.
UniValue help(const JSONRPCRequest &jsonRequest)
std::string HelpExampleCli(const std::string &methodname, const std::string &args)
CAmount AmountFromValue(const UniValue &value)
bool IsDeprecatedRPCEnabled(const std::string &method)
void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface)
Set the factory function for timer, but only, if unset.
std::vector< unsigned char > ParseHexV(const UniValue &v, std::string strName)
virtual RPCTimerBase * NewTimer(std::function< void()> &func, int64_t millis)=0
Factory function for timers.
void parse(const UniValue &valRequest)
int RPCSerializationFlags()
const UniValue & get_obj() const
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
void RPCTypeCheckArgument(const UniValue &value, const UniValueType &typeExpected)
Type-check one argument; throws JSONRPCError if wrong type given.
void RPCSetTimerInterface(RPCTimerInterface *iface)
Set the factory function for timers.
bool RPCIsInWarmup(std::string *outStatus)
const UniValue NullUniValue
Standard JSON-RPC 2.0 errors.
virtual const char * Name()=0
Implementation name.
UniValue JSONRPCError(int code, const std::string &message)
void OnStarted(std::function< void()> slot)
int64_t GetTime()
GetTimeMicros() and GetTimeMillis() both return the system time, but in different units...
std::vector< std::string > GetArgs(const std::string &strArg) const
Return a vector of strings of the given argument.
UniValue JSONRPCReplyObj(const UniValue &result, const UniValue &error, const UniValue &id)
std::vector< std::string > listCommands() const
Returns a list of registered commands.
std::string SanitizeString(const std::string &str, int rule)
Remove unsafe chars.
UniValue(* rpcfn_type)(const JSONRPCRequest &jsonRequest)
Wrapper for UniValue::VType, which includes typeAny: Used to denote don't care type.
UniValue stop(const JSONRPCRequest &jsonRequest)
std::vector< unsigned char > ParseHex(const char *psz)