Bitcoin Core 32.99.0
P2P Digital Currency
server_util.cpp
Go to the documentation of this file.
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
21namespace Consensus {
22struct Params;
23} // namespace Consensus
24
27
29{
30 auto node_context = util::AnyPtr<NodeContext>(context);
31 if (!node_context) {
32 throw JSONRPCError(RPC_INTERNAL_ERROR, "Node context not found");
33 }
34 return *node_context;
35}
36
38{
39 if (!node.mempool) {
40 throw JSONRPCError(RPC_CLIENT_MEMPOOL_DISABLED, "Mempool disabled or instance not found");
41 }
42 return *node.mempool;
43}
44
46{
48}
49
50
52{
53 if (!node.banman) {
54 throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
55 }
56 return *node.banman;
57}
58
60{
62}
63
65{
66 if (!node.args) {
67 throw JSONRPCError(RPC_INTERNAL_ERROR, "Node args not found");
68 }
69 return *node.args;
70}
71
73{
75}
76
78{
79 if (!node.chainman) {
80 throw JSONRPCError(RPC_INTERNAL_ERROR, "Node chainman not found");
81 }
82 return *node.chainman;
83}
84
86{
88}
89
91{
92 if (!node.fee_estimator_man) {
93 throw JSONRPCError(RPC_INTERNAL_ERROR, "Fee estimation disabled");
94 }
95 return *node.fee_estimator_man;
96}
97
99{
101}
102
104{
105 if (!node.connman) {
106 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
107 }
108 return *node.connman;
109}
110
112{
113 if (!node.block_template_manager) {
114 throw JSONRPCError(RPC_INTERNAL_ERROR, "Block template manager not found");
115 }
116 return *node.block_template_manager;
117}
118
120{
121 if (!node.peerman) {
122 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
123 }
124 return *node.peerman;
125}
126
128{
129 if (!node.addrman) {
130 throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled");
131 }
132 return *node.addrman;
133}
134
136{
138}
139
140void NextEmptyBlockIndex(CBlockIndex& tip, const Consensus::Params& consensusParams, CBlockIndex& next_index)
141{
142 CBlockHeader next_header{};
143 next_header.hashPrevBlock = tip.GetBlockHash();
144 UpdateTime(&next_header, consensusParams, &tip);
145 next_header.nBits = GetNextWorkRequired(&tip, &next_header, consensusParams);
146 next_header.nNonce = 0;
147
148 next_index.pprev = &tip;
149 next_index.nTime = next_header.nTime;
150 next_index.nBits = next_header.nBits;
151 next_index.nNonce = next_header.nNonce;
152 next_index.nHeight = tip.nHeight + 1;
153}
const CChainParams & Params()
Return the currently selected parameters.
Stochastic address manager.
Definition: addrman.h:110
Definition: banman.h:64
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:27
uint256 hashPrevBlock
Definition: block.h:31
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:94
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: chain.h:100
uint32_t nTime
Definition: chain.h:142
uint32_t nNonce
Definition: chain.h:144
uint256 GetBlockHash() const
Definition: chain.h:198
uint32_t nBits
Definition: chain.h:143
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:106
Definition: net.h:1085
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:187
Interface for managing multiple Chainstate objects, where each chainstate is associated with chainsta...
Definition: validation.h:950
Manages fee rate estimators.
Definition: estimator_man.h:31
Creates block templates, submits solved blocks, and provides tip-waiting helpers for mining code.
Transaction validation functions.
Definition: messages.h:21
int64_t UpdateTime(CBlockHeader *pblock, const Consensus::Params &consensusParams, const CBlockIndex *pindexPrev)
Definition: miner.cpp:70
unsigned int GetNextWorkRequired(const CBlockIndex *pindexLast, const CBlockHeader *pblock, const Consensus::Params &params)
Definition: pow.cpp:14
UniValue JSONRPCError(int code, const std::string &message)
Definition: request.cpp:75
@ RPC_CLIENT_MEMPOOL_DISABLED
Chain errors.
Definition: protocol.h:94
@ RPC_INTERNAL_ERROR
Definition: protocol.h:61
@ RPC_DATABASE_ERROR
Database error.
Definition: protocol.h:70
@ RPC_CLIENT_P2P_DISABLED
No valid connection manager instance found.
Definition: protocol.h:90
ChainstateManager & EnsureAnyChainman(const std::any &context)
Definition: server_util.cpp:85
BanMan & EnsureBanman(const NodeContext &node)
Definition: server_util.cpp:51
void NextEmptyBlockIndex(CBlockIndex &tip, const Consensus::Params &consensusParams, CBlockIndex &next_index)
Return an empty block index on top of the tip, with height, time and nBits set.
AddrMan & EnsureAnyAddrman(const std::any &context)
FeeRateEstimatorManager & EnsureFeeEstimatorMan(const NodeContext &node)
Definition: server_util.cpp:90
NodeContext & EnsureAnyNodeContext(const std::any &context)
Definition: server_util.cpp:28
CTxMemPool & EnsureMemPool(const NodeContext &node)
Definition: server_util.cpp:37
BanMan & EnsureAnyBanman(const std::any &context)
Definition: server_util.cpp:59
PeerManager & EnsurePeerman(const NodeContext &node)
ChainstateManager & EnsureChainman(const NodeContext &node)
Definition: server_util.cpp:77
node::BlockTemplateManager & EnsureBlockTemplateManager(const NodeContext &node)
CTxMemPool & EnsureAnyMemPool(const std::any &context)
Definition: server_util.cpp:45
FeeRateEstimatorManager & EnsureAnyFeeEstimatorMan(const std::any &context)
Definition: server_util.cpp:98
ArgsManager & EnsureArgsman(const NodeContext &node)
Definition: server_util.cpp:64
AddrMan & EnsureAddrman(const NodeContext &node)
CConnman & EnsureConnman(const NodeContext &node)
ArgsManager & EnsureAnyArgsman(const std::any &context)
Definition: server_util.cpp:72
Parameters that influence chain consensus.
Definition: params.h:88
NodeContext struct containing references to chain state and connection state.
Definition: context.h:57
WalletContext context