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 <coins.h>
11 : : #include <common/args.h>
12 : : #include <consensus/amount.h>
13 : : #include <consensus/consensus.h>
14 : : #include <consensus/merkle.h>
15 : : #include <consensus/tx_verify.h>
16 : : #include <consensus/validation.h>
17 : : #include <deploymentstatus.h>
18 : : #include <logging.h>
19 : : #include <node/context.h>
20 : : #include <node/kernel_notifications.h>
21 : : #include <policy/feerate.h>
22 : : #include <policy/policy.h>
23 : : #include <pow.h>
24 : : #include <primitives/transaction.h>
25 : : #include <util/moneystr.h>
26 : : #include <util/signalinterrupt.h>
27 : : #include <util/time.h>
28 : : #include <validation.h>
29 : :
30 : : #include <algorithm>
31 : : #include <utility>
32 : : #include <numeric>
33 : :
34 : : namespace node {
35 : :
36 : 333253 : int64_t GetMinimumTime(const CBlockIndex* pindexPrev, const int64_t difficulty_adjustment_interval)
37 : : {
38 : 333253 : int64_t min_time{pindexPrev->GetMedianTimePast() + 1};
39 : : // Height of block to be mined.
40 : 333253 : const int height{pindexPrev->nHeight + 1};
41 : : // Account for BIP94 timewarp rule on all networks. This makes future
42 : : // activation safer.
43 [ + + ]: 333253 : if (height % difficulty_adjustment_interval == 0) {
44 [ + - ]: 2954 : min_time = std::max<int64_t>(min_time, pindexPrev->GetBlockTime() - MAX_TIMEWARP);
45 : : }
46 : 333253 : return min_time;
47 : : }
48 : :
49 : 333253 : int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
50 : : {
51 : 333253 : int64_t nOldTime = pblock->nTime;
52 [ + + ]: 333253 : int64_t nNewTime{std::max<int64_t>(GetMinimumTime(pindexPrev, consensusParams.DifficultyAdjustmentInterval()),
53 : 333253 : TicksSinceEpoch<std::chrono::seconds>(NodeClock::now()))};
54 : :
55 [ + + ]: 333253 : if (nOldTime < nNewTime) {
56 : 234330 : pblock->nTime = nNewTime;
57 : : }
58 : :
59 : : // Updating time can change work required on testnet:
60 [ + - ]: 333253 : if (consensusParams.fPowAllowMinDifficultyBlocks) {
61 : 333253 : pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, consensusParams);
62 : : }
63 : :
64 : 333253 : return nNewTime - nOldTime;
65 : : }
66 : :
67 : 90467 : void RegenerateCommitments(CBlock& block, ChainstateManager& chainman)
68 : : {
69 : 90467 : CMutableTransaction tx{*block.vtx.at(0)};
70 : 90467 : tx.vout.erase(tx.vout.begin() + GetWitnessCommitmentIndex(block));
71 [ + - + - : 180934 : block.vtx.at(0) = MakeTransactionRef(tx);
- + ]
72 : :
73 [ + - + - : 271401 : const CBlockIndex* prev_block = WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock));
+ - ]
74 [ + - ]: 90467 : chainman.GenerateCoinbaseCommitment(block, prev_block);
75 : :
76 [ + - ]: 90467 : block.hashMerkleRoot = BlockMerkleRoot(block);
77 : 90467 : }
78 : :
79 : 333249 : static BlockAssembler::Options ClampOptions(BlockAssembler::Options options)
80 : : {
81 : : // Apply DEFAULT_BLOCK_RESERVED_WEIGHT when the caller left it unset.
82 [ - + + - : 666498 : options.block_reserved_weight = std::clamp<size_t>(options.block_reserved_weight.value_or(DEFAULT_BLOCK_RESERVED_WEIGHT), MINIMUM_BLOCK_RESERVED_WEIGHT, MAX_BLOCK_WEIGHT);
- + ]
83 [ - + ]: 333249 : options.coinbase_output_max_additional_sigops = std::clamp<size_t>(options.coinbase_output_max_additional_sigops, 0, MAX_BLOCK_SIGOPS_COST);
84 : : // Limit weight to between block_reserved_weight and MAX_BLOCK_WEIGHT for sanity:
85 : : // block_reserved_weight can safely exceed -blockmaxweight, but the rest of the block template will be empty.
86 [ + + ]: 333249 : options.nBlockMaxWeight = std::clamp<size_t>(options.nBlockMaxWeight, *options.block_reserved_weight, MAX_BLOCK_WEIGHT);
87 : 333249 : return options;
88 : : }
89 : :
90 : 333249 : BlockAssembler::BlockAssembler(Chainstate& chainstate, const CTxMemPool* mempool, const Options& options)
91 [ - + ]: 333249 : : chainparams{chainstate.m_chainman.GetParams()},
92 [ - + ]: 333249 : m_mempool{options.use_mempool ? mempool : nullptr},
93 : 333249 : m_chainstate{chainstate},
94 [ - + ]: 333249 : m_options{ClampOptions(options)}
95 : : {
96 : 333249 : }
97 : :
98 : 4 : void ApplyArgsManOptions(const ArgsManager& args, BlockAssembler::Options& options)
99 : : {
100 : : // Block resource limits
101 : 8 : options.nBlockMaxWeight = args.GetIntArg("-blockmaxweight", options.nBlockMaxWeight);
102 [ + - - + ]: 8 : if (const auto blockmintxfee{args.GetArg("-blockmintxfee")}) {
103 [ # # # # ]: 0 : if (const auto parsed{ParseMoney(*blockmintxfee)}) options.blockMinFeeRate = CFeeRate{*parsed};
104 : 0 : }
105 [ + - ]: 4 : options.print_modified_fee = args.GetBoolArg("-printpriority", options.print_modified_fee);
106 [ + - ]: 4 : if (!options.block_reserved_weight) {
107 : 8 : options.block_reserved_weight = args.GetIntArg("-blockreservedweight");
108 : : }
109 : 4 : }
110 : :
111 : 333249 : void BlockAssembler::resetBlock()
112 : : {
113 : : // Reserve space for fixed-size block header, txs count, and coinbase tx.
114 [ - + ]: 333249 : nBlockWeight = *Assert(m_options.block_reserved_weight);
115 : 333249 : nBlockSigOpsCost = m_options.coinbase_output_max_additional_sigops;
116 : :
117 : : // These counters do not include coinbase tx
118 : 333249 : nBlockTx = 0;
119 : 333249 : nFees = 0;
120 : 333249 : }
121 : :
122 : 333249 : std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock()
123 : : {
124 : 333249 : const auto time_start{SteadyClock::now()};
125 : :
126 : 333249 : resetBlock();
127 : :
128 [ - + ]: 333249 : pblocktemplate.reset(new CBlockTemplate());
129 : 333249 : CBlock* const pblock = &pblocktemplate->block; // pointer for convenience
130 : :
131 : : // Add dummy coinbase tx as first transaction. It is skipped by the
132 : : // getblocktemplate RPC and mining interface consumers must not use it.
133 : 333249 : pblock->vtx.emplace_back();
134 : :
135 : 333249 : LOCK(::cs_main);
136 [ - + ]: 333249 : CBlockIndex* pindexPrev = m_chainstate.m_chain.Tip();
137 [ - + ]: 333249 : assert(pindexPrev != nullptr);
138 : 333249 : nHeight = pindexPrev->nHeight + 1;
139 : :
140 [ + - ]: 333249 : pblock->nVersion = m_chainstate.m_chainman.m_versionbitscache.ComputeBlockVersion(pindexPrev, chainparams.GetConsensus());
141 : : // -regtest only: allow overriding block.nVersion with
142 : : // -blockversion=N to test forking scenarios
143 [ + - ]: 333249 : if (chainparams.MineBlocksOnDemand()) {
144 [ + - ]: 666498 : pblock->nVersion = gArgs.GetIntArg("-blockversion", pblock->nVersion);
145 : : }
146 : :
147 : 333249 : pblock->nTime = TicksSinceEpoch<std::chrono::seconds>(NodeClock::now());
148 : 333249 : m_lock_time_cutoff = pindexPrev->GetMedianTimePast();
149 : :
150 [ + - ]: 333249 : if (m_mempool) {
151 [ + - ]: 333249 : LOCK(m_mempool->cs);
152 : 333249 : m_mempool->StartBlockBuilding();
153 [ + - ]: 333249 : addChunks();
154 [ + - ]: 333249 : m_mempool->StopBlockBuilding();
155 : 333249 : }
156 : :
157 : 333249 : const auto time_1{SteadyClock::now()};
158 : :
159 [ + + ]: 333249 : m_last_block_num_txs = nBlockTx;
160 [ + + ]: 333249 : m_last_block_weight = nBlockWeight;
161 : :
162 : : // Create coinbase transaction.
163 [ + - ]: 333249 : CMutableTransaction coinbaseTx;
164 : :
165 : : // Construct coinbase transaction struct in parallel
166 [ + - ]: 333249 : CoinbaseTx& coinbase_tx{pblocktemplate->m_coinbase_tx};
167 : 333249 : coinbase_tx.version = coinbaseTx.version;
168 : :
169 [ + - ]: 333249 : coinbaseTx.vin.resize(1);
170 : 333249 : coinbaseTx.vin[0].prevout.SetNull();
171 [ + - ]: 333249 : coinbaseTx.vin[0].nSequence = CTxIn::MAX_SEQUENCE_NONFINAL; // Make sure timelock is enforced.
172 : 333249 : coinbase_tx.sequence = coinbaseTx.vin[0].nSequence;
173 : :
174 : : // Add an output that spends the full coinbase reward.
175 [ + - ]: 333249 : coinbaseTx.vout.resize(1);
176 : 333249 : coinbaseTx.vout[0].scriptPubKey = m_options.coinbase_output_script;
177 : : // Block subsidy + fees
178 [ + - ]: 333249 : const CAmount block_reward{nFees + GetBlockSubsidy(nHeight, chainparams.GetConsensus())};
179 [ + - ]: 333249 : coinbaseTx.vout[0].nValue = block_reward;
180 : 333249 : coinbase_tx.block_reward_remaining = block_reward;
181 : :
182 : : // Start the coinbase scriptSig with the block height as required by BIP34.
183 : : // Mining clients are expected to append extra data to this prefix, so
184 : : // increasing its length would reduce the space they can use and may break
185 : : // existing clients.
186 [ + - ]: 333249 : coinbaseTx.vin[0].scriptSig = CScript() << nHeight;
187 : : // Set script_sig_prefix here, so IPC mining clients are not affected by
188 : : // the optional scriptSig padding below. They provide their own extraNonce,
189 : : // and in a typical setup a pool name or realistic extraNonce already makes
190 : : // the scriptSig long enough.
191 : 333249 : coinbase_tx.script_sig_prefix = coinbaseTx.vin[0].scriptSig;
192 [ + + ]: 333249 : if (nHeight <= 16) {
193 : : // For blocks at heights <= 16, the BIP34-encoded height alone is only
194 : : // one byte. Consensus requires coinbase scriptSigs to be at least two
195 : : // bytes long (bad-cb-length), so an OP_0 is always appended at those
196 : : // heights.
197 [ + - ]: 47577 : coinbaseTx.vin[0].scriptSig << OP_0;
198 : : }
199 [ - + ]: 333249 : Assert(nHeight > 0);
200 : 333249 : coinbaseTx.nLockTime = static_cast<uint32_t>(nHeight - 1);
201 : 333249 : coinbase_tx.lock_time = coinbaseTx.nLockTime;
202 : :
203 [ + - - + ]: 666498 : pblock->vtx[0] = MakeTransactionRef(std::move(coinbaseTx));
204 [ + - ]: 333249 : m_chainstate.m_chainman.GenerateCoinbaseCommitment(*pblock, pindexPrev);
205 : :
206 [ + - ]: 333249 : const CTransactionRef& final_coinbase{pblock->vtx[0]};
207 [ + - ]: 333249 : if (final_coinbase->HasWitness()) {
208 [ - + ]: 333249 : const auto& witness_stack{final_coinbase->vin[0].scriptWitness.stack};
209 : : // Consensus requires the coinbase witness stack to have exactly one
210 : : // element of 32 bytes.
211 [ - + + - : 333249 : Assert(witness_stack.size() == 1 && witness_stack[0].size() == 32);
- + - + -
+ ]
212 [ - + - + ]: 333249 : coinbase_tx.witness = uint256(witness_stack[0]);
213 : : }
214 [ + - ]: 333249 : if (const int witness_index = GetWitnessCommitmentIndex(*pblock); witness_index != NO_WITNESS_COMMITMENT) {
215 [ + - - + : 333249 : Assert(witness_index >= 0 && static_cast<size_t>(witness_index) < final_coinbase->vout.size());
- + - + ]
216 [ + - ]: 333249 : coinbase_tx.required_outputs.push_back(final_coinbase->vout[witness_index]);
217 : : }
218 : :
219 [ + - ]: 333249 : LogInfo("CreateNewBlock(): block weight: %u txs: %u fees: %ld sigops %d\n", GetBlockWeight(*pblock), nBlockTx, nFees, nBlockSigOpsCost);
220 : :
221 : : // Fill in header
222 : 333249 : pblock->hashPrevBlock = pindexPrev->GetBlockHash();
223 [ + - ]: 333249 : UpdateTime(pblock, chainparams.GetConsensus(), pindexPrev);
224 [ + - ]: 333249 : pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, chainparams.GetConsensus());
225 : 333249 : pblock->nNonce = 0;
226 : :
227 [ + - ]: 333249 : if (m_options.test_block_validity) {
228 [ + - - + ]: 333249 : if (BlockValidationState state{TestBlockValidity(m_chainstate, *pblock, /*check_pow=*/false, /*check_merkle_root=*/false)}; !state.IsValid()) {
229 [ # # # # : 0 : throw std::runtime_error(strprintf("TestBlockValidity failed: %s", state.ToString()));
# # ]
230 : 333249 : }
231 : : }
232 : 333249 : const auto time_2{SteadyClock::now()};
233 : :
234 [ + - + + : 333249 : LogDebug(BCLog::BENCH, "CreateNewBlock() chunks: %.2fms, validity: %.2fms (total %.2fms)\n",
+ - ]
235 : : Ticks<MillisecondsDouble>(time_1 - time_start),
236 : : Ticks<MillisecondsDouble>(time_2 - time_1),
237 : : Ticks<MillisecondsDouble>(time_2 - time_start));
238 : :
239 : 333249 : return std::move(pblocktemplate);
240 [ + - ]: 666498 : }
241 : :
242 : 47697 : bool BlockAssembler::TestChunkBlockLimits(FeePerWeight chunk_feerate, int64_t chunk_sigops_cost) const
243 : : {
244 [ + + ]: 47697 : if (nBlockWeight + chunk_feerate.size >= m_options.nBlockMaxWeight) {
245 : : return false;
246 : : }
247 [ - + ]: 34700 : if (nBlockSigOpsCost + chunk_sigops_cost >= MAX_BLOCK_SIGOPS_COST) {
248 : 0 : return false;
249 : : }
250 : : return true;
251 : : }
252 : :
253 : : // Perform transaction-level checks before adding to block:
254 : : // - transaction finality (locktime)
255 : 34700 : bool BlockAssembler::TestChunkTransactions(const std::vector<CTxMemPoolEntryRef>& txs) const
256 : : {
257 [ + + ]: 72875 : for (const auto tx : txs) {
258 [ + - ]: 38175 : if (!IsFinalTx(tx.get().GetTx(), nHeight, m_lock_time_cutoff)) {
259 : : return false;
260 : : }
261 : : }
262 : : return true;
263 : : }
264 : :
265 : 38175 : void BlockAssembler::AddToBlock(const CTxMemPoolEntry& entry)
266 : : {
267 [ + - + - ]: 76350 : pblocktemplate->block.vtx.emplace_back(entry.GetSharedTx());
268 : 38175 : pblocktemplate->vTxFees.push_back(entry.GetFee());
269 : 38175 : pblocktemplate->vTxSigOpsCost.push_back(entry.GetSigOpCost());
270 [ - + ]: 38175 : nBlockWeight += entry.GetTxWeight();
271 : 38175 : ++nBlockTx;
272 [ - + ]: 38175 : nBlockSigOpsCost += entry.GetSigOpCost();
273 : 38175 : nFees += entry.GetFee();
274 : :
275 [ - + ]: 38175 : if (m_options.print_modified_fee) {
276 [ # # # # : 0 : LogInfo("fee rate %s txid %s\n",
# # # # ]
277 : : CFeeRate(entry.GetModifiedFee(), entry.GetTxSize()).ToString(),
278 : : entry.GetTx().GetHash().ToString());
279 : : }
280 : 38175 : }
281 : :
282 : 333249 : void BlockAssembler::addChunks()
283 : : {
284 : : // Limit the number of attempts to add transactions to the block when it is
285 : : // close to full; this is just a simple heuristic to finish quickly if the
286 : : // mempool has a lot of entries.
287 : 333249 : const int64_t MAX_CONSECUTIVE_FAILURES = 1000;
288 : 333249 : constexpr int32_t BLOCK_FULL_ENOUGH_WEIGHT_DELTA = 4000;
289 : 333249 : int64_t nConsecutiveFailed = 0;
290 : :
291 : 333249 : std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef> selected_transactions;
292 [ + - ]: 333249 : selected_transactions.reserve(MAX_CLUSTER_COUNT_LIMIT);
293 : 333249 : FeePerWeight chunk_feerate;
294 : :
295 : : // This fills selected_transactions
296 [ + - ]: 333249 : chunk_feerate = m_mempool->GetBlockBuilderChunk(selected_transactions);
297 : 333249 : FeePerVSize chunk_feerate_vsize = ToFeePerVSize(chunk_feerate);
298 : :
299 [ - + + + ]: 380946 : while (selected_transactions.size() > 0) {
300 : : // Check to see if min fee rate is still respected.
301 [ + + ]: 48978 : if (ByRatio{chunk_feerate_vsize} < ByRatio{m_options.blockMinFeeRate.GetFeePerVSize()}) {
302 : : // Everything else we might consider has a lower feerate
303 : : return;
304 : : }
305 : :
306 : 47697 : int64_t chunk_sig_ops = 0;
307 [ + + ]: 100292 : for (const auto& tx : selected_transactions) {
308 : 52595 : chunk_sig_ops += tx.get().GetSigOpCost();
309 : : }
310 : :
311 : : // Check to see if this chunk will fit.
312 [ + - + + : 47697 : if (!TestChunkBlockLimits(chunk_feerate, chunk_sig_ops) || !TestChunkTransactions(selected_transactions)) {
+ - - + ]
313 : : // This chunk won't fit, so we skip it and will try the next best one.
314 : 12997 : m_mempool->SkipBuilderChunk();
315 : 12997 : ++nConsecutiveFailed;
316 : :
317 [ - + ]: 12997 : if (nConsecutiveFailed > MAX_CONSECUTIVE_FAILURES && nBlockWeight +
318 [ # # ]: 0 : BLOCK_FULL_ENOUGH_WEIGHT_DELTA > m_options.nBlockMaxWeight) {
319 : : // Give up if we're close to full and haven't succeeded in a while
320 : : return;
321 : : }
322 : : } else {
323 : 34700 : m_mempool->IncludeBuilderChunk();
324 : :
325 : : // This chunk will fit, so add it to the block.
326 : 34700 : nConsecutiveFailed = 0;
327 [ + + ]: 72875 : for (const auto& tx : selected_transactions) {
328 [ + - ]: 38175 : AddToBlock(tx);
329 : : }
330 [ + - ]: 34700 : pblocktemplate->m_package_feerates.emplace_back(chunk_feerate_vsize);
331 : : }
332 : :
333 [ + - ]: 47697 : selected_transactions.clear();
334 [ + - ]: 47697 : chunk_feerate = m_mempool->GetBlockBuilderChunk(selected_transactions);
335 : 47697 : chunk_feerate_vsize = ToFeePerVSize(chunk_feerate);
336 : : }
337 : 333249 : }
338 : :
339 : 0 : void AddMerkleRootAndCoinbase(CBlock& block, CTransactionRef coinbase, uint32_t version, uint32_t timestamp, uint32_t nonce)
340 : : {
341 [ # # # # ]: 0 : if (block.vtx.size() == 0) {
342 : 0 : block.vtx.emplace_back(coinbase);
343 : : } else {
344 : 0 : block.vtx[0] = coinbase;
345 : : }
346 : 0 : block.nVersion = version;
347 : 0 : block.nTime = timestamp;
348 : 0 : block.nNonce = nonce;
349 : 0 : block.hashMerkleRoot = BlockMerkleRoot(block);
350 : :
351 : : // Reset cached checks
352 : 0 : block.m_checked_witness_commitment = false;
353 : 0 : block.m_checked_merkle_root = false;
354 : 0 : block.fChecked = false;
355 : 0 : }
356 : :
357 : 0 : void InterruptWait(KernelNotifications& kernel_notifications, bool& interrupt_wait)
358 : : {
359 : 0 : LOCK(kernel_notifications.m_tip_block_mutex);
360 : 0 : interrupt_wait = true;
361 [ # # ]: 0 : kernel_notifications.m_tip_block_cv.notify_all();
362 : 0 : }
363 : :
364 : 0 : std::unique_ptr<CBlockTemplate> WaitAndCreateNewBlock(ChainstateManager& chainman,
365 : : KernelNotifications& kernel_notifications,
366 : : CTxMemPool* mempool,
367 : : const std::unique_ptr<CBlockTemplate>& block_template,
368 : : const BlockWaitOptions& options,
369 : : const BlockAssembler::Options& assemble_options,
370 : : bool& interrupt_wait)
371 : : {
372 : : // Delay calculating the current template fees, just in case a new block
373 : : // comes in before the next tick.
374 : 0 : CAmount current_fees = -1;
375 : :
376 : : // Alternate waiting for a new tip and checking if fees have risen.
377 : : // The latter check is expensive so we only run it once per second.
378 : 0 : auto now{NodeClock::now()};
379 : 0 : const auto deadline = now + options.timeout;
380 : 0 : const MillisecondsDouble tick{1000};
381 : 0 : const bool allow_min_difficulty{chainman.GetParams().GetConsensus().fPowAllowMinDifficultyBlocks};
382 : :
383 : 0 : do {
384 : 0 : bool tip_changed{false};
385 : 0 : {
386 : 0 : WAIT_LOCK(kernel_notifications.m_tip_block_mutex, lock);
387 : : // Note that wait_until() checks the predicate before waiting
388 [ # # ]: 0 : kernel_notifications.m_tip_block_cv.wait_until(lock, std::min(now + tick, deadline), [&]() EXCLUSIVE_LOCKS_REQUIRED(kernel_notifications.m_tip_block_mutex) {
389 : 0 : AssertLockHeld(kernel_notifications.m_tip_block_mutex);
390 : 0 : const auto tip_block{kernel_notifications.TipBlock()};
391 : : // We assume tip_block is set, because this is an instance
392 : : // method on BlockTemplate and no template could have been
393 : : // generated before a tip exists.
394 [ # # # # : 0 : tip_changed = Assume(tip_block) && tip_block != block_template->block.hashPrevBlock;
# # ]
395 [ # # # # : 0 : return tip_changed || chainman.m_interrupt || interrupt_wait;
# # ]
396 : : });
397 [ # # ]: 0 : if (interrupt_wait) {
398 : 0 : interrupt_wait = false;
399 [ # # ]: 0 : return nullptr;
400 : : }
401 : 0 : }
402 : :
403 [ # # ]: 0 : if (chainman.m_interrupt) return nullptr;
404 : : // At this point the tip changed, a full tick went by or we reached
405 : : // the deadline.
406 : :
407 : : // Must release m_tip_block_mutex before locking cs_main, to avoid deadlocks.
408 : 0 : LOCK(::cs_main);
409 : :
410 : : // On test networks return a minimum difficulty block after 20 minutes
411 [ # # # # ]: 0 : if (!tip_changed && allow_min_difficulty) {
412 [ # # # # ]: 0 : const NodeClock::time_point tip_time{std::chrono::seconds{chainman.ActiveChain().Tip()->GetBlockTime()}};
413 [ # # ]: 0 : if (now > tip_time + 20min) {
414 : 0 : tip_changed = true;
415 : : }
416 : : }
417 : :
418 : : /**
419 : : * We determine if fees increased compared to the previous template by generating
420 : : * a fresh template. There may be more efficient ways to determine how much
421 : : * (approximate) fees for the next block increased, perhaps more so after
422 : : * Cluster Mempool.
423 : : *
424 : : * We'll also create a new template if the tip changed during this iteration.
425 : : */
426 [ # # # # ]: 0 : if (options.fee_threshold < MAX_MONEY || tip_changed) {
427 : 0 : auto new_tmpl{BlockAssembler{
428 : : chainman.ActiveChainstate(),
429 : : mempool,
430 [ # # # # ]: 0 : assemble_options}
431 [ # # ]: 0 : .CreateNewBlock()};
432 : :
433 : : // If the tip changed, return the new template regardless of its fees.
434 [ # # ]: 0 : if (tip_changed) return new_tmpl;
435 : :
436 : : // Calculate the original template total fees if we haven't already
437 [ # # ]: 0 : if (current_fees == -1) {
438 : 0 : current_fees = std::accumulate(block_template->vTxFees.begin(), block_template->vTxFees.end(), CAmount{0});
439 : : }
440 : :
441 : : // Check if fees increased enough to return the new template
442 : 0 : const CAmount new_fees = std::accumulate(new_tmpl->vTxFees.begin(), new_tmpl->vTxFees.end(), CAmount{0});
443 [ # # ]: 0 : Assume(options.fee_threshold != MAX_MONEY);
444 [ # # ]: 0 : if (new_fees >= current_fees + options.fee_threshold) return new_tmpl;
445 [ # # ]: 0 : }
446 : :
447 [ # # ]: 0 : now = NodeClock::now();
448 [ # # ]: 0 : } while (now < deadline);
449 : :
450 : 0 : return nullptr;
451 : : }
452 : :
453 : 0 : std::optional<BlockRef> GetTip(ChainstateManager& chainman)
454 : : {
455 : 0 : LOCK(::cs_main);
456 [ # # # # ]: 0 : CBlockIndex* tip{chainman.ActiveChain().Tip()};
457 [ # # ]: 0 : if (!tip) return {};
458 : 0 : return BlockRef{tip->GetBlockHash(), tip->nHeight};
459 : 0 : }
460 : :
461 : 0 : bool CooldownIfHeadersAhead(ChainstateManager& chainman, KernelNotifications& kernel_notifications, const BlockRef& last_tip, bool& interrupt_mining)
462 : : {
463 : 0 : uint256 last_tip_hash{last_tip.hash};
464 : :
465 [ # # ]: 0 : while (const std::optional<int> remaining = chainman.BlocksAheadOfTip()) {
466 [ # # ]: 0 : const int cooldown_seconds = std::clamp(*remaining, 3, 20);
467 : 0 : const auto cooldown_deadline{MockableSteadyClock::now() + std::chrono::seconds{cooldown_seconds}};
468 : :
469 : 0 : {
470 : 0 : WAIT_LOCK(kernel_notifications.m_tip_block_mutex, lock);
471 [ # # ]: 0 : kernel_notifications.m_tip_block_cv.wait_until(lock, cooldown_deadline, [&]() EXCLUSIVE_LOCKS_REQUIRED(kernel_notifications.m_tip_block_mutex) {
472 : 0 : const auto tip_block = kernel_notifications.TipBlock();
473 [ # # # # : 0 : return chainman.m_interrupt || interrupt_mining || (tip_block && *tip_block != last_tip_hash);
# # # # ]
474 : : });
475 [ # # # # : 0 : if (chainman.m_interrupt || interrupt_mining) {
# # ]
476 : 0 : interrupt_mining = false;
477 [ # # ]: 0 : return false;
478 : : }
479 : :
480 : : // If the tip changed during the wait, extend the deadline
481 [ # # ]: 0 : const auto tip_block = kernel_notifications.TipBlock();
482 [ # # # # ]: 0 : if (tip_block && *tip_block != last_tip_hash) {
483 [ # # ]: 0 : last_tip_hash = *tip_block;
484 [ # # ]: 0 : continue;
485 : : }
486 : 0 : }
487 : :
488 : : // No tip change and the cooldown window has expired.
489 [ # # ]: 0 : if (MockableSteadyClock::now() >= cooldown_deadline) break;
490 : : }
491 : :
492 : : return true;
493 : : }
494 : :
495 : 0 : std::optional<BlockRef> WaitTipChanged(ChainstateManager& chainman, KernelNotifications& kernel_notifications, const uint256& current_tip, MillisecondsDouble& timeout, bool& interrupt)
496 : : {
497 [ # # ]: 0 : Assume(timeout >= 0ms); // No internal callers should use a negative timeout
498 [ # # ]: 0 : if (timeout < 0ms) timeout = 0ms;
499 [ # # ]: 0 : if (timeout > std::chrono::years{100}) timeout = std::chrono::years{100}; // Upper bound to avoid UB in std::chrono
500 : 0 : auto deadline{std::chrono::steady_clock::now() + timeout};
501 : 0 : {
502 : 0 : WAIT_LOCK(kernel_notifications.m_tip_block_mutex, lock);
503 : : // For callers convenience, wait longer than the provided timeout
504 : : // during startup for the tip to be non-null. That way this function
505 : : // always returns valid tip information when possible and only
506 : : // returns null when shutting down, not when timing out.
507 [ # # ]: 0 : kernel_notifications.m_tip_block_cv.wait(lock, [&]() EXCLUSIVE_LOCKS_REQUIRED(kernel_notifications.m_tip_block_mutex) {
508 [ # # # # : 0 : return kernel_notifications.TipBlock() || chainman.m_interrupt || interrupt;
# # ]
509 : : });
510 [ # # # # : 0 : if (chainman.m_interrupt || interrupt) {
# # ]
511 : 0 : interrupt = false;
512 : 0 : return {};
513 : : }
514 : : // At this point TipBlock is set, so continue to wait until it is
515 : : // different then `current_tip` provided by caller.
516 [ # # ]: 0 : kernel_notifications.m_tip_block_cv.wait_until(lock, deadline, [&]() EXCLUSIVE_LOCKS_REQUIRED(kernel_notifications.m_tip_block_mutex) {
517 [ # # # # : 0 : return Assume(kernel_notifications.TipBlock()) != current_tip || chainman.m_interrupt || interrupt;
# # # # ]
518 : : });
519 [ # # # # : 0 : if (chainman.m_interrupt || interrupt) {
# # ]
520 : 0 : interrupt = false;
521 : 0 : return {};
522 : : }
523 : 0 : }
524 : :
525 : : // Must release m_tip_block_mutex before getTip() locks cs_main, to
526 : : // avoid deadlocks.
527 : 0 : return GetTip(chainman);
528 : : }
529 : :
530 : : } // namespace node
|