Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

miner.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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 CConnman;
20 class CScript;
21 
22 namespace Consensus { struct Params; };
23 
24 static const bool DEFAULT_PRINTPRIORITY = false;
25 
27 {
29  std::vector<CAmount> vTxFees;
30  std::vector<int64_t> vTxSigOps;
31  uint32_t nPrevBits; // nBits of previous block (for subsidy calculation)
32  std::vector<CTxOut> voutMasternodePayments; // masternode payment
33  std::vector<CTxOut> voutSuperblockPayments; // superblock payment
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  nSigOpCountWithAncestors = entry->GetSigOpCountWithAncestors();
45  }
46 
47  int64_t GetModifiedFee() const { return iter->GetModifiedFee(); }
48  uint64_t GetSizeWithAncestors() const { return nSizeWithAncestors; }
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 
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 
116  {
117  e.nModFeesWithAncestors -= iter->GetFee();
118  e.nSizeWithAncestors -= iter->GetTxSize();
119  e.nSigOpCountWithAncestors -= iter->GetSigOpCount();
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
135  unsigned int nBlockMaxSize;
137 
138  // Information on the current status of the block
139  uint64_t nBlockSize;
140  uint64_t nBlockTx;
141  unsigned int nBlockSigOps;
144 
145  // Chain context for the block
146  int nHeight;
149 
150 public:
151  struct Options {
152  Options();
155  };
156 
157  explicit BlockAssembler(const CChainParams& params);
158  BlockAssembler(const CChainParams& params, const Options& options);
159 
161  std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn);
162 
163 private:
164  // utility functions
166  void resetBlock();
168  void AddToBlock(CTxMemPool::txiter iter);
169 
170  // Methods for how to add transactions to a block.
174  void addPackageTxs(int &nPackagesSelected, int &nDescendantsUpdated);
175 
176  // helper functions for addPackageTxs()
180  bool TestPackage(uint64_t packageSize, unsigned int packageSigOps) const;
185  bool TestPackageTransactions(const CTxMemPool::setEntries& package);
190  void SortForBlock(const CTxMemPool::setEntries& package, CTxMemPool::txiter entry, std::vector<CTxMemPool::txiter>& sortedEntries);
194  int UpdatePackagesForAdded(const CTxMemPool::setEntries& alreadyAdded, indexed_modified_transaction_set &mapModifiedTx);
195 };
196 
198 void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
199 int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
200 
201 #endif // BITCOIN_MINER_H
void operator()(CTxMemPoolModifiedEntry &e)
Definition: miner.h:115
const CTransaction & GetTx() const
Definition: miner.h:51
indexed_modified_transaction_set::nth_index< 0 >::type::iterator modtxiter
Definition: miner.h:108
bool TestPackageTransactions(const CTxMemPool::setEntries &package)
Perform checks on each transaction in a package: locktime These checks should always succeed...
Definition: miner.cpp:276
uint64_t nBlockSize
Definition: miner.h:139
Comparator for CTxMemPool::txiter objects.
Definition: miner.h:64
CFeeRate blockMinFeeRate
Definition: miner.h:154
Definition: miner.h:38
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:349
void AddToBlock(CTxMemPool::txiter iter)
Add a tx to the block.
Definition: miner.cpp:288
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:140
void onlyUnconfirmed(CTxMemPool::setEntries &testSet)
Remove confirmed (inBlock) entries from given set.
Definition: miner.cpp:251
int64_t nLockTimeCutoff
Definition: miner.h:147
CTxMemPool::setEntries inBlock
Definition: miner.h:143
std::unique_ptr< CBlockTemplate > pblocktemplate
Definition: miner.h:130
CBlock * pblock
Definition: miner.h:132
std::set< txiter, CompareIteratorByHash > setEntries
Definition: txmempool.h:499
CChainParams defines various tweakable parameters of a given instance of the Dash system...
Definition: chainparams.h:41
size_t GetTxSize() const
Definition: miner.h:50
unsigned int nBlockMaxSize
Definition: miner.h:135
std::vector< CTxOut > voutMasternodePayments
Definition: miner.h:32
uint32_t nPrevBits
Definition: miner.h:31
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
CTxMemPool::txiter result_type
Definition: miner.h:72
Definition: txmempool.h:281
const CChainParams & chainparams
Definition: miner.h:148
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:142
update_for_parent_inclusion(CTxMemPool::txiter it)
Definition: miner.h:113
BlockAssembler(const CChainParams &params)
Definition: miner.cpp:104
Generate a new block, without valid proof-of-work.
Definition: miner.h:126
Definition: net.h:136
std::unique_ptr< CBlockTemplate > CreateNewBlock(const CScript &scriptPubKeyIn)
Construct a new block template with coinbase to scriptPubKeyIn.
Definition: miner.cpp:119
std::vector< CAmount > vTxFees
Definition: miner.h:29
Parameters that influence chain consensus.
Definition: params.h:130
CTxMemPool::txiter iter
Definition: miner.h:122
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:307
unsigned int nBlockSigOps
Definition: miner.h:141
int64_t UpdateTime(CBlockHeader *pblock, const Consensus::Params &consensusParams, const CBlockIndex *pindexPrev)
Definition: miner.cpp:59
indexed_transaction_set::nth_index< 0 >::type::iterator txiter
Definition: txmempool.h:491
uint64_t nSizeWithAncestors
Definition: miner.h:54
uint64_t GetSizeWithAncestors() const
Definition: miner.h:48
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&#39;s cache...
Definition: miner.cpp:343
CAmount nModFeesWithAncestors
Definition: miner.h:55
std::vector< CTxOut > voutSuperblockPayments
Definition: miner.h:33
CFeeRate blockMinFeeRate
Definition: miner.h:136
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:170
const CChainParams & Params()
Return the currently selected parameters.
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:389
std::vector< int64_t > vTxSigOps
Definition: miner.h:30
int nHeight
Definition: miner.h:146
int64_t GetModifiedFee() const
Definition: miner.h:47
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:19
unsigned int nSigOpCountWithAncestors
Definition: miner.h:56
CAmount GetModFeesWithAncestors() const
Definition: miner.h:49
void resetBlock()
Clear the block&#39;s state and prepare for assembling a new block.
Definition: miner.cpp:106
CTxMemPoolModifiedEntry(CTxMemPool::txiter entry)
Definition: miner.h:39
void addPackageTxs(int &nPackagesSelected, int &nDescendantsUpdated)
Add transactions based on feerate including unconfirmed ancestors Increments nPackagesSelected / nDes...
Definition: miner.cpp:370
bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const
Definition: miner.h:83
CBlock block
Definition: miner.h:28
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:499
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:198
static const bool DEFAULT_PRINTPRIORITY
Definition: miner.h:24
result_type operator()(const CTxMemPoolModifiedEntry &entry) const
Definition: miner.h:73
bool TestPackage(uint64_t packageSize, unsigned int packageSigOps) const
Test if a new package would "fit" in the block.
Definition: miner.cpp:264
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:20
Released under the MIT license