Bitcoin ABC  0.26.3
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.chainman) {
41  throw JSONRPCError(RPC_INTERNAL_ERROR, "Node chainman not found");
42  }
43  return *node.chainman;
44 }
45 
46 ChainstateManager &EnsureAnyChainman(const std::any &context) {
47  return EnsureChainman(EnsureAnyNodeContext(context));
48 }
49 
51  if (!node.connman) {
52  throw JSONRPCError(
54  "Error: Peer-to-peer functionality missing or disabled");
55  }
56  return *node.connman;
57 }
58 
60  if (!node.peerman) {
61  throw JSONRPCError(
63  "Error: Peer-to-peer functionality missing or disabled");
64  }
65  return *node.peerman;
66 }
Definition: net.h:907
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:305
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:1062
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
ChainstateManager & EnsureAnyChainman(const std::any &context)
Definition: server_util.cpp:46
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:50
PeerManager & EnsurePeerman(const NodeContext &node)
Definition: server_util.cpp:59
CTxMemPool & EnsureMemPool(const NodeContext &node)
Definition: server_util.cpp:27
ChainstateManager & EnsureChainman(const NodeContext &node)
Definition: server_util.cpp:39
NodeContext struct containing references to chain state and connection state.
Definition: context.h:38