Bitcoin Core  31.0.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/types.h>
11 #include <primitives/block.h>
12 #include <primitives/transaction.h>
13 #include <uint256.h>
14 #include <util/time.h>
15 
16 #include <cstdint>
17 #include <memory>
18 #include <optional>
19 #include <vector>
20 
21 namespace node {
22 struct NodeContext;
23 } // namespace node
24 
26 class CScript;
27 
28 namespace interfaces {
29 
32 {
33 public:
34  virtual ~BlockTemplate() = default;
35 
36  virtual CBlockHeader getBlockHeader() = 0;
37  // Block contains a dummy coinbase transaction that should not be used.
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 
46  virtual node::CoinbaseTx getCoinbaseTx() = 0;
47 
53  virtual std::vector<uint256> getCoinbaseMerklePath() = 0;
54 
73  virtual bool submitSolution(uint32_t version, uint32_t timestamp, uint32_t nonce, CTransactionRef coinbase) = 0;
74 
86  virtual std::unique_ptr<BlockTemplate> waitNext(node::BlockWaitOptions options = {}) = 0;
87 
91  virtual void interruptWait() = 0;
92 };
93 
96 class Mining
97 {
98 public:
99  virtual ~Mining() = default;
100 
102  virtual bool isTestChain() = 0;
103 
105  virtual bool isInitialBlockDownload() = 0;
106 
108  virtual std::optional<BlockRef> getTip() = 0;
109 
121  virtual std::optional<BlockRef> waitTipChanged(uint256 current_tip, MillisecondsDouble timeout = MillisecondsDouble::max()) = 0;
122 
135  virtual std::unique_ptr<BlockTemplate> createNewBlock(const node::BlockCreateOptions& options = {}, bool cooldown = true) = 0;
136 
140  virtual void interrupt() = 0;
141 
155  virtual bool checkBlock(const CBlock& block, const node::BlockCheckOptions& options, std::string& reason, std::string& debug) = 0;
156 
159  virtual node::NodeContext* context() { return nullptr; }
160 };
161 
167 std::unique_ptr<Mining> MakeMining(node::NodeContext& node, bool wait_loaded=true);
168 
169 } // namespace interfaces
170 
171 #endif // BITCOIN_INTERFACES_MINING_H
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:403
Block template interface.
Definition: mining.h:31
virtual CBlockHeader getBlockHeader()=0
unsigned int nonce
Definition: miner_tests.cpp:82
Definition: block.h:73
virtual ~Mining()=default
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 node::CoinbaseTx getCoinbaseTx()=0
Return fields needed to construct a coinbase transaction.
NodeContext struct containing references to chain state and connection state.
Definition: context.h:56
Interface giving clients (RPC, Stratum v2 Template Provider in the future) ability to create block te...
Definition: mining.h:96
std::unique_ptr< Mining > MakeMining(node::NodeContext &node, bool wait_loaded=true)
Return implementation of Mining interface.
virtual bool submitSolution(uint32_t version, uint32_t timestamp, uint32_t nonce, CTransactionRef coinbase)=0
Construct and broadcast the block.
Template containing all coinbase transaction fields that are set by our miner code.
Definition: types.h:119
virtual std::vector< CAmount > getTxFees()=0
virtual void interrupt()=0
Interrupts createNewBlock and waitTipChanged.
virtual std::vector< uint256 > getCoinbaseMerklePath()=0
Compute merkle path to the coinbase transaction.
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 bool isInitialBlockDownload()=0
Returns whether IBD is still in progress.
Definition: messages.h:21
256-bit opaque blob.
Definition: uint256.h:195
virtual node::NodeContext * context()
Get internal node context.
Definition: mining.h:159
virtual ~BlockTemplate()=default
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:404
virtual std::optional< BlockRef > getTip()=0
Returns the hash and height for the tip of this chain.
virtual std::vector< int64_t > getTxSigops()=0
virtual std::optional< BlockRef > waitTipChanged(uint256 current_tip, MillisecondsDouble timeout=MillisecondsDouble::max())=0
Waits for the connected tip to change.
virtual CBlock getBlock()=0
is a home for public enum and struct type definitions that are used internally by node code...
virtual bool isTestChain()=0
If this chain is exclusively used for testing.
std::chrono::duration< double, std::chrono::milliseconds::period > MillisecondsDouble
Definition: time.h:94
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:26
virtual void interruptWait()=0
Interrupts the current wait for the next block template.
virtual std::unique_ptr< BlockTemplate > createNewBlock(const node::BlockCreateOptions &options={}, bool cooldown=true)=0
Construct a new block template.