Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : // Copyright (c) 2009-2020 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.
5 :
6 : #include <miner.h>
7 :
8 : #include <amount.h>
9 : #include <chain.h>
10 : #include <chainparams.h>
11 : #include <coins.h>
12 : #include <consensus/consensus.h>
13 : #include <consensus/merkle.h>
14 : #include <consensus/tx_verify.h>
15 : #include <consensus/validation.h>
16 : #include <policy/feerate.h>
17 : #include <policy/policy.h>
18 : #include <pow.h>
19 : #include <primitives/transaction.h>
20 : #include <timedata.h>
21 : #include <util/moneystr.h>
22 : #include <util/system.h>
23 :
24 : #include <algorithm>
25 : #include <utility>
26 :
27 21027 : int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
28 : {
29 21027 : int64_t nOldTime = pblock->nTime;
30 21027 : int64_t nNewTime = std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());
31 :
32 21027 : if (nOldTime < nNewTime)
33 17647 : pblock->nTime = nNewTime;
34 :
35 : // Updating time can change work required on testnet:
36 21027 : if (consensusParams.fPowAllowMinDifficultyBlocks)
37 21008 : pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, consensusParams);
38 :
39 21027 : return nNewTime - nOldTime;
40 : }
41 :
42 2355 : void RegenerateCommitments(CBlock& block)
43 : {
44 2355 : CMutableTransaction tx{*block.vtx.at(0)};
45 2355 : tx.vout.erase(tx.vout.begin() + GetWitnessCommitmentIndex(block));
46 2355 : block.vtx.at(0) = MakeTransactionRef(tx);
47 :
48 4710 : GenerateCoinbaseCommitment(block, WITH_LOCK(cs_main, return LookupBlockIndex(block.hashPrevBlock)), Params().GetConsensus());
49 :
50 2355 : block.hashMerkleRoot = BlockMerkleRoot(block);
51 2355 : }
52 :
53 42022 : BlockAssembler::Options::Options() {
54 21011 : blockMinFeeRate = CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE);
55 21011 : nBlockMaxWeight = DEFAULT_BLOCK_MAX_WEIGHT;
56 42022 : }
57 :
58 42022 : BlockAssembler::BlockAssembler(const CTxMemPool& mempool, const CChainParams& params, const Options& options)
59 21011 : : chainparams(params),
60 21011 : m_mempool(mempool)
61 21011 : {
62 21011 : blockMinFeeRate = options.blockMinFeeRate;
63 : // Limit weight to between 4K and MAX_BLOCK_WEIGHT-4K for sanity:
64 21011 : nBlockMaxWeight = std::max<size_t>(4000, std::min<size_t>(MAX_BLOCK_WEIGHT - 4000, options.nBlockMaxWeight));
65 42022 : }
66 :
67 20992 : static BlockAssembler::Options DefaultOptions()
68 : {
69 : // Block resource limits
70 : // If -blockmaxweight is not given, limit to DEFAULT_BLOCK_MAX_WEIGHT
71 20992 : BlockAssembler::Options options;
72 20992 : options.nBlockMaxWeight = gArgs.GetArg("-blockmaxweight", DEFAULT_BLOCK_MAX_WEIGHT);
73 20992 : CAmount n = 0;
74 20992 : if (gArgs.IsArgSet("-blockmintxfee") && ParseMoney(gArgs.GetArg("-blockmintxfee", ""), n)) {
75 0 : options.blockMinFeeRate = CFeeRate(n);
76 0 : } else {
77 20992 : options.blockMinFeeRate = CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE);
78 : }
79 : return options;
80 20992 : }
81 :
82 20992 : BlockAssembler::BlockAssembler(const CTxMemPool& mempool, const CChainParams& params)
83 20992 : : BlockAssembler(mempool, params, DefaultOptions()) {}
84 :
85 21011 : void BlockAssembler::resetBlock()
86 : {
87 21011 : inBlock.clear();
88 :
89 : // Reserve space for coinbase tx
90 21011 : nBlockWeight = 4000;
91 21011 : nBlockSigOpsCost = 400;
92 21011 : fIncludeWitness = false;
93 :
94 : // These counters do not include coinbase tx
95 21011 : nBlockTx = 0;
96 21011 : nFees = 0;
97 21011 : }
98 :
99 640 : Optional<int64_t> BlockAssembler::m_last_block_num_txs{nullopt};
100 640 : Optional<int64_t> BlockAssembler::m_last_block_weight{nullopt};
101 :
102 21011 : std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn)
103 : {
104 21011 : int64_t nTimeStart = GetTimeMicros();
105 :
106 21011 : resetBlock();
107 :
108 21011 : pblocktemplate.reset(new CBlockTemplate());
109 :
110 21011 : if(!pblocktemplate.get())
111 0 : return nullptr;
112 21011 : CBlock* const pblock = &pblocktemplate->block; // pointer for convenience
113 :
114 : // Add dummy coinbase tx as first transaction
115 21011 : pblock->vtx.emplace_back();
116 21011 : pblocktemplate->vTxFees.push_back(-1); // updated at end
117 21011 : pblocktemplate->vTxSigOpsCost.push_back(-1); // updated at end
118 :
119 21011 : LOCK2(cs_main, m_mempool.cs);
120 21011 : CBlockIndex* pindexPrev = ::ChainActive().Tip();
121 21011 : assert(pindexPrev != nullptr);
122 21011 : nHeight = pindexPrev->nHeight + 1;
123 :
124 21011 : pblock->nVersion = ComputeBlockVersion(pindexPrev, chainparams.GetConsensus());
125 : // -regtest only: allow overriding block.nVersion with
126 : // -blockversion=N to test forking scenarios
127 21011 : if (chainparams.MineBlocksOnDemand())
128 20992 : pblock->nVersion = gArgs.GetArg("-blockversion", pblock->nVersion);
129 :
130 21011 : pblock->nTime = GetAdjustedTime();
131 21011 : const int64_t nMedianTimePast = pindexPrev->GetMedianTimePast();
132 :
133 21011 : nLockTimeCutoff = (STANDARD_LOCKTIME_VERIFY_FLAGS & LOCKTIME_MEDIAN_TIME_PAST)
134 : ? nMedianTimePast
135 : : pblock->GetBlockTime();
136 :
137 : // Decide whether to include witness transactions
138 : // This is only needed in case the witness softfork activation is reverted
139 : // (which would require a very deep reorganization).
140 : // Note that the mempool would accept transactions with witness data before
141 : // IsWitnessEnabled, but we would only ever mine blocks after IsWitnessEnabled
142 : // unless there is a massive block reorganization with the witness softfork
143 : // not activated.
144 : // TODO: replace this with a call to main to assess validity of a mempool
145 : // transaction (which in most cases can be a no-op).
146 21011 : fIncludeWitness = IsWitnessEnabled(pindexPrev, chainparams.GetConsensus());
147 :
148 21011 : int nPackagesSelected = 0;
149 21011 : int nDescendantsUpdated = 0;
150 21011 : addPackageTxs(nPackagesSelected, nDescendantsUpdated);
151 :
152 21011 : int64_t nTime1 = GetTimeMicros();
153 :
154 21011 : m_last_block_num_txs = nBlockTx;
155 21011 : m_last_block_weight = nBlockWeight;
156 :
157 : // Create coinbase transaction.
158 21011 : CMutableTransaction coinbaseTx;
159 21011 : coinbaseTx.vin.resize(1);
160 21011 : coinbaseTx.vin[0].prevout.SetNull();
161 21011 : coinbaseTx.vout.resize(1);
162 21011 : coinbaseTx.vout[0].scriptPubKey = scriptPubKeyIn;
163 21011 : coinbaseTx.vout[0].nValue = nFees + GetBlockSubsidy(nHeight, chainparams.GetConsensus());
164 21011 : coinbaseTx.vin[0].scriptSig = CScript() << nHeight << OP_0;
165 21011 : pblock->vtx[0] = MakeTransactionRef(std::move(coinbaseTx));
166 21011 : pblocktemplate->vchCoinbaseCommitment = GenerateCoinbaseCommitment(*pblock, pindexPrev, chainparams.GetConsensus());
167 21011 : pblocktemplate->vTxFees[0] = -nFees;
168 :
169 21011 : LogPrintf("CreateNewBlock(): block weight: %u txs: %u fees: %ld sigops %d\n", GetBlockWeight(*pblock), nBlockTx, nFees, nBlockSigOpsCost);
170 :
171 : // Fill in header
172 21011 : pblock->hashPrevBlock = pindexPrev->GetBlockHash();
173 21011 : UpdateTime(pblock, chainparams.GetConsensus(), pindexPrev);
174 21011 : pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, chainparams.GetConsensus());
175 21011 : pblock->nNonce = 0;
176 21011 : pblocktemplate->vTxSigOpsCost[0] = WITNESS_SCALE_FACTOR * GetLegacySigOpCount(*pblock->vtx[0]);
177 :
178 21011 : BlockValidationState state;
179 21011 : if (!TestBlockValidity(state, chainparams, *pblock, pindexPrev, false, false)) {
180 5 : throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", __func__, state.ToString()));
181 : }
182 21006 : int64_t nTime2 = GetTimeMicros();
183 :
184 21006 : 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));
185 :
186 21006 : return std::move(pblocktemplate);
187 21011 : }
188 :
189 9345 : void BlockAssembler::onlyUnconfirmed(CTxMemPool::setEntries& testSet)
190 : {
191 22604 : for (CTxMemPool::setEntries::iterator iit = testSet.begin(); iit != testSet.end(); ) {
192 : // Only test txs not already in the block
193 13259 : if (inBlock.count(*iit)) {
194 10950 : testSet.erase(iit++);
195 10950 : }
196 : else {
197 2309 : iit++;
198 : }
199 : }
200 9345 : }
201 :
202 34267 : bool BlockAssembler::TestPackage(uint64_t packageSize, int64_t packageSigOpsCost) const
203 : {
204 : // TODO: switch to weight-based accounting for packages instead of vsize-based accounting.
205 34267 : if (nBlockWeight + WITNESS_SCALE_FACTOR * packageSize >= nBlockMaxWeight)
206 24909 : return false;
207 9358 : if (nBlockSigOpsCost + packageSigOpsCost >= MAX_BLOCK_SIGOPS_COST)
208 13 : return false;
209 9345 : return true;
210 34267 : }
211 :
212 : // Perform transaction-level checks before adding to block:
213 : // - transaction finality (locktime)
214 : // - premature witness (in case segwit transactions are added to mempool before
215 : // segwit activation)
216 9345 : bool BlockAssembler::TestPackageTransactions(const CTxMemPool::setEntries& package)
217 : {
218 20999 : for (CTxMemPool::txiter it : package) {
219 11654 : if (!IsFinalTx(it->GetTx(), nHeight, nLockTimeCutoff))
220 2 : return false;
221 11652 : if (!fIncludeWitness && it->GetTx().HasWitness())
222 26 : return false;
223 11654 : }
224 9317 : return true;
225 9345 : }
226 :
227 11626 : void BlockAssembler::AddToBlock(CTxMemPool::txiter iter)
228 : {
229 11626 : pblocktemplate->block.vtx.emplace_back(iter->GetSharedTx());
230 11626 : pblocktemplate->vTxFees.push_back(iter->GetFee());
231 11626 : pblocktemplate->vTxSigOpsCost.push_back(iter->GetSigOpCost());
232 11626 : nBlockWeight += iter->GetTxWeight();
233 11626 : ++nBlockTx;
234 11626 : nBlockSigOpsCost += iter->GetSigOpCost();
235 11626 : nFees += iter->GetFee();
236 11626 : inBlock.insert(iter);
237 :
238 11626 : bool fPrintPriority = gArgs.GetBoolArg("-printpriority", DEFAULT_PRINTPRIORITY);
239 11626 : if (fPrintPriority) {
240 129 : LogPrintf("fee %s txid %s\n",
241 129 : CFeeRate(iter->GetModifiedFee(), iter->GetTxSize()).ToString(),
242 129 : iter->GetTx().GetHash().ToString());
243 129 : }
244 11626 : }
245 :
246 30328 : int BlockAssembler::UpdatePackagesForAdded(const CTxMemPool::setEntries& alreadyAdded,
247 : indexed_modified_transaction_set &mapModifiedTx)
248 : {
249 1077865 : int nDescendantsUpdated = 0;
250 41954 : for (CTxMemPool::txiter it : alreadyAdded) {
251 11626 : CTxMemPool::setEntries descendants;
252 11626 : m_mempool.CalculateDescendants(it, descendants);
253 : // Insert all descendants (not yet in block) into the modified set
254 1035911 : for (CTxMemPool::txiter desc : descendants) {
255 1024285 : if (alreadyAdded.count(desc))
256 241098 : continue;
257 783187 : ++nDescendantsUpdated;
258 783187 : modtxiter mit = mapModifiedTx.find(desc);
259 783187 : if (mit == mapModifiedTx.end()) {
260 2511 : CTxMemPoolModifiedEntry modEntry(desc);
261 2511 : modEntry.nSizeWithAncestors -= it->GetTxSize();
262 2511 : modEntry.nModFeesWithAncestors -= it->GetModifiedFee();
263 2511 : modEntry.nSigOpCostWithAncestors -= it->GetSigOpCost();
264 2511 : mapModifiedTx.insert(modEntry);
265 2511 : } else {
266 780676 : mapModifiedTx.modify(mit, update_for_parent_inclusion(it));
267 : }
268 1024285 : }
269 11626 : }
270 30328 : return nDescendantsUpdated;
271 0 : }
272 :
273 : // Skip entries in mapTx that are already in a block or are present
274 : // in mapModifiedTx (which implies that the mapTx ancestor state is
275 : // stale due to ancestor inclusion in the block)
276 : // Also skip transactions that we've already failed to add. This can happen if
277 : // we consider a transaction in mapModifiedTx and it fails: we can then
278 : // potentially consider it again while walking mapTx. It's currently
279 : // guaranteed to fail again, but as a belt-and-suspenders check we put it in
280 : // failedTx and avoid re-evaluation, since the re-evaluation would be using
281 : // cached size/sigops/fee values that are not actually correct.
282 36605 : bool BlockAssembler::SkipMapTxEntry(CTxMemPool::txiter it, indexed_modified_transaction_set &mapModifiedTx, CTxMemPool::setEntries &failedTx)
283 : {
284 36605 : assert(it != m_mempool.mapTx.end());
285 36605 : return mapModifiedTx.count(it) || inBlock.count(it) || failedTx.count(it);
286 : }
287 :
288 9317 : void BlockAssembler::SortForBlock(const CTxMemPool::setEntries& package, std::vector<CTxMemPool::txiter>& sortedEntries)
289 : {
290 : // Sort package by ancestor count
291 : // If a transaction A depends on transaction B, then A's ancestor count
292 : // must be greater than B's. So this is sufficient to validly order the
293 : // transactions for block inclusion.
294 9317 : sortedEntries.clear();
295 9317 : sortedEntries.insert(sortedEntries.begin(), package.begin(), package.end());
296 9317 : std::sort(sortedEntries.begin(), sortedEntries.end(), CompareTxIterByAncestorCount());
297 9317 : }
298 :
299 : // This transaction selection algorithm orders the mempool based
300 : // on feerate of a transaction including all unconfirmed ancestors.
301 : // Since we don't remove transactions from the mempool as we select them
302 : // for block inclusion, we need an alternate method of updating the feerate
303 : // of a transaction with its not-yet-selected ancestors as we go.
304 : // This is accomplished by walking the in-mempool descendants of selected
305 : // transactions and storing a temporary modified state in mapModifiedTxs.
306 : // Each time through the loop, we compare the best transaction in
307 : // mapModifiedTxs with the next transaction in the mempool to decide what
308 : // transaction package to work on next.
309 21011 : void BlockAssembler::addPackageTxs(int &nPackagesSelected, int &nDescendantsUpdated)
310 : {
311 : // mapModifiedTx will store sorted packages after they are modified
312 : // because some of their txs are already in the block
313 21011 : indexed_modified_transaction_set mapModifiedTx;
314 : // Keep track of entries that failed inclusion, to avoid duplicate work
315 21011 : CTxMemPool::setEntries failedTx;
316 :
317 : // Start by adding all descendants of previously added txs to mapModifiedTx
318 : // and modifying them for their already included ancestors
319 21011 : UpdatePackagesForAdded(inBlock, mapModifiedTx);
320 :
321 21011 : CTxMemPool::indexed_transaction_set::index<ancestor_score>::type::iterator mi = m_mempool.mapTx.get<ancestor_score>().begin();
322 21011 : CTxMemPool::txiter iter;
323 :
324 : // Limit the number of attempts to add transactions to the block when it is
325 : // close to full; this is just a simple heuristic to finish quickly if the
326 : // mempool has a lot of entries.
327 : const int64_t MAX_CONSECUTIVE_FAILURES = 1000;
328 58020 : int64_t nConsecutiveFailed = 0;
329 :
330 58020 : while (mi != m_mempool.mapTx.get<ancestor_score>().end() || !mapModifiedTx.empty()) {
331 : // First try to find a new transaction in mapTx to evaluate.
332 73627 : if (mi != m_mempool.mapTx.get<ancestor_score>().end() &&
333 36605 : SkipMapTxEntry(m_mempool.mapTx.project<0>(mi), mapModifiedTx, failedTx)) {
334 2742 : ++mi;
335 : continue;
336 : }
337 :
338 : // Now that mi is not stale, determine which transaction to evaluate:
339 : // the next entry from mapTx, or the best from mapModifiedTx?
340 : bool fUsingModified = false;
341 :
342 34280 : modtxscoreiter modit = mapModifiedTx.get<ancestor_score>().begin();
343 34280 : if (mi == m_mempool.mapTx.get<ancestor_score>().end()) {
344 : // We're out of entries in mapTx; use the entry from mapModifiedTx
345 417 : iter = modit->iter;
346 : fUsingModified = true;
347 417 : } else {
348 : // Try to compare the mapTx entry to the mapModifiedTx entry
349 33863 : iter = m_mempool.mapTx.project<0>(mi);
350 33956 : if (modit != mapModifiedTx.get<ancestor_score>().end() &&
351 93 : CompareTxMemPoolEntryByAncestorFee()(*modit, CTxMemPoolModifiedEntry(iter))) {
352 : // The best entry in mapModifiedTx has higher score
353 : // than the one from mapTx.
354 : // Switch which transaction (package) to consider
355 22 : iter = modit->iter;
356 : fUsingModified = true;
357 22 : } else {
358 : // Either no entry in mapModifiedTx, or it's worse than mapTx.
359 : // Increment mi for the next loop iteration.
360 33841 : ++mi;
361 : }
362 : }
363 :
364 : // We skip mapTx entries that are inBlock, and mapModifiedTx shouldn't
365 : // contain anything that is inBlock.
366 34280 : assert(!inBlock.count(iter));
367 :
368 34280 : uint64_t packageSize = iter->GetSizeWithAncestors();
369 34280 : CAmount packageFees = iter->GetModFeesWithAncestors();
370 34280 : int64_t packageSigOpsCost = iter->GetSigOpCostWithAncestors();
371 34280 : if (fUsingModified) {
372 439 : packageSize = modit->nSizeWithAncestors;
373 439 : packageFees = modit->nModFeesWithAncestors;
374 439 : packageSigOpsCost = modit->nSigOpCostWithAncestors;
375 439 : }
376 :
377 34280 : if (packageFees < blockMinFeeRate.GetFee(packageSize)) {
378 : // Everything else we might consider has a lower fee rate
379 13 : return;
380 : }
381 :
382 34267 : if (!TestPackage(packageSize, packageSigOpsCost)) {
383 24922 : if (fUsingModified) {
384 : // Since we always look at the best entry in mapModifiedTx,
385 : // we must erase failed entries so that we can consider the
386 : // next best entry on the next loop iteration
387 36 : mapModifiedTx.get<ancestor_score>().erase(modit);
388 36 : failedTx.insert(iter);
389 36 : }
390 :
391 24922 : ++nConsecutiveFailed;
392 :
393 24922 : if (nConsecutiveFailed > MAX_CONSECUTIVE_FAILURES && nBlockWeight >
394 0 : nBlockMaxWeight - 4000) {
395 : // Give up if we're close to full and haven't succeeded in a while
396 0 : break;
397 : }
398 24922 : continue;
399 : }
400 :
401 9345 : CTxMemPool::setEntries ancestors;
402 9345 : uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
403 9345 : std::string dummy;
404 9345 : m_mempool.CalculateMemPoolAncestors(*iter, ancestors, nNoLimit, nNoLimit, nNoLimit, nNoLimit, dummy, false);
405 :
406 9345 : onlyUnconfirmed(ancestors);
407 9345 : ancestors.insert(iter);
408 :
409 : // Test if all tx's are Final
410 9345 : if (!TestPackageTransactions(ancestors)) {
411 28 : if (fUsingModified) {
412 0 : mapModifiedTx.get<ancestor_score>().erase(modit);
413 0 : failedTx.insert(iter);
414 0 : }
415 28 : continue;
416 : }
417 :
418 : // This transaction will make it in; reset the failed counter.
419 : nConsecutiveFailed = 0;
420 :
421 : // Package can be added. Sort the entries in a valid order.
422 9317 : std::vector<CTxMemPool::txiter> sortedEntries;
423 9317 : SortForBlock(ancestors, sortedEntries);
424 :
425 20943 : for (size_t i=0; i<sortedEntries.size(); ++i) {
426 11626 : AddToBlock(sortedEntries[i]);
427 : // Erase from the modified set, if present
428 11626 : mapModifiedTx.erase(sortedEntries[i]);
429 : }
430 :
431 9317 : ++nPackagesSelected;
432 :
433 : // Update transactions that depend on each of these
434 9317 : nDescendantsUpdated += UpdatePackagesForAdded(ancestors, mapModifiedTx);
435 34280 : }
436 21011 : }
437 :
438 16687 : void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce)
439 : {
440 : // Update nExtraNonce
441 16687 : static uint256 hashPrevBlock;
442 16687 : if (hashPrevBlock != pblock->hashPrevBlock)
443 : {
444 16685 : nExtraNonce = 0;
445 16685 : hashPrevBlock = pblock->hashPrevBlock;
446 16685 : }
447 16687 : ++nExtraNonce;
448 16687 : unsigned int nHeight = pindexPrev->nHeight+1; // Height first in coinbase required for block.version=2
449 16687 : CMutableTransaction txCoinbase(*pblock->vtx[0]);
450 16687 : txCoinbase.vin[0].scriptSig = (CScript() << nHeight << CScriptNum(nExtraNonce));
451 16687 : assert(txCoinbase.vin[0].scriptSig.size() <= 100);
452 :
453 16687 : pblock->vtx[0] = MakeTransactionRef(std::move(txCoinbase));
454 16687 : pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);
455 16687 : }
|