Bitcoin Core  22.99.0
P2P Digital Currency
server.h
Go to the documentation of this file.
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 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 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 
34 
39 void SetRPCWarmupStatus(const std::string& newStatus);
40 /* Mark warmup as done. RPC calls will be processed from now on. */
42 
43 /* returns the current warmup state. */
44 bool RPCIsInWarmup(std::string *outStatus);
45 
51 {
52 public:
53  virtual ~RPCTimerBase() {}
54 };
55 
60 {
61 public:
62  virtual ~RPCTimerInterface() {}
64  virtual const char *Name() = 0;
71  virtual RPCTimerBase* NewTimer(std::function<void()>& func, int64_t millis) = 0;
72 };
73 
80 
85 void RPCRunLater(const std::string& name, std::function<void()> func, int64_t nSeconds);
86 
88 
90 {
91 public:
95  using Actor = std::function<bool(const JSONRPCRequest& request, UniValue& result, bool last_handler)>;
96 
98  CRPCCommand(std::string category, std::string name, Actor actor, std::vector<std::string> args, intptr_t unique_id)
99  : category(std::move(category)), name(std::move(name)), actor(std::move(actor)), argNames(std::move(args)),
101  {
102  }
103 
106  : CRPCCommand(
107  category,
108  fn().m_name,
109  [fn](const JSONRPCRequest& request, UniValue& result, bool) { result = fn().HandleRequest(request); return true; },
110  fn().GetArgNames(),
111  intptr_t(fn))
112  {
113  }
114 
115  std::string category;
116  std::string name;
118  std::vector<std::string> argNames;
119  intptr_t unique_id;
120 };
121 
126 {
127 private:
128  std::map<std::string, std::vector<const CRPCCommand*>> mapCommands;
129 public:
130  CRPCTable();
131  std::string help(const std::string& name, const JSONRPCRequest& helpreq) const;
132 
139  UniValue execute(const JSONRPCRequest &request) const;
140 
145  std::vector<std::string> listCommands() const;
146 
150  UniValue dumpArgMap(const JSONRPCRequest& request) const;
151 
164  void appendCommand(const std::string& name, const CRPCCommand* pcmd);
165  bool removeCommand(const std::string& name, const CRPCCommand* pcmd);
166 };
167 
168 bool IsDeprecatedRPCEnabled(const std::string& method);
169 
170 extern CRPCTable tableRPC;
171 
172 void StartRPC();
173 void InterruptRPC();
174 void StopRPC();
175 std::string JSONRPCExecBatch(const JSONRPCRequest& jreq, const UniValue& vReq);
176 
177 // Retrieves any serialization flags requested in command line argument
179 
180 #endif // BITCOIN_RPC_SERVER_H
CRPCTable::mapCommands
std::map< std::string, std::vector< const CRPCCommand * > > mapCommands
Definition: server.h:128
IsDeprecatedRPCEnabled
bool IsDeprecatedRPCEnabled(const std::string &method)
Definition: server.cpp:352
StopRPC
void StopRPC()
Definition: server.cpp:308
RPCTimerInterface::NewTimer
virtual RPCTimerBase * NewTimer(std::function< void()> &func, int64_t millis)=0
Factory function for timers.
JSONRPCExecBatch
std::string JSONRPCExecBatch(const JSONRPCRequest &jreq, const UniValue &vReq)
Definition: server.cpp:382
RPCTimerInterface
RPC timer "driver".
Definition: server.h:59
RPCHelpMan
Definition: util.h:345
StartRPC
void StartRPC()
Definition: server.cpp:290
RPCTimerBase
Opaque base class for timers returned by NewTimerFunc.
Definition: server.h:50
SetRPCWarmupStatus
void SetRPCWarmupStatus(const std::string &newStatus)
Set the RPC warmup status.
Definition: server.cpp:331
RPCUnsetTimerInterface
void RPCUnsetTimerInterface(RPCTimerInterface *iface)
Unset factory function for timers.
Definition: server.cpp:524
RPCServer::OnStopped
void OnStopped(std::function< void()> slot)
Definition: server.cpp:74
RPCServer
Definition: server.h:23
UniValue
Definition: univalue.h:17
RPCIsInWarmup
bool RPCIsInWarmup(std::string *outStatus)
Definition: server.cpp:344
CRPCTable::CRPCTable
CRPCTable()
Definition: server.cpp:263
CRPCCommand
Definition: server.h:89
CRPCCommand::unique_id
intptr_t unique_id
Definition: server.h:119
RPCRunLater
void RPCRunLater(const std::string &name, std::function< void()> func, int64_t nSeconds)
Run func nSeconds from now.
Definition: server.cpp:530
CRPCTable::execute
UniValue execute(const JSONRPCRequest &request) const
Execute a method.
Definition: server.cpp:451
univalue.h
CRPCCommand::CRPCCommand
CRPCCommand(std::string category, RpcMethodFnType fn)
Simplified constructor taking plain RpcMethodFnType function pointer.
Definition: server.h:105
CRPCTable::listCommands
std::vector< std::string > listCommands() const
Returns a list of registered commands.
Definition: server.cpp:489
SetRPCWarmupFinished
void SetRPCWarmupFinished()
Definition: server.cpp:337
RPCSetTimerInterface
void RPCSetTimerInterface(RPCTimerInterface *iface)
Set the factory function for timers.
Definition: server.cpp:519
DEFAULT_RPC_SERIALIZE_VERSION
static const unsigned int DEFAULT_RPC_SERIALIZE_VERSION
Definition: server.h:19
request.h
InterruptRPC
void InterruptRPC()
Definition: server.cpp:297
CRPCCommand::category
std::string category
Definition: server.h:115
RPCTimerBase::~RPCTimerBase
virtual ~RPCTimerBase()
Definition: server.h:53
RPCTimerInterface::Name
virtual const char * Name()=0
Implementation name.
CRPCTable
RPC command dispatcher.
Definition: server.h:125
name
const char * name
Definition: rest.cpp:43
RPCSetTimerInterfaceIfUnset
void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface)
Set the factory function for timer, but only, if unset.
Definition: server.cpp:513
CRPCCommand::actor
Actor actor
Definition: server.h:117
CRPCTable::appendCommand
void appendCommand(const std::string &name, const CRPCCommand *pcmd)
Appends a CRPCCommand to the dispatch table.
Definition: server.cpp:270
CRPCCommand::CRPCCommand
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:98
CRPCCommand::Actor
std::function< bool(const JSONRPCRequest &request, UniValue &result, bool last_handler)> Actor
RPC method handler reading request and assigning result.
Definition: server.h:95
CRPCTable::help
std::string help(const std::string &name, const JSONRPCRequest &helpreq) const
Definition: server.cpp:79
CRPCTable::removeCommand
bool removeCommand(const std::string &name, const CRPCCommand *pcmd)
Definition: server.cpp:277
RpcInterruptionPoint
void RpcInterruptionPoint()
Throw JSONRPCError if RPC is not running.
Definition: server.cpp:326
IsRPCRunning
bool IsRPCRunning()
Query whether RPC is running.
Definition: server.cpp:321
CRPCTable::dumpArgMap
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:496
RPCServer::OnStarted
void OnStarted(std::function< void()> slot)
Definition: server.cpp:69
RPCTimerInterface::~RPCTimerInterface
virtual ~RPCTimerInterface()
Definition: server.h:62
JSONRPCRequest
Definition: request.h:28
util.h
RpcMethodFnType
RPCHelpMan(* RpcMethodFnType)()
Definition: server.h:87
CRPCCommand::argNames
std::vector< std::string > argNames
Definition: server.h:118
tableRPC
CRPCTable tableRPC
Definition: server.cpp:548
CRPCCommand::name
std::string name
Definition: server.h:116
RPCSerializationFlags
int RPCSerializationFlags()
Definition: server.cpp:540