Coverage Report

Created: 2026-09-01 13:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/bitcoin/src/rpc/server_util.cpp
Line
Count
Source
1
// Copyright (c) 2021-present 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/estimator_man.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
NodeContext& EnsureAnyNodeContext(const std::any& context)
26
1.20k
{
27
1.20k
    auto node_context = util::AnyPtr<NodeContext>(context);
28
1.20k
    if (!node_context) {
  Branch (28:9): [True: 0, False: 1.20k]
29
0
        throw JSONRPCError(RPC_INTERNAL_ERROR, "Node context not found");
30
0
    }
31
1.20k
    return *node_context;
32
1.20k
}
33
34
CTxMemPool& EnsureMemPool(const NodeContext& node)
35
197
{
36
197
    if (!node.mempool) {
  Branch (36:9): [True: 0, False: 197]
37
0
        throw JSONRPCError(RPC_CLIENT_MEMPOOL_DISABLED, "Mempool disabled or instance not found");
38
0
    }
39
197
    return *node.mempool;
40
197
}
41
42
CTxMemPool& EnsureAnyMemPool(const std::any& context)
43
16
{
44
16
    return EnsureMemPool(EnsureAnyNodeContext(context));
45
16
}
46
47
48
BanMan& EnsureBanman(const NodeContext& node)
49
3
{
50
3
    if (!node.banman) {
  Branch (50:9): [True: 0, False: 3]
51
0
        throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
52
0
    }
53
3
    return *node.banman;
54
3
}
55
56
BanMan& EnsureAnyBanman(const std::any& context)
57
3
{
58
3
    return EnsureBanman(EnsureAnyNodeContext(context));
59
3
}
60
61
ArgsManager& EnsureArgsman(const NodeContext& node)
62
0
{
63
0
    if (!node.args) {
  Branch (63:9): [True: 0, False: 0]
64
0
        throw JSONRPCError(RPC_INTERNAL_ERROR, "Node args not found");
65
0
    }
66
0
    return *node.args;
67
0
}
68
69
ArgsManager& EnsureAnyArgsman(const std::any& context)
70
0
{
71
0
    return EnsureArgsman(EnsureAnyNodeContext(context));
72
0
}
73
74
ChainstateManager& EnsureChainman(const NodeContext& node)
75
552
{
76
552
    if (!node.chainman) {
  Branch (76:9): [True: 0, False: 552]
77
0
        throw JSONRPCError(RPC_INTERNAL_ERROR, "Node chainman not found");
78
0
    }
79
552
    return *node.chainman;
80
552
}
81
82
ChainstateManager& EnsureAnyChainman(const std::any& context)
83
255
{
84
255
    return EnsureChainman(EnsureAnyNodeContext(context));
85
255
}
86
87
FeeRateEstimatorManager& EnsureFeeEstimatorMan(const NodeContext& node)
88
2
{
89
2
    if (!node.fee_estimator_man) {
  Branch (89:9): [True: 2, False: 0]
90
2
        throw JSONRPCError(RPC_INTERNAL_ERROR, "Fee estimation disabled");
91
2
    }
92
0
    return *node.fee_estimator_man;
93
2
}
94
95
FeeRateEstimatorManager& EnsureAnyFeeEstimatorMan(const std::any& context)
96
2
{
97
2
    return EnsureFeeEstimatorMan(EnsureAnyNodeContext(context));
98
2
}
99
100
CConnman& EnsureConnman(const NodeContext& node)
101
54
{
102
54
    if (!node.connman) {
  Branch (102:9): [True: 0, False: 54]
103
0
        throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
104
0
    }
105
54
    return *node.connman;
106
54
}
107
108
interfaces::Mining& EnsureMining(const NodeContext& node)
109
29
{
110
29
    if (!node.mining) {
  Branch (110:9): [True: 29, False: 0]
111
29
        throw JSONRPCError(RPC_INTERNAL_ERROR, "Node miner not found");
112
29
    }
113
0
    return *node.mining;
114
29
}
115
116
PeerManager& EnsurePeerman(const NodeContext& node)
117
15
{
118
15
    if (!node.peerman) {
  Branch (118:9): [True: 0, False: 15]
119
0
        throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
120
0
    }
121
15
    return *node.peerman;
122
15
}
123
124
AddrMan& EnsureAddrman(const NodeContext& node)
125
2
{
126
2
    if (!node.addrman) {
  Branch (126:9): [True: 0, False: 2]
127
0
        throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled");
128
0
    }
129
2
    return *node.addrman;
130
2
}
131
132
AddrMan& EnsureAnyAddrman(const std::any& context)
133
2
{
134
2
    return EnsureAddrman(EnsureAnyNodeContext(context));
135
2
}
136
137
void NextEmptyBlockIndex(CBlockIndex& tip, const Consensus::Params& consensusParams, CBlockIndex& next_index)
138
2
{
139
2
    CBlockHeader next_header{};
140
2
    next_header.hashPrevBlock  = tip.GetBlockHash();
141
2
    UpdateTime(&next_header, consensusParams, &tip);
142
2
    next_header.nBits = GetNextWorkRequired(&tip, &next_header, consensusParams);
143
2
    next_header.nNonce = 0;
144
145
2
    next_index.pprev = &tip;
146
2
    next_index.nTime = next_header.nTime;
147
2
    next_index.nBits = next_header.nBits;
148
2
    next_index.nNonce = next_header.nNonce;
149
2
    next_index.nHeight = tip.nHeight + 1;
150
2
}