/tmp/bitcoin/src/node/miner.cpp
Line | Count | Source |
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 <interfaces/types.h> |
18 | | #include <node/blockstorage.h> |
19 | | #include <node/kernel_notifications.h> |
20 | | #include <node/mining_args.h> |
21 | | #include <node/mining_types.h> |
22 | | #include <policy/feerate.h> |
23 | | #include <policy/policy.h> |
24 | | #include <pow.h> |
25 | | #include <primitives/block.h> |
26 | | #include <primitives/transaction.h> |
27 | | #include <script/script.h> |
28 | | #include <sync.h> |
29 | | #include <tinyformat.h> |
30 | | #include <txgraph.h> |
31 | | #include <txmempool.h> |
32 | | #include <uint256.h> |
33 | | #include <util/check.h> |
34 | | #include <util/feefrac.h> |
35 | | #include <util/log.h> |
36 | | #include <util/result.h> |
37 | | #include <util/signalinterrupt.h> |
38 | | #include <util/time.h> |
39 | | #include <util/translation.h> |
40 | | #include <validation.h> |
41 | | #include <validationinterface.h> |
42 | | #include <versionbits.h> |
43 | | |
44 | | #include <algorithm> |
45 | | #include <compare> |
46 | | #include <condition_variable> |
47 | | #include <cstddef> |
48 | | #include <functional> |
49 | | #include <numeric> |
50 | | #include <span> |
51 | | #include <stdexcept> |
52 | | #include <string> |
53 | | #include <utility> |
54 | | |
55 | | namespace node { |
56 | | |
57 | | int64_t GetMinimumTime(const CBlockIndex* pindexPrev, const int64_t difficulty_adjustment_interval) |
58 | 48.8k | { |
59 | 48.8k | int64_t min_time{pindexPrev->GetMedianTimePast() + 1}; |
60 | | // Height of block to be mined. |
61 | 48.8k | const int height{pindexPrev->nHeight + 1}; |
62 | | // Account for BIP94 timewarp rule on all networks. This makes future |
63 | | // activation safer. |
64 | 48.8k | if (height % difficulty_adjustment_interval == 0) { |
65 | 214 | min_time = std::max<int64_t>(min_time, pindexPrev->GetBlockTime() - MAX_TIMEWARP); |
66 | 214 | } |
67 | 48.8k | return min_time; |
68 | 48.8k | } |
69 | | |
70 | | int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev) |
71 | 46.8k | { |
72 | 46.8k | int64_t nOldTime = pblock->nTime; |
73 | 46.8k | int64_t nNewTime{std::max<int64_t>(GetMinimumTime(pindexPrev, consensusParams.DifficultyAdjustmentInterval()), |
74 | 46.8k | TicksSinceEpoch<std::chrono::seconds>(NodeClock::now()))}; |
75 | | |
76 | 46.8k | if (nOldTime < nNewTime) { |
77 | 34.6k | pblock->nTime = nNewTime; |
78 | 34.6k | } |
79 | | |
80 | | // Updating time can change work required on testnet: |
81 | 46.8k | if (consensusParams.fPowAllowMinDifficultyBlocks) { |
82 | 46.6k | pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, consensusParams); |
83 | 46.6k | } |
84 | | |
85 | 46.8k | return nNewTime - nOldTime; |
86 | 46.8k | } |
87 | | |
88 | | void RegenerateCommitments(CBlock& block, ChainstateManager& chainman) |
89 | 7.37k | { |
90 | 7.37k | CMutableTransaction tx{*block.vtx.at(0)}; |
91 | 7.37k | tx.vout.erase(tx.vout.begin() + GetWitnessCommitmentIndex(block)); |
92 | 7.37k | block.vtx.at(0) = MakeTransactionRef(tx); |
93 | | |
94 | 7.37k | const CBlockIndex* prev_block = WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock)); |
95 | 7.37k | chainman.GenerateCoinbaseCommitment(block, prev_block); |
96 | | |
97 | 7.37k | block.hashMerkleRoot = BlockMerkleRoot(block); |
98 | 7.37k | } |
99 | | |
100 | | BlockAssembler::BlockAssembler(Chainstate& chainstate, |
101 | | const CTxMemPool* mempool, |
102 | | BlockCreateOptions options) |
103 | 44.7k | : chainparams{chainstate.m_chainman.GetParams()}, |
104 | 44.7k | m_mempool{options.use_mempool ? mempool : nullptr}, |
105 | 44.7k | m_chainstate{chainstate}, |
106 | 44.7k | m_options{[&] { |
107 | 44.7k | if (auto result{CheckMiningOptions(options, /*use_argnames=*/false)}; !result) { |
108 | 1 | throw std::runtime_error(util::ErrorString(result).original); |
109 | 1 | } |
110 | 44.7k | return FlattenMiningOptions(std::move(options)); |
111 | 44.7k | }()} |
112 | 44.7k | { |
113 | 44.7k | } |
114 | | |
115 | | void BlockAssembler::resetBlock() |
116 | 44.7k | { |
117 | | // Reserve space for fixed-size block header, txs count, and coinbase tx. |
118 | 44.7k | nBlockWeight = *Assert(m_options.block_reserved_weight); |
119 | 44.7k | nBlockSigOpsCost = m_options.coinbase_output_max_additional_sigops; |
120 | | |
121 | | // These counters do not include coinbase tx |
122 | 44.7k | nBlockTx = 0; |
123 | 44.7k | nFees = 0; |
124 | 44.7k | } |
125 | | |
126 | | std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock() |
127 | 44.7k | { |
128 | 44.7k | const auto time_start{SteadyClock::now()}; |
129 | | |
130 | 44.7k | resetBlock(); |
131 | | |
132 | 44.7k | pblocktemplate.reset(new CBlockTemplate()); |
133 | 44.7k | 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 | 44.7k | pblock->vtx.emplace_back(); |
138 | | |
139 | 44.7k | LOCK(::cs_main); |
140 | 44.7k | CBlockIndex* pindexPrev = m_chainstate.m_chain.Tip(); |
141 | 44.7k | assert(pindexPrev != nullptr); |
142 | 44.7k | nHeight = pindexPrev->nHeight + 1; |
143 | | |
144 | 44.7k | 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 | 44.7k | if (chainparams.MineBlocksOnDemand()) { |
148 | 44.5k | pblock->nVersion = gArgs.GetIntArg("-blockversion", pblock->nVersion); |
149 | 44.5k | } |
150 | | |
151 | 44.7k | pblock->nTime = TicksSinceEpoch<std::chrono::seconds>(NodeClock::now()); |
152 | 44.7k | m_lock_time_cutoff = pindexPrev->GetMedianTimePast(); |
153 | | |
154 | 44.7k | if (m_mempool) { |
155 | 37.3k | LOCK(m_mempool->cs); |
156 | 37.3k | m_mempool->StartBlockBuilding(); |
157 | 37.3k | addChunks(); |
158 | 37.3k | m_mempool->StopBlockBuilding(); |
159 | 37.3k | } |
160 | | |
161 | 44.7k | const auto time_1{SteadyClock::now()}; |
162 | | |
163 | 44.7k | m_last_block_num_txs = nBlockTx; |
164 | 44.7k | m_last_block_weight = nBlockWeight; |
165 | | |
166 | | // Create coinbase transaction. |
167 | 44.7k | CMutableTransaction coinbaseTx; |
168 | | |
169 | | // Construct coinbase transaction struct in parallel |
170 | 44.7k | CoinbaseTx& coinbase_tx{pblocktemplate->m_coinbase_tx}; |
171 | 44.7k | coinbase_tx.version = coinbaseTx.version; |
172 | | |
173 | 44.7k | coinbaseTx.vin.resize(1); |
174 | 44.7k | coinbaseTx.vin[0].prevout.SetNull(); |
175 | 44.7k | coinbaseTx.vin[0].nSequence = CTxIn::MAX_SEQUENCE_NONFINAL; // Make sure timelock is enforced. |
176 | 44.7k | coinbase_tx.sequence = coinbaseTx.vin[0].nSequence; |
177 | | |
178 | | // Add an output that spends the full coinbase reward. |
179 | 44.7k | coinbaseTx.vout.resize(1); |
180 | 44.7k | coinbaseTx.vout[0].scriptPubKey = m_options.coinbase_output_script; |
181 | | // Block subsidy + fees |
182 | 44.7k | const CAmount block_reward{nFees + GetBlockSubsidy(nHeight, chainparams.GetConsensus())}; |
183 | 44.7k | coinbaseTx.vout[0].nValue = block_reward; |
184 | 44.7k | 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 | 44.7k | 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 | 44.7k | coinbase_tx.script_sig_prefix = coinbaseTx.vin[0].scriptSig; |
196 | 44.7k | 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 | 2.70k | coinbaseTx.vin[0].scriptSig << OP_0; |
202 | 2.70k | } |
203 | 44.7k | Assert(nHeight > 0); |
204 | 44.7k | coinbaseTx.nLockTime = static_cast<uint32_t>(nHeight - 1); |
205 | 44.7k | coinbase_tx.lock_time = coinbaseTx.nLockTime; |
206 | | |
207 | 44.7k | pblock->vtx[0] = MakeTransactionRef(std::move(coinbaseTx)); |
208 | 44.7k | m_chainstate.m_chainman.GenerateCoinbaseCommitment(*pblock, pindexPrev); |
209 | | |
210 | 44.7k | const CTransactionRef& final_coinbase{pblock->vtx[0]}; |
211 | 44.7k | if (final_coinbase->HasWitness()) { |
212 | 44.0k | 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 | 44.0k | Assert(witness_stack.size() == 1 && witness_stack[0].size() == 32); |
216 | 44.0k | coinbase_tx.witness = uint256(witness_stack[0]); |
217 | 44.0k | } |
218 | 44.7k | if (const int witness_index = GetWitnessCommitmentIndex(*pblock); witness_index != NO_WITNESS_COMMITMENT) { |
219 | 44.7k | Assert(witness_index >= 0 && static_cast<size_t>(witness_index) < final_coinbase->vout.size()); |
220 | 44.7k | coinbase_tx.required_outputs.push_back(final_coinbase->vout[witness_index]); |
221 | 44.7k | } |
222 | | |
223 | 44.7k | LogInfo("CreateNewBlock(): block weight: %u txs: %u fees: %ld sigops %d\n", GetBlockWeight(*pblock), nBlockTx, nFees, nBlockSigOpsCost); |
224 | | |
225 | | // Fill in header |
226 | 44.7k | pblock->hashPrevBlock = pindexPrev->GetBlockHash(); |
227 | 44.7k | UpdateTime(pblock, chainparams.GetConsensus(), pindexPrev); |
228 | 44.7k | pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, chainparams.GetConsensus()); |
229 | 44.7k | pblock->nNonce = 0; |
230 | | |
231 | 44.7k | if (m_options.test_block_validity) { |
232 | 44.7k | if (BlockValidationState state{TestBlockValidity(m_chainstate, *pblock, /*check_pow=*/false, /*check_merkle_root=*/false)}; !state.IsValid()) { |
233 | 5 | throw std::runtime_error(strprintf("TestBlockValidity failed: %s", state.ToString())); |
234 | 5 | } |
235 | 44.7k | } |
236 | 44.7k | const auto time_2{SteadyClock::now()}; |
237 | | |
238 | 44.7k | LogDebug(BCLog::BENCH, "CreateNewBlock() chunks: %.2fms, validity: %.2fms (total %.2fms)\n", |
239 | 44.7k | Ticks<MillisecondsDouble>(time_1 - time_start), |
240 | 44.7k | Ticks<MillisecondsDouble>(time_2 - time_1), |
241 | 44.7k | Ticks<MillisecondsDouble>(time_2 - time_start)); |
242 | | |
243 | 44.7k | return std::move(pblocktemplate); |
244 | 44.7k | } |
245 | | |
246 | | bool BlockAssembler::TestChunkBlockLimits(FeePerWeight chunk_feerate, int64_t chunk_sigops_cost) const |
247 | 50.5k | { |
248 | 50.5k | if (nBlockWeight + chunk_feerate.size >= m_options.block_max_weight) { |
249 | 40.8k | return false; |
250 | 40.8k | } |
251 | 9.74k | if (nBlockSigOpsCost + chunk_sigops_cost >= MAX_BLOCK_SIGOPS_COST) { |
252 | 2 | return false; |
253 | 2 | } |
254 | 9.74k | return true; |
255 | 9.74k | } |
256 | | |
257 | | // Perform transaction-level checks before adding to block: |
258 | | // - transaction finality (locktime) |
259 | | bool BlockAssembler::TestChunkTransactions(const std::vector<CTxMemPoolEntryRef>& txs) const |
260 | 9.74k | { |
261 | 10.6k | for (const auto tx : txs) { |
262 | 10.6k | if (!IsFinalTx(tx.get().GetTx(), nHeight, m_lock_time_cutoff)) { |
263 | 2 | return false; |
264 | 2 | } |
265 | 10.6k | } |
266 | 9.73k | return true; |
267 | 9.74k | } |
268 | | |
269 | | void BlockAssembler::AddToBlock(const CTxMemPoolEntry& entry) |
270 | 10.6k | { |
271 | 10.6k | pblocktemplate->block.vtx.emplace_back(entry.GetSharedTx()); |
272 | 10.6k | pblocktemplate->vTxFees.push_back(entry.GetFee()); |
273 | 10.6k | pblocktemplate->vTxSigOpsCost.push_back(entry.GetSigOpCost()); |
274 | 10.6k | nBlockWeight += entry.GetTxWeight(); |
275 | 10.6k | ++nBlockTx; |
276 | 10.6k | nBlockSigOpsCost += entry.GetSigOpCost(); |
277 | 10.6k | nFees += entry.GetFee(); |
278 | | |
279 | 10.6k | if (*m_options.print_modified_fee) { |
280 | 88 | LogInfo("fee rate %s txid %s\n", |
281 | 88 | CFeeRate(entry.GetModifiedFee(), entry.GetTxSize()).ToString(), |
282 | 88 | entry.GetTx().GetHash().ToString()); |
283 | 88 | } |
284 | 10.6k | } |
285 | | |
286 | | void BlockAssembler::addChunks() |
287 | 37.3k | { |
288 | | // Limit the number of attempts to add transactions to the block when it is |
289 | | // close to full; this is just a simple heuristic to finish quickly if the |
290 | | // mempool has a lot of entries. |
291 | 37.3k | const int64_t MAX_CONSECUTIVE_FAILURES = 1000; |
292 | 37.3k | constexpr int32_t BLOCK_FULL_ENOUGH_WEIGHT_DELTA = 4000; |
293 | 37.3k | int64_t nConsecutiveFailed = 0; |
294 | | |
295 | 37.3k | std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef> selected_transactions; |
296 | 37.3k | selected_transactions.reserve(MAX_CLUSTER_COUNT_LIMIT); |
297 | 37.3k | FeePerWeight chunk_feerate; |
298 | | |
299 | | // This fills selected_transactions |
300 | 37.3k | chunk_feerate = m_mempool->GetBlockBuilderChunk(selected_transactions); |
301 | 37.3k | FeePerVSize chunk_feerate_vsize = ToFeePerVSize(chunk_feerate); |
302 | | |
303 | 87.8k | while (selected_transactions.size() > 0) { |
304 | | // Check to see if min fee rate is still respected. |
305 | 50.5k | if (ByRatio{chunk_feerate_vsize} < ByRatio{m_options.block_min_fee_rate->GetFeePerVSize()}) { |
306 | | // Everything else we might consider has a lower feerate |
307 | 46 | return; |
308 | 46 | } |
309 | | |
310 | 50.5k | int64_t chunk_sig_ops = 0; |
311 | 53.4k | for (const auto& tx : selected_transactions) { |
312 | 53.4k | chunk_sig_ops += tx.get().GetSigOpCost(); |
313 | 53.4k | } |
314 | | |
315 | | // Check to see if this chunk will fit. |
316 | 50.5k | if (!TestChunkBlockLimits(chunk_feerate, chunk_sig_ops) || !TestChunkTransactions(selected_transactions)) { |
317 | | // This chunk won't fit, so we skip it and will try the next best one. |
318 | 40.8k | m_mempool->SkipBuilderChunk(); |
319 | 40.8k | ++nConsecutiveFailed; |
320 | | |
321 | 40.8k | if (nConsecutiveFailed > MAX_CONSECUTIVE_FAILURES && nBlockWeight + |
322 | 17 | BLOCK_FULL_ENOUGH_WEIGHT_DELTA > m_options.block_max_weight) { |
323 | | // Give up if we're close to full and haven't succeeded in a while |
324 | 17 | return; |
325 | 17 | } |
326 | 40.8k | } else { |
327 | 9.73k | m_mempool->IncludeBuilderChunk(); |
328 | | |
329 | | // This chunk will fit, so add it to the block. |
330 | 9.73k | nConsecutiveFailed = 0; |
331 | 10.6k | for (const auto& tx : selected_transactions) { |
332 | 10.6k | AddToBlock(tx); |
333 | 10.6k | } |
334 | 9.73k | pblocktemplate->m_package_feerates.emplace_back(chunk_feerate_vsize); |
335 | 9.73k | } |
336 | | |
337 | 50.5k | selected_transactions.clear(); |
338 | 50.5k | chunk_feerate = m_mempool->GetBlockBuilderChunk(selected_transactions); |
339 | 50.5k | chunk_feerate_vsize = ToFeePerVSize(chunk_feerate); |
340 | 50.5k | } |
341 | 37.3k | } |
342 | | |
343 | | void AddMerkleRootAndCoinbase(CBlock& block, CTransactionRef coinbase, uint32_t version, uint32_t timestamp, uint32_t nonce) |
344 | 55 | { |
345 | 55 | if (block.vtx.size() == 0) { |
346 | 0 | block.vtx.emplace_back(coinbase); |
347 | 55 | } else { |
348 | 55 | block.vtx[0] = coinbase; |
349 | 55 | } |
350 | 55 | block.nVersion = version; |
351 | 55 | block.nTime = timestamp; |
352 | 55 | block.nNonce = nonce; |
353 | 55 | block.hashMerkleRoot = BlockMerkleRoot(block); |
354 | | |
355 | | // Reset cached checks |
356 | 55 | block.m_checked_witness_commitment = false; |
357 | 55 | block.m_checked_merkle_root = false; |
358 | 55 | block.fChecked = false; |
359 | 55 | } |
360 | | |
361 | | namespace { |
362 | | class SubmitBlockStateCatcher final : public CValidationInterface |
363 | | { |
364 | | public: |
365 | | uint256 m_hash; |
366 | | bool m_found{false}; |
367 | | BlockValidationState m_state; |
368 | | |
369 | 165 | explicit SubmitBlockStateCatcher(const uint256& hash) : m_hash{hash} {} |
370 | | |
371 | | protected: |
372 | | void BlockChecked(const std::shared_ptr<const CBlock>& block, const BlockValidationState& state) override |
373 | 110 | { |
374 | 110 | if (block->GetHash() != m_hash) return; |
375 | | // ProcessNewBlock emits BlockChecked synchronously while holding cs_main, |
376 | | // so SubmitBlock can read these fields after ProcessNewBlock returns |
377 | | // without extra synchronization. |
378 | 110 | m_found = true; |
379 | 110 | m_state = state; |
380 | 110 | } |
381 | | }; |
382 | | } // namespace |
383 | | |
384 | | bool SubmitBlock(ChainstateManager& chainman, const std::shared_ptr<const CBlock>& block, bool* new_block, std::string& reason, std::string& debug) |
385 | 165 | { |
386 | 165 | reason.clear(); |
387 | 165 | debug.clear(); |
388 | | |
389 | | // This follows the submitblock RPC's validation-state capture pattern, but |
390 | | // is intentionally kept separate from the RPC implementation. The RPC entry |
391 | | // point decodes hex, formats BIP22/JSONRPC results, and calls |
392 | | // UpdateUncommittedBlockStructures() for legacy witness handling. IPC |
393 | | // callers submit already-formed blocks and need bool + reason/debug |
394 | | // results, while submitSolution() preserves its duplicate-as-success |
395 | | // behavior. |
396 | 165 | auto sc = std::make_shared<SubmitBlockStateCatcher>(block->GetHash()); |
397 | 165 | CHECK_NONFATAL(chainman.m_options.signals)->RegisterSharedValidationInterface(sc); |
398 | 165 | bool accepted = chainman.ProcessNewBlock(block, /*force_processing=*/true, /*min_pow_checked=*/true, /*new_block=*/new_block); |
399 | 165 | CHECK_NONFATAL(chainman.m_options.signals)->UnregisterSharedValidationInterface(sc); |
400 | | |
401 | 165 | if (new_block && !*new_block && accepted) { |
402 | 55 | reason = "duplicate"; |
403 | 110 | } else if (!sc->m_found) { |
404 | | // A block can be accepted and stored without being connected, for |
405 | | // example if it does not have more work than the current tip. In that |
406 | | // case no BlockChecked callback is emitted, so the validation result is |
407 | | // inconclusive. Mining::submitBlock treats this as an error for mining |
408 | | // clients, but it does not mean the block is invalid. |
409 | 0 | reason = "inconclusive"; |
410 | 110 | } else if (!sc->m_state.IsValid()) { |
411 | 0 | reason = sc->m_state.GetRejectReason(); |
412 | 0 | debug = sc->m_state.GetDebugMessage(); |
413 | 0 | } |
414 | 165 | return accepted; |
415 | 165 | } |
416 | | |
417 | | void InterruptWait(KernelNotifications& kernel_notifications, bool& interrupt_wait) |
418 | 0 | { |
419 | 0 | LOCK(kernel_notifications.m_tip_block_mutex); |
420 | 0 | interrupt_wait = true; |
421 | 0 | kernel_notifications.m_tip_block_cv.notify_all(); |
422 | 0 | } |
423 | | |
424 | | std::unique_ptr<CBlockTemplate> WaitAndCreateNewBlock(ChainstateManager& chainman, |
425 | | KernelNotifications& kernel_notifications, |
426 | | CTxMemPool* mempool, |
427 | | const std::unique_ptr<CBlockTemplate>& block_template, |
428 | | const BlockWaitOptions& wait_options, |
429 | | const BlockCreateOptions& create_options, |
430 | | bool& interrupt_wait) |
431 | 62 | { |
432 | | // Delay calculating the current template fees, just in case a new block |
433 | | // comes in before the next tick. |
434 | 62 | CAmount current_fees = -1; |
435 | | |
436 | | // Alternate waiting for a new tip and checking if fees have risen. |
437 | | // The latter check is expensive so we only run it once per second. |
438 | 62 | auto now{NodeClock::now()}; |
439 | 62 | const auto deadline = now + wait_options.timeout; |
440 | 62 | const MillisecondsDouble tick{1000}; |
441 | 62 | const bool allow_min_difficulty{chainman.GetParams().GetConsensus().fPowAllowMinDifficultyBlocks}; |
442 | | |
443 | 62 | do { |
444 | 62 | bool tip_changed{false}; |
445 | 62 | { |
446 | 62 | WAIT_LOCK(kernel_notifications.m_tip_block_mutex, lock); |
447 | | // Note that wait_until() checks the predicate before waiting |
448 | 69 | kernel_notifications.m_tip_block_cv.wait_until(lock, std::min(now + tick, deadline), [&]() EXCLUSIVE_LOCKS_REQUIRED(kernel_notifications.m_tip_block_mutex) { |
449 | 69 | AssertLockHeld(kernel_notifications.m_tip_block_mutex); |
450 | 69 | const auto tip_block{kernel_notifications.TipBlock()}; |
451 | | // We assume tip_block is set, because this is an instance |
452 | | // method on BlockTemplate and no template could have been |
453 | | // generated before a tip exists. |
454 | 69 | tip_changed = Assume(tip_block) && tip_block != block_template->block.hashPrevBlock; |
455 | 69 | return tip_changed || chainman.m_interrupt || interrupt_wait; |
456 | 69 | }); |
457 | 62 | if (interrupt_wait) { |
458 | 0 | interrupt_wait = false; |
459 | 0 | return nullptr; |
460 | 0 | } |
461 | 62 | } |
462 | | |
463 | 62 | if (chainman.m_interrupt) return nullptr; |
464 | | // At this point the tip changed, a full tick went by or we reached |
465 | | // the deadline. |
466 | | |
467 | | // Must release m_tip_block_mutex before locking cs_main, to avoid deadlocks. |
468 | 62 | LOCK(::cs_main); |
469 | | |
470 | | // On test networks return a minimum difficulty block after 20 minutes |
471 | 62 | if (!tip_changed && allow_min_difficulty) { |
472 | 3 | const NodeClock::time_point tip_time{std::chrono::seconds{chainman.ActiveChain().Tip()->GetBlockTime()}}; |
473 | 3 | if (now > tip_time + 20min) { |
474 | 1 | tip_changed = true; |
475 | 1 | } |
476 | 3 | } |
477 | | |
478 | | /** |
479 | | * We determine if fees increased compared to the previous template by generating |
480 | | * a fresh template. There may be more efficient ways to determine how much |
481 | | * (approximate) fees for the next block increased, perhaps more so after |
482 | | * Cluster Mempool. |
483 | | * |
484 | | * We'll also create a new template if the tip changed during this iteration. |
485 | | */ |
486 | 62 | if (wait_options.fee_threshold < MAX_MONEY || tip_changed) { |
487 | 62 | auto new_tmpl{BlockAssembler{ |
488 | 62 | chainman.ActiveChainstate(), |
489 | 62 | mempool, |
490 | 62 | create_options |
491 | 62 | }.CreateNewBlock()}; |
492 | | |
493 | | // If the tip changed, return the new template regardless of its fees. |
494 | 62 | if (tip_changed) return new_tmpl; |
495 | | |
496 | | // Calculate the original template total fees if we haven't already |
497 | 6 | if (current_fees == -1) { |
498 | 6 | current_fees = std::accumulate(block_template->vTxFees.begin(), block_template->vTxFees.end(), CAmount{0}); |
499 | 6 | } |
500 | | |
501 | | // Check if fees increased enough to return the new template |
502 | 6 | const CAmount new_fees = std::accumulate(new_tmpl->vTxFees.begin(), new_tmpl->vTxFees.end(), CAmount{0}); |
503 | 6 | Assume(wait_options.fee_threshold != MAX_MONEY); |
504 | 6 | if (new_fees >= current_fees + wait_options.fee_threshold) return new_tmpl; |
505 | 6 | } |
506 | | |
507 | 4 | now = NodeClock::now(); |
508 | 4 | } while (now < deadline); |
509 | | |
510 | 4 | return nullptr; |
511 | 62 | } |
512 | | |
513 | | std::optional<BlockRef> GetTip(ChainstateManager& chainman) |
514 | 46.9k | { |
515 | 46.9k | LOCK(::cs_main); |
516 | 46.9k | CBlockIndex* tip{chainman.ActiveChain().Tip()}; |
517 | 46.9k | if (!tip) return {}; |
518 | 46.9k | return BlockRef{tip->GetBlockHash(), tip->nHeight}; |
519 | 46.9k | } |
520 | | |
521 | | bool CooldownIfHeadersAhead(ChainstateManager& chainman, KernelNotifications& kernel_notifications, const BlockRef& last_tip, bool& interrupt_mining) |
522 | 0 | { |
523 | 0 | uint256 last_tip_hash{last_tip.hash}; |
524 | |
|
525 | 0 | while (const std::optional<int> remaining = chainman.BlocksAheadOfTip()) { |
526 | 0 | const int cooldown_seconds = std::clamp(*remaining, 3, 20); |
527 | 0 | const auto cooldown_deadline{MockableSteadyClock::now() + std::chrono::seconds{cooldown_seconds}}; |
528 | |
|
529 | 0 | { |
530 | 0 | WAIT_LOCK(kernel_notifications.m_tip_block_mutex, lock); |
531 | 0 | kernel_notifications.m_tip_block_cv.wait_until(lock, cooldown_deadline, [&]() EXCLUSIVE_LOCKS_REQUIRED(kernel_notifications.m_tip_block_mutex) { |
532 | 0 | const auto tip_block = kernel_notifications.TipBlock(); |
533 | 0 | return chainman.m_interrupt || interrupt_mining || (tip_block && *tip_block != last_tip_hash); |
534 | 0 | }); |
535 | 0 | if (chainman.m_interrupt || interrupt_mining) { |
536 | 0 | interrupt_mining = false; |
537 | 0 | return false; |
538 | 0 | } |
539 | | |
540 | | // If the tip changed during the wait, extend the deadline |
541 | 0 | const auto tip_block = kernel_notifications.TipBlock(); |
542 | 0 | if (tip_block && *tip_block != last_tip_hash) { |
543 | 0 | last_tip_hash = *tip_block; |
544 | 0 | continue; |
545 | 0 | } |
546 | 0 | } |
547 | | |
548 | | // No tip change and the cooldown window has expired. |
549 | 0 | if (MockableSteadyClock::now() >= cooldown_deadline) break; |
550 | 0 | } |
551 | | |
552 | 0 | return true; |
553 | 0 | } |
554 | | |
555 | | std::optional<BlockRef> WaitTipChanged(ChainstateManager& chainman, KernelNotifications& kernel_notifications, const uint256& current_tip, MillisecondsDouble& timeout, bool& interrupt) |
556 | 44.7k | { |
557 | 44.7k | Assume(timeout >= 0ms); // No internal callers should use a negative timeout |
558 | 44.7k | if (timeout < 0ms) timeout = 0ms; |
559 | 44.7k | if (timeout > std::chrono::years{100}) timeout = std::chrono::years{100}; // Upper bound to avoid UB in std::chrono |
560 | 44.7k | auto deadline{std::chrono::steady_clock::now() + timeout}; |
561 | 44.7k | { |
562 | 44.7k | WAIT_LOCK(kernel_notifications.m_tip_block_mutex, lock); |
563 | | // For callers convenience, wait longer than the provided timeout |
564 | | // during startup for the tip to be non-null. That way this function |
565 | | // always returns valid tip information when possible and only |
566 | | // returns null when shutting down, not when timing out. |
567 | 44.7k | kernel_notifications.m_tip_block_cv.wait(lock, [&]() EXCLUSIVE_LOCKS_REQUIRED(kernel_notifications.m_tip_block_mutex) { |
568 | 44.7k | return kernel_notifications.TipBlock() || chainman.m_interrupt || interrupt; |
569 | 44.7k | }); |
570 | 44.7k | if (chainman.m_interrupt || interrupt) { |
571 | 0 | interrupt = false; |
572 | 0 | return {}; |
573 | 0 | } |
574 | | // At this point TipBlock is set, so continue to wait until it is |
575 | | // different then `current_tip` provided by caller. |
576 | 44.7k | kernel_notifications.m_tip_block_cv.wait_until(lock, deadline, [&]() EXCLUSIVE_LOCKS_REQUIRED(kernel_notifications.m_tip_block_mutex) { |
577 | 44.7k | return Assume(kernel_notifications.TipBlock()) != current_tip || chainman.m_interrupt || interrupt; |
578 | 44.7k | }); |
579 | 44.7k | if (chainman.m_interrupt || interrupt) { |
580 | 2 | interrupt = false; |
581 | 2 | return {}; |
582 | 2 | } |
583 | 44.7k | } |
584 | | |
585 | | // Must release m_tip_block_mutex before getTip() locks cs_main, to |
586 | | // avoid deadlocks. |
587 | 44.7k | return GetTip(chainman); |
588 | 44.7k | } |
589 | | |
590 | | } // namespace node |