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) 2024-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#ifndef BITCOIN_INTERFACES_MINING_H6#define BITCOIN_INTERFACES_MINING_H8#include <consensus/amount.h>9#include <interfaces/types.h>10#include <node/mining_types.h>11#include <primitives/block.h>12#include <primitives/transaction.h>13#include <uint256.h>14#include <util/time.h>16#include <cstdint>17#include <memory>18#include <optional>19#include <stdexcept>20#include <string>21#include <vector>23namespace node {24struct NodeContext;25} // namespace node27namespace interfaces {29//! Block template interface30class BlockTemplate31{32public:33virtual ~BlockTemplate() = default;35virtual CBlockHeader getBlockHeader() = 0;36// Block contains a dummy coinbase transaction that should not be used and37// it may not match a transaction constructed from getCoinbaseTx().38virtual CBlock getBlock() = 0;40// Fees per transaction, not including coinbase transaction.41virtual std::vector<CAmount> getTxFees() = 0;42// Sigop cost per transaction, not including coinbase transaction.43virtual std::vector<int64_t> getTxSigops() = 0;45/** Return fields needed to construct a coinbase transaction */46virtual node::CoinbaseTx getCoinbaseTx() = 0;48/**49* Compute merkle path to the coinbase transaction50*51* @return merkle path ordered from the deepest52*/53virtual std::vector<uint256> getCoinbaseMerklePath() = 0;55/**56* Construct and broadcast the block. Modifies the template in place,57* updating the fields listed below as well as the merkle root.58*59* @param[in] version version block header field60* @param[in] timestamp time block header field (unix timestamp)61* @param[in] nonce nonce block header field62* @param[in] coinbase complete coinbase transaction (including witness)63* @param[out] reason failure reason (BIP22)64* @param[out] debug more detailed rejection reason65*66* @note Unlike the submitblock RPC, this method does not call67* UpdateUncommittedBlockStructures to add a missing coinbase witness68* reserved value. Callers must provide a complete coinbase transaction,69* including the witness when a witness commitment is present.70*71* @note for heights <= 16, the BIP34 height push in getCoinbaseTx().script_sig_prefix72* is only one byte long, so the coinbase scriptSig needs at least73* one additional byte of data to avoid bad-cb-length.74*75* @returns true if the block was accepted as a new block76*/77virtual bool submitSolution(uint32_t version, uint32_t timestamp, uint32_t nonce, CTransactionRef coinbase, std::string& reason, std::string& debug) = 0;79//! Deprecated older method preserved to return an explicit error for IPC80//! clients using mining.capnp @7.81virtual bool submitSolutionOld7(uint32_t, uint32_t, uint32_t, CTransactionRef)82{83throw std::runtime_error("Old submitSolution (@7) not supported. Please update your client!");84}86/**87* Waits for fees in the next block to rise, a new tip or the timeout.88*89* @param[in] options Control the timeout (default forever) and by how much total fees90* for the next block should rise (default infinite).91*92* @returns a new BlockTemplate or nothing if the timeout occurs.93*94* On testnet this will additionally return a template with difficulty 1 if95* the tip is more than 20 minutes old.96*/97virtual std::unique_ptr<BlockTemplate> waitNext(node::BlockWaitOptions options = {}) = 0;99/**100* Interrupts the current wait for the next block template.101*/102virtual void interruptWait() = 0;103};105//! Interface giving clients (RPC, Stratum v2 Template Provider in the future)106//! ability to create block templates.107class Mining108{109public:110virtual ~Mining() = default;112//! If this chain is exclusively used for testing113virtual bool isTestChain() = 0;115//! Returns whether IBD is still in progress.116virtual bool isInitialBlockDownload() = 0;118//! Returns the hash and height for the tip of this chain119virtual std::optional<BlockRef> getTip() = 0;121/**122* Waits for the connected tip to change. During node initialization, this will123* wait until the tip is connected (regardless of `timeout`).124*125* @param[in] current_tip block hash of the current chain tip. Function waits126* for the chain tip to differ from this.127* @param[in] timeout how long to wait for a new tip (default is forever)128*129* @retval BlockRef hash and height of the current chain tip after this call.130* @retval std::nullopt if the node is shut down or interrupt() is called.131*/132virtual std::optional<BlockRef> waitTipChanged(uint256 current_tip, MillisecondsDouble timeout = MillisecondsDouble::max()) = 0;134/**135* Construct a new block template.136*137* @param[in] options options for creating the block138* @param[in] cooldown wait for tip to be connected and IBD to complete.139* If the best header is ahead of the tip, wait for the140* tip to catch up. It's recommended to disable this on141* regtest and signets with only one miner, as these142* could stall.143* @retval BlockTemplate a block template.144* @retval std::nullptr if the node is shut down or interrupt() is called.145*/146virtual std::unique_ptr<BlockTemplate> createNewBlock(const node::BlockCreateOptions& options = {}, bool cooldown = true) = 0;148/**149* Interrupts createNewBlock and waitTipChanged.150*/151virtual void interrupt() = 0;153/**154* Checks if a given block is valid.155*156* @param[in] block the block to check157* @param[in] options verification options: the proof-of-work check can be158* skipped in order to verify a template generated by159* external software.160* @param[out] reason failure reason (BIP22)161* @param[out] debug more detailed rejection reason162* @returns whether the block is valid163*164* For signets the challenge verification is skipped when check_pow is false.165*/166virtual bool checkBlock(const CBlock& block, const node::BlockCheckOptions& options, std::string& reason, std::string& debug) = 0;168/**169* Process a fully assembled block.170*171* Similar to the submitblock RPC. Accepts a complete block, validates172* it, and if accepted as new, processes it into chainstate. Accepted173* blocks may then be announced to peers through normal validation signals.174*175* @param[in] block the complete block to submit176* @param[out] reason failure reason (BIP22)177* @param[out] debug more detailed rejection reason178* @returns true if the block was accepted as a new block. Returns179* false and sets reason if the block is a duplicate or180* the validation result is inconclusive.181*182* @note Unlike the submitblock RPC, this method does not call183* UpdateUncommittedBlockStructures to add a missing coinbase witness184* reserved value. Callers must submit a fully formed block, including185* the coinbase witness when a witness commitment is present.186*/187virtual bool submitBlock(const CBlock& block, std::string& reason, std::string& debug) = 0;189/**190* Fetch raw transactions from the mempool by txid.191*192* @param[in] txids transaction ids to look up193* @returns one entry per requested txid containing the194* transaction if found, otherwise nullptr195*/196virtual std::vector<CTransactionRef> getTransactionsByTxID(const std::vector<Txid>& txids) = 0;198/**199* Fetch raw transactions from the mempool by wtxid.200*201* @param[in] wtxids witness transaction ids to look up202* @returns one entry per requested wtxid containing the203* transaction if found, otherwise nullptr204*/205virtual std::vector<CTransactionRef> getTransactionsByWitnessID(const std::vector<Wtxid>& wtxids) = 0;207//! Get internal node context. Useful for RPC and testing,208//! but not accessible across processes.209virtual const node::NodeContext* context() { return nullptr; }210};212//! Return implementation of Mining interface.213//!214//! @param[in] wait_loaded waits for chainstate data to be loaded before215//! returning. Used to prevent external clients from216//! being able to crash the node during startup.217std::unique_ptr<Mining> MakeMining(const node::NodeContext& node, bool wait_loaded=true);219} // namespace interfaces221#endif // BITCOIN_INTERFACES_MINING_H
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.