Line data Source code
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 <banman.h> 8 : #include <net_processing.h> 9 : #include <node/context.h> 10 : #include <policy/fees.h> 11 : #include <rpc/protocol.h> 12 : #include <rpc/request.h> 13 : #include <txmempool.h> 14 : #include <util/system.h> 15 : #include <validation.h> 16 : 17 : #ifdef ENABLE_WALLET 18 : #include <wallet/context.h> 19 : #endif // ENABLE_WALLET 20 : 21 : #include <any> 22 : 23 : using node::NodeContext; 24 : #ifdef ENABLE_WALLET 25 : using wallet::WalletContext; 26 : #endif // ENABLE_WALLET 27 : 28 339190 : NodeContext& EnsureAnyNodeContext(const CoreContext& context) 29 : { 30 339190 : auto* const node_context = GetContext<NodeContext>(context); 31 339190 : if (node_context) { 32 335358 : return *node_context; 33 : } 34 : #ifdef ENABLE_WALLET 35 : // We're now going to try our luck with WalletContext on the off chance 36 : // we're being called by a wallet RPC that's trying to access NodeContext 37 : // See comment on WalletContext::node_context for more information. 38 : // TODO: Find a solution that removes the need for this workaround 39 3832 : auto* const wallet_context = GetContext<WalletContext>(context); 40 3832 : if (wallet_context && wallet_context->node_context) { 41 3832 : return *wallet_context->node_context; 42 : } 43 : #endif // ENABLE_WALLET 44 0 : throw JSONRPCError(RPC_INTERNAL_ERROR, "Node context not found"); 45 339190 : } 46 : 47 61888 : CTxMemPool& EnsureMemPool(const NodeContext& node) 48 : { 49 61888 : if (!node.mempool) { 50 0 : throw JSONRPCError(RPC_CLIENT_MEMPOOL_DISABLED, "Mempool disabled or instance not found"); 51 : } 52 61888 : return *node.mempool; 53 0 : } 54 : 55 323 : CTxMemPool& EnsureAnyMemPool(const CoreContext& context) 56 : { 57 323 : return EnsureMemPool(EnsureAnyNodeContext(context)); 58 : } 59 : 60 : 61 198 : BanMan& EnsureBanman(const NodeContext& node) 62 : { 63 198 : if (!node.banman) { 64 0 : throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded"); 65 : } 66 198 : return *node.banman; 67 0 : } 68 : 69 118 : BanMan& EnsureAnyBanman(const CoreContext& context) 70 : { 71 118 : return EnsureBanman(EnsureAnyNodeContext(context)); 72 : } 73 : 74 15146 : ArgsManager& EnsureArgsman(const NodeContext& node) 75 : { 76 15146 : if (!node.args) { 77 0 : throw JSONRPCError(RPC_INTERNAL_ERROR, "Node args not found"); 78 : } 79 15146 : return *node.args; 80 0 : } 81 : 82 10 : ArgsManager& EnsureAnyArgsman(const CoreContext& context) 83 : { 84 10 : return EnsureArgsman(EnsureAnyNodeContext(context)); 85 : } 86 : 87 162653 : ChainstateManager& EnsureChainman(const NodeContext& node) 88 : { 89 162653 : if (!node.chainman) { 90 0 : throw JSONRPCError(RPC_INTERNAL_ERROR, "Node chainman not found"); 91 : } 92 162653 : return *node.chainman; 93 0 : } 94 : 95 73868 : ChainstateManager& EnsureAnyChainman(const CoreContext& context) 96 : { 97 73868 : return EnsureChainman(EnsureAnyNodeContext(context)); 98 : } 99 : 100 622 : CBlockPolicyEstimator& EnsureFeeEstimator(const NodeContext& node) 101 : { 102 622 : if (!node.fee_estimator) { 103 2 : throw JSONRPCError(RPC_INTERNAL_ERROR, "Fee estimation disabled"); 104 : } 105 620 : return *node.fee_estimator; 106 2 : } 107 : 108 622 : CBlockPolicyEstimator& EnsureAnyFeeEstimator(const CoreContext& context) 109 : { 110 622 : return EnsureFeeEstimator(EnsureAnyNodeContext(context)); 111 : } 112 : 113 73944 : LLMQContext& EnsureLLMQContext(const NodeContext& node) 114 : { 115 73944 : if (!node.llmq_ctx) { 116 0 : throw JSONRPCError(RPC_INTERNAL_ERROR, "Node LLMQ context not found"); 117 : } 118 73944 : return *node.llmq_ctx; 119 0 : } 120 : 121 0 : LLMQContext& EnsureAnyLLMQContext(const CoreContext& context) 122 : { 123 0 : return EnsureLLMQContext(EnsureAnyNodeContext(context)); 124 : } 125 : 126 61127 : CConnman& EnsureConnman(const NodeContext& node) 127 : { 128 61127 : if (!node.connman) { 129 0 : throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled"); 130 : } 131 61127 : return *node.connman; 132 0 : } 133 : 134 31721 : PeerManager& EnsurePeerman(const NodeContext& node) 135 : { 136 31721 : if (!node.peerman) { 137 0 : throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled"); 138 : } 139 31721 : return *node.peerman; 140 0 : }