Bitcoin Core  24.1.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  CAmount 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->GetModifiedFee();
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
135  unsigned int nBlockMaxWeight;
137 
138  // Information on the current status of the block
139  uint64_t nBlockWeight;
140  uint64_t nBlockTx;
144 
145  // Chain context for the block
146  int nHeight;
148 
150  const CTxMemPool* const m_mempool;
152 
153 public:
154  struct Options {
155  Options();
158  };
159 
160  explicit BlockAssembler(Chainstate& chainstate, const CTxMemPool* mempool);
161  explicit BlockAssembler(Chainstate& chainstate, const CTxMemPool* mempool, const Options& options);
162 
164  std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn);
165 
166  inline static std::optional<int64_t> m_last_block_num_txs{};
167  inline static std::optional<int64_t> m_last_block_weight{};
168 
169 private:
170  // utility functions
172  void resetBlock();
174  void AddToBlock(CTxMemPool::txiter iter);
175 
176  // Methods for how to add transactions to a block.
180  void addPackageTxs(const CTxMemPool& mempool, int& nPackagesSelected, int& nDescendantsUpdated) EXCLUSIVE_LOCKS_REQUIRED(mempool.cs);
181 
182  // helper functions for addPackageTxs()
186  bool TestPackage(uint64_t packageSize, int64_t packageSigOpsCost) const;
191  bool TestPackageTransactions(const CTxMemPool::setEntries& package) const;
193  void SortForBlock(const CTxMemPool::setEntries& package, std::vector<CTxMemPool::txiter>& sortedEntries);
194 };
195 
196 int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
197 
199 void RegenerateCommitments(CBlock& block, ChainstateManager& chainman);
200 } // namespace node
201 
202 #endif // BITCOIN_NODE_MINER_H
indexed_modified_transaction_set::index< ancestor_score >::type::iterator modtxscoreiter
Definition: miner.h:111
void onlyUnconfirmed(CTxMemPool::setEntries &testSet)
Remove confirmed (inBlock) entries from given set.
Definition: miner.cpp:183
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
Definition: txmempool.h:524
Generate a new block, without valid proof-of-work.
Definition: miner.h:128
CTxMemPool::setEntries inBlock
Definition: miner.h:143
uint64_t nBlockWeight
Definition: miner.h:139
void resetBlock()
Clear the block&#39;s state and prepare for assembling a new block.
Definition: miner.cpp:93
Definition: block.h:68
bool TestPackage(uint64_t packageSize, int64_t packageSigOpsCost) const
Test if a new package would "fit" in the block.
Definition: miner.cpp:195
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:799
bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const
Definition: miner.h:84
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:209
CFeeRate blockMinFeeRate
Definition: miner.h:136
BlockAssembler(Chainstate &chainstate, const CTxMemPool *mempool)
Definition: miner.cpp:90
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:272
static std::optional< int64_t > m_last_block_num_txs
Definition: miner.h:166
uint64_t nBlockTx
Definition: miner.h:140
std::set< txiter, CompareIteratorByHash > setEntries
Definition: txmempool.h:527
unsigned int nBlockMaxWeight
Definition: miner.h:135
Comparator for CTxMemPool::txiter objects.
Definition: miner.h:65
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system...
Definition: chainparams.h:69
void addPackageTxs(const CTxMemPool &mempool, int &nPackagesSelected, int &nDescendantsUpdated) EXCLUSIVE_LOCKS_REQUIRED(mempool.cs)
Add transactions based on feerate including unconfirmed ancestors Increments nPackagesSelected / nDes...
Definition: miner.cpp:293
void operator()(CTxMemPoolModifiedEntry &e)
Definition: miner.h:117
int64_t UpdateTime(CBlockHeader *pblock, const Consensus::Params &consensusParams, const CBlockIndex *pindexPrev)
Definition: miner.cpp:30
int64_t m_lock_time_cutoff
Definition: miner.h:147
std::vector< unsigned char > vchCoinbaseCommitment
Definition: miner.h:34
Definition: miner.h:39
void AddToBlock(CTxMemPool::txiter iter)
Add a tx to the block.
Definition: miner.cpp:219
Transaction validation functions.
Definition: params.h:15
const CTransaction & GetTx() const
Definition: miner.h:52
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
std::vector< int64_t > vTxSigOpsCost
Definition: miner.h:33
CAmount nModFeesWithAncestors
Definition: miner.h:56
CAmount GetModifiedFee() const
Definition: miner.h:48
Chainstate stores and provides an API to update our local knowledge of the current best chain...
Definition: validation.h:437
Definition: txmempool.h:277
size_t GetTxSize() const
Definition: miner.h:51
std::unique_ptr< CBlockTemplate > CreateNewBlock(const CScript &scriptPubKeyIn)
Construct a new block template with coinbase to scriptPubKeyIn.
Definition: miner.cpp:106
Parameters that influence chain consensus.
Definition: params.h:73
Chainstate & m_chainstate
Definition: miner.h:151
CTxMemPoolModifiedEntry(CTxMemPool::txiter entry)
Definition: miner.h:40
Definition: init.h:25
CTxMemPool::txiter iter
Definition: miner.h:124
CTxMemPool::txiter iter
Definition: miner.h:54
update_for_parent_inclusion(CTxMemPool::txiter it)
Definition: miner.h:115
static std::optional< int64_t > m_last_block_weight
Definition: miner.h:167
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
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
CAmount GetModFeesWithAncestors() const
Definition: miner.h:50
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:431
int64_t nSigOpCostWithAncestors
Definition: miner.h:57
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:151
const CChainParams & Params()
Return the currently selected parameters.
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:410
bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const
Definition: miner.h:66
const CChainParams & chainparams
Definition: miner.h:149
indexed_modified_transaction_set::nth_index< 0 >::type::iterator modtxiter
Definition: miner.h:110
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
Definition: feerate.h:32
uint64_t nBlockSigOpsCost
Definition: miner.h:141
std::vector< CAmount > vTxFees
Definition: miner.h:32
CTxMemPool::txiter result_type
Definition: miner.h:73
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:287
result_type operator()(const CTxMemPoolModifiedEntry &entry) const
Definition: miner.h:74
std::unique_ptr< CBlockTemplate > pblocktemplate
Definition: miner.h:132
static const bool DEFAULT_PRINTPRIORITY
Definition: miner.h:27
void RegenerateCommitments(CBlock &block, ChainstateManager &chainman)
Update an old GenerateCoinbaseCommitment from CreateNewBlock after the block txs have changed...
Definition: miner.cpp:47
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:21
uint64_t nSizeWithAncestors
Definition: miner.h:55
uint64_t GetSizeWithAncestors() const
Definition: miner.h:49
const CTxMemPool *const m_mempool
Definition: miner.h:150