Bitcoin ABC  0.22.13
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 <rwcollection.h>
14 #include <util/system.h>
15 
16 #include <univalue.h>
17 
18 #include <boost/noncopyable.hpp>
19 
20 #include <cstdint>
21 #include <functional>
22 #include <map>
23 #include <string>
24 
25 static const unsigned int DEFAULT_RPC_SERIALIZE_VERSION = 1;
26 
27 class CRPCCommand;
28 
29 namespace RPCServerSignals {
30 void OnStarted(std::function<void()> slot);
31 void OnStopped(std::function<void()> slot);
32 } // namespace RPCServerSignals
33 
34 class Config;
35 
36 typedef std::map<std::string, std::unique_ptr<RPCCommand>> RPCCommandMap;
37 
41 class RPCServer : public boost::noncopyable {
42 private:
44 
45 public:
46  RPCServer() {}
47 
53  const JSONRPCRequest &request) const;
54 
58  void RegisterCommand(std::unique_ptr<RPCCommand> command);
59 };
60 
64 bool IsRPCRunning();
65 
68 
73 void SetRPCWarmupStatus(const std::string &newStatus);
74 
79 
83 bool RPCIsInWarmup(std::string *outStatus);
84 
90 class RPCTimerBase {
91 public:
92  virtual ~RPCTimerBase() {}
93 };
94 
99 public:
100  virtual ~RPCTimerInterface() {}
101 
105  virtual const char *Name() = 0;
106 
117  virtual RPCTimerBase *NewTimer(std::function<void()> &func,
118  int64_t millis) = 0;
119 };
120 
125 
130 
135 
140 void RPCRunLater(const std::string &name, std::function<void()> func,
141  int64_t nSeconds);
142 
143 using rpcfn_type = UniValue (*)(Config &config,
144  const JSONRPCRequest &jsonRequest);
145 using const_rpcfn_type = UniValue (*)(const Config &config,
146  const JSONRPCRequest &jsonRequest);
147 
148 class CRPCCommand {
149 public:
153  using Actor =
154  std::function<bool(Config &config, const JSONRPCRequest &request,
155  UniValue &result, bool last_handler)>;
156 
158  CRPCCommand(std::string _category, std::string _name, Actor _actor,
159  std::vector<std::string> _args, 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(const char *_category, const char *_name, rpcfn_type _fn,
166  std::initializer_list<const char *> _args)
167  : CRPCCommand(
168  _category, _name,
169  [_fn](Config &config, const JSONRPCRequest &request,
170  UniValue &result, bool) {
171  result = _fn(config, request);
172  return true;
173  },
174  {_args.begin(), _args.end()}, intptr_t(_fn)) {}
175 
177  CRPCCommand(const char *_category, const char *_name, const_rpcfn_type _fn,
178  std::initializer_list<const char *> _args)
179  : CRPCCommand(
180  _category, _name,
181  [_fn](const Config &config, const JSONRPCRequest &request,
182  UniValue &result, bool) {
183  result = _fn(config, request);
184  return true;
185  },
186  {_args.begin(), _args.end()}, intptr_t(_fn)) {}
187 
188  std::string category;
189  std::string name;
191  std::vector<std::string> argNames;
192  intptr_t unique_id;
193 };
194 
198 class CRPCTable {
199 private:
200  std::map<std::string, std::vector<const CRPCCommand *>> mapCommands;
201 
202 public:
203  CRPCTable();
204  std::string help(Config &config, const std::string &name,
205  const JSONRPCRequest &helpreq) const;
206 
213  UniValue execute(Config &config, const JSONRPCRequest &request) const;
214 
219  std::vector<std::string> listCommands() const;
220 
234  bool appendCommand(const std::string &name, const CRPCCommand *pcmd);
235  bool removeCommand(const std::string &name, const CRPCCommand *pcmd);
236 };
237 
238 bool IsDeprecatedRPCEnabled(const ArgsManager &args, const std::string &method);
239 
240 extern CRPCTable tableRPC;
241 
242 void StartRPC();
243 void InterruptRPC();
244 void StopRPC();
245 std::string JSONRPCExecBatch(Config &config, RPCServer &rpcServer,
246  const JSONRPCRequest &req, const UniValue &vReq);
247 
252 
253 #endif // BITCOIN_RPC_SERVER_H
std::function< bool(Config &config, const JSONRPCRequest &request, UniValue &result, bool last_handler)> Actor
RPC method handler reading request and assigning result.
Definition: server.h:155
static UniValue help(Config &config, const JSONRPCRequest &jsonRequest)
Definition: server.cpp:175
std::string JSONRPCExecBatch(Config &config, RPCServer &rpcServer, const JSONRPCRequest &req, const UniValue &vReq)
Definition: server.cpp:415
RPC timer "driver".
Definition: server.h:98
std::string category
Definition: server.h:188
bool IsRPCRunning()
Query whether RPC is running.
Definition: server.cpp:358
CRPCCommand(const char *_category, const char *_name, const_rpcfn_type _fn, std::initializer_list< const char *> _args)
Simplified constructor taking plain const_rpcfn_type function pointer.
Definition: server.h:177
Bitcoin RPC command dispatcher.
Definition: server.h:198
UniValue(*)(Config &config, const JSONRPCRequest &jsonRequest) rpcfn_type
Definition: server.h:144
static const unsigned int DEFAULT_RPC_SERIALIZE_VERSION
Definition: server.h:25
Actor actor
Definition: server.h:190
void SetRPCWarmupFinished()
Mark warmup as done.
Definition: server.cpp:373
void RpcInterruptionPoint()
Throw JSONRPCError if RPC is not running.
Definition: server.cpp:362
void RPCSetTimerInterface(RPCTimerInterface *iface)
Set the factory function for timers.
Definition: server.cpp:534
std::map< std::string, std::vector< const CRPCCommand * > > mapCommands
Definition: server.h:200
std::vector< std::string > argNames
Definition: server.h:191
void OnStarted(std::function< void()> slot)
Definition: server.cpp:106
CRPCTable tableRPC
Definition: server.cpp:563
std::string name
Definition: server.h:189
Definition: config.h:19
const char * name
Definition: rest.cpp:43
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:158
int RPCSerializationFlags()
Retrieves any serialization flags requested in command line argument.
Definition: server.cpp:559
virtual ~RPCTimerInterface()
Definition: server.h:100
virtual ~RPCTimerBase()
Definition: server.h:92
CRPCCommand(const char *_category, const char *_name, rpcfn_type _fn, std::initializer_list< const char *> _args)
Simplified constructor taking plain rpcfn_type function pointer.
Definition: server.h:165
Class for registering and managing all RPC calls.
Definition: server.h:41
void RPCRunLater(const std::string &name, std::function< void()> func, int64_t nSeconds)
Run func nSeconds from now.
Definition: server.cpp:544
void RPCUnsetTimerInterface(RPCTimerInterface *iface)
Unset factory function for timers.
Definition: server.cpp:538
void StopRPC()
Definition: server.cpp:351
void StartRPC()
Definition: server.cpp:339
void InterruptRPC()
Definition: server.cpp:345
Opaque base class for timers returned by NewTimerFunc.
Definition: server.h:90
RPCServer()
Definition: server.h:46
static bool ExecuteCommand(Config &config, const CRPCCommand &command, const JSONRPCRequest &request, UniValue &result, bool last_handler)
Definition: server.cpp:502
intptr_t unique_id
Definition: server.h:192
void SetRPCWarmupStatus(const std::string &newStatus)
Set the RPC warmup status.
Definition: server.cpp:368
RWCollection< RPCCommandMap > commands
Definition: server.h:43
void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface)
Set the factory function for timer, but only, if unset.
Definition: server.cpp:528
void OnStopped(std::function< void()> slot)
Definition: server.cpp:110
bool RPCIsInWarmup(std::string *outStatus)
Returns the current warmup state.
Definition: server.cpp:379
bool IsDeprecatedRPCEnabled(const ArgsManager &args, const std::string &method)
Definition: server.cpp:387
UniValue(*)(const Config &config, const JSONRPCRequest &jsonRequest) const_rpcfn_type
Definition: server.h:146
std::map< std::string, std::unique_ptr< RPCCommand > > RPCCommandMap
Definition: server.h:34