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/request.h>
11 #include <uint256.h>
12 
13 #include <list>
14 #include <map>
15 #include <stdint.h>
16 #include <string>
17 #include <functional>
18 
19 #include <univalue.h>
20 
21 static const unsigned int DEFAULT_RPC_SERIALIZE_VERSION = 1;
22 
23 class CRPCCommand;
24 
25 namespace RPCServer
26 {
27  void OnStarted(std::function<void ()> slot);
28  void OnStopped(std::function<void ()> slot);
29 }
30 
32 bool IsRPCRunning();
33 
38 void SetRPCWarmupStatus(const std::string& newStatus);
39 /* Mark warmup as done. RPC calls will be processed from now on. */
41 
42 /* returns the current warmup state. */
43 bool RPCIsInWarmup(std::string *outStatus);
44 
50 {
51 public:
52  virtual ~RPCTimerBase() {}
53 };
54 
59 {
60 public:
61  virtual ~RPCTimerInterface() {}
63  virtual const char *Name() = 0;
70  virtual RPCTimerBase* NewTimer(std::function<void()>& func, int64_t millis) = 0;
71 };
72 
79 
84 void RPCRunLater(const std::string& name, std::function<void()> func, int64_t nSeconds);
85 
86 typedef UniValue(*rpcfn_type)(const JSONRPCRequest& jsonRequest);
87 
89 {
90 public:
94  using Actor = std::function<bool(const JSONRPCRequest& request, UniValue& result, bool last_handler)>;
95 
97  CRPCCommand(std::string category, std::string name, Actor actor, std::vector<std::string> args, intptr_t unique_id)
98  : category(std::move(category)), name(std::move(name)), actor(std::move(actor)), argNames(std::move(args)),
99  unique_id(unique_id)
100  {
101  }
102 
104  CRPCCommand(const char* category, const char* name, rpcfn_type fn, std::initializer_list<const char*> args)
105  : CRPCCommand(category, name,
106  [fn](const JSONRPCRequest& request, UniValue& result, bool) { result = fn(request); return true; },
107  {args.begin(), args.end()}, intptr_t(fn))
108  {
109  }
110 
111  std::string category;
112  std::string name;
114  std::vector<std::string> argNames;
115  intptr_t unique_id;
116 };
117 
122 {
123 private:
124  std::map<std::string, std::vector<const CRPCCommand*>> mapCommands;
125 public:
126  CRPCTable();
127  std::string help(const std::string& name, const JSONRPCRequest& helpreq) const;
128 
135  UniValue execute(const JSONRPCRequest &request) const;
136 
141  std::vector<std::string> listCommands() const;
142 
143 
156  bool appendCommand(const std::string& name, const CRPCCommand* pcmd);
157  bool removeCommand(const std::string& name, const CRPCCommand* pcmd);
158 };
159 
160 bool IsDeprecatedRPCEnabled(const std::string& method);
161 
162 extern CRPCTable tableRPC;
163 
164 void StartRPC();
165 void InterruptRPC();
166 void StopRPC();
167 std::string JSONRPCExecBatch(const JSONRPCRequest& jreq, const UniValue& vReq);
168 
169 // Retrieves any serialization flags requested in command line argument
171 
172 #endif // BITCOIN_RPC_SERVER_H
RPC timer "driver".
Definition: server.h:58
std::string category
Definition: server.h:111
bool IsRPCRunning()
Query whether RPC is running.
Definition: server.cpp:305
Bitcoin RPC command dispatcher.
Definition: server.h:121
static const unsigned int DEFAULT_RPC_SERIALIZE_VERSION
Definition: server.h:21
Actor actor
Definition: server.h:113
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:97
void SetRPCWarmupFinished()
Definition: server.cpp:316
void OnStopped(std::function< void()> slot)
Definition: server.cpp:73
std::function< bool(const JSONRPCRequest &request, UniValue &result, bool last_handler)> Actor
RPC method handler reading request and assigning result.
Definition: server.h:94
void RPCSetTimerInterface(RPCTimerInterface *iface)
Set the factory function for timers.
Definition: server.cpp:473
std::vector< std::string > argNames
Definition: server.h:114
CRPCTable tableRPC
Definition: server.cpp:501
std::string name
Definition: server.h:112
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:104
const char * name
Definition: rest.cpp:39
bool IsDeprecatedRPCEnabled(const std::string &method)
Definition: server.cpp:331
int RPCSerializationFlags()
Definition: server.cpp:493
virtual ~RPCTimerInterface()
Definition: server.h:61
virtual ~RPCTimerBase()
Definition: server.h:52
UniValue help(const JSONRPCRequest &jsonRequest)
Definition: server.cpp:132
void RPCRunLater(const std::string &name, std::function< void()> func, int64_t nSeconds)
Run func nSeconds from now.
Definition: server.cpp:484
void RPCUnsetTimerInterface(RPCTimerInterface *iface)
Unset factory function for timers.
Definition: server.cpp:478
void StopRPC()
Definition: server.cpp:297
void StartRPC()
Definition: server.cpp:283
void InterruptRPC()
Definition: server.cpp:290
Opaque base class for timers returned by NewTimerFunc.
Definition: server.h:49
void OnStarted(std::function< void()> slot)
Definition: server.cpp:68
std::map< std::string, std::vector< const CRPCCommand * > > mapCommands
Definition: server.h:124
intptr_t unique_id
Definition: server.h:115
void SetRPCWarmupStatus(const std::string &newStatus)
Set the RPC warmup status.
Definition: server.cpp:310
std::string JSONRPCExecBatch(const JSONRPCRequest &jreq, const UniValue &vReq)
Definition: server.cpp:361
void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface)
Set the factory function for timer, but only, if unset.
Definition: server.cpp:467
UniValue(* rpcfn_type)(const JSONRPCRequest &jsonRequest)
Definition: server.h:86
bool RPCIsInWarmup(std::string *outStatus)
Definition: server.cpp:323