Bitcoin Core  21.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 <amount.h>
10 #include <rpc/request.h>
11 #include <rpc/util.h>
12 
13 #include <functional>
14 #include <map>
15 #include <stdint.h>
16 #include <string>
17 
18 #include <univalue.h>
19 
20 static const unsigned int DEFAULT_RPC_SERIALIZE_VERSION = 1;
21 
22 class CRPCCommand;
23 
24 namespace RPCServer
25 {
26  void OnStarted(std::function<void ()> slot);
27  void OnStopped(std::function<void ()> slot);
28 }
29 
31 bool IsRPCRunning();
32 
35 
40 void SetRPCWarmupStatus(const std::string& newStatus);
41 /* Mark warmup as done. RPC calls will be processed from now on. */
43 
44 /* returns the current warmup state. */
45 bool RPCIsInWarmup(std::string *outStatus);
46 
52 {
53 public:
54  virtual ~RPCTimerBase() {}
55 };
56 
61 {
62 public:
63  virtual ~RPCTimerInterface() {}
65  virtual const char *Name() = 0;
72  virtual RPCTimerBase* NewTimer(std::function<void()>& func, int64_t millis) = 0;
73 };
74 
81 
86 void RPCRunLater(const std::string& name, std::function<void()> func, int64_t nSeconds);
87 
89 
91 {
92 public:
96  using Actor = std::function<bool(const JSONRPCRequest& request, UniValue& result, bool last_handler)>;
97 
99  CRPCCommand(std::string category, std::string name, Actor actor, std::vector<std::string> args, intptr_t unique_id)
100  : category(std::move(category)), name(std::move(name)), actor(std::move(actor)), argNames(std::move(args)),
102  {
103  }
104 
107  : CRPCCommand(
108  category,
109  fn().m_name,
110  [fn](const JSONRPCRequest& request, UniValue& result, bool) { result = fn().HandleRequest(request); return true; },
111  fn().GetArgNames(),
112  intptr_t(fn))
113  {
114  }
115 
116  std::string category;
117  std::string name;
119  std::vector<std::string> 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 std::string JSONRPCExecBatch(const JSONRPCRequest& jreq, const UniValue& vReq);
177 
178 // Retrieves any serialization flags requested in command line argument
180 
181 #endif // BITCOIN_RPC_SERVER_H
CRPCTable::mapCommands
std::map< std::string, std::vector< const CRPCCommand * > > mapCommands
Definition: server.h:129
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:60
RPCHelpMan
Definition: util.h:338
StartRPC
void StartRPC()
Definition: server.cpp:290
RPCTimerBase
Opaque base class for timers returned by NewTimerFunc.
Definition: server.h:51
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:24
UniValue
Definition: univalue.h:19
RPCIsInWarmup
bool RPCIsInWarmup(std::string *outStatus)
Definition: server.cpp:344
CRPCTable::CRPCTable
CRPCTable()
Definition: server.cpp:263
CRPCCommand
Definition: server.h:90
CRPCCommand::unique_id
intptr_t unique_id
Definition: server.h:120
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:106
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:20
request.h
InterruptRPC
void InterruptRPC()
Definition: server.cpp:297
CRPCCommand::category
std::string category
Definition: server.h:116
RPCTimerBase::~RPCTimerBase
virtual ~RPCTimerBase()
Definition: server.h:54
RPCTimerInterface::Name
virtual const char * Name()=0
Implementation name.
CRPCTable
RPC command dispatcher.
Definition: server.h:126
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:118
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:99
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:96
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:63
JSONRPCRequest
Definition: request.h:28
util.h
RpcMethodFnType
RPCHelpMan(* RpcMethodFnType)()
Definition: server.h:88
CRPCCommand::argNames
std::vector< std::string > argNames
Definition: server.h:119
amount.h
tableRPC
CRPCTable tableRPC
Definition: server.cpp:548
CRPCCommand::name
std::string name
Definition: server.h:117
RPCSerializationFlags
int RPCSerializationFlags()
Definition: server.cpp:540