|
| 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 <policy/fees.h> |
| 10 | +#include <rpc/protocol.h> |
| 11 | +#include <rpc/request.h> |
| 12 | +#include <txmempool.h> |
| 13 | +#include <util/system.h> |
| 14 | +#include <validation.h> |
| 15 | + |
| 16 | +#include <any> |
| 17 | + |
| 18 | +NodeContext& EnsureAnyNodeContext(const std::any& context) |
| 19 | +{ |
| 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 | + |
| 27 | +CTxMemPool& EnsureMemPool(const NodeContext& node) |
| 28 | +{ |
| 29 | + if (!node.mempool) { |
| 30 | + throw JSONRPCError(RPC_CLIENT_MEMPOOL_DISABLED, "Mempool disabled or instance not found"); |
| 31 | + } |
| 32 | + return *node.mempool; |
| 33 | +} |
| 34 | + |
| 35 | +CTxMemPool& EnsureAnyMemPool(const std::any& context) |
| 36 | +{ |
| 37 | + return EnsureMemPool(EnsureAnyNodeContext(context)); |
| 38 | +} |
| 39 | + |
| 40 | +ChainstateManager& EnsureChainman(const NodeContext& node) |
| 41 | +{ |
| 42 | + if (!node.chainman) { |
| 43 | + throw JSONRPCError(RPC_INTERNAL_ERROR, "Node chainman not found"); |
| 44 | + } |
| 45 | + return *node.chainman; |
| 46 | +} |
| 47 | + |
| 48 | +ChainstateManager& EnsureAnyChainman(const std::any& context) |
| 49 | +{ |
| 50 | + return EnsureChainman(EnsureAnyNodeContext(context)); |
| 51 | +} |
| 52 | + |
| 53 | +CBlockPolicyEstimator& EnsureFeeEstimator(const NodeContext& node) |
| 54 | +{ |
| 55 | + if (!node.fee_estimator) { |
| 56 | + throw JSONRPCError(RPC_INTERNAL_ERROR, "Fee estimation disabled"); |
| 57 | + } |
| 58 | + return *node.fee_estimator; |
| 59 | +} |
| 60 | + |
| 61 | +CBlockPolicyEstimator& EnsureAnyFeeEstimator(const std::any& context) |
| 62 | +{ |
| 63 | + return EnsureFeeEstimator(EnsureAnyNodeContext(context)); |
| 64 | +} |
| 65 | + |
| 66 | +CConnman& EnsureConnman(const NodeContext& node) |
| 67 | +{ |
| 68 | + if (!node.connman) { |
| 69 | + throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled"); |
| 70 | + } |
| 71 | + return *node.connman; |
| 72 | +} |
| 73 | + |
| 74 | +PeerManager& EnsurePeerman(const NodeContext& node) |
| 75 | +{ |
| 76 | + if (!node.peerman) { |
| 77 | + throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled"); |
| 78 | + } |
| 79 | + return *node.peerman; |
| 80 | +} |
0 commit comments