Dogecoin Core  1.14.2
P2P Digital Currency
miner.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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 <stdint.h>
13 #include <memory>
14 #include "boost/multi_index_container.hpp"
15 #include "boost/multi_index/ordered_index.hpp"
16 
17 class CBlockIndex;
18 class CChainParams;
19 class CReserveKey;
20 class CScript;
21 class CWallet;
22 
23 namespace Consensus { struct Params; };
24 
25 static const bool DEFAULT_PRINTPRIORITY = false;
26 
28 {
30  std::vector<CAmount> vTxFees;
31  std::vector<int64_t> vTxSigOpsCost;
32  std::vector<unsigned char> vchCoinbaseCommitment;
33 };
34 
35 // Container for tracking updates to ancestor feerate as we include (parent)
36 // transactions in a block
39  {
40  iter = entry;
41  nSizeWithAncestors = entry->GetSizeWithAncestors();
42  nModFeesWithAncestors = entry->GetModFeesWithAncestors();
43  nSigOpCostWithAncestors = entry->GetSigOpCostWithAncestors();
44  }
45 
50 };
51 
58  bool operator()(const CTxMemPool::txiter& a, const CTxMemPool::txiter& b) const
59  {
60  return &(*a) < &(*b);
61  }
62 };
63 
67  {
68  return entry.iter;
69  }
70 };
71 
72 // This matches the calculation in CompareTxMemPoolEntryByAncestorFee,
73 // except operating on CTxMemPoolModifiedEntry.
74 // TODO: refactor to avoid duplication of this logic.
77  {
78  double f1 = (double)a.nModFeesWithAncestors * b.nSizeWithAncestors;
79  double f2 = (double)b.nModFeesWithAncestors * a.nSizeWithAncestors;
80  if (f1 == f2) {
82  }
83  return f1 > f2;
84  }
85 };
86 
87 // A comparator that sorts transactions based on number of ancestors.
88 // This is sufficient to sort an ancestor package in an order that is valid
89 // to appear in a block.
91  bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const
92  {
93  if (a->GetCountWithAncestors() != b->GetCountWithAncestors())
94  return a->GetCountWithAncestors() < b->GetCountWithAncestors();
95  return CTxMemPool::CompareIteratorByHash()(a, b);
96  }
97 };
98 
99 typedef boost::multi_index_container<
101  boost::multi_index::indexed_by<
102  boost::multi_index::ordered_unique<
105  >,
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>,
112  >
113  >
115 
116 typedef indexed_modified_transaction_set::nth_index<0>::type::iterator modtxiter;
117 typedef indexed_modified_transaction_set::index<ancestor_score>::type::iterator modtxscoreiter;
118 
120 {
122 
124  {
125  e.nModFeesWithAncestors -= iter->GetFee();
126  e.nSizeWithAncestors -= iter->GetTxSize();
127  e.nSigOpCostWithAncestors -= iter->GetSigOpCost();
128  }
129 
131 };
132 
135 {
136 private:
137  // The constructed block template
138  std::unique_ptr<CBlockTemplate> pblocktemplate;
139  // A convenience pointer that always refers to the CBlock in pblocktemplate
141 
142  // Configuration parameters for the block size
147 
148  // Information on the current status of the block
149  uint64_t nBlockWeight;
150  uint64_t nBlockSize;
151  uint64_t nBlockTx;
155 
156  // Chain context for the block
157  int nHeight;
160 
161  // Variables used for addPriorityTxs
164 
165 public:
168  std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn, bool fMineWitnessTx);
169 
170 private:
171  // utility functions
173  void resetBlock();
175  void AddToBlock(CTxMemPool::txiter iter);
176 
177  // Methods for how to add transactions to a block.
179  void addPriorityTxs();
183  void addPackageTxs(int &nPackagesSelected, int &nDescendantsUpdated);
184 
185  // helper function for addPriorityTxs
187  bool TestForBlock(CTxMemPool::txiter iter);
190 
191  // helper functions for addPackageTxs()
195  bool TestPackage(uint64_t packageSize, int64_t packageSigOpsCost);
200  bool TestPackageTransactions(const CTxMemPool::setEntries& package);
205  void SortForBlock(const CTxMemPool::setEntries& package, CTxMemPool::txiter entry, std::vector<CTxMemPool::txiter>& sortedEntries);
209  int UpdatePackagesForAdded(const CTxMemPool::setEntries& alreadyAdded, indexed_modified_transaction_set &mapModifiedTx);
210 };
211 
213 void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
214 int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
215 
216 #endif // BITCOIN_MINER_H
217 
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:15
const CChainParams & Params()
Return the currently selected parameters.
Generate a new block, without valid proof-of-work.
Definition: miner.h:135
int64_t nLockTimeCutoff
Definition: miner.h:158
bool TestForBlock(CTxMemPool::txiter iter)
Test if tx will still "fit" in the block.
Definition: miner.cpp:281
uint64_t nBlockWeight
Definition: miner.h:149
void AddToBlock(CTxMemPool::txiter iter)
Add a tx to the block.
Definition: miner.cpp:333
CTxMemPool::setEntries inBlock
Definition: miner.h:154
bool isStillDependent(CTxMemPool::txiter iter)
Test if tx still has unconfirmed parents not yet in block.
Definition: miner.cpp:223
bool TestPackage(uint64_t packageSize, int64_t packageSigOpsCost)
Test if a new package would "fit" in the block.
Definition: miner.cpp:247
int lastFewTxs
Definition: miner.h:162
unsigned int nBlockMaxSize
Definition: miner.h:144
void SortForBlock(const CTxMemPool::setEntries &package, CTxMemPool::txiter entry, std::vector< CTxMemPool::txiter > &sortedEntries)
Sort the package in an order that is valid to appear in a block.
Definition: miner.cpp:403
void onlyUnconfirmed(CTxMemPool::setEntries &testSet)
Remove confirmed (inBlock) entries from given set.
Definition: miner.cpp:234
CFeeRate blockMinFeeRate
Definition: miner.h:146
bool TestPackageTransactions(const CTxMemPool::setEntries &package)
Perform checks on each transaction in a package: locktime, premature-witness, serialized size (if nec...
Definition: miner.cpp:262
uint64_t nBlockTx
Definition: miner.h:151
std::unique_ptr< CBlockTemplate > CreateNewBlock(const CScript &scriptPubKeyIn, bool fMineWitnessTx)
Construct a new block template with coinbase to scriptPubKeyIn.
Definition: miner.cpp:131
uint64_t nBlockSize
Definition: miner.h:150
BlockAssembler(const CChainParams &chainparams)
Definition: miner.cpp:76
bool fNeedSizeAccounting
Definition: miner.h:145
bool fIncludeWitness
Definition: miner.h:143
CAmount nFees
Definition: miner.h:153
CBlock * pblock
Definition: miner.h:140
void resetBlock()
Clear the block's state and prepare for assembling a new block.
Definition: miner.cpp:113
std::unique_ptr< CBlockTemplate > pblocktemplate
Definition: miner.h:138
const CChainParams & chainparams
Definition: miner.h:159
bool blockFinished
Definition: miner.h:163
unsigned int nBlockMaxWeight
Definition: miner.h:144
bool SkipMapTxEntry(CTxMemPool::txiter it, indexed_modified_transaction_set &mapModifiedTx, CTxMemPool::setEntries &failedTx)
Return true if given transaction from mapTx has already been evaluated, or if the transaction's cache...
Definition: miner.cpp:395
void addPriorityTxs()
Add transactions based on tx "priority".
Definition: miner.cpp:554
int nHeight
Definition: miner.h:157
void addPackageTxs(int &nPackagesSelected, int &nDescendantsUpdated)
Add transactions based on feerate including unconfirmed ancestors Increments nPackagesSelected / nDes...
Definition: miner.cpp:424
int UpdatePackagesForAdded(const CTxMemPool::setEntries &alreadyAdded, indexed_modified_transaction_set &mapModifiedTx)
Add descendants of given transactions to mapModifiedTx with ancestor state updated assuming given tra...
Definition: miner.cpp:359
uint64_t nBlockSigOpsCost
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:25
Definition: block.h:67
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:158
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:47
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: amount.h:38
A key allocated from the key pool.
Definition: wallet.h:928
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:377
std::set< txiter, CompareIteratorByHash > setEntries
Definition: txmempool.h:494
indexed_transaction_set::nth_index< 0 >::type::iterator txiter
Definition: txmempool.h:486
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances,...
Definition: wallet.h:493
indexed_modified_transaction_set::nth_index< 0 >::type::iterator modtxiter
Definition: miner.h:116
void IncrementExtraNonce(CBlock *pblock, const CBlockIndex *pindexPrev, unsigned int &nExtraNonce)
Modify the extranonce in a block.
Definition: miner.cpp:636
indexed_modified_transaction_set::index< ancestor_score >::type::iterator modtxscoreiter
Definition: miner.h:117
int64_t UpdateTime(CBlockHeader *pblock, const Consensus::Params &consensusParams, const CBlockIndex *pindexPrev)
Definition: miner.cpp:61
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 >, CompareModifiedEntry > >> indexed_modified_transaction_set
Definition: miner.h:114
CBlock block
Definition: miner.h:29
std::vector< CAmount > vTxFees
Definition: miner.h:30
std::vector< int64_t > vTxSigOpsCost
Definition: miner.h:31
std::vector< unsigned char > vchCoinbaseCommitment
Definition: miner.h:32
Definition: miner.h:37
CTxMemPoolModifiedEntry(CTxMemPool::txiter entry)
Definition: miner.h:38
uint64_t nSizeWithAncestors
Definition: miner.h:47
CAmount nModFeesWithAncestors
Definition: miner.h:48
CTxMemPool::txiter iter
Definition: miner.h:46
int64_t nSigOpCostWithAncestors
Definition: miner.h:49
Comparator for CTxMemPool::txiter objects.
Definition: miner.h:57
bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const
Definition: miner.h:58
Definition: miner.h:75
bool operator()(const CTxMemPoolModifiedEntry &a, const CTxMemPoolModifiedEntry &b) const
Definition: miner.h:76
bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const
Definition: miner.h:91
Parameters that influence chain consensus.
Definition: params.h:39
result_type operator()(const CTxMemPoolModifiedEntry &entry) const
Definition: miner.h:66
CTxMemPool::txiter result_type
Definition: miner.h:65
update_for_parent_inclusion(CTxMemPool::txiter it)
Definition: miner.h:121
CTxMemPool::txiter iter
Definition: miner.h:130
void operator()(CTxMemPoolModifiedEntry &e)
Definition: miner.h:123