Bitcoin Core  0.19.99
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 <optional.h>
10 #include <primitives/block.h>
11 #include <txmempool.h>
12 #include <validation.h>
13 
14 #include <memory>
15 #include <stdint.h>
16 
17 #include <boost/multi_index_container.hpp>
18 #include <boost/multi_index/ordered_index.hpp>
19 
20 class CBlockIndex;
21 class CChainParams;
22 class CScript;
23 
24 namespace Consensus { struct Params; };
25 
26 static const bool DEFAULT_PRINTPRIORITY = false;
27 
29 {
31  std::vector<CAmount> vTxFees;
32  std::vector<int64_t> vTxSigOpsCost;
33  std::vector<unsigned char> vchCoinbaseCommitment;
34 };
35 
36 // Container for tracking updates to ancestor feerate as we include (parent)
37 // transactions in a block
40  {
41  iter = entry;
42  nSizeWithAncestors = entry->GetSizeWithAncestors();
43  nModFeesWithAncestors = entry->GetModFeesWithAncestors();
44  nSigOpCostWithAncestors = entry->GetSigOpCostWithAncestors();
45  }
46 
47  int64_t GetModifiedFee() const { return iter->GetModifiedFee(); }
48  uint64_t GetSizeWithAncestors() const { return nSizeWithAncestors; }
49  CAmount GetModFeesWithAncestors() const { return nModFeesWithAncestors; }
50  size_t GetTxSize() const { return iter->GetTxSize(); }
51  const CTransaction& GetTx() const { return iter->GetTx(); }
52 
57 };
58 
65  bool operator()(const CTxMemPool::txiter& a, const CTxMemPool::txiter& b) const
66  {
67  return &(*a) < &(*b);
68  }
69 };
70 
73  result_type operator() (const CTxMemPoolModifiedEntry &entry) const
74  {
75  return entry.iter;
76  }
77 };
78 
79 // A comparator that sorts transactions based on number of ancestors.
80 // This is sufficient to sort an ancestor package in an order that is valid
81 // to appear in a block.
83  bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const
84  {
85  if (a->GetCountWithAncestors() != b->GetCountWithAncestors())
86  return a->GetCountWithAncestors() < b->GetCountWithAncestors();
87  return CTxMemPool::CompareIteratorByHash()(a, b);
88  }
89 };
90 
91 typedef boost::multi_index_container<
93  boost::multi_index::indexed_by<
94  boost::multi_index::ordered_unique<
97  >,
98  // sorted by modified ancestor fee rate
99  boost::multi_index::ordered_non_unique<
100  // Reuse same tag from CTxMemPool's similar index
101  boost::multi_index::tag<ancestor_score>,
102  boost::multi_index::identity<CTxMemPoolModifiedEntry>,
104  >
105  >
107 
108 typedef indexed_modified_transaction_set::nth_index<0>::type::iterator modtxiter;
109 typedef indexed_modified_transaction_set::index<ancestor_score>::type::iterator modtxscoreiter;
110 
112 {
114 
115  void operator() (CTxMemPoolModifiedEntry &e)
116  {
117  e.nModFeesWithAncestors -= iter->GetFee();
118  e.nSizeWithAncestors -= iter->GetTxSize();
119  e.nSigOpCostWithAncestors -= iter->GetSigOpCost();
120  }
121 
123 };
124 
127 {
128 private:
129  // The constructed block template
130  std::unique_ptr<CBlockTemplate> pblocktemplate;
131  // A convenience pointer that always refers to the CBlock in pblocktemplate
133 
134  // Configuration parameters for the block size
136  unsigned int nBlockMaxWeight;
138 
139  // Information on the current status of the block
140  uint64_t nBlockWeight;
141  uint64_t nBlockTx;
145 
146  // Chain context for the block
147  int nHeight;
151 
152 public:
153  struct Options {
154  Options();
157  };
158 
159  explicit BlockAssembler(const CTxMemPool& mempool, const CChainParams& params);
160  explicit BlockAssembler(const CTxMemPool& mempool, const CChainParams& params, const Options& options);
161 
163  std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn);
164 
167 
168 private:
169  // utility functions
171  void resetBlock();
173  void AddToBlock(CTxMemPool::txiter iter);
174 
175  // Methods for how to add transactions to a block.
179  void addPackageTxs(int& nPackagesSelected, int& nDescendantsUpdated) EXCLUSIVE_LOCKS_REQUIRED(m_mempool.cs);
180 
181  // helper functions for addPackageTxs()
183  void onlyUnconfirmed(CTxMemPool::setEntries& testSet);
185  bool TestPackage(uint64_t packageSize, int64_t packageSigOpsCost) const;
190  bool TestPackageTransactions(const CTxMemPool::setEntries& package);
193  bool SkipMapTxEntry(CTxMemPool::txiter it, indexed_modified_transaction_set& mapModifiedTx, CTxMemPool::setEntries& failedTx) EXCLUSIVE_LOCKS_REQUIRED(m_mempool.cs);
195  void SortForBlock(const CTxMemPool::setEntries& package, std::vector<CTxMemPool::txiter>& sortedEntries);
199  int UpdatePackagesForAdded(const CTxMemPool::setEntries& alreadyAdded, indexed_modified_transaction_set& mapModifiedTx) EXCLUSIVE_LOCKS_REQUIRED(m_mempool.cs);
200 };
201 
203 void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
204 int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
205 
206 #endif // BITCOIN_MINER_H
const CTransaction & GetTx() const
Definition: miner.h:51
indexed_modified_transaction_set::nth_index< 0 >::type::iterator modtxiter
Definition: miner.h:108
CTxMemPool mempool
Comparator for CTxMemPool::txiter objects.
Definition: miner.h:64
CFeeRate blockMinFeeRate
Definition: miner.h:156
static Optional< int64_t > m_last_block_num_txs
Definition: miner.h:165
Definition: miner.h:38
unsigned int nBlockMaxWeight
Definition: miner.h:136
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
Definition: txmempool.h:523
Definition: block.h:72
bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const
Definition: miner.h:65
uint64_t nBlockTx
Definition: miner.h:141
int64_t nLockTimeCutoff
Definition: miner.h:148
CTxMemPool::setEntries inBlock
Definition: miner.h:144
std::unique_ptr< CBlockTemplate > pblocktemplate
Definition: miner.h:130
CBlock * pblock
Definition: miner.h:132
std::set< txiter, CompareIteratorByHash > setEntries
Definition: txmempool.h:531
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system...
Definition: chainparams.h:47
size_t GetTxSize() const
Definition: miner.h:50
Transaction validation functions.
Definition: params.h:12
indexed_modified_transaction_set::index< ancestor_score >::type::iterator modtxscoreiter
Definition: miner.h:109
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
std::vector< int64_t > vTxSigOpsCost
Definition: miner.h:32
CTxMemPool::txiter result_type
Definition: miner.h:72
Definition: txmempool.h:277
const CChainParams & chainparams
Definition: miner.h:149
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:106
CAmount nFees
Definition: miner.h:143
update_for_parent_inclusion(CTxMemPool::txiter it)
Definition: miner.h:113
size_t nBlockMaxWeight
Definition: miner.h:155
Generate a new block, without valid proof-of-work.
Definition: miner.h:126
std::vector< CAmount > vTxFees
Definition: miner.h:31
Parameters that influence chain consensus.
Definition: params.h:45
static Optional< int64_t > m_last_block_weight
Definition: miner.h:166
CTxMemPool::txiter iter
Definition: miner.h:122
int64_t UpdateTime(CBlockHeader *pblock, const Consensus::Params &consensusParams, const CBlockIndex *pindexPrev)
Definition: miner.cpp:27
const CTxMemPool & m_mempool
Definition: miner.h:150
uint64_t nSizeWithAncestors
Definition: miner.h:54
uint64_t GetSizeWithAncestors() const
Definition: miner.h:48
uint64_t nBlockSigOpsCost
Definition: miner.h:142
CAmount nModFeesWithAncestors
Definition: miner.h:55
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:51
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:443
CFeeRate blockMinFeeRate
Definition: miner.h:137
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:137
const CChainParams & Params()
Return the currently selected parameters.
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:390
int nHeight
Definition: miner.h:147
int64_t GetModifiedFee() const
Definition: miner.h:47
int64_t nSigOpCostWithAncestors
Definition: miner.h:56
uint64_t nBlockWeight
Definition: miner.h:140
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:19
bool fIncludeWitness
Definition: miner.h:135
CAmount GetModFeesWithAncestors() const
Definition: miner.h:49
CTxMemPoolModifiedEntry(CTxMemPool::txiter entry)
Definition: miner.h:39
bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const
Definition: miner.h:83
CBlock block
Definition: miner.h:30
CTxMemPool::txiter iter
Definition: miner.h:53
void IncrementExtraNonce(CBlock *pblock, const CBlockIndex *pindexPrev, unsigned int &nExtraNonce)
Modify the extranonce in a block.
Definition: miner.cpp:427
std::vector< unsigned char > vchCoinbaseCommitment
Definition: miner.h:33
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:270
boost::optional< T > Optional
Substitute for C++17 std::optional.
Definition: optional.h:14
static const bool DEFAULT_PRINTPRIORITY
Definition: miner.h:26
auto it
Definition: validation.cpp:361
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:20
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it...
Definition: txmempool.h:520