Raven Core  3.0.0
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 // Copyright (c) 2017-2019 The Raven Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef RAVEN_MINER_H
8 #define RAVEN_MINER_H
9 
10 #include "primitives/block.h"
11 #include "txmempool.h"
12 
13 #include <stdint.h>
14 #include <memory>
15 #include <boost/multi_index_container.hpp>
16 #include <boost/multi_index/ordered_index.hpp>
17 
18 class CBlockIndex;
19 class CChainParams;
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> vTxSigOpsCost;
31  std::vector<unsigned char> vchCoinbaseCommitment;
32 };
33 
34 // Container for tracking updates to ancestor feerate as we include (parent)
35 // transactions in a block
38  {
39  iter = entry;
40  nSizeWithAncestors = entry->GetSizeWithAncestors();
41  nModFeesWithAncestors = entry->GetModFeesWithAncestors();
42  nSigOpCostWithAncestors = entry->GetSigOpCostWithAncestors();
43  }
44 
49 };
50 
57  bool operator()(const CTxMemPool::txiter& a, const CTxMemPool::txiter& b) const
58  {
59  return &(*a) < &(*b);
60  }
61 };
62 
65  result_type operator() (const CTxMemPoolModifiedEntry &entry) const
66  {
67  return entry.iter;
68  }
69 };
70 
71 // This matches the calculation in CompareTxMemPoolEntryByAncestorFee,
72 // except operating on CTxMemPoolModifiedEntry.
73 // TODO: refactor to avoid duplication of this logic.
76  {
77  double f1 = (double)a.nModFeesWithAncestors * b.nSizeWithAncestors;
78  double f2 = (double)b.nModFeesWithAncestors * a.nSizeWithAncestors;
79  if (f1 == f2) {
81  }
82  return f1 > f2;
83  }
84 };
85 
86 // A comparator that sorts transactions based on number of ancestors.
87 // This is sufficient to sort an ancestor package in an order that is valid
88 // to appear in a block.
90  bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const
91  {
92  if (a->GetCountWithAncestors() != b->GetCountWithAncestors())
93  return a->GetCountWithAncestors() < b->GetCountWithAncestors();
94  return CTxMemPool::CompareIteratorByHash()(a, b);
95  }
96 };
97 
98 typedef boost::multi_index_container<
100  boost::multi_index::indexed_by<
101  boost::multi_index::ordered_unique<
104  >,
105  // sorted by modified ancestor fee rate
106  boost::multi_index::ordered_non_unique<
107  // Reuse same tag from CTxMemPool's similar index
108  boost::multi_index::tag<ancestor_score>,
109  boost::multi_index::identity<CTxMemPoolModifiedEntry>,
111  >
112  >
114 
115 typedef indexed_modified_transaction_set::nth_index<0>::type::iterator modtxiter;
116 typedef indexed_modified_transaction_set::index<ancestor_score>::type::iterator modtxscoreiter;
117 
119 {
121 
122  void operator() (CTxMemPoolModifiedEntry &e)
123  {
124  e.nModFeesWithAncestors -= iter->GetFee();
125  e.nSizeWithAncestors -= iter->GetTxSize();
126  e.nSigOpCostWithAncestors -= iter->GetSigOpCost();
127  }
128 
130 };
131 
134 {
135 private:
136  // The constructed block template
137  std::unique_ptr<CBlockTemplate> pblocktemplate;
138  // A convenience pointer that always refers to the CBlock in pblocktemplate
140 
141  // Configuration parameters for the block size
143  unsigned int nBlockMaxWeight;
145 
146  // Information on the current status of the block
147  uint64_t nBlockWeight;
148  uint64_t nBlockTx;
152 
153  // Chain context for the block
154  int nHeight;
157 
158 public:
159  struct Options {
160  Options();
163  };
164 
165  explicit BlockAssembler(const CChainParams& params);
166  BlockAssembler(const CChainParams& params, const Options& options);
167 
169  std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn, bool fMineWitnessTx=true);
170 
171 private:
172  // utility functions
174  void resetBlock();
176  void AddToBlock(CTxMemPool::txiter iter);
177 
178  // Methods for how to add transactions to a block.
182  void addPackageTxs(int &nPackagesSelected, int &nDescendantsUpdated);
183 
184  // helper functions for addPackageTxs()
186  void onlyUnconfirmed(CTxMemPool::setEntries& testSet);
188  bool TestPackage(uint64_t packageSize, int64_t packageSigOpsCost) const;
193  bool TestPackageTransactions(const CTxMemPool::setEntries& package);
196  bool SkipMapTxEntry(CTxMemPool::txiter it, indexed_modified_transaction_set &mapModifiedTx, CTxMemPool::setEntries &failedTx);
198  void SortForBlock(const CTxMemPool::setEntries& package, CTxMemPool::txiter entry, std::vector<CTxMemPool::txiter>& sortedEntries);
202  int UpdatePackagesForAdded(const CTxMemPool::setEntries& alreadyAdded, indexed_modified_transaction_set &mapModifiedTx);
203 };
204 
206 void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
207 int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
208 
209 int GenerateRavens(bool fGenerate, int nThreads, const CChainParams& chainparams);
210 #endif // RAVEN_MINER_H
indexed_modified_transaction_set::nth_index< 0 >::type::iterator modtxiter
Definition: miner.h:115
Comparator for CTxMemPool::txiter objects.
Definition: miner.h:56
CFeeRate blockMinFeeRate
Definition: miner.h:162
Definition: miner.h:36
unsigned int nBlockMaxWeight
Definition: miner.h:143
Definition: block.h:73
bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const
Definition: miner.h:57
uint64_t nBlockTx
Definition: miner.h:148
int64_t nLockTimeCutoff
Definition: miner.h:155
CTxMemPool::setEntries inBlock
Definition: miner.h:151
std::unique_ptr< CBlockTemplate > pblocktemplate
Definition: miner.h:137
CBlock * pblock
Definition: miner.h:139
std::set< txiter, CompareIteratorByHash > setEntries
Definition: txmempool.h:499
CChainParams defines various tweakable parameters of a given instance of the Raven system...
Definition: chainparams.h:48
bool operator()(const CTxMemPoolModifiedEntry &a, const CTxMemPoolModifiedEntry &b) const
Definition: miner.h:75
indexed_modified_transaction_set::index< ancestor_score >::type::iterator modtxscoreiter
Definition: miner.h:116
int64_t CAmount
Amount in corbies (Can be negative)
Definition: amount.h:13
std::vector< int64_t > vTxSigOpsCost
Definition: miner.h:30
CTxMemPool::txiter result_type
Definition: miner.h:64
const CChainParams & chainparams
Definition: miner.h:156
int GenerateRavens(bool fGenerate, int nThreads, const CChainParams &chainparams)
Definition: miner.cpp:668
CAmount nFees
Definition: miner.h:150
update_for_parent_inclusion(CTxMemPool::txiter it)
Definition: miner.h:120
size_t nBlockMaxWeight
Definition: miner.h:161
Generate a new block, without valid proof-of-work.
Definition: miner.h:133
std::vector< CAmount > vTxFees
Definition: miner.h:29
Parameters that influence chain consensus.
Definition: params.h:47
CTxMemPool::txiter iter
Definition: miner.h:129
int64_t UpdateTime(CBlockHeader *pblock, const Consensus::Params &consensusParams, const CBlockIndex *pindexPrev)
Definition: miner.cpp:60
indexed_transaction_set::nth_index< 0 >::type::iterator txiter
Definition: txmempool.h:491
uint64_t nSizeWithAncestors
Definition: miner.h:46
uint64_t nBlockSigOpsCost
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 >, CompareModifiedEntry > >> indexed_modified_transaction_set
Definition: miner.h:113
CAmount nModFeesWithAncestors
Definition: miner.h:47
CFeeRate blockMinFeeRate
Definition: miner.h:144
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:172
const CChainParams & Params()
Return the currently selected parameters.
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:396
int nHeight
Definition: miner.h:154
int64_t nSigOpCostWithAncestors
Definition: miner.h:48
uint64_t nBlockWeight
Definition: miner.h:147
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:20
bool fIncludeWitness
Definition: miner.h:142
CTxMemPoolModifiedEntry(CTxMemPool::txiter entry)
Definition: miner.h:37
bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const
Definition: miner.h:90
CBlock block
Definition: miner.h:28
CTxMemPool::txiter iter
Definition: miner.h:45
void IncrementExtraNonce(CBlock *pblock, const CBlockIndex *pindexPrev, unsigned int &nExtraNonce)
Modify the extranonce in a block.
Definition: miner.cpp:455
std::vector< unsigned char > vchCoinbaseCommitment
Definition: miner.h:31
Definition: miner.h:74
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:21