Bitcoin Core 31.99.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#include <string_view>
17
18#include <univalue.h>
19
20class CRPCCommand;
21
23bool IsRPCRunning();
24
27
32void SetRPCWarmupStatus(const std::string& newStatus);
34/* Mark warmup as done. RPC calls will be processed from now on. */
36
37/* returns the current warmup state. */
38bool RPCIsInWarmup(std::string *outStatus);
39
41
43{
44public:
48 using Actor = std::function<bool(const JSONRPCRequest& request, UniValue& result, bool last_handler)>;
49
51 CRPCCommand(std::string category, std::string name, Actor actor, std::vector<std::pair<std::string, bool>> args, intptr_t unique_id)
52 : category(std::move(category)), name(std::move(name)), actor(std::move(actor)), argNames(std::move(args)),
54 {
55 }
56
61 fn().m_name,
62 [fn](const JSONRPCRequest& request, UniValue& result, bool) { result = fn().HandleRequest(request); return true; },
63 fn().GetArgNames(),
64 intptr_t(fn))
65 {
66 this->metadata_fn = fn;
67 }
68
69 std::string category;
70 std::string name;
81 std::vector<std::pair<std::string, bool>> argNames;
82 intptr_t unique_id;
84};
85
90{
91private:
92 std::map<std::string, std::vector<const CRPCCommand*>> mapCommands;
93public:
94 CRPCTable();
95 std::string help(std::string_view name, const JSONRPCRequest& helpreq) const;
96
103 UniValue execute(const JSONRPCRequest &request) const;
104
109 std::vector<std::string> listCommands() const;
111 UniValue buildOpenRPCDoc(bool include_hidden = false) const;
112
116 UniValue dumpArgMap(const JSONRPCRequest& request) const;
117
130 void appendCommand(const std::string& name, const CRPCCommand* pcmd);
131 bool removeCommand(const std::string& name, const CRPCCommand* pcmd);
132};
133
134bool IsDeprecatedRPCEnabled(const std::string& method);
135
136extern CRPCTable tableRPC;
137
138void StartRPC();
139void InterruptRPC();
140void StopRPC();
141UniValue JSONRPCExec(const JSONRPCRequest& jreq, bool catch_errors);
142
143#endif // BITCOIN_RPC_SERVER_H
ArgsManager & args
Definition: bitcoind.cpp:280
std::string category
Definition: server.h:69
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:51
intptr_t unique_id
Definition: server.h:82
std::vector< std::pair< std::string, bool > > argNames
List of method arguments and whether they are named-only.
Definition: server.h:81
std::string name
Definition: server.h:70
RpcMethodFnType metadata_fn
Definition: server.h:83
std::function< bool(const JSONRPCRequest &request, UniValue &result, bool last_handler)> Actor
RPC method handler reading request and assigning result.
Definition: server.h:48
Actor actor
Definition: server.h:71
CRPCCommand(std::string category, RpcMethodFnType fn)
Simplified constructor taking plain RpcMethodFnType function pointer.
Definition: server.h:58
RPC command dispatcher.
Definition: server.h:90
CRPCTable()
Definition: server.cpp:603
std::map< std::string, std::vector< const CRPCCommand * > > mapCommands
Definition: server.h:92
bool removeCommand(const std::string &name, const CRPCCommand *pcmd)
Definition: server.cpp:617
std::string help(std::string_view name, const JSONRPCRequest &helpreq) const
Definition: server.cpp:72
std::vector< std::string > listCommands() const
Returns a list of registered commands.
Definition: server.cpp:879
UniValue buildOpenRPCDoc(bool include_hidden=false) const
Return a complete OpenRPC 1.4.1 document for registered commands.
Definition: server.cpp:887
UniValue execute(const JSONRPCRequest &request) const
Execute a method.
Definition: server.cpp:842
void appendCommand(const std::string &name, const CRPCCommand *pcmd)
Appends a CRPCCommand to the dispatch table.
Definition: server.cpp:610
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:969
const char * name
Definition: rest.cpp:50
bool IsDeprecatedRPCEnabled(const std::string &method)
Definition: server.cpp:699
void SetRPCWarmupFinished()
Definition: server.cpp:684
void StartRPC()
Definition: server.cpp:633
bool RPCIsInWarmup(std::string *outStatus)
Definition: server.cpp:691
void StopRPC()
Definition: server.cpp:650
bool IsRPCRunning()
Query whether RPC is running.
Definition: server.cpp:662
void SetRPCWarmupStarting()
Definition: server.cpp:678
void InterruptRPC()
Definition: server.cpp:639
UniValue JSONRPCExec(const JSONRPCRequest &jreq, bool catch_errors)
Definition: server.cpp:706
RPCMethod(* RpcMethodFnType)()
Definition: server.h:40
void SetRPCWarmupStatus(const std::string &newStatus)
Set the RPC warmup status.
Definition: server.cpp:672
CRPCTable tableRPC
Definition: server.cpp:986
void RpcInterruptionPoint()
Throw JSONRPCError if RPC is not running.
Definition: server.cpp:667