Bitcoin ABC  0.24.10
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 <amount.h>
11 #include <rpc/command.h>
12 #include <rpc/request.h>
13 #include <rpc/util.h>
14 #include <rwcollection.h>
15 #include <util/system.h>
16 
17 #include <univalue.h>
18 
19 #include <cstdint>
20 #include <functional>
21 #include <map>
22 #include <string>
23 
24 static const unsigned int DEFAULT_RPC_SERIALIZE_VERSION = 1;
25 
26 class CRPCCommand;
27 
28 namespace RPCServerSignals {
29 void OnStarted(std::function<void()> slot);
30 void OnStopped(std::function<void()> slot);
31 } // namespace RPCServerSignals
32 
33 class Config;
34 
35 typedef std::map<std::string, std::unique_ptr<RPCCommand>> RPCCommandMap;
36 
40 class RPCServer {
41 private:
43 
44 public:
45  RPCServer() {}
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 
66 bool IsRPCRunning();
67 
70 
75 void SetRPCWarmupStatus(const std::string &newStatus);
76 
81 
85 bool RPCIsInWarmup(std::string *outStatus);
86 
92 class RPCTimerBase {
93 public:
94  virtual ~RPCTimerBase() {}
95 };
96 
101 public:
102  virtual ~RPCTimerInterface() {}
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 
142 void RPCRunLater(const std::string &name, std::function<void()> func,
143  int64_t nSeconds);
144 
146 
147 class CRPCCommand {
148 public:
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::string> _args, intptr_t _unique_id)
159  : category(std::move(_category)), name(std::move(_name)),
160  actor(std::move(_actor)), argNames(std::move(_args)),
161  unique_id(_unique_id) {}
162 
164  CRPCCommand(std::string _category, RpcMethodFnType _fn)
165  : CRPCCommand(
166  _category, _fn().m_name,
167  [_fn](const Config &config, const JSONRPCRequest &request,
168  UniValue &result, bool) {
169  result = _fn().HandleRequest(config, request);
170  return true;
171  },
172  _fn().GetArgNames(), intptr_t(_fn)) {}
173 
174  std::string category;
175  std::string name;
177  std::vector<std::string> argNames;
178  intptr_t unique_id;
179 };
180 
184 class CRPCTable {
185 private:
186  std::map<std::string, std::vector<const CRPCCommand *>> mapCommands;
187 
188 public:
189  CRPCTable();
190  std::string help(const Config &config, const std::string &name,
191  const JSONRPCRequest &helpreq) const;
192 
199  UniValue execute(const Config &config, const JSONRPCRequest &request) const;
200 
205  std::vector<std::string> listCommands() const;
206 
211  UniValue dumpArgMap(const Config &config,
212  const JSONRPCRequest &request) const;
213 
226  void appendCommand(const std::string &name, const CRPCCommand *pcmd);
227  bool removeCommand(const std::string &name, const CRPCCommand *pcmd);
228 };
229 
230 bool IsDeprecatedRPCEnabled(const ArgsManager &args, const std::string &method);
231 
232 extern CRPCTable tableRPC;
233 
234 void StartRPC();
235 void InterruptRPC();
236 void StopRPC();
237 std::string JSONRPCExecBatch(const Config &config, RPCServer &rpcServer,
238  const JSONRPCRequest &req, const UniValue &vReq);
239 
244 
245 #endif // BITCOIN_RPC_SERVER_H
CRPCTable::mapCommands
std::map< std::string, std::vector< const CRPCCommand * > > mapCommands
Definition: server.h:186
StopRPC
void StopRPC()
Definition: server.cpp:360
RPCTimerInterface::NewTimer
virtual RPCTimerBase * NewTimer(std::function< void()> &func, int64_t millis)=0
Factory function for timers.
CRPCCommand::CRPCCommand
CRPCCommand(std::string _category, std::string _name, Actor _actor, std::vector< std::string > _args, intptr_t _unique_id)
Constructor taking Actor callback supporting multiple handlers.
Definition: server.h:157
RPCServer::RPCServer
RPCServer()
Definition: server.h:45
RPCTimerInterface
RPC timer "driver".
Definition: server.h:100
CRPCCommand::CRPCCommand
CRPCCommand(std::string _category, RpcMethodFnType _fn)
Simplified constructor taking plain RpcMethodFnType function pointer.
Definition: server.h:164
CRPCTable::dumpArgMap
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:552
RPCHelpMan
Definition: util.h:334
RPCCommandMap
std::map< std::string, std::unique_ptr< RPCCommand > > RPCCommandMap
Definition: server.h:33
StartRPC
void StartRPC()
Definition: server.cpp:343
RPCTimerBase
Opaque base class for timers returned by NewTimerFunc.
Definition: server.h:92
SetRPCWarmupStatus
void SetRPCWarmupStatus(const std::string &newStatus)
Set the RPC warmup status.
Definition: server.cpp:383
RpcMethodFnType
RPCHelpMan(*)() RpcMethodFnType
Definition: server.h:145
RPCUnsetTimerInterface
void RPCUnsetTimerInterface(RPCTimerInterface *iface)
Unset factory function for timers.
Definition: server.cpp:579
RPCServer
Class for registering and managing all RPC calls.
Definition: server.h:40
UniValue
Definition: univalue.h:23
RPCIsInWarmup
bool RPCIsInWarmup(std::string *outStatus)
Returns the current warmup state.
Definition: server.cpp:394
Config
Definition: config.h:17
CRPCTable::CRPCTable
CRPCTable()
Definition: server.cpp:316
command.h
RPCServer::ExecuteCommand
UniValue ExecuteCommand(const Config &config, const JSONRPCRequest &request) const
Attempts to execute an RPC command from the given request.
Definition: server.cpp:66
IsDeprecatedRPCEnabled
bool IsDeprecatedRPCEnabled(const ArgsManager &args, const std::string &method)
Definition: server.cpp:402
CRPCCommand
Definition: server.h:147
CRPCCommand::unique_id
intptr_t unique_id
Definition: server.h:178
RPCRunLater
void RPCRunLater(const std::string &name, std::function< void()> func, int64_t nSeconds)
Run func nSeconds from now.
Definition: server.cpp:585
CRPCCommand::Actor
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
univalue.h
RPCServer::commands
RWCollection< RPCCommandMap > commands
Definition: server.h:42
CRPCTable::listCommands
std::vector< std::string > listCommands() const
Returns a list of registered commands.
Definition: server.cpp:544
SetRPCWarmupFinished
void SetRPCWarmupFinished()
Mark warmup as done.
Definition: server.cpp:388
RPCSetTimerInterface
void RPCSetTimerInterface(RPCTimerInterface *iface)
Set the factory function for timers.
Definition: server.cpp:575
DEFAULT_RPC_SERIALIZE_VERSION
static const unsigned int DEFAULT_RPC_SERIALIZE_VERSION
Definition: server.h:24
request.h
InterruptRPC
void InterruptRPC()
Definition: server.cpp:349
RPCServerSignals::OnStarted
void OnStarted(std::function< void()> slot)
Definition: server.cpp:108
CRPCCommand::category
std::string category
Definition: server.h:174
RPCTimerBase::~RPCTimerBase
virtual ~RPCTimerBase()
Definition: server.h:94
RPCTimerInterface::Name
virtual const char * Name()=0
Implementation name.
CRPCTable::help
std::string help(const Config &config, const std::string &name, const JSONRPCRequest &helpreq) const
Definition: server.cpp:116
CRPCTable
RPC command dispatcher.
Definition: server.h:184
name
const char * name
Definition: rest.cpp:43
CRPCTable::execute
UniValue execute(const Config &config, const JSONRPCRequest &request) const
Execute a method.
Definition: server.cpp:505
RPCSetTimerInterfaceIfUnset
void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface)
Set the factory function for timer, but only, if unset.
Definition: server.cpp:569
system.h
RPCServerSignals
Definition: server.h:28
JSONRPCExecBatch
std::string JSONRPCExecBatch(const Config &config, RPCServer &rpcServer, const JSONRPCRequest &req, const UniValue &vReq)
Definition: server.cpp:430
CRPCCommand::actor
Actor actor
Definition: server.h:176
CRPCTable::appendCommand
void appendCommand(const std::string &name, const CRPCCommand *pcmd)
Appends a CRPCCommand to the dispatch table.
Definition: server.cpp:322
RPCServer::operator=
RPCServer & operator=(const RPCServer &)=delete
ArgsManager
Definition: system.h:148
CRPCTable::removeCommand
bool removeCommand(const std::string &name, const CRPCCommand *pcmd)
Definition: server.cpp:330
RpcInterruptionPoint
void RpcInterruptionPoint()
Throw JSONRPCError if RPC is not running.
Definition: server.cpp:377
RPCServerSignals::OnStopped
void OnStopped(std::function< void()> slot)
Definition: server.cpp:112
RPCServer::RegisterCommand
void RegisterCommand(std::unique_ptr< RPCCommand > command)
Register an RPC command.
Definition: server.cpp:95
IsRPCRunning
bool IsRPCRunning()
Query whether RPC is running.
Definition: server.cpp:373
RPCTimerInterface::~RPCTimerInterface
virtual ~RPCTimerInterface()
Definition: server.h:102
JSONRPCRequest
Definition: request.h:33
util.h
CRPCCommand::argNames
std::vector< std::string > argNames
Definition: server.h:177
rwcollection.h
amount.h
tableRPC
CRPCTable tableRPC
Definition: server.cpp:604
CRPCCommand::name
std::string name
Definition: server.h:175
RPCSerializationFlags
int RPCSerializationFlags()
Retrieves any serialization flags requested in command line argument.
Definition: server.cpp:600
RWCollection< RPCCommandMap >