forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathserver_util.cpp
120 lines (103 loc) · 3.3 KB
/
server_util.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright (c) 2021 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <rpc/server_util.h>
#include <net_processing.h>
#include <node/context.h>
#include <policy/fees.h>
#include <rpc/protocol.h>
#include <rpc/request.h>
#include <txmempool.h>
#include <util/system.h>
#include <validation.h>
#ifdef ENABLE_WALLET
#include <wallet/context.h>
#endif // ENABLE_WALLET
#include <any>
NodeContext& EnsureAnyNodeContext(const CoreContext& context)
{
auto* const node_context = GetContext<NodeContext>(context);
if (node_context) {
return *node_context;
}
#ifdef ENABLE_WALLET
// We're now going to try our luck with WalletContext on the off chance
// we're being called by a wallet RPC that's trying to access NodeContext
// See comment on WalletContext::node_context for more information.
// TODO: Find a solution that removes the need for this workaround
auto* const wallet_context = GetContext<WalletContext>(context);
if (wallet_context && wallet_context->node_context) {
return *wallet_context->node_context;
}
#endif // ENABLE_WALLET
throw JSONRPCError(RPC_INTERNAL_ERROR, "Node context not found");
}
CTxMemPool& EnsureMemPool(const NodeContext& node)
{
if (!node.mempool) {
throw JSONRPCError(RPC_CLIENT_MEMPOOL_DISABLED, "Mempool disabled or instance not found");
}
return *node.mempool;
}
CTxMemPool& EnsureAnyMemPool(const CoreContext& context)
{
return EnsureMemPool(EnsureAnyNodeContext(context));
}
ArgsManager& EnsureArgsman(const NodeContext& node)
{
if (!node.args) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Node args not found");
}
return *node.args;
}
ArgsManager& EnsureAnyArgsman(const CoreContext& context)
{
return EnsureArgsman(EnsureAnyNodeContext(context));
}
ChainstateManager& EnsureChainman(const NodeContext& node)
{
if (!node.chainman) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Node chainman not found");
}
return *node.chainman;
}
ChainstateManager& EnsureAnyChainman(const CoreContext& context)
{
return EnsureChainman(EnsureAnyNodeContext(context));
}
CBlockPolicyEstimator& EnsureFeeEstimator(const NodeContext& node)
{
if (!node.fee_estimator) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Fee estimation disabled");
}
return *node.fee_estimator;
}
CBlockPolicyEstimator& EnsureAnyFeeEstimator(const CoreContext& context)
{
return EnsureFeeEstimator(EnsureAnyNodeContext(context));
}
LLMQContext& EnsureLLMQContext(const NodeContext& node)
{
if (!node.llmq_ctx) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Node LLMQ context not found");
}
return *node.llmq_ctx;
}
LLMQContext& EnsureAnyLLMQContext(const CoreContext& context)
{
return EnsureLLMQContext(EnsureAnyNodeContext(context));
}
CConnman& EnsureConnman(const NodeContext& node)
{
if (!node.connman) {
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
}
return *node.connman;
}
PeerManager& EnsurePeerman(const NodeContext& node)
{
if (!node.peerman) {
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
}
return *node.peerman;
}