Bitcoin ABC 0.30.7
P2P Digital Currency
server.h
Go to the documentation of this file.
1// Copyright (c) 2010 Satoshi Nakamoto
2// Copyright (c) 2009-2016 The Bitcoin Core developers
3// Copyright (c) 2017-2019 The Bitcoin developers
4// Distributed under the MIT software license, see the accompanying
5// file COPYING or http://www.opensource.org/licenses/mit-license.php.
6
7#ifndef BITCOIN_RPC_SERVER_H
8#define BITCOIN_RPC_SERVER_H
9
10#include <common/system.h>
11#include <rpc/command.h>
12#include <rpc/request.h>
13#include <rpc/util.h>
14#include <rwcollection.h>
15
16#include <univalue.h>
17
18#include <cstdint>
19#include <functional>
20#include <map>
21#include <string>
22
23static const unsigned int DEFAULT_RPC_SERIALIZE_VERSION = 1;
24
25class ArgsManager;
26class CRPCCommand;
27
29void OnStarted(std::function<void()> slot);
30void OnStopped(std::function<void()> slot);
31} // namespace RPCServerSignals
32
33class Config;
34
35typedef std::map<std::string, std::unique_ptr<RPCCommand>> RPCCommandMap;
36
40class RPCServer {
41private:
43
44public:
46
47 RPCServer(const RPCServer &) = delete;
48 RPCServer &operator=(const RPCServer &) = delete;
49
54 UniValue ExecuteCommand(const Config &config,
55 const JSONRPCRequest &request) const;
56
60 void RegisterCommand(std::unique_ptr<RPCCommand> command);
61};
62
66bool IsRPCRunning();
67
70
75void SetRPCWarmupStatus(const std::string &newStatus);
76
81
85bool RPCIsInWarmup(std::string *outStatus);
86
93public:
94 virtual ~RPCTimerBase() {}
95};
96
101public:
103
107 virtual const char *Name() = 0;
108
119 virtual RPCTimerBase *NewTimer(std::function<void()> &func,
120 int64_t millis) = 0;
121};
122
127
132
137
142void RPCRunLater(const std::string &name, std::function<void()> func,
143 int64_t nSeconds);
144
146
148public:
152 using Actor =
153 std::function<bool(const Config &config, const JSONRPCRequest &request,
154 UniValue &result, bool last_handler)>;
155
157 CRPCCommand(std::string _category, std::string _name, Actor _actor,
158 std::vector<std::pair<std::string, bool>> _args,
159 intptr_t _unique_id)
160 : category(std::move(_category)), name(std::move(_name)),
161 actor(std::move(_actor)), argNames(std::move(_args)),
162 unique_id(_unique_id) {}
163
165 CRPCCommand(std::string _category, RpcMethodFnType _fn)
166 : CRPCCommand(
167 _category, _fn().m_name,
168 [_fn](const Config &config, const JSONRPCRequest &request,
169 UniValue &result, bool) {
170 result = _fn().HandleRequest(config, request);
171 return true;
172 },
173 _fn().GetArgNames(), intptr_t(_fn)) {}
174
175 std::string category;
176 std::string name;
187 std::vector<std::pair<std::string, bool>> argNames;
188 intptr_t unique_id;
189};
190
195private:
196 std::map<std::string, std::vector<const CRPCCommand *>> mapCommands;
197
198public:
199 CRPCTable();
200 std::string help(const Config &config, const std::string &name,
201 const JSONRPCRequest &helpreq) const;
202
209 UniValue execute(const Config &config, const JSONRPCRequest &request) const;
210
215 std::vector<std::string> listCommands() const;
216
221 UniValue dumpArgMap(const Config &config,
222 const JSONRPCRequest &request) const;
223
236 void appendCommand(const std::string &name, const CRPCCommand *pcmd);
237 bool removeCommand(const std::string &name, const CRPCCommand *pcmd);
238};
239
240bool IsDeprecatedRPCEnabled(const ArgsManager &args, const std::string &method);
241
242extern CRPCTable tableRPC;
243
244void StartRPC();
245void InterruptRPC();
246void StopRPC();
247std::string JSONRPCExecBatch(const Config &config, RPCServer &rpcServer,
248 const JSONRPCRequest &req, const UniValue &vReq);
249
254
255#endif // BITCOIN_RPC_SERVER_H
std::string category
Definition: server.h:175
intptr_t unique_id
Definition: server.h:188
std::vector< std::pair< std::string, bool > > argNames
List of method arguments and whether they are named-only.
Definition: server.h:187
std::string name
Definition: server.h:176
CRPCCommand(std::string _category, RpcMethodFnType _fn)
Simplified constructor taking plain RpcMethodFnType function pointer.
Definition: server.h:165
Actor actor
Definition: server.h:177
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:157
std::function< bool(const Config &config, const JSONRPCRequest &request, UniValue &result, bool last_handler)> Actor
RPC method handler reading request and assigning result.
Definition: server.h:154
RPC command dispatcher.
Definition: server.h:194
std::map< std::string, std::vector< const CRPCCommand * > > mapCommands
Definition: server.h:196
CRPCTable()
Definition: server.cpp:321
bool removeCommand(const std::string &name, const CRPCCommand *pcmd)
Definition: server.cpp:335
std::string help(const Config &config, const std::string &name, const JSONRPCRequest &helpreq) const
Definition: server.cpp:121
std::vector< std::string > listCommands() const
Returns a list of registered commands.
Definition: server.cpp:623
UniValue execute(const Config &config, const JSONRPCRequest &request) const
Execute a method.
Definition: server.cpp:582
void appendCommand(const std::string &name, const CRPCCommand *pcmd)
Appends a CRPCCommand to the dispatch table.
Definition: server.cpp:327
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.
Definition: server.cpp:631
Definition: config.h:19
Class for registering and managing all RPC calls.
Definition: server.h:40
RPCServer & operator=(const RPCServer &)=delete
RPCServer(const RPCServer &)=delete
UniValue ExecuteCommand(const Config &config, const JSONRPCRequest &request) const
Attempts to execute an RPC command from the given request.
Definition: server.cpp:71
RWCollection< RPCCommandMap > commands
Definition: server.h:42
void RegisterCommand(std::unique_ptr< RPCCommand > command)
Register an RPC command.
Definition: server.cpp:100
RPCServer()
Definition: server.h:45
Opaque base class for timers returned by NewTimerFunc.
Definition: server.h:92
virtual ~RPCTimerBase()
Definition: server.h:94
RPC timer "driver".
Definition: server.h:100
virtual ~RPCTimerInterface()
Definition: server.h:102
virtual RPCTimerBase * NewTimer(std::function< void()> &func, int64_t millis)=0
Factory function for timers.
virtual const char * Name()=0
Implementation name.
void OnStarted(std::function< void()> slot)
Definition: server.cpp:113
void OnStopped(std::function< void()> slot)
Definition: server.cpp:117
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Definition: rcu.h:259
const char * name
Definition: rest.cpp:47
void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface)
Set the factory function for timer, but only, if unset.
Definition: server.cpp:648
void SetRPCWarmupFinished()
Mark warmup as done.
Definition: server.cpp:393
static const unsigned int DEFAULT_RPC_SERIALIZE_VERSION
Definition: server.h:23
bool IsDeprecatedRPCEnabled(const ArgsManager &args, const std::string &method)
Definition: server.cpp:407
void StartRPC()
Definition: server.cpp:348
void RPCUnsetTimerInterface(RPCTimerInterface *iface)
Unset factory function for timers.
Definition: server.cpp:658
void RPCRunLater(const std::string &name, std::function< void()> func, int64_t nSeconds)
Run func nSeconds from now.
Definition: server.cpp:664
bool RPCIsInWarmup(std::string *outStatus)
Returns the current warmup state.
Definition: server.cpp:399
std::string JSONRPCExecBatch(const Config &config, RPCServer &rpcServer, const JSONRPCRequest &req, const UniValue &vReq)
Definition: server.cpp:435
void StopRPC()
Definition: server.cpp:365
RPCHelpMan(*)() RpcMethodFnType
Definition: server.h:145
bool IsRPCRunning()
Query whether RPC is running.
Definition: server.cpp:378
int RPCSerializationFlags()
Retrieves any serialization flags requested in command line argument.
Definition: server.cpp:679
void InterruptRPC()
Definition: server.cpp:354
std::map< std::string, std::unique_ptr< RPCCommand > > RPCCommandMap
Definition: server.h:35
void SetRPCWarmupStatus(const std::string &newStatus)
Set the RPC warmup status.
Definition: server.cpp:388
CRPCTable tableRPC
Definition: server.cpp:683
void RPCSetTimerInterface(RPCTimerInterface *iface)
Set the factory function for timers.
Definition: server.cpp:654
void RpcInterruptionPoint()
Throw JSONRPCError if RPC is not running.
Definition: server.cpp:382