Bitcoin Core 31.99.0
P2P Digital Currency
miner.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-present The Bitcoin Core developers
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#ifndef BITCOIN_NODE_MINER_H
7#define BITCOIN_NODE_MINER_H
8
9#include <consensus/amount.h>
10#include <node/mining_types.h>
11#include <primitives/block.h>
13#include <threadsafety.h>
14#include <txmempool.h>
15#include <util/feefrac.h>
16#include <util/time.h>
17
18#include <cstdint>
19#include <memory>
20#include <optional>
21#include <string>
22#include <vector>
23
24class CBlockIndex;
25class CChainParams;
26class Chainstate;
28
29namespace Consensus {
30struct Params;
31} // namespace Consensus
32class uint256;
33namespace interfaces {
34struct BlockRef;
35} // namespace interfaces
36
38
39namespace node {
40class KernelNotifications;
41
43{
45 // Fees per transaction, not including coinbase transaction (unlike CBlock::vtx).
46 std::vector<CAmount> vTxFees;
47 // Sigops per transaction, not including coinbase transaction (unlike CBlock::vtx).
48 std::vector<int64_t> vTxSigOpsCost;
49 /* A vector of package fee rates, ordered by the sequence in which
50 * packages are selected for inclusion in the block template.*/
51 std::vector<FeePerVSize> m_package_feerates;
52 /*
53 * Template containing all coinbase transaction fields that are set by our
54 * miner code.
55 */
57};
58
61{
62private:
63 // The constructed block template
64 std::unique_ptr<CBlockTemplate> pblocktemplate;
65
66 // Information on the current status of the block
67 uint64_t nBlockWeight;
68 uint64_t nBlockTx;
71
72 // Chain context for the block
75
77 const CTxMemPool* const m_mempool;
79
80public:
81 explicit BlockAssembler(Chainstate& chainstate,
82 const CTxMemPool* mempool,
83 BlockCreateOptions create_options);
84
86 std::unique_ptr<CBlockTemplate> CreateNewBlock();
87
89 inline static std::optional<int64_t> m_last_block_num_txs{};
91 inline static std::optional<int64_t> m_last_block_weight{};
92
93private:
95
96 // utility functions
98 void resetBlock();
100 void AddToBlock(const CTxMemPoolEntry& entry);
101
102 // Methods for how to add transactions to a block.
108
109 // helper functions for addChunks()
111 bool TestChunkBlockLimits(FeePerWeight chunk_feerate, int64_t chunk_sigops_cost) const;
115 bool TestChunkTransactions(const std::vector<CTxMemPoolEntryRef>& txs) const;
116};
117
123int64_t GetMinimumTime(const CBlockIndex* pindexPrev, int64_t difficulty_adjustment_interval);
124
125int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
126
128void RegenerateCommitments(CBlock& block, ChainstateManager& chainman);
129
130/* Compute the block's merkle root, insert or replace the coinbase transaction and the merkle root into the block */
131void AddMerkleRootAndCoinbase(CBlock& block, CTransactionRef coinbase, uint32_t version, uint32_t timestamp, uint32_t nonce);
132
135bool SubmitBlock(ChainstateManager& chainman, const std::shared_ptr<const CBlock>& block, bool* new_block, std::string& reason, std::string& debug);
136
137/* Interrupt a blocking call. */
138void InterruptWait(KernelNotifications& kernel_notifications, bool& interrupt_wait);
144 KernelNotifications& kernel_notifications,
145 CTxMemPool* mempool,
146 const std::unique_ptr<CBlockTemplate>& block_template,
147 const BlockWaitOptions& wait_options,
148 const BlockCreateOptions& create_options,
149 bool& interrupt_wait);
150
151/* Locks cs_main and returns the block hash and block height of the active chain if it exists; otherwise, returns nullopt.*/
152std::optional<BlockRef> GetTip(ChainstateManager& chainman);
153
154/* Waits for the connected tip to change until timeout has elapsed. During node initialization, this will wait until the tip is connected (regardless of `timeout`).
155 * Returns the current tip, or nullopt if the node is shutting down or interrupt()
156 * is called.
157 */
158std::optional<BlockRef> WaitTipChanged(ChainstateManager& chainman, KernelNotifications& kernel_notifications, const uint256& current_tip, MillisecondsDouble& timeout, bool& interrupt);
159
179bool CooldownIfHeadersAhead(ChainstateManager& chainman, KernelNotifications& kernel_notifications, const BlockRef& last_tip, bool& interrupt_mining);
180} // namespace node
181
182#endif // BITCOIN_NODE_MINER_H
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
static void pool cs
const CChainParams & Params()
Return the currently selected parameters.
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
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:94
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:77
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: mempool_entry.h:66
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:187
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:551
Interface for managing multiple Chainstate objects, where each chainstate is associated with chainsta...
Definition: validation.h:940
Generate a new block, without valid proof-of-work.
Definition: miner.h:61
Chainstate & m_chainstate
Definition: miner.h:78
void resetBlock()
Clear the block's state and prepare for assembling a new block.
Definition: miner.cpp:115
const CTxMemPool *const m_mempool
Definition: miner.h:77
bool TestChunkBlockLimits(FeePerWeight chunk_feerate, int64_t chunk_sigops_cost) const
Test if a new chunk would "fit" in the block.
Definition: miner.cpp:246
bool TestChunkTransactions(const std::vector< CTxMemPoolEntryRef > &txs) const
Perform locktime checks on each transaction in a chunk: This check should always succeed,...
Definition: miner.cpp:259
const CChainParams & chainparams
Definition: miner.h:76
CAmount nFees
Definition: miner.h:70
uint64_t nBlockTx
Definition: miner.h:68
void AddToBlock(const CTxMemPoolEntry &entry)
Add a tx to the block.
Definition: miner.cpp:269
uint64_t nBlockSigOpsCost
Definition: miner.h:69
int64_t m_lock_time_cutoff
Definition: miner.h:74
uint64_t nBlockWeight
Definition: miner.h:67
std::unique_ptr< CBlockTemplate > pblocktemplate
Definition: miner.h:64
static std::optional< int64_t > m_last_block_num_txs
The number of transactions in the last assembled block (excluding coinbase transaction)
Definition: miner.h:89
std::unique_ptr< CBlockTemplate > CreateNewBlock()
Construct a new block template.
Definition: miner.cpp:126
static std::optional< int64_t > m_last_block_weight
The weight of the last assembled block (including reserved weight for block header,...
Definition: miner.h:91
void addChunks() EXCLUSIVE_LOCKS_REQUIRED(m_mempool -> cs)
Add transactions based on chunk feerate.
Definition: miner.cpp:286
const BlockCreateOptions m_options
Definition: miner.h:94
BlockAssembler(Chainstate &chainstate, const CTxMemPool *mempool, BlockCreateOptions create_options)
Definition: miner.cpp:100
256-bit opaque blob.
Definition: uint256.h:196
CTxMemPoolEntry::CTxMemPoolEntryRef CTxMemPoolEntryRef
unsigned int nonce
Definition: miner_tests.cpp:99
is used externally by mining IPC clients, so it should only declare simple data definitions.
Transaction validation functions.
Definition: messages.h:21
void RegenerateCommitments(CBlock &block, ChainstateManager &chainman)
Update an old GenerateCoinbaseCommitment from CreateNewBlock after the block txs have changed.
Definition: miner.cpp:88
std::optional< BlockRef > WaitTipChanged(ChainstateManager &chainman, KernelNotifications &kernel_notifications, const uint256 &current_tip, MillisecondsDouble &timeout, bool &interrupt)
Definition: miner.cpp:555
int64_t UpdateTime(CBlockHeader *pblock, const Consensus::Params &consensusParams, const CBlockIndex *pindexPrev)
Definition: miner.cpp:70
bool SubmitBlock(ChainstateManager &chainman, const std::shared_ptr< const CBlock > &block, bool *new_block, std::string &reason, std::string &debug)
Submit a block and capture the validation state via the BlockChecked callback.
Definition: miner.cpp:384
int64_t GetMinimumTime(const CBlockIndex *pindexPrev, const int64_t difficulty_adjustment_interval)
Get the minimum time a miner should use in the next block.
Definition: miner.cpp:57
void InterruptWait(KernelNotifications &kernel_notifications, bool &interrupt_wait)
Definition: miner.cpp:417
std::unique_ptr< CBlockTemplate > WaitAndCreateNewBlock(ChainstateManager &chainman, KernelNotifications &kernel_notifications, CTxMemPool *mempool, const std::unique_ptr< CBlockTemplate > &block_template, const BlockWaitOptions &wait_options, const BlockCreateOptions &create_options, bool &interrupt_wait)
Return a new block template when fees rise to a certain threshold or after a new tip; return nullopt ...
Definition: miner.cpp:424
void AddMerkleRootAndCoinbase(CBlock &block, CTransactionRef coinbase, uint32_t version, uint32_t timestamp, uint32_t nonce)
Definition: miner.cpp:343
bool CooldownIfHeadersAhead(ChainstateManager &chainman, KernelNotifications &kernel_notifications, const BlockRef &last_tip, bool &interrupt_mining)
Wait while the best known header extends the current chain tip AND at least one block is being added ...
Definition: miner.cpp:521
std::optional< BlockRef > GetTip(ChainstateManager &chainman)
Definition: miner.cpp:513
Definition: common.h:30
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:403
Tagged wrapper around FeeFrac to avoid unit confusion.
Definition: feefrac.h:192
Hash/height pair to help track and identify blocks.
Definition: types.h:13
Block template creation options.
Definition: mining_types.h:33
CoinbaseTx m_coinbase_tx
Definition: miner.h:56
std::vector< int64_t > vTxSigOpsCost
Definition: miner.h:48
std::vector< CAmount > vTxFees
Definition: miner.h:46
std::vector< FeePerVSize > m_package_feerates
Definition: miner.h:51
Template containing all coinbase transaction fields that are set by our miner code.
Definition: mining_types.h:132
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
std::chrono::duration< double, std::chrono::milliseconds::period > MillisecondsDouble
Definition: time.h:103