Merge #12079: Improve prioritisetransaction test coverage
[bitcoinplatinum.git] / src / miner.cpp
blob4e63ab4df05011632f9ff7c4cc84f8eb7f06c244
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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 #include <miner.h>
8 #include <amount.h>
9 #include <chain.h>
10 #include <chainparams.h>
11 #include <coins.h>
12 #include <consensus/consensus.h>
13 #include <consensus/tx_verify.h>
14 #include <consensus/merkle.h>
15 #include <consensus/validation.h>
16 #include <hash.h>
17 #include <validation.h>
18 #include <net.h>
19 #include <policy/feerate.h>
20 #include <policy/policy.h>
21 #include <pow.h>
22 #include <primitives/transaction.h>
23 #include <script/standard.h>
24 #include <timedata.h>
25 #include <util.h>
26 #include <utilmoneystr.h>
27 #include <validationinterface.h>
29 #include <algorithm>
30 #include <queue>
31 #include <utility>
33 //////////////////////////////////////////////////////////////////////////////
35 // BitcoinMiner
39 // Unconfirmed transactions in the memory pool often depend on other
40 // transactions in the memory pool. When we select transactions from the
41 // pool, we select by highest fee rate of a transaction combined with all
42 // its ancestors.
44 uint64_t nLastBlockTx = 0;
45 uint64_t nLastBlockWeight = 0;
47 int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
49 int64_t nOldTime = pblock->nTime;
50 int64_t nNewTime = std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());
52 if (nOldTime < nNewTime)
53 pblock->nTime = nNewTime;
55 // Updating time can change work required on testnet:
56 if (consensusParams.fPowAllowMinDifficultyBlocks)
57 pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, consensusParams);
59 return nNewTime - nOldTime;
62 BlockAssembler::Options::Options() {
63 blockMinFeeRate = CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE);
64 nBlockMaxWeight = DEFAULT_BLOCK_MAX_WEIGHT;
67 BlockAssembler::BlockAssembler(const CChainParams& params, const Options& options) : chainparams(params)
69 blockMinFeeRate = options.blockMinFeeRate;
70 // Limit weight to between 4K and MAX_BLOCK_WEIGHT-4K for sanity:
71 nBlockMaxWeight = std::max<size_t>(4000, std::min<size_t>(MAX_BLOCK_WEIGHT - 4000, options.nBlockMaxWeight));
74 static BlockAssembler::Options DefaultOptions(const CChainParams& params)
76 // Block resource limits
77 // If neither -blockmaxsize or -blockmaxweight is given, limit to DEFAULT_BLOCK_MAX_*
78 // If only one is given, only restrict the specified resource.
79 // If both are given, restrict both.
80 BlockAssembler::Options options;
81 options.nBlockMaxWeight = gArgs.GetArg("-blockmaxweight", DEFAULT_BLOCK_MAX_WEIGHT);
82 if (gArgs.IsArgSet("-blockmintxfee")) {
83 CAmount n = 0;
84 ParseMoney(gArgs.GetArg("-blockmintxfee", ""), n);
85 options.blockMinFeeRate = CFeeRate(n);
86 } else {
87 options.blockMinFeeRate = CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE);
89 return options;
92 BlockAssembler::BlockAssembler(const CChainParams& params) : BlockAssembler(params, DefaultOptions(params)) {}
94 void BlockAssembler::resetBlock()
96 inBlock.clear();
98 // Reserve space for coinbase tx
99 nBlockWeight = 4000;
100 nBlockSigOpsCost = 400;
101 fIncludeWitness = false;
103 // These counters do not include coinbase tx
104 nBlockTx = 0;
105 nFees = 0;
108 std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn, bool fMineWitnessTx)
110 int64_t nTimeStart = GetTimeMicros();
112 resetBlock();
114 pblocktemplate.reset(new CBlockTemplate());
116 if(!pblocktemplate.get())
117 return nullptr;
118 pblock = &pblocktemplate->block; // pointer for convenience
120 // Add dummy coinbase tx as first transaction
121 pblock->vtx.emplace_back();
122 pblocktemplate->vTxFees.push_back(-1); // updated at end
123 pblocktemplate->vTxSigOpsCost.push_back(-1); // updated at end
125 LOCK2(cs_main, mempool.cs);
126 CBlockIndex* pindexPrev = chainActive.Tip();
127 assert(pindexPrev != nullptr);
128 nHeight = pindexPrev->nHeight + 1;
130 pblock->nVersion = ComputeBlockVersion(pindexPrev, chainparams.GetConsensus());
131 // -regtest only: allow overriding block.nVersion with
132 // -blockversion=N to test forking scenarios
133 if (chainparams.MineBlocksOnDemand())
134 pblock->nVersion = gArgs.GetArg("-blockversion", pblock->nVersion);
136 pblock->nTime = GetAdjustedTime();
137 const int64_t nMedianTimePast = pindexPrev->GetMedianTimePast();
139 nLockTimeCutoff = (STANDARD_LOCKTIME_VERIFY_FLAGS & LOCKTIME_MEDIAN_TIME_PAST)
140 ? nMedianTimePast
141 : pblock->GetBlockTime();
143 // Decide whether to include witness transactions
144 // This is only needed in case the witness softfork activation is reverted
145 // (which would require a very deep reorganization) or when
146 // -promiscuousmempoolflags is used.
147 // TODO: replace this with a call to main to assess validity of a mempool
148 // transaction (which in most cases can be a no-op).
149 fIncludeWitness = IsWitnessEnabled(pindexPrev, chainparams.GetConsensus()) && fMineWitnessTx;
151 int nPackagesSelected = 0;
152 int nDescendantsUpdated = 0;
153 addPackageTxs(nPackagesSelected, nDescendantsUpdated);
155 int64_t nTime1 = GetTimeMicros();
157 nLastBlockTx = nBlockTx;
158 nLastBlockWeight = nBlockWeight;
160 // Create coinbase transaction.
161 CMutableTransaction coinbaseTx;
162 coinbaseTx.vin.resize(1);
163 coinbaseTx.vin[0].prevout.SetNull();
164 coinbaseTx.vout.resize(1);
165 coinbaseTx.vout[0].scriptPubKey = scriptPubKeyIn;
166 coinbaseTx.vout[0].nValue = nFees + GetBlockSubsidy(nHeight, chainparams.GetConsensus());
167 coinbaseTx.vin[0].scriptSig = CScript() << nHeight << OP_0;
168 pblock->vtx[0] = MakeTransactionRef(std::move(coinbaseTx));
169 pblocktemplate->vchCoinbaseCommitment = GenerateCoinbaseCommitment(*pblock, pindexPrev, chainparams.GetConsensus());
170 pblocktemplate->vTxFees[0] = -nFees;
172 LogPrintf("CreateNewBlock(): block weight: %u txs: %u fees: %ld sigops %d\n", GetBlockWeight(*pblock), nBlockTx, nFees, nBlockSigOpsCost);
174 // Fill in header
175 pblock->hashPrevBlock = pindexPrev->GetBlockHash();
176 UpdateTime(pblock, chainparams.GetConsensus(), pindexPrev);
177 pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, chainparams.GetConsensus());
178 pblock->nNonce = 0;
179 pblocktemplate->vTxSigOpsCost[0] = WITNESS_SCALE_FACTOR * GetLegacySigOpCount(*pblock->vtx[0]);
181 CValidationState state;
182 if (!TestBlockValidity(state, chainparams, *pblock, pindexPrev, false, false)) {
183 throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", __func__, FormatStateMessage(state)));
185 int64_t nTime2 = GetTimeMicros();
187 LogPrint(BCLog::BENCH, "CreateNewBlock() packages: %.2fms (%d packages, %d updated descendants), validity: %.2fms (total %.2fms)\n", 0.001 * (nTime1 - nTimeStart), nPackagesSelected, nDescendantsUpdated, 0.001 * (nTime2 - nTime1), 0.001 * (nTime2 - nTimeStart));
189 return std::move(pblocktemplate);
192 void BlockAssembler::onlyUnconfirmed(CTxMemPool::setEntries& testSet)
194 for (CTxMemPool::setEntries::iterator iit = testSet.begin(); iit != testSet.end(); ) {
195 // Only test txs not already in the block
196 if (inBlock.count(*iit)) {
197 testSet.erase(iit++);
199 else {
200 iit++;
205 bool BlockAssembler::TestPackage(uint64_t packageSize, int64_t packageSigOpsCost) const
207 // TODO: switch to weight-based accounting for packages instead of vsize-based accounting.
208 if (nBlockWeight + WITNESS_SCALE_FACTOR * packageSize >= nBlockMaxWeight)
209 return false;
210 if (nBlockSigOpsCost + packageSigOpsCost >= MAX_BLOCK_SIGOPS_COST)
211 return false;
212 return true;
215 // Perform transaction-level checks before adding to block:
216 // - transaction finality (locktime)
217 // - premature witness (in case segwit transactions are added to mempool before
218 // segwit activation)
219 bool BlockAssembler::TestPackageTransactions(const CTxMemPool::setEntries& package)
221 for (const CTxMemPool::txiter it : package) {
222 if (!IsFinalTx(it->GetTx(), nHeight, nLockTimeCutoff))
223 return false;
224 if (!fIncludeWitness && it->GetTx().HasWitness())
225 return false;
227 return true;
230 void BlockAssembler::AddToBlock(CTxMemPool::txiter iter)
232 pblock->vtx.emplace_back(iter->GetSharedTx());
233 pblocktemplate->vTxFees.push_back(iter->GetFee());
234 pblocktemplate->vTxSigOpsCost.push_back(iter->GetSigOpCost());
235 nBlockWeight += iter->GetTxWeight();
236 ++nBlockTx;
237 nBlockSigOpsCost += iter->GetSigOpCost();
238 nFees += iter->GetFee();
239 inBlock.insert(iter);
241 bool fPrintPriority = gArgs.GetBoolArg("-printpriority", DEFAULT_PRINTPRIORITY);
242 if (fPrintPriority) {
243 LogPrintf("fee %s txid %s\n",
244 CFeeRate(iter->GetModifiedFee(), iter->GetTxSize()).ToString(),
245 iter->GetTx().GetHash().ToString());
249 int BlockAssembler::UpdatePackagesForAdded(const CTxMemPool::setEntries& alreadyAdded,
250 indexed_modified_transaction_set &mapModifiedTx)
252 int nDescendantsUpdated = 0;
253 for (const CTxMemPool::txiter it : alreadyAdded) {
254 CTxMemPool::setEntries descendants;
255 mempool.CalculateDescendants(it, descendants);
256 // Insert all descendants (not yet in block) into the modified set
257 for (CTxMemPool::txiter desc : descendants) {
258 if (alreadyAdded.count(desc))
259 continue;
260 ++nDescendantsUpdated;
261 modtxiter mit = mapModifiedTx.find(desc);
262 if (mit == mapModifiedTx.end()) {
263 CTxMemPoolModifiedEntry modEntry(desc);
264 modEntry.nSizeWithAncestors -= it->GetTxSize();
265 modEntry.nModFeesWithAncestors -= it->GetModifiedFee();
266 modEntry.nSigOpCostWithAncestors -= it->GetSigOpCost();
267 mapModifiedTx.insert(modEntry);
268 } else {
269 mapModifiedTx.modify(mit, update_for_parent_inclusion(it));
273 return nDescendantsUpdated;
276 // Skip entries in mapTx that are already in a block or are present
277 // in mapModifiedTx (which implies that the mapTx ancestor state is
278 // stale due to ancestor inclusion in the block)
279 // Also skip transactions that we've already failed to add. This can happen if
280 // we consider a transaction in mapModifiedTx and it fails: we can then
281 // potentially consider it again while walking mapTx. It's currently
282 // guaranteed to fail again, but as a belt-and-suspenders check we put it in
283 // failedTx and avoid re-evaluation, since the re-evaluation would be using
284 // cached size/sigops/fee values that are not actually correct.
285 bool BlockAssembler::SkipMapTxEntry(CTxMemPool::txiter it, indexed_modified_transaction_set &mapModifiedTx, CTxMemPool::setEntries &failedTx)
287 assert (it != mempool.mapTx.end());
288 return mapModifiedTx.count(it) || inBlock.count(it) || failedTx.count(it);
291 void BlockAssembler::SortForBlock(const CTxMemPool::setEntries& package, CTxMemPool::txiter entry, std::vector<CTxMemPool::txiter>& sortedEntries)
293 // Sort package by ancestor count
294 // If a transaction A depends on transaction B, then A's ancestor count
295 // must be greater than B's. So this is sufficient to validly order the
296 // transactions for block inclusion.
297 sortedEntries.clear();
298 sortedEntries.insert(sortedEntries.begin(), package.begin(), package.end());
299 std::sort(sortedEntries.begin(), sortedEntries.end(), CompareTxIterByAncestorCount());
302 // This transaction selection algorithm orders the mempool based
303 // on feerate of a transaction including all unconfirmed ancestors.
304 // Since we don't remove transactions from the mempool as we select them
305 // for block inclusion, we need an alternate method of updating the feerate
306 // of a transaction with its not-yet-selected ancestors as we go.
307 // This is accomplished by walking the in-mempool descendants of selected
308 // transactions and storing a temporary modified state in mapModifiedTxs.
309 // Each time through the loop, we compare the best transaction in
310 // mapModifiedTxs with the next transaction in the mempool to decide what
311 // transaction package to work on next.
312 void BlockAssembler::addPackageTxs(int &nPackagesSelected, int &nDescendantsUpdated)
314 // mapModifiedTx will store sorted packages after they are modified
315 // because some of their txs are already in the block
316 indexed_modified_transaction_set mapModifiedTx;
317 // Keep track of entries that failed inclusion, to avoid duplicate work
318 CTxMemPool::setEntries failedTx;
320 // Start by adding all descendants of previously added txs to mapModifiedTx
321 // and modifying them for their already included ancestors
322 UpdatePackagesForAdded(inBlock, mapModifiedTx);
324 CTxMemPool::indexed_transaction_set::index<ancestor_score>::type::iterator mi = mempool.mapTx.get<ancestor_score>().begin();
325 CTxMemPool::txiter iter;
327 // Limit the number of attempts to add transactions to the block when it is
328 // close to full; this is just a simple heuristic to finish quickly if the
329 // mempool has a lot of entries.
330 const int64_t MAX_CONSECUTIVE_FAILURES = 1000;
331 int64_t nConsecutiveFailed = 0;
333 while (mi != mempool.mapTx.get<ancestor_score>().end() || !mapModifiedTx.empty())
335 // First try to find a new transaction in mapTx to evaluate.
336 if (mi != mempool.mapTx.get<ancestor_score>().end() &&
337 SkipMapTxEntry(mempool.mapTx.project<0>(mi), mapModifiedTx, failedTx)) {
338 ++mi;
339 continue;
342 // Now that mi is not stale, determine which transaction to evaluate:
343 // the next entry from mapTx, or the best from mapModifiedTx?
344 bool fUsingModified = false;
346 modtxscoreiter modit = mapModifiedTx.get<ancestor_score>().begin();
347 if (mi == mempool.mapTx.get<ancestor_score>().end()) {
348 // We're out of entries in mapTx; use the entry from mapModifiedTx
349 iter = modit->iter;
350 fUsingModified = true;
351 } else {
352 // Try to compare the mapTx entry to the mapModifiedTx entry
353 iter = mempool.mapTx.project<0>(mi);
354 if (modit != mapModifiedTx.get<ancestor_score>().end() &&
355 CompareModifiedEntry()(*modit, CTxMemPoolModifiedEntry(iter))) {
356 // The best entry in mapModifiedTx has higher score
357 // than the one from mapTx.
358 // Switch which transaction (package) to consider
359 iter = modit->iter;
360 fUsingModified = true;
361 } else {
362 // Either no entry in mapModifiedTx, or it's worse than mapTx.
363 // Increment mi for the next loop iteration.
364 ++mi;
368 // We skip mapTx entries that are inBlock, and mapModifiedTx shouldn't
369 // contain anything that is inBlock.
370 assert(!inBlock.count(iter));
372 uint64_t packageSize = iter->GetSizeWithAncestors();
373 CAmount packageFees = iter->GetModFeesWithAncestors();
374 int64_t packageSigOpsCost = iter->GetSigOpCostWithAncestors();
375 if (fUsingModified) {
376 packageSize = modit->nSizeWithAncestors;
377 packageFees = modit->nModFeesWithAncestors;
378 packageSigOpsCost = modit->nSigOpCostWithAncestors;
381 if (packageFees < blockMinFeeRate.GetFee(packageSize)) {
382 // Everything else we might consider has a lower fee rate
383 return;
386 if (!TestPackage(packageSize, packageSigOpsCost)) {
387 if (fUsingModified) {
388 // Since we always look at the best entry in mapModifiedTx,
389 // we must erase failed entries so that we can consider the
390 // next best entry on the next loop iteration
391 mapModifiedTx.get<ancestor_score>().erase(modit);
392 failedTx.insert(iter);
395 ++nConsecutiveFailed;
397 if (nConsecutiveFailed > MAX_CONSECUTIVE_FAILURES && nBlockWeight >
398 nBlockMaxWeight - 4000) {
399 // Give up if we're close to full and haven't succeeded in a while
400 break;
402 continue;
405 CTxMemPool::setEntries ancestors;
406 uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
407 std::string dummy;
408 mempool.CalculateMemPoolAncestors(*iter, ancestors, nNoLimit, nNoLimit, nNoLimit, nNoLimit, dummy, false);
410 onlyUnconfirmed(ancestors);
411 ancestors.insert(iter);
413 // Test if all tx's are Final
414 if (!TestPackageTransactions(ancestors)) {
415 if (fUsingModified) {
416 mapModifiedTx.get<ancestor_score>().erase(modit);
417 failedTx.insert(iter);
419 continue;
422 // This transaction will make it in; reset the failed counter.
423 nConsecutiveFailed = 0;
425 // Package can be added. Sort the entries in a valid order.
426 std::vector<CTxMemPool::txiter> sortedEntries;
427 SortForBlock(ancestors, iter, sortedEntries);
429 for (size_t i=0; i<sortedEntries.size(); ++i) {
430 AddToBlock(sortedEntries[i]);
431 // Erase from the modified set, if present
432 mapModifiedTx.erase(sortedEntries[i]);
435 ++nPackagesSelected;
437 // Update transactions that depend on each of these
438 nDescendantsUpdated += UpdatePackagesForAdded(ancestors, mapModifiedTx);
442 void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce)
444 // Update nExtraNonce
445 static uint256 hashPrevBlock;
446 if (hashPrevBlock != pblock->hashPrevBlock)
448 nExtraNonce = 0;
449 hashPrevBlock = pblock->hashPrevBlock;
451 ++nExtraNonce;
452 unsigned int nHeight = pindexPrev->nHeight+1; // Height first in coinbase required for block.version=2
453 CMutableTransaction txCoinbase(*pblock->vtx[0]);
454 txCoinbase.vin[0].scriptSig = (CScript() << nHeight << CScriptNum(nExtraNonce)) + COINBASE_FLAGS;
455 assert(txCoinbase.vin[0].scriptSig.size() <= 100);
457 pblock->vtx[0] = MakeTransactionRef(std::move(txCoinbase));
458 pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);