You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

miner.h 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2016 The Starwels developers
  3. // Distributed under the MIT software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #ifndef STARWELS_MINER_H
  6. #define STARWELS_MINER_H
  7. #include "primitives/block.h"
  8. #include "txmempool.h"
  9. #include <stdint.h>
  10. #include <memory>
  11. #include "boost/multi_index_container.hpp"
  12. #include "boost/multi_index/ordered_index.hpp"
  13. class CBlockIndex;
  14. class CChainParams;
  15. class CScript;
  16. namespace Consensus { struct Params; };
  17. static const bool DEFAULT_PRINTPRIORITY = false;
  18. struct CBlockTemplate
  19. {
  20. CBlock block;
  21. std::vector<CAmount> vTxFees;
  22. std::vector<int64_t> vTxSigOpsCost;
  23. std::vector<unsigned char> vchCoinbaseCommitment;
  24. };
  25. // Container for tracking updates to ancestor feerate as we include (parent)
  26. // transactions in a block
  27. struct CTxMemPoolModifiedEntry {
  28. CTxMemPoolModifiedEntry(CTxMemPool::txiter entry)
  29. {
  30. iter = entry;
  31. nSizeWithAncestors = entry->GetSizeWithAncestors();
  32. nModFeesWithAncestors = entry->GetModFeesWithAncestors();
  33. nSigOpCostWithAncestors = entry->GetSigOpCostWithAncestors();
  34. }
  35. CTxMemPool::txiter iter;
  36. uint64_t nSizeWithAncestors;
  37. CAmount nModFeesWithAncestors;
  38. int64_t nSigOpCostWithAncestors;
  39. };
  40. /** Comparator for CTxMemPool::txiter objects.
  41. * It simply compares the internal memory address of the CTxMemPoolEntry object
  42. * pointed to. This means it has no meaning, and is only useful for using them
  43. * as key in other indexes.
  44. */
  45. struct CompareCTxMemPoolIter {
  46. bool operator()(const CTxMemPool::txiter& a, const CTxMemPool::txiter& b) const
  47. {
  48. return &(*a) < &(*b);
  49. }
  50. };
  51. struct modifiedentry_iter {
  52. typedef CTxMemPool::txiter result_type;
  53. result_type operator() (const CTxMemPoolModifiedEntry &entry) const
  54. {
  55. return entry.iter;
  56. }
  57. };
  58. // This matches the calculation in CompareTxMemPoolEntryByAncestorFee,
  59. // except operating on CTxMemPoolModifiedEntry.
  60. // TODO: refactor to avoid duplication of this logic.
  61. struct CompareModifiedEntry {
  62. bool operator()(const CTxMemPoolModifiedEntry &a, const CTxMemPoolModifiedEntry &b) const
  63. {
  64. double f1 = (double)a.nModFeesWithAncestors * b.nSizeWithAncestors;
  65. double f2 = (double)b.nModFeesWithAncestors * a.nSizeWithAncestors;
  66. if (f1 == f2) {
  67. return CTxMemPool::CompareIteratorByHash()(a.iter, b.iter);
  68. }
  69. return f1 > f2;
  70. }
  71. };
  72. // A comparator that sorts transactions based on number of ancestors.
  73. // This is sufficient to sort an ancestor package in an order that is valid
  74. // to appear in a block.
  75. struct CompareTxIterByAncestorCount {
  76. bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const
  77. {
  78. if (a->GetCountWithAncestors() != b->GetCountWithAncestors())
  79. return a->GetCountWithAncestors() < b->GetCountWithAncestors();
  80. return CTxMemPool::CompareIteratorByHash()(a, b);
  81. }
  82. };
  83. typedef boost::multi_index_container<
  84. CTxMemPoolModifiedEntry,
  85. boost::multi_index::indexed_by<
  86. boost::multi_index::ordered_unique<
  87. modifiedentry_iter,
  88. CompareCTxMemPoolIter
  89. >,
  90. // sorted by modified ancestor fee rate
  91. boost::multi_index::ordered_non_unique<
  92. // Reuse same tag from CTxMemPool's similar index
  93. boost::multi_index::tag<ancestor_score>,
  94. boost::multi_index::identity<CTxMemPoolModifiedEntry>,
  95. CompareModifiedEntry
  96. >
  97. >
  98. > indexed_modified_transaction_set;
  99. typedef indexed_modified_transaction_set::nth_index<0>::type::iterator modtxiter;
  100. typedef indexed_modified_transaction_set::index<ancestor_score>::type::iterator modtxscoreiter;
  101. struct update_for_parent_inclusion
  102. {
  103. update_for_parent_inclusion(CTxMemPool::txiter it) : iter(it) {}
  104. void operator() (CTxMemPoolModifiedEntry &e)
  105. {
  106. e.nModFeesWithAncestors -= iter->GetFee();
  107. e.nSizeWithAncestors -= iter->GetTxSize();
  108. e.nSigOpCostWithAncestors -= iter->GetSigOpCost();
  109. }
  110. CTxMemPool::txiter iter;
  111. };
  112. /** Generate a new block, without valid proof-of-work */
  113. class BlockAssembler
  114. {
  115. private:
  116. // The constructed block template
  117. std::unique_ptr<CBlockTemplate> pblocktemplate;
  118. // A convenience pointer that always refers to the CBlock in pblocktemplate
  119. CBlock* pblock;
  120. // Configuration parameters for the block size
  121. bool fIncludeWitness;
  122. unsigned int nBlockMaxWeight;
  123. CFeeRate blockMinFeeRate;
  124. // Information on the current status of the block
  125. uint64_t nBlockWeight;
  126. uint64_t nBlockTx;
  127. uint64_t nBlockSigOpsCost;
  128. CAmount nFees;
  129. CTxMemPool::setEntries inBlock;
  130. // Chain context for the block
  131. int nHeight;
  132. int64_t nLockTimeCutoff;
  133. const CChainParams& chainparams;
  134. public:
  135. struct Options {
  136. Options();
  137. size_t nBlockMaxWeight;
  138. size_t nBlockMaxSize;
  139. CFeeRate blockMinFeeRate;
  140. };
  141. BlockAssembler(const CChainParams& params);
  142. BlockAssembler(const CChainParams& params, const Options& options);
  143. /** Construct a new block template with coinbase to scriptPubKeyIn */
  144. std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn, bool fMineWitnessTx=true);
  145. private:
  146. // utility functions
  147. /** Clear the block's state and prepare for assembling a new block */
  148. void resetBlock();
  149. /** Add a tx to the block */
  150. void AddToBlock(CTxMemPool::txiter iter);
  151. // Methods for how to add transactions to a block.
  152. /** Add transactions based on feerate including unconfirmed ancestors
  153. * Increments nPackagesSelected / nDescendantsUpdated with corresponding
  154. * statistics from the package selection (for logging statistics). */
  155. void addPackageTxs(int &nPackagesSelected, int &nDescendantsUpdated);
  156. // helper functions for addPackageTxs()
  157. /** Remove confirmed (inBlock) entries from given set */
  158. void onlyUnconfirmed(CTxMemPool::setEntries& testSet);
  159. /** Test if a new package would "fit" in the block */
  160. bool TestPackage(uint64_t packageSize, int64_t packageSigOpsCost);
  161. /** Perform checks on each transaction in a package:
  162. * locktime, premature-witness, serialized size (if necessary)
  163. * These checks should always succeed, and they're here
  164. * only as an extra check in case of suboptimal node configuration */
  165. bool TestPackageTransactions(const CTxMemPool::setEntries& package);
  166. /** Return true if given transaction from mapTx has already been evaluated,
  167. * or if the transaction's cached data in mapTx is incorrect. */
  168. bool SkipMapTxEntry(CTxMemPool::txiter it, indexed_modified_transaction_set &mapModifiedTx, CTxMemPool::setEntries &failedTx);
  169. /** Sort the package in an order that is valid to appear in a block */
  170. void SortForBlock(const CTxMemPool::setEntries& package, CTxMemPool::txiter entry, std::vector<CTxMemPool::txiter>& sortedEntries);
  171. /** Add descendants of given transactions to mapModifiedTx with ancestor
  172. * state updated assuming given transactions are inBlock. Returns number
  173. * of updated descendants. */
  174. int UpdatePackagesForAdded(const CTxMemPool::setEntries& alreadyAdded, indexed_modified_transaction_set &mapModifiedTx);
  175. };
  176. /** Modify the extranonce in a block */
  177. void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
  178. int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
  179. #endif // STARWELS_MINER_H