Bitcoin Core  31.0.0
P2P Digital Currency
server.h
Go to the documentation of this file.
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-present 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 <cstdint>
13 #include <functional>
14 #include <map>
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);
33 /* Mark warmup as done. RPC calls will be processed from now on. */
35 
36 /* returns the current warmup state. */
37 bool RPCIsInWarmup(std::string *outStatus);
38 
40 
42 {
43 public:
47  using Actor = std::function<bool(const JSONRPCRequest& request, UniValue& result, bool last_handler)>;
48 
50  CRPCCommand(std::string category, std::string name, Actor actor, std::vector<std::pair<std::string, bool>> args, intptr_t unique_id)
51  : category(std::move(category)), name(std::move(name)), actor(std::move(actor)), argNames(std::move(args)),
53  {
54  }
55 
58  : CRPCCommand(
59  category,
60  fn().m_name,
61  [fn](const JSONRPCRequest& request, UniValue& result, bool) { result = fn().HandleRequest(request); return true; },
62  fn().GetArgNames(),
63  intptr_t(fn))
64  {
65  }
66 
67  std::string category;
68  std::string name;
79  std::vector<std::pair<std::string, bool>> argNames;
80  intptr_t unique_id;
81 };
82 
86 class CRPCTable
87 {
88 private:
89  std::map<std::string, std::vector<const CRPCCommand*>> mapCommands;
90 public:
91  CRPCTable();
92  std::string help(std::string_view name, const JSONRPCRequest& helpreq) const;
93 
100  UniValue execute(const JSONRPCRequest &request) const;
101 
106  std::vector<std::string> listCommands() const;
107 
111  UniValue dumpArgMap(const JSONRPCRequest& request) const;
112 
125  void appendCommand(const std::string& name, const CRPCCommand* pcmd);
126  bool removeCommand(const std::string& name, const CRPCCommand* pcmd);
127 };
128 
129 bool IsDeprecatedRPCEnabled(const std::string& method);
130 
131 extern CRPCTable tableRPC;
132 
133 void StartRPC();
134 void InterruptRPC();
135 void StopRPC();
136 UniValue JSONRPCExec(const JSONRPCRequest& jreq, bool catch_errors);
137 
138 #endif // BITCOIN_RPC_SERVER_H
std::string category
Definition: server.h:67
bool IsRPCRunning()
Query whether RPC is running.
Definition: server.cpp:302
RPC command dispatcher.
Definition: server.h:86
Actor actor
Definition: server.h:69
void appendCommand(const std::string &name, const CRPCCommand *pcmd)
Appends a CRPCCommand to the dispatch table.
Definition: server.cpp:253
void SetRPCWarmupFinished()
Definition: server.cpp:324
RPCHelpMan(* RpcMethodFnType)()
Definition: server.h:39
void RpcInterruptionPoint()
Throw JSONRPCError if RPC is not running.
Definition: server.cpp:307
bool removeCommand(const std::string &name, const CRPCCommand *pcmd)
Definition: server.cpp:260
std::function< bool(const JSONRPCRequest &request, UniValue &result, bool last_handler)> Actor
RPC method handler reading request and assigning result.
Definition: server.h:47
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:50
Definition: common.h:29
UniValue execute(const JSONRPCRequest &request) const
Execute a method.
Definition: server.cpp:482
CRPCTable tableRPC
Definition: server.cpp:544
std::string name
Definition: server.h:68
ArgsManager & args
Definition: bitcoind.cpp:277
const char * name
Definition: rest.cpp:48
bool IsDeprecatedRPCEnabled(const std::string &method)
Definition: server.cpp:339
std::vector< std::pair< std::string, bool > > argNames
List of method arguments and whether they are named-only.
Definition: server.h:79
auto result
Definition: common-types.h:74
void StopRPC()
Definition: server.cpp:290
void StartRPC()
Definition: server.cpp:273
void InterruptRPC()
Definition: server.cpp:279
std::string help(std::string_view name, const JSONRPCRequest &helpreq) const
Definition: server.cpp:69
std::map< std::string, std::vector< const CRPCCommand * > > mapCommands
Definition: server.h:89
void SetRPCWarmupStarting()
Definition: server.cpp:318
UniValue JSONRPCExec(const JSONRPCRequest &jreq, bool catch_errors)
Definition: server.cpp:346
intptr_t unique_id
Definition: server.h:80
void SetRPCWarmupStatus(const std::string &newStatus)
Set the RPC warmup status.
Definition: server.cpp:312
std::vector< std::string > listCommands() const
Returns a list of registered commands.
Definition: server.cpp:519
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:527
CRPCTable()
Definition: server.cpp:246
bool RPCIsInWarmup(std::string *outStatus)
Definition: server.cpp:331
CRPCCommand(std::string category, RpcMethodFnType fn)
Simplified constructor taking plain RpcMethodFnType function pointer.
Definition: server.h:57