Bitcoin Core 31.99.0
P2P Digital Currency
mining.h
Go to the documentation of this file.
1// Copyright (c) 2024-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#ifndef BITCOIN_INTERFACES_MINING_H
6#define BITCOIN_INTERFACES_MINING_H
7
8#include <consensus/amount.h>
9#include <interfaces/types.h>
10#include <node/mining_types.h>
11#include <primitives/block.h>
13#include <uint256.h>
14#include <util/time.h>
15
16#include <cstdint>
17#include <memory>
18#include <optional>
19#include <stdexcept>
20#include <string>
21#include <vector>
22
23namespace node {
24struct NodeContext;
25} // namespace node
26
27namespace interfaces {
28
31{
32public:
33 virtual ~BlockTemplate() = default;
34
36 // Block contains a dummy coinbase transaction that should not be used and
37 // it may not match a transaction constructed from getCoinbaseTx().
38 virtual CBlock getBlock() = 0;
39
40 // Fees per transaction, not including coinbase transaction.
41 virtual std::vector<CAmount> getTxFees() = 0;
42 // Sigop cost per transaction, not including coinbase transaction.
43 virtual std::vector<int64_t> getTxSigops() = 0;
44
47
53 virtual std::vector<uint256> getCoinbaseMerklePath() = 0;
54
77 virtual bool submitSolution(uint32_t version, uint32_t timestamp, uint32_t nonce, CTransactionRef coinbase, std::string& reason, std::string& debug) = 0;
78
81 virtual bool submitSolutionOld7(uint32_t, uint32_t, uint32_t, CTransactionRef)
82 {
83 throw std::runtime_error("Old submitSolution (@7) not supported. Please update your client!");
84 }
85
97 virtual std::unique_ptr<BlockTemplate> waitNext(node::BlockWaitOptions options = {}) = 0;
98
102 virtual void interruptWait() = 0;
103};
104
108{
109public:
110 virtual ~Mining() = default;
111
113 virtual bool isTestChain() = 0;
114
116 virtual bool isInitialBlockDownload() = 0;
117
119 virtual std::optional<BlockRef> getTip() = 0;
120
132 virtual std::optional<BlockRef> waitTipChanged(uint256 current_tip, MillisecondsDouble timeout = MillisecondsDouble::max()) = 0;
133
146 virtual std::unique_ptr<BlockTemplate> createNewBlock(const node::BlockCreateOptions& options = {}, bool cooldown = true) = 0;
147
151 virtual void interrupt() = 0;
152
166 virtual bool checkBlock(const CBlock& block, const node::BlockCheckOptions& options, std::string& reason, std::string& debug) = 0;
167
187 virtual bool submitBlock(const CBlock& block, std::string& reason, std::string& debug) = 0;
188
196 virtual std::vector<CTransactionRef> getTransactionsByTxID(const std::vector<Txid>& txids) = 0;
197
205 virtual std::vector<CTransactionRef> getTransactionsByWitnessID(const std::vector<Wtxid>& wtxids) = 0;
206
209 virtual const node::NodeContext* context() { return nullptr; }
210};
211
217std::unique_ptr<Mining> MakeMining(const node::NodeContext& node, bool wait_loaded=true);
218
219} // namespace interfaces
220
221#endif // BITCOIN_INTERFACES_MINING_H
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:27
Definition: block.h:74
Block template interface.
Definition: mining.h:31
virtual bool submitSolutionOld7(uint32_t, uint32_t, uint32_t, CTransactionRef)
Deprecated older method preserved to return an explicit error for IPC clients using mining....
Definition: mining.h:81
virtual std::vector< int64_t > getTxSigops()=0
virtual bool submitSolution(uint32_t version, uint32_t timestamp, uint32_t nonce, CTransactionRef coinbase, std::string &reason, std::string &debug)=0
Construct and broadcast the block.
virtual std::vector< CAmount > getTxFees()=0
virtual node::CoinbaseTx getCoinbaseTx()=0
Return fields needed to construct a coinbase transaction.
virtual std::vector< uint256 > getCoinbaseMerklePath()=0
Compute merkle path to the coinbase transaction.
virtual std::unique_ptr< BlockTemplate > waitNext(node::BlockWaitOptions options={})=0
Waits for fees in the next block to rise, a new tip or the timeout.
virtual void interruptWait()=0
Interrupts the current wait for the next block template.
virtual CBlock getBlock()=0
virtual CBlockHeader getBlockHeader()=0
virtual ~BlockTemplate()=default
Interface giving clients (RPC, Stratum v2 Template Provider in the future) ability to create block te...
Definition: mining.h:108
virtual ~Mining()=default
virtual bool submitBlock(const CBlock &block, std::string &reason, std::string &debug)=0
Process a fully assembled block.
virtual bool checkBlock(const CBlock &block, const node::BlockCheckOptions &options, std::string &reason, std::string &debug)=0
Checks if a given block is valid.
virtual std::optional< BlockRef > waitTipChanged(uint256 current_tip, MillisecondsDouble timeout=MillisecondsDouble::max())=0
Waits for the connected tip to change.
virtual bool isInitialBlockDownload()=0
Returns whether IBD is still in progress.
virtual const node::NodeContext * context()
Get internal node context.
Definition: mining.h:209
virtual std::vector< CTransactionRef > getTransactionsByTxID(const std::vector< Txid > &txids)=0
Fetch raw transactions from the mempool by txid.
virtual void interrupt()=0
Interrupts createNewBlock and waitTipChanged.
virtual std::optional< BlockRef > getTip()=0
Returns the hash and height for the tip of this chain.
virtual bool isTestChain()=0
If this chain is exclusively used for testing.
virtual std::vector< CTransactionRef > getTransactionsByWitnessID(const std::vector< Wtxid > &wtxids)=0
Fetch raw transactions from the mempool by wtxid.
virtual std::unique_ptr< BlockTemplate > createNewBlock(const node::BlockCreateOptions &options={}, bool cooldown=true)=0
Construct a new block template.
256-bit opaque blob.
Definition: uint256.h:196
unsigned int nonce
is used externally by mining IPC clients, so it should only declare simple data definitions.
std::unique_ptr< Mining > MakeMining(const node::NodeContext &node, bool wait_loaded=true)
Return implementation of Mining interface.
Definition: messages.h:21
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:403
Block template creation options.
Definition: mining_types.h:33
Template containing all coinbase transaction fields that are set by our miner code.
Definition: mining_types.h:132
NodeContext struct containing references to chain state and connection state.
Definition: context.h:59
std::chrono::duration< double, std::chrono::milliseconds::period > MillisecondsDouble
Definition: time.h:103