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 
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 
87 typedef UniValue(*rpcfn_type)(const JSONRPCRequest& jsonRequest);
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)),
100  unique_id(unique_id)
101  {
102  }
103 
105  CRPCCommand(const char* category, const char* name, rpcfn_type fn, std::initializer_list<const char*> args)
106  : CRPCCommand(category, name,
107  [fn](const JSONRPCRequest& request, UniValue& result, bool) { result = fn(request); return true; },
108  {args.begin(), args.end()}, intptr_t(fn))
109  {
110  }
111 
112  std::string category;
113  std::string name;
115  std::vector<std::string> argNames;
116  intptr_t unique_id;
117 };
118 
123 {
124 private:
125  std::map<std::string, std::vector<const CRPCCommand*>> mapCommands;
126 public:
127  CRPCTable();
128  std::string help(const std::string& name, const JSONRPCRequest& helpreq) const;
129 
136  UniValue execute(const JSONRPCRequest &request) const;
137 
142  std::vector<std::string> listCommands() const;
143 
144 
157  bool appendCommand(const std::string& name, const CRPCCommand* pcmd);
158  bool removeCommand(const std::string& name, const CRPCCommand* pcmd);
159 };
160 
161 bool IsDeprecatedRPCEnabled(const std::string& method);
162 
163 extern CRPCTable tableRPC;
164 
165 void StartRPC();
166 void InterruptRPC();
167 void StopRPC();
168 std::string JSONRPCExecBatch(const JSONRPCRequest& jreq, const UniValue& vReq);
169 
170 // Retrieves any serialization flags requested in command line argument
172 
173 #endif // BITCOIN_RPC_SERVER_H
RPC timer "driver".
Definition: server.h:59
std::string category
Definition: server.h:112
bool IsRPCRunning()
Query whether RPC is running.
Definition: server.cpp:318
Bitcoin RPC command dispatcher.
Definition: server.h:122
static const unsigned int DEFAULT_RPC_SERIALIZE_VERSION
Definition: server.h:19
Actor actor
Definition: server.h:114
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
void SetRPCWarmupFinished()
Definition: server.cpp:334
void OnStopped(std::function< void()> slot)
Definition: server.cpp:74
void RpcInterruptionPoint()
Throw JSONRPCError if RPC is not running.
Definition: server.cpp:323
std::function< bool(const JSONRPCRequest &request, UniValue &result, bool last_handler)> Actor
RPC method handler reading request and assigning result.
Definition: server.h:95
void RPCSetTimerInterface(RPCTimerInterface *iface)
Set the factory function for timers.
Definition: server.cpp:491
std::vector< std::string > argNames
Definition: server.h:115
CRPCTable tableRPC
Definition: server.cpp:520
std::string name
Definition: server.h:113
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:105
const char * name
Definition: rest.cpp:41
bool IsDeprecatedRPCEnabled(const std::string &method)
Definition: server.cpp:349
int RPCSerializationFlags()
Definition: server.cpp:512
virtual ~RPCTimerInterface()
Definition: server.h:62
virtual ~RPCTimerBase()
Definition: server.h:53
UniValue help(const JSONRPCRequest &jsonRequest)
Definition: server.cpp:133
void RPCRunLater(const std::string &name, std::function< void()> func, int64_t nSeconds)
Run func nSeconds from now.
Definition: server.cpp:502
void RPCUnsetTimerInterface(RPCTimerInterface *iface)
Unset factory function for timers.
Definition: server.cpp:496
void StopRPC()
Definition: server.cpp:305
void StartRPC()
Definition: server.cpp:287
void InterruptRPC()
Definition: server.cpp:294
Opaque base class for timers returned by NewTimerFunc.
Definition: server.h:50
void OnStarted(std::function< void()> slot)
Definition: server.cpp:69
std::map< std::string, std::vector< const CRPCCommand * > > mapCommands
Definition: server.h:125
intptr_t unique_id
Definition: server.h:116
void SetRPCWarmupStatus(const std::string &newStatus)
Set the RPC warmup status.
Definition: server.cpp:328
std::string JSONRPCExecBatch(const JSONRPCRequest &jreq, const UniValue &vReq)
Definition: server.cpp:379
void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface)
Set the factory function for timer, but only, if unset.
Definition: server.cpp:485
UniValue(* rpcfn_type)(const JSONRPCRequest &jsonRequest)
Definition: server.h:87
bool RPCIsInWarmup(std::string *outStatus)
Definition: server.cpp:341