Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto 2 : // Copyright (c) 2009-2020 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 <optional.h> 10 : #include <primitives/block.h> 11 : #include <txmempool.h> 12 : #include <validation.h> 13 : 14 : #include <memory> 15 : #include <stdint.h> 16 : 17 : #include <boost/multi_index_container.hpp> 18 : #include <boost/multi_index/ordered_index.hpp> 19 : 20 : class CBlockIndex; 21 : class CChainParams; 22 : class CScript; 23 : 24 : namespace Consensus { struct Params; }; 25 : 26 : static const bool DEFAULT_PRINTPRIORITY = false; 27 : 28 84044 : struct CBlockTemplate 29 : { 30 : CBlock block; 31 : std::vector<CAmount> vTxFees; 32 : std::vector<int64_t> vTxSigOpsCost; 33 : std::vector<unsigned char> vchCoinbaseCommitment; 34 : }; 35 : 36 : // Container for tracking updates to ancestor feerate as we include (parent) 37 : // transactions in a block 38 : struct CTxMemPoolModifiedEntry { 39 5208 : explicit CTxMemPoolModifiedEntry(CTxMemPool::txiter entry) 40 2604 : { 41 2604 : iter = entry; 42 2604 : nSizeWithAncestors = entry->GetSizeWithAncestors(); 43 2604 : nModFeesWithAncestors = entry->GetModFeesWithAncestors(); 44 2604 : nSigOpCostWithAncestors = entry->GetSigOpCostWithAncestors(); 45 5208 : } 46 : 47 6355850 : int64_t GetModifiedFee() const { return iter->GetModifiedFee(); } 48 3178084 : uint64_t GetSizeWithAncestors() const { return nSizeWithAncestors; } 49 3178084 : CAmount GetModFeesWithAncestors() const { return nModFeesWithAncestors; } 50 6355850 : size_t GetTxSize() const { return iter->GetTxSize(); } 51 3174260 : const CTransaction& GetTx() const { return iter->GetTx(); } 52 : 53 : CTxMemPool::txiter iter; 54 : uint64_t nSizeWithAncestors; 55 : CAmount nModFeesWithAncestors; 56 : int64_t nSigOpCostWithAncestors; 57 : }; 58 : 59 : /** Comparator for CTxMemPool::txiter objects. 60 : * It simply compares the internal memory address of the CTxMemPoolEntry object 61 : * pointed to. This means it has no meaning, and is only useful for using them 62 : * as key in other indexes. 63 : */ 64 : struct CompareCTxMemPoolIter { 65 9860082 : bool operator()(const CTxMemPool::txiter& a, const CTxMemPool::txiter& b) const 66 : { 67 9860082 : return &(*a) < &(*b); 68 : } 69 : }; 70 : 71 : struct modifiedentry_iter { 72 : typedef CTxMemPool::txiter result_type; 73 11419548 : result_type operator() (const CTxMemPoolModifiedEntry &entry) const 74 : { 75 11419548 : 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. 82 : struct CompareTxIterByAncestorCount { 83 20269 : bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b) const 84 : { 85 20269 : if (a->GetCountWithAncestors() != b->GetCountWithAncestors()) 86 20163 : return a->GetCountWithAncestors() < b->GetCountWithAncestors(); 87 106 : return CompareIteratorByHash()(a, b); 88 20269 : } 89 : }; 90 : 91 : typedef boost::multi_index_container< 92 : CTxMemPoolModifiedEntry, 93 : boost::multi_index::indexed_by< 94 : boost::multi_index::ordered_unique< 95 : modifiedentry_iter, 96 : CompareCTxMemPoolIter 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>, 103 : CompareTxMemPoolEntryByAncestorFee 104 : > 105 : > 106 : > indexed_modified_transaction_set; 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 : 111 : struct update_for_parent_inclusion 112 : { 113 1561352 : explicit update_for_parent_inclusion(CTxMemPool::txiter it) : iter(it) {} 114 : 115 780676 : void operator() (CTxMemPoolModifiedEntry &e) 116 : { 117 780676 : e.nModFeesWithAncestors -= iter->GetFee(); 118 780676 : e.nSizeWithAncestors -= iter->GetTxSize(); 119 780676 : e.nSigOpCostWithAncestors -= iter->GetSigOpCost(); 120 780676 : } 121 : 122 : CTxMemPool::txiter iter; 123 : }; 124 : 125 : /** Generate a new block, without valid proof-of-work */ 126 42022 : class BlockAssembler 127 : { 128 : private: 129 : // The constructed block template 130 : std::unique_ptr<CBlockTemplate> pblocktemplate; 131 : 132 : // Configuration parameters for the block size 133 : bool fIncludeWitness; 134 : unsigned int nBlockMaxWeight; 135 : CFeeRate blockMinFeeRate; 136 : 137 : // Information on the current status of the block 138 : uint64_t nBlockWeight; 139 : uint64_t nBlockTx; 140 : uint64_t nBlockSigOpsCost; 141 : CAmount nFees; 142 : CTxMemPool::setEntries inBlock; 143 : 144 : // Chain context for the block 145 : int nHeight; 146 : int64_t nLockTimeCutoff; 147 : const CChainParams& chainparams; 148 : const CTxMemPool& m_mempool; 149 : 150 : public: 151 : struct Options { 152 : Options(); 153 : size_t nBlockMaxWeight; 154 : CFeeRate blockMinFeeRate; 155 : }; 156 : 157 : explicit BlockAssembler(const CTxMemPool& mempool, const CChainParams& params); 158 : explicit BlockAssembler(const CTxMemPool& mempool, const CChainParams& params, const Options& options); 159 : 160 : /** Construct a new block template with coinbase to scriptPubKeyIn */ 161 : std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn); 162 : 163 : static Optional<int64_t> m_last_block_num_txs; 164 : static Optional<int64_t> m_last_block_weight; 165 : 166 : private: 167 : // utility functions 168 : /** Clear the block's state and prepare for assembling a new block */ 169 : void resetBlock(); 170 : /** Add a tx to the block */ 171 : void AddToBlock(CTxMemPool::txiter iter); 172 : 173 : // Methods for how to add transactions to a block. 174 : /** Add transactions based on feerate including unconfirmed ancestors 175 : * Increments nPackagesSelected / nDescendantsUpdated with corresponding 176 : * statistics from the package selection (for logging statistics). */ 177 : void addPackageTxs(int& nPackagesSelected, int& nDescendantsUpdated) EXCLUSIVE_LOCKS_REQUIRED(m_mempool.cs); 178 : 179 : // helper functions for addPackageTxs() 180 : /** Remove confirmed (inBlock) entries from given set */ 181 : void onlyUnconfirmed(CTxMemPool::setEntries& testSet); 182 : /** Test if a new package would "fit" in the block */ 183 : bool TestPackage(uint64_t packageSize, int64_t packageSigOpsCost) const; 184 : /** Perform checks on each transaction in a package: 185 : * locktime, premature-witness, serialized size (if necessary) 186 : * These checks should always succeed, and they're here 187 : * only as an extra check in case of suboptimal node configuration */ 188 : bool TestPackageTransactions(const CTxMemPool::setEntries& package); 189 : /** Return true if given transaction from mapTx has already been evaluated, 190 : * or if the transaction's cached data in mapTx is incorrect. */ 191 : bool SkipMapTxEntry(CTxMemPool::txiter it, indexed_modified_transaction_set& mapModifiedTx, CTxMemPool::setEntries& failedTx) EXCLUSIVE_LOCKS_REQUIRED(m_mempool.cs); 192 : /** Sort the package in an order that is valid to appear in a block */ 193 : void SortForBlock(const CTxMemPool::setEntries& package, std::vector<CTxMemPool::txiter>& sortedEntries); 194 : /** Add descendants of given transactions to mapModifiedTx with ancestor 195 : * state updated assuming given transactions are inBlock. Returns number 196 : * of updated descendants. */ 197 : int UpdatePackagesForAdded(const CTxMemPool::setEntries& alreadyAdded, indexed_modified_transaction_set& mapModifiedTx) EXCLUSIVE_LOCKS_REQUIRED(m_mempool.cs); 198 : }; 199 : 200 : /** Modify the extranonce in a block */ 201 : void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce); 202 : int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev); 203 : 204 : /** Update an old GenerateCoinbaseCommitment from CreateNewBlock after the block txs have changed */ 205 : void RegenerateCommitments(CBlock& block); 206 : 207 : #endif // BITCOIN_MINER_H