Bitcoin Core  24.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 
19 static const unsigned int DEFAULT_RPC_SERIALIZE_VERSION = 1;
20 
21 class CRPCCommand;
22 
23 namespace RPCServer
24 {
25  void OnStarted(std::function<void ()> slot);
26  void OnStopped(std::function<void ()> slot);
27 }
28 
30 bool IsRPCRunning();
31 
34 
39 void SetRPCWarmupStatus(const std::string& newStatus);
40 /* Mark warmup as done. RPC calls will be processed from now on. */
42 
43 /* returns the current warmup state. */
44 bool RPCIsInWarmup(std::string *outStatus);
45 
51 {
52 public:
53  virtual ~RPCTimerBase() {}
54 };
55 
60 {
61 public:
62  virtual ~RPCTimerInterface() {}
64  virtual const char *Name() = 0;
71  virtual RPCTimerBase* NewTimer(std::function<void()>& func, int64_t millis) = 0;
72 };
73 
80 
85 void RPCRunLater(const std::string& name, std::function<void()> func, int64_t nSeconds);
86 
88 
90 {
91 public:
95  using Actor = std::function<bool(const JSONRPCRequest& request, UniValue& result, bool last_handler)>;
96 
98  CRPCCommand(std::string category, std::string name, Actor actor, std::vector<std::string> args, intptr_t unique_id)
99  : category(std::move(category)), name(std::move(name)), actor(std::move(actor)), argNames(std::move(args)),
101  {
102  }
103 
106  : CRPCCommand(
107  category,
108  fn().m_name,
109  [fn](const JSONRPCRequest& request, UniValue& result, bool) { result = fn().HandleRequest(request); return true; },
110  fn().GetArgNames(),
111  intptr_t(fn))
112  {
113  }
114 
115  std::string category;
116  std::string name;
118  std::vector<std::string> argNames;
119  intptr_t unique_id;
120 };
121 
126 {
127 private:
128  std::map<std::string, std::vector<const CRPCCommand*>> mapCommands;
129 public:
130  CRPCTable();
131  std::string help(const std::string& name, const JSONRPCRequest& helpreq) const;
132 
139  UniValue execute(const JSONRPCRequest &request) const;
140 
145  std::vector<std::string> listCommands() const;
146 
150  UniValue dumpArgMap(const JSONRPCRequest& request) const;
151 
164  void appendCommand(const std::string& name, const CRPCCommand* pcmd);
165  bool removeCommand(const std::string& name, const CRPCCommand* pcmd);
166 };
167 
168 bool IsDeprecatedRPCEnabled(const std::string& method);
169 
170 extern CRPCTable tableRPC;
171 
172 void StartRPC();
173 void InterruptRPC();
174 void StopRPC();
175 std::string JSONRPCExecBatch(const JSONRPCRequest& jreq, const UniValue& vReq);
176 
177 // Retrieves any serialization flags requested in command line argument
179 
180 #endif // BITCOIN_RPC_SERVER_H
std::vector< std::string > argNames
Definition: server.h:118
std::string category
Definition: server.h:115
intptr_t unique_id
Definition: server.h:119
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:98
std::string name
Definition: server.h:116
std::function< bool(const JSONRPCRequest &request, UniValue &result, bool last_handler)> Actor
RPC method handler reading request and assigning result.
Definition: server.h:95
Actor actor
Definition: server.h:117
CRPCCommand(std::string category, RpcMethodFnType fn)
Simplified constructor taking plain RpcMethodFnType function pointer.
Definition: server.h:105
RPC command dispatcher.
Definition: server.h:126
CRPCTable()
Definition: server.cpp:260
std::map< std::string, std::vector< const CRPCCommand * > > mapCommands
Definition: server.h:128
bool removeCommand(const std::string &name, const CRPCCommand *pcmd)
Definition: server.cpp:274
std::vector< std::string > listCommands() const
Returns a list of registered commands.
Definition: server.cpp:484
UniValue execute(const JSONRPCRequest &request) const
Execute a method.
Definition: server.cpp:447
void appendCommand(const std::string &name, const CRPCCommand *pcmd)
Appends a CRPCCommand to the dispatch table.
Definition: server.cpp:267
std::string help(const std::string &name, const JSONRPCRequest &helpreq) const
Definition: server.cpp:80
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:491
Opaque base class for timers returned by NewTimerFunc.
Definition: server.h:51
virtual ~RPCTimerBase()
Definition: server.h:53
RPC timer "driver".
Definition: server.h:60
virtual RPCTimerBase * NewTimer(std::function< void()> &func, int64_t millis)=0
Factory function for timers.
virtual ~RPCTimerInterface()
Definition: server.h:62
virtual const char * Name()=0
Implementation name.
void OnStarted(std::function< void()> slot)
Definition: server.cpp:70
void OnStopped(std::function< void()> slot)
Definition: server.cpp:75
ArgsManager args
const char * name
Definition: rest.cpp:46
void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface)
Set the factory function for timer, but only, if unset.
Definition: server.cpp:508
bool IsDeprecatedRPCEnabled(const std::string &method)
Definition: server.cpp:349
void SetRPCWarmupFinished()
Definition: server.cpp:334
static const unsigned int DEFAULT_RPC_SERIALIZE_VERSION
Definition: server.h:19
void StartRPC()
Definition: server.cpp:287
void RPCUnsetTimerInterface(RPCTimerInterface *iface)
Unset factory function for timers.
Definition: server.cpp:519
void RPCRunLater(const std::string &name, std::function< void()> func, int64_t nSeconds)
Run func nSeconds from now.
Definition: server.cpp:525
bool RPCIsInWarmup(std::string *outStatus)
Definition: server.cpp:341
RPCHelpMan(* RpcMethodFnType)()
Definition: server.h:87
void StopRPC()
Definition: server.cpp:305
bool IsRPCRunning()
Query whether RPC is running.
Definition: server.cpp:318
std::string JSONRPCExecBatch(const JSONRPCRequest &jreq, const UniValue &vReq)
Definition: server.cpp:379
int RPCSerializationFlags()
Definition: server.cpp:535
void InterruptRPC()
Definition: server.cpp:294
void SetRPCWarmupStatus(const std::string &newStatus)
Set the RPC warmup status.
Definition: server.cpp:328
CRPCTable tableRPC
Definition: server.cpp:543
void RPCSetTimerInterface(RPCTimerInterface *iface)
Set the factory function for timers.
Definition: server.cpp:514
void RpcInterruptionPoint()
Throw JSONRPCError if RPC is not running.
Definition: server.cpp:323