Bitcoin Core  24.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-2022 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 <policy/policy.h>
10 #include <primitives/block.h>
11 #include <txmempool.h>
12 
13 #include <memory>
14 #include <optional>
15 #include <stdint.h>
16 
17 #include <boost/multi_index/ordered_index.hpp>
18 #include <boost/multi_index_container.hpp>
19 
20 class ArgsManager;
21 class ChainstateManager;
22 class CBlockIndex;
23 class CChainParams;
24 class CScript;
25 
26 namespace Consensus { struct Params; };
27 
28 namespace node {
29 static const bool DEFAULT_PRINTPRIORITY = false;
30 
32 {
34  std::vector<CAmount> vTxFees;
35  std::vector<int64_t> vTxSigOpsCost;
36  std::vector<unsigned char> vchCoinbaseCommitment;
37 };
38 
39 // Container for tracking updates to ancestor feerate as we include (parent)
40 // transactions in a block
43  {
44  iter = entry;
45  nSizeWithAncestors = entry->GetSizeWithAncestors();
46  nModFeesWithAncestors = entry->GetModFeesWithAncestors();
47  nSigOpCostWithAncestors = entry->GetSigOpCostWithAncestors();
48  }
49 
50  CAmount GetModifiedFee() const { return iter->GetModifiedFee(); }
51  uint64_t GetSizeWithAncestors() const { return nSizeWithAncestors; }
53  size_t GetTxSize() const { return iter->GetTxSize(); }
54  const CTransaction& GetTx() const { return iter->GetTx(); }
55 
60 };
61 
68  bool operator()(const CTxMemPool::txiter& a, const CTxMemPool::txiter& b) const
69  {
70  return &(*a) < &(*b);
71  }
72 };
73 
77  {
78  return entry.iter;
79  }
80 };
81 
82 // A comparator that sorts transactions based on number of ancestors.
83 // This is sufficient to sort an ancestor package in an order that is valid
84 // to appear in a block.
86  bool operator()(const CTxMemPool::txiter& a, const CTxMemPool::txiter& b) const
87  {
88  if (a->GetCountWithAncestors() != b->GetCountWithAncestors()) {
89  return a->GetCountWithAncestors() < b->GetCountWithAncestors();
90  }
91  return CompareIteratorByHash()(a, b);
92  }
93 };
94 
95 typedef boost::multi_index_container<
96  CTxMemPoolModifiedEntry,
97  boost::multi_index::indexed_by<
98  boost::multi_index::ordered_unique<
99  modifiedentry_iter,
100  CompareCTxMemPoolIter
101  >,
102  // sorted by modified ancestor fee rate
103  boost::multi_index::ordered_non_unique<
104  // Reuse same tag from CTxMemPool's similar index
105  boost::multi_index::tag<ancestor_score>,
106  boost::multi_index::identity<CTxMemPoolModifiedEntry>,
108  >
109  >
111 
112 typedef indexed_modified_transaction_set::nth_index<0>::type::iterator modtxiter;
113 typedef indexed_modified_transaction_set::index<ancestor_score>::type::iterator modtxscoreiter;
114 
116 {
118 
120  {
121  e.nModFeesWithAncestors -= iter->GetModifiedFee();
122  e.nSizeWithAncestors -= iter->GetTxSize();
123  e.nSigOpCostWithAncestors -= iter->GetSigOpCost();
124  }
125 
127 };
128 
131 {
132 private:
133  // The constructed block template
134  std::unique_ptr<CBlockTemplate> pblocktemplate;
135 
136  // Information on the current status of the block
137  uint64_t nBlockWeight;
138  uint64_t nBlockTx;
142 
143  // Chain context for the block
144  int nHeight;
146 
148  const CTxMemPool* const m_mempool;
150 
151 public:
152  struct Options {
153  // Configuration parameters for the block size
156  // Whether to call TestBlockValidity() at the end of CreateNewBlock().
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:
171 
172  // utility functions
174  void resetBlock();
176  void AddToBlock(CTxMemPool::txiter iter);
177 
178  // Methods for how to add transactions to a block.
182  void addPackageTxs(const CTxMemPool& mempool, int& nPackagesSelected, int& nDescendantsUpdated) EXCLUSIVE_LOCKS_REQUIRED(mempool.cs);
183 
184  // helper functions for addPackageTxs()
188  bool TestPackage(uint64_t packageSize, int64_t packageSigOpsCost) const;
193  bool TestPackageTransactions(const CTxMemPool::setEntries& package) const;
195  void SortForBlock(const CTxMemPool::setEntries& package, std::vector<CTxMemPool::txiter>& sortedEntries);
196 };
197 
198 int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
199 
201 void RegenerateCommitments(CBlock& block, ChainstateManager& chainman);
202 
205 } // namespace node
206 
207 #endif // BITCOIN_NODE_MINER_H
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:94
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:151
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:76
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
Definition: feerate.h:33
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:411
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:295
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:316
std::set< txiter, CompareIteratorByHash > setEntries
Definition: txmempool.h:411
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
Definition: txmempool.h:408
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:455
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:866
Definition: txmempool.h:160
Generate a new block, without valid proof-of-work.
Definition: miner.h:131
Chainstate & m_chainstate
Definition: miner.h:149
void onlyUnconfirmed(CTxMemPool::setEntries &testSet)
Remove confirmed (inBlock) entries from given set.
Definition: miner.cpp:186
void resetBlock()
Clear the block's state and prepare for assembling a new block.
Definition: miner.cpp:92
bool TestPackage(uint64_t packageSize, int64_t packageSigOpsCost) const
Test if a new package would "fit" in the block.
Definition: miner.cpp:198
const CTxMemPool *const m_mempool
Definition: miner.h:148
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:212
const CChainParams & chainparams
Definition: miner.h:147
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:292
std::unique_ptr< CBlockTemplate > CreateNewBlock(const CScript &scriptPubKeyIn)
Construct a new block template with coinbase to scriptPubKeyIn.
Definition: miner.cpp:105
const Options m_options
Definition: miner.h:170
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:271
uint64_t nBlockTx
Definition: miner.h:138
CTxMemPool::setEntries inBlock
Definition: miner.h:141
uint64_t nBlockSigOpsCost
Definition: miner.h:139
BlockAssembler(Chainstate &chainstate, const CTxMemPool *mempool)
Definition: miner.cpp:89
int64_t m_lock_time_cutoff
Definition: miner.h:145
uint64_t nBlockWeight
Definition: miner.h:137
std::unique_ptr< CBlockTemplate > pblocktemplate
Definition: miner.h:134
static std::optional< int64_t > m_last_block_num_txs
Definition: miner.h:166
void AddToBlock(CTxMemPool::txiter iter)
Add a tx to the block.
Definition: miner.cpp:222
static std::optional< int64_t > m_last_block_weight
Definition: miner.h:167
Transaction validation functions.
Definition: init.h:25
indexed_modified_transaction_set::nth_index< 0 >::type::iterator modtxiter
Definition: miner.h:112
void RegenerateCommitments(CBlock &block, ChainstateManager &chainman)
Update an old GenerateCoinbaseCommitment from CreateNewBlock after the block txs have changed.
Definition: miner.cpp:47
static const bool DEFAULT_PRINTPRIORITY
Definition: miner.h:29
int64_t UpdateTime(CBlockHeader *pblock, const Consensus::Params &consensusParams, const CBlockIndex *pindexPrev)
Definition: miner.cpp:30
indexed_modified_transaction_set::index< ancestor_score >::type::iterator modtxscoreiter
Definition: miner.h:113
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:110
std::optional< bilingual_str > ApplyArgsManOptions(const ArgsManager &args, BlockManager::Options &opts)
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:25
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:23
Parameters that influence chain consensus.
Definition: params.h:74
std::vector< int64_t > vTxSigOpsCost
Definition: miner.h:35
std::vector< CAmount > vTxFees
Definition: miner.h:34
std::vector< unsigned char > vchCoinbaseCommitment
Definition: miner.h:36
Definition: miner.h:41
CAmount nModFeesWithAncestors
Definition: miner.h:58
CAmount GetModFeesWithAncestors() const
Definition: miner.h:52
CTxMemPoolModifiedEntry(CTxMemPool::txiter entry)
Definition: miner.h:42
CAmount GetModifiedFee() const
Definition: miner.h:50
size_t GetTxSize() const
Definition: miner.h:53
uint64_t GetSizeWithAncestors() const
Definition: miner.h:51
uint64_t nSizeWithAncestors
Definition: miner.h:57
CTxMemPool::txiter iter
Definition: miner.h:56
int64_t nSigOpCostWithAncestors
Definition: miner.h:59
const CTransaction & GetTx() const
Definition: miner.h:54
Comparator for CTxMemPool::txiter objects.
Definition: miner.h:67
bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const
Definition: miner.h:68
bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const
Definition: miner.h:86
CTxMemPool::txiter result_type
Definition: miner.h:75
result_type operator()(const CTxMemPoolModifiedEntry &entry) const
Definition: miner.h:76
update_for_parent_inclusion(CTxMemPool::txiter it)
Definition: miner.h:117
CTxMemPool::txiter iter
Definition: miner.h:126
void operator()(CTxMemPoolModifiedEntry &e)
Definition: miner.h:119
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
ArgsManager gArgs
Definition: system.cpp:73