Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2021-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <rpc/server_util.h>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>17#include <any>18#include <memory>19#include <string>21namespace Consensus {22struct Params;23} // namespace Consensus25using node::NodeContext;26using node::UpdateTime;28NodeContext& EnsureAnyNodeContext(const std::any& context)29{30auto node_context = util::AnyPtr<NodeContext>(context);31if (!node_context) {32throw JSONRPCError(RPC_INTERNAL_ERROR, "Node context not found");33}34return *node_context;35}37CTxMemPool& EnsureMemPool(const NodeContext& node)38{39if (!node.mempool) {40throw JSONRPCError(RPC_CLIENT_MEMPOOL_DISABLED, "Mempool disabled or instance not found");41}42return *node.mempool;43}45CTxMemPool& EnsureAnyMemPool(const std::any& context)46{47return EnsureMemPool(EnsureAnyNodeContext(context));48}51BanMan& EnsureBanman(const NodeContext& node)52{53if (!node.banman) {54throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");55}56return *node.banman;57}59BanMan& EnsureAnyBanman(const std::any& context)60{61return EnsureBanman(EnsureAnyNodeContext(context));62}64ArgsManager& EnsureArgsman(const NodeContext& node)65{66if (!node.args) {67throw JSONRPCError(RPC_INTERNAL_ERROR, "Node args not found");68}69return *node.args;70}72ArgsManager& EnsureAnyArgsman(const std::any& context)73{74return EnsureArgsman(EnsureAnyNodeContext(context));75}77ChainstateManager& EnsureChainman(const NodeContext& node)78{79if (!node.chainman) {80throw JSONRPCError(RPC_INTERNAL_ERROR, "Node chainman not found");81}82return *node.chainman;83}85ChainstateManager& EnsureAnyChainman(const std::any& context)86{87return EnsureChainman(EnsureAnyNodeContext(context));88}90FeeRateEstimatorManager& EnsureFeeEstimatorMan(const NodeContext& node)91{92if (!node.fee_estimator_man) {93throw JSONRPCError(RPC_INTERNAL_ERROR, "Fee estimation disabled");94}95return *node.fee_estimator_man;96}98FeeRateEstimatorManager& EnsureAnyFeeEstimatorMan(const std::any& context)99{100return EnsureFeeEstimatorMan(EnsureAnyNodeContext(context));101}103CConnman& EnsureConnman(const NodeContext& node)104{105if (!node.connman) {106throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");107}108return *node.connman;109}111interfaces::Mining& EnsureMining(const NodeContext& node)112{113if (!node.mining) {114throw JSONRPCError(RPC_INTERNAL_ERROR, "Node miner not found");115}116return *node.mining;117}119PeerManager& EnsurePeerman(const NodeContext& node)120{121if (!node.peerman) {122throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");123}124return *node.peerman;125}127AddrMan& EnsureAddrman(const NodeContext& node)128{129if (!node.addrman) {130throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled");131}132return *node.addrman;133}135AddrMan& EnsureAnyAddrman(const std::any& context)136{137return EnsureAddrman(EnsureAnyNodeContext(context));138}140void NextEmptyBlockIndex(CBlockIndex& tip, const Consensus::Params& consensusParams, CBlockIndex& next_index)141{142CBlockHeader next_header{};143next_header.hashPrevBlock = tip.GetBlockHash();144UpdateTime(&next_header, consensusParams, &tip);145next_header.nBits = GetNextWorkRequired(&tip, &next_header, consensusParams);146next_header.nNonce = 0;148next_index.pprev = &tip;149next_index.nTime = next_header.nTime;150next_index.nBits = next_header.nBits;151next_index.nNonce = next_header.nNonce;152next_index.nHeight = tip.nHeight + 1;153}