Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
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
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
63
65{
66 if (!node.args) {
67 throw JSONRPCError(RPC_INTERNAL_ERROR, "Node args not found");
68 }
69 return *node.args;
70}
71
76
78{
79 if (!node.chainman) {
80 throw JSONRPCError(RPC_INTERNAL_ERROR, "Node chainman not found");
81 }
82 return *node.chainman;
83}
84
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
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
139
141{
143 next_header.hashPrevBlock = tip.GetBlockHash();
144 UpdateTime(&next_header, consensusParams, &tip);
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
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
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
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition txmempool.h:188
Interface for managing multiple Chainstate objects, where each chainstate is associated with chainsta...
Definition validation.h:950
Manages fee rate estimators.
Creates block templates, submits solved blocks, and provides tip-waiting helpers for mining code.
Transaction validation functions.
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)
BanMan & EnsureBanman(const NodeContext &node)
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)
NodeContext & EnsureAnyNodeContext(const std::any &context)
CTxMemPool & EnsureMemPool(const NodeContext &node)
BanMan & EnsureAnyBanman(const std::any &context)
PeerManager & EnsurePeerman(const NodeContext &node)
ChainstateManager & EnsureChainman(const NodeContext &node)
node::BlockTemplateManager & EnsureBlockTemplateManager(const NodeContext &node)
CTxMemPool & EnsureAnyMemPool(const std::any &context)
FeeRateEstimatorManager & EnsureAnyFeeEstimatorMan(const std::any &context)
ArgsManager & EnsureArgsman(const NodeContext &node)
AddrMan & EnsureAddrman(const NodeContext &node)
CConnman & EnsureConnman(const NodeContext &node)
ArgsManager & EnsureAnyArgsman(const std::any &context)
Parameters that influence chain consensus.
Definition params.h:88
NodeContext struct containing references to chain state and connection state.
Definition context.h:57
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82
WalletContext context