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