Bitcoin Core  21.99.0
P2P Digital Currency
server.h
Go to the documentation of this file.
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 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 <amount.h>
10 #include <rpc/request.h>
11 #include <rpc/util.h>
12 
13 #include <functional>
14 #include <map>
15 #include <stdint.h>
16 #include <string>
17 
18 #include <univalue.h>
19 
20 static const unsigned int DEFAULT_RPC_SERIALIZE_VERSION = 1;
21 
22 class CRPCCommand;
23 
24 namespace RPCServer
25 {
26  void OnStarted(std::function<void ()> slot);
27  void OnStopped(std::function<void ()> slot);
28 }
29 
31 bool IsRPCRunning();
32 
35 
40 void SetRPCWarmupStatus(const std::string& newStatus);
41 /* Mark warmup as done. RPC calls will be processed from now on. */
43 
44 /* returns the current warmup state. */
45 bool RPCIsInWarmup(std::string *outStatus);
46 
52 {
53 public:
54  virtual ~RPCTimerBase() {}
55 };
56 
61 {
62 public:
63  virtual ~RPCTimerInterface() {}
65  virtual const char *Name() = 0;
72  virtual RPCTimerBase* NewTimer(std::function<void()>& func, int64_t millis) = 0;
73 };
74 
81 
86 void RPCRunLater(const std::string& name, std::function<void()> func, int64_t nSeconds);
87 
89 
91 {
92 public:
96  using Actor = std::function<bool(const JSONRPCRequest& request, UniValue& result, bool last_handler)>;
97 
99  CRPCCommand(std::string category, std::string name, Actor actor, std::vector<std::string> args, intptr_t unique_id)
100  : category(std::move(category)), name(std::move(name)), actor(std::move(actor)), argNames(std::move(args)),
101  unique_id(unique_id)
102  {
103  }
104 
106  CRPCCommand(std::string category, std::string name_in, RpcMethodFnType fn, std::vector<std::string> args_in)
107  : CRPCCommand(
108  category,
109  fn().m_name,
110  [fn](const JSONRPCRequest& request, UniValue& result, bool) { result = fn().HandleRequest(request); return true; },
111  fn().GetArgNames(),
112  intptr_t(fn))
113  {
114  CHECK_NONFATAL(fn().m_name == name_in);
115  CHECK_NONFATAL(fn().GetArgNames() == args_in);
116  }
117 
118  std::string category;
119  std::string name;
121  std::vector<std::string> argNames;
122  intptr_t unique_id;
123 };
124 
129 {
130 private:
131  std::map<std::string, std::vector<const CRPCCommand*>> mapCommands;
132 public:
133  CRPCTable();
134  std::string help(const std::string& name, const JSONRPCRequest& helpreq) const;
135 
142  UniValue execute(const JSONRPCRequest &request) const;
143 
148  std::vector<std::string> listCommands() const;
149 
150 
163  void appendCommand(const std::string& name, const CRPCCommand* pcmd);
164  bool removeCommand(const std::string& name, const CRPCCommand* pcmd);
165 };
166 
167 bool IsDeprecatedRPCEnabled(const std::string& method);
168 
169 extern CRPCTable tableRPC;
170 
171 void StartRPC();
172 void InterruptRPC();
173 void StopRPC();
174 std::string JSONRPCExecBatch(const JSONRPCRequest& jreq, const UniValue& vReq);
175 
176 // Retrieves any serialization flags requested in command line argument
178 
179 #endif // BITCOIN_RPC_SERVER_H
RPC timer "driver".
Definition: server.h:60
std::string category
Definition: server.h:118
bool IsRPCRunning()
Query whether RPC is running.
Definition: server.cpp:315
RPC command dispatcher.
Definition: server.h:128
static const unsigned int DEFAULT_RPC_SERIALIZE_VERSION
Definition: server.h:20
Actor actor
Definition: server.h:120
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:99
void SetRPCWarmupFinished()
Definition: server.cpp:331
#define CHECK_NONFATAL(condition)
Throw a NonFatalCheckError when the condition evaluates to false.
Definition: check.h:32
void OnStopped(std::function< void()> slot)
Definition: server.cpp:74
RPCHelpMan(* RpcMethodFnType)()
Definition: server.h:88
void RpcInterruptionPoint()
Throw JSONRPCError if RPC is not running.
Definition: server.cpp:320
std::function< bool(const JSONRPCRequest &request, UniValue &result, bool last_handler)> Actor
RPC method handler reading request and assigning result.
Definition: server.h:96
void RPCSetTimerInterface(RPCTimerInterface *iface)
Set the factory function for timers.
Definition: server.cpp:488
std::vector< std::string > argNames
Definition: server.h:121
CRPCTable tableRPC
Definition: server.cpp:517
std::string name
Definition: server.h:119
const char * name
Definition: rest.cpp:41
bool IsDeprecatedRPCEnabled(const std::string &method)
Definition: server.cpp:346
int RPCSerializationFlags()
Definition: server.cpp:509
virtual ~RPCTimerInterface()
Definition: server.h:63
virtual ~RPCTimerBase()
Definition: server.h:54
void RPCRunLater(const std::string &name, std::function< void()> func, int64_t nSeconds)
Run func nSeconds from now.
Definition: server.cpp:499
void RPCUnsetTimerInterface(RPCTimerInterface *iface)
Unset factory function for timers.
Definition: server.cpp:493
void StopRPC()
Definition: server.cpp:302
void StartRPC()
Definition: server.cpp:284
void InterruptRPC()
Definition: server.cpp:291
CRPCCommand(std::string category, std::string name_in, RpcMethodFnType fn, std::vector< std::string > args_in)
Simplified constructor taking plain RpcMethodFnType function pointer.
Definition: server.h:106
Opaque base class for timers returned by NewTimerFunc.
Definition: server.h:51
void OnStarted(std::function< void()> slot)
Definition: server.cpp:69
std::map< std::string, std::vector< const CRPCCommand * > > mapCommands
Definition: server.h:131
intptr_t unique_id
Definition: server.h:122
void SetRPCWarmupStatus(const std::string &newStatus)
Set the RPC warmup status.
Definition: server.cpp:325
std::string JSONRPCExecBatch(const JSONRPCRequest &jreq, const UniValue &vReq)
Definition: server.cpp:376
void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface)
Set the factory function for timer, but only, if unset.
Definition: server.cpp:482
static RPCHelpMan help()
Definition: server.cpp:133
bool RPCIsInWarmup(std::string *outStatus)
Definition: server.cpp:338