Bitcoin Core  0.20.99
P2P Digital Currency
server.h
Go to the documentation of this file.
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2019 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 
88 typedef UniValue(*rpcfn_type)(const JSONRPCRequest& jsonRequest);
90 
92 {
93 public:
97  using Actor = std::function<bool(const JSONRPCRequest& request, UniValue& result, bool last_handler)>;
98 
100  CRPCCommand(std::string category, std::string name, Actor actor, std::vector<std::string> args, intptr_t unique_id)
101  : category(std::move(category)), name(std::move(name)), actor(std::move(actor)), argNames(std::move(args)),
102  unique_id(unique_id)
103  {
104  }
105 
107  CRPCCommand(std::string category, std::string name_in, RpcMethodFnType fn, std::vector<std::string> args_in)
108  : CRPCCommand(
109  category,
110  fn().m_name,
111  [fn](const JSONRPCRequest& request, UniValue& result, bool) { result = fn().HandleRequest(request); return true; },
112  fn().GetArgNames(),
113  intptr_t(fn))
114  {
115  CHECK_NONFATAL(fn().m_name == name_in);
116  CHECK_NONFATAL(fn().GetArgNames() == args_in);
117  }
118 
120  CRPCCommand(const char* category, const char* name, rpcfn_type fn, std::initializer_list<const char*> args)
121  : CRPCCommand(category, name,
122  [fn](const JSONRPCRequest& request, UniValue& result, bool) { result = fn(request); return true; },
123  {args.begin(), args.end()}, intptr_t(fn))
124  {
125  }
126 
127  std::string category;
128  std::string name;
130  std::vector<std::string> argNames;
131  intptr_t unique_id;
132 };
133 
138 {
139 private:
140  std::map<std::string, std::vector<const CRPCCommand*>> mapCommands;
141 public:
142  CRPCTable();
143  std::string help(const std::string& name, const JSONRPCRequest& helpreq) const;
144 
151  UniValue execute(const JSONRPCRequest &request) const;
152 
157  std::vector<std::string> listCommands() const;
158 
159 
172  void appendCommand(const std::string& name, const CRPCCommand* pcmd);
173  bool removeCommand(const std::string& name, const CRPCCommand* pcmd);
174 };
175 
176 bool IsDeprecatedRPCEnabled(const std::string& method);
177 
178 extern CRPCTable tableRPC;
179 
180 void StartRPC();
181 void InterruptRPC();
182 void StopRPC();
183 std::string JSONRPCExecBatch(const JSONRPCRequest& jreq, const UniValue& vReq);
184 
185 // Retrieves any serialization flags requested in command line argument
187 
188 #endif // BITCOIN_RPC_SERVER_H
RPC timer "driver".
Definition: server.h:60
std::string category
Definition: server.h:127
bool IsRPCRunning()
Query whether RPC is running.
Definition: server.cpp:315
RPC command dispatcher.
Definition: server.h:137
static const unsigned int DEFAULT_RPC_SERIALIZE_VERSION
Definition: server.h:20
Actor actor
Definition: server.h:129
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:100
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:89
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:97
void RPCSetTimerInterface(RPCTimerInterface *iface)
Set the factory function for timers.
Definition: server.cpp:488
std::vector< std::string > argNames
Definition: server.h:130
CRPCTable tableRPC
Definition: server.cpp:517
std::string name
Definition: server.h:128
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:120
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:107
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:140
intptr_t unique_id
Definition: server.h:131
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
UniValue(* rpcfn_type)(const JSONRPCRequest &jsonRequest)
Definition: server.h:88
static RPCHelpMan help()
Definition: server.cpp:133
bool RPCIsInWarmup(std::string *outStatus)
Definition: server.cpp:338