Bitcoin Core 28.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
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
19class CRPCCommand;
20
21namespace RPCServer
22{
23 void OnStarted(std::function<void ()> slot);
24 void OnStopped(std::function<void ()> slot);
25}
26
28bool IsRPCRunning();
29
32
37void 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. */
42bool RPCIsInWarmup(std::string *outStatus);
43
49{
50public:
51 virtual ~RPCTimerBase() = default;
52};
53
58{
59public:
60 virtual ~RPCTimerInterface() = default;
62 virtual const char *Name() = 0;
69 virtual RPCTimerBase* NewTimer(std::function<void()>& func, int64_t millis) = 0;
70};
71
78
83void RPCRunLater(const std::string& name, std::function<void()> func, int64_t nSeconds);
84
86
88{
89public:
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{
134private:
135 std::map<std::string, std::vector<const CRPCCommand*>> mapCommands;
136public:
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
175bool IsDeprecatedRPCEnabled(const std::string& method);
176
177extern CRPCTable tableRPC;
178
179void StartRPC();
180void InterruptRPC();
181void StopRPC();
182UniValue JSONRPCExec(const JSONRPCRequest& jreq, bool catch_errors);
183
184#endif // BITCOIN_RPC_SERVER_H
ArgsManager & args
Definition bitcoind.cpp:270
std::string category
Definition server.h:113
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
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
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
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:283
std::vector< std::string > listCommands() const
Returns a list of registered commands.
Definition server.cpp:538
UniValue execute(const JSONRPCRequest &request) const
Execute a method.
Definition server.cpp:501
void appendCommand(const std::string &name, const CRPCCommand *pcmd)
Appends a CRPCCommand to the dispatch table.
Definition server.cpp:276
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:546
Opaque base class for timers returned by NewTimerFunc.
Definition server.h:49
virtual ~RPCTimerBase()=default
RPC timer "driver".
Definition server.h:58
virtual RPCTimerBase * NewTimer(std::function< void()> &func, int64_t millis)=0
Factory function for timers.
virtual const char * Name()=0
Implementation name.
virtual ~RPCTimerInterface()=default
void OnStarted(std::function< void()> slot)
Definition server.cpp:78
void OnStopped(std::function< void()> slot)
Definition server.cpp:83
const char * name
Definition rest.cpp:49
static RPCHelpMan help()
Definition server.cpp:143
void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface)
Set the factory function for timer, but only, if unset.
Definition server.cpp:563
bool IsDeprecatedRPCEnabled(const std::string &method)
Definition server.cpp:358
void SetRPCWarmupFinished()
Definition server.cpp:343
void StartRPC()
Definition server.cpp:296
void RPCUnsetTimerInterface(RPCTimerInterface *iface)
Unset factory function for timers.
Definition server.cpp:574
void RPCRunLater(const std::string &name, std::function< void()> func, int64_t nSeconds)
Run func nSeconds from now.
Definition server.cpp:580
bool RPCIsInWarmup(std::string *outStatus)
Definition server.cpp:350
RPCHelpMan(* RpcMethodFnType)()
Definition server.h:85
void StopRPC()
Definition server.cpp:314
bool IsRPCRunning()
Query whether RPC is running.
Definition server.cpp:327
void InterruptRPC()
Definition server.cpp:303
UniValue JSONRPCExec(const JSONRPCRequest &jreq, bool catch_errors)
Definition server.cpp:365
void SetRPCWarmupStatus(const std::string &newStatus)
Set the RPC warmup status.
Definition server.cpp:337
CRPCTable tableRPC
Definition server.cpp:590
void RPCSetTimerInterface(RPCTimerInterface *iface)
Set the factory function for timers.
Definition server.cpp:569
void RpcInterruptionPoint()
Throw JSONRPCError if RPC is not running.
Definition server.cpp:332