net: correctly ban before the handshake is complete
[bitcoinplatinum.git] / src / miner.h
blob3ba92b16b8e03f539dc7bcff84b0a7d8cc0de4e2
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 // Variables used for addPriorityTxs
162 int lastFewTxs;
163 bool blockFinished;
165 public:
166 BlockAssembler(const CChainParams& chainparams);
167 /** Construct a new block template with coinbase to scriptPubKeyIn */
168 std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn);
170 private:
171 // utility functions
172 /** Clear the block's state and prepare for assembling a new block */
173 void resetBlock();
174 /** Add a tx to the block */
175 void AddToBlock(CTxMemPool::txiter iter);
177 // Methods for how to add transactions to a block.
178 /** Add transactions based on tx "priority" */
179 void addPriorityTxs();
180 /** Add transactions based on feerate including unconfirmed ancestors */
181 void addPackageTxs();
183 // helper function for addPriorityTxs
184 /** Test if tx will still "fit" in the block */
185 bool TestForBlock(CTxMemPool::txiter iter);
186 /** Test if tx still has unconfirmed parents not yet in block */
187 bool isStillDependent(CTxMemPool::txiter iter);
189 // helper functions for addPackageTxs()
190 /** Remove confirmed (inBlock) entries from given set */
191 void onlyUnconfirmed(CTxMemPool::setEntries& testSet);
192 /** Test if a new package would "fit" in the block */
193 bool TestPackage(uint64_t packageSize, int64_t packageSigOpsCost);
194 /** Perform checks on each transaction in a package:
195 * locktime, premature-witness, serialized size (if necessary)
196 * These checks should always succeed, and they're here
197 * only as an extra check in case of suboptimal node configuration */
198 bool TestPackageTransactions(const CTxMemPool::setEntries& package);
199 /** Return true if given transaction from mapTx has already been evaluated,
200 * or if the transaction's cached data in mapTx is incorrect. */
201 bool SkipMapTxEntry(CTxMemPool::txiter it, indexed_modified_transaction_set &mapModifiedTx, CTxMemPool::setEntries &failedTx);
202 /** Sort the package in an order that is valid to appear in a block */
203 void SortForBlock(const CTxMemPool::setEntries& package, CTxMemPool::txiter entry, std::vector<CTxMemPool::txiter>& sortedEntries);
204 /** Add descendants of given transactions to mapModifiedTx with ancestor
205 * state updated assuming given transactions are inBlock. */
206 void 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