Coverage Report

Created: 2026-07-23 20:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/interfaces/mining.h
Line
Count
Source
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>
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 <stdexcept>
20
#include <string>
21
#include <vector>
22
23
namespace node {
24
struct NodeContext;
25
} // namespace node
26
27
namespace interfaces {
28
29
//! Block template interface
30
class BlockTemplate
31
{
32
public:
33
47.9k
    virtual ~BlockTemplate() = default;
34
35
    virtual CBlockHeader getBlockHeader() = 0;
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
45
    /** Return fields needed to construct a coinbase transaction */
46
    virtual node::CoinbaseTx getCoinbaseTx() = 0;
47
48
    /**
49
     * Compute merkle path to the coinbase transaction
50
     *
51
     * @return merkle path ordered from the deepest
52
     */
53
    virtual std::vector<uint256> getCoinbaseMerklePath() = 0;
54
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 field
60
     * @param[in] timestamp time block header field (unix timestamp)
61
     * @param[in] nonce nonce block header field
62
     * @param[in] coinbase complete coinbase transaction (including witness)
63
     * @param[out] reason failure reason (BIP22)
64
     * @param[out] debug  more detailed rejection reason
65
     *
66
     * @note Unlike the submitblock RPC, this method does not call
67
     *       UpdateUncommittedBlockStructures to add a missing coinbase witness
68
     *       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_prefix
72
     *       is only one byte long, so the coinbase scriptSig needs at least
73
     *       one additional byte of data to avoid bad-cb-length.
74
     *
75
     * @returns true if the block was accepted as a new block
76
     */
77
    virtual bool submitSolution(uint32_t version, uint32_t timestamp, uint32_t nonce, CTransactionRef coinbase, std::string& reason, std::string& debug) = 0;
78
79
    //! Deprecated older method preserved to return an explicit error for IPC
80
    //! clients using mining.capnp @7.
81
    virtual bool submitSolutionOld7(uint32_t, uint32_t, uint32_t, CTransactionRef)
82
55
    {
83
55
        throw std::runtime_error("Old submitSolution (@7) not supported. Please update your client!");
84
55
    }
85
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 fees
90
     *                      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 if
95
     * the tip is more than 20 minutes old.
96
     */
97
    virtual std::unique_ptr<BlockTemplate> waitNext(node::BlockWaitOptions options = {}) = 0;
98
99
    /**
100
     * Interrupts the current wait for the next block template.
101
    */
102
    virtual void interruptWait() = 0;
103
};
104
105
//! Interface giving clients (RPC, Stratum v2 Template Provider in the future)
106
//! ability to create block templates.
107
class Mining
108
{
109
public:
110
9.44k
    virtual ~Mining() = default;
111
112
    //! If this chain is exclusively used for testing
113
    virtual bool isTestChain() = 0;
114
115
    //! Returns whether IBD is still in progress.
116
    virtual bool isInitialBlockDownload() = 0;
117
118
    //! Returns the hash and height for the tip of this chain
119
    virtual std::optional<BlockRef> getTip() = 0;
120
121
    /**
122
     * Waits for the connected tip to change. During node initialization, this will
123
     * wait until the tip is connected (regardless of `timeout`).
124
     *
125
     * @param[in] current_tip block hash of the current chain tip. Function waits
126
     *                        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
     */
132
    virtual std::optional<BlockRef> waitTipChanged(uint256 current_tip, MillisecondsDouble timeout = MillisecondsDouble::max()) = 0;
133
134
   /**
135
     * Construct a new block template.
136
     *
137
     * @param[in] options options for creating the block
138
     * @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 the
140
     *                     tip to catch up. It's recommended to disable this on
141
     *                     regtest and signets with only one miner, as these
142
     *                     could stall.
143
     * @retval BlockTemplate a block template.
144
     * @retval std::nullptr if the node is shut down or interrupt() is called.
145
     */
146
    virtual std::unique_ptr<BlockTemplate> createNewBlock(const node::BlockCreateOptions& options = {}, bool cooldown = true) = 0;
147
148
    /**
149
     * Interrupts createNewBlock and waitTipChanged.
150
     */
151
    virtual void interrupt() = 0;
152
153
    /**
154
     * Checks if a given block is valid.
155
     *
156
     * @param[in] block       the block to check
157
     * @param[in] options     verification options: the proof-of-work check can be
158
     *                        skipped in order to verify a template generated by
159
     *                        external software.
160
     * @param[out] reason     failure reason (BIP22)
161
     * @param[out] debug      more detailed rejection reason
162
     * @returns               whether the block is valid
163
     *
164
     * For signets the challenge verification is skipped when check_pow is false.
165
     */
166
    virtual bool checkBlock(const CBlock& block, const node::BlockCheckOptions& options, std::string& reason, std::string& debug) = 0;
167
168
    /**
169
     * Process a fully assembled block.
170
     *
171
     * Similar to the submitblock RPC. Accepts a complete block, validates
172
     * it, and if accepted as new, processes it into chainstate. Accepted
173
     * blocks may then be announced to peers through normal validation signals.
174
     *
175
     * @param[in]  block  the complete block to submit
176
     * @param[out] reason failure reason (BIP22)
177
     * @param[out] debug  more detailed rejection reason
178
     * @returns           true if the block was accepted as a new block. Returns
179
     *                    false and sets reason if the block is a duplicate or
180
     *                    the validation result is inconclusive.
181
     *
182
     * @note Unlike the submitblock RPC, this method does not call
183
     *       UpdateUncommittedBlockStructures to add a missing coinbase witness
184
     *       reserved value. Callers must submit a fully formed block, including
185
     *       the coinbase witness when a witness commitment is present.
186
     */
187
    virtual bool submitBlock(const CBlock& block, std::string& reason, std::string& debug) = 0;
188
189
    /**
190
     * Fetch raw transactions from the mempool by txid.
191
     *
192
     * @param[in] txids   transaction ids to look up
193
     * @returns           one entry per requested txid containing the
194
     *                    transaction if found, otherwise nullptr
195
     */
196
    virtual std::vector<CTransactionRef> getTransactionsByTxID(const std::vector<Txid>& txids) = 0;
197
198
    /**
199
     * Fetch raw transactions from the mempool by wtxid.
200
     *
201
     * @param[in] wtxids   witness transaction ids to look up
202
     * @returns            one entry per requested wtxid containing the
203
     *                     transaction if found, otherwise nullptr
204
     */
205
    virtual std::vector<CTransactionRef> getTransactionsByWitnessID(const std::vector<Wtxid>& wtxids) = 0;
206
207
    //! Get internal node context. Useful for RPC and testing,
208
    //! but not accessible across processes.
209
0
    virtual const node::NodeContext* context() { return nullptr; }
210
};
211
212
//! Return implementation of Mining interface.
213
//!
214
//! @param[in] wait_loaded waits for chainstate data to be loaded before
215
//!                        returning. Used to prevent external clients from
216
//!                        being able to crash the node during startup.
217
std::unique_ptr<Mining> MakeMining(const node::NodeContext& node, bool wait_loaded=true);
218
219
} // namespace interfaces
220
221
#endif // BITCOIN_INTERFACES_MINING_H