Bitcoin Core  27.99.0
P2P Digital Currency
server.h
Go to the documentation of this file.
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2021 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 <rpc/request.h>
10 #include <rpc/util.h>
11 
12 #include <functional>
13 #include <map>
14 #include <stdint.h>
15 #include <string>
16 
17 #include <univalue.h>
18 
19 class CRPCCommand;
20 
21 namespace RPCServer
22 {
23  void OnStarted(std::function<void ()> slot);
24  void OnStopped(std::function<void ()> slot);
25 }
26 
28 bool IsRPCRunning();
29 
32 
37 void SetRPCWarmupStatus(const std::string& newStatus);
38 /* Mark warmup as done. RPC calls will be processed from now on. */
40 
41 /* returns the current warmup state. */
42 bool RPCIsInWarmup(std::string *outStatus);
43 
49 {
50 public:
51  virtual ~RPCTimerBase() {}
52 };
53 
58 {
59 public:
60  virtual ~RPCTimerInterface() {}
62  virtual const char *Name() = 0;
69  virtual RPCTimerBase* NewTimer(std::function<void()>& func, int64_t millis) = 0;
70 };
71 
78 
83 void RPCRunLater(const std::string& name, std::function<void()> func, int64_t nSeconds);
84 
86 
88 {
89 public:
93  using Actor = std::function<bool(const JSONRPCRequest& request, UniValue& result, bool last_handler)>;
94 
96  CRPCCommand(std::string category, std::string name, Actor actor, std::vector<std::pair<std::string, bool>> args, intptr_t unique_id)
97  : category(std::move(category)), name(std::move(name)), actor(std::move(actor)), argNames(std::move(args)),
99  {
100  }
101 
104  : CRPCCommand(
105  category,
106  fn().m_name,
107  [fn](const JSONRPCRequest& request, UniValue& result, bool) { result = fn().HandleRequest(request); return true; },
108  fn().GetArgNames(),
109  intptr_t(fn))
110  {
111  }
112 
113  std::string category;
114  std::string name;
125  std::vector<std::pair<std::string, bool>> argNames;
126  intptr_t unique_id;
127 };
128 
133 {
134 private:
135  std::map<std::string, std::vector<const CRPCCommand*>> mapCommands;
136 public:
137  CRPCTable();
138  std::string help(const std::string& name, const JSONRPCRequest& helpreq) const;
139 
146  UniValue execute(const JSONRPCRequest &request) const;
147 
152  std::vector<std::string> listCommands() const;
153 
157  UniValue dumpArgMap(const JSONRPCRequest& request) const;
158 
171  void appendCommand(const std::string& name, const CRPCCommand* pcmd);
172  bool removeCommand(const std::string& name, const CRPCCommand* pcmd);
173 };
174 
175 bool IsDeprecatedRPCEnabled(const std::string& method);
176 
177 extern CRPCTable tableRPC;
178 
179 void StartRPC();
180 void InterruptRPC();
181 void StopRPC();
182 UniValue JSONRPCExec(const JSONRPCRequest& jreq, bool catch_errors);
183 
184 #endif // BITCOIN_RPC_SERVER_H
ArgsManager & args
Definition: bitcoind.cpp:267
std::string category
Definition: server.h:113
intptr_t unique_id
Definition: server.h:126
std::vector< std::pair< std::string, bool > > argNames
List of method arguments and whether they are named-only.
Definition: server.h:125
CRPCCommand(std::string category, std::string name, Actor actor, std::vector< std::pair< std::string, bool >> args, intptr_t unique_id)
Constructor taking Actor callback supporting multiple handlers.
Definition: server.h:96
std::string name
Definition: server.h:114
std::function< bool(const JSONRPCRequest &request, UniValue &result, bool last_handler)> Actor
RPC method handler reading request and assigning result.
Definition: server.h:93
Actor actor
Definition: server.h:115
CRPCCommand(std::string category, RpcMethodFnType fn)
Simplified constructor taking plain RpcMethodFnType function pointer.
Definition: server.h:103
RPC command dispatcher.
Definition: server.h:133
CRPCTable()
Definition: server.cpp:267
std::map< std::string, std::vector< const CRPCCommand * > > mapCommands
Definition: server.h:135
bool removeCommand(const std::string &name, const CRPCCommand *pcmd)
Definition: server.cpp:281
std::vector< std::string > listCommands() const
Returns a list of registered commands.
Definition: server.cpp:536
UniValue execute(const JSONRPCRequest &request) const
Execute a method.
Definition: server.cpp:499
void appendCommand(const std::string &name, const CRPCCommand *pcmd)
Appends a CRPCCommand to the dispatch table.
Definition: server.cpp:274
std::string help(const std::string &name, const JSONRPCRequest &helpreq) const
Definition: server.cpp:86
UniValue dumpArgMap(const JSONRPCRequest &request) const
Return all named arguments that need to be converted by the client from string to another JSON type.
Definition: server.cpp:544
Opaque base class for timers returned by NewTimerFunc.
Definition: server.h:49
virtual ~RPCTimerBase()
Definition: server.h:51
RPC timer "driver".
Definition: server.h:58
virtual RPCTimerBase * NewTimer(std::function< void()> &func, int64_t millis)=0
Factory function for timers.
virtual ~RPCTimerInterface()
Definition: server.h:60
virtual const char * Name()=0
Implementation name.
void OnStarted(std::function< void()> slot)
Definition: server.cpp:76
void OnStopped(std::function< void()> slot)
Definition: server.cpp:81
const char * name
Definition: rest.cpp:48
void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface)
Set the factory function for timer, but only, if unset.
Definition: server.cpp:561
bool IsDeprecatedRPCEnabled(const std::string &method)
Definition: server.cpp:356
void SetRPCWarmupFinished()
Definition: server.cpp:341
void StartRPC()
Definition: server.cpp:294
void RPCUnsetTimerInterface(RPCTimerInterface *iface)
Unset factory function for timers.
Definition: server.cpp:572
void RPCRunLater(const std::string &name, std::function< void()> func, int64_t nSeconds)
Run func nSeconds from now.
Definition: server.cpp:578
bool RPCIsInWarmup(std::string *outStatus)
Definition: server.cpp:348
RPCHelpMan(* RpcMethodFnType)()
Definition: server.h:85
void StopRPC()
Definition: server.cpp:312
bool IsRPCRunning()
Query whether RPC is running.
Definition: server.cpp:325
void InterruptRPC()
Definition: server.cpp:301
UniValue JSONRPCExec(const JSONRPCRequest &jreq, bool catch_errors)
Definition: server.cpp:363
void SetRPCWarmupStatus(const std::string &newStatus)
Set the RPC warmup status.
Definition: server.cpp:335
CRPCTable tableRPC
Definition: server.cpp:588
void RPCSetTimerInterface(RPCTimerInterface *iface)
Set the factory function for timers.
Definition: server.cpp:567
void RpcInterruptionPoint()
Throw JSONRPCError if RPC is not running.
Definition: server.cpp:330