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
;
143 CFeeRate blockMinFeeRate
;
145 // Information on the current status of the block
146 uint64_t nBlockWeight
;
148 uint64_t nBlockSigOpsCost
;
150 CTxMemPool::setEntries inBlock
;
152 // Chain context for the block
154 int64_t nLockTimeCutoff
;
155 const CChainParams
& chainparams
;
160 size_t nBlockMaxWeight
;
161 CFeeRate blockMinFeeRate
;
164 explicit BlockAssembler(const CChainParams
& params
);
165 BlockAssembler(const CChainParams
& params
, const Options
& options
);
167 /** Construct a new block template with coinbase to scriptPubKeyIn */
168 std::unique_ptr
<CBlockTemplate
> CreateNewBlock(const CScript
& scriptPubKeyIn
, bool fMineWitnessTx
=true);
172 /** Clear the block's state and prepare for assembling a new block */
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 feerate including unconfirmed ancestors
179 * Increments nPackagesSelected / nDescendantsUpdated with corresponding
180 * statistics from the package selection (for logging statistics). */
181 void addPackageTxs(int &nPackagesSelected
, int &nDescendantsUpdated
);
183 // helper functions for addPackageTxs()
184 /** Remove confirmed (inBlock) entries from given set */
185 void onlyUnconfirmed(CTxMemPool::setEntries
& testSet
);
186 /** Test if a new package would "fit" in the block */
187 bool TestPackage(uint64_t packageSize
, int64_t packageSigOpsCost
) const;
188 /** Perform checks on each transaction in a package:
189 * locktime, premature-witness, serialized size (if necessary)
190 * These checks should always succeed, and they're here
191 * only as an extra check in case of suboptimal node configuration */
192 bool TestPackageTransactions(const CTxMemPool::setEntries
& package
);
193 /** Return true if given transaction from mapTx has already been evaluated,
194 * or if the transaction's cached data in mapTx is incorrect. */
195 bool SkipMapTxEntry(CTxMemPool::txiter it
, indexed_modified_transaction_set
&mapModifiedTx
, CTxMemPool::setEntries
&failedTx
);
196 /** Sort the package in an order that is valid to appear in a block */
197 void SortForBlock(const CTxMemPool::setEntries
& package
, CTxMemPool::txiter entry
, std::vector
<CTxMemPool::txiter
>& sortedEntries
);
198 /** Add descendants of given transactions to mapModifiedTx with ancestor
199 * state updated assuming given transactions are inBlock. Returns number
200 * of updated descendants. */
201 int UpdatePackagesForAdded(const CTxMemPool::setEntries
& alreadyAdded
, indexed_modified_transaction_set
&mapModifiedTx
);
204 /** Modify the extranonce in a block */
205 void IncrementExtraNonce(CBlock
* pblock
, const CBlockIndex
* pindexPrev
, unsigned int& nExtraNonce
);
206 int64_t UpdateTime(CBlockHeader
* pblock
, const Consensus::Params
& consensusParams
, const CBlockIndex
* pindexPrev
);
208 #endif // BITCOIN_MINER_H