BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
miner.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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 #include <validation.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 // current local hashes/sec
23 extern uint64_t nHashesPerSec;
24 
25 namespace Consensus { struct Params; };
26 
27 static const bool DEFAULT_PRINTPRIORITY = false;
28 
30 {
32  std::vector<CAmount> vTxFees;
33  std::vector<int64_t> vTxSigOpsCost;
34  std::vector<unsigned char> vchCoinbaseCommitment;
35 };
36 
37 // Container for tracking updates to ancestor feerate as we include (parent)
38 // transactions in a block
41  {
42  iter = entry;
43  nSizeWithAncestors = entry->GetSizeWithAncestors();
44  nModFeesWithAncestors = entry->GetModFeesWithAncestors();
45  nSigOpCostWithAncestors = entry->GetSigOpCostWithAncestors();
46  }
47 
48  int64_t GetModifiedFee() const { return iter->GetModifiedFee(); }
49  uint64_t GetSizeWithAncestors() const { return nSizeWithAncestors; }
51  size_t GetTxSize() const { return iter->GetTxSize(); }
52  const CTransaction& GetTx() const { return iter->GetTx(); }
53 
58 };
59 
66  bool operator()(const CTxMemPool::txiter& a, const CTxMemPool::txiter& b) const
67  {
68  return &(*a) < &(*b);
69  }
70 };
71 
75  {
76  return entry.iter;
77  }
78 };
79 
80 // A comparator that sorts transactions based on number of ancestors.
81 // This is sufficient to sort an ancestor package in an order that is valid
82 // to appear in a block.
84  bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const
85  {
86  if (a->GetCountWithAncestors() != b->GetCountWithAncestors())
87  return a->GetCountWithAncestors() < b->GetCountWithAncestors();
88  return CTxMemPool::CompareIteratorByHash()(a, b);
89  }
90 };
91 
92 typedef boost::multi_index_container<
94  boost::multi_index::indexed_by<
95  boost::multi_index::ordered_unique<
98  >,
99  // sorted by modified ancestor fee rate
100  boost::multi_index::ordered_non_unique<
101  // Reuse same tag from CTxMemPool's similar index
102  boost::multi_index::tag<ancestor_score>,
103  boost::multi_index::identity<CTxMemPoolModifiedEntry>,
105  >
106  >
108 
109 typedef indexed_modified_transaction_set::nth_index<0>::type::iterator modtxiter;
110 typedef indexed_modified_transaction_set::index<ancestor_score>::type::iterator modtxscoreiter;
111 
113 {
115 
117  {
118  e.nModFeesWithAncestors -= iter->GetFee();
119  e.nSizeWithAncestors -= iter->GetTxSize();
120  e.nSigOpCostWithAncestors -= iter->GetSigOpCost();
121  }
122 
124 };
125 
128 {
129 private:
130  // The constructed block template
131  std::unique_ptr<CBlockTemplate> pblocktemplate;
132  // A convenience pointer that always refers to the CBlock in pblocktemplate
134 
135  // Configuration parameters for the block size
137  unsigned int nBlockMaxWeight;
139 
140  // Information on the current status of the block
141  uint64_t nBlockWeight;
142  uint64_t nBlockTx;
146 
147  // Chain context for the block
148  int nHeight;
151 
152 public:
153  struct Options {
154  Options();
157  };
158 
159  explicit BlockAssembler(const CChainParams& params);
160  BlockAssembler(const CChainParams& params, const Options& options);
161 
163  std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn, bool fMineWitnessTx=true);
164 
165 private:
166  // utility functions
168  void resetBlock();
170  void AddToBlock(CTxMemPool::txiter iter);
171 
172  // Methods for how to add transactions to a block.
176  void addPackageTxs(int &nPackagesSelected, int &nDescendantsUpdated) EXCLUSIVE_LOCKS_REQUIRED(mempool.cs);
177 
178  // helper functions for addPackageTxs()
182  bool TestPackage(uint64_t packageSize, int64_t packageSigOpsCost) const;
187  bool TestPackageTransactions(const CTxMemPool::setEntries& package);
192  void SortForBlock(const CTxMemPool::setEntries& package, std::vector<CTxMemPool::txiter>& sortedEntries);
197 };
198 
200 void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
201 int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
202 
203 int GenerateBSHA3s(bool fGenerate, int nThreads, const CChainParams& chainparams);
204 
205 #endif // BITCOIN_MINER_H
void operator()(CTxMemPoolModifiedEntry &e)
Definition: miner.h:116
const CTransaction & GetTx() const
Definition: miner.h:52
indexed_modified_transaction_set::nth_index< 0 >::type::iterator modtxiter
Definition: miner.h:109
bool TestPackageTransactions(const CTxMemPool::setEntries &package)
Perform checks on each transaction in a package: locktime, premature-witness, serialized size (if nec...
Definition: miner.cpp:220
CTxMemPool mempool
Comparator for CTxMemPool::txiter objects.
Definition: miner.h:65
CFeeRate blockMinFeeRate
Definition: miner.h:156
Definition: miner.h:39
unsigned int nBlockMaxWeight
Definition: miner.h:137
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
Definition: txmempool.h:490
void AddToBlock(CTxMemPool::txiter iter)
Add a tx to the block.
Definition: miner.cpp:231
Definition: block.h:74
bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const
Definition: miner.h:66
uint64_t nBlockTx
Definition: miner.h:142
int GenerateBSHA3s(bool fGenerate, int nThreads, const CChainParams &chainparams)
Definition: miner.cpp:648
void onlyUnconfirmed(CTxMemPool::setEntries &testSet)
Remove confirmed (inBlock) entries from given set.
Definition: miner.cpp:193
int64_t nLockTimeCutoff
Definition: miner.h:149
CTxMemPool::setEntries inBlock
Definition: miner.h:145
std::unique_ptr< CBlockTemplate > pblocktemplate
Definition: miner.h:131
CBlock * pblock
Definition: miner.h:133
std::set< txiter, CompareIteratorByHash > setEntries
Definition: txmempool.h:498
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system...
Definition: chainparams.h:47
size_t GetTxSize() const
Definition: miner.h:51
indexed_modified_transaction_set::index< ancestor_score >::type::iterator modtxscoreiter
Definition: miner.h:110
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
std::vector< int64_t > vTxSigOpsCost
Definition: miner.h:33
CTxMemPool::txiter result_type
Definition: miner.h:73
Definition: txmempool.h:277
uint64_t nHashesPerSec
Definition: miner.cpp:45
const CChainParams & chainparams
Definition: miner.h:150
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:107
CAmount nFees
Definition: miner.h:144
update_for_parent_inclusion(CTxMemPool::txiter it)
Definition: miner.h:114
size_t nBlockMaxWeight
Definition: miner.h:155
BlockAssembler(const CChainParams &params)
Definition: miner.cpp:90
Generate a new block, without valid proof-of-work.
Definition: miner.h:127
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:292
std::vector< CAmount > vTxFees
Definition: miner.h:32
Parameters that influence chain consensus.
Definition: params.h:40
CTxMemPool::txiter iter
Definition: miner.h:123
int64_t UpdateTime(CBlockHeader *pblock, const Consensus::Params &consensusParams, const CBlockIndex *pindexPrev)
Definition: miner.cpp:47
void addPackageTxs(int &nPackagesSelected, int &nDescendantsUpdated) EXCLUSIVE_LOCKS_REQUIRED(mempool.cs)
Add transactions based on feerate including unconfirmed ancestors Increments nPackagesSelected / nDes...
Definition: miner.cpp:313
CCriticalSection cs
Definition: txmempool.h:487
uint64_t nSizeWithAncestors
Definition: miner.h:55
uint64_t GetSizeWithAncestors() const
Definition: miner.h:49
uint64_t nBlockSigOpsCost
Definition: miner.h:143
CAmount nModFeesWithAncestors
Definition: miner.h:56
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:51
CFeeRate blockMinFeeRate
Definition: miner.h:138
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:384
int nHeight
Definition: miner.h:148
int64_t GetModifiedFee() const
Definition: miner.h:48
bool SkipMapTxEntry(CTxMemPool::txiter it, indexed_modified_transaction_set &mapModifiedTx, CTxMemPool::setEntries &failedTx) EXCLUSIVE_LOCKS_REQUIRED(mempool.cs)
Return true if given transaction from mapTx has already been evaluated, or if the transaction&#39;s cache...
Definition: miner.cpp:286
int64_t nSigOpCostWithAncestors
Definition: miner.h:57
uint64_t nBlockWeight
Definition: miner.h:141
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:19
bool fIncludeWitness
Definition: miner.h:136
CAmount GetModFeesWithAncestors() const
Definition: miner.h:50
void resetBlock()
Clear the block&#39;s state and prepare for assembling a new block.
Definition: miner.cpp:92
CTxMemPoolModifiedEntry(CTxMemPool::txiter entry)
Definition: miner.h:40
bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const
Definition: miner.h:84
CBlock block
Definition: miner.h:31
CTxMemPool::txiter iter
Definition: miner.h:54
void IncrementExtraNonce(CBlock *pblock, const CBlockIndex *pindexPrev, unsigned int &nExtraNonce)
Modify the extranonce in a block.
Definition: miner.cpp:443
std::vector< unsigned char > vchCoinbaseCommitment
Definition: miner.h:34
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:264
int UpdatePackagesForAdded(const CTxMemPool::setEntries &alreadyAdded, indexed_modified_transaction_set &mapModifiedTx) EXCLUSIVE_LOCKS_REQUIRED(mempool.cs)
Add descendants of given transactions to mapModifiedTx with ancestor state updated assuming given tra...
Definition: miner.cpp:250
bool TestPackage(uint64_t packageSize, int64_t packageSigOpsCost) const
Test if a new package would "fit" in the block.
Definition: miner.cpp:206
result_type operator()(const CTxMemPoolModifiedEntry &entry) const
Definition: miner.h:74
std::unique_ptr< CBlockTemplate > CreateNewBlock(const CScript &scriptPubKeyIn, bool fMineWitnessTx=true)
Construct a new block template with coinbase to scriptPubKeyIn.
Definition: miner.cpp:106
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:20