Coverage Report

Created: 2026-09-14 20:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/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 <node/context.h>
9
#include <node/miner.h>
10
#include <pow.h>
11
#include <primitives/block.h>
12
#include <rpc/protocol.h>
13
#include <rpc/request.h>
14
#include <uint256.h>
15
#include <util/any.h>
16
17
#include <any>
18
#include <memory>
19
#include <string>
20
21
namespace Consensus {
22
struct Params;
23
} // namespace Consensus
24
25
using node::NodeContext;
26
using node::UpdateTime;
27
28
NodeContext& EnsureAnyNodeContext(const std::any& context)
29
188k
{
30
188k
    auto node_context = util::AnyPtr<NodeContext>(context);
31
188k
    if (!node_context) {
32
0
        throw JSONRPCError(RPC_INTERNAL_ERROR, "Node context not found");
33
0
    }
34
188k
    return *node_context;
35
188k
}
36
37
CTxMemPool& EnsureMemPool(const NodeContext& node)
38
25.3k
{
39
25.3k
    if (!node.mempool) {
40
0
        throw JSONRPCError(RPC_CLIENT_MEMPOOL_DISABLED, "Mempool disabled or instance not found");
41
0
    }
42
25.3k
    return *node.mempool;
43
25.3k
}
44
45
CTxMemPool& EnsureAnyMemPool(const std::any& context)
46
23.0k
{
47
23.0k
    return EnsureMemPool(EnsureAnyNodeContext(context));
48
23.0k
}
49
50
51
BanMan& EnsureBanman(const NodeContext& node)
52
106
{
53
106
    if (!node.banman) {
54
0
        throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
55
0
    }
56
106
    return *node.banman;
57
106
}
58
59
BanMan& EnsureAnyBanman(const std::any& context)
60
58
{
61
58
    return EnsureBanman(EnsureAnyNodeContext(context));
62
58
}
63
64
ArgsManager& EnsureArgsman(const NodeContext& node)
65
59
{
66
59
    if (!node.args) {
67
0
        throw JSONRPCError(RPC_INTERNAL_ERROR, "Node args not found");
68
0
    }
69
59
    return *node.args;
70
59
}
71
72
ArgsManager& EnsureAnyArgsman(const std::any& context)
73
18
{
74
18
    return EnsureArgsman(EnsureAnyNodeContext(context));
75
18
}
76
77
ChainstateManager& EnsureChainman(const NodeContext& node)
78
64.0k
{
79
64.0k
    if (!node.chainman) {
80
0
        throw JSONRPCError(RPC_INTERNAL_ERROR, "Node chainman not found");
81
0
    }
82
64.0k
    return *node.chainman;
83
64.0k
}
84
85
ChainstateManager& EnsureAnyChainman(const std::any& context)
86
53.6k
{
87
53.6k
    return EnsureChainman(EnsureAnyNodeContext(context));
88
53.6k
}
89
90
FeeRateEstimatorManager& EnsureFeeEstimatorMan(const NodeContext& node)
91
321
{
92
321
    if (!node.fee_estimator_man) {
93
1
        throw JSONRPCError(RPC_INTERNAL_ERROR, "Fee estimation disabled");
94
1
    }
95
320
    return *node.fee_estimator_man;
96
321
}
97
98
FeeRateEstimatorManager& EnsureAnyFeeEstimatorMan(const std::any& context)
99
321
{
100
321
    return EnsureFeeEstimatorMan(EnsureAnyNodeContext(context));
101
321
}
102
103
CConnman& EnsureConnman(const NodeContext& node)
104
8.81k
{
105
8.81k
    if (!node.connman) {
106
0
        throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
107
0
    }
108
8.81k
    return *node.connman;
109
8.81k
}
110
111
interfaces::Mining& EnsureMining(const NodeContext& node)
112
3.73k
{
113
3.73k
    if (!node.mining) {
114
0
        throw JSONRPCError(RPC_INTERNAL_ERROR, "Node miner not found");
115
0
    }
116
3.73k
    return *node.mining;
117
3.73k
}
118
119
PeerManager& EnsurePeerman(const NodeContext& node)
120
8.20k
{
121
8.20k
    if (!node.peerman) {
122
0
        throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
123
0
    }
124
8.20k
    return *node.peerman;
125
8.20k
}
126
127
AddrMan& EnsureAddrman(const NodeContext& node)
128
32.3k
{
129
32.3k
    if (!node.addrman) {
130
0
        throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled");
131
0
    }
132
32.3k
    return *node.addrman;
133
32.3k
}
134
135
AddrMan& EnsureAnyAddrman(const std::any& context)
136
32.3k
{
137
32.3k
    return EnsureAddrman(EnsureAnyNodeContext(context));
138
32.3k
}
139
140
void NextEmptyBlockIndex(CBlockIndex& tip, const Consensus::Params& consensusParams, CBlockIndex& next_index)
141
25
{
142
25
    CBlockHeader next_header{};
143
25
    next_header.hashPrevBlock  = tip.GetBlockHash();
144
25
    UpdateTime(&next_header, consensusParams, &tip);
145
25
    next_header.nBits = GetNextWorkRequired(&tip, &next_header, consensusParams);
146
25
    next_header.nNonce = 0;
147
148
25
    next_index.pprev = &tip;
149
25
    next_index.nTime = next_header.nTime;
150
25
    next_index.nBits = next_header.nBits;
151
25
    next_index.nNonce = next_header.nNonce;
152
25
    next_index.nHeight = tip.nHeight + 1;
153
25
}