Bitcoin Core  0.18.99
P2P Digital Currency
server.h
Go to the documentation of this file.
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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/protocol.h>
11 #include <uint256.h>
12 
13 #include <list>
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 {
32 public:
34  std::string strMethod;
36  bool fHelp;
37  std::string URI;
38  std::string authUser;
39  std::string peerAddr;
40 
41  JSONRPCRequest() : id(NullUniValue), params(NullUniValue), fHelp(false) {}
42  void parse(const UniValue& valRequest);
43 };
44 
46 bool IsRPCRunning();
47 
52 void SetRPCWarmupStatus(const std::string& newStatus);
53 /* Mark warmup as done. RPC calls will be processed from now on. */
55 
56 /* returns the current warmup state. */
57 bool RPCIsInWarmup(std::string *outStatus);
58 
64 {
65 public:
66  virtual ~RPCTimerBase() {}
67 };
68 
73 {
74 public:
75  virtual ~RPCTimerInterface() {}
77  virtual const char *Name() = 0;
84  virtual RPCTimerBase* NewTimer(std::function<void()>& func, int64_t millis) = 0;
85 };
86 
93 
98 void RPCRunLater(const std::string& name, std::function<void()> func, int64_t nSeconds);
99 
100 typedef UniValue(*rpcfn_type)(const JSONRPCRequest& jsonRequest);
101 
103 {
104 public:
108  using Actor = std::function<bool(const JSONRPCRequest& request, UniValue& result, bool last_handler)>;
109 
111  CRPCCommand(std::string category, std::string name, Actor actor, std::vector<std::string> args, intptr_t unique_id)
112  : category(std::move(category)), name(std::move(name)), actor(std::move(actor)), argNames(std::move(args)),
113  unique_id(unique_id)
114  {
115  }
116 
118  CRPCCommand(const char* category, const char* name, rpcfn_type fn, std::initializer_list<const char*> args)
119  : CRPCCommand(category, name,
120  [fn](const JSONRPCRequest& request, UniValue& result, bool) { result = fn(request); return true; },
121  {args.begin(), args.end()}, intptr_t(fn))
122  {
123  }
124 
125  std::string category;
126  std::string name;
128  std::vector<std::string> argNames;
129  intptr_t unique_id;
130 };
131 
136 {
137 private:
138  std::map<std::string, std::vector<const CRPCCommand*>> mapCommands;
139 public:
140  CRPCTable();
141  std::string help(const std::string& name, const JSONRPCRequest& helpreq) const;
142 
149  UniValue execute(const JSONRPCRequest &request) const;
150 
155  std::vector<std::string> listCommands() const;
156 
157 
170  bool appendCommand(const std::string& name, const CRPCCommand* pcmd);
171  bool removeCommand(const std::string& name, const CRPCCommand* pcmd);
172 };
173 
174 bool IsDeprecatedRPCEnabled(const std::string& method);
175 
176 extern CRPCTable tableRPC;
177 
178 void StartRPC();
179 void InterruptRPC();
180 void StopRPC();
181 std::string JSONRPCExecBatch(const JSONRPCRequest& jreq, const UniValue& vReq);
182 
183 // Retrieves any serialization flags requested in command line argument
185 
186 #endif // BITCOIN_RPC_SERVER_H
RPC timer "driver".
Definition: server.h:72
std::string category
Definition: server.h:125
bool IsRPCRunning()
Query whether RPC is running.
Definition: server.cpp:308
Bitcoin RPC command dispatcher.
Definition: server.h:135
Actor actor
Definition: server.h:127
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:111
void SetRPCWarmupFinished()
Definition: server.cpp:319
void OnStopped(std::function< void()> slot)
Definition: server.cpp:75
std::function< bool(const JSONRPCRequest &request, UniValue &result, bool last_handler)> Actor
RPC method handler reading request and assigning result.
Definition: server.h:108
void RPCSetTimerInterface(RPCTimerInterface *iface)
Set the factory function for timers.
Definition: server.cpp:509
std::vector< std::string > argNames
Definition: server.h:128
std::string strMethod
Definition: server.h:34
CRPCTable tableRPC
Definition: server.cpp:537
std::string name
Definition: server.h:126
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:118
std::string peerAddr
Definition: server.h:39
UniValue params
Definition: server.h:35
const char * name
Definition: rest.cpp:38
bool IsDeprecatedRPCEnabled(const std::string &method)
Definition: server.cpp:367
UniValue id
Definition: server.h:33
int RPCSerializationFlags()
Definition: server.cpp:529
virtual ~RPCTimerInterface()
Definition: server.h:75
virtual ~RPCTimerBase()
Definition: server.h:66
UniValue help(const JSONRPCRequest &jsonRequest)
Definition: server.cpp:134
void RPCRunLater(const std::string &name, std::function< void()> func, int64_t nSeconds)
Run func nSeconds from now.
Definition: server.cpp:520
bool fHelp
Definition: server.h:36
void RPCUnsetTimerInterface(RPCTimerInterface *iface)
Unset factory function for timers.
Definition: server.cpp:514
JSONRPCRequest()
Definition: server.h:41
void StopRPC()
Definition: server.cpp:300
std::string URI
Definition: server.h:37
void StartRPC()
Definition: server.cpp:286
void InterruptRPC()
Definition: server.cpp:293
std::string authUser
Definition: server.h:38
Opaque base class for timers returned by NewTimerFunc.
Definition: server.h:63
const UniValue NullUniValue
Definition: univalue.cpp:13
void OnStarted(std::function< void()> slot)
Definition: server.cpp:70
std::map< std::string, std::vector< const CRPCCommand * > > mapCommands
Definition: server.h:138
intptr_t unique_id
Definition: server.h:129
void SetRPCWarmupStatus(const std::string &newStatus)
Set the RPC warmup status.
Definition: server.cpp:313
std::string JSONRPCExecBatch(const JSONRPCRequest &jreq, const UniValue &vReq)
Definition: server.cpp:397
void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface)
Set the factory function for timer, but only, if unset.
Definition: server.cpp:503
UniValue(* rpcfn_type)(const JSONRPCRequest &jsonRequest)
Definition: server.h:100
bool RPCIsInWarmup(std::string *outStatus)
Definition: server.cpp:326