19#include <boost/signals2/signal.hpp>
26#include <unordered_map>
39static std::map<std::string, std::unique_ptr<RPCTimerBase>>
58 std::list<RPCCommandExecutionInfo>::iterator
it;
63 {method, SteadyClock::now()});
89 return iter->second.get()->Execute(request);
124 std::string category;
126 std::vector<std::pair<std::string, const CRPCCommand *>>
vCommands;
130 std::make_pair(entry.second.front()->category + entry.first,
131 entry.second.front()));
139 for (
const std::pair<std::string, const CRPCCommand *> &
command :
148 jreq.strMethod = strMethod;
155 }
catch (
const std::exception &
e) {
157 std::string
strHelp = std::string(
e.what());
159 if (
strHelp.find(
'\n') != std::string::npos) {
163 if (category !=
pcmd->category) {
164 if (!category.empty()) {
167 category =
pcmd->category;
185 "List all commands, or get help for a specified command.\n",
188 "The command to get help on"},
201 if (
strCommand ==
"dump_all_command_conversions") {
224 "A string with the content '" +
RESULT +
"'"},
243 "Returns the total uptime of the server.\n",
246 "The number of seconds that the server has been running"},
258 "Returns details of the RPC server.\n",
266 "All active commands",
270 "Information about an active command",
273 "The name of the RPC command"},
275 "The running time in microseconds"},
279 "The complete file path to the debug log"},
291 entry.
pushKV(
"method", info.method);
294 SteadyClock::now() - info.start)});
315 {
"control",
help, },
316 {
"control",
stop, },
339 auto new_end = std::remove(it->second.begin(), it->second.end(),
pcmd);
340 if (it->second.end() !=
new_end) {
341 it->second.erase(
new_end, it->second.end());
408 const std::string &method) {
410 args.
GetArgs(
"-deprecatedrpc");
427 }
catch (
const std::exception &
e) {
438 for (
size_t i = 0; i <
vReq.size(); i++) {
442 return ret.write() +
"\n";
451 const std::vector<std::pair<std::string, bool>> &argNames) {
456 const std::vector<std::string> &keys =
in.params.getKeys();
457 const std::vector<UniValue> &values =
in.params.getValues();
458 std::unordered_map<std::string, const UniValue *>
argsIn;
459 for (
size_t i = 0; i < keys.size(); ++i) {
463 "Parameter " + keys[i] +
464 " specified multiple times");
494 if (options.exists(
fr->first)) {
496 "Parameter " +
fr->first +
497 " specified multiple times");
499 options.pushKVEnd(
fr->first, *
fr->second);
505 if (!options.empty() ||
fr !=
argsIn.end()) {
506 for (
size_t i = 0; i <
hole; ++i) {
527 if (!options.empty()) {
529 "Parameter " +
fr->first +
530 " conflicts with parameter " +
531 options.getKeys().front());
536 if (!options.empty()) {
552 " specified twice both as positional and named argument");
564 "Unknown named parameter " +
argsIn.begin()->first);
571 const std::vector<const CRPCCommand *> &commands,
573 for (
const auto &
command : commands) {
575 &
command == &commands.back())) {
618 }
catch (
const std::exception &
e) {
640 for (
const auto &values : result.
getValues()) {
668 "No timer handler registered for RPC");
675 name, std::unique_ptr<RPCTimerBase>(
#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.
std::map< std::string, std::vector< const CRPCCommand * > > mapCommands
bool removeCommand(const std::string &name, const CRPCCommand *pcmd)
std::string help(const Config &config, const std::string &name, const JSONRPCRequest &helpreq) const
std::vector< std::string > listCommands() const
Returns a list of registered commands.
UniValue execute(const Config &config, const JSONRPCRequest &request) const
Execute a method.
void appendCommand(const std::string &name, const CRPCCommand *pcmd)
Appends a CRPCCommand to the dispatch table.
UniValue dumpArgMap(const Config &config, 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
Class for registering and managing all RPC calls.
UniValue ExecuteCommand(const Config &config, const JSONRPCRequest &request) const
Attempts to execute an RPC command from the given request.
RWCollection< RPCCommandMap > commands
void RegisterCommand(std::unique_ptr< RPCCommand > command)
Register an RPC command.
virtual RPCTimerBase * NewTimer(std::function< void()> &func, int64_t millis)=0
Factory function for timers.
virtual const char * Name()=0
Implementation name.
ReadView getReadView() const
void push_back(UniValue val)
const std::string & get_str() const
const std::vector< UniValue > & getValues() 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)
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
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 std::exception thrown in command handling.
@ RPC_TYPE_ERROR
Unexpected type was passed as parameter.
@ RPC_CLIENT_NOT_CONNECTED
P2P client errors Bitcoin is not connected.
@ 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)
void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface)
Set the factory function for timer, but only, if unset.
void SetRPCWarmupFinished()
Mark warmup as done.
bool IsDeprecatedRPCEnabled(const ArgsManager &args, const std::string &method)
static RPCHelpMan uptime()
std::chrono::steady_clock SteadyClock
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.
bool RPCIsInWarmup(std::string *outStatus)
Returns the current warmup state.
static UniValue JSONRPCExecOne(const Config &config, RPCServer &rpcServer, JSONRPCRequest jreq, const UniValue &req)
static RPCTimerInterface * timerInterface
static std::atomic< bool > g_rpc_running
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...
static GlobalMutex g_deadline_timers_mutex
bool IsRPCRunning()
Query whether RPC is running.
static bool ExecuteCommands(const Config &config, const std::vector< const CRPCCommand * > &commands, const JSONRPCRequest &request, UniValue &result)
int RPCSerializationFlags()
Retrieves any serialization flags requested in command line argument.
static struct CRPCSignals g_rpcSignals
static GlobalMutex g_rpc_warmup_mutex
static bool ExecuteCommand(const Config &config, const CRPCCommand &command, const JSONRPCRequest &request, UniValue &result, bool last_handler)
static RPCServerInfo g_rpc_server_info
static const CRPCCommand vRPCCommands[]
std::string JSONRPCExecBatch(const Config &config, RPCServer &rpcServer, const JSONRPCRequest &jreq, const UniValue &vReq)
void SetRPCWarmupStatus(const std::string &newStatus)
Set the RPC warmup status.
void RPCSetTimerInterface(RPCTimerInterface *iface)
Set the factory function for timers.
void RpcInterruptionPoint()
Throw JSONRPCError if RPC is not running.
bool IsRPCRunning()
Query whether RPC is running.
void StartShutdown()
Request shutdown of the application.
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
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.
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)
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
bilingual_str _(const char *psz)
Translation function.
const UniValue NullUniValue