Remove duplicate destination decoding
[bitcoinplatinum.git] / src / miner.cpp
blob249ea172a64bc3e9a19ea6cf3603dc80a1409480
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 #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 "txmempool.h"
26 #include "util.h"
27 #include "utilmoneystr.h"
28 #include "validationinterface.h"
30 #include <algorithm>
31 #include <queue>
32 #include <utility>
34 //////////////////////////////////////////////////////////////////////////////
36 // BitcoinMiner
40 // Unconfirmed transactions in the memory pool often depend on other
41 // transactions in the memory pool. When we select transactions from the
42 // pool, we select by highest fee rate of a transaction combined with all
43 // its ancestors.
45 uint64_t nLastBlockTx = 0;
46 uint64_t nLastBlockSize = 0;
47 uint64_t nLastBlockWeight = 0;
49 int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
51 int64_t nOldTime = pblock->nTime;
52 int64_t nNewTime = std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());
54 if (nOldTime < nNewTime)
55 pblock->nTime = nNewTime;
57 // Updating time can change work required on testnet:
58 if (consensusParams.fPowAllowMinDifficultyBlocks)
59 pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, consensusParams);
61 return nNewTime - nOldTime;
64 BlockAssembler::Options::Options() {
65 blockMinFeeRate = CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE);
66 nBlockMaxWeight = DEFAULT_BLOCK_MAX_WEIGHT;
67 nBlockMaxSize = DEFAULT_BLOCK_MAX_SIZE;
70 BlockAssembler::BlockAssembler(const CChainParams& params, const Options& options) : chainparams(params)
72 blockMinFeeRate = options.blockMinFeeRate;
73 // Limit weight to between 4K and MAX_BLOCK_WEIGHT-4K for sanity:
74 nBlockMaxWeight = std::max<size_t>(4000, std::min<size_t>(MAX_BLOCK_WEIGHT - 4000, options.nBlockMaxWeight));
75 // Limit size to between 1K and MAX_BLOCK_SERIALIZED_SIZE-1K for sanity:
76 nBlockMaxSize = std::max<size_t>(1000, std::min<size_t>(MAX_BLOCK_SERIALIZED_SIZE - 1000, options.nBlockMaxSize));
77 // Whether we need to account for byte usage (in addition to weight usage)
78 fNeedSizeAccounting = (nBlockMaxSize < MAX_BLOCK_SERIALIZED_SIZE - 1000);
81 static BlockAssembler::Options DefaultOptions(const CChainParams& params)
83 // Block resource limits
84 // If neither -blockmaxsize or -blockmaxweight is given, limit to DEFAULT_BLOCK_MAX_*
85 // If only one is given, only restrict the specified resource.
86 // If both are given, restrict both.
87 BlockAssembler::Options options;
88 options.nBlockMaxWeight = DEFAULT_BLOCK_MAX_WEIGHT;
89 options.nBlockMaxSize = DEFAULT_BLOCK_MAX_SIZE;
90 bool fWeightSet = false;
91 if (gArgs.IsArgSet("-blockmaxweight")) {
92 options.nBlockMaxWeight = gArgs.GetArg("-blockmaxweight", DEFAULT_BLOCK_MAX_WEIGHT);
93 options.nBlockMaxSize = MAX_BLOCK_SERIALIZED_SIZE;
94 fWeightSet = true;
96 if (gArgs.IsArgSet("-blockmaxsize")) {
97 options.nBlockMaxSize = gArgs.GetArg("-blockmaxsize", DEFAULT_BLOCK_MAX_SIZE);
98 if (!fWeightSet) {
99 options.nBlockMaxWeight = options.nBlockMaxSize * WITNESS_SCALE_FACTOR;
102 if (gArgs.IsArgSet("-blockmintxfee")) {
103 CAmount n = 0;
104 ParseMoney(gArgs.GetArg("-blockmintxfee", ""), n);
105 options.blockMinFeeRate = CFeeRate(n);
106 } else {
107 options.blockMinFeeRate = CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE);
109 return options;
112 BlockAssembler::BlockAssembler(const CChainParams& params) : BlockAssembler(params, DefaultOptions(params)) {}
114 void BlockAssembler::resetBlock()
116 inBlock.clear();
118 // Reserve space for coinbase tx
119 nBlockSize = 1000;
120 nBlockWeight = 4000;
121 nBlockSigOpsCost = 400;
122 fIncludeWitness = false;
124 // These counters do not include coinbase tx
125 nBlockTx = 0;
126 nFees = 0;
129 std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn, bool fMineWitnessTx)
131 int64_t nTimeStart = GetTimeMicros();
133 resetBlock();
135 pblocktemplate.reset(new CBlockTemplate());
137 if(!pblocktemplate.get())
138 return nullptr;
139 pblock = &pblocktemplate->block; // pointer for convenience
141 // Add dummy coinbase tx as first transaction
142 pblock->vtx.emplace_back();
143 pblocktemplate->vTxFees.push_back(-1); // updated at end
144 pblocktemplate->vTxSigOpsCost.push_back(-1); // updated at end
146 LOCK2(cs_main, mempool.cs);
147 CBlockIndex* pindexPrev = chainActive.Tip();
148 assert(pindexPrev != nullptr);
149 nHeight = pindexPrev->nHeight + 1;
151 pblock->nVersion = ComputeBlockVersion(pindexPrev, chainparams.GetConsensus());
152 // -regtest only: allow overriding block.nVersion with
153 // -blockversion=N to test forking scenarios
154 if (chainparams.MineBlocksOnDemand())
155 pblock->nVersion = gArgs.GetArg("-blockversion", pblock->nVersion);
157 pblock->nTime = GetAdjustedTime();
158 const int64_t nMedianTimePast = pindexPrev->GetMedianTimePast();
160 nLockTimeCutoff = (STANDARD_LOCKTIME_VERIFY_FLAGS & LOCKTIME_MEDIAN_TIME_PAST)
161 ? nMedianTimePast
162 : pblock->GetBlockTime();
164 // Decide whether to include witness transactions
165 // This is only needed in case the witness softfork activation is reverted
166 // (which would require a very deep reorganization) or when
167 // -promiscuousmempoolflags is used.
168 // TODO: replace this with a call to main to assess validity of a mempool
169 // transaction (which in most cases can be a no-op).
170 fIncludeWitness = IsWitnessEnabled(pindexPrev, chainparams.GetConsensus()) && fMineWitnessTx;
172 int nPackagesSelected = 0;
173 int nDescendantsUpdated = 0;
174 addPackageTxs(nPackagesSelected, nDescendantsUpdated);
176 int64_t nTime1 = GetTimeMicros();
178 nLastBlockTx = nBlockTx;
179 nLastBlockSize = nBlockSize;
180 nLastBlockWeight = nBlockWeight;
182 // Create coinbase transaction.
183 CMutableTransaction coinbaseTx;
184 coinbaseTx.vin.resize(1);
185 coinbaseTx.vin[0].prevout.SetNull();
186 coinbaseTx.vout.resize(1);
187 coinbaseTx.vout[0].scriptPubKey = scriptPubKeyIn;
188 coinbaseTx.vout[0].nValue = nFees + GetBlockSubsidy(nHeight, chainparams.GetConsensus());
189 coinbaseTx.vin[0].scriptSig = CScript() << nHeight << OP_0;
190 pblock->vtx[0] = MakeTransactionRef(std::move(coinbaseTx));
191 pblocktemplate->vchCoinbaseCommitment = GenerateCoinbaseCommitment(*pblock, pindexPrev, chainparams.GetConsensus());
192 pblocktemplate->vTxFees[0] = -nFees;
194 uint64_t nSerializeSize = GetSerializeSize(*pblock, SER_NETWORK, PROTOCOL_VERSION);
195 LogPrintf("CreateNewBlock(): total size: %u block weight: %u txs: %u fees: %ld sigops %d\n", nSerializeSize, GetBlockWeight(*pblock), nBlockTx, nFees, nBlockSigOpsCost);
197 // Fill in header
198 pblock->hashPrevBlock = pindexPrev->GetBlockHash();
199 UpdateTime(pblock, chainparams.GetConsensus(), pindexPrev);
200 pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, chainparams.GetConsensus());
201 pblock->nNonce = 0;
202 pblocktemplate->vTxSigOpsCost[0] = WITNESS_SCALE_FACTOR * GetLegacySigOpCount(*pblock->vtx[0]);
204 CValidationState state;
205 if (!TestBlockValidity(state, chainparams, *pblock, pindexPrev, false, false)) {
206 throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", __func__, FormatStateMessage(state)));
208 int64_t nTime2 = GetTimeMicros();
210 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));
212 return std::move(pblocktemplate);
215 void BlockAssembler::onlyUnconfirmed(CTxMemPool::setEntries& testSet)
217 for (CTxMemPool::setEntries::iterator iit = testSet.begin(); iit != testSet.end(); ) {
218 // Only test txs not already in the block
219 if (inBlock.count(*iit)) {
220 testSet.erase(iit++);
222 else {
223 iit++;
228 bool BlockAssembler::TestPackage(uint64_t packageSize, int64_t packageSigOpsCost) const
230 // TODO: switch to weight-based accounting for packages instead of vsize-based accounting.
231 if (nBlockWeight + WITNESS_SCALE_FACTOR * packageSize >= nBlockMaxWeight)
232 return false;
233 if (nBlockSigOpsCost + packageSigOpsCost >= MAX_BLOCK_SIGOPS_COST)
234 return false;
235 return true;
238 // Perform transaction-level checks before adding to block:
239 // - transaction finality (locktime)
240 // - premature witness (in case segwit transactions are added to mempool before
241 // segwit activation)
242 // - serialized size (in case -blockmaxsize is in use)
243 bool BlockAssembler::TestPackageTransactions(const CTxMemPool::setEntries& package)
245 uint64_t nPotentialBlockSize = nBlockSize; // only used with fNeedSizeAccounting
246 for (const CTxMemPool::txiter it : package) {
247 if (!IsFinalTx(it->GetTx(), nHeight, nLockTimeCutoff))
248 return false;
249 if (!fIncludeWitness && it->GetTx().HasWitness())
250 return false;
251 if (fNeedSizeAccounting) {
252 uint64_t nTxSize = ::GetSerializeSize(it->GetTx(), SER_NETWORK, PROTOCOL_VERSION);
253 if (nPotentialBlockSize + nTxSize >= nBlockMaxSize) {
254 return false;
256 nPotentialBlockSize += nTxSize;
259 return true;
262 void BlockAssembler::AddToBlock(CTxMemPool::txiter iter)
264 pblock->vtx.emplace_back(iter->GetSharedTx());
265 pblocktemplate->vTxFees.push_back(iter->GetFee());
266 pblocktemplate->vTxSigOpsCost.push_back(iter->GetSigOpCost());
267 if (fNeedSizeAccounting) {
268 nBlockSize += ::GetSerializeSize(iter->GetTx(), SER_NETWORK, PROTOCOL_VERSION);
270 nBlockWeight += iter->GetTxWeight();
271 ++nBlockTx;
272 nBlockSigOpsCost += iter->GetSigOpCost();
273 nFees += iter->GetFee();
274 inBlock.insert(iter);
276 bool fPrintPriority = gArgs.GetBoolArg("-printpriority", DEFAULT_PRINTPRIORITY);
277 if (fPrintPriority) {
278 LogPrintf("fee %s txid %s\n",
279 CFeeRate(iter->GetModifiedFee(), iter->GetTxSize()).ToString(),
280 iter->GetTx().GetHash().ToString());
284 int BlockAssembler::UpdatePackagesForAdded(const CTxMemPool::setEntries& alreadyAdded,
285 indexed_modified_transaction_set &mapModifiedTx)
287 int nDescendantsUpdated = 0;
288 for (const CTxMemPool::txiter it : alreadyAdded) {
289 CTxMemPool::setEntries descendants;
290 mempool.CalculateDescendants(it, descendants);
291 // Insert all descendants (not yet in block) into the modified set
292 for (CTxMemPool::txiter desc : descendants) {
293 if (alreadyAdded.count(desc))
294 continue;
295 ++nDescendantsUpdated;
296 modtxiter mit = mapModifiedTx.find(desc);
297 if (mit == mapModifiedTx.end()) {
298 CTxMemPoolModifiedEntry modEntry(desc);
299 modEntry.nSizeWithAncestors -= it->GetTxSize();
300 modEntry.nModFeesWithAncestors -= it->GetModifiedFee();
301 modEntry.nSigOpCostWithAncestors -= it->GetSigOpCost();
302 mapModifiedTx.insert(modEntry);
303 } else {
304 mapModifiedTx.modify(mit, update_for_parent_inclusion(it));
308 return nDescendantsUpdated;
311 // Skip entries in mapTx that are already in a block or are present
312 // in mapModifiedTx (which implies that the mapTx ancestor state is
313 // stale due to ancestor inclusion in the block)
314 // Also skip transactions that we've already failed to add. This can happen if
315 // we consider a transaction in mapModifiedTx and it fails: we can then
316 // potentially consider it again while walking mapTx. It's currently
317 // guaranteed to fail again, but as a belt-and-suspenders check we put it in
318 // failedTx and avoid re-evaluation, since the re-evaluation would be using
319 // cached size/sigops/fee values that are not actually correct.
320 bool BlockAssembler::SkipMapTxEntry(CTxMemPool::txiter it, indexed_modified_transaction_set &mapModifiedTx, CTxMemPool::setEntries &failedTx)
322 assert (it != mempool.mapTx.end());
323 return mapModifiedTx.count(it) || inBlock.count(it) || failedTx.count(it);
326 void BlockAssembler::SortForBlock(const CTxMemPool::setEntries& package, CTxMemPool::txiter entry, std::vector<CTxMemPool::txiter>& sortedEntries)
328 // Sort package by ancestor count
329 // If a transaction A depends on transaction B, then A's ancestor count
330 // must be greater than B's. So this is sufficient to validly order the
331 // transactions for block inclusion.
332 sortedEntries.clear();
333 sortedEntries.insert(sortedEntries.begin(), package.begin(), package.end());
334 std::sort(sortedEntries.begin(), sortedEntries.end(), CompareTxIterByAncestorCount());
337 // This transaction selection algorithm orders the mempool based
338 // on feerate of a transaction including all unconfirmed ancestors.
339 // Since we don't remove transactions from the mempool as we select them
340 // for block inclusion, we need an alternate method of updating the feerate
341 // of a transaction with its not-yet-selected ancestors as we go.
342 // This is accomplished by walking the in-mempool descendants of selected
343 // transactions and storing a temporary modified state in mapModifiedTxs.
344 // Each time through the loop, we compare the best transaction in
345 // mapModifiedTxs with the next transaction in the mempool to decide what
346 // transaction package to work on next.
347 void BlockAssembler::addPackageTxs(int &nPackagesSelected, int &nDescendantsUpdated)
349 // mapModifiedTx will store sorted packages after they are modified
350 // because some of their txs are already in the block
351 indexed_modified_transaction_set mapModifiedTx;
352 // Keep track of entries that failed inclusion, to avoid duplicate work
353 CTxMemPool::setEntries failedTx;
355 // Start by adding all descendants of previously added txs to mapModifiedTx
356 // and modifying them for their already included ancestors
357 UpdatePackagesForAdded(inBlock, mapModifiedTx);
359 CTxMemPool::indexed_transaction_set::index<ancestor_score>::type::iterator mi = mempool.mapTx.get<ancestor_score>().begin();
360 CTxMemPool::txiter iter;
362 // Limit the number of attempts to add transactions to the block when it is
363 // close to full; this is just a simple heuristic to finish quickly if the
364 // mempool has a lot of entries.
365 const int64_t MAX_CONSECUTIVE_FAILURES = 1000;
366 int64_t nConsecutiveFailed = 0;
368 while (mi != mempool.mapTx.get<ancestor_score>().end() || !mapModifiedTx.empty())
370 // First try to find a new transaction in mapTx to evaluate.
371 if (mi != mempool.mapTx.get<ancestor_score>().end() &&
372 SkipMapTxEntry(mempool.mapTx.project<0>(mi), mapModifiedTx, failedTx)) {
373 ++mi;
374 continue;
377 // Now that mi is not stale, determine which transaction to evaluate:
378 // the next entry from mapTx, or the best from mapModifiedTx?
379 bool fUsingModified = false;
381 modtxscoreiter modit = mapModifiedTx.get<ancestor_score>().begin();
382 if (mi == mempool.mapTx.get<ancestor_score>().end()) {
383 // We're out of entries in mapTx; use the entry from mapModifiedTx
384 iter = modit->iter;
385 fUsingModified = true;
386 } else {
387 // Try to compare the mapTx entry to the mapModifiedTx entry
388 iter = mempool.mapTx.project<0>(mi);
389 if (modit != mapModifiedTx.get<ancestor_score>().end() &&
390 CompareModifiedEntry()(*modit, CTxMemPoolModifiedEntry(iter))) {
391 // The best entry in mapModifiedTx has higher score
392 // than the one from mapTx.
393 // Switch which transaction (package) to consider
394 iter = modit->iter;
395 fUsingModified = true;
396 } else {
397 // Either no entry in mapModifiedTx, or it's worse than mapTx.
398 // Increment mi for the next loop iteration.
399 ++mi;
403 // We skip mapTx entries that are inBlock, and mapModifiedTx shouldn't
404 // contain anything that is inBlock.
405 assert(!inBlock.count(iter));
407 uint64_t packageSize = iter->GetSizeWithAncestors();
408 CAmount packageFees = iter->GetModFeesWithAncestors();
409 int64_t packageSigOpsCost = iter->GetSigOpCostWithAncestors();
410 if (fUsingModified) {
411 packageSize = modit->nSizeWithAncestors;
412 packageFees = modit->nModFeesWithAncestors;
413 packageSigOpsCost = modit->nSigOpCostWithAncestors;
416 if (packageFees < blockMinFeeRate.GetFee(packageSize)) {
417 // Everything else we might consider has a lower fee rate
418 return;
421 if (!TestPackage(packageSize, packageSigOpsCost)) {
422 if (fUsingModified) {
423 // Since we always look at the best entry in mapModifiedTx,
424 // we must erase failed entries so that we can consider the
425 // next best entry on the next loop iteration
426 mapModifiedTx.get<ancestor_score>().erase(modit);
427 failedTx.insert(iter);
430 ++nConsecutiveFailed;
432 if (nConsecutiveFailed > MAX_CONSECUTIVE_FAILURES && nBlockWeight >
433 nBlockMaxWeight - 4000) {
434 // Give up if we're close to full and haven't succeeded in a while
435 break;
437 continue;
440 CTxMemPool::setEntries ancestors;
441 uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
442 std::string dummy;
443 mempool.CalculateMemPoolAncestors(*iter, ancestors, nNoLimit, nNoLimit, nNoLimit, nNoLimit, dummy, false);
445 onlyUnconfirmed(ancestors);
446 ancestors.insert(iter);
448 // Test if all tx's are Final
449 if (!TestPackageTransactions(ancestors)) {
450 if (fUsingModified) {
451 mapModifiedTx.get<ancestor_score>().erase(modit);
452 failedTx.insert(iter);
454 continue;
457 // This transaction will make it in; reset the failed counter.
458 nConsecutiveFailed = 0;
460 // Package can be added. Sort the entries in a valid order.
461 std::vector<CTxMemPool::txiter> sortedEntries;
462 SortForBlock(ancestors, iter, sortedEntries);
464 for (size_t i=0; i<sortedEntries.size(); ++i) {
465 AddToBlock(sortedEntries[i]);
466 // Erase from the modified set, if present
467 mapModifiedTx.erase(sortedEntries[i]);
470 ++nPackagesSelected;
472 // Update transactions that depend on each of these
473 nDescendantsUpdated += UpdatePackagesForAdded(ancestors, mapModifiedTx);
477 void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce)
479 // Update nExtraNonce
480 static uint256 hashPrevBlock;
481 if (hashPrevBlock != pblock->hashPrevBlock)
483 nExtraNonce = 0;
484 hashPrevBlock = pblock->hashPrevBlock;
486 ++nExtraNonce;
487 unsigned int nHeight = pindexPrev->nHeight+1; // Height first in coinbase required for block.version=2
488 CMutableTransaction txCoinbase(*pblock->vtx[0]);
489 txCoinbase.vin[0].scriptSig = (CScript() << nHeight << CScriptNum(nExtraNonce)) + COINBASE_FLAGS;
490 assert(txCoinbase.vin[0].scriptSig.size() <= 100);
492 pblock->vtx[0] = MakeTransactionRef(std::move(txCoinbase));
493 pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);