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"
14 #include <boost/multi_index_container.hpp>
15 #include <boost/multi_index/ordered_index.hpp>
21 namespace Consensus
{ struct Params
; };
23 static const bool DEFAULT_PRINTPRIORITY
= false;
28 std::vector
<CAmount
> vTxFees
;
29 std::vector
<int64_t> vTxSigOpsCost
;
30 std::vector
<unsigned char> vchCoinbaseCommitment
;
33 // Container for tracking updates to ancestor feerate as we include (parent)
34 // transactions in a block
35 struct CTxMemPoolModifiedEntry
{
36 explicit CTxMemPoolModifiedEntry(CTxMemPool::txiter entry
)
39 nSizeWithAncestors
= entry
->GetSizeWithAncestors();
40 nModFeesWithAncestors
= entry
->GetModFeesWithAncestors();
41 nSigOpCostWithAncestors
= entry
->GetSigOpCostWithAncestors();
44 CTxMemPool::txiter iter
;
45 uint64_t nSizeWithAncestors
;
46 CAmount nModFeesWithAncestors
;
47 int64_t nSigOpCostWithAncestors
;
50 /** Comparator for CTxMemPool::txiter objects.
51 * It simply compares the internal memory address of the CTxMemPoolEntry object
52 * pointed to. This means it has no meaning, and is only useful for using them
53 * as key in other indexes.
55 struct CompareCTxMemPoolIter
{
56 bool operator()(const CTxMemPool::txiter
& a
, const CTxMemPool::txiter
& b
) const
62 struct modifiedentry_iter
{
63 typedef CTxMemPool::txiter result_type
;
64 result_type
operator() (const CTxMemPoolModifiedEntry
&entry
) const
70 // This matches the calculation in CompareTxMemPoolEntryByAncestorFee,
71 // except operating on CTxMemPoolModifiedEntry.
72 // TODO: refactor to avoid duplication of this logic.
73 struct CompareModifiedEntry
{
74 bool operator()(const CTxMemPoolModifiedEntry
&a
, const CTxMemPoolModifiedEntry
&b
)
76 double f1
= (double)a
.nModFeesWithAncestors
* b
.nSizeWithAncestors
;
77 double f2
= (double)b
.nModFeesWithAncestors
* a
.nSizeWithAncestors
;
79 return CTxMemPool::CompareIteratorByHash()(a
.iter
, b
.iter
);
85 // A comparator that sorts transactions based on number of ancestors.
86 // This is sufficient to sort an ancestor package in an order that is valid
87 // to appear in a block.
88 struct CompareTxIterByAncestorCount
{
89 bool operator()(const CTxMemPool::txiter
&a
, const CTxMemPool::txiter
&b
)
91 if (a
->GetCountWithAncestors() != b
->GetCountWithAncestors())
92 return a
->GetCountWithAncestors() < b
->GetCountWithAncestors();
93 return CTxMemPool::CompareIteratorByHash()(a
, b
);
97 typedef boost::multi_index_container
<
98 CTxMemPoolModifiedEntry
,
99 boost::multi_index::indexed_by
<
100 boost::multi_index::ordered_unique
<
102 CompareCTxMemPoolIter
104 // sorted by modified ancestor fee rate
105 boost::multi_index::ordered_non_unique
<
106 // Reuse same tag from CTxMemPool's similar index
107 boost::multi_index::tag
<ancestor_score
>,
108 boost::multi_index::identity
<CTxMemPoolModifiedEntry
>,
112 > indexed_modified_transaction_set
;
114 typedef indexed_modified_transaction_set::nth_index
<0>::type::iterator modtxiter
;
115 typedef indexed_modified_transaction_set::index
<ancestor_score
>::type::iterator modtxscoreiter
;
117 struct update_for_parent_inclusion
119 explicit update_for_parent_inclusion(CTxMemPool::txiter it
) : iter(it
) {}
121 void operator() (CTxMemPoolModifiedEntry
&e
)
123 e
.nModFeesWithAncestors
-= iter
->GetFee();
124 e
.nSizeWithAncestors
-= iter
->GetTxSize();
125 e
.nSigOpCostWithAncestors
-= iter
->GetSigOpCost();
128 CTxMemPool::txiter iter
;
131 /** Generate a new block, without valid proof-of-work */
135 // The constructed block template
136 std::unique_ptr
<CBlockTemplate
> pblocktemplate
;
137 // A convenience pointer that always refers to the CBlock in pblocktemplate
140 // Configuration parameters for the block size
141 bool fIncludeWitness
;
142 unsigned int nBlockMaxWeight
, nBlockMaxSize
;
143 bool fNeedSizeAccounting
;
144 CFeeRate blockMinFeeRate
;
146 // Information on the current status of the block
147 uint64_t nBlockWeight
;
150 uint64_t nBlockSigOpsCost
;
152 CTxMemPool::setEntries inBlock
;
154 // Chain context for the block
156 int64_t nLockTimeCutoff
;
157 const CChainParams
& chainparams
;
162 size_t nBlockMaxWeight
;
163 size_t nBlockMaxSize
;
164 CFeeRate blockMinFeeRate
;
167 explicit BlockAssembler(const CChainParams
& params
);
168 BlockAssembler(const CChainParams
& params
, const Options
& options
);
170 /** Construct a new block template with coinbase to scriptPubKeyIn */
171 std::unique_ptr
<CBlockTemplate
> CreateNewBlock(const CScript
& scriptPubKeyIn
, bool fMineWitnessTx
=true);
175 /** Clear the block's state and prepare for assembling a new block */
177 /** Add a tx to the block */
178 void AddToBlock(CTxMemPool::txiter iter
);
180 // Methods for how to add transactions to a block.
181 /** Add transactions based on feerate including unconfirmed ancestors
182 * Increments nPackagesSelected / nDescendantsUpdated with corresponding
183 * statistics from the package selection (for logging statistics). */
184 void addPackageTxs(int &nPackagesSelected
, int &nDescendantsUpdated
);
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
) const;
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. Returns number
203 * of updated descendants. */
204 int UpdatePackagesForAdded(const CTxMemPool::setEntries
& alreadyAdded
, indexed_modified_transaction_set
&mapModifiedTx
);
207 /** Modify the extranonce in a block */
208 void IncrementExtraNonce(CBlock
* pblock
, const CBlockIndex
* pindexPrev
, unsigned int& nExtraNonce
);
209 int64_t UpdateTime(CBlockHeader
* pblock
, const Consensus::Params
& consensusParams
, const CBlockIndex
* pindexPrev
);
211 #endif // BITCOIN_MINER_H