Bitcoin ABC  0.24.7
P2P Digital Currency
miner.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2019 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 
12 #include <boost/multi_index/ordered_index.hpp>
13 #include <boost/multi_index_container.hpp>
14 
15 #include <cstdint>
16 #include <memory>
17 #include <optional>
18 
19 class CBlockIndex;
20 class CChainParams;
21 class Config;
22 class CScript;
23 
24 namespace Consensus {
25 struct Params;
26 }
27 
28 static const bool DEFAULT_PRINTPRIORITY = false;
29 
33  int64_t sigOpCount;
34 
35  CBlockTemplateEntry(CTransactionRef _tx, Amount _fees, int64_t _sigOpCount)
36  : tx(_tx), fees(_fees), sigOpCount(_sigOpCount){};
37 };
38 
41 
42  std::vector<CBlockTemplateEntry> entries;
43 };
44 
45 // Container for tracking updates to ancestor feerate as we include (parent)
46 // transactions in a block
49  iter = entry;
50  nSizeWithAncestors = entry->GetSizeWithAncestors();
51  nModFeesWithAncestors = entry->GetModFeesWithAncestors();
52  nSigOpCountWithAncestors = entry->GetSigOpCountWithAncestors();
53  }
54 
55  Amount GetModifiedFee() const { return iter->GetModifiedFee(); }
56  uint64_t GetSizeWithAncestors() const { return nSizeWithAncestors; }
57  uint64_t GetVirtualSizeWithAncestors() const;
59  size_t GetTxSize() const { return iter->GetTxSize(); }
60  size_t GetTxVirtualSize() const { return iter->GetTxVirtualSize(); }
61  const CTransaction &GetTx() const { return iter->GetTx(); }
62 
67 };
68 
77  const CTxMemPool::txiter &b) const {
78  return &(*a) < &(*b);
79  }
80 };
81 
85  return entry.iter;
86  }
87 };
88 
89 // A comparator that sorts transactions based on number of ancestors.
90 // This is sufficient to sort an ancestor package in an order that is valid
91 // to appear in a block.
94  const CTxMemPool::txiter &b) const {
95  if (a->GetCountWithAncestors() != b->GetCountWithAncestors()) {
96  return a->GetCountWithAncestors() < b->GetCountWithAncestors();
97  }
98  return CompareIteratorById()(a, b);
99  }
100 };
101 
102 typedef boost::multi_index_container<
104  boost::multi_index::indexed_by<
105  boost::multi_index::ordered_unique<modifiedentry_iter,
107  // sorted by modified ancestor fee rate
108  boost::multi_index::ordered_non_unique<
109  // Reuse same tag from CTxMemPool's similar index
110  boost::multi_index::tag<ancestor_score>,
111  boost::multi_index::identity<CTxMemPoolModifiedEntry>,
114 
115 typedef indexed_modified_transaction_set::nth_index<0>::type::iterator
117 typedef indexed_modified_transaction_set::index<ancestor_score>::type::iterator
119 
122 
124  e.nModFeesWithAncestors -= iter->GetFee();
125  e.nSizeWithAncestors -= iter->GetTxSize();
126  e.nSigOpCountWithAncestors -= iter->GetSigOpCount();
127  }
128 
130 };
131 
134 private:
135  // The constructed block template
136  std::unique_ptr<CBlockTemplate> pblocktemplate;
137 
138  // Configuration parameters for the block size
142 
143  // Information on the current status of the block
144  uint64_t nBlockSize;
145  uint64_t nBlockTx;
146  uint64_t nBlockSigOps;
149 
150  // Chain context for the block
151  int nHeight;
155 
157 
158 public:
159  struct Options {
160  Options();
164  };
165 
166  BlockAssembler(const Config &config, const CTxMemPool &mempool);
167  BlockAssembler(const CChainParams &params, const CTxMemPool &mempool,
168  const Options &options);
169 
171  std::unique_ptr<CBlockTemplate>
172  CreateNewBlock(const CScript &scriptPubKeyIn);
173 
175 
176  static std::optional<int64_t> m_last_block_num_txs;
177  static std::optional<int64_t> m_last_block_size;
178 
179 private:
180  // utility functions
182  void resetBlock();
184  void AddToBlock(CTxMemPool::txiter iter);
185 
186  // Methods for how to add transactions to a block.
192  void addPackageTxs(int &nPackagesSelected, int &nDescendantsUpdated)
194 
195  // helper functions for addPackageTxs()
199  bool TestPackage(uint64_t packageSize, int64_t packageSigOpCount) const;
206  bool TestPackageTransactions(const CTxMemPool::setEntries &package);
212  indexed_modified_transaction_set &mapModifiedTx,
213  CTxMemPool::setEntries &failedTx)
216  void SortForBlock(const CTxMemPool::setEntries &package,
217  std::vector<CTxMemPool::txiter> &sortedEntries);
223  int UpdatePackagesForAdded(const CTxMemPool::setEntries &alreadyAdded,
224  indexed_modified_transaction_set &mapModifiedTx)
226 };
227 
229 void IncrementExtraNonce(CBlock *pblock, const CBlockIndex *pindexPrev,
230  uint64_t nExcessiveBlockSize,
231  unsigned int &nExtraNonce);
232 int64_t UpdateTime(CBlockHeader *pblock, const CChainParams &chainParams,
233  const CBlockIndex *pindexPrev);
234 #endif // BITCOIN_MINER_H
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:361
BlockAssembler::Options::blockMinFeeRate
CFeeRate blockMinFeeRate
Definition: miner.h:163
block.h
CompareIteratorById
Definition: txmempool.h:55
BlockAssembler::m_last_block_size
static std::optional< int64_t > m_last_block_size
Definition: miner.h:177
BlockAssembler::nBlockSize
uint64_t nBlockSize
Definition: miner.h:144
CTxMemPoolModifiedEntry::nSigOpCountWithAncestors
int64_t nSigOpCountWithAncestors
Definition: miner.h:66
BlockAssembler::AddToBlock
void AddToBlock(CTxMemPool::txiter iter)
Add a tx to the block.
Definition: miner.cpp:297
modifiedentry_iter::result_type
CTxMemPool::txiter result_type
Definition: miner.h:83
IncrementExtraNonce
void IncrementExtraNonce(CBlock *pblock, const CBlockIndex *pindexPrev, uint64_t nExcessiveBlockSize, unsigned int &nExtraNonce)
Modify the extranonce in a block.
Definition: miner.cpp:546
modifiedentry_iter
Definition: miner.h:82
CTxMemPoolModifiedEntry::nModFeesWithAncestors
Amount nModFeesWithAncestors
Definition: miner.h:65
BlockAssembler::Options::nExcessiveBlockSize
uint64_t nExcessiveBlockSize
Definition: miner.h:161
CBlockHeader
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:22
BlockAssembler::resetBlock
void resetBlock()
Clear the block's state and prepare for assembling a new block.
Definition: miner.cpp:105
CTxMemPool::txiter
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
Definition: txmempool.h:580
CTxMemPoolModifiedEntry::GetTx
const CTransaction & GetTx() const
Definition: miner.h:61
CTxMemPoolModifiedEntry::GetTxSize
size_t GetTxSize() const
Definition: miner.h:59
CTxMemPool
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:495
CompareTxMemPoolEntryByAncestorFee
Definition: txmempool.h:333
CompareCTxMemPoolIter
Comparator for CTxMemPool::txiter objects.
Definition: miner.h:75
CChainParams
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:47
BlockAssembler::nMaxGeneratedBlockSigChecks
uint64_t nMaxGeneratedBlockSigChecks
Definition: miner.h:140
CTxMemPool::setEntries
std::set< txiter, CompareIteratorById > setEntries
Definition: txmempool.h:584
BlockAssembler
Generate a new block, without valid proof-of-work.
Definition: miner.h:133
CBlockTemplateEntry
Definition: miner.h:30
CFeeRate
Fee rate in satoshis per kilobyte: Amount / kB.
Definition: feerate.h:21
CTxMemPoolModifiedEntry::CTxMemPoolModifiedEntry
CTxMemPoolModifiedEntry(CTxMemPool::txiter entry)
Definition: miner.h:48
BlockAssembler::Options::Options
Options()
Definition: miner.cpp:55
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:113
BlockAssembler::nBlockTx
uint64_t nBlockTx
Definition: miner.h:145
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:194
BlockAssembler::m_last_block_num_txs
static std::optional< int64_t > m_last_block_num_txs
Definition: miner.h:176
modtxiter
indexed_modified_transaction_set::nth_index< 0 >::type::iterator modtxiter
Definition: miner.h:116
txmempool.h
CTxMemPoolModifiedEntry::iter
CTxMemPool::txiter iter
Definition: miner.h:63
Config
Definition: config.h:17
CTxMemPool::cs
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:577
BlockAssembler::blockMinFeeRate
CFeeRate blockMinFeeRate
Definition: miner.h:141
BlockAssembler::m_mempool
const CTxMemPool & m_mempool
Definition: miner.h:156
Consensus
Definition: blockdb.h:10
update_for_parent_inclusion
Definition: miner.h:120
CBlockTemplateEntry::tx
CTransactionRef tx
Definition: miner.h:31
DEFAULT_PRINTPRIORITY
static const bool DEFAULT_PRINTPRIORITY
Definition: miner.h:28
modifiedentry_iter::operator()
result_type operator()(const CTxMemPoolModifiedEntry &entry) const
Definition: miner.h:84
CTxMemPoolModifiedEntry::GetModFeesWithAncestors
Amount GetModFeesWithAncestors() const
Definition: miner.h:58
BlockAssembler::nHeight
int nHeight
Definition: miner.h:151
BlockAssembler::Options::nMaxGeneratedBlockSize
uint64_t nMaxGeneratedBlockSize
Definition: miner.h:162
BlockAssembler::nLockTimeCutoff
int64_t nLockTimeCutoff
Definition: miner.h:152
CBlockTemplateEntry::sigOpCount
int64_t sigOpCount
Definition: miner.h:33
CompareCTxMemPoolIter::operator()
bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const
Definition: miner.h:76
CTxMemPoolModifiedEntry::GetSizeWithAncestors
uint64_t GetSizeWithAncestors() const
Definition: miner.h:56
BlockAssembler::TestPackage
bool TestPackage(uint64_t packageSize, int64_t packageSigOpCount) const
Test if a new package would "fit" in the block.
Definition: miner.cpp:256
CompareTxIterByAncestorCount::operator()
bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const
Definition: miner.h:93
update_for_parent_inclusion::operator()
void operator()(CTxMemPoolModifiedEntry &e)
Definition: miner.h:123
UpdateTime
int64_t UpdateTime(CBlockHeader *pblock, const CChainParams &chainParams, const CBlockIndex *pindexPrev)
Definition: miner.cpp:32
BlockAssembler::onlyUnconfirmed
void onlyUnconfirmed(CTxMemPool::setEntries &testSet)
Remove confirmed (inBlock) entries from given set.
Definition: miner.cpp:244
BlockAssembler::TestPackageTransactions
bool TestPackageTransactions(const CTxMemPool::setEntries &package)
Perform checks on each transaction in a package: locktime, serialized size (if necessary).
Definition: miner.cpp:275
BlockAssembler::nFees
Amount nFees
Definition: miner.h:147
Amount
Definition: amount.h:19
BlockAssembler::chainParams
const CChainParams & chainParams
Definition: miner.h:154
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:430
CompareTxIterByAncestorCount
Definition: miner.h:92
modtxscoreiter
indexed_modified_transaction_set::index< ancestor_score >::type::iterator modtxscoreiter
Definition: miner.h:118
CTxMemPoolModifiedEntry::GetVirtualSizeWithAncestors
uint64_t GetVirtualSizeWithAncestors() const
Definition: miner.cpp:50
CBlock
Definition: block.h:55
BlockAssembler::Options
Definition: miner.h:159
CBlockTemplateEntry::fees
Amount fees
Definition: miner.h:32
BlockAssembler::CreateNewBlock
std::unique_ptr< CBlockTemplate > CreateNewBlock(const CScript &scriptPubKeyIn)
Construct a new block template with coinbase to scriptPubKeyIn.
Definition: miner.cpp:121
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:316
update_for_parent_inclusion::iter
CTxMemPool::txiter iter
Definition: miner.h:129
CBlockTemplate
Definition: miner.h:39
EXCLUSIVE_LOCKS_REQUIRED
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
BlockAssembler::addPackageTxs
void addPackageTxs(int &nPackagesSelected, int &nDescendantsUpdated) EXCLUSIVE_LOCKS_REQUIRED(m_mempool.cs)
Add transactions based on feerate including unconfirmed ancestors.
Definition: miner.cpp:380
BlockAssembler::nMedianTimePast
int64_t nMedianTimePast
Definition: miner.h:153
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:508
CTransactionRef
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:319
CTxMemPoolModifiedEntry
Definition: miner.h:47
BlockAssembler::GetMaxGeneratedBlockSize
uint64_t GetMaxGeneratedBlockSize() const
Definition: miner.h:174
update_for_parent_inclusion::update_for_parent_inclusion
update_for_parent_inclusion(CTxMemPool::txiter it)
Definition: miner.h:121
CBlockTemplateEntry::CBlockTemplateEntry
CBlockTemplateEntry(CTransactionRef _tx, Amount _fees, int64_t _sigOpCount)
Definition: miner.h:35
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:354
CTxMemPoolModifiedEntry::nSizeWithAncestors
uint64_t nSizeWithAncestors
Definition: miner.h:64
BlockAssembler::nMaxGeneratedBlockSize
uint64_t nMaxGeneratedBlockSize
Definition: miner.h:139
BlockAssembler::pblocktemplate
std::unique_ptr< CBlockTemplate > pblocktemplate
Definition: miner.h:136
CTxMemPoolModifiedEntry::GetModifiedFee
Amount GetModifiedFee() const
Definition: miner.h:55
BlockAssembler::nBlockSigOps
uint64_t nBlockSigOps
Definition: miner.h:146
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:23
CBlockTemplate::entries
std::vector< CBlockTemplateEntry > entries
Definition: miner.h:42
CBlockTemplate::block
CBlock block
Definition: miner.h:40
BlockAssembler::inBlock
CTxMemPool::setEntries inBlock
Definition: miner.h:148
CTxMemPoolModifiedEntry::GetTxVirtualSize
size_t GetTxVirtualSize() const
Definition: miner.h:60
BlockAssembler::BlockAssembler
BlockAssembler(const Config &config, const CTxMemPool &mempool)
Definition: miner.cpp:101