14#include <boost/algorithm/string/classification.hpp>
15#include <boost/algorithm/string/split.hpp>
16#include <boost/signals2/signal.hpp>
21#include <unordered_map>
50 std::list<RPCCommandExecutionInfo>::iterator
it;
83 std::set<intptr_t> setDone;
84 std::vector<std::pair<std::string, const CRPCCommand*> > vCommands;
87 vCommands.push_back(make_pair(entry.second.front()->category + entry.first, entry.second.front()));
88 sort(vCommands.begin(), vCommands.end());
94 for (
const std::pair<std::string, const CRPCCommand*>& command : vCommands)
97 std::string strMethod = pcmd->
name;
98 if ((strCommand !=
"" || pcmd->
category ==
"hidden") && strMethod != strCommand)
104 if (setDone.insert(pcmd->
unique_id).second)
105 pcmd->
actor(jreq, unused_result,
true );
107 catch (
const std::exception& e)
110 std::string strHelp = std::string(e.what());
111 if (strCommand ==
"")
113 if (strHelp.find(
'\n') != std::string::npos)
114 strHelp = strHelp.substr(0, strHelp.find(
'\n'));
118 if (!category.empty())
121 strRet +=
"== " +
Capitalize(category) +
" ==\n";
124 strRet += strHelp +
"\n";
128 strRet =
strprintf(
"help: unknown command: %s\n", strCommand);
129 strRet = strRet.substr(0,strRet.size()-1);
136 "\nList all commands, or get help for a specified command.\n",
147 std::string strCommand;
148 if (jsonRequest.params.size() > 0) {
149 strCommand = jsonRequest.params[0].
get_str();
151 if (strCommand ==
"dump_all_command_conversions") {
163 static const std::string RESULT{
PACKAGE_NAME " stopping"};
179 if (jsonRequest.params[0].isNum()) {
190 "\nReturns the total uptime of the server.\n",
209 "\nReturns details of the RPC server.\n",
240 result.
pushKV(
"active_commands", active_commands);
244 result.
pushKV(
"logpath", log_path);
257 {
"control", &
help, },
258 {
"control", &
stop, },
281 auto new_end = std::remove(it->second.begin(), it->second.end(), pcmd);
282 if (it->second.end() != new_end) {
283 it->second.erase(new_end, it->second.end());
299 static std::once_flag g_rpc_interrupt_flag;
301 std::call_once(g_rpc_interrupt_flag, []() {
310 static std::once_flag g_rpc_stop_flag;
313 std::call_once(g_rpc_stop_flag, []() {
334 rpcWarmupStatus = newStatus;
341 fRPCInWarmup =
false;
348 *outStatus = rpcWarmupStatus;
354 const std::vector<std::string> enabled_methods =
gArgs.
GetArgs(
"-deprecatedrpc");
356 return find(enabled_methods.begin(), enabled_methods.end(), method) != enabled_methods.end();
373 catch (
const std::exception& e)
385 for (
unsigned int reqIdx = 0; reqIdx < vReq.
size(); reqIdx++)
388 return ret.
write() +
"\n";
403 std::unordered_map<std::string, const UniValue*> argsIn;
404 for (
size_t i=0; i<keys.size(); ++i) {
405 argsIn[keys[i]] = &
values[i];
409 for (
const std::string &argNamePattern: argNames) {
410 std::vector<std::string> vargNames;
411 boost::algorithm::split(vargNames, argNamePattern, boost::algorithm::is_any_of(
"|"));
412 auto fr = argsIn.end();
413 for (
const std::string & argName : vargNames) {
414 fr = argsIn.find(argName);
415 if (fr != argsIn.end()) {
419 if (fr != argsIn.end()) {
420 for (
int i = 0; i < hole; ++i) {
434 if (!argsIn.empty()) {
443 for (
const auto& command : commands) {
444 if (
ExecuteCommand(*command, request, result, &command == &commands.back())) {
480 return command.
actor(request, result, last_handler);
483 catch (
const std::exception& e)
491 std::vector<std::string> commandList;
492 for (
const auto& i :
mapCommands) commandList.emplace_back(i.first);
530void RPCRunLater(
const std::string&
name, std::function<
void()> func, int64_t nSeconds)
535 deadlineTimers.erase(
name);
#define CHECK_NONFATAL(condition)
Throw a NonFatalCheckError when the condition evaluates to false.
std::vector< std::string > GetArgs(const std::string &strArg) const
Return a vector of strings of the given argument.
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
std::vector< std::string > argNames
std::map< std::string, std::vector< const CRPCCommand * > > mapCommands
bool removeCommand(const std::string &name, const CRPCCommand *pcmd)
std::vector< std::string > listCommands() const
Returns a list of registered commands.
UniValue execute(const JSONRPCRequest &request) const
Execute a method.
void appendCommand(const std::string &name, const CRPCCommand *pcmd)
Appends a CRPCCommand to the dispatch table.
std::string help(const std::string &name, const JSONRPCRequest &helpreq) const
UniValue dumpArgMap(const JSONRPCRequest &request) const
Return all named arguments that need to be converted by the client from string to another JSON type.
enum JSONRPCRequest::Mode mode
void parse(const UniValue &valRequest)
virtual RPCTimerBase * NewTimer(std::function< void()> &func, int64_t millis)=0
Factory function for timers.
virtual const char * Name()=0
Implementation name.
const std::string & get_str() const
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
const std::vector< UniValue > & getValues() const
const std::vector< std::string > & getKeys() const
bool push_back(const UniValue &val)
bool pushKV(const std::string &key, const UniValue &val)
std::string u8string() const
BCLog::Logger & LogInstance()
#define LogPrint(category,...)
void OnStarted(std::function< void()> slot)
void OnStopped(std::function< void()> slot)
static const int SERIALIZE_TRANSACTION_NO_WITNESS
A flag that is ORed into the protocol version to designate that a transaction should be (un)serialize...
UniValue JSONRPCError(int code, const std::string &message)
void DeleteAuthCookie()
Delete RPC authentication cookie from disk.
UniValue JSONRPCReplyObj(const UniValue &result, const UniValue &error, const UniValue &id)
@ RPC_MISC_ERROR
General application defined errors.
@ RPC_CLIENT_NOT_CONNECTED
P2P client errors.
@ RPC_INVALID_PARAMETER
Invalid, missing or duplicate parameter.
@ RPC_IN_WARMUP
Client still warming up.
std::string HelpExampleCli(const std::string &methodname, const std::string &args)
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
static const int64_t values[]
A selection of numbers that do not trigger int64_t overflow when added/subtracted.
void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface)
Set the factory function for timer, but only, if unset.
bool IsDeprecatedRPCEnabled(const std::string &method)
void SetRPCWarmupFinished()
static RPCHelpMan uptime()
void RPCUnsetTimerInterface(RPCTimerInterface *iface)
Unset factory function for timers.
static RPCHelpMan getrpcinfo()
static Mutex g_rpc_warmup_mutex
void RPCRunLater(const std::string &name, std::function< void()> func, int64_t nSeconds)
Run func nSeconds from now.
static bool ExecuteCommands(const std::vector< const CRPCCommand * > &commands, const JSONRPCRequest &request, UniValue &result)
bool RPCIsInWarmup(std::string *outStatus)
static RPCTimerInterface * timerInterface
static bool ExecuteCommand(const CRPCCommand &command, const JSONRPCRequest &request, UniValue &result, bool last_handler)
static std::atomic< bool > g_rpc_running
bool IsRPCRunning()
Query whether RPC is running.
std::string JSONRPCExecBatch(const JSONRPCRequest &jreq, const UniValue &vReq)
static UniValue JSONRPCExecOne(JSONRPCRequest jreq, const UniValue &req)
static Mutex g_deadline_timers_mutex
int RPCSerializationFlags()
static struct CRPCSignals g_rpcSignals
static bool fRPCInWarmup GUARDED_BY(g_rpc_warmup_mutex)
static RPCServerInfo g_rpc_server_info
static const CRPCCommand vRPCCommands[]
void SetRPCWarmupStatus(const std::string &newStatus)
Set the RPC warmup status.
static JSONRPCRequest transformNamedArguments(const JSONRPCRequest &in, const std::vector< std::string > &argNames)
Process named arguments into a vector of positional arguments, based on the passed-in specification f...
void RPCSetTimerInterface(RPCTimerInterface *iface)
Set the factory function for timers.
void RpcInterruptionPoint()
Throw JSONRPCError if RPC is not running.
static const unsigned int DEFAULT_RPC_SERIALIZE_VERSION
void StartShutdown()
Request shutdown of the application.
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
boost::signals2::signal< void()> Started
boost::signals2::signal< void()> Stopped
@ OMITTED_NAMED_ARG
Optional arg that is a named argument and has a default value of null.
RPCCommandExecution(const std::string &method)
std::list< RPCCommandExecutionInfo >::iterator it
@ ANY
Special type to disable type checks (for testing only)
std::list< RPCCommandExecutionInfo > active_commands GUARDED_BY(mutex)
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
int64_t GetTimeMicros()
Returns the system time (not mockable)
void UninterruptibleSleep(const std::chrono::microseconds &n)
int64_t GetTime()
DEPRECATED Use either GetTimeSeconds (not mockable) or GetTime<T> (mockable)
const UniValue NullUniValue