Bitcoin Core 30.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 <interfaces/types.h>
10#include <node/types.h>
11#include <policy/policy.h>
12#include <primitives/block.h>
13#include <txmempool.h>
14#include <util/feefrac.h>
15
16#include <cstdint>
17#include <memory>
18#include <optional>
19
20#include <boost/multi_index/identity.hpp>
21#include <boost/multi_index/indexed_by.hpp>
22#include <boost/multi_index/ordered_index.hpp>
23#include <boost/multi_index/tag.hpp>
24#include <boost/multi_index_container.hpp>
25
26class ArgsManager;
27class CBlockIndex;
28class CChainParams;
29class CScript;
30class Chainstate;
32
33namespace Consensus { struct Params; };
34
36
37namespace node {
38class KernelNotifications;
39
40static const bool DEFAULT_PRINT_MODIFIED_FEE = false;
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 std::vector<unsigned char> vchCoinbaseCommitment;
50 /* A vector of package fee rates, ordered by the sequence in which
51 * packages are selected for inclusion in the block template.*/
52 std::vector<FeePerVSize> m_package_feerates;
53};
54
57{
58private:
59 // The constructed block template
60 std::unique_ptr<CBlockTemplate> pblocktemplate;
61
62 // Information on the current status of the block
63 uint64_t nBlockWeight;
64 uint64_t nBlockTx;
67
68 // Chain context for the block
71
73 const CTxMemPool* const m_mempool;
75
76public:
78 // Configuration parameters for the block size
81 // Whether to call TestBlockValidity() at the end of CreateNewBlock().
84 };
85
86 explicit BlockAssembler(Chainstate& chainstate, const CTxMemPool* mempool, const Options& options);
87
89 std::unique_ptr<CBlockTemplate> CreateNewBlock();
90
92 inline static std::optional<int64_t> m_last_block_num_txs{};
94 inline static std::optional<int64_t> m_last_block_weight{};
95
96private:
98
99 // utility functions
101 void resetBlock();
103 void AddToBlock(const CTxMemPoolEntry& entry);
104
105 // Methods for how to add transactions to a block.
111
112 // helper functions for addChunks()
114 bool TestChunkBlockLimits(FeePerWeight chunk_feerate, int64_t chunk_sigops_cost) const;
118 bool TestChunkTransactions(const std::vector<CTxMemPoolEntryRef>& txs) const;
119};
120
126int64_t GetMinimumTime(const CBlockIndex* pindexPrev, const int64_t difficulty_adjustment_interval);
127
128int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
129
131void RegenerateCommitments(CBlock& block, ChainstateManager& chainman);
132
135
136/* Compute the block's merkle root, insert or replace the coinbase transaction and the merkle root into the block */
137void AddMerkleRootAndCoinbase(CBlock& block, CTransactionRef coinbase, uint32_t version, uint32_t timestamp, uint32_t nonce);
138
139
140/* Interrupt the current wait for the next block template. */
141void InterruptWait(KernelNotifications& kernel_notifications, bool& interrupt_wait);
147 KernelNotifications& kernel_notifications,
148 CTxMemPool* mempool,
149 const std::unique_ptr<CBlockTemplate>& block_template,
150 const BlockWaitOptions& options,
151 const BlockAssembler::Options& assemble_options,
152 bool& interrupt_wait);
153
154/* Locks cs_main and returns the block hash and block height of the active chain if it exists; otherwise, returns nullopt.*/
155std::optional<BlockRef> GetTip(ChainstateManager& chainman);
156
157/* 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`).
158 * Returns the current tip, or nullopt if the node is shutting down. */
159std::optional<BlockRef> WaitTipChanged(ChainstateManager& chainman, KernelNotifications& kernel_notifications, const uint256& current_tip, MillisecondsDouble& timeout);
160} // namespace node
161
162#endif // BITCOIN_NODE_MINER_H
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
ArgsManager gArgs
Definition: args.cpp:40
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:22
Definition: block.h:69
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:103
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:78
Fee rate in satoshis per virtualbyte: CAmount / vB the feerate is represented internally as FeeFrac.
Definition: feerate.h:35
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:405
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: mempool_entry.h:67
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:189
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:532
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:899
Generate a new block, without valid proof-of-work.
Definition: miner.h:57
Chainstate & m_chainstate
Definition: miner.h:74
void resetBlock()
Clear the block's state and prepare for assembling a new block.
Definition: miner.cpp:108
const CTxMemPool *const m_mempool
Definition: miner.h:73
bool TestChunkBlockLimits(FeePerWeight chunk_feerate, int64_t chunk_sigops_cost) const
Test if a new chunk would "fit" in the block.
Definition: miner.cpp:196
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:209
const CChainParams & chainparams
Definition: miner.h:72
CAmount nFees
Definition: miner.h:66
const Options m_options
Definition: miner.h:97
uint64_t nBlockTx
Definition: miner.h:64
void AddToBlock(const CTxMemPoolEntry &entry)
Add a tx to the block.
Definition: miner.cpp:219
uint64_t nBlockSigOpsCost
Definition: miner.h:65
int64_t m_lock_time_cutoff
Definition: miner.h:70
uint64_t nBlockWeight
Definition: miner.h:63
std::unique_ptr< CBlockTemplate > pblocktemplate
Definition: miner.h:60
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:92
BlockAssembler(Chainstate &chainstate, const CTxMemPool *mempool, const Options &options)
Definition: miner.cpp:89
std::unique_ptr< CBlockTemplate > CreateNewBlock()
Construct a new block template.
Definition: miner.cpp:119
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:94
void addChunks() EXCLUSIVE_LOCKS_REQUIRED(m_mempool -> cs)
Add transactions based on chunk feerate.
Definition: miner.cpp:236
256-bit opaque blob.
Definition: uint256.h:195
CTxMemPoolEntry::CTxMemPoolEntryRef CTxMemPoolEntryRef
unsigned int nonce
Definition: miner_tests.cpp:81
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:67
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:36
int64_t UpdateTime(CBlockHeader *pblock, const Consensus::Params &consensusParams, const CBlockIndex *pindexPrev)
Definition: miner.cpp:49
util::Result< void > ApplyArgsManOptions(const ArgsManager &args, BlockManager::Options &opts)
std::unique_ptr< CBlockTemplate > WaitAndCreateNewBlock(ChainstateManager &chainman, KernelNotifications &kernel_notifications, CTxMemPool *mempool, const std::unique_ptr< CBlockTemplate > &block_template, const BlockWaitOptions &options, const BlockAssembler::Options &assemble_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:318
static const bool DEFAULT_PRINT_MODIFIED_FEE
Definition: miner.h:40
std::optional< BlockRef > WaitTipChanged(ChainstateManager &chainman, KernelNotifications &kernel_notifications, const uint256 &current_tip, MillisecondsDouble &timeout)
Definition: miner.cpp:415
void InterruptWait(KernelNotifications &kernel_notifications, bool &interrupt_wait)
Definition: miner.cpp:311
void AddMerkleRootAndCoinbase(CBlock &block, CTransactionRef coinbase, uint32_t version, uint32_t timestamp, uint32_t nonce)
Definition: miner.cpp:293
std::optional< BlockRef > GetTip(ChainstateManager &chainman)
Definition: miner.cpp:407
is a home for public enum and struct type definitions that are used internally by node code,...
static constexpr unsigned int DEFAULT_BLOCK_MIN_TX_FEE
Default for -blockmintxfee, which sets the minimum feerate for a transaction in blocks created by min...
Definition: policy.h:33
static constexpr unsigned int DEFAULT_BLOCK_MAX_WEIGHT
Default for -blockmaxweight, which controls the range of block weights the mining code will create.
Definition: policy.h:24
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:403
Tagged wrapper around FeeFrac to avoid unit confusion.
Definition: feefrac.h:239
Hash/height pair to help track and identify blocks.
Definition: types.h:13
std::vector< int64_t > vTxSigOpsCost
Definition: miner.h:48
std::vector< CAmount > vTxFees
Definition: miner.h:46
std::vector< unsigned char > vchCoinbaseCommitment
Definition: miner.h:49
std::vector< FeePerVSize > m_package_feerates
Definition: miner.h:52
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:51
std::chrono::duration< double, std::chrono::milliseconds::period > MillisecondsDouble
Definition: time.h:88