Coverage Report

Created: 2026-04-29 19:21

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 <common/args.h>
9
#include <net_processing.h>
10
#include <node/context.h>
11
#include <node/miner.h>
12
#include <policy/fees/block_policy_estimator.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
171k
{
27
171k
    auto node_context = util::AnyPtr<NodeContext>(context);
28
171k
    if (!node_context) {
29
0
        throw JSONRPCError(RPC_INTERNAL_ERROR, "Node context not found");
30
0
    }
31
171k
    return *node_context;
32
171k
}
33
34
CTxMemPool& EnsureMemPool(const NodeContext& node)
35
26.1k
{
36
26.1k
    if (!node.mempool) {
37
0
        throw JSONRPCError(RPC_CLIENT_MEMPOOL_DISABLED, "Mempool disabled or instance not found");
38
0
    }
39
26.1k
    return *node.mempool;
40
26.1k
}
41
42
CTxMemPool& EnsureAnyMemPool(const std::any& context)
43
21.8k
{
44
21.8k
    return EnsureMemPool(EnsureAnyNodeContext(context));
45
21.8k
}
46
47
48
BanMan& EnsureBanman(const NodeContext& node)
49
106
{
50
106
    if (!node.banman) {
51
0
        throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
52
0
    }
53
106
    return *node.banman;
54
106
}
55
56
BanMan& EnsureAnyBanman(const std::any& context)
57
58
{
58
58
    return EnsureBanman(EnsureAnyNodeContext(context));
59
58
}
60
61
ArgsManager& EnsureArgsman(const NodeContext& node)
62
59
{
63
59
    if (!node.args) {
64
0
        throw JSONRPCError(RPC_INTERNAL_ERROR, "Node args not found");
65
0
    }
66
59
    return *node.args;
67
59
}
68
69
ArgsManager& EnsureAnyArgsman(const std::any& context)
70
18
{
71
18
    return EnsureArgsman(EnsureAnyNodeContext(context));
72
18
}
73
74
ChainstateManager& EnsureChainman(const NodeContext& node)
75
62.9k
{
76
62.9k
    if (!node.chainman) {
77
0
        throw JSONRPCError(RPC_INTERNAL_ERROR, "Node chainman not found");
78
0
    }
79
62.9k
    return *node.chainman;
80
62.9k
}
81
82
ChainstateManager& EnsureAnyChainman(const std::any& context)
83
51.2k
{
84
51.2k
    return EnsureChainman(EnsureAnyNodeContext(context));
85
51.2k
}
86
87
CBlockPolicyEstimator& EnsureFeeEstimator(const NodeContext& node)
88
302
{
89
302
    if (!node.fee_estimator) {
90
1
        throw JSONRPCError(RPC_INTERNAL_ERROR, "Fee estimation disabled");
91
1
    }
92
301
    return *node.fee_estimator;
93
302
}
94
95
CBlockPolicyEstimator& EnsureAnyFeeEstimator(const std::any& context)
96
302
{
97
302
    return EnsureFeeEstimator(EnsureAnyNodeContext(context));
98
302
}
99
100
CConnman& EnsureConnman(const NodeContext& node)
101
7.77k
{
102
7.77k
    if (!node.connman) {
103
0
        throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
104
0
    }
105
7.77k
    return *node.connman;
106
7.77k
}
107
108
interfaces::Mining& EnsureMining(const NodeContext& node)
109
5.63k
{
110
5.63k
    if (!node.mining) {
111
0
        throw JSONRPCError(RPC_INTERNAL_ERROR, "Node miner not found");
112
0
    }
113
5.63k
    return *node.mining;
114
5.63k
}
115
116
PeerManager& EnsurePeerman(const NodeContext& node)
117
7.23k
{
118
7.23k
    if (!node.peerman) {
119
0
        throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
120
0
    }
121
7.23k
    return *node.peerman;
122
7.23k
}
123
124
AddrMan& EnsureAddrman(const NodeContext& node)
125
32.3k
{
126
32.3k
    if (!node.addrman) {
127
0
        throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled");
128
0
    }
129
32.3k
    return *node.addrman;
130
32.3k
}
131
132
AddrMan& EnsureAnyAddrman(const std::any& context)
133
32.3k
{
134
32.3k
    return EnsureAddrman(EnsureAnyNodeContext(context));
135
32.3k
}
136
137
void NextEmptyBlockIndex(CBlockIndex& tip, const Consensus::Params& consensusParams, CBlockIndex& next_index)
138
24
{
139
24
    CBlockHeader next_header{};
140
24
    next_header.hashPrevBlock  = tip.GetBlockHash();
141
24
    UpdateTime(&next_header, consensusParams, &tip);
142
24
    next_header.nBits = GetNextWorkRequired(&tip, &next_header, consensusParams);
143
24
    next_header.nNonce = 0;
144
145
24
    next_index.pprev = &tip;
146
24
    next_index.nTime = next_header.nTime;
147
24
    next_index.nBits = next_header.nBits;
148
24
    next_index.nNonce = next_header.nNonce;
149
24
    next_index.nHeight = tip.nHeight + 1;
150
24
}