Merge #10432: [Trivial] Add BITCOIN_FS_H endif footer in fs.h
[bitcoinplatinum.git] / src / miner.h
blob1f3c9d652f175a4e3a7109438b530499871e40ba
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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.
6 #ifndef BITCOIN_MINER_H
7 #define BITCOIN_MINER_H
9 #include "primitives/block.h"
10 #include "txmempool.h"
12 #include <stdint.h>
13 #include <memory>
14 #include "boost/multi_index_container.hpp"
15 #include "boost/multi_index/ordered_index.hpp"
17 class CBlockIndex;
18 class CChainParams;
19 class CReserveKey;
20 class CScript;
21 class CWallet;
23 namespace Consensus { struct Params; };
25 static const bool DEFAULT_PRINTPRIORITY = false;
27 struct CBlockTemplate
29 CBlock block;
30 std::vector<CAmount> vTxFees;
31 std::vector<int64_t> vTxSigOpsCost;
32 std::vector<unsigned char> vchCoinbaseCommitment;
35 // Container for tracking updates to ancestor feerate as we include (parent)
36 // transactions in a block
37 struct CTxMemPoolModifiedEntry {
38 CTxMemPoolModifiedEntry(CTxMemPool::txiter entry)
40 iter = entry;
41 nSizeWithAncestors = entry->GetSizeWithAncestors();
42 nModFeesWithAncestors = entry->GetModFeesWithAncestors();
43 nSigOpCostWithAncestors = entry->GetSigOpCostWithAncestors();
46 CTxMemPool::txiter iter;
47 uint64_t nSizeWithAncestors;
48 CAmount nModFeesWithAncestors;
49 int64_t nSigOpCostWithAncestors;
52 /** Comparator for CTxMemPool::txiter objects.
53 * It simply compares the internal memory address of the CTxMemPoolEntry object
54 * pointed to. This means it has no meaning, and is only useful for using them
55 * as key in other indexes.
57 struct CompareCTxMemPoolIter {
58 bool operator()(const CTxMemPool::txiter& a, const CTxMemPool::txiter& b) const
60 return &(*a) < &(*b);
64 struct modifiedentry_iter {
65 typedef CTxMemPool::txiter result_type;
66 result_type operator() (const CTxMemPoolModifiedEntry &entry) const
68 return entry.iter;
72 // This matches the calculation in CompareTxMemPoolEntryByAncestorFee,
73 // except operating on CTxMemPoolModifiedEntry.
74 // TODO: refactor to avoid duplication of this logic.
75 struct CompareModifiedEntry {
76 bool operator()(const CTxMemPoolModifiedEntry &a, const CTxMemPoolModifiedEntry &b)
78 double f1 = (double)a.nModFeesWithAncestors * b.nSizeWithAncestors;
79 double f2 = (double)b.nModFeesWithAncestors * a.nSizeWithAncestors;
80 if (f1 == f2) {
81 return CTxMemPool::CompareIteratorByHash()(a.iter, b.iter);
83 return f1 > f2;
87 // A comparator that sorts transactions based on number of ancestors.
88 // This is sufficient to sort an ancestor package in an order that is valid
89 // to appear in a block.
90 struct CompareTxIterByAncestorCount {
91 bool operator()(const CTxMemPool::txiter &a, const CTxMemPool::txiter &b)
93 if (a->GetCountWithAncestors() != b->GetCountWithAncestors())
94 return a->GetCountWithAncestors() < b->GetCountWithAncestors();
95 return CTxMemPool::CompareIteratorByHash()(a, b);
99 typedef boost::multi_index_container<
100 CTxMemPoolModifiedEntry,
101 boost::multi_index::indexed_by<
102 boost::multi_index::ordered_unique<
103 modifiedentry_iter,
104 CompareCTxMemPoolIter
106 // sorted by modified ancestor fee rate
107 boost::multi_index::ordered_non_unique<
108 // Reuse same tag from CTxMemPool's similar index
109 boost::multi_index::tag<ancestor_score>,
110 boost::multi_index::identity<CTxMemPoolModifiedEntry>,
111 CompareModifiedEntry
114 > indexed_modified_transaction_set;
116 typedef indexed_modified_transaction_set::nth_index<0>::type::iterator modtxiter;
117 typedef indexed_modified_transaction_set::index<ancestor_score>::type::iterator modtxscoreiter;
119 struct update_for_parent_inclusion
121 update_for_parent_inclusion(CTxMemPool::txiter it) : iter(it) {}
123 void operator() (CTxMemPoolModifiedEntry &e)
125 e.nModFeesWithAncestors -= iter->GetFee();
126 e.nSizeWithAncestors -= iter->GetTxSize();
127 e.nSigOpCostWithAncestors -= iter->GetSigOpCost();
130 CTxMemPool::txiter iter;
133 /** Generate a new block, without valid proof-of-work */
134 class BlockAssembler
136 private:
137 // The constructed block template
138 std::unique_ptr<CBlockTemplate> pblocktemplate;
139 // A convenience pointer that always refers to the CBlock in pblocktemplate
140 CBlock* pblock;
142 // Configuration parameters for the block size
143 bool fIncludeWitness;
144 unsigned int nBlockMaxWeight, nBlockMaxSize;
145 bool fNeedSizeAccounting;
146 CFeeRate blockMinFeeRate;
148 // Information on the current status of the block
149 uint64_t nBlockWeight;
150 uint64_t nBlockSize;
151 uint64_t nBlockTx;
152 uint64_t nBlockSigOpsCost;
153 CAmount nFees;
154 CTxMemPool::setEntries inBlock;
156 // Chain context for the block
157 int nHeight;
158 int64_t nLockTimeCutoff;
159 const CChainParams& chainparams;
161 public:
162 struct Options {
163 Options();
164 size_t nBlockMaxWeight;
165 size_t nBlockMaxSize;
166 CFeeRate blockMinFeeRate;
169 BlockAssembler(const CChainParams& params);
170 BlockAssembler(const CChainParams& params, const Options& options);
172 /** Construct a new block template with coinbase to scriptPubKeyIn */
173 std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn, bool fMineWitnessTx=true);
175 private:
176 // utility functions
177 /** Clear the block's state and prepare for assembling a new block */
178 void resetBlock();
179 /** Add a tx to the block */
180 void AddToBlock(CTxMemPool::txiter iter);
182 // Methods for how to add transactions to a block.
183 /** Add transactions based on feerate including unconfirmed ancestors
184 * Increments nPackagesSelected / nDescendantsUpdated with corresponding
185 * statistics from the package selection (for logging statistics). */
186 void addPackageTxs(int &nPackagesSelected, int &nDescendantsUpdated);
188 // helper functions for addPackageTxs()
189 /** Remove confirmed (inBlock) entries from given set */
190 void onlyUnconfirmed(CTxMemPool::setEntries& testSet);
191 /** Test if a new package would "fit" in the block */
192 bool TestPackage(uint64_t packageSize, int64_t packageSigOpsCost);
193 /** Perform checks on each transaction in a package:
194 * locktime, premature-witness, serialized size (if necessary)
195 * These checks should always succeed, and they're here
196 * only as an extra check in case of suboptimal node configuration */
197 bool TestPackageTransactions(const CTxMemPool::setEntries& package);
198 /** Return true if given transaction from mapTx has already been evaluated,
199 * or if the transaction's cached data in mapTx is incorrect. */
200 bool SkipMapTxEntry(CTxMemPool::txiter it, indexed_modified_transaction_set &mapModifiedTx, CTxMemPool::setEntries &failedTx);
201 /** Sort the package in an order that is valid to appear in a block */
202 void SortForBlock(const CTxMemPool::setEntries& package, CTxMemPool::txiter entry, std::vector<CTxMemPool::txiter>& sortedEntries);
203 /** Add descendants of given transactions to mapModifiedTx with ancestor
204 * state updated assuming given transactions are inBlock. Returns number
205 * of updated descendants. */
206 int UpdatePackagesForAdded(const CTxMemPool::setEntries& alreadyAdded, indexed_modified_transaction_set &mapModifiedTx);
209 /** Modify the extranonce in a block */
210 void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
211 int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
213 #endif // BITCOIN_MINER_H