Bitcoin Core  29.1.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 
22 bool IsRPCRunning();
23 
26 
31 void SetRPCWarmupStatus(const std::string& newStatus);
32 /* Mark warmup as done. RPC calls will be processed from now on. */
34 
35 /* returns the current warmup state. */
36 bool RPCIsInWarmup(std::string *outStatus);
37 
43 {
44 public:
45  virtual ~RPCTimerBase() = default;
46 };
47 
52 {
53 public:
54  virtual ~RPCTimerInterface() = default;
56  virtual const char *Name() = 0;
63  virtual RPCTimerBase* NewTimer(std::function<void()>& func, int64_t millis) = 0;
64 };
65 
72 
77 void RPCRunLater(const std::string& name, std::function<void()> func, int64_t nSeconds);
78 
80 
82 {
83 public:
87  using Actor = std::function<bool(const JSONRPCRequest& request, UniValue& result, bool last_handler)>;
88 
90  CRPCCommand(std::string category, std::string name, Actor actor, std::vector<std::pair<std::string, bool>> args, intptr_t unique_id)
91  : category(std::move(category)), name(std::move(name)), actor(std::move(actor)), argNames(std::move(args)),
93  {
94  }
95 
98  : CRPCCommand(
99  category,
100  fn().m_name,
101  [fn](const JSONRPCRequest& request, UniValue& result, bool) { result = fn().HandleRequest(request); return true; },
102  fn().GetArgNames(),
103  intptr_t(fn))
104  {
105  }
106 
107  std::string category;
108  std::string name;
119  std::vector<std::pair<std::string, bool>> argNames;
120  intptr_t unique_id;
121 };
122 
127 {
128 private:
129  std::map<std::string, std::vector<const CRPCCommand*>> mapCommands;
130 public:
131  CRPCTable();
132  std::string help(const std::string& name, const JSONRPCRequest& helpreq) const;
133 
140  UniValue execute(const JSONRPCRequest &request) const;
141 
146  std::vector<std::string> listCommands() const;
147 
151  UniValue dumpArgMap(const JSONRPCRequest& request) const;
152 
165  void appendCommand(const std::string& name, const CRPCCommand* pcmd);
166  bool removeCommand(const std::string& name, const CRPCCommand* pcmd);
167 };
168 
169 bool IsDeprecatedRPCEnabled(const std::string& method);
170 
171 extern CRPCTable tableRPC;
172 
173 void StartRPC();
174 void InterruptRPC();
175 void StopRPC();
176 UniValue JSONRPCExec(const JSONRPCRequest& jreq, bool catch_errors);
177 
178 #endif // BITCOIN_RPC_SERVER_H
RPC timer "driver".
Definition: server.h:51
std::string help(const std::string &name, const JSONRPCRequest &helpreq) const
Definition: server.cpp:72
std::string category
Definition: server.h:107
bool IsRPCRunning()
Query whether RPC is running.
Definition: server.cpp:310
RPC command dispatcher.
Definition: server.h:126
Actor actor
Definition: server.h:109
void appendCommand(const std::string &name, const CRPCCommand *pcmd)
Appends a CRPCCommand to the dispatch table.
Definition: server.cpp:260
void SetRPCWarmupFinished()
Definition: server.cpp:326
RPCHelpMan(* RpcMethodFnType)()
Definition: server.h:79
void RpcInterruptionPoint()
Throw JSONRPCError if RPC is not running.
Definition: server.cpp:315
bool removeCommand(const std::string &name, const CRPCCommand *pcmd)
Definition: server.cpp:267
std::function< bool(const JSONRPCRequest &request, UniValue &result, bool last_handler)> Actor
RPC method handler reading request and assigning result.
Definition: server.h:87
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:90
void RPCSetTimerInterface(RPCTimerInterface *iface)
Set the factory function for timers.
Definition: server.cpp:552
UniValue execute(const JSONRPCRequest &request) const
Execute a method.
Definition: server.cpp:484
CRPCTable tableRPC
Definition: server.cpp:573
std::string name
Definition: server.h:108
ArgsManager & args
Definition: bitcoind.cpp:277
const char * name
Definition: rest.cpp:49
bool IsDeprecatedRPCEnabled(const std::string &method)
Definition: server.cpp:341
std::vector< std::pair< std::string, bool > > argNames
List of method arguments and whether they are named-only.
Definition: server.h:119
void RPCRunLater(const std::string &name, std::function< void()> func, int64_t nSeconds)
Run func nSeconds from now.
Definition: server.cpp:563
virtual ~RPCTimerInterface()=default
virtual RPCTimerBase * NewTimer(std::function< void()> &func, int64_t millis)=0
Factory function for timers.
auto result
Definition: common-types.h:74
void RPCUnsetTimerInterface(RPCTimerInterface *iface)
Unset factory function for timers.
Definition: server.cpp:557
void StopRPC()
Definition: server.cpp:297
void StartRPC()
Definition: server.cpp:280
void InterruptRPC()
Definition: server.cpp:286
Opaque base class for timers returned by NewTimerFunc.
Definition: server.h:42
virtual const char * Name()=0
Implementation name.
std::map< std::string, std::vector< const CRPCCommand * > > mapCommands
Definition: server.h:129
UniValue JSONRPCExec(const JSONRPCRequest &jreq, bool catch_errors)
Definition: server.cpp:348
virtual ~RPCTimerBase()=default
intptr_t unique_id
Definition: server.h:120
void SetRPCWarmupStatus(const std::string &newStatus)
Set the RPC warmup status.
Definition: server.cpp:320
std::vector< std::string > listCommands() const
Returns a list of registered commands.
Definition: server.cpp:521
void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface)
Set the factory function for timer, but only, if unset.
Definition: server.cpp:546
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:529
CRPCTable()
Definition: server.cpp:253
bool RPCIsInWarmup(std::string *outStatus)
Definition: server.cpp:333
CRPCCommand(std::string category, RpcMethodFnType fn)
Simplified constructor taking plain RpcMethodFnType function pointer.
Definition: server.h:97