Bitcoin Core  0.19.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 <map>
13 #include <stdint.h>
14 #include <string>
15 #include <functional>
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 
36 void SetRPCWarmupStatus(const std::string& newStatus);
37 /* Mark warmup as done. RPC calls will be processed from now on. */
39 
40 /* returns the current warmup state. */
41 bool RPCIsInWarmup(std::string *outStatus);
42 
48 {
49 public:
50  virtual ~RPCTimerBase() {}
51 };
52 
57 {
58 public:
59  virtual ~RPCTimerInterface() {}
61  virtual const char *Name() = 0;
68  virtual RPCTimerBase* NewTimer(std::function<void()>& func, int64_t millis) = 0;
69 };
70 
77 
82 void RPCRunLater(const std::string& name, std::function<void()> func, int64_t nSeconds);
83 
84 typedef UniValue(*rpcfn_type)(const JSONRPCRequest& jsonRequest);
85 
87 {
88 public:
92  using Actor = std::function<bool(const JSONRPCRequest& request, UniValue& result, bool last_handler)>;
93 
95  CRPCCommand(std::string category, std::string name, Actor actor, std::vector<std::string> args, intptr_t unique_id)
96  : category(std::move(category)), name(std::move(name)), actor(std::move(actor)), argNames(std::move(args)),
97  unique_id(unique_id)
98  {
99  }
100 
102  CRPCCommand(const char* category, const char* name, rpcfn_type fn, std::initializer_list<const char*> args)
103  : CRPCCommand(category, name,
104  [fn](const JSONRPCRequest& request, UniValue& result, bool) { result = fn(request); return true; },
105  {args.begin(), args.end()}, intptr_t(fn))
106  {
107  }
108 
109  std::string category;
110  std::string name;
112  std::vector<std::string> argNames;
113  intptr_t unique_id;
114 };
115 
120 {
121 private:
122  std::map<std::string, std::vector<const CRPCCommand*>> mapCommands;
123 public:
124  CRPCTable();
125  std::string help(const std::string& name, const JSONRPCRequest& helpreq) const;
126 
133  UniValue execute(const JSONRPCRequest &request) const;
134 
139  std::vector<std::string> listCommands() const;
140 
141 
154  bool appendCommand(const std::string& name, const CRPCCommand* pcmd);
155  bool removeCommand(const std::string& name, const CRPCCommand* pcmd);
156 };
157 
158 bool IsDeprecatedRPCEnabled(const std::string& method);
159 
160 extern CRPCTable tableRPC;
161 
162 void StartRPC();
163 void InterruptRPC();
164 void StopRPC();
165 std::string JSONRPCExecBatch(const JSONRPCRequest& jreq, const UniValue& vReq);
166 
167 // Retrieves any serialization flags requested in command line argument
169 
170 #endif // BITCOIN_RPC_SERVER_H
RPC timer "driver".
Definition: server.h:56
std::string category
Definition: server.h:109
bool IsRPCRunning()
Query whether RPC is running.
Definition: server.cpp:303
Bitcoin RPC command dispatcher.
Definition: server.h:119
static const unsigned int DEFAULT_RPC_SERIALIZE_VERSION
Definition: server.h:19
Actor actor
Definition: server.h:111
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:95
void SetRPCWarmupFinished()
Definition: server.cpp:314
void OnStopped(std::function< void()> slot)
Definition: server.cpp:71
std::function< bool(const JSONRPCRequest &request, UniValue &result, bool last_handler)> Actor
RPC method handler reading request and assigning result.
Definition: server.h:92
void RPCSetTimerInterface(RPCTimerInterface *iface)
Set the factory function for timers.
Definition: server.cpp:471
std::vector< std::string > argNames
Definition: server.h:112
CRPCTable tableRPC
Definition: server.cpp:499
std::string name
Definition: server.h:110
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:102
const char * name
Definition: rest.cpp:40
bool IsDeprecatedRPCEnabled(const std::string &method)
Definition: server.cpp:329
int RPCSerializationFlags()
Definition: server.cpp:491
virtual ~RPCTimerInterface()
Definition: server.h:59
virtual ~RPCTimerBase()
Definition: server.h:50
UniValue help(const JSONRPCRequest &jsonRequest)
Definition: server.cpp:130
void RPCRunLater(const std::string &name, std::function< void()> func, int64_t nSeconds)
Run func nSeconds from now.
Definition: server.cpp:482
void RPCUnsetTimerInterface(RPCTimerInterface *iface)
Unset factory function for timers.
Definition: server.cpp:476
void StopRPC()
Definition: server.cpp:295
void StartRPC()
Definition: server.cpp:281
void InterruptRPC()
Definition: server.cpp:288
Opaque base class for timers returned by NewTimerFunc.
Definition: server.h:47
void OnStarted(std::function< void()> slot)
Definition: server.cpp:66
std::map< std::string, std::vector< const CRPCCommand * > > mapCommands
Definition: server.h:122
intptr_t unique_id
Definition: server.h:113
void SetRPCWarmupStatus(const std::string &newStatus)
Set the RPC warmup status.
Definition: server.cpp:308
std::string JSONRPCExecBatch(const JSONRPCRequest &jreq, const UniValue &vReq)
Definition: server.cpp:359
void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface)
Set the factory function for timer, but only, if unset.
Definition: server.cpp:465
UniValue(* rpcfn_type)(const JSONRPCRequest &jsonRequest)
Definition: server.h:84
bool RPCIsInWarmup(std::string *outStatus)
Definition: server.cpp:321