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