LCOV - code coverage report
Current view: top level - src/rpc - server_util.cpp (source / functions) Hit Total Coverage
Test: test_dash_coverage.info Lines: 16 61 26.2 %
Date: 2026-06-25 07:23:51 Functions: 5 15 33.3 %

          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          34 : NodeContext& EnsureAnyNodeContext(const CoreContext& context)
      29             : {
      30          34 :     auto* const node_context = GetContext<NodeContext>(context);
      31          34 :     if (node_context) {
      32          34 :         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           0 :     auto* const wallet_context = GetContext<WalletContext>(context);
      40           0 :     if (wallet_context && wallet_context->node_context) {
      41           0 :         return *wallet_context->node_context;
      42             :     }
      43             : #endif // ENABLE_WALLET
      44           0 :     throw JSONRPCError(RPC_INTERNAL_ERROR, "Node context not found");
      45          34 : }
      46             : 
      47           0 : CTxMemPool& EnsureMemPool(const NodeContext& node)
      48             : {
      49           0 :     if (!node.mempool) {
      50           0 :         throw JSONRPCError(RPC_CLIENT_MEMPOOL_DISABLED, "Mempool disabled or instance not found");
      51             :     }
      52           0 :     return *node.mempool;
      53           0 : }
      54             : 
      55           0 : CTxMemPool& EnsureAnyMemPool(const CoreContext& context)
      56             : {
      57           0 :     return EnsureMemPool(EnsureAnyNodeContext(context));
      58             : }
      59             : 
      60             : 
      61          26 : BanMan& EnsureBanman(const NodeContext& node)
      62             : {
      63          26 :     if (!node.banman) {
      64           0 :         throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
      65             :     }
      66          26 :     return *node.banman;
      67           0 : }
      68             : 
      69          14 : BanMan& EnsureAnyBanman(const CoreContext& context)
      70             : {
      71          14 :     return EnsureBanman(EnsureAnyNodeContext(context));
      72             : }
      73             : 
      74           0 : ArgsManager& EnsureArgsman(const NodeContext& node)
      75             : {
      76           0 :     if (!node.args) {
      77           0 :         throw JSONRPCError(RPC_INTERNAL_ERROR, "Node args not found");
      78             :     }
      79           0 :     return *node.args;
      80           0 : }
      81             : 
      82           0 : ArgsManager& EnsureAnyArgsman(const CoreContext& context)
      83             : {
      84           0 :     return EnsureArgsman(EnsureAnyNodeContext(context));
      85             : }
      86             : 
      87           1 : ChainstateManager& EnsureChainman(const NodeContext& node)
      88             : {
      89           1 :     if (!node.chainman) {
      90           0 :         throw JSONRPCError(RPC_INTERNAL_ERROR, "Node chainman not found");
      91             :     }
      92           1 :     return *node.chainman;
      93           0 : }
      94             : 
      95           0 : ChainstateManager& EnsureAnyChainman(const CoreContext& context)
      96             : {
      97           0 :     return EnsureChainman(EnsureAnyNodeContext(context));
      98             : }
      99             : 
     100           0 : CBlockPolicyEstimator& EnsureFeeEstimator(const NodeContext& node)
     101             : {
     102           0 :     if (!node.fee_estimator) {
     103           0 :         throw JSONRPCError(RPC_INTERNAL_ERROR, "Fee estimation disabled");
     104             :     }
     105           0 :     return *node.fee_estimator;
     106           0 : }
     107             : 
     108           0 : CBlockPolicyEstimator& EnsureAnyFeeEstimator(const CoreContext& context)
     109             : {
     110           0 :     return EnsureFeeEstimator(EnsureAnyNodeContext(context));
     111             : }
     112             : 
     113           0 : LLMQContext& EnsureLLMQContext(const NodeContext& node)
     114             : {
     115           0 :     if (!node.llmq_ctx) {
     116           0 :         throw JSONRPCError(RPC_INTERNAL_ERROR, "Node LLMQ context not found");
     117             :     }
     118           0 :     return *node.llmq_ctx;
     119           0 : }
     120             : 
     121           0 : LLMQContext& EnsureAnyLLMQContext(const CoreContext& context)
     122             : {
     123           0 :     return EnsureLLMQContext(EnsureAnyNodeContext(context));
     124             : }
     125             : 
     126           2 : CConnman& EnsureConnman(const NodeContext& node)
     127             : {
     128           2 :     if (!node.connman) {
     129           0 :         throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
     130             :     }
     131           2 :     return *node.connman;
     132           0 : }
     133             : 
     134           0 : PeerManager& EnsurePeerman(const NodeContext& node)
     135             : {
     136           0 :     if (!node.peerman) {
     137           0 :         throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
     138             :     }
     139           0 :     return *node.peerman;
     140           0 : }

Generated by: LCOV version 1.16