Bitcoin Core 22.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-2020 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_MINER_H
7#define BITCOIN_MINER_H
8
9#include <primitives/block.h>
10#include <txmempool.h>
11#include <validation.h>
12
13#include <memory>
14#include <optional>
15#include <stdint.h>
16
17#include <boost/multi_index_container.hpp>
18#include <boost/multi_index/ordered_index.hpp>
19
20class CBlockIndex;
21class CChainParams;
22class CScript;
23
24namespace Consensus { struct Params; };
25
26static const bool DEFAULT_PRINTPRIORITY = false;
27
29{
31 std::vector<CAmount> vTxFees;
32 std::vector<int64_t> vTxSigOpsCost;
33 std::vector<unsigned char> vchCoinbaseCommitment;
34};
35
36// Container for tracking updates to ancestor feerate as we include (parent)
37// transactions in a block
40 {
41 iter = entry;
42 nSizeWithAncestors = entry->GetSizeWithAncestors();
43 nModFeesWithAncestors = entry->GetModFeesWithAncestors();
44 nSigOpCostWithAncestors = entry->GetSigOpCostWithAncestors();
45 }
46
47 int64_t GetModifiedFee() const { return iter->GetModifiedFee(); }
48 uint64_t GetSizeWithAncestors() const { return nSizeWithAncestors; }
50 size_t GetTxSize() const { return iter->GetTxSize(); }
51 const CTransaction& GetTx() const { return iter->GetTx(); }
52
57};
58
65 bool operator()(const CTxMemPool::txiter& a, const CTxMemPool::txiter& b) const
66 {
67 return &(*a) < &(*b);
68 }
69};
70
74 {
75 return entry.iter;
76 }
77};
78
79// A comparator that sorts transactions based on number of ancestors.
80// This is sufficient to sort an ancestor package in an order that is valid
81// to appear in a block.
83 bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const
84 {
85 if (a->GetCountWithAncestors() != b->GetCountWithAncestors())
86 return a->GetCountWithAncestors() < b->GetCountWithAncestors();
87 return CompareIteratorByHash()(a, b);
88 }
89};
90
91typedef boost::multi_index_container<
93 boost::multi_index::indexed_by<
94 boost::multi_index::ordered_unique<
97 >,
98 // sorted by modified ancestor fee rate
99 boost::multi_index::ordered_non_unique<
100 // Reuse same tag from CTxMemPool's similar index
101 boost::multi_index::tag<ancestor_score>,
102 boost::multi_index::identity<CTxMemPoolModifiedEntry>,
104 >
105 >
107
108typedef indexed_modified_transaction_set::nth_index<0>::type::iterator modtxiter;
109typedef indexed_modified_transaction_set::index<ancestor_score>::type::iterator modtxscoreiter;
110
112{
114
116 {
117 e.nModFeesWithAncestors -= iter->GetFee();
118 e.nSizeWithAncestors -= iter->GetTxSize();
119 e.nSigOpCostWithAncestors -= iter->GetSigOpCost();
120 }
121
123};
124
127{
128private:
129 // The constructed block template
130 std::unique_ptr<CBlockTemplate> pblocktemplate;
131
132 // Configuration parameters for the block size
134 unsigned int nBlockMaxWeight;
136
137 // Information on the current status of the block
138 uint64_t nBlockWeight;
139 uint64_t nBlockTx;
143
144 // Chain context for the block
150
151public:
152 struct Options {
153 Options();
156 };
157
158 explicit BlockAssembler(CChainState& chainstate, const CTxMemPool& mempool, const CChainParams& params);
159 explicit BlockAssembler(CChainState& chainstate, const CTxMemPool& mempool, const CChainParams& params, const Options& options);
160
162 std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn);
163
164 inline static std::optional<int64_t> m_last_block_num_txs{};
165 inline static std::optional<int64_t> m_last_block_weight{};
166
167private:
168 // utility functions
170 void resetBlock();
173
174 // Methods for how to add transactions to a block.
178 void addPackageTxs(int& nPackagesSelected, int& nDescendantsUpdated) EXCLUSIVE_LOCKS_REQUIRED(m_mempool.cs);
179
180 // helper functions for addPackageTxs()
184 bool TestPackage(uint64_t packageSize, int64_t packageSigOpsCost) const;
189 bool TestPackageTransactions(const CTxMemPool::setEntries& package) const;
194 void SortForBlock(const CTxMemPool::setEntries& package, std::vector<CTxMemPool::txiter>& sortedEntries);
199};
200
202void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
203int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
204
206void RegenerateCommitments(CBlock& block, ChainstateManager& chainman);
207
208#endif // BITCOIN_MINER_H
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
const CChainParams & Params()
Return the currently selected parameters.
Generate a new block, without valid proof-of-work.
Definition: miner.h:127
int64_t nLockTimeCutoff
Definition: miner.h:146
int UpdatePackagesForAdded(const CTxMemPool::setEntries &alreadyAdded, indexed_modified_transaction_set &mapModifiedTx) EXCLUSIVE_LOCKS_REQUIRED(m_mempool.cs)
Add descendants of given transactions to mapModifiedTx with ancestor state updated assuming given tra...
Definition: miner.cpp:246
uint64_t nBlockWeight
Definition: miner.h:138
void AddToBlock(CTxMemPool::txiter iter)
Add a tx to the block.
Definition: miner.cpp:227
CTxMemPool::setEntries inBlock
Definition: miner.h:142
CChainState & m_chainstate
Definition: miner.h:149
void addPackageTxs(int &nPackagesSelected, int &nDescendantsUpdated) EXCLUSIVE_LOCKS_REQUIRED(m_mempool.cs)
Add transactions based on feerate including unconfirmed ancestors Increments nPackagesSelected / nDes...
Definition: miner.cpp:309
bool TestPackageTransactions(const CTxMemPool::setEntries &package) const
Perform checks on each transaction in a package: locktime, premature-witness, serialized size (if nec...
Definition: miner.cpp:216
void onlyUnconfirmed(CTxMemPool::setEntries &testSet)
Remove confirmed (inBlock) entries from given set.
Definition: miner.cpp:189
CFeeRate blockMinFeeRate
Definition: miner.h:135
uint64_t nBlockTx
Definition: miner.h:139
static std::optional< int64_t > m_last_block_num_txs
Definition: miner.h:164
bool fIncludeWitness
Definition: miner.h:133
CAmount nFees
Definition: miner.h:141
const CTxMemPool & m_mempool
Definition: miner.h:148
void resetBlock()
Clear the block's state and prepare for assembling a new block.
Definition: miner.cpp:88
void SortForBlock(const CTxMemPool::setEntries &package, std::vector< CTxMemPool::txiter > &sortedEntries)
Sort the package in an order that is valid to appear in a block.
Definition: miner.cpp:288
std::unique_ptr< CBlockTemplate > pblocktemplate
Definition: miner.h:130
BlockAssembler(CChainState &chainstate, const CTxMemPool &mempool, const CChainParams &params)
Definition: miner.cpp:85
const CChainParams & chainparams
Definition: miner.h:147
std::unique_ptr< CBlockTemplate > CreateNewBlock(const CScript &scriptPubKeyIn)
Construct a new block template with coinbase to scriptPubKeyIn.
Definition: miner.cpp:102
static std::optional< int64_t > m_last_block_weight
Definition: miner.h:165
bool SkipMapTxEntry(CTxMemPool::txiter it, indexed_modified_transaction_set &mapModifiedTx, CTxMemPool::setEntries &failedTx) EXCLUSIVE_LOCKS_REQUIRED(m_mempool.cs)
Return true if given transaction from mapTx has already been evaluated, or if the transaction's cache...
Definition: miner.cpp:282
unsigned int nBlockMaxWeight
Definition: miner.h:134
int nHeight
Definition: miner.h:145
uint64_t nBlockSigOpsCost
Definition: miner.h:140
bool TestPackage(uint64_t packageSize, int64_t packageSigOpsCost) const
Test if a new package would "fit" in the block.
Definition: miner.cpp:202
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:21
Definition: block.h:63
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:146
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:70
CChainState stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:544
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:30
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:406
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:260
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:424
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:511
std::set< txiter, CompareIteratorByHash > setEntries
Definition: txmempool.h:517
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
Definition: txmempool.h:514
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:847
Definition: txmempool.h:270
indexed_modified_transaction_set::nth_index< 0 >::type::iterator modtxiter
Definition: miner.h:108
void IncrementExtraNonce(CBlock *pblock, const CBlockIndex *pindexPrev, unsigned int &nExtraNonce)
Modify the extranonce in a block.
Definition: miner.cpp:438
static const bool DEFAULT_PRINTPRIORITY
Definition: miner.h:26
boost::multi_index_container< CTxMemPoolModifiedEntry, boost::multi_index::indexed_by< boost::multi_index::ordered_unique< modifiedentry_iter, CompareCTxMemPoolIter >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< ancestor_score >, boost::multi_index::identity< CTxMemPoolModifiedEntry >, CompareTxMemPoolEntryByAncestorFee > > > indexed_modified_transaction_set
Definition: miner.h:106
indexed_modified_transaction_set::index< ancestor_score >::type::iterator modtxscoreiter
Definition: miner.h:109
int64_t UpdateTime(CBlockHeader *pblock, const Consensus::Params &consensusParams, const CBlockIndex *pindexPrev)
Definition: miner.cpp:28
void RegenerateCommitments(CBlock &block, ChainstateManager &chainman)
Update an old GenerateCoinbaseCommitment from CreateNewBlock after the block txs have changed.
Definition: miner.cpp:43
Transaction validation functions.
Definition: params.h:12
CFeeRate blockMinFeeRate
Definition: miner.h:155
size_t nBlockMaxWeight
Definition: miner.h:154
CBlock block
Definition: miner.h:30
std::vector< CAmount > vTxFees
Definition: miner.h:31
std::vector< int64_t > vTxSigOpsCost
Definition: miner.h:32
std::vector< unsigned char > vchCoinbaseCommitment
Definition: miner.h:33
Definition: miner.h:38
size_t GetTxSize() const
Definition: miner.h:50
CTxMemPoolModifiedEntry(CTxMemPool::txiter entry)
Definition: miner.h:39
int64_t GetModifiedFee() const
Definition: miner.h:47
const CTransaction & GetTx() const
Definition: miner.h:51
uint64_t nSizeWithAncestors
Definition: miner.h:54
CAmount nModFeesWithAncestors
Definition: miner.h:55
CTxMemPool::txiter iter
Definition: miner.h:53
CAmount GetModFeesWithAncestors() const
Definition: miner.h:49
int64_t nSigOpCostWithAncestors
Definition: miner.h:56
uint64_t GetSizeWithAncestors() const
Definition: miner.h:48
Comparator for CTxMemPool::txiter objects.
Definition: miner.h:64
bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const
Definition: miner.h:65
bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const
Definition: miner.h:83
Parameters that influence chain consensus.
Definition: params.h:70
result_type operator()(const CTxMemPoolModifiedEntry &entry) const
Definition: miner.h:73
CTxMemPool::txiter result_type
Definition: miner.h:72
update_for_parent_inclusion(CTxMemPool::txiter it)
Definition: miner.h:113
CTxMemPool::txiter iter
Definition: miner.h:122
void operator()(CTxMemPoolModifiedEntry &e)
Definition: miner.h:115
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49