Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2010 Satoshi Nakamoto2// Copyright (c) 2009-present The Bitcoin Core developers3// Distributed under the MIT software license, see the accompanying4// file COPYING or http://www.opensource.org/licenses/mit-license.php.6#ifndef BITCOIN_RPC_SERVER_H7#define BITCOIN_RPC_SERVER_H9#include <rpc/request.h>10#include <rpc/util.h>11#include <univalue.h>13#include <cstdint>14#include <functional>15#include <map>16#include <string>17#include <string_view>18#include <utility>19#include <vector>21/** Query whether RPC is running */22bool IsRPCRunning();24/** Throw JSONRPCError if RPC is not running */25void RpcInterruptionPoint();27/**28* Set the RPC warmup status. When this is done, all RPC calls will error out29* immediately with RPC_IN_WARMUP.30*/31void SetRPCWarmupStatus(const std::string& newStatus);32void SetRPCWarmupStarting();33/* Mark warmup as done. RPC calls will be processed from now on. */34void SetRPCWarmupFinished();36/* returns the current warmup state. */37bool RPCIsInWarmup(std::string *outStatus);39typedef RPCMethod (*RpcMethodFnType)();41class CRPCCommand42{43public:44//! RPC method handler reading request and assigning result. Should return45//! true if request is fully handled, false if it should be passed on to46//! subsequent handlers.47using Actor = std::function<bool(const JSONRPCRequest& request, UniValue& result, bool last_handler)>;49//! Constructor taking Actor callback supporting multiple handlers.50CRPCCommand(std::string category, std::string name, Actor actor, std::vector<std::pair<std::string, bool>> args, intptr_t unique_id)51: category(std::move(category)), name(std::move(name)), actor(std::move(actor)), argNames(std::move(args)),52unique_id(unique_id)53{54}56//! Simplified constructor taking plain RpcMethodFnType function pointer.57CRPCCommand(std::string category, RpcMethodFnType fn)58: CRPCCommand(59category,60fn().m_name,61[fn](const JSONRPCRequest& request, UniValue& result, bool) { result = fn().HandleRequest(request); return true; },62fn().GetArgNames(),63intptr_t(fn))64{65this->metadata_fn = fn;66}68std::string category;69std::string name;70Actor actor;71//! List of method arguments and whether they are named-only. Incoming RPC72//! requests contain a "params" field that can either be an array containing73//! unnamed arguments or an object containing named arguments. The74//! "argNames" vector is used in the latter case to transform the params75//! object into an array. Each argument in "argNames" gets mapped to a76//! unique position in the array, based on the order it is listed, unless77//! the argument is a named-only argument with argNames[x].second set to78//! true. Named-only arguments are combined into a JSON object that is79//! appended after other arguments, see transformNamedArguments for details.80std::vector<std::pair<std::string, bool>> argNames;81intptr_t unique_id;82RpcMethodFnType metadata_fn{nullptr};83};85/**86* RPC command dispatcher.87*/88class CRPCTable89{90private:91std::map<std::string, std::vector<const CRPCCommand*>> mapCommands;92public:93CRPCTable();94std::string help(std::string_view name, const JSONRPCRequest& helpreq) const;96/**97* Execute a method.98* @param request The JSONRPCRequest to execute99* @returns Result of the call.100* @throws an exception (UniValue) when an error happens.101*/102UniValue execute(const JSONRPCRequest &request) const;104/**105* Returns a list of registered commands106* @returns List of registered commands.107*/108std::vector<std::string> listCommands() const;109/** Return a complete OpenRPC 1.4.1 document for registered commands. */110UniValue buildOpenRPCDoc(bool include_hidden = false) const;112/**113* Return all named arguments that need to be converted by the client from string to another JSON type114*/115UniValue dumpArgMap(const JSONRPCRequest& request) const;117/**118* Appends a CRPCCommand to the dispatch table.119*120* Precondition: RPC server is not running121*122* Commands with different method names but the same unique_id will123* be considered aliases, and only the first registered method name will124* show up in the help text command listing. Aliased commands do not have125* to have the same behavior. Server and client code can distinguish126* between calls based on method name, and aliased commands can also127* register different names, types, and numbers of parameters.128*/129void appendCommand(const std::string& name, const CRPCCommand* pcmd);130bool removeCommand(const std::string& name, const CRPCCommand* pcmd);131};133bool IsDeprecatedRPCEnabled(const std::string& method);135extern CRPCTable tableRPC;137void StartRPC();138void InterruptRPC();139void StopRPC();140UniValue JSONRPCExec(const JSONRPCRequest& jreq, bool catch_errors);142#endif // BITCOIN_RPC_SERVER_H