Bitcoin Core 28.99.0
P2P Digital Currency
server.h
Go to the documentation of this file.
1// Copyright (c) 2010 Satoshi Nakamoto
2// Copyright (c) 2009-2021 The Bitcoin Core developers
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#ifndef BITCOIN_RPC_SERVER_H
7#define BITCOIN_RPC_SERVER_H
8
9#include <rpc/request.h>
10#include <rpc/util.h>
11
12#include <functional>
13#include <map>
14#include <stdint.h>
15#include <string>
16
17#include <univalue.h>
18
19class CRPCCommand;
20
22bool IsRPCRunning();
23
26
31void SetRPCWarmupStatus(const std::string& newStatus);
32/* Mark warmup as done. RPC calls will be processed from now on. */
34
35/* returns the current warmup state. */
36bool RPCIsInWarmup(std::string *outStatus);
37
43{
44public:
45 virtual ~RPCTimerBase() = default;
46};
47
52{
53public:
54 virtual ~RPCTimerInterface() = default;
56 virtual const char *Name() = 0;
63 virtual RPCTimerBase* NewTimer(std::function<void()>& func, int64_t millis) = 0;
64};
65
72
77void RPCRunLater(const std::string& name, std::function<void()> func, int64_t nSeconds);
78
80
82{
83public:
87 using Actor = std::function<bool(const JSONRPCRequest& request, UniValue& result, bool last_handler)>;
88
90 CRPCCommand(std::string category, std::string name, Actor actor, std::vector<std::pair<std::string, bool>> args, intptr_t unique_id)
91 : category(std::move(category)), name(std::move(name)), actor(std::move(actor)), argNames(std::move(args)),
93 {
94 }
95
100 fn().m_name,
101 [fn](const JSONRPCRequest& request, UniValue& result, bool) { result = fn().HandleRequest(request); return true; },
102 fn().GetArgNames(),
103 intptr_t(fn))
104 {
105 }
106
107 std::string category;
108 std::string name;
119 std::vector<std::pair<std::string, bool>> argNames;
120 intptr_t unique_id;
121};
122
127{
128private:
129 std::map<std::string, std::vector<const CRPCCommand*>> mapCommands;
130public:
131 CRPCTable();
132 std::string help(const std::string& name, const JSONRPCRequest& helpreq) const;
133
140 UniValue execute(const JSONRPCRequest &request) const;
141
146 std::vector<std::string> listCommands() const;
147
151 UniValue dumpArgMap(const JSONRPCRequest& request) const;
152
165 void appendCommand(const std::string& name, const CRPCCommand* pcmd);
166 bool removeCommand(const std::string& name, const CRPCCommand* pcmd);
167};
168
169bool IsDeprecatedRPCEnabled(const std::string& method);
170
171extern CRPCTable tableRPC;
172
173void StartRPC();
174void InterruptRPC();
175void StopRPC();
176UniValue JSONRPCExec(const JSONRPCRequest& jreq, bool catch_errors);
177
178#endif // BITCOIN_RPC_SERVER_H
ArgsManager & args
Definition: bitcoind.cpp:277
std::string category
Definition: server.h:107
CRPCCommand(std::string category, std::string name, Actor actor, std::vector< std::pair< std::string, bool > > args, intptr_t unique_id)
Constructor taking Actor callback supporting multiple handlers.
Definition: server.h:90
intptr_t unique_id
Definition: server.h:120
std::vector< std::pair< std::string, bool > > argNames
List of method arguments and whether they are named-only.
Definition: server.h:119
std::string name
Definition: server.h:108
std::function< bool(const JSONRPCRequest &request, UniValue &result, bool last_handler)> Actor
RPC method handler reading request and assigning result.
Definition: server.h:87
Actor actor
Definition: server.h:109
CRPCCommand(std::string category, RpcMethodFnType fn)
Simplified constructor taking plain RpcMethodFnType function pointer.
Definition: server.h:97
RPC command dispatcher.
Definition: server.h:127
CRPCTable()
Definition: server.cpp:253
std::map< std::string, std::vector< const CRPCCommand * > > mapCommands
Definition: server.h:129
bool removeCommand(const std::string &name, const CRPCCommand *pcmd)
Definition: server.cpp:267
std::vector< std::string > listCommands() const
Returns a list of registered commands.
Definition: server.cpp:521
UniValue execute(const JSONRPCRequest &request) const
Execute a method.
Definition: server.cpp:484
void appendCommand(const std::string &name, const CRPCCommand *pcmd)
Appends a CRPCCommand to the dispatch table.
Definition: server.cpp:260
std::string help(const std::string &name, const JSONRPCRequest &helpreq) const
Definition: server.cpp:72
UniValue dumpArgMap(const JSONRPCRequest &request) const
Return all named arguments that need to be converted by the client from string to another JSON type.
Definition: server.cpp:529
Opaque base class for timers returned by NewTimerFunc.
Definition: server.h:43
virtual ~RPCTimerBase()=default
RPC timer "driver".
Definition: server.h:52
virtual RPCTimerBase * NewTimer(std::function< void()> &func, int64_t millis)=0
Factory function for timers.
virtual const char * Name()=0
Implementation name.
virtual ~RPCTimerInterface()=default
const char * name
Definition: rest.cpp:49
void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface)
Set the factory function for timer, but only, if unset.
Definition: server.cpp:546
bool IsDeprecatedRPCEnabled(const std::string &method)
Definition: server.cpp:341
void SetRPCWarmupFinished()
Definition: server.cpp:326
void StartRPC()
Definition: server.cpp:280
void RPCUnsetTimerInterface(RPCTimerInterface *iface)
Unset factory function for timers.
Definition: server.cpp:557
void RPCRunLater(const std::string &name, std::function< void()> func, int64_t nSeconds)
Run func nSeconds from now.
Definition: server.cpp:563
bool RPCIsInWarmup(std::string *outStatus)
Definition: server.cpp:333
RPCHelpMan(* RpcMethodFnType)()
Definition: server.h:79
void StopRPC()
Definition: server.cpp:297
bool IsRPCRunning()
Query whether RPC is running.
Definition: server.cpp:310
void InterruptRPC()
Definition: server.cpp:286
UniValue JSONRPCExec(const JSONRPCRequest &jreq, bool catch_errors)
Definition: server.cpp:348
void SetRPCWarmupStatus(const std::string &newStatus)
Set the RPC warmup status.
Definition: server.cpp:320
CRPCTable tableRPC
Definition: server.cpp:573
void RPCSetTimerInterface(RPCTimerInterface *iface)
Set the factory function for timers.
Definition: server.cpp:552
void RpcInterruptionPoint()
Throw JSONRPCError if RPC is not running.
Definition: server.cpp:315