LCOV - code coverage report
Current view: top level - src/rpc - server_util.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 83.0 % 53 44
Test Date: 2024-07-04 05:05:02 Functions: 100.0 % 16 16
Branches: 21.7 % 60 13

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2021-2022 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 <common/args.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/any.h>
      15                 :             : #include <validation.h>
      16                 :             : 
      17                 :             : #include <any>
      18                 :             : 
      19                 :             : using node::NodeContext;
      20                 :             : 
      21                 :      172583 : NodeContext& EnsureAnyNodeContext(const std::any& context)
      22                 :             : {
      23                 :      172583 :     auto node_context = util::AnyPtr<NodeContext>(context);
      24         [ -  + ]:      172583 :     if (!node_context) {
      25   [ #  #  #  # ]:           0 :         throw JSONRPCError(RPC_INTERNAL_ERROR, "Node context not found");
      26                 :             :     }
      27                 :      172583 :     return *node_context;
      28                 :             : }
      29                 :             : 
      30                 :       13650 : CTxMemPool& EnsureMemPool(const NodeContext& node)
      31                 :             : {
      32         [ -  + ]:       13650 :     if (!node.mempool) {
      33   [ #  #  #  # ]:           0 :         throw JSONRPCError(RPC_CLIENT_MEMPOOL_DISABLED, "Mempool disabled or instance not found");
      34                 :             :     }
      35                 :       13650 :     return *node.mempool;
      36                 :             : }
      37                 :             : 
      38                 :       11957 : CTxMemPool& EnsureAnyMemPool(const std::any& context)
      39                 :             : {
      40                 :       11957 :     return EnsureMemPool(EnsureAnyNodeContext(context));
      41                 :             : }
      42                 :             : 
      43                 :             : 
      44                 :         106 : BanMan& EnsureBanman(const NodeContext& node)
      45                 :             : {
      46         [ -  + ]:         106 :     if (!node.banman) {
      47   [ #  #  #  # ]:           0 :         throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
      48                 :             :     }
      49                 :         106 :     return *node.banman;
      50                 :             : }
      51                 :             : 
      52                 :          58 : BanMan& EnsureAnyBanman(const std::any& context)
      53                 :             : {
      54                 :          58 :     return EnsureBanman(EnsureAnyNodeContext(context));
      55                 :             : }
      56                 :             : 
      57                 :          42 : ArgsManager& EnsureArgsman(const NodeContext& node)
      58                 :             : {
      59         [ -  + ]:          42 :     if (!node.args) {
      60   [ #  #  #  # ]:           0 :         throw JSONRPCError(RPC_INTERNAL_ERROR, "Node args not found");
      61                 :             :     }
      62                 :          42 :     return *node.args;
      63                 :             : }
      64                 :             : 
      65                 :           9 : ArgsManager& EnsureAnyArgsman(const std::any& context)
      66                 :             : {
      67                 :           9 :     return EnsureArgsman(EnsureAnyNodeContext(context));
      68                 :             : }
      69                 :             : 
      70                 :       64396 : ChainstateManager& EnsureChainman(const NodeContext& node)
      71                 :             : {
      72         [ -  + ]:       64396 :     if (!node.chainman) {
      73   [ #  #  #  # ]:           0 :         throw JSONRPCError(RPC_INTERNAL_ERROR, "Node chainman not found");
      74                 :             :     }
      75                 :       64396 :     return *node.chainman;
      76                 :             : }
      77                 :             : 
      78                 :       53608 : ChainstateManager& EnsureAnyChainman(const std::any& context)
      79                 :             : {
      80                 :       53608 :     return EnsureChainman(EnsureAnyNodeContext(context));
      81                 :             : }
      82                 :             : 
      83                 :         317 : CBlockPolicyEstimator& EnsureFeeEstimator(const NodeContext& node)
      84                 :             : {
      85         [ +  + ]:         317 :     if (!node.fee_estimator) {
      86   [ +  -  +  - ]:           2 :         throw JSONRPCError(RPC_INTERNAL_ERROR, "Fee estimation disabled");
      87                 :             :     }
      88                 :         316 :     return *node.fee_estimator;
      89                 :             : }
      90                 :             : 
      91                 :         317 : CBlockPolicyEstimator& EnsureAnyFeeEstimator(const std::any& context)
      92                 :             : {
      93                 :         317 :     return EnsureFeeEstimator(EnsureAnyNodeContext(context));
      94                 :             : }
      95                 :             : 
      96                 :        9574 : CConnman& EnsureConnman(const NodeContext& node)
      97                 :             : {
      98         [ -  + ]:        9574 :     if (!node.connman) {
      99   [ #  #  #  # ]:           0 :         throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
     100                 :             :     }
     101                 :        9574 :     return *node.connman;
     102                 :             : }
     103                 :             : 
     104                 :       11334 : interfaces::Mining& EnsureMining(const NodeContext& node)
     105                 :             : {
     106         [ -  + ]:       11334 :     if (!node.mining) {
     107   [ #  #  #  # ]:           0 :         throw JSONRPCError(RPC_INTERNAL_ERROR, "Node miner not found");
     108                 :             :     }
     109                 :       11334 :     return *node.mining;
     110                 :             : }
     111                 :             : 
     112                 :        8637 : PeerManager& EnsurePeerman(const NodeContext& node)
     113                 :             : {
     114         [ -  + ]:        8637 :     if (!node.peerman) {
     115   [ #  #  #  # ]:           0 :         throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
     116                 :             :     }
     117                 :        8637 :     return *node.peerman;
     118                 :             : }
     119                 :             : 
     120                 :       32183 : AddrMan& EnsureAddrman(const NodeContext& node)
     121                 :             : {
     122         [ -  + ]:       32183 :     if (!node.addrman) {
     123   [ #  #  #  # ]:           0 :         throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled");
     124                 :             :     }
     125                 :       32183 :     return *node.addrman;
     126                 :             : }
     127                 :             : 
     128                 :       32183 : AddrMan& EnsureAnyAddrman(const std::any& context)
     129                 :             : {
     130                 :       32183 :     return EnsureAddrman(EnsureAnyNodeContext(context));
     131                 :             : }
        

Generated by: LCOV version 2.0-1