util: Work around (virtual) memory exhaustion on 32-bit w/ glibc
[bitcoinplatinum.git] / src / miner.h
bloba159b055340ebd4384390794e8ac1507159c3e7d
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 void addPackageTxs();
186 // helper functions for addPackageTxs()
187 /** Remove confirmed (inBlock) entries from given set */
188 void onlyUnconfirmed(CTxMemPool::setEntries& testSet);
189 /** Test if a new package would "fit" in the block */
190 bool TestPackage(uint64_t packageSize, int64_t packageSigOpsCost);
191 /** Perform checks on each transaction in a package:
192 * locktime, premature-witness, serialized size (if necessary)
193 * These checks should always succeed, and they're here
194 * only as an extra check in case of suboptimal node configuration */
195 bool TestPackageTransactions(const CTxMemPool::setEntries& package);
196 /** Return true if given transaction from mapTx has already been evaluated,
197 * or if the transaction's cached data in mapTx is incorrect. */
198 bool SkipMapTxEntry(CTxMemPool::txiter it, indexed_modified_transaction_set &mapModifiedTx, CTxMemPool::setEntries &failedTx);
199 /** Sort the package in an order that is valid to appear in a block */
200 void SortForBlock(const CTxMemPool::setEntries& package, CTxMemPool::txiter entry, std::vector<CTxMemPool::txiter>& sortedEntries);
201 /** Add descendants of given transactions to mapModifiedTx with ancestor
202 * state updated assuming given transactions are inBlock. */
203 void UpdatePackagesForAdded(const CTxMemPool::setEntries& alreadyAdded, indexed_modified_transaction_set &mapModifiedTx);
206 /** Modify the extranonce in a block */
207 void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
208 int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
210 #endif // BITCOIN_MINER_H