18 #include <boost/signals2/signal.hpp> 24 #include <unordered_map> 53 std::list<RPCCommandExecutionInfo>::iterator
it;
86 std::set<intptr_t> setDone;
87 std::vector<std::pair<std::string, const CRPCCommand*> > vCommands;
91 vCommands.emplace_back(entry.second.front()->category + entry.first, entry.second.front());
92 sort(vCommands.begin(), vCommands.end());
98 for (
const std::pair<std::string, const CRPCCommand*>&
command : vCommands)
101 std::string strMethod = pcmd->
name;
102 if ((strCommand !=
"" || pcmd->
category ==
"hidden") && strMethod != strCommand)
104 jreq.strMethod = strMethod;
108 if (setDone.insert(pcmd->
unique_id).second)
109 pcmd->
actor(jreq, unused_result,
true);
111 catch (
const std::exception& e)
114 std::string strHelp = std::string(e.what());
115 if (strCommand ==
"")
117 if (strHelp.find(
'\n') != std::string::npos)
118 strHelp = strHelp.substr(0, strHelp.find(
'\n'));
122 if (!category.empty())
125 strRet +=
"== " +
Capitalize(category) +
" ==\n";
128 strRet += strHelp +
"\n";
132 strRet =
strprintf(
"help: unknown command: %s\n", strCommand);
133 strRet = strRet.substr(0,strRet.size()-1);
140 "\nList all commands, or get help for a specified command.\n",
151 std::string strCommand;
152 if (jsonRequest.params.size() > 0) {
153 strCommand = jsonRequest.params[0].
get_str();
155 if (strCommand ==
"dump_all_command_conversions") {
167 static const std::string RESULT{
PACKAGE_NAME " stopping"};
183 if (jsonRequest.params[0].isNum()) {
194 "\nReturns the total uptime of the server.\n",
213 "\nReturns details of the RPC server.\n",
239 entry.
pushKV(
"duration", int64_t{Ticks<std::chrono::microseconds>(SteadyClock::now() - info.
start)});
244 result.
pushKV(
"active_commands", active_commands);
248 result.
pushKV(
"logpath", log_path);
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 auto [
_, inserted] = argsIn.emplace(keys[i], &
values[i]);
419 int initial_hole_size = 0;
420 const std::string* initial_param =
nullptr;
422 for (
const auto& [argNamePattern, named_only]: argNames) {
423 std::vector<std::string> vargNames =
SplitString(argNamePattern,
'|');
424 auto fr = argsIn.end();
425 for (
const std::string & argName : vargNames) {
426 fr = argsIn.find(argName);
427 if (fr != argsIn.end()) {
436 if (fr != argsIn.end()) {
437 if (options.exists(fr->first)) {
440 options.pushKVEnd(fr->first, *fr->second);
446 if (!options.empty() || fr != argsIn.end()) {
447 for (
int i = 0; i < hole; ++i) {
454 if (!initial_param) initial_param = &argNamePattern;
457 if (
out.params.empty()) initial_hole_size = hole;
463 if (fr != argsIn.end()) {
464 if (!options.empty()) {
467 out.params.push_back(*fr->second);
470 if (!options.empty()) {
471 out.params.push_back(std::move(options));
479 auto positional_args{argsIn.extract(
"args")};
480 if (positional_args && positional_args.mapped()->isArray()) {
481 if (initial_hole_size < (
int)positional_args.mapped()->size() && initial_param) {
486 out.params = *positional_args.mapped();
487 for (
size_t i{
out.params.size()}; i < named_args.size(); ++i) {
488 out.params.push_back(named_args[i]);
492 if (!argsIn.empty()) {
501 for (
const auto&
command : commands) {
537 return command.actor(request, result, last_handler);
541 }
catch (
const std::exception& e) {
548 std::vector<std::string> commandList;
550 for (
const auto& i :
mapCommands) commandList.emplace_back(i.first);
588 void RPCRunLater(
const std::string&
name, std::function<
void()> func, int64_t nSeconds)
593 deadlineTimers.erase(
name);
void push_back(UniValue val)
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...
std::string help(const std::string &name, const JSONRPCRequest &helpreq) const
const std::vector< UniValue > & getValues() const
BCLog::Logger & LogInstance()
static GlobalMutex g_deadline_timers_mutex
#define LogPrint(category,...)
bool IsRPCRunning()
Query whether RPC is running.
void SetRPCWarmupStatus(const std::string &newStatus)
Set the RPC warmup status.
static const unsigned int DEFAULT_RPC_SERIALIZE_VERSION
static bool ExecuteCommands(const std::vector< const CRPCCommand *> &commands, const JSONRPCRequest &request, UniValue &result)
bool hidden
For testing only.
void appendCommand(const std::string &name, const CRPCCommand *pcmd)
Appends a CRPCCommand to the dispatch table.
static bool fRPCInWarmup GUARDED_BY(g_rpc_warmup_mutex)
#define CHECK_NONFATAL(condition)
Identity function.
static std::atomic< bool > g_rpc_running
void OnStopped(std::function< void()> slot)
static GlobalMutex g_rpc_warmup_mutex
bool removeCommand(const std::string &name, const CRPCCommand *pcmd)
const std::string & get_str() const
static const int64_t values[]
A selection of numbers that do not trigger int64_t overflow when added/subtracted.
const std::vector< std::string > & getKeys() const
static const CRPCCommand vRPCCommands[]
void RPCRunLater(const std::string &name, std::function< void()> func, int64_t nSeconds)
Run func nSeconds from now.
std::vector< std::string > SplitString(std::string_view str, char sep)
Invalid, missing or duplicate parameter.
static RPCServerInfo g_rpc_server_info
void RPCUnsetTimerInterface(RPCTimerInterface *iface)
Unset factory function for timers.
void DeleteAuthCookie()
Delete RPC authentication cookie from disk.
static bool ExecuteCommand(const CRPCCommand &command, const JSONRPCRequest &request, UniValue &result, bool last_handler)
UniValue execute(const JSONRPCRequest &request) const
Execute a method.
boost::signals2::signal< void()> Stopped
void SetRPCWarmupFinished()
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
UniValue JSONRPCError(int code, const std::string &message)
static JSONRPCRequest transformNamedArguments(const JSONRPCRequest &in, const std::vector< std::pair< std::string, bool >> &argNames)
Process named arguments into a vector of positional arguments, based on the passed-in specification f...
enum JSONRPCRequest::Mode mode
bilingual_str _(const char *psz)
Translation function.
static RPCTimerInterface * timerInterface
boost::signals2::signal< void()> Started
std::string JSONRPCExecBatch(const JSONRPCRequest &jreq, const UniValue &vReq)
Unexpected type was passed as parameter.
Special type to disable type checks (for testing only)
General application defined errors.
std::string DefaultHint
Hint for default value.
RPCCommandExecution(const std::string &method)
std::string HelpExampleCli(const std::string &methodname, const std::string &args)
bool IsDeprecatedRPCEnabled(const std::string &method)
static UniValue JSONRPCExecOne(JSONRPCRequest jreq, const UniValue &req)
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
static struct CRPCSignals g_rpcSignals
void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface)
Set the factory function for timer, but only, if unset.
virtual RPCTimerBase * NewTimer(std::function< void()> &func, int64_t millis)=0
Factory function for timers.
Optional argument for which the default value is omitted from help text for one of two reasons: ...
void parse(const UniValue &valRequest)
void UninterruptibleSleep(const std::chrono::microseconds &n)
void pushKV(std::string key, UniValue val)
int RPCSerializationFlags()
void RpcInterruptionPoint()
Throw JSONRPCError if RPC is not running.
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
static RPCHelpMan getrpcinfo()
void RPCSetTimerInterface(RPCTimerInterface *iface)
Set the factory function for timers.
UniValue JSONRPCReplyObj(const UniValue &result, const UniValue &error, const UniValue &id)
std::list< RPCCommandExecutionInfo >::iterator it
std::string u8string() const
bool RPCIsInWarmup(std::string *outStatus)
const UniValue NullUniValue
void StartShutdown()
Request shutdown of the application.
virtual const char * Name()=0
Implementation name.
void OnStarted(std::function< void()> slot)
std::map< std::string, std::vector< const CRPCCommand * > > mapCommands
Different type to mark Mutex at global scope.
std::list< RPCCommandExecutionInfo > active_commands GUARDED_BY(mutex)
std::vector< std::string > GetArgs(const std::string &strArg) const
Return a vector of strings of the given argument.
SteadyClock::time_point start
int64_t GetTime()
DEPRECATED, see GetTime.
std::vector< std::string > listCommands() const
Returns a list of registered commands.
UniValue dumpArgMap(const JSONRPCRequest &request) const
Return all named arguments that need to be converted by the client from string to another JSON type...
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
static RPCHelpMan uptime()