LCOV - code coverage report
Current view: top level - src/rpc - server_util.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 86.2 % 65 56
Test Date: 2025-02-22 05:08:25 Functions: 100.0 % 17 17
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 <chain.h>
       8                 :             : #include <common/args.h>
       9                 :             : #include <net_processing.h>
      10                 :             : #include <node/context.h>
      11                 :             : #include <node/miner.h>
      12                 :             : #include <policy/fees.h>
      13                 :             : #include <pow.h>
      14                 :             : #include <rpc/protocol.h>
      15                 :             : #include <rpc/request.h>
      16                 :             : #include <txmempool.h>
      17                 :             : #include <util/any.h>
      18                 :             : #include <validation.h>
      19                 :             : 
      20                 :             : #include <any>
      21                 :             : 
      22                 :             : using node::NodeContext;
      23                 :             : using node::UpdateTime;
      24                 :             : 
      25                 :      193431 : NodeContext& EnsureAnyNodeContext(const std::any& context)
      26                 :             : {
      27                 :      193431 :     auto node_context = util::AnyPtr<NodeContext>(context);
      28         [ -  + ]:      193431 :     if (!node_context) {
      29   [ #  #  #  # ]:           0 :         throw JSONRPCError(RPC_INTERNAL_ERROR, "Node context not found");
      30                 :             :     }
      31                 :      193431 :     return *node_context;
      32                 :             : }
      33                 :             : 
      34                 :       13455 : CTxMemPool& EnsureMemPool(const NodeContext& node)
      35                 :             : {
      36         [ -  + ]:       13455 :     if (!node.mempool) {
      37   [ #  #  #  # ]:           0 :         throw JSONRPCError(RPC_CLIENT_MEMPOOL_DISABLED, "Mempool disabled or instance not found");
      38                 :             :     }
      39                 :       13455 :     return *node.mempool;
      40                 :             : }
      41                 :             : 
      42                 :        9222 : CTxMemPool& EnsureAnyMemPool(const std::any& context)
      43                 :             : {
      44                 :        9222 :     return EnsureMemPool(EnsureAnyNodeContext(context));
      45                 :             : }
      46                 :             : 
      47                 :             : 
      48                 :         106 : BanMan& EnsureBanman(const NodeContext& node)
      49                 :             : {
      50         [ -  + ]:         106 :     if (!node.banman) {
      51   [ #  #  #  # ]:           0 :         throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
      52                 :             :     }
      53                 :         106 :     return *node.banman;
      54                 :             : }
      55                 :             : 
      56                 :          58 : BanMan& EnsureAnyBanman(const std::any& context)
      57                 :             : {
      58                 :          58 :     return EnsureBanman(EnsureAnyNodeContext(context));
      59                 :             : }
      60                 :             : 
      61                 :          56 : ArgsManager& EnsureArgsman(const NodeContext& node)
      62                 :             : {
      63         [ -  + ]:          56 :     if (!node.args) {
      64   [ #  #  #  # ]:           0 :         throw JSONRPCError(RPC_INTERNAL_ERROR, "Node args not found");
      65                 :             :     }
      66                 :          56 :     return *node.args;
      67                 :             : }
      68                 :             : 
      69                 :          16 : ArgsManager& EnsureAnyArgsman(const std::any& context)
      70                 :             : {
      71                 :          16 :     return EnsureArgsman(EnsureAnyNodeContext(context));
      72                 :             : }
      73                 :             : 
      74                 :       96279 : ChainstateManager& EnsureChainman(const NodeContext& node)
      75                 :             : {
      76         [ -  + ]:       96279 :     if (!node.chainman) {
      77   [ #  #  #  # ]:           0 :         throw JSONRPCError(RPC_INTERNAL_ERROR, "Node chainman not found");
      78                 :             :     }
      79                 :       96279 :     return *node.chainman;
      80                 :             : }
      81                 :             : 
      82                 :       85138 : ChainstateManager& EnsureAnyChainman(const std::any& context)
      83                 :             : {
      84                 :       85138 :     return EnsureChainman(EnsureAnyNodeContext(context));
      85                 :             : }
      86                 :             : 
      87                 :         325 : CBlockPolicyEstimator& EnsureFeeEstimator(const NodeContext& node)
      88                 :             : {
      89         [ +  + ]:         325 :     if (!node.fee_estimator) {
      90   [ +  -  +  - ]:           2 :         throw JSONRPCError(RPC_INTERNAL_ERROR, "Fee estimation disabled");
      91                 :             :     }
      92                 :         324 :     return *node.fee_estimator;
      93                 :             : }
      94                 :             : 
      95                 :         325 : CBlockPolicyEstimator& EnsureAnyFeeEstimator(const std::any& context)
      96                 :             : {
      97                 :         325 :     return EnsureFeeEstimator(EnsureAnyNodeContext(context));
      98                 :             : }
      99                 :             : 
     100                 :        6858 : CConnman& EnsureConnman(const NodeContext& node)
     101                 :             : {
     102         [ -  + ]:        6858 :     if (!node.connman) {
     103   [ #  #  #  # ]:           0 :         throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
     104                 :             :     }
     105                 :        6858 :     return *node.connman;
     106                 :             : }
     107                 :             : 
     108                 :        5110 : interfaces::Mining& EnsureMining(const NodeContext& node)
     109                 :             : {
     110         [ -  + ]:        5110 :     if (!node.mining) {
     111   [ #  #  #  # ]:           0 :         throw JSONRPCError(RPC_INTERNAL_ERROR, "Node miner not found");
     112                 :             :     }
     113                 :        5110 :     return *node.mining;
     114                 :             : }
     115                 :             : 
     116                 :        6183 : PeerManager& EnsurePeerman(const NodeContext& node)
     117                 :             : {
     118         [ -  + ]:        6183 :     if (!node.peerman) {
     119   [ #  #  #  # ]:           0 :         throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
     120                 :             :     }
     121                 :        6183 :     return *node.peerman;
     122                 :             : }
     123                 :             : 
     124                 :       32193 : AddrMan& EnsureAddrman(const NodeContext& node)
     125                 :             : {
     126         [ -  + ]:       32193 :     if (!node.addrman) {
     127   [ #  #  #  # ]:           0 :         throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled");
     128                 :             :     }
     129                 :       32193 :     return *node.addrman;
     130                 :             : }
     131                 :             : 
     132                 :       32193 : AddrMan& EnsureAnyAddrman(const std::any& context)
     133                 :             : {
     134                 :       32193 :     return EnsureAddrman(EnsureAnyNodeContext(context));
     135                 :             : }
     136                 :             : 
     137                 :          10 : void NextEmptyBlockIndex(CBlockIndex& tip, const Consensus::Params& consensusParams, CBlockIndex& next_index)
     138                 :             : {
     139                 :          10 :     CBlockHeader next_header{};
     140                 :          10 :     next_header.hashPrevBlock  = tip.GetBlockHash();
     141                 :          10 :     UpdateTime(&next_header, consensusParams, &tip);
     142                 :          10 :     next_header.nBits = GetNextWorkRequired(&tip, &next_header, consensusParams);
     143                 :          10 :     next_header.nNonce = 0;
     144                 :             : 
     145                 :          10 :     next_index.pprev = &tip;
     146                 :          10 :     next_index.nTime = next_header.nTime;
     147                 :          10 :     next_index.nBits = next_header.nBits;
     148                 :          10 :     next_index.nNonce = next_header.nNonce;
     149                 :          10 :     next_index.nHeight = tip.nHeight + 1;
     150                 :          10 : }
        

Generated by: LCOV version 2.0-1