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
19static const unsigned int DEFAULT_RPC_SERIALIZE_VERSION = 1;
20
21class CRPCCommand;
22
23namespace RPCServer
24{
25 void OnStarted(std::function<void ()> slot);
26 void OnStopped(std::function<void ()> slot);
27}
28
30bool IsRPCRunning();
31
34
39void 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. */
44bool RPCIsInWarmup(std::string *outStatus);
45
51{
52public:
53 virtual ~RPCTimerBase() {}
54};
55
60{
61public:
62 virtual ~RPCTimerInterface() {}
64 virtual const char *Name() = 0;
71 virtual RPCTimerBase* NewTimer(std::function<void()>& func, int64_t millis) = 0;
72};
73
80
85void RPCRunLater(const std::string& name, std::function<void()> func, int64_t nSeconds);
86
88
90{
91public:
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{
127private:
128 std::map<std::string, std::vector<const CRPCCommand*>> mapCommands;
129public:
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
168bool IsDeprecatedRPCEnabled(const std::string& method);
169
170extern CRPCTable tableRPC;
171
172void StartRPC();
173void InterruptRPC();
174void StopRPC();
175std::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
std::vector< std::string > argNames
Definition: server.h:118
std::string category
Definition: server.h:115
intptr_t unique_id
Definition: server.h:119
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
std::string name
Definition: server.h:116
std::function< bool(const JSONRPCRequest &request, UniValue &result, bool last_handler)> Actor
RPC method handler reading request and assigning result.
Definition: server.h:95
Actor actor
Definition: server.h:117
CRPCCommand(std::string category, RpcMethodFnType fn)
Simplified constructor taking plain RpcMethodFnType function pointer.
Definition: server.h:105
RPC command dispatcher.
Definition: server.h:126
CRPCTable()
Definition: server.cpp:263
std::map< std::string, std::vector< const CRPCCommand * > > mapCommands
Definition: server.h:128
bool removeCommand(const std::string &name, const CRPCCommand *pcmd)
Definition: server.cpp:277
std::vector< std::string > listCommands() const
Returns a list of registered commands.
Definition: server.cpp:489
UniValue execute(const JSONRPCRequest &request) const
Execute a method.
Definition: server.cpp:451
void appendCommand(const std::string &name, const CRPCCommand *pcmd)
Appends a CRPCCommand to the dispatch table.
Definition: server.cpp:270
std::string help(const std::string &name, const JSONRPCRequest &helpreq) const
Definition: server.cpp:79
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
Opaque base class for timers returned by NewTimerFunc.
Definition: server.h:51
virtual ~RPCTimerBase()
Definition: server.h:53
RPC timer "driver".
Definition: server.h:60
virtual ~RPCTimerInterface()
Definition: server.h:62
virtual RPCTimerBase * NewTimer(std::function< void()> &func, int64_t millis)=0
Factory function for timers.
virtual const char * Name()=0
Implementation name.
void OnStarted(std::function< void()> slot)
Definition: server.cpp:69
void OnStopped(std::function< void()> slot)
Definition: server.cpp:74
const char * name
Definition: rest.cpp:43
void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface)
Set the factory function for timer, but only, if unset.
Definition: server.cpp:513
bool IsDeprecatedRPCEnabled(const std::string &method)
Definition: server.cpp:352
void SetRPCWarmupFinished()
Definition: server.cpp:337
static const unsigned int DEFAULT_RPC_SERIALIZE_VERSION
Definition: server.h:19
void StartRPC()
Definition: server.cpp:290
void RPCUnsetTimerInterface(RPCTimerInterface *iface)
Unset factory function for timers.
Definition: server.cpp:524
void RPCRunLater(const std::string &name, std::function< void()> func, int64_t nSeconds)
Run func nSeconds from now.
Definition: server.cpp:530
bool RPCIsInWarmup(std::string *outStatus)
Definition: server.cpp:344
RPCHelpMan(* RpcMethodFnType)()
Definition: server.h:87
void StopRPC()
Definition: server.cpp:308
bool IsRPCRunning()
Query whether RPC is running.
Definition: server.cpp:321
std::string JSONRPCExecBatch(const JSONRPCRequest &jreq, const UniValue &vReq)
Definition: server.cpp:382
int RPCSerializationFlags()
Definition: server.cpp:540
void InterruptRPC()
Definition: server.cpp:297
void SetRPCWarmupStatus(const std::string &newStatus)
Set the RPC warmup status.
Definition: server.cpp:331
CRPCTable tableRPC
Definition: server.cpp:548
void RPCSetTimerInterface(RPCTimerInterface *iface)
Set the factory function for timers.
Definition: server.cpp:519
void RpcInterruptionPoint()
Throw JSONRPCError if RPC is not running.
Definition: server.cpp:326