6#include <bitcoin-build-config.h>
22#include <validation.h>
30#include <unordered_map>
56 std::list<RPCCommandExecutionInfo>::iterator
it;
73 std::set<intptr_t> setDone;
74 std::vector<std::pair<std::string, const CRPCCommand*> > vCommands;
78 vCommands.emplace_back(entry.second.front()->category + entry.first, entry.second.front());
79 std::ranges::sort(vCommands);
85 for (
const auto& [
_, pcmd] : vCommands) {
86 std::string strMethod = pcmd->name;
87 if ((strCommand !=
"" || pcmd->category ==
"hidden") && strMethod != strCommand)
93 if (setDone.insert(pcmd->unique_id).second)
94 pcmd->actor(jreq, unused_result,
true);
96 std::string strHelp{e.what()};
99 if (strHelp.find(
'\n') != std::string::npos)
100 strHelp = strHelp.substr(0, strHelp.find(
'\n'));
102 if (category != pcmd->category)
104 if (!category.empty())
106 category = pcmd->category;
107 strRet +=
"== " +
Capitalize(category) +
" ==\n";
110 strRet += strHelp +
"\n";
114 strRet =
strprintf(
"help: unknown command: %s\n", strCommand);
115 strRet = strRet.substr(0,strRet.size()-1);
123 "List all commands, or get help for a specified command.\n",
135 if (
command ==
"dump_all_command_conversions") {
147 static const std::string RESULT{CLIENT_NAME
" stopping"};
153 "Request a graceful shutdown of " CLIENT_NAME
".",
164 if (jsonRequest.params[0].isNum()) {
176 "Returns the total uptime of the server.\n",
196 "Returns details of the RPC server.\n",
222 entry.
pushKV(
"duration", int64_t{Ticks<std::chrono::microseconds>(SteadyClock::now() - info.
start)});
223 active_commands.
push_back(std::move(entry));
227 result.
pushKV(
"active_commands", std::move(active_commands));
231 result.
pushKV(
"logpath", std::move(log_path));
264 auto new_end = std::remove(it->second.begin(), it->second.end(), pcmd);
265 if (it->second.end() != new_end) {
266 it->second.erase(new_end, it->second.end());
281 static std::once_flag g_rpc_interrupt_flag;
283 std::call_once(g_rpc_interrupt_flag, []() {
292 static std::once_flag g_rpc_stop_flag;
295 std::call_once(g_rpc_stop_flag, [&]() {
315 rpcWarmupStatus = newStatus;
328 fRPCInWarmup =
false;
335 *outStatus = rpcWarmupStatus;
341 const std::vector<std::string> enabled_methods =
gArgs.
GetArgs(
"-deprecatedrpc");
343 return find(enabled_methods.begin(), enabled_methods.end(), method) != enabled_methods.end();
354 }
catch (
const std::exception& e) {
376 std::unordered_map<std::string, const UniValue*> argsIn;
377 for (
size_t i=0; i<keys.size(); ++i) {
378 auto [
_, inserted] = argsIn.emplace(keys[i], &
values[i]);
392 int initial_hole_size = 0;
393 const std::string* initial_param =
nullptr;
395 for (
const auto& [argNamePattern, named_only]: argNames) {
396 std::vector<std::string> vargNames =
SplitString(argNamePattern,
'|');
397 auto fr = argsIn.end();
398 for (
const std::string & argName : vargNames) {
399 fr = argsIn.find(argName);
400 if (fr != argsIn.end()) {
409 if (fr != argsIn.end()) {
410 if (options.exists(fr->first)) {
413 options.pushKVEnd(fr->first, *fr->second);
419 if (!options.empty() || fr != argsIn.end()) {
420 for (
int i = 0; i < hole; ++i) {
427 if (!initial_param) initial_param = &argNamePattern;
430 if (
out.params.empty()) initial_hole_size = hole;
436 if (fr != argsIn.end()) {
437 if (!options.empty()) {
440 out.params.push_back(*fr->second);
443 if (!options.empty()) {
444 out.params.push_back(std::move(options));
452 auto positional_args{argsIn.extract(
"args")};
453 if (positional_args && positional_args.mapped()->isArray()) {
454 if (initial_hole_size < (
int)positional_args.mapped()->size() && initial_param) {
459 out.params = *positional_args.mapped();
460 for (
size_t i{
out.params.size()}; i < named_args.size(); ++i) {
461 out.params.push_back(named_args[i]);
465 if (!argsIn.empty()) {
474 for (
const auto&
command : commands) {
510 return command.actor(request, result, last_handler);
514 }
catch (
const std::exception& e) {
521 std::vector<std::string> commandList;
523 for (
const auto& i :
mapCommands) commandList.emplace_back(i.first);
#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(std::string_view name, const JSONRPCRequest &helpreq) const
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.
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.
JSONRPCVersion m_json_version
enum JSONRPCRequest::Mode mode
std::optional< UniValue > id
auto MaybeArg(std::string_view key) const
Helper to get an optional request argument.
void push_back(UniValue val)
const std::vector< UniValue > & getValues() const
const std::vector< std::string > & getKeys() const
void pushKV(std::string key, UniValue val)
BCLog::Logger & LogInstance()
#define LogDebug(category,...)
std::vector< std::string > SplitString(std::string_view str, char sep)
UniValue JSONRPCError(int code, const std::string &message)
UniValue JSONRPCReplyObj(UniValue result, UniValue error, std::optional< UniValue > id, JSONRPCVersion jsonrpc_version)
void DeleteAuthCookie()
Delete RPC authentication cookie from disk.
@ 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.
bool IsDeprecatedRPCEnabled(const std::string &method)
void SetRPCWarmupFinished()
static RPCHelpMan uptime()
static RPCHelpMan getrpcinfo()
static bool ExecuteCommands(const std::vector< const CRPCCommand * > &commands, const JSONRPCRequest &request, UniValue &result)
bool RPCIsInWarmup(std::string *outStatus)
static bool ExecuteCommand(const CRPCCommand &command, const JSONRPCRequest &request, UniValue &result, bool last_handler)
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...
bool IsRPCRunning()
Query whether RPC is running.
void SetRPCWarmupStarting()
UniValue JSONRPCExec(const JSONRPCRequest &jreq, bool catch_errors)
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.
void RpcInterruptionPoint()
Throw JSONRPCError if RPC is not running.
NodeContext & EnsureAnyNodeContext(const std::any &context)
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)
consteval auto _(util::TranslatedLiteral str)
const UniValue NullUniValue
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
void UninterruptibleSleep(const std::chrono::microseconds &n)
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.