Branch data Line data Source code
1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-present 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 <node/miner.h>
7 : :
8 : : #include <chain.h>
9 : : #include <chainparams.h>
10 : : #include <common/args.h>
11 : : #include <consensus/amount.h>
12 : : #include <consensus/consensus.h>
13 : : #include <consensus/merkle.h>
14 : : #include <consensus/params.h>
15 : : #include <consensus/tx_verify.h>
16 : : #include <consensus/validation.h>
17 : : #include <node/blockstorage.h>
18 : : #include <node/mining_args.h>
19 : : #include <node/mining_types.h>
20 : : #include <policy/feerate.h>
21 : : #include <policy/policy.h>
22 : : #include <pow.h>
23 : : #include <primitives/block.h>
24 : : #include <primitives/transaction.h>
25 : : #include <script/script.h>
26 : : #include <sync.h>
27 : : #include <tinyformat.h>
28 : : #include <txgraph.h>
29 : : #include <txmempool.h>
30 : : #include <uint256.h>
31 : : #include <util/check.h>
32 : : #include <util/feefrac.h>
33 : : #include <util/log.h>
34 : : #include <util/result.h>
35 : : #include <util/time.h>
36 : : #include <util/translation.h>
37 : : #include <validation.h>
38 : : #include <versionbits.h>
39 : :
40 : : #include <algorithm>
41 : : #include <cstddef>
42 : : #include <functional>
43 : : #include <span>
44 : : #include <stdexcept>
45 : : #include <string>
46 : : #include <utility>
47 : :
48 : : namespace node {
49 : :
50 : 383571 : int64_t GetMinimumTime(const CBlockIndex* pindexPrev, const int64_t difficulty_adjustment_interval)
51 : : {
52 : 383571 : int64_t min_time{pindexPrev->GetMedianTimePast() + 1};
53 : : // Height of block to be mined.
54 : 383571 : const int height{pindexPrev->nHeight + 1};
55 : : // Account for BIP94 timewarp rule on all networks. This makes future
56 : : // activation safer.
57 [ + + ]: 383571 : if (height % difficulty_adjustment_interval == 0) {
58 [ + - ]: 3464 : min_time = std::max<int64_t>(min_time, pindexPrev->GetBlockTime() - MAX_TIMEWARP);
59 : : }
60 : : // Account for the BIP54 Murch-Zawy rule on all networks: the last block of
61 : : // a difficulty adjustment period may not be earlier than its first block.
62 [ + + ]: 383571 : if (height % difficulty_adjustment_interval == difficulty_adjustment_interval - 1) {
63 : 1498 : const int first_height{height - static_cast<int>(difficulty_adjustment_interval) + 1};
64 [ - + ]: 1498 : const CBlockIndex* first_block{Assert(pindexPrev->GetAncestor(first_height))};
65 [ + - ]: 2996 : min_time = std::max<int64_t>(min_time, first_block->GetBlockTime());
66 : : }
67 : 383571 : return min_time;
68 : : }
69 : :
70 : 383571 : int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
71 : : {
72 : 383571 : int64_t nOldTime = pblock->nTime;
73 [ + + ]: 383571 : int64_t nNewTime{std::max<int64_t>(GetMinimumTime(pindexPrev, consensusParams.DifficultyAdjustmentInterval()),
74 : 383571 : TicksSinceEpoch<std::chrono::seconds>(NodeClock::now()))};
75 : :
76 [ + + ]: 383571 : if (nOldTime < nNewTime) {
77 : 267027 : pblock->nTime = nNewTime;
78 : : }
79 : :
80 : : // Updating time can change work required on testnet:
81 [ + - ]: 383571 : if (consensusParams.fPowAllowMinDifficultyBlocks) {
82 : 383571 : pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, consensusParams);
83 : : }
84 : :
85 : 383571 : return nNewTime - nOldTime;
86 : : }
87 : :
88 : 124916 : void RegenerateCommitments(CBlock& block, ChainstateManager& chainman)
89 : : {
90 : 124916 : CMutableTransaction tx{*block.vtx.at(0)};
91 : 124916 : tx.vout.erase(tx.vout.begin() + GetWitnessCommitmentIndex(block));
92 [ + - + - : 249832 : block.vtx.at(0) = MakeTransactionRef(tx);
- + ]
93 : :
94 [ + - + - : 374748 : const CBlockIndex* prev_block = WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock));
+ - ]
95 [ + - ]: 124916 : chainman.GenerateCoinbaseCommitment(block, prev_block);
96 : :
97 [ + - ]: 124916 : block.hashMerkleRoot = BlockMerkleRoot(block);
98 : 124916 : }
99 : :
100 : 383569 : BlockAssembler::BlockAssembler(Chainstate& chainstate,
101 : : const CTxMemPool* mempool,
102 : 383569 : BlockCreateOptions options)
103 [ - + ]: 383569 : : chainparams{chainstate.m_chainman.GetParams()},
104 [ - + ]: 383569 : m_mempool{options.use_mempool ? mempool : nullptr},
105 : 383569 : m_chainstate{chainstate},
106 [ + - ]: 383569 : m_options{[&] {
107 [ + - - + ]: 767138 : if (auto result{CheckMiningOptions(options, /*use_argnames=*/false)}; !result) {
108 [ # # # # ]: 0 : throw std::runtime_error(util::ErrorString(result).original);
109 : 0 : }
110 [ + - ]: 767138 : return FlattenMiningOptions(std::move(options));
111 [ - + ]: 383569 : }()}
112 : : {
113 : 383569 : }
114 : :
115 : 383569 : void BlockAssembler::resetBlock()
116 : : {
117 : : // Reserve space for fixed-size block header, txs count, and coinbase tx.
118 [ - + ]: 383569 : nBlockWeight = *Assert(m_options.block_reserved_weight);
119 : 383569 : nBlockSigOpsCost = m_options.coinbase_output_max_additional_sigops;
120 : :
121 : : // These counters do not include coinbase tx
122 : 383569 : nBlockTx = 0;
123 : 383569 : nFees = 0;
124 : 383569 : }
125 : :
126 : 383569 : std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock()
127 : : {
128 : 383569 : const auto time_start{SteadyClock::now()};
129 : :
130 : 383569 : resetBlock();
131 : :
132 [ - + ]: 383569 : pblocktemplate.reset(new CBlockTemplate());
133 : 383569 : CBlock* const pblock = &pblocktemplate->block; // pointer for convenience
134 : :
135 : : // Add dummy coinbase tx as first transaction. It is skipped by the
136 : : // getblocktemplate RPC and mining interface consumers must not use it.
137 : 383569 : pblock->vtx.emplace_back();
138 : :
139 : 383569 : LOCK(::cs_main);
140 [ - + ]: 383569 : CBlockIndex* pindexPrev = m_chainstate.m_chain.Tip();
141 [ - + ]: 383569 : assert(pindexPrev != nullptr);
142 : 383569 : nHeight = pindexPrev->nHeight + 1;
143 : :
144 [ + - ]: 383569 : pblock->nVersion = m_chainstate.m_chainman.m_versionbitscache.ComputeBlockVersion(pindexPrev, chainparams.GetConsensus());
145 : : // -regtest only: allow overriding block.nVersion with
146 : : // -blockversion=N to test forking scenarios
147 [ + - ]: 383569 : if (chainparams.MineBlocksOnDemand()) {
148 [ + - ]: 767138 : pblock->nVersion = gArgs.GetIntArg("-blockversion", pblock->nVersion);
149 : : }
150 : :
151 : 383569 : pblock->nTime = TicksSinceEpoch<std::chrono::seconds>(NodeClock::now());
152 : 383569 : m_lock_time_cutoff = pindexPrev->GetMedianTimePast();
153 : :
154 [ + - ]: 383569 : if (m_mempool) {
155 [ + - ]: 383569 : LOCK(m_mempool->cs);
156 : 383569 : m_mempool->StartBlockBuilding();
157 [ + - ]: 383569 : addChunks();
158 [ + - ]: 383569 : m_mempool->StopBlockBuilding();
159 : 383569 : }
160 : :
161 : 383569 : const auto time_1{SteadyClock::now()};
162 : :
163 [ + + ]: 383569 : m_last_block_num_txs = nBlockTx;
164 [ + + ]: 383569 : m_last_block_weight = nBlockWeight;
165 : :
166 : : // Create coinbase transaction.
167 [ + - ]: 383569 : CMutableTransaction coinbaseTx;
168 : :
169 : : // Construct coinbase transaction struct in parallel
170 [ + - ]: 383569 : CoinbaseTx& coinbase_tx{pblocktemplate->m_coinbase_tx};
171 : 383569 : coinbase_tx.version = coinbaseTx.version;
172 : :
173 [ + - ]: 383569 : coinbaseTx.vin.resize(1);
174 : 383569 : coinbaseTx.vin[0].prevout.SetNull();
175 [ + - ]: 383569 : coinbaseTx.vin[0].nSequence = CTxIn::MAX_SEQUENCE_NONFINAL; // Make sure timelock is enforced.
176 : 383569 : coinbase_tx.sequence = coinbaseTx.vin[0].nSequence;
177 : :
178 : : // Add an output that spends the full coinbase reward.
179 [ + - ]: 383569 : coinbaseTx.vout.resize(1);
180 : 383569 : coinbaseTx.vout[0].scriptPubKey = m_options.coinbase_output_script;
181 : : // Block subsidy + fees
182 [ + - ]: 383569 : const CAmount block_reward{nFees + GetBlockSubsidy(nHeight, chainparams.GetConsensus())};
183 [ + - ]: 383569 : coinbaseTx.vout[0].nValue = block_reward;
184 : 383569 : coinbase_tx.block_reward_remaining = block_reward;
185 : :
186 : : // Start the coinbase scriptSig with the block height as required by BIP34.
187 : : // Mining clients are expected to append extra data to this prefix, so
188 : : // increasing its length would reduce the space they can use and may break
189 : : // existing clients.
190 [ + - ]: 383569 : coinbaseTx.vin[0].scriptSig = CScript() << nHeight;
191 : : // Set script_sig_prefix here, so IPC mining clients are not affected by
192 : : // the optional scriptSig padding below. They provide their own extraNonce,
193 : : // and in a typical setup a pool name or realistic extraNonce already makes
194 : : // the scriptSig long enough.
195 : 383569 : coinbase_tx.script_sig_prefix = coinbaseTx.vin[0].scriptSig;
196 [ + + ]: 383569 : if (nHeight <= 16) {
197 : : // For blocks at heights <= 16, the BIP34-encoded height alone is only
198 : : // one byte. Consensus requires coinbase scriptSigs to be at least two
199 : : // bytes long (bad-cb-length), so an OP_0 is always appended at those
200 : : // heights.
201 [ + - ]: 56007 : coinbaseTx.vin[0].scriptSig << OP_0;
202 : : }
203 [ - + ]: 383569 : Assert(nHeight > 0);
204 : 383569 : coinbaseTx.nLockTime = static_cast<uint32_t>(nHeight - 1);
205 : 383569 : coinbase_tx.lock_time = coinbaseTx.nLockTime;
206 : :
207 [ + - - + ]: 767138 : pblock->vtx[0] = MakeTransactionRef(std::move(coinbaseTx));
208 [ + - ]: 383569 : m_chainstate.m_chainman.GenerateCoinbaseCommitment(*pblock, pindexPrev);
209 : :
210 [ + - ]: 383569 : const CTransactionRef& final_coinbase{pblock->vtx[0]};
211 [ + - ]: 383569 : if (final_coinbase->HasWitness()) {
212 [ - + ]: 383569 : const auto& witness_stack{final_coinbase->vin[0].scriptWitness.stack};
213 : : // Consensus requires the coinbase witness stack to have exactly one
214 : : // element of 32 bytes.
215 [ - + + - : 383569 : Assert(witness_stack.size() == 1 && witness_stack[0].size() == 32);
- + - + -
+ ]
216 [ - + - + ]: 383569 : coinbase_tx.witness = uint256(witness_stack[0]);
217 : : }
218 [ + - ]: 383569 : if (const int witness_index = GetWitnessCommitmentIndex(*pblock); witness_index != NO_WITNESS_COMMITMENT) {
219 [ + - - + : 383569 : Assert(witness_index >= 0 && static_cast<size_t>(witness_index) < final_coinbase->vout.size());
- + - + ]
220 [ + - ]: 383569 : coinbase_tx.required_outputs.push_back(final_coinbase->vout[witness_index]);
221 : : }
222 : :
223 [ + - ]: 383569 : LogInfo("CreateNewBlock(): block weight: %u txs: %u fees: %ld sigops %d\n", GetBlockWeight(*pblock), nBlockTx, nFees, nBlockSigOpsCost);
224 : :
225 : : // Fill in header
226 : 383569 : pblock->hashPrevBlock = pindexPrev->GetBlockHash();
227 [ + - ]: 383569 : UpdateTime(pblock, chainparams.GetConsensus(), pindexPrev);
228 [ + - ]: 383569 : pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, chainparams.GetConsensus());
229 : 383569 : pblock->nNonce = 0;
230 : :
231 [ + - ]: 383569 : if (m_options.test_block_validity) {
232 [ + - - + ]: 383569 : if (BlockValidationState state{TestBlockValidity(m_chainstate, *pblock, /*check_pow=*/false, /*check_merkle_root=*/false)}; !state.IsValid()) {
233 [ # # # # : 0 : throw std::runtime_error(strprintf("TestBlockValidity failed: %s", state.ToString()));
# # ]
234 : 383569 : }
235 : : }
236 : 383569 : const auto time_2{SteadyClock::now()};
237 : :
238 [ + - + + : 383569 : LogDebug(BCLog::BENCH, "CreateNewBlock() chunks: %.2fms, validity: %.2fms (total %.2fms)\n",
+ - ]
239 : : Ticks<MillisecondsDouble>(time_1 - time_start),
240 : : Ticks<MillisecondsDouble>(time_2 - time_1),
241 : : Ticks<MillisecondsDouble>(time_2 - time_start));
242 : :
243 : 383569 : return std::move(pblocktemplate);
244 [ + - ]: 767138 : }
245 : :
246 : 61443 : bool BlockAssembler::TestChunkBlockLimits(int64_t chunk_weight, int64_t chunk_sigops_cost) const
247 : : {
248 : : // block_max_weight has been flattened before block assembly limit checks.
249 [ - + ]: 61443 : Assert(m_options.block_max_weight);
250 [ + + ]: 61443 : if (nBlockWeight + chunk_weight >= m_options.block_max_weight) {
251 : : return false;
252 : : }
253 [ - + ]: 45727 : if (nBlockSigOpsCost + chunk_sigops_cost >= MAX_BLOCK_SIGOPS_COST) {
254 : 0 : return false;
255 : : }
256 : : return true;
257 : : }
258 : :
259 : : // Perform transaction-level checks before adding to block:
260 : : // - transaction finality (locktime)
261 : 45727 : bool BlockAssembler::TestChunkTransactions(const std::vector<CTxMemPoolEntryRef>& txs) const
262 : : {
263 [ + + ]: 97229 : for (const auto tx : txs) {
264 [ + - ]: 51502 : if (!IsFinalTx(tx.get().GetTx(), nHeight, m_lock_time_cutoff)) {
265 : : return false;
266 : : }
267 : : }
268 : : return true;
269 : : }
270 : :
271 : 51502 : void BlockAssembler::AddToBlock(const CTxMemPoolEntry& entry)
272 : : {
273 [ + - + - ]: 103004 : pblocktemplate->block.vtx.emplace_back(entry.GetSharedTx());
274 : 51502 : pblocktemplate->vTxFees.push_back(entry.GetFee());
275 : 51502 : pblocktemplate->vTxSigOpsCost.push_back(entry.GetSigOpCost());
276 [ - + ]: 51502 : nBlockWeight += entry.GetTxWeight();
277 : 51502 : ++nBlockTx;
278 [ - + ]: 51502 : nBlockSigOpsCost += entry.GetSigOpCost();
279 : 51502 : nFees += entry.GetFee();
280 : :
281 [ - + ]: 51502 : if (*m_options.print_modified_fee) {
282 [ # # # # : 0 : LogInfo("fee rate %s txid %s\n",
# # # # ]
283 : : CFeeRate(entry.GetModifiedFee(), entry.GetTxSize()).ToString(),
284 : : entry.GetTx().GetHash().ToString());
285 : : }
286 : 51502 : }
287 : :
288 : 383569 : void BlockAssembler::addChunks()
289 : : {
290 : : // Limit the number of attempts to add transactions to the block when it is
291 : : // close to full; this is just a simple heuristic to finish quickly if the
292 : : // mempool has a lot of entries.
293 : 383569 : const int64_t MAX_CONSECUTIVE_FAILURES = 1000;
294 : 383569 : constexpr int32_t BLOCK_FULL_ENOUGH_WEIGHT_DELTA = 4000;
295 : 383569 : int64_t nConsecutiveFailed = 0;
296 : :
297 : 383569 : std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef> selected_transactions;
298 [ + - ]: 383569 : selected_transactions.reserve(MAX_CLUSTER_COUNT_LIMIT);
299 : 383569 : FeePerWeight chunk_feerate;
300 : :
301 : : // This fills selected_transactions
302 [ + - ]: 383569 : chunk_feerate = m_mempool->GetBlockBuilderChunk(selected_transactions);
303 : 383569 : FeePerVSize chunk_feerate_vsize = ToFeePerVSize(chunk_feerate);
304 : :
305 [ - + + + ]: 445012 : while (selected_transactions.size() > 0) {
306 : : // Check to see if min fee rate is still respected.
307 [ + + ]: 63050 : if (ByRatio{chunk_feerate_vsize} < ByRatio{m_options.block_min_fee_rate->GetFeePerVSize()}) {
308 : : // Everything else we might consider has a lower feerate
309 : : return;
310 : : }
311 : :
312 : 61443 : int64_t chunk_sig_ops = 0;
313 : 61443 : int64_t chunk_weight = 0;
314 [ + + ]: 130563 : for (const auto& tx : selected_transactions) {
315 : 69120 : chunk_sig_ops += tx.get().GetSigOpCost();
316 : 69120 : chunk_weight += tx.get().GetTxWeight();
317 : : }
318 : :
319 : : // Check to see if this chunk will fit.
320 [ + - + + : 61443 : if (!TestChunkBlockLimits(chunk_weight, chunk_sig_ops) || !TestChunkTransactions(selected_transactions)) {
+ - - + ]
321 : : // This chunk won't fit, so we skip it and will try the next best one.
322 : 15716 : m_mempool->SkipBuilderChunk();
323 : 15716 : ++nConsecutiveFailed;
324 : :
325 : : // block_max_weight has been flattened before block assembly limit checks.
326 [ - + ]: 15716 : Assert(m_options.block_max_weight);
327 [ - + ]: 15716 : if (nConsecutiveFailed > MAX_CONSECUTIVE_FAILURES && nBlockWeight +
328 [ # # ]: 0 : BLOCK_FULL_ENOUGH_WEIGHT_DELTA > *m_options.block_max_weight) {
329 : : // Give up if we're close to full and haven't succeeded in a while
330 : : return;
331 : : }
332 : : } else {
333 : 45727 : m_mempool->IncludeBuilderChunk();
334 : :
335 : : // This chunk will fit, so add it to the block.
336 : 45727 : nConsecutiveFailed = 0;
337 [ + + ]: 97229 : for (const auto& tx : selected_transactions) {
338 [ + - ]: 51502 : AddToBlock(tx);
339 : : }
340 [ + - ]: 45727 : pblocktemplate->m_package_feerates.emplace_back(chunk_feerate_vsize);
341 : : }
342 : :
343 [ + - ]: 61443 : selected_transactions.clear();
344 [ + - ]: 61443 : chunk_feerate = m_mempool->GetBlockBuilderChunk(selected_transactions);
345 : 61443 : chunk_feerate_vsize = ToFeePerVSize(chunk_feerate);
346 : : }
347 : 383569 : }
348 : :
349 : 0 : void AddMerkleRootAndCoinbase(CBlock& block, CTransactionRef coinbase, uint32_t version, uint32_t timestamp, uint32_t nonce)
350 : : {
351 [ # # # # ]: 0 : if (block.vtx.size() == 0) {
352 : 0 : block.vtx.emplace_back(coinbase);
353 : : } else {
354 : 0 : block.vtx[0] = coinbase;
355 : : }
356 : 0 : block.nVersion = version;
357 : 0 : block.nTime = timestamp;
358 : 0 : block.nNonce = nonce;
359 : 0 : block.hashMerkleRoot = BlockMerkleRoot(block);
360 : :
361 : : // Reset cached checks
362 : 0 : block.m_checked_witness_commitment = false;
363 : 0 : block.m_checked_merkle_root = false;
364 : 0 : block.fChecked = false;
365 : 0 : }
366 : :
367 : : } // namespace node
|