Bitcoin ABC  0.26.3
P2P Digital Currency
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
9 #include <consensus/amount.h>
10 #include <primitives/block.h>
11 #include <txmempool.h>
13 #include <boost/multi_index/ordered_index.hpp>
14 #include <boost/multi_index_container.hpp>
16 #include <cstdint>
17 #include <memory>
18 #include <optional>
20 class CBlockIndex;
21 class CChainParams;
22 class Config;
23 class CScript;
25 namespace Consensus {
26 struct Params;
27 }
29 static const bool DEFAULT_PRINTPRIORITY = false;
34  int64_t sigOpCount;
36  CBlockTemplateEntry(CTransactionRef _tx, Amount _fees, int64_t _sigOpCount)
37  : tx(_tx), fees(_fees), sigOpCount(_sigOpCount){};
38 };
43  std::vector<CBlockTemplateEntry> entries;
44 };
46 // Container for tracking updates to ancestor feerate as we include (parent)
47 // transactions in a block
50  iter = entry;
51  nSizeWithAncestors = entry->GetSizeWithAncestors();
52  nModFeesWithAncestors = entry->GetModFeesWithAncestors();
53  nSigOpCountWithAncestors = entry->GetSigOpCountWithAncestors();
54  }
56  Amount GetModifiedFee() const { return iter->GetModifiedFee(); }
57  uint64_t GetSizeWithAncestors() const { return nSizeWithAncestors; }
58  uint64_t GetVirtualSizeWithAncestors() const;
60  size_t GetTxSize() const { return iter->GetTxSize(); }
61  size_t GetTxVirtualSize() const { return iter->GetTxVirtualSize(); }
62  const CTransaction &GetTx() const { return iter->GetTx(); }
68 };
78  const CTxMemPool::txiter &b) const {
79  return &(*a) < &(*b);
80  }
81 };
86  return entry.iter;
87  }
88 };
90 // A comparator that sorts transactions based on number of ancestors.
91 // This is sufficient to sort an ancestor package in an order that is valid
92 // to appear in a block.
95  const CTxMemPool::txiter &b) const {
96  if (a->GetCountWithAncestors() != b->GetCountWithAncestors()) {
97  return a->GetCountWithAncestors() < b->GetCountWithAncestors();
98  }
99  return CompareIteratorById()(a, b);
100  }
101 };
103 typedef boost::multi_index_container<
105  boost::multi_index::indexed_by<
106  boost::multi_index::ordered_unique<modifiedentry_iter,
108  // sorted by modified ancestor fee rate
109  boost::multi_index::ordered_non_unique<
110  // Reuse same tag from CTxMemPool's similar index
111  boost::multi_index::tag<ancestor_score>,
112  boost::multi_index::identity<CTxMemPoolModifiedEntry>,
116 typedef indexed_modified_transaction_set::nth_index<0>::type::iterator
118 typedef indexed_modified_transaction_set::index<ancestor_score>::type::iterator
125  e.nModFeesWithAncestors -= iter->GetFee();
126  e.nSizeWithAncestors -= iter->GetTxSize();
127  e.nSigOpCountWithAncestors -= iter->GetSigOpCount();
128  }
131 };
135 private:
136  // The constructed block template
137  std::unique_ptr<CBlockTemplate> pblocktemplate;
139  // Configuration parameters for the block size
144  // Information on the current status of the block
145  uint64_t nBlockSize;
146  uint64_t nBlockTx;
147  uint64_t nBlockSigOps;
151  // Chain context for the block
152  int nHeight;
160 public:
161  struct Options {
162  Options();
166  };
168  BlockAssembler(const Config &config, CChainState &chainstate,
169  const CTxMemPool &mempool);
170  BlockAssembler(CChainState &chainstate, const CChainParams &params,
171  const CTxMemPool &mempool, const Options &options);
174  std::unique_ptr<CBlockTemplate>
175  CreateNewBlock(const CScript &scriptPubKeyIn);
179  static std::optional<int64_t> m_last_block_num_txs;
180  static std::optional<int64_t> m_last_block_size;
182 private:
183  // utility functions
185  void resetBlock();
187  void AddToBlock(CTxMemPool::txiter iter);
189  // Methods for how to add transactions to a block.
195  void addPackageTxs(int &nPackagesSelected, int &nDescendantsUpdated)
198  // helper functions for addPackageTxs()
202  bool TestPackage(uint64_t packageSize, int64_t packageSigOpCount) const;
209  bool TestPackageTransactions(const CTxMemPool::setEntries &package) const;
215  indexed_modified_transaction_set &mapModifiedTx,
216  CTxMemPool::setEntries &failedTx)
219  void SortForBlock(const CTxMemPool::setEntries &package,
220  std::vector<CTxMemPool::txiter> &sortedEntries);
226  int UpdatePackagesForAdded(const CTxMemPool::setEntries &alreadyAdded,
227  indexed_modified_transaction_set &mapModifiedTx)
229 };
232 void IncrementExtraNonce(CBlock *pblock, const CBlockIndex *pindexPrev,
233  uint64_t nExcessiveBlockSize,
234  unsigned int &nExtraNonce);
235 int64_t UpdateTime(CBlockHeader *pblock, const CChainParams &chainParams,
236  const CBlockIndex *pindexPrev);
237 #endif // BITCOIN_MINER_H
const CChainParams & Params()
Return the currently selected parameters.
Generate a new block, without valid proof-of-work.
Definition: miner.h:134
int64_t nLockTimeCutoff
Definition: miner.h:153
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:320
bool TestPackage(uint64_t packageSize, int64_t packageSigOpCount) const
Test if a new package would "fit" in the block.
Definition: miner.cpp:260
uint64_t GetMaxGeneratedBlockSize() const
Definition: miner.h:177
void AddToBlock(CTxMemPool::txiter iter)
Add a tx to the block.
Definition: miner.cpp:301
CTxMemPool::setEntries inBlock
Definition: miner.h:149
int64_t nMedianTimePast
Definition: miner.h:154
uint64_t nMaxGeneratedBlockSize
Definition: miner.h:140
CChainState & m_chainstate
Definition: miner.h:158
void addPackageTxs(int &nPackagesSelected, int &nDescendantsUpdated) EXCLUSIVE_LOCKS_REQUIRED(m_mempool.cs)
Add transactions based on feerate including unconfirmed ancestors.
Definition: miner.cpp:384
bool TestPackageTransactions(const CTxMemPool::setEntries &package) const
Perform checks on each transaction in a package: locktime, serialized size (if necessary).
Definition: miner.cpp:279
void onlyUnconfirmed(CTxMemPool::setEntries &testSet)
Remove confirmed (inBlock) entries from given set.
Definition: miner.cpp:248
uint64_t nMaxGeneratedBlockSigChecks
Definition: miner.h:141
CFeeRate blockMinFeeRate
Definition: miner.h:142
uint64_t nBlockTx
Definition: miner.h:146
uint64_t nBlockSigOps
Definition: miner.h:147
uint64_t nBlockSize
Definition: miner.h:145
Amount nFees
Definition: miner.h:148
static std::optional< int64_t > m_last_block_size
Definition: miner.h:180
const CChainParams & chainParams
Definition: miner.h:155
BlockAssembler(const Config &config, CChainState &chainstate, const CTxMemPool &mempool)
Definition: miner.cpp:102
const CTxMemPool & m_mempool
Definition: miner.h:157
void resetBlock()
Clear the block's state and prepare for assembling a new block.
Definition: miner.cpp:107
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:365
std::unique_ptr< CBlockTemplate > pblocktemplate
Definition: miner.h:137
std::unique_ptr< CBlockTemplate > CreateNewBlock(const CScript &scriptPubKeyIn)
Construct a new block template with coinbase to scriptPubKeyIn.
Definition: miner.cpp:123
static std::optional< int64_t > m_last_block_num_txs
Definition: miner.h:179
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:358
int nHeight
Definition: miner.h:152
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:55
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:23
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:74
CChainState stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:662
Fee rate in satoshis per kilobyte: Amount / kB.
Definition: feerate.h:21
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:431
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:194
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:442
std::set< txiter, CompareIteratorById > setEntries
Definition: txmempool.h:532
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:525
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
Definition: txmempool.h:528
Definition: txmempool.h:287
Definition: config.h:17
indexed_modified_transaction_set::nth_index< 0 >::type::iterator modtxiter
Definition: miner.h:117
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:114
int64_t UpdateTime(CBlockHeader *pblock, const CChainParams &chainParams, const CBlockIndex *pindexPrev)
Definition: miner.cpp:32
Definition: miner.h:29
void IncrementExtraNonce(CBlock *pblock, const CBlockIndex *pindexPrev, uint64_t nExcessiveBlockSize, unsigned int &nExtraNonce)
Modify the extranonce in a block.
Definition: miner.cpp:550
indexed_modified_transaction_set::index< ancestor_score >::type::iterator modtxscoreiter
Definition: miner.h:119
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:319
Definition: amount.h:19
CFeeRate blockMinFeeRate
Definition: miner.h:165
uint64_t nMaxGeneratedBlockSize
Definition: miner.h:164
uint64_t nExcessiveBlockSize
Definition: miner.h:163
Definition: miner.h:31
int64_t sigOpCount
Definition: miner.h:34
CBlockTemplateEntry(CTransactionRef _tx, Amount _fees, int64_t _sigOpCount)
Definition: miner.h:36
CTransactionRef tx
Definition: miner.h:32
Amount fees
Definition: miner.h:33
CBlock block
Definition: miner.h:41
std::vector< CBlockTemplateEntry > entries
Definition: miner.h:43
Definition: miner.h:48
Amount GetModifiedFee() const
Definition: miner.h:56
size_t GetTxVirtualSize() const
Definition: miner.h:61
const CTransaction & GetTx() const
Definition: miner.h:62
size_t GetTxSize() const
Definition: miner.h:60
Amount GetModFeesWithAncestors() const
Definition: miner.h:59
CTxMemPoolModifiedEntry(CTxMemPool::txiter entry)
Definition: miner.h:49
Amount nModFeesWithAncestors
Definition: miner.h:66
uint64_t nSizeWithAncestors
Definition: miner.h:65
CTxMemPool::txiter iter
Definition: miner.h:64
uint64_t GetVirtualSizeWithAncestors() const
Definition: miner.cpp:50
int64_t nSigOpCountWithAncestors
Definition: miner.h:67
uint64_t GetSizeWithAncestors() const
Definition: miner.h:57
Comparator for CTxMemPool::txiter objects.
Definition: miner.h:76
bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const
Definition: miner.h:77
bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const
Definition: miner.h:94
result_type operator()(const CTxMemPoolModifiedEntry &entry) const
Definition: miner.h:85
CTxMemPool::txiter result_type
Definition: miner.h:84
update_for_parent_inclusion(CTxMemPool::txiter it)
Definition: miner.h:122
CTxMemPool::txiter iter
Definition: miner.h:130
void operator()(CTxMemPoolModifiedEntry &e)
Definition: miner.h:124
Definition: threadsafety.h:56