Bitcoin Core 32.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
17#include <cstdint>
18#include <memory>
19#include <optional>
20#include <vector>
21
22class CBlockIndex;
23class CChainParams;
24class Chainstate;
26
27namespace Consensus {
28struct Params;
29} // namespace Consensus
30namespace node {
31
33{
35 // Fees per transaction, not including coinbase transaction (unlike CBlock::vtx).
36 std::vector<CAmount> vTxFees;
37 // Sigops per transaction, not including coinbase transaction (unlike CBlock::vtx).
38 std::vector<int64_t> vTxSigOpsCost;
39 /* A vector of package fee rates, ordered by the sequence in which
40 * packages are selected for inclusion in the block template.*/
41 std::vector<FeePerVSize> m_package_feerates;
42 /*
43 * Template containing all coinbase transaction fields that are set by our
44 * miner code.
45 */
47};
48
51{
52private:
53 // The constructed block template
54 std::unique_ptr<CBlockTemplate> pblocktemplate;
55
56 // Information on the current status of the block
57 uint64_t nBlockWeight;
58 uint64_t nBlockTx;
61
62 // Chain context for the block
65
67 const CTxMemPool* const m_mempool;
69
70public:
71 explicit BlockAssembler(Chainstate& chainstate,
72 const CTxMemPool* mempool,
73 BlockCreateOptions create_options);
74
76 std::unique_ptr<CBlockTemplate> CreateNewBlock();
77
79 inline static std::optional<int64_t> m_last_block_num_txs{};
81 inline static std::optional<int64_t> m_last_block_weight{};
82
83private:
85
86 // utility functions
88 void resetBlock();
90 void AddToBlock(const CTxMemPoolEntry& entry);
91
92 // Methods for how to add transactions to a block.
98
99 // helper functions for addChunks()
101 bool TestChunkBlockLimits(int64_t chunk_weight, int64_t chunk_sigops_cost) const;
105 bool TestChunkTransactions(const std::vector<CTxMemPoolEntryRef>& txs) const;
106};
107
113int64_t GetMinimumTime(const CBlockIndex* pindexPrev, int64_t difficulty_adjustment_interval);
114
115int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
116
118void RegenerateCommitments(CBlock& block, ChainstateManager& chainman);
119
120/* Compute the block's merkle root, insert or replace the coinbase transaction and the merkle root into the block */
121void AddMerkleRootAndCoinbase(CBlock& block, CTransactionRef coinbase, uint32_t version, uint32_t timestamp, uint32_t nonce);
122} // namespace node
123
124#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:188
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:550
Interface for managing multiple Chainstate objects, where each chainstate is associated with chainsta...
Definition: validation.h:950
Generate a new block, without valid proof-of-work.
Definition: miner.h:51
Chainstate & m_chainstate
Definition: miner.h:68
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:67
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:261
const CChainParams & chainparams
Definition: miner.h:66
CAmount nFees
Definition: miner.h:60
uint64_t nBlockTx
Definition: miner.h:58
void AddToBlock(const CTxMemPoolEntry &entry)
Add a tx to the block.
Definition: miner.cpp:271
uint64_t nBlockSigOpsCost
Definition: miner.h:59
int64_t m_lock_time_cutoff
Definition: miner.h:64
uint64_t nBlockWeight
Definition: miner.h:57
std::unique_ptr< CBlockTemplate > pblocktemplate
Definition: miner.h:54
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:79
bool TestChunkBlockLimits(int64_t chunk_weight, int64_t chunk_sigops_cost) const
Test if a new chunk would "fit" in the block.
Definition: miner.cpp:246
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:81
void addChunks() EXCLUSIVE_LOCKS_REQUIRED(m_mempool -> cs)
Add transactions based on chunk feerate.
Definition: miner.cpp:288
const BlockCreateOptions m_options
Definition: miner.h:84
BlockAssembler(Chainstate &chainstate, const CTxMemPool *mempool, BlockCreateOptions create_options)
Definition: miner.cpp:100
CTxMemPoolEntry::CTxMemPoolEntryRef CTxMemPoolEntryRef
unsigned int nonce
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
int64_t UpdateTime(CBlockHeader *pblock, const Consensus::Params &consensusParams, const CBlockIndex *pindexPrev)
Definition: miner.cpp:70
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:50
void AddMerkleRootAndCoinbase(CBlock &block, CTransactionRef coinbase, uint32_t version, uint32_t timestamp, uint32_t nonce)
Definition: miner.cpp:349
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:417
Block template creation options.
Definition: mining_types.h:33
CoinbaseTx m_coinbase_tx
Definition: miner.h:46
std::vector< int64_t > vTxSigOpsCost
Definition: miner.h:38
std::vector< CAmount > vTxFees
Definition: miner.h:36
std::vector< FeePerVSize > m_package_feerates
Definition: miner.h:41
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