16 #include <boost/signals2/signal.hpp>
22 #include <unordered_map>
51 std::list<RPCCommandExecutionInfo>::iterator
it;
84 std::set<intptr_t> setDone;
85 std::vector<std::pair<std::string, const CRPCCommand*> > vCommands;
88 vCommands.push_back(make_pair(entry.second.front()->category + entry.first, entry.second.front()));
89 sort(vCommands.begin(), vCommands.end());
95 for (
const std::pair<std::string, const CRPCCommand*>&
command : vCommands)
98 std::string strMethod = pcmd->
name;
99 if ((strCommand !=
"" || pcmd->
category ==
"hidden") && strMethod != strCommand)
105 if (setDone.insert(pcmd->
unique_id).second)
106 pcmd->
actor(jreq, unused_result,
true);
108 catch (
const std::exception& e)
111 std::string strHelp = std::string(e.what());
112 if (strCommand ==
"")
114 if (strHelp.find(
'\n') != std::string::npos)
115 strHelp = strHelp.substr(0, strHelp.find(
'\n'));
119 if (!category.empty())
122 strRet +=
"== " +
Capitalize(category) +
" ==\n";
125 strRet += strHelp +
"\n";
129 strRet =
strprintf(
"help: unknown command: %s\n", strCommand);
130 strRet = strRet.substr(0,strRet.size()-1);
137 "\nList all commands, or get help for a specified command.\n",
148 std::string strCommand;
149 if (jsonRequest.params.size() > 0) {
150 strCommand = jsonRequest.params[0].
get_str();
152 if (strCommand ==
"dump_all_command_conversions") {
164 static const std::string RESULT{
PACKAGE_NAME " stopping"};
180 if (jsonRequest.params[0].isNum()) {
191 "\nReturns the total uptime of the server.\n",
210 "\nReturns details of the RPC server.\n",
236 entry.
pushKV(
"duration", int64_t{Ticks<std::chrono::microseconds>(SteadyClock::now() - info.
start)});
241 result.
pushKV(
"active_commands", active_commands);
245 result.
pushKV(
"logpath", log_path);
278 auto new_end = std::remove(it->second.begin(), it->second.end(), pcmd);
279 if (it->second.end() != new_end) {
280 it->second.erase(new_end, it->second.end());
296 static std::once_flag g_rpc_interrupt_flag;
298 std::call_once(g_rpc_interrupt_flag, []() {
307 static std::once_flag g_rpc_stop_flag;
310 std::call_once(g_rpc_stop_flag, []() {
331 rpcWarmupStatus = newStatus;
338 fRPCInWarmup =
false;
345 *outStatus = rpcWarmupStatus;
351 const std::vector<std::string> enabled_methods =
gArgs.
GetArgs(
"-deprecatedrpc");
353 return find(enabled_methods.begin(), enabled_methods.end(), method) != enabled_methods.end();
370 catch (
const std::exception& e)
382 for (
unsigned int reqIdx = 0; reqIdx < vReq.
size(); reqIdx++)
385 return ret.write() +
"\n";
400 std::unordered_map<std::string, const UniValue*> argsIn;
401 for (
size_t i=0; i<keys.size(); ++i) {
402 auto [
_, inserted] = argsIn.emplace(keys[i], &
values[i]);
416 int initial_hole_size = 0;
417 for (
const std::string &argNamePattern: argNames) {
418 std::vector<std::string> vargNames =
SplitString(argNamePattern,
'|');
419 auto fr = argsIn.end();
420 for (
const std::string & argName : vargNames) {
421 fr = argsIn.find(argName);
422 if (fr != argsIn.end()) {
426 if (fr != argsIn.end()) {
427 for (
int i = 0; i < hole; ++i) {
445 auto positional_args{argsIn.extract(
"args")};
446 if (positional_args && positional_args.mapped()->isArray()) {
447 const bool has_named_arguments{initial_hole_size < (int)argNames.size()};
448 if (initial_hole_size < (
int)positional_args.mapped()->size() && has_named_arguments) {
453 out.
params = *positional_args.mapped();
454 for (
size_t i{out.
params.
size()}; i < named_args.size(); ++i) {
459 if (!argsIn.empty()) {
468 for (
const auto&
command : commands) {
504 return command.actor(request, result, last_handler);
508 }
catch (
const std::exception& e) {
515 std::vector<std::string> commandList;
516 for (
const auto& i :
mapCommands) commandList.emplace_back(i.first);
554 void RPCRunLater(
const std::string&
name, std::function<
void()> func, int64_t nSeconds)
559 deadlineTimers.erase(
name);
#define CHECK_NONFATAL(condition)
Identity function.
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::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.
Different type to mark Mutex at global scope.
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.
void push_back(UniValue val)
const std::string & get_str() const
const std::vector< UniValue > & getValues() const
const std::vector< std::string > & getKeys() const
void pushKV(std::string key, 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_TYPE_ERROR
Unexpected type was passed as parameter.
@ 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()
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
static GlobalMutex g_deadline_timers_mutex
bool IsRPCRunning()
Query whether RPC is running.
std::string JSONRPCExecBatch(const JSONRPCRequest &jreq, const UniValue &vReq)
static UniValue JSONRPCExecOne(JSONRPCRequest jreq, const UniValue &req)
int RPCSerializationFlags()
static struct CRPCSignals g_rpcSignals
static bool fRPCInWarmup GUARDED_BY(g_rpc_warmup_mutex)
static GlobalMutex 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::vector< std::string > SplitString(std::string_view str, char sep)
boost::signals2::signal< void()> Started
boost::signals2::signal< void()> Stopped
std::string DefaultHint
Hint for default value.
@ OMITTED
Optional argument for which the default value is omitted from help text for one of two reasons:
bool hidden
For testing only.
RPCCommandExecution(const std::string &method)
std::list< RPCCommandExecutionInfo >::iterator it
SteadyClock::time_point start
@ 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.
void UninterruptibleSleep(const std::chrono::microseconds &n)
bilingual_str _(const char *psz)
Translation function.
const UniValue NullUniValue
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.