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