Bitcoin ABC  0.28.12
P2P Digital Currency
server_util.cpp
Go to the documentation of this file.
1 // Copyright (c) 2021 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <rpc/server_util.h>
6 
7 #include <net_processing.h>
8 #include <node/context.h>
9 #include <rpc/protocol.h>
10 #include <rpc/request.h>
11 #include <txmempool.h>
12 #include <util/system.h>
13 #include <validation.h>
14 
15 #include <any>
16 
17 using node::NodeContext;
18 
19 NodeContext &EnsureAnyNodeContext(const std::any &context) {
20  auto node_context = util::AnyPtr<NodeContext>(context);
21  if (!node_context) {
22  throw JSONRPCError(RPC_INTERNAL_ERROR, "Node context not found");
23  }
24  return *node_context;
25 }
26 
28  if (!node.mempool) {
30  "Mempool disabled or instance not found");
31  }
32  return *node.mempool;
33 }
34 
35 CTxMemPool &EnsureAnyMemPool(const std::any &context) {
36  return EnsureMemPool(EnsureAnyNodeContext(context));
37 }
38 
40  if (!node.args) {
41  throw JSONRPCError(RPC_INTERNAL_ERROR, "Node args not found");
42  }
43  return *node.args;
44 }
45 
46 ArgsManager &EnsureAnyArgsman(const std::any &context) {
47  return EnsureArgsman(EnsureAnyNodeContext(context));
48 }
49 
51  if (!node.chainman) {
52  throw JSONRPCError(RPC_INTERNAL_ERROR, "Node chainman not found");
53  }
54  return *node.chainman;
55 }
56 
57 ChainstateManager &EnsureAnyChainman(const std::any &context) {
58  return EnsureChainman(EnsureAnyNodeContext(context));
59 }
60 
62  if (!node.connman) {
63  throw JSONRPCError(
65  "Error: Peer-to-peer functionality missing or disabled");
66  }
67  return *node.connman;
68 }
69 
71  if (!node.peerman) {
72  throw JSONRPCError(
74  "Error: Peer-to-peer functionality missing or disabled");
75  }
76  return *node.peerman;
77 }
Definition: net.h:845
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:209
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:1157
Definition: init.h:28
UniValue JSONRPCError(int code, const std::string &message)
Definition: request.cpp:57
@ RPC_CLIENT_MEMPOOL_DISABLED
Chain errors.
Definition: protocol.h:86
@ RPC_INTERNAL_ERROR
Definition: protocol.h:33
@ RPC_CLIENT_P2P_DISABLED
No valid connection manager instance found.
Definition: protocol.h:81
ArgsManager & EnsureArgsman(const NodeContext &node)
Definition: server_util.cpp:39
ArgsManager & EnsureAnyArgsman(const std::any &context)
Definition: server_util.cpp:46
ChainstateManager & EnsureAnyChainman(const std::any &context)
Definition: server_util.cpp:57
CTxMemPool & EnsureAnyMemPool(const std::any &context)
Definition: server_util.cpp:35
NodeContext & EnsureAnyNodeContext(const std::any &context)
Definition: server_util.cpp:19
CConnman & EnsureConnman(const NodeContext &node)
Definition: server_util.cpp:61
PeerManager & EnsurePeerman(const NodeContext &node)
Definition: server_util.cpp:70
CTxMemPool & EnsureMemPool(const NodeContext &node)
Definition: server_util.cpp:27
ChainstateManager & EnsureChainman(const NodeContext &node)
Definition: server_util.cpp:50
NodeContext struct containing references to chain state and connection state.
Definition: context.h:38