Bitcoin ABC 0.31.6
P2P Digital Currency
validation.cpp
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2018 The Bitcoin Core developers
3// Copyright (c) 2017-2020 The Bitcoin developers
4// Distributed under the MIT software license, see the accompanying
5// file COPYING or http://www.opensource.org/licenses/mit-license.php.
6
7#include <validation.h>
8
9#include <kernel/chain.h>
10#include <kernel/chainparams.h>
11#include <kernel/coinstats.h>
15
16#include <arith_uint256.h>
17#include <avalanche/avalanche.h>
18#include <avalanche/processor.h>
19#include <blockvalidity.h>
20#include <chainparams.h>
21#include <checkpoints.h>
22#include <checkqueue.h>
23#include <common/args.h>
24#include <config.h>
26#include <consensus/amount.h>
27#include <consensus/merkle.h>
28#include <consensus/tx_check.h>
29#include <consensus/tx_verify.h>
31#include <hash.h>
33#include <logging.h>
34#include <logging/timer.h>
35#include <minerfund.h>
36#include <node/blockstorage.h>
37#include <node/utxo_snapshot.h>
40#include <policy/block/rtt.h>
42#include <policy/policy.h>
43#include <policy/settings.h>
44#include <pow/pow.h>
45#include <primitives/block.h>
47#include <random.h>
48#include <reverse_iterator.h>
49#include <script/script.h>
50#include <script/scriptcache.h>
51#include <script/sigcache.h>
52#include <shutdown.h>
53#include <tinyformat.h>
54#include <txdb.h>
55#include <txmempool.h>
56#include <undo.h>
57#include <util/check.h> // For NDEBUG compile time check
58#include <util/fs.h>
59#include <util/fs_helpers.h>
60#include <util/strencodings.h>
61#include <util/string.h>
62#include <util/time.h>
63#include <util/trace.h>
64#include <util/translation.h>
65#include <validationinterface.h>
66#include <warnings.h>
67
68#include <algorithm>
69#include <atomic>
70#include <cassert>
71#include <chrono>
72#include <deque>
73#include <numeric>
74#include <optional>
75#include <string>
76#include <thread>
77#include <tuple>
78
84
88using node::BlockMap;
89using node::fReindex;
92
93#define MICRO 0.000001
94#define MILLI 0.001
95
97static constexpr std::chrono::hours DATABASE_WRITE_INTERVAL{1};
99static constexpr std::chrono::hours DATABASE_FLUSH_INTERVAL{24};
100const std::vector<std::string> CHECKLEVEL_DOC{
101 "level 0 reads the blocks from disk",
102 "level 1 verifies block validity",
103 "level 2 verifies undo data",
104 "level 3 checks disconnection of tip blocks",
105 "level 4 tries to reconnect the blocks",
106 "each level includes the checks of the previous levels",
107};
114static constexpr int PRUNE_LOCK_BUFFER{10};
115
116static constexpr uint64_t HEADERS_TIME_VERSION{1};
117
119std::condition_variable g_best_block_cv;
121
123 : excessiveBlockSize(config.GetMaxBlockSize()), checkPoW(true),
124 checkMerkleRoot(true) {}
125
126const CBlockIndex *
129
130 // Find the latest block common to locator and chain - we expect that
131 // locator.vHave is sorted descending by height.
132 for (const BlockHash &hash : locator.vHave) {
133 const CBlockIndex *pindex{m_blockman.LookupBlockIndex(hash)};
134 if (pindex) {
135 if (m_chain.Contains(pindex)) {
136 return pindex;
137 }
138 if (pindex->GetAncestor(m_chain.Height()) == m_chain.Tip()) {
139 return m_chain.Tip();
140 }
141 }
142 }
143 return m_chain.Genesis();
144}
145
146static uint32_t GetNextBlockScriptFlags(const CBlockIndex *pindex,
147 const ChainstateManager &chainman);
148
149namespace {
161std::optional<std::vector<int>> CalculatePrevHeights(const CBlockIndex &tip,
162 const CCoinsView &coins,
163 const CTransaction &tx) {
164 std::vector<int> prev_heights;
165 prev_heights.resize(tx.vin.size());
166 for (size_t i = 0; i < tx.vin.size(); ++i) {
167 const CTxIn &txin = tx.vin[i];
168 Coin coin;
169 if (!coins.GetCoin(txin.prevout, coin)) {
170 LogPrintf("ERROR: %s: Missing input %d in transaction \'%s\'\n",
171 __func__, i, tx.GetId().GetHex());
172 return std::nullopt;
173 }
174 if (coin.GetHeight() == MEMPOOL_HEIGHT) {
175 // Assume all mempool transaction confirm in the next block.
176 prev_heights[i] = tip.nHeight + 1;
177 } else {
178 prev_heights[i] = coin.GetHeight();
179 }
180 }
181 return prev_heights;
182}
183} // namespace
184
185std::optional<LockPoints> CalculateLockPointsAtTip(CBlockIndex *tip,
186 const CCoinsView &coins_view,
187 const CTransaction &tx) {
188 assert(tip);
189
190 auto prev_heights{CalculatePrevHeights(*tip, coins_view, tx)};
191 if (!prev_heights.has_value()) {
192 return std::nullopt;
193 }
194
195 CBlockIndex next_tip;
196 next_tip.pprev = tip;
197 // When SequenceLocks() is called within ConnectBlock(), the height
198 // of the block *being* evaluated is what is used.
199 // Thus if we want to know if a transaction can be part of the
200 // *next* block, we need to use one more than
201 // active_chainstate.m_chain.Height()
202 next_tip.nHeight = tip->nHeight + 1;
203 const auto [min_height, min_time] = CalculateSequenceLocks(
204 tx, STANDARD_LOCKTIME_VERIFY_FLAGS, prev_heights.value(), next_tip);
205
206 return LockPoints{min_height, min_time};
207}
208
209bool CheckSequenceLocksAtTip(CBlockIndex *tip, const LockPoints &lock_points) {
210 assert(tip != nullptr);
211
212 CBlockIndex index;
213 index.pprev = tip;
214 // CheckSequenceLocksAtTip() uses active_chainstate.m_chain.Height()+1 to
215 // evaluate height based locks because when SequenceLocks() is called within
216 // ConnectBlock(), the height of the block *being* evaluated is what is
217 // used. Thus if we want to know if a transaction can be part of the *next*
218 // block, we need to use one more than active_chainstate.m_chain.Height()
219 index.nHeight = tip->nHeight + 1;
220
221 return EvaluateSequenceLocks(index, {lock_points.height, lock_points.time});
222}
223
224// Command-line argument "-replayprotectionactivationtime=<timestamp>" will
225// cause the node to switch to replay protected SigHash ForkID value when the
226// median timestamp of the previous 11 blocks is greater than or equal to
227// <timestamp>. Defaults to the pre-defined timestamp when not set.
229 int64_t nMedianTimePast) {
230 return nMedianTimePast >= gArgs.GetIntArg("-replayprotectionactivationtime",
232}
233
235 const CBlockIndex *pindexPrev) {
236 if (pindexPrev == nullptr) {
237 return false;
238 }
239
240 return IsReplayProtectionEnabled(params, pindexPrev->GetMedianTimePast());
241}
242
249 const CTransaction &tx, TxValidationState &state,
250 const CCoinsViewCache &view, const CTxMemPool &pool, const uint32_t flags,
251 PrecomputedTransactionData &txdata, int &nSigChecksOut,
255
256 assert(!tx.IsCoinBase());
257 for (const CTxIn &txin : tx.vin) {
258 const Coin &coin = view.AccessCoin(txin.prevout);
259
260 // This coin was checked in PreChecks and MemPoolAccept
261 // has been holding cs_main since then.
262 Assume(!coin.IsSpent());
263 if (coin.IsSpent()) {
264 return false;
265 }
266
267 // If the Coin is available, there are 2 possibilities:
268 // it is available in our current ChainstateActive UTXO set,
269 // or it's a UTXO provided by a transaction in our mempool.
270 // Ensure the scriptPubKeys in Coins from CoinsView are correct.
271 const CTransactionRef &txFrom = pool.get(txin.prevout.GetTxId());
272 if (txFrom) {
273 assert(txFrom->GetId() == txin.prevout.GetTxId());
274 assert(txFrom->vout.size() > txin.prevout.GetN());
275 assert(txFrom->vout[txin.prevout.GetN()] == coin.GetTxOut());
276 } else {
277 const Coin &coinFromUTXOSet = coins_tip.AccessCoin(txin.prevout);
278 assert(!coinFromUTXOSet.IsSpent());
279 assert(coinFromUTXOSet.GetTxOut() == coin.GetTxOut());
280 }
281 }
282
283 // Call CheckInputScripts() to cache signature and script validity against
284 // current tip consensus rules.
285 return CheckInputScripts(tx, state, view, flags, /*sigCacheStore=*/true,
286 /*scriptCacheStore=*/true, txdata, nSigChecksOut);
287}
288
289namespace {
290
291class MemPoolAccept {
292public:
293 MemPoolAccept(CTxMemPool &mempool, Chainstate &active_chainstate)
294 : m_pool(mempool), m_view(&m_dummy),
295 m_viewmempool(&active_chainstate.CoinsTip(), m_pool),
296 m_active_chainstate(active_chainstate) {}
297
298 // We put the arguments we're handed into a struct, so we can pass them
299 // around easier.
300 struct ATMPArgs {
301 const Config &m_config;
302 const int64_t m_accept_time;
303 const bool m_bypass_limits;
304 /*
305 * Return any outpoints which were not previously present in the coins
306 * cache, but were added as a result of validating the tx for mempool
307 * acceptance. This allows the caller to optionally remove the cache
308 * additions if the associated transaction ends up being rejected by
309 * the mempool.
310 */
311 std::vector<COutPoint> &m_coins_to_uncache;
312 const bool m_test_accept;
313 const unsigned int m_heightOverride;
319 const bool m_package_submission;
325 const bool m_package_feerates;
326
328 static ATMPArgs SingleAccept(const Config &config, int64_t accept_time,
329 bool bypass_limits,
330 std::vector<COutPoint> &coins_to_uncache,
331 bool test_accept,
332 unsigned int heightOverride) {
333 return ATMPArgs{
334 config,
335 accept_time,
336 bypass_limits,
337 coins_to_uncache,
338 test_accept,
339 heightOverride,
340 /*package_submission=*/false,
341 /*package_feerates=*/false,
342 };
343 }
344
349 static ATMPArgs
350 PackageTestAccept(const Config &config, int64_t accept_time,
351 std::vector<COutPoint> &coins_to_uncache) {
352 return ATMPArgs{
353 config,
354 accept_time,
355 /*bypass_limits=*/false,
356 coins_to_uncache,
357 /*test_accept=*/true,
358 /*height_override=*/0,
359 // not submitting to mempool
360 /*package_submission=*/false,
361 /*package_feerates=*/false,
362 };
363 }
364
366 static ATMPArgs
367 PackageChildWithParents(const Config &config, int64_t accept_time,
368 std::vector<COutPoint> &coins_to_uncache) {
369 return ATMPArgs{
370 config,
371 accept_time,
372 /*bypass_limits=*/false,
373 coins_to_uncache,
374 /*test_accept=*/false,
375 /*height_override=*/0,
376 /*package_submission=*/true,
377 /*package_feerates=*/true,
378 };
379 }
380
382 static ATMPArgs SingleInPackageAccept(const ATMPArgs &package_args) {
383 return ATMPArgs{
384 /*config=*/package_args.m_config,
385 /*accept_time=*/package_args.m_accept_time,
386 /*bypass_limits=*/false,
387 /*coins_to_uncache=*/package_args.m_coins_to_uncache,
388 /*test_accept=*/package_args.m_test_accept,
389 /*height_override=*/package_args.m_heightOverride,
390 // do not LimitMempoolSize in Finalize()
391 /*package_submission=*/true,
392 // only 1 transaction
393 /*package_feerates=*/false,
394 };
395 }
396
397 private:
398 // Private ctor to avoid exposing details to clients and allowing the
399 // possibility of mixing up the order of the arguments. Use static
400 // functions above instead.
401 ATMPArgs(const Config &config, int64_t accept_time, bool bypass_limits,
402 std::vector<COutPoint> &coins_to_uncache, bool test_accept,
403 unsigned int height_override, bool package_submission,
404 bool package_feerates)
405 : m_config{config}, m_accept_time{accept_time},
406 m_bypass_limits{bypass_limits},
407 m_coins_to_uncache{coins_to_uncache}, m_test_accept{test_accept},
408 m_heightOverride{height_override},
409 m_package_submission{package_submission},
410 m_package_feerates(package_feerates) {}
411 };
412
413 // Single transaction acceptance
414 MempoolAcceptResult AcceptSingleTransaction(const CTransactionRef &ptx,
415 ATMPArgs &args)
417
425 AcceptMultipleTransactions(const std::vector<CTransactionRef> &txns,
426 ATMPArgs &args)
428
442 AcceptSubPackage(const std::vector<CTransactionRef> &subpackage,
443 ATMPArgs &args)
445
451 PackageMempoolAcceptResult AcceptPackage(const Package &package,
452 ATMPArgs &args)
454
455private:
456 // All the intermediate state that gets passed between the various levels
457 // of checking a given transaction.
458 struct Workspace {
459 Workspace(const CTransactionRef &ptx,
460 const uint32_t next_block_script_verify_flags)
461 : m_ptx(ptx),
462 m_next_block_script_verify_flags(next_block_script_verify_flags) {
463 }
469 std::unique_ptr<CTxMemPoolEntry> m_entry;
470
475 int64_t m_vsize;
480 Amount m_base_fees;
481
486 Amount m_modified_fees;
487
494 CFeeRate m_package_feerate{Amount::zero()};
495
496 const CTransactionRef &m_ptx;
497 TxValidationState m_state;
503 PrecomputedTransactionData m_precomputed_txdata;
504
505 // ABC specific flags that are used in both PreChecks and
506 // ConsensusScriptChecks
507 const uint32_t m_next_block_script_verify_flags;
508 int m_sig_checks_standard;
509 };
510
511 // Run the policy checks on a given transaction, excluding any script
512 // checks. Looks up inputs, calculates feerate, considers replacement,
513 // evaluates package limits, etc. As this function can be invoked for "free"
514 // by a peer, only tests that are fast should be done here (to avoid CPU
515 // DoS).
516 bool PreChecks(ATMPArgs &args, Workspace &ws)
518
519 // Re-run the script checks, using consensus flags, and try to cache the
520 // result in the scriptcache. This should be done after
521 // PolicyScriptChecks(). This requires that all inputs either be in our
522 // utxo set or in the mempool.
523 bool ConsensusScriptChecks(const ATMPArgs &args, Workspace &ws)
525
526 // Try to add the transaction to the mempool, removing any conflicts first.
527 // Returns true if the transaction is in the mempool after any size
528 // limiting is performed, false otherwise.
529 bool Finalize(const ATMPArgs &args, Workspace &ws)
531
532 // Submit all transactions to the mempool and call ConsensusScriptChecks to
533 // add to the script cache - should only be called after successful
534 // validation of all transactions in the package.
535 // Does not call LimitMempoolSize(), so mempool max_size_bytes may be
536 // temporarily exceeded.
537 bool SubmitPackage(const ATMPArgs &args, std::vector<Workspace> &workspaces,
538 PackageValidationState &package_state,
539 std::map<TxId, MempoolAcceptResult> &results)
541
542 // Compare a package's feerate against minimum allowed.
543 bool CheckFeeRate(size_t package_size, size_t package_vsize,
544 Amount package_fee, TxValidationState &state)
547 AssertLockHeld(m_pool.cs);
548
549 const Amount mempoolRejectFee =
550 m_pool.GetMinFee().GetFee(package_vsize);
551
552 if (mempoolRejectFee > Amount::zero() &&
553 package_fee < mempoolRejectFee) {
554 return state.Invalid(
556 "mempool min fee not met",
557 strprintf("%d < %d", package_fee, mempoolRejectFee));
558 }
559
560 // Do not change this to use virtualsize without coordinating a network
561 // policy upgrade.
562 if (package_fee < m_pool.m_min_relay_feerate.GetFee(package_size)) {
563 return state.Invalid(
565 "min relay fee not met",
566 strprintf("%d < %d", package_fee,
567 m_pool.m_min_relay_feerate.GetFee(package_size)));
568 }
569
570 return true;
571 }
572
573private:
574 CTxMemPool &m_pool;
575 CCoinsViewCache m_view;
576 CCoinsViewMemPool m_viewmempool;
577 CCoinsView m_dummy;
578
579 Chainstate &m_active_chainstate;
580};
581
582bool MemPoolAccept::PreChecks(ATMPArgs &args, Workspace &ws) {
584 AssertLockHeld(m_pool.cs);
585 const CTransactionRef &ptx = ws.m_ptx;
586 const CTransaction &tx = *ws.m_ptx;
587 const TxId &txid = ws.m_ptx->GetId();
588
589 // Copy/alias what we need out of args
590 const int64_t nAcceptTime = args.m_accept_time;
591 const bool bypass_limits = args.m_bypass_limits;
592 std::vector<COutPoint> &coins_to_uncache = args.m_coins_to_uncache;
593 const unsigned int heightOverride = args.m_heightOverride;
594
595 // Alias what we need out of ws
596 TxValidationState &state = ws.m_state;
597 // Coinbase is only valid in a block, not as a loose transaction.
598 if (!CheckRegularTransaction(tx, state)) {
599 // state filled in by CheckRegularTransaction.
600 return false;
601 }
602
603 // Rather not work on nonstandard transactions (unless -testnet)
604 std::string reason;
605 if (m_pool.m_require_standard &&
606 !IsStandardTx(tx, m_pool.m_max_datacarrier_bytes,
607 m_pool.m_permit_bare_multisig,
608 m_pool.m_dust_relay_feerate, reason)) {
609 return state.Invalid(TxValidationResult::TX_NOT_STANDARD, reason);
610 }
611
612 // Only accept nLockTime-using transactions that can be mined in the next
613 // block; we don't want our mempool filled up with transactions that can't
614 // be mined yet.
615 TxValidationState ctxState;
617 *Assert(m_active_chainstate.m_chain.Tip()),
618 args.m_config.GetChainParams().GetConsensus(), tx, ctxState)) {
619 // We copy the state from a dummy to ensure we don't increase the
620 // ban score of peer for transaction that could be valid in the future.
622 ctxState.GetRejectReason(),
623 ctxState.GetDebugMessage());
624 }
625
626 // Is it already in the memory pool?
627 if (m_pool.exists(txid)) {
629 "txn-already-in-mempool");
630 }
631
632 // Check for conflicts with in-memory transactions
633 for (const CTxIn &txin : tx.vin) {
634 if (const auto ptxConflicting = m_pool.GetConflictTx(txin.prevout)) {
635 if (m_pool.isAvalancheFinalized(ptxConflicting->GetId())) {
637 "finalized-tx-conflict");
638 }
639
640 return state.Invalid(
642 "txn-mempool-conflict");
643 }
644 }
645
646 m_view.SetBackend(m_viewmempool);
647
648 const CCoinsViewCache &coins_cache = m_active_chainstate.CoinsTip();
649 // Do all inputs exist?
650 for (const CTxIn &txin : tx.vin) {
651 if (!coins_cache.HaveCoinInCache(txin.prevout)) {
652 coins_to_uncache.push_back(txin.prevout);
653 }
654
655 // Note: this call may add txin.prevout to the coins cache
656 // (coins_cache.cacheCoins) by way of FetchCoin(). It should be
657 // removed later (via coins_to_uncache) if this tx turns out to be
658 // invalid.
659 if (!m_view.HaveCoin(txin.prevout)) {
660 // Are inputs missing because we already have the tx?
661 for (size_t out = 0; out < tx.vout.size(); out++) {
662 // Optimistically just do efficient check of cache for
663 // outputs.
664 if (coins_cache.HaveCoinInCache(COutPoint(txid, out))) {
666 "txn-already-known");
667 }
668 }
669
670 // Otherwise assume this might be an orphan tx for which we just
671 // haven't seen parents yet.
673 "bad-txns-inputs-missingorspent");
674 }
675 }
676
677 // Are the actual inputs available?
678 if (!m_view.HaveInputs(tx)) {
680 "bad-txns-inputs-spent");
681 }
682
683 // Bring the best block into scope.
684 m_view.GetBestBlock();
685
686 // we have all inputs cached now, so switch back to dummy (to protect
687 // against bugs where we pull more inputs from disk that miss being
688 // added to coins_to_uncache)
689 m_view.SetBackend(m_dummy);
690
691 assert(m_active_chainstate.m_blockman.LookupBlockIndex(
692 m_view.GetBestBlock()) == m_active_chainstate.m_chain.Tip());
693
694 // Only accept BIP68 sequence locked transactions that can be mined in
695 // the next block; we don't want our mempool filled up with transactions
696 // that can't be mined yet.
697 // Pass in m_view which has all of the relevant inputs cached. Note that,
698 // since m_view's backend was removed, it no longer pulls coins from the
699 // mempool.
700 const std::optional<LockPoints> lock_points{CalculateLockPointsAtTip(
701 m_active_chainstate.m_chain.Tip(), m_view, tx)};
702 if (!lock_points.has_value() ||
703 !CheckSequenceLocksAtTip(m_active_chainstate.m_chain.Tip(),
704 *lock_points)) {
706 "non-BIP68-final");
707 }
708
709 // The mempool holds txs for the next block, so pass height+1 to
710 // CheckTxInputs
711 if (!Consensus::CheckTxInputs(tx, state, m_view,
712 m_active_chainstate.m_chain.Height() + 1,
713 ws.m_base_fees)) {
714 // state filled in by CheckTxInputs
715 return false;
716 }
717
718 // Check for non-standard pay-to-script-hash in inputs
719 if (m_pool.m_require_standard &&
720 !AreInputsStandard(tx, m_view, ws.m_next_block_script_verify_flags)) {
722 "bad-txns-nonstandard-inputs");
723 }
724
725 // ws.m_modified_fess includes any fee deltas from PrioritiseTransaction
726 ws.m_modified_fees = ws.m_base_fees;
727 m_pool.ApplyDelta(txid, ws.m_modified_fees);
728
729 unsigned int nSize = tx.GetTotalSize();
730
731 // Validate input scripts against standard script flags.
732 const uint32_t scriptVerifyFlags =
733 ws.m_next_block_script_verify_flags | STANDARD_SCRIPT_VERIFY_FLAGS;
734 ws.m_precomputed_txdata = PrecomputedTransactionData{tx};
735 if (!CheckInputScripts(tx, state, m_view, scriptVerifyFlags, true, false,
736 ws.m_precomputed_txdata, ws.m_sig_checks_standard)) {
737 // State filled in by CheckInputScripts
738 return false;
739 }
740
741 ws.m_entry = std::make_unique<CTxMemPoolEntry>(
742 ptx, ws.m_base_fees, nAcceptTime,
743 heightOverride ? heightOverride : m_active_chainstate.m_chain.Height(),
744 ws.m_sig_checks_standard, lock_points.value());
745
746 ws.m_vsize = ws.m_entry->GetTxVirtualSize();
747
748 // No individual transactions are allowed below the min relay feerate except
749 // from disconnected blocks. This requirement, unlike CheckFeeRate, cannot
750 // be bypassed using m_package_feerates because, while a tx could be package
751 // CPFP'd when entering the mempool, we do not have a DoS-resistant method
752 // of ensuring the tx remains bumped. For example, the fee-bumping child
753 // could disappear due to a replacement.
754 if (!bypass_limits &&
755 ws.m_modified_fees <
756 m_pool.m_min_relay_feerate.GetFee(ws.m_ptx->GetTotalSize())) {
757 // Even though this is a fee-related failure, this result is
758 // TX_MEMPOOL_POLICY, not TX_PACKAGE_RECONSIDERABLE, because it cannot
759 // be bypassed using package validation.
760 return state.Invalid(
761 TxValidationResult::TX_MEMPOOL_POLICY, "min relay fee not met",
762 strprintf("%d < %d", ws.m_modified_fees,
763 m_pool.m_min_relay_feerate.GetFee(nSize)));
764 }
765 // No individual transactions are allowed below the mempool min feerate
766 // except from disconnected blocks and transactions in a package. Package
767 // transactions will be checked using package feerate later.
768 if (!bypass_limits && !args.m_package_feerates &&
769 !CheckFeeRate(nSize, ws.m_vsize, ws.m_modified_fees, state)) {
770 return false;
771 }
772
773 return true;
774}
775
776bool MemPoolAccept::ConsensusScriptChecks(const ATMPArgs &args, Workspace &ws) {
778 AssertLockHeld(m_pool.cs);
779 const CTransaction &tx = *ws.m_ptx;
780 const TxId &txid = tx.GetId();
781 TxValidationState &state = ws.m_state;
782
783 // Check again against the next block's script verification flags
784 // to cache our script execution flags.
785 //
786 // This is also useful in case of bugs in the standard flags that cause
787 // transactions to pass as valid when they're actually invalid. For
788 // instance the STRICTENC flag was incorrectly allowing certain CHECKSIG
789 // NOT scripts to pass, even though they were invalid.
790 //
791 // There is a similar check in CreateNewBlock() to prevent creating
792 // invalid blocks (using TestBlockValidity), however allowing such
793 // transactions into the mempool can be exploited as a DoS attack.
794 int nSigChecksConsensus;
796 tx, state, m_view, m_pool, ws.m_next_block_script_verify_flags,
797 ws.m_precomputed_txdata, nSigChecksConsensus,
798 m_active_chainstate.CoinsTip())) {
799 // This can occur under some circumstances, if the node receives an
800 // unrequested tx which is invalid due to new consensus rules not
801 // being activated yet (during IBD).
802 LogPrintf("BUG! PLEASE REPORT THIS! CheckInputScripts failed against "
803 "latest-block but not STANDARD flags %s, %s\n",
804 txid.ToString(), state.ToString());
805 return Assume(false);
806 }
807
808 if (ws.m_sig_checks_standard != nSigChecksConsensus) {
809 // We can't accept this transaction as we've used the standard count
810 // for the mempool/mining, but the consensus count will be enforced
811 // in validation (we don't want to produce bad block templates).
812 return error(
813 "%s: BUG! PLEASE REPORT THIS! SigChecks count differed between "
814 "standard and consensus flags in %s",
815 __func__, txid.ToString());
816 }
817 return true;
818}
819
820// Get the coins spent by ptx from the coins_view. Assumes coins are present.
821static std::vector<Coin> getSpentCoins(const CTransactionRef &ptx,
822 const CCoinsViewCache &coins_view) {
823 std::vector<Coin> spent_coins;
824 spent_coins.reserve(ptx->vin.size());
825 for (const CTxIn &input : ptx->vin) {
826 Coin coin;
827 const bool coinFound = coins_view.GetCoin(input.prevout, coin);
828 Assume(coinFound);
829 spent_coins.push_back(std::move(coin));
830 }
831 return spent_coins;
832}
833
834bool MemPoolAccept::Finalize(const ATMPArgs &args, Workspace &ws) {
836 AssertLockHeld(m_pool.cs);
837 const TxId &txid = ws.m_ptx->GetId();
838 TxValidationState &state = ws.m_state;
839 const bool bypass_limits = args.m_bypass_limits;
840
841 // Store transaction in memory
842 CTxMemPoolEntry *pentry = ws.m_entry.release();
843 auto entry = CTxMemPoolEntryRef::acquire(pentry);
844 m_pool.addUnchecked(entry);
845
847 ws.m_ptx,
848 std::make_shared<const std::vector<Coin>>(
849 getSpentCoins(ws.m_ptx, m_view)),
850 m_pool.GetAndIncrementSequence());
851
852 // Trim mempool and check if tx was trimmed.
853 // If we are validating a package, don't trim here because we could evict a
854 // previous transaction in the package. LimitMempoolSize() should be called
855 // at the very end to make sure the mempool is still within limits and
856 // package submission happens atomically.
857 if (!args.m_package_submission && !bypass_limits) {
858 m_pool.LimitSize(m_active_chainstate.CoinsTip());
859 if (!m_pool.exists(txid)) {
860 // The tx no longer meets our (new) mempool minimum feerate but
861 // could be reconsidered in a package.
863 "mempool full");
864 }
865 }
866 return true;
867}
868
869bool MemPoolAccept::SubmitPackage(
870 const ATMPArgs &args, std::vector<Workspace> &workspaces,
871 PackageValidationState &package_state,
872 std::map<TxId, MempoolAcceptResult> &results) {
874 AssertLockHeld(m_pool.cs);
875 // Sanity check: none of the transactions should be in the mempool.
876 assert(std::all_of(
877 workspaces.cbegin(), workspaces.cend(),
878 [this](const auto &ws) { return !m_pool.exists(ws.m_ptx->GetId()); }));
879
880 bool all_submitted = true;
881 // ConsensusScriptChecks adds to the script cache and is therefore
882 // consensus-critical; CheckInputsFromMempoolAndCache asserts that
883 // transactions only spend coins available from the mempool or UTXO set.
884 // Submit each transaction to the mempool immediately after calling
885 // ConsensusScriptChecks to make the outputs available for subsequent
886 // transactions.
887 for (Workspace &ws : workspaces) {
888 if (!ConsensusScriptChecks(args, ws)) {
889 results.emplace(ws.m_ptx->GetId(),
890 MempoolAcceptResult::Failure(ws.m_state));
891 // Since PreChecks() passed, this should never fail.
892 all_submitted = false;
893 package_state.Invalid(
895 strprintf("BUG! PolicyScriptChecks succeeded but "
896 "ConsensusScriptChecks failed: %s",
897 ws.m_ptx->GetId().ToString()));
898 }
899
900 // If we call LimitMempoolSize() for each individual Finalize(), the
901 // mempool will not take the transaction's descendant feerate into
902 // account because it hasn't seen them yet. Also, we risk evicting a
903 // transaction that a subsequent package transaction depends on.
904 // Instead, allow the mempool to temporarily bypass limits, the maximum
905 // package size) while submitting transactions individually and then
906 // trim at the very end.
907 if (!Finalize(args, ws)) {
908 results.emplace(ws.m_ptx->GetId(),
909 MempoolAcceptResult::Failure(ws.m_state));
910 // Since LimitMempoolSize() won't be called, this should never fail.
911 all_submitted = false;
913 strprintf("BUG! Adding to mempool failed: %s",
914 ws.m_ptx->GetId().ToString()));
915 }
916 }
917
918 // It may or may not be the case that all the transactions made it into the
919 // mempool. Regardless, make sure we haven't exceeded max mempool size.
920 m_pool.LimitSize(m_active_chainstate.CoinsTip());
921
922 std::vector<TxId> all_package_txids;
923 all_package_txids.reserve(workspaces.size());
924 std::transform(workspaces.cbegin(), workspaces.cend(),
925 std::back_inserter(all_package_txids),
926 [](const auto &ws) { return ws.m_ptx->GetId(); });
927
928 // Add successful results. The returned results may change later if
929 // LimitMempoolSize() evicts them.
930 for (Workspace &ws : workspaces) {
931 const auto effective_feerate =
932 args.m_package_feerates
933 ? ws.m_package_feerate
934 : CFeeRate{ws.m_modified_fees,
935 static_cast<uint32_t>(ws.m_vsize)};
936 const auto effective_feerate_txids =
937 args.m_package_feerates ? all_package_txids
938 : std::vector<TxId>({ws.m_ptx->GetId()});
939 results.emplace(ws.m_ptx->GetId(),
940 MempoolAcceptResult::Success(ws.m_vsize, ws.m_base_fees,
941 effective_feerate,
942 effective_feerate_txids));
943 }
944 return all_submitted;
945}
946
948MemPoolAccept::AcceptSingleTransaction(const CTransactionRef &ptx,
949 ATMPArgs &args) {
951 // mempool "read lock" (held through
952 // GetMainSignals().TransactionAddedToMempool())
953 LOCK(m_pool.cs);
954
955 const CBlockIndex *tip = m_active_chainstate.m_chain.Tip();
956
957 Workspace ws(ptx,
958 GetNextBlockScriptFlags(tip, m_active_chainstate.m_chainman));
959
960 const std::vector<TxId> single_txid{ws.m_ptx->GetId()};
961
962 // Perform the inexpensive checks first and avoid hashing and signature
963 // verification unless those checks pass, to mitigate CPU exhaustion
964 // denial-of-service attacks.
965 if (!PreChecks(args, ws)) {
966 if (ws.m_state.GetResult() ==
968 // Failed for fee reasons. Provide the effective feerate and which
969 // tx was included.
971 ws.m_state, CFeeRate(ws.m_modified_fees, ws.m_vsize),
972 single_txid);
973 }
974 return MempoolAcceptResult::Failure(ws.m_state);
975 }
976
977 if (!ConsensusScriptChecks(args, ws)) {
978 return MempoolAcceptResult::Failure(ws.m_state);
979 }
980
981 const TxId txid = ptx->GetId();
982
983 // Mempool sanity check -- in our new mempool no tx can be added if its
984 // outputs are already spent in the mempool (that is, no children before
985 // parents allowed; the mempool must be consistent at all times).
986 //
987 // This means that on reorg, the disconnectpool *must* always import
988 // the existing mempool tx's, clear the mempool, and then re-add
989 // remaining tx's in topological order via this function. Our new mempool
990 // has fast adds, so this is ok.
991 if (auto it = m_pool.mapNextTx.lower_bound(COutPoint{txid, 0});
992 it != m_pool.mapNextTx.end() && it->first->GetTxId() == txid) {
993 LogPrintf("%s: BUG! PLEASE REPORT THIS! Attempt to add txid %s, but "
994 "its outputs are already spent in the "
995 "mempool\n",
996 __func__, txid.ToString());
998 "txn-child-before-parent");
999 return MempoolAcceptResult::Failure(ws.m_state);
1000 }
1001
1002 const CFeeRate effective_feerate{ws.m_modified_fees,
1003 static_cast<uint32_t>(ws.m_vsize)};
1004 // Tx was accepted, but not added
1005 if (args.m_test_accept) {
1006 return MempoolAcceptResult::Success(ws.m_vsize, ws.m_base_fees,
1007 effective_feerate, single_txid);
1008 }
1009
1010 if (!Finalize(args, ws)) {
1011 // The only possible failure reason is fee-related (mempool full).
1012 // Failed for fee reasons. Provide the effective feerate and which txns
1013 // were included.
1014 Assume(ws.m_state.GetResult() ==
1017 ws.m_state, CFeeRate(ws.m_modified_fees, ws.m_vsize), single_txid);
1018 }
1019
1020 return MempoolAcceptResult::Success(ws.m_vsize, ws.m_base_fees,
1021 effective_feerate, single_txid);
1022}
1023
1024PackageMempoolAcceptResult MemPoolAccept::AcceptMultipleTransactions(
1025 const std::vector<CTransactionRef> &txns, ATMPArgs &args) {
1027
1028 // These context-free package limits can be done before taking the mempool
1029 // lock.
1030 PackageValidationState package_state;
1031 if (!CheckPackage(txns, package_state)) {
1032 return PackageMempoolAcceptResult(package_state, {});
1033 }
1034
1035 std::vector<Workspace> workspaces{};
1036 workspaces.reserve(txns.size());
1037 std::transform(
1038 txns.cbegin(), txns.cend(), std::back_inserter(workspaces),
1039 [this](const auto &tx) {
1040 return Workspace(
1041 tx, GetNextBlockScriptFlags(m_active_chainstate.m_chain.Tip(),
1042 m_active_chainstate.m_chainman));
1043 });
1044 std::map<TxId, MempoolAcceptResult> results;
1045
1046 LOCK(m_pool.cs);
1047
1048 // Do all PreChecks first and fail fast to avoid running expensive script
1049 // checks when unnecessary.
1050 std::vector<TxId> valid_txids;
1051 for (Workspace &ws : workspaces) {
1052 if (!PreChecks(args, ws)) {
1054 "transaction failed");
1055 // Exit early to avoid doing pointless work. Update the failed tx
1056 // result; the rest are unfinished.
1057 results.emplace(ws.m_ptx->GetId(),
1058 MempoolAcceptResult::Failure(ws.m_state));
1059 return PackageMempoolAcceptResult(package_state,
1060 std::move(results));
1061 }
1062 // Make the coins created by this transaction available for subsequent
1063 // transactions in the package to spend.
1064 m_viewmempool.PackageAddTransaction(ws.m_ptx);
1065 valid_txids.push_back(ws.m_ptx->GetId());
1066 }
1067
1068 // Transactions must meet two minimum feerates: the mempool minimum fee and
1069 // min relay fee. For transactions consisting of exactly one child and its
1070 // parents, it suffices to use the package feerate
1071 // (total modified fees / total size or vsize) to check this requirement.
1072 // Note that this is an aggregate feerate; this function has not checked
1073 // that there are transactions too low feerate to pay for themselves, or
1074 // that the child transactions are higher feerate than their parents. Using
1075 // aggregate feerate may allow "parents pay for child" behavior and permit
1076 // a child that is below mempool minimum feerate. To avoid these behaviors,
1077 // callers of AcceptMultipleTransactions need to restrict txns topology
1078 // (e.g. to ancestor sets) and check the feerates of individuals and
1079 // subsets.
1080 const auto m_total_size = std::accumulate(
1081 workspaces.cbegin(), workspaces.cend(), int64_t{0},
1082 [](int64_t sum, auto &ws) { return sum + ws.m_ptx->GetTotalSize(); });
1083 const auto m_total_vsize =
1084 std::accumulate(workspaces.cbegin(), workspaces.cend(), int64_t{0},
1085 [](int64_t sum, auto &ws) { return sum + ws.m_vsize; });
1086 const auto m_total_modified_fees = std::accumulate(
1087 workspaces.cbegin(), workspaces.cend(), Amount::zero(),
1088 [](Amount sum, auto &ws) { return sum + ws.m_modified_fees; });
1089 const CFeeRate package_feerate(m_total_modified_fees, m_total_vsize);
1090 std::vector<TxId> all_package_txids;
1091 all_package_txids.reserve(workspaces.size());
1092 std::transform(workspaces.cbegin(), workspaces.cend(),
1093 std::back_inserter(all_package_txids),
1094 [](const auto &ws) { return ws.m_ptx->GetId(); });
1095 TxValidationState placeholder_state;
1096 if (args.m_package_feerates &&
1097 !CheckFeeRate(m_total_size, m_total_vsize, m_total_modified_fees,
1098 placeholder_state)) {
1100 "transaction failed");
1102 package_state, {{workspaces.back().m_ptx->GetId(),
1104 placeholder_state,
1105 CFeeRate(m_total_modified_fees, m_total_vsize),
1106 all_package_txids)}});
1107 }
1108
1109 for (Workspace &ws : workspaces) {
1110 ws.m_package_feerate = package_feerate;
1111 const TxId &ws_txid = ws.m_ptx->GetId();
1112 if (args.m_test_accept &&
1113 std::find(valid_txids.begin(), valid_txids.end(), ws_txid) !=
1114 valid_txids.end()) {
1115 const auto effective_feerate =
1116 args.m_package_feerates
1117 ? ws.m_package_feerate
1118 : CFeeRate{ws.m_modified_fees,
1119 static_cast<uint32_t>(ws.m_vsize)};
1120 const auto effective_feerate_txids =
1121 args.m_package_feerates ? all_package_txids
1122 : std::vector<TxId>{ws.m_ptx->GetId()};
1123 // When test_accept=true, transactions that pass PreChecks
1124 // are valid because there are no further mempool checks (passing
1125 // PreChecks implies passing ConsensusScriptChecks).
1126 results.emplace(ws_txid,
1128 ws.m_vsize, ws.m_base_fees, effective_feerate,
1129 effective_feerate_txids));
1130 }
1131 }
1132
1133 if (args.m_test_accept) {
1134 return PackageMempoolAcceptResult(package_state, std::move(results));
1135 }
1136
1137 if (!SubmitPackage(args, workspaces, package_state, results)) {
1138 // PackageValidationState filled in by SubmitPackage().
1139 return PackageMempoolAcceptResult(package_state, std::move(results));
1140 }
1141
1142 return PackageMempoolAcceptResult(package_state, std::move(results));
1143}
1144
1146MemPoolAccept::AcceptSubPackage(const std::vector<CTransactionRef> &subpackage,
1147 ATMPArgs &args) {
1149 AssertLockHeld(m_pool.cs);
1150
1151 auto result = [&]() EXCLUSIVE_LOCKS_REQUIRED(::cs_main, m_pool.cs) {
1152 if (subpackage.size() > 1) {
1153 return AcceptMultipleTransactions(subpackage, args);
1154 }
1155 const auto &tx = subpackage.front();
1156 ATMPArgs single_args = ATMPArgs::SingleInPackageAccept(args);
1157 const auto single_res = AcceptSingleTransaction(tx, single_args);
1158 PackageValidationState package_state_wrapped;
1159 if (single_res.m_result_type !=
1161 package_state_wrapped.Invalid(PackageValidationResult::PCKG_TX,
1162 "transaction failed");
1163 }
1164 return PackageMempoolAcceptResult(package_state_wrapped,
1165 {{tx->GetId(), single_res}});
1166 }();
1167
1168 // Clean up m_view and m_viewmempool so that other subpackage evaluations
1169 // don't have access to coins they shouldn't. Keep some coins in order to
1170 // minimize re-fetching coins from the UTXO set.
1171 //
1172 // There are 3 kinds of coins in m_view:
1173 // (1) Temporary coins from the transactions in subpackage, constructed by
1174 // m_viewmempool.
1175 // (2) Mempool coins from transactions in the mempool, constructed by
1176 // m_viewmempool.
1177 // (3) Confirmed coins fetched from our current UTXO set.
1178 //
1179 // (1) Temporary coins need to be removed, regardless of whether the
1180 // transaction was submitted. If the transaction was submitted to the
1181 // mempool, m_viewmempool will be able to fetch them from there. If it
1182 // wasn't submitted to mempool, it is incorrect to keep them - future calls
1183 // may try to spend those coins that don't actually exist.
1184 // (2) Mempool coins also need to be removed. If the mempool contents have
1185 // changed as a result of submitting or replacing transactions, coins
1186 // previously fetched from mempool may now be spent or nonexistent. Those
1187 // coins need to be deleted from m_view.
1188 // (3) Confirmed coins don't need to be removed. The chainstate has not
1189 // changed (we are holding cs_main and no blocks have been processed) so the
1190 // confirmed tx cannot disappear like a mempool tx can. The coin may now be
1191 // spent after we submitted a tx to mempool, but we have already checked
1192 // that the package does not have 2 transactions spending the same coin.
1193 // Keeping them in m_view is an optimization to not re-fetch confirmed coins
1194 // if we later look up inputs for this transaction again.
1195 for (const auto &outpoint : m_viewmempool.GetNonBaseCoins()) {
1196 // In addition to resetting m_viewmempool, we also need to manually
1197 // delete these coins from m_view because it caches copies of the coins
1198 // it fetched from m_viewmempool previously.
1199 m_view.Uncache(outpoint);
1200 }
1201 // This deletes the temporary and mempool coins.
1202 m_viewmempool.Reset();
1203 return result;
1204}
1205
1206PackageMempoolAcceptResult MemPoolAccept::AcceptPackage(const Package &package,
1207 ATMPArgs &args) {
1209 // Used if returning a PackageMempoolAcceptResult directly from this
1210 // function.
1211 PackageValidationState package_state_quit_early;
1212
1213 // Check that the package is well-formed. If it isn't, we won't try to
1214 // validate any of the transactions and thus won't return any
1215 // MempoolAcceptResults, just a package-wide error.
1216
1217 // Context-free package checks.
1218 if (!CheckPackage(package, package_state_quit_early)) {
1219 return PackageMempoolAcceptResult(package_state_quit_early, {});
1220 }
1221
1222 // All transactions in the package must be a parent of the last transaction.
1223 // This is just an opportunity for us to fail fast on a context-free check
1224 // without taking the mempool lock.
1225 if (!IsChildWithParents(package)) {
1226 package_state_quit_early.Invalid(PackageValidationResult::PCKG_POLICY,
1227 "package-not-child-with-parents");
1228 return PackageMempoolAcceptResult(package_state_quit_early, {});
1229 }
1230
1231 // IsChildWithParents() guarantees the package is > 1 transactions.
1232 assert(package.size() > 1);
1233 // The package must be 1 child with all of its unconfirmed parents. The
1234 // package is expected to be sorted, so the last transaction is the child.
1235 const auto &child = package.back();
1236 std::unordered_set<TxId, SaltedTxIdHasher> unconfirmed_parent_txids;
1237 std::transform(
1238 package.cbegin(), package.cend() - 1,
1239 std::inserter(unconfirmed_parent_txids, unconfirmed_parent_txids.end()),
1240 [](const auto &tx) { return tx->GetId(); });
1241
1242 // All child inputs must refer to a preceding package transaction or a
1243 // confirmed UTXO. The only way to verify this is to look up the child's
1244 // inputs in our current coins view (not including mempool), and enforce
1245 // that all parents not present in the package be available at chain tip.
1246 // Since this check can bring new coins into the coins cache, keep track of
1247 // these coins and uncache them if we don't end up submitting this package
1248 // to the mempool.
1249 const CCoinsViewCache &coins_tip_cache = m_active_chainstate.CoinsTip();
1250 for (const auto &input : child->vin) {
1251 if (!coins_tip_cache.HaveCoinInCache(input.prevout)) {
1252 args.m_coins_to_uncache.push_back(input.prevout);
1253 }
1254 }
1255 // Using the MemPoolAccept m_view cache allows us to look up these same
1256 // coins faster later. This should be connecting directly to CoinsTip, not
1257 // to m_viewmempool, because we specifically require inputs to be confirmed
1258 // if they aren't in the package.
1259 m_view.SetBackend(m_active_chainstate.CoinsTip());
1260 const auto package_or_confirmed = [this, &unconfirmed_parent_txids](
1261 const auto &input) {
1262 return unconfirmed_parent_txids.count(input.prevout.GetTxId()) > 0 ||
1263 m_view.HaveCoin(input.prevout);
1264 };
1265 if (!std::all_of(child->vin.cbegin(), child->vin.cend(),
1266 package_or_confirmed)) {
1267 package_state_quit_early.Invalid(
1269 "package-not-child-with-unconfirmed-parents");
1270 return PackageMempoolAcceptResult(package_state_quit_early, {});
1271 }
1272 // Protect against bugs where we pull more inputs from disk that miss being
1273 // added to coins_to_uncache. The backend will be connected again when
1274 // needed in PreChecks.
1275 m_view.SetBackend(m_dummy);
1276
1277 LOCK(m_pool.cs);
1278 // Stores results from which we will create the returned
1279 // PackageMempoolAcceptResult. A result may be changed if a mempool
1280 // transaction is evicted later due to LimitMempoolSize().
1281 std::map<TxId, MempoolAcceptResult> results_final;
1282 // Results from individual validation which will be returned if no other
1283 // result is available for this transaction. "Nonfinal" because if a
1284 // transaction fails by itself but succeeds later (i.e. when evaluated with
1285 // a fee-bumping child), the result in this map may be discarded.
1286 std::map<TxId, MempoolAcceptResult> individual_results_nonfinal;
1287 bool quit_early{false};
1288 std::vector<CTransactionRef> txns_package_eval;
1289 for (const auto &tx : package) {
1290 const auto &txid = tx->GetId();
1291 // An already confirmed tx is treated as one not in mempool, because all
1292 // we know is that the inputs aren't available.
1293 if (m_pool.exists(txid)) {
1294 // Exact transaction already exists in the mempool.
1295 // Node operators are free to set their mempool policies however
1296 // they please, nodes may receive transactions in different orders,
1297 // and malicious counterparties may try to take advantage of policy
1298 // differences to pin or delay propagation of transactions. As such,
1299 // it's possible for some package transaction(s) to already be in
1300 // the mempool, and we don't want to reject the entire package in
1301 // that case (as that could be a censorship vector). De-duplicate
1302 // the transactions that are already in the mempool, and only call
1303 // AcceptMultipleTransactions() with the new transactions. This
1304 // ensures we don't double-count transaction counts and sizes when
1305 // checking ancestor/descendant limits, or double-count transaction
1306 // fees for fee-related policy.
1307 auto iter = m_pool.GetIter(txid);
1308 assert(iter != std::nullopt);
1309 results_final.emplace(txid, MempoolAcceptResult::MempoolTx(
1310 (*iter.value())->GetTxSize(),
1311 (*iter.value())->GetFee()));
1312 } else {
1313 // Transaction does not already exist in the mempool.
1314 // Try submitting the transaction on its own.
1315 const auto single_package_res = AcceptSubPackage({tx}, args);
1316 const auto &single_res = single_package_res.m_tx_results.at(txid);
1317 if (single_res.m_result_type ==
1319 // The transaction succeeded on its own and is now in the
1320 // mempool. Don't include it in package validation, because its
1321 // fees should only be "used" once.
1322 assert(m_pool.exists(txid));
1323 results_final.emplace(txid, single_res);
1324 } else if (single_res.m_state.GetResult() !=
1326 single_res.m_state.GetResult() !=
1328 // Package validation policy only differs from individual policy
1329 // in its evaluation of feerate. For example, if a transaction
1330 // fails here due to violation of a consensus rule, the result
1331 // will not change when it is submitted as part of a package. To
1332 // minimize the amount of repeated work, unless the transaction
1333 // fails due to feerate or missing inputs (its parent is a
1334 // previous transaction in the package that failed due to
1335 // feerate), don't run package validation. Note that this
1336 // decision might not make sense if different types of packages
1337 // are allowed in the future. Continue individually validating
1338 // the rest of the transactions, because some of them may still
1339 // be valid.
1340 quit_early = true;
1341 package_state_quit_early.Invalid(
1342 PackageValidationResult::PCKG_TX, "transaction failed");
1343 individual_results_nonfinal.emplace(txid, single_res);
1344 } else {
1345 individual_results_nonfinal.emplace(txid, single_res);
1346 txns_package_eval.push_back(tx);
1347 }
1348 }
1349 }
1350
1351 auto multi_submission_result =
1352 quit_early || txns_package_eval.empty()
1353 ? PackageMempoolAcceptResult(package_state_quit_early, {})
1354 : AcceptSubPackage(txns_package_eval, args);
1355 PackageValidationState &package_state_final =
1356 multi_submission_result.m_state;
1357
1358 // Make sure we haven't exceeded max mempool size.
1359 // Package transactions that were submitted to mempool or already in mempool
1360 // may be evicted.
1361 m_pool.LimitSize(m_active_chainstate.CoinsTip());
1362
1363 for (const auto &tx : package) {
1364 const auto &txid = tx->GetId();
1365 if (multi_submission_result.m_tx_results.count(txid) > 0) {
1366 // We shouldn't have re-submitted if the tx result was already in
1367 // results_final.
1368 Assume(results_final.count(txid) == 0);
1369 // If it was submitted, check to see if the tx is still in the
1370 // mempool. It could have been evicted due to LimitMempoolSize()
1371 // above.
1372 const auto &txresult =
1373 multi_submission_result.m_tx_results.at(txid);
1374 if (txresult.m_result_type ==
1376 !m_pool.exists(txid)) {
1377 package_state_final.Invalid(PackageValidationResult::PCKG_TX,
1378 "transaction failed");
1379 TxValidationState mempool_full_state;
1380 mempool_full_state.Invalid(
1382 results_final.emplace(
1383 txid, MempoolAcceptResult::Failure(mempool_full_state));
1384 } else {
1385 results_final.emplace(txid, txresult);
1386 }
1387 } else if (const auto final_it{results_final.find(txid)};
1388 final_it != results_final.end()) {
1389 // Already-in-mempool transaction. Check to see if it's still there,
1390 // as it could have been evicted when LimitMempoolSize() was called.
1391 Assume(final_it->second.m_result_type !=
1393 Assume(individual_results_nonfinal.count(txid) == 0);
1394 if (!m_pool.exists(tx->GetId())) {
1395 package_state_final.Invalid(PackageValidationResult::PCKG_TX,
1396 "transaction failed");
1397 TxValidationState mempool_full_state;
1398 mempool_full_state.Invalid(
1400 // Replace the previous result.
1401 results_final.erase(txid);
1402 results_final.emplace(
1403 txid, MempoolAcceptResult::Failure(mempool_full_state));
1404 }
1405 } else if (const auto non_final_it{
1406 individual_results_nonfinal.find(txid)};
1407 non_final_it != individual_results_nonfinal.end()) {
1408 Assume(non_final_it->second.m_result_type ==
1410 // Interesting result from previous processing.
1411 results_final.emplace(txid, non_final_it->second);
1412 }
1413 }
1414 Assume(results_final.size() == package.size());
1415 return PackageMempoolAcceptResult(package_state_final,
1416 std::move(results_final));
1417}
1418} // namespace
1419
1421 const CTransactionRef &tx,
1422 int64_t accept_time, bool bypass_limits,
1423 bool test_accept,
1424 unsigned int heightOverride) {
1426 assert(active_chainstate.GetMempool() != nullptr);
1427 CTxMemPool &pool{*active_chainstate.GetMempool()};
1428
1429 std::vector<COutPoint> coins_to_uncache;
1430 auto args = MemPoolAccept::ATMPArgs::SingleAccept(
1431 active_chainstate.m_chainman.GetConfig(), accept_time, bypass_limits,
1432 coins_to_uncache, test_accept, heightOverride);
1433 MempoolAcceptResult result = MemPoolAccept(pool, active_chainstate)
1434 .AcceptSingleTransaction(tx, args);
1436 // Remove coins that were not present in the coins cache before calling
1437 // ATMPW; this is to prevent memory DoS in case we receive a large
1438 // number of invalid transactions that attempt to overrun the in-memory
1439 // coins cache
1440 // (`CCoinsViewCache::cacheCoins`).
1441
1442 for (const COutPoint &outpoint : coins_to_uncache) {
1443 active_chainstate.CoinsTip().Uncache(outpoint);
1444 }
1445 }
1446
1447 // After we've (potentially) uncached entries, ensure our coins cache is
1448 // still within its size limits
1449 BlockValidationState stateDummy;
1450 active_chainstate.FlushStateToDisk(stateDummy, FlushStateMode::PERIODIC);
1451 return result;
1452}
1453
1455 CTxMemPool &pool,
1456 const Package &package,
1457 bool test_accept) {
1459 assert(!package.empty());
1460 assert(std::all_of(package.cbegin(), package.cend(),
1461 [](const auto &tx) { return tx != nullptr; }));
1462
1463 const Config &config = active_chainstate.m_chainman.GetConfig();
1464
1465 std::vector<COutPoint> coins_to_uncache;
1466 auto result = [&]() EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
1468 if (test_accept) {
1469 auto args = MemPoolAccept::ATMPArgs::PackageTestAccept(
1470 config, GetTime(), coins_to_uncache);
1471 return MemPoolAccept(pool, active_chainstate)
1472 .AcceptMultipleTransactions(package, args);
1473 } else {
1474 auto args = MemPoolAccept::ATMPArgs::PackageChildWithParents(
1475 config, GetTime(), coins_to_uncache);
1476 return MemPoolAccept(pool, active_chainstate)
1477 .AcceptPackage(package, args);
1478 }
1479 }();
1480
1481 // Uncache coins pertaining to transactions that were not submitted to the
1482 // mempool.
1483 if (test_accept || result.m_state.IsInvalid()) {
1484 for (const COutPoint &hashTx : coins_to_uncache) {
1485 active_chainstate.CoinsTip().Uncache(hashTx);
1486 }
1487 }
1488 // Ensure the coins cache is still within limits.
1489 BlockValidationState state_dummy;
1490 active_chainstate.FlushStateToDisk(state_dummy, FlushStateMode::PERIODIC);
1491 return result;
1492}
1493
1494Amount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams) {
1495 int halvings = nHeight / consensusParams.nSubsidyHalvingInterval;
1496 // Force block reward to zero when right shift is undefined.
1497 if (halvings >= 64) {
1498 return Amount::zero();
1499 }
1500
1501 Amount nSubsidy = 50 * COIN;
1502 // Subsidy is cut in half every 210,000 blocks which will occur
1503 // approximately every 4 years.
1504 return ((nSubsidy / SATOSHI) >> halvings) * SATOSHI;
1505}
1506
1508 : m_dbview{std::move(db_params), std::move(options)},
1509 m_catcherview(&m_dbview) {}
1510
1511void CoinsViews::InitCache() {
1513 m_cacheview = std::make_unique<CCoinsViewCache>(&m_catcherview);
1514}
1515
1517 ChainstateManager &chainman,
1518 std::optional<BlockHash> from_snapshot_blockhash)
1519 : m_mempool(mempool), m_blockman(blockman), m_chainman(chainman),
1520 m_from_snapshot_blockhash(from_snapshot_blockhash) {}
1521
1522const CBlockIndex *Chainstate::SnapshotBase() {
1524 return nullptr;
1525 }
1526 if (!m_cached_snapshot_base) {
1527 m_cached_snapshot_base = Assert(
1529 }
1530 return m_cached_snapshot_base;
1531}
1532
1533void Chainstate::InitCoinsDB(size_t cache_size_bytes, bool in_memory,
1534 bool should_wipe, std::string leveldb_name) {
1536 leveldb_name += node::SNAPSHOT_CHAINSTATE_SUFFIX;
1537 }
1538
1539 m_coins_views = std::make_unique<CoinsViews>(
1540 DBParams{.path = m_chainman.m_options.datadir / leveldb_name,
1541 .cache_bytes = cache_size_bytes,
1542 .memory_only = in_memory,
1543 .wipe_data = should_wipe,
1544 .obfuscate = true,
1545 .options = m_chainman.m_options.coins_db},
1547}
1548
1549void Chainstate::InitCoinsCache(size_t cache_size_bytes) {
1551 assert(m_coins_views != nullptr);
1552 m_coinstip_cache_size_bytes = cache_size_bytes;
1553 m_coins_views->InitCache();
1554}
1555
1556// Note that though this is marked const, we may end up modifying
1557// `m_cached_finished_ibd`, which is a performance-related implementation
1558// detail. This function must be marked `const` so that `CValidationInterface`
1559// clients (which are given a `const Chainstate*`) can call it.
1560//
1562 // Optimization: pre-test latch before taking the lock.
1563 if (m_cached_finished_ibd.load(std::memory_order_relaxed)) {
1564 return false;
1565 }
1566
1567 LOCK(cs_main);
1568 if (m_cached_finished_ibd.load(std::memory_order_relaxed)) {
1569 return false;
1570 }
1571 if (m_blockman.LoadingBlocks()) {
1572 return true;
1573 }
1574 CChain &chain{ActiveChain()};
1575 if (chain.Tip() == nullptr) {
1576 return true;
1577 }
1578 if (chain.Tip()->nChainWork < MinimumChainWork()) {
1579 return true;
1580 }
1581 if (chain.Tip()->Time() < Now<NodeSeconds>() - m_options.max_tip_age) {
1582 return true;
1583 }
1584 LogPrintf("Leaving InitialBlockDownload (latching to false)\n");
1585 m_cached_finished_ibd.store(true, std::memory_order_relaxed);
1586 return false;
1587}
1588
1591
1592 // Before we get past initial download, we cannot reliably alert about forks
1593 // (we assume we don't get stuck on a fork before finishing our initial
1594 // sync)
1596 return;
1597 }
1598
1599 // If our best fork is no longer within 72 blocks (+/- 12 hours if no one
1600 // mines it) of our head, or if it is back on the active chain, drop it
1603 m_best_fork_tip = nullptr;
1604 }
1605
1606 if (m_best_fork_tip ||
1607 (m_chainman.m_best_invalid &&
1608 m_chainman.m_best_invalid->nChainWork >
1609 m_chain.Tip()->nChainWork + (GetBlockProof(*m_chain.Tip()) * 6))) {
1611 std::string warning =
1612 std::string("'Warning: Large-work fork detected, forking after "
1613 "block ") +
1614 m_best_fork_base->phashBlock->ToString() + std::string("'");
1616 }
1617
1619 LogPrintf("%s: Warning: Large fork found\n forking the "
1620 "chain at height %d (%s)\n lasting to height %d "
1621 "(%s).\nChain state database corruption likely.\n",
1622 __func__, m_best_fork_base->nHeight,
1627 } else {
1628 LogPrintf("%s: Warning: Found invalid chain at least ~6 blocks "
1629 "longer than our best chain.\nChain state database "
1630 "corruption likely.\n",
1631 __func__);
1633 }
1634 } else {
1637 }
1638}
1639
1641 CBlockIndex *pindexNewForkTip) {
1643
1644 // If we are on a fork that is sufficiently large, set a warning flag.
1645 const CBlockIndex *pfork = m_chain.FindFork(pindexNewForkTip);
1646
1647 // We define a condition where we should warn the user about as a fork of at
1648 // least 7 blocks with a tip within 72 blocks (+/- 12 hours if no one mines
1649 // it) of ours. We use 7 blocks rather arbitrarily as it represents just
1650 // under 10% of sustained network hash rate operating on the fork, or a
1651 // chain that is entirely longer than ours and invalid (note that this
1652 // should be detected by both). We define it this way because it allows us
1653 // to only store the highest fork tip (+ base) which meets the 7-block
1654 // condition and from this always have the most-likely-to-cause-warning fork
1655 if (pfork &&
1656 (!m_best_fork_tip ||
1657 pindexNewForkTip->nHeight > m_best_fork_tip->nHeight) &&
1658 pindexNewForkTip->nChainWork - pfork->nChainWork >
1659 (GetBlockProof(*pfork) * 7) &&
1660 m_chain.Height() - pindexNewForkTip->nHeight < 72) {
1661 m_best_fork_tip = pindexNewForkTip;
1662 m_best_fork_base = pfork;
1663 }
1664
1666}
1667
1668// Called both upon regular invalid block discovery *and* InvalidateBlock
1671 if (!m_chainman.m_best_invalid ||
1672 pindexNew->nChainWork > m_chainman.m_best_invalid->nChainWork) {
1673 m_chainman.m_best_invalid = pindexNew;
1674 }
1675 SetBlockFailureFlags(pindexNew);
1676 if (m_chainman.m_best_header != nullptr &&
1677 m_chainman.m_best_header->GetAncestor(pindexNew->nHeight) ==
1678 pindexNew) {
1679 m_chainman.RecalculateBestHeader();
1680 }
1681
1682 // If the invalid chain found is supposed to be finalized, we need to move
1683 // back the finalization point.
1684 if (IsBlockAvalancheFinalized(pindexNew)) {
1686 m_avalancheFinalizedBlockIndex = pindexNew->pprev;
1687 }
1688
1689 LogPrintf("%s: invalid block=%s height=%d log2_work=%f date=%s\n",
1690 __func__, pindexNew->GetBlockHash().ToString(),
1691 pindexNew->nHeight,
1692 log(pindexNew->nChainWork.getdouble()) / log(2.0),
1693 FormatISO8601DateTime(pindexNew->GetBlockTime()));
1694 CBlockIndex *tip = m_chain.Tip();
1695 assert(tip);
1696 LogPrintf("%s: current best=%s height=%d log2_work=%f date=%s\n",
1697 __func__, tip->GetBlockHash().ToString(), m_chain.Height(),
1698 log(tip->nChainWork.getdouble()) / log(2.0),
1700}
1701
1702// Same as InvalidChainFound, above, except not called directly from
1703// InvalidateBlock, which does its own setBlockIndexCandidates management.
1705 const BlockValidationState &state) {
1708 pindex->nStatus = pindex->nStatus.withFailed();
1709 m_chainman.m_failed_blocks.insert(pindex);
1710 m_blockman.m_dirty_blockindex.insert(pindex);
1711 InvalidChainFound(pindex);
1712 }
1713}
1714
1715void SpendCoins(CCoinsViewCache &view, const CTransaction &tx, CTxUndo &txundo,
1716 int nHeight) {
1717 // Mark inputs spent.
1718 if (tx.IsCoinBase()) {
1719 return;
1720 }
1721
1722 txundo.vprevout.reserve(tx.vin.size());
1723 for (const CTxIn &txin : tx.vin) {
1724 txundo.vprevout.emplace_back();
1725 bool is_spent = view.SpendCoin(txin.prevout, &txundo.vprevout.back());
1726 assert(is_spent);
1727 }
1728}
1729
1730void UpdateCoins(CCoinsViewCache &view, const CTransaction &tx, CTxUndo &txundo,
1731 int nHeight) {
1732 SpendCoins(view, tx, txundo, nHeight);
1733 AddCoins(view, tx, nHeight);
1734}
1735
1737 const CScript &scriptSig = ptxTo->vin[nIn].scriptSig;
1738 if (!VerifyScript(scriptSig, m_tx_out.scriptPubKey, nFlags,
1741 metrics, &error)) {
1742 return false;
1743 }
1744 if ((pTxLimitSigChecks &&
1748 // we can't assign a meaningful script error (since the script
1749 // succeeded), but remove the ScriptError::OK which could be
1750 // misinterpreted.
1752 return false;
1753 }
1754 return true;
1755}
1756
1757bool CheckInputScripts(const CTransaction &tx, TxValidationState &state,
1758 const CCoinsViewCache &inputs, const uint32_t flags,
1759 bool sigCacheStore, bool scriptCacheStore,
1760 const PrecomputedTransactionData &txdata,
1761 int &nSigChecksOut, TxSigCheckLimiter &txLimitSigChecks,
1762 CheckInputsLimiter *pBlockLimitSigChecks,
1763 std::vector<CScriptCheck> *pvChecks) {
1765 assert(!tx.IsCoinBase());
1766
1767 if (pvChecks) {
1768 pvChecks->reserve(tx.vin.size());
1769 }
1770
1771 // First check if script executions have been cached with the same flags.
1772 // Note that this assumes that the inputs provided are correct (ie that the
1773 // transaction hash which is in tx's prevouts properly commits to the
1774 // scriptPubKey in the inputs view of that transaction).
1775 ScriptCacheKey hashCacheEntry(tx, flags);
1776 if (IsKeyInScriptCache(hashCacheEntry, !scriptCacheStore, nSigChecksOut)) {
1777 if (!txLimitSigChecks.consume_and_check(nSigChecksOut) ||
1778 (pBlockLimitSigChecks &&
1779 !pBlockLimitSigChecks->consume_and_check(nSigChecksOut))) {
1781 "too-many-sigchecks");
1782 }
1783 return true;
1784 }
1785
1786 int nSigChecksTotal = 0;
1787
1788 for (size_t i = 0; i < tx.vin.size(); i++) {
1789 const COutPoint &prevout = tx.vin[i].prevout;
1790 const Coin &coin = inputs.AccessCoin(prevout);
1791 assert(!coin.IsSpent());
1792
1793 // We very carefully only pass in things to CScriptCheck which are
1794 // clearly committed to by tx's hash. This provides a sanity
1795 // check that our caching is not introducing consensus failures through
1796 // additional data in, eg, the coins being spent being checked as a part
1797 // of CScriptCheck.
1798
1799 // Verify signature
1800 CScriptCheck check(coin.GetTxOut(), tx, i, flags, sigCacheStore, txdata,
1801 &txLimitSigChecks, pBlockLimitSigChecks);
1802
1803 // If pvChecks is not null, defer the check execution to the caller.
1804 if (pvChecks) {
1805 pvChecks->push_back(std::move(check));
1806 continue;
1807 }
1808
1809 if (!check()) {
1810 ScriptError scriptError = check.GetScriptError();
1811 // Compute flags without the optional standardness flags.
1812 // This differs from MANDATORY_SCRIPT_VERIFY_FLAGS as it contains
1813 // additional upgrade flags (see AcceptToMemoryPoolWorker variable
1814 // extraFlags).
1815 uint32_t mandatoryFlags =
1816 flags & ~STANDARD_NOT_MANDATORY_VERIFY_FLAGS;
1817 if (flags != mandatoryFlags) {
1818 // Check whether the failure was caused by a non-mandatory
1819 // script verification check. If so, ensure we return
1820 // NOT_STANDARD instead of CONSENSUS to avoid downstream users
1821 // splitting the network between upgraded and non-upgraded nodes
1822 // by banning CONSENSUS-failing data providers.
1823 CScriptCheck check2(coin.GetTxOut(), tx, i, mandatoryFlags,
1824 sigCacheStore, txdata);
1825 if (check2()) {
1826 return state.Invalid(
1828 strprintf("non-mandatory-script-verify-flag (%s)",
1829 ScriptErrorString(scriptError)));
1830 }
1831 // update the error message to reflect the mandatory violation.
1832 scriptError = check2.GetScriptError();
1833 }
1834
1835 // MANDATORY flag failures correspond to
1836 // TxValidationResult::TX_CONSENSUS. Because CONSENSUS failures are
1837 // the most serious case of validation failures, we may need to
1838 // consider using RECENT_CONSENSUS_CHANGE for any script failure
1839 // that could be due to non-upgraded nodes which we may want to
1840 // support, to avoid splitting the network (but this depends on the
1841 // details of how net_processing handles such errors).
1842 return state.Invalid(
1844 strprintf("mandatory-script-verify-flag-failed (%s)",
1845 ScriptErrorString(scriptError)));
1846 }
1847
1848 nSigChecksTotal += check.GetScriptExecutionMetrics().nSigChecks;
1849 }
1850
1851 nSigChecksOut = nSigChecksTotal;
1852
1853 if (scriptCacheStore && !pvChecks) {
1854 // We executed all of the provided scripts, and were told to cache the
1855 // result. Do so now.
1856 AddKeyInScriptCache(hashCacheEntry, nSigChecksTotal);
1857 }
1858
1859 return true;
1860}
1861
1862bool AbortNode(BlockValidationState &state, const std::string &strMessage,
1863 const bilingual_str &userMessage) {
1864 AbortNode(strMessage, userMessage);
1865 return state.Error(strMessage);
1866}
1867
1870 const COutPoint &out) {
1871 bool fClean = true;
1872
1873 if (view.HaveCoin(out)) {
1874 // Overwriting transaction output.
1875 fClean = false;
1876 }
1877
1878 if (undo.GetHeight() == 0) {
1879 // Missing undo metadata (height and coinbase). Older versions included
1880 // this information only in undo records for the last spend of a
1881 // transactions' outputs. This implies that it must be present for some
1882 // other output of the same tx.
1883 const Coin &alternate = AccessByTxid(view, out.GetTxId());
1884 if (alternate.IsSpent()) {
1885 // Adding output for transaction without known metadata
1887 }
1888
1889 // This is somewhat ugly, but hopefully utility is limited. This is only
1890 // useful when working from legacy on disck data. In any case, putting
1891 // the correct information in there doesn't hurt.
1892 const_cast<Coin &>(undo) = Coin(undo.GetTxOut(), alternate.GetHeight(),
1893 alternate.IsCoinBase());
1894 }
1895
1896 // If the coin already exists as an unspent coin in the cache, then the
1897 // possible_overwrite parameter to AddCoin must be set to true. We have
1898 // already checked whether an unspent coin exists above using HaveCoin, so
1899 // we don't need to guess. When fClean is false, an unspent coin already
1900 // existed and it is an overwrite.
1901 view.AddCoin(out, std::move(undo), !fClean);
1902
1904}
1905
1910DisconnectResult Chainstate::DisconnectBlock(const CBlock &block,
1911 const CBlockIndex *pindex,
1912 CCoinsViewCache &view) {
1914 CBlockUndo blockUndo;
1915 if (!m_blockman.UndoReadFromDisk(blockUndo, *pindex)) {
1916 error("DisconnectBlock(): failure reading undo data");
1918 }
1919
1920 return ApplyBlockUndo(std::move(blockUndo), block, pindex, view);
1921}
1922
1924 const CBlockIndex *pindex,
1925 CCoinsViewCache &view) {
1926 bool fClean = true;
1927
1928 if (blockUndo.vtxundo.size() + 1 != block.vtx.size()) {
1929 error("DisconnectBlock(): block and undo data inconsistent");
1931 }
1932
1933 // First, restore inputs.
1934 for (size_t i = 1; i < block.vtx.size(); i++) {
1935 const CTransaction &tx = *(block.vtx[i]);
1936 CTxUndo &txundo = blockUndo.vtxundo[i - 1];
1937 if (txundo.vprevout.size() != tx.vin.size()) {
1938 error("DisconnectBlock(): transaction and undo data inconsistent");
1940 }
1941
1942 for (size_t j = 0; j < tx.vin.size(); j++) {
1943 const COutPoint &out = tx.vin[j].prevout;
1944 DisconnectResult res =
1945 UndoCoinSpend(std::move(txundo.vprevout[j]), view, out);
1946 if (res == DisconnectResult::FAILED) {
1948 }
1949 fClean = fClean && res != DisconnectResult::UNCLEAN;
1950 }
1951 // At this point, all of txundo.vprevout should have been moved out.
1952 }
1953
1954 // Second, revert created outputs.
1955 for (const auto &ptx : block.vtx) {
1956 const CTransaction &tx = *ptx;
1957 const TxId &txid = tx.GetId();
1958 const bool is_coinbase = tx.IsCoinBase();
1959
1960 // Check that all outputs are available and match the outputs in the
1961 // block itself exactly.
1962 for (size_t o = 0; o < tx.vout.size(); o++) {
1963 if (tx.vout[o].scriptPubKey.IsUnspendable()) {
1964 continue;
1965 }
1966
1967 COutPoint out(txid, o);
1968 Coin coin;
1969 bool is_spent = view.SpendCoin(out, &coin);
1970 if (!is_spent || tx.vout[o] != coin.GetTxOut() ||
1971 uint32_t(pindex->nHeight) != coin.GetHeight() ||
1972 is_coinbase != coin.IsCoinBase()) {
1973 // transaction output mismatch
1974 fClean = false;
1975 }
1976 }
1977 }
1978
1979 // Move best block pointer to previous block.
1980 view.SetBestBlock(block.hashPrevBlock);
1981
1983}
1984
1986
1987void StartScriptCheckWorkerThreads(int threads_num) {
1988 scriptcheckqueue.StartWorkerThreads(threads_num);
1989}
1990
1992 scriptcheckqueue.StopWorkerThreads();
1993}
1994
1995// Returns the script flags which should be checked for the block after
1996// the given block.
1997static uint32_t GetNextBlockScriptFlags(const CBlockIndex *pindex,
1998 const ChainstateManager &chainman) {
1999 const Consensus::Params &consensusparams = chainman.GetConsensus();
2000
2001 uint32_t flags = SCRIPT_VERIFY_NONE;
2002
2003 // Enforce P2SH (BIP16)
2004 if (DeploymentActiveAfter(pindex, chainman, Consensus::DEPLOYMENT_P2SH)) {
2006 }
2007
2008 // Enforce the DERSIG (BIP66) rule.
2009 if (DeploymentActiveAfter(pindex, chainman, Consensus::DEPLOYMENT_DERSIG)) {
2011 }
2012
2013 // Start enforcing CHECKLOCKTIMEVERIFY (BIP65) rule.
2014 if (DeploymentActiveAfter(pindex, chainman, Consensus::DEPLOYMENT_CLTV)) {
2016 }
2017
2018 // Start enforcing CSV (BIP68, BIP112 and BIP113) rule.
2019 if (DeploymentActiveAfter(pindex, chainman, Consensus::DEPLOYMENT_CSV)) {
2021 }
2022
2023 // If the UAHF is enabled, we start accepting replay protected txns
2024 if (IsUAHFenabled(consensusparams, pindex)) {
2027 }
2028
2029 // If the DAA HF is enabled, we start rejecting transaction that use a high
2030 // s in their signature. We also make sure that signature that are supposed
2031 // to fail (for instance in multisig or other forms of smart contracts) are
2032 // null.
2033 if (IsDAAEnabled(consensusparams, pindex)) {
2036 }
2037
2038 // When the magnetic anomaly fork is enabled, we start accepting
2039 // transactions using the OP_CHECKDATASIG opcode and it's verify
2040 // alternative. We also start enforcing push only signatures and
2041 // clean stack.
2042 if (IsMagneticAnomalyEnabled(consensusparams, pindex)) {
2045 }
2046
2047 if (IsGravitonEnabled(consensusparams, pindex)) {
2050 }
2051
2052 if (IsPhononEnabled(consensusparams, pindex)) {
2054 }
2055
2056 // We make sure this node will have replay protection during the next hard
2057 // fork.
2058 if (IsReplayProtectionEnabled(consensusparams, pindex)) {
2060 }
2061
2062 return flags;
2063}
2064
2065static int64_t nTimeCheck = 0;
2066static int64_t nTimeForks = 0;
2067static int64_t nTimeVerify = 0;
2068static int64_t nTimeConnect = 0;
2069static int64_t nTimeIndex = 0;
2070static int64_t nTimeTotal = 0;
2071static int64_t nBlocksTotal = 0;
2072
2079bool Chainstate::ConnectBlock(const CBlock &block, BlockValidationState &state,
2080 CBlockIndex *pindex, CCoinsViewCache &view,
2081 BlockValidationOptions options, Amount *blockFees,
2082 bool fJustCheck) {
2084 assert(pindex);
2085
2086 const BlockHash block_hash{block.GetHash()};
2087 assert(*pindex->phashBlock == block_hash);
2088
2089 int64_t nTimeStart = GetTimeMicros();
2090
2091 const CChainParams &params{m_chainman.GetParams()};
2092 const Consensus::Params &consensusParams = params.GetConsensus();
2093
2094 // Check it again in case a previous version let a bad block in
2095 // NOTE: We don't currently (re-)invoke ContextualCheckBlock() or
2096 // ContextualCheckBlockHeader() here. This means that if we add a new
2097 // consensus rule that is enforced in one of those two functions, then we
2098 // may have let in a block that violates the rule prior to updating the
2099 // software, and we would NOT be enforcing the rule here. Fully solving
2100 // upgrade from one software version to the next after a consensus rule
2101 // change is potentially tricky and issue-specific.
2102 // Also, currently the rule against blocks more than 2 hours in the future
2103 // is enforced in ContextualCheckBlockHeader(); we wouldn't want to
2104 // re-enforce that rule here (at least until we make it impossible for
2105 // m_adjusted_time_callback() to go backward).
2106 if (!CheckBlock(block, state, consensusParams,
2107 options.withCheckPoW(!fJustCheck)
2108 .withCheckMerkleRoot(!fJustCheck))) {
2110 // We don't write down blocks to disk if they may have been
2111 // corrupted, so this should be impossible unless we're having
2112 // hardware problems.
2113 return AbortNode(state, "Corrupt block found indicating potential "
2114 "hardware failure; shutting down");
2115 }
2116 return error("%s: Consensus::CheckBlock: %s", __func__,
2117 state.ToString());
2118 }
2119
2120 // Verify that the view's current state corresponds to the previous block
2121 BlockHash hashPrevBlock =
2122 pindex->pprev == nullptr ? BlockHash() : pindex->pprev->GetBlockHash();
2123 assert(hashPrevBlock == view.GetBestBlock());
2124
2125 nBlocksTotal++;
2126
2127 // Special case for the genesis block, skipping connection of its
2128 // transactions (its coinbase is unspendable)
2129 if (block_hash == consensusParams.hashGenesisBlock) {
2130 if (!fJustCheck) {
2131 view.SetBestBlock(pindex->GetBlockHash());
2132 }
2133
2134 return true;
2135 }
2136
2137 bool fScriptChecks = true;
2139 // We've been configured with the hash of a block which has been
2140 // externally verified to have a valid history. A suitable default value
2141 // is included with the software and updated from time to time. Because
2142 // validity relative to a piece of software is an objective fact these
2143 // defaults can be easily reviewed. This setting doesn't force the
2144 // selection of any particular chain but makes validating some faster by
2145 // effectively caching the result of part of the verification.
2146 BlockMap::const_iterator it{
2147 m_blockman.m_block_index.find(m_chainman.AssumedValidBlock())};
2148 if (it != m_blockman.m_block_index.end()) {
2149 if (it->second.GetAncestor(pindex->nHeight) == pindex &&
2150 m_chainman.m_best_header->GetAncestor(pindex->nHeight) ==
2151 pindex &&
2152 m_chainman.m_best_header->nChainWork >=
2154 // This block is a member of the assumed verified chain and an
2155 // ancestor of the best header.
2156 // Script verification is skipped when connecting blocks under
2157 // the assumevalid block. Assuming the assumevalid block is
2158 // valid this is safe because block merkle hashes are still
2159 // computed and checked, Of course, if an assumed valid block is
2160 // invalid due to false scriptSigs this optimization would allow
2161 // an invalid chain to be accepted.
2162 // The equivalent time check discourages hash power from
2163 // extorting the network via DOS attack into accepting an
2164 // invalid block through telling users they must manually set
2165 // assumevalid. Requiring a software change or burying the
2166 // invalid block, regardless of the setting, makes it hard to
2167 // hide the implication of the demand. This also avoids having
2168 // release candidates that are hardly doing any signature
2169 // verification at all in testing without having to artificially
2170 // set the default assumed verified block further back. The test
2171 // against the minimum chain work prevents the skipping when
2172 // denied access to any chain at least as good as the expected
2173 // chain.
2174 fScriptChecks = (GetBlockProofEquivalentTime(
2175 *m_chainman.m_best_header, *pindex,
2176 *m_chainman.m_best_header,
2177 consensusParams) <= 60 * 60 * 24 * 7 * 2);
2178 }
2179 }
2180 }
2181
2182 int64_t nTime1 = GetTimeMicros();
2183 nTimeCheck += nTime1 - nTimeStart;
2184 LogPrint(BCLog::BENCH, " - Sanity checks: %.2fms [%.2fs (%.2fms/blk)]\n",
2185 MILLI * (nTime1 - nTimeStart), nTimeCheck * MICRO,
2187
2188 // Do not allow blocks that contain transactions which 'overwrite' older
2189 // transactions, unless those are already completely spent. If such
2190 // overwrites are allowed, coinbases and transactions depending upon those
2191 // can be duplicated to remove the ability to spend the first instance --
2192 // even after being sent to another address.
2193 // See BIP30, CVE-2012-1909, and http://r6.ca/blog/20120206T005236Z.html
2194 // for more information. This rule was originally applied to all blocks
2195 // with a timestamp after March 15, 2012, 0:00 UTC. Now that the whole
2196 // chain is irreversibly beyond that time it is applied to all blocks
2197 // except the two in the chain that violate it. This prevents exploiting
2198 // the issue against nodes during their initial block download.
2199 bool fEnforceBIP30 = !((pindex->nHeight == 91842 &&
2200 pindex->GetBlockHash() ==
2201 uint256S("0x00000000000a4d0a398161ffc163c503763"
2202 "b1f4360639393e0e4c8e300e0caec")) ||
2203 (pindex->nHeight == 91880 &&
2204 pindex->GetBlockHash() ==
2205 uint256S("0x00000000000743f190a18c5577a3c2d2a1f"
2206 "610ae9601ac046a38084ccb7cd721")));
2207
2208 // Once BIP34 activated it was not possible to create new duplicate
2209 // coinbases and thus other than starting with the 2 existing duplicate
2210 // coinbase pairs, not possible to create overwriting txs. But by the time
2211 // BIP34 activated, in each of the existing pairs the duplicate coinbase had
2212 // overwritten the first before the first had been spent. Since those
2213 // coinbases are sufficiently buried it's no longer possible to create
2214 // further duplicate transactions descending from the known pairs either. If
2215 // we're on the known chain at height greater than where BIP34 activated, we
2216 // can save the db accesses needed for the BIP30 check.
2217
2218 // BIP34 requires that a block at height X (block X) has its coinbase
2219 // scriptSig start with a CScriptNum of X (indicated height X). The above
2220 // logic of no longer requiring BIP30 once BIP34 activates is flawed in the
2221 // case that there is a block X before the BIP34 height of 227,931 which has
2222 // an indicated height Y where Y is greater than X. The coinbase for block
2223 // X would also be a valid coinbase for block Y, which could be a BIP30
2224 // violation. An exhaustive search of all mainnet coinbases before the
2225 // BIP34 height which have an indicated height greater than the block height
2226 // reveals many occurrences. The 3 lowest indicated heights found are
2227 // 209,921, 490,897, and 1,983,702 and thus coinbases for blocks at these 3
2228 // heights would be the first opportunity for BIP30 to be violated.
2229
2230 // The search reveals a great many blocks which have an indicated height
2231 // greater than 1,983,702, so we simply remove the optimization to skip
2232 // BIP30 checking for blocks at height 1,983,702 or higher. Before we reach
2233 // that block in another 25 years or so, we should take advantage of a
2234 // future consensus change to do a new and improved version of BIP34 that
2235 // will actually prevent ever creating any duplicate coinbases in the
2236 // future.
2237 static constexpr int BIP34_IMPLIES_BIP30_LIMIT = 1983702;
2238
2239 // There is no potential to create a duplicate coinbase at block 209,921
2240 // because this is still before the BIP34 height and so explicit BIP30
2241 // checking is still active.
2242
2243 // The final case is block 176,684 which has an indicated height of
2244 // 490,897. Unfortunately, this issue was not discovered until about 2 weeks
2245 // before block 490,897 so there was not much opportunity to address this
2246 // case other than to carefully analyze it and determine it would not be a
2247 // problem. Block 490,897 was, in fact, mined with a different coinbase than
2248 // block 176,684, but it is important to note that even if it hadn't been or
2249 // is remined on an alternate fork with a duplicate coinbase, we would still
2250 // not run into a BIP30 violation. This is because the coinbase for 176,684
2251 // is spent in block 185,956 in transaction
2252 // d4f7fbbf92f4a3014a230b2dc70b8058d02eb36ac06b4a0736d9d60eaa9e8781. This
2253 // spending transaction can't be duplicated because it also spends coinbase
2254 // 0328dd85c331237f18e781d692c92de57649529bd5edf1d01036daea32ffde29. This
2255 // coinbase has an indicated height of over 4.2 billion, and wouldn't be
2256 // duplicatable until that height, and it's currently impossible to create a
2257 // chain that long. Nevertheless we may wish to consider a future soft fork
2258 // which retroactively prevents block 490,897 from creating a duplicate
2259 // coinbase. The two historical BIP30 violations often provide a confusing
2260 // edge case when manipulating the UTXO and it would be simpler not to have
2261 // another edge case to deal with.
2262
2263 // testnet3 has no blocks before the BIP34 height with indicated heights
2264 // post BIP34 before approximately height 486,000,000 and presumably will
2265 // be reset before it reaches block 1,983,702 and starts doing unnecessary
2266 // BIP30 checking again.
2267 assert(pindex->pprev);
2268 CBlockIndex *pindexBIP34height =
2269 pindex->pprev->GetAncestor(consensusParams.BIP34Height);
2270 // Only continue to enforce if we're below BIP34 activation height or the
2271 // block hash at that height doesn't correspond.
2272 fEnforceBIP30 =
2273 fEnforceBIP30 &&
2274 (!pindexBIP34height ||
2275 !(pindexBIP34height->GetBlockHash() == consensusParams.BIP34Hash));
2276
2277 // TODO: Remove BIP30 checking from block height 1,983,702 on, once we have
2278 // a consensus change that ensures coinbases at those heights can not
2279 // duplicate earlier coinbases.
2280 if (fEnforceBIP30 || pindex->nHeight >= BIP34_IMPLIES_BIP30_LIMIT) {
2281 for (const auto &tx : block.vtx) {
2282 for (size_t o = 0; o < tx->vout.size(); o++) {
2283 if (view.HaveCoin(COutPoint(tx->GetId(), o))) {
2284 LogPrintf("ERROR: ConnectBlock(): tried to overwrite "
2285 "transaction\n");
2287 "bad-txns-BIP30");
2288 }
2289 }
2290 }
2291 }
2292
2293 // Enforce BIP68 (sequence locks).
2294 int nLockTimeFlags = 0;
2295 if (DeploymentActiveAt(*pindex, consensusParams,
2297 nLockTimeFlags |= LOCKTIME_VERIFY_SEQUENCE;
2298 }
2299
2300 const uint32_t flags = GetNextBlockScriptFlags(pindex->pprev, m_chainman);
2301
2302 int64_t nTime2 = GetTimeMicros();
2303 nTimeForks += nTime2 - nTime1;
2304 LogPrint(BCLog::BENCH, " - Fork checks: %.2fms [%.2fs (%.2fms/blk)]\n",
2305 MILLI * (nTime2 - nTime1), nTimeForks * MICRO,
2307
2308 std::vector<int> prevheights;
2309 Amount nFees = Amount::zero();
2310 int nInputs = 0;
2311
2312 // Limit the total executed signature operations in the block, a consensus
2313 // rule. Tracking during the CPU-consuming part (validation of uncached
2314 // inputs) is per-input atomic and validation in each thread stops very
2315 // quickly after the limit is exceeded, so an adversary cannot cause us to
2316 // exceed the limit by much at all.
2317 CheckInputsLimiter nSigChecksBlockLimiter(
2319
2320 std::vector<TxSigCheckLimiter> nSigChecksTxLimiters;
2321 nSigChecksTxLimiters.resize(block.vtx.size() - 1);
2322
2323 CBlockUndo blockundo;
2324 blockundo.vtxundo.resize(block.vtx.size() - 1);
2325
2326 CCheckQueueControl<CScriptCheck> control(fScriptChecks ? &scriptcheckqueue
2327 : nullptr);
2328
2329 // Add all outputs
2330 try {
2331 for (const auto &ptx : block.vtx) {
2332 AddCoins(view, *ptx, pindex->nHeight);
2333 }
2334 } catch (const std::logic_error &e) {
2335 // This error will be thrown from AddCoin if we try to connect a block
2336 // containing duplicate transactions. Such a thing should normally be
2337 // caught early nowadays (due to ContextualCheckBlock's CTOR
2338 // enforcement) however some edge cases can escape that:
2339 // - ContextualCheckBlock does not get re-run after saving the block to
2340 // disk, and older versions may have saved a weird block.
2341 // - its checks are not applied to pre-CTOR chains, which we might visit
2342 // with checkpointing off.
2343 LogPrintf("ERROR: ConnectBlock(): tried to overwrite transaction\n");
2345 "tx-duplicate");
2346 }
2347
2348 size_t txIndex = 0;
2349 // nSigChecksRet may be accurate (found in cache) or 0 (checks were
2350 // deferred into vChecks).
2351 int nSigChecksRet;
2352 for (const auto &ptx : block.vtx) {
2353 const CTransaction &tx = *ptx;
2354 const bool isCoinBase = tx.IsCoinBase();
2355 nInputs += tx.vin.size();
2356
2357 {
2358 Amount txfee = Amount::zero();
2359 TxValidationState tx_state;
2360 if (!isCoinBase &&
2361 !Consensus::CheckTxInputs(tx, tx_state, view, pindex->nHeight,
2362 txfee)) {
2363 // Any transaction validation failure in ConnectBlock is a block
2364 // consensus failure.
2366 tx_state.GetRejectReason(),
2367 tx_state.GetDebugMessage());
2368
2369 return error("%s: Consensus::CheckTxInputs: %s, %s", __func__,
2370 tx.GetId().ToString(), state.ToString());
2371 }
2372 nFees += txfee;
2373 }
2374
2375 if (!MoneyRange(nFees)) {
2376 LogPrintf("ERROR: %s: accumulated fee in the block out of range.\n",
2377 __func__);
2379 "bad-txns-accumulated-fee-outofrange");
2380 }
2381
2382 // The following checks do not apply to the coinbase.
2383 if (isCoinBase) {
2384 continue;
2385 }
2386
2387 // Check that transaction is BIP68 final BIP68 lock checks (as
2388 // opposed to nLockTime checks) must be in ConnectBlock because they
2389 // require the UTXO set.
2390 prevheights.resize(tx.vin.size());
2391 for (size_t j = 0; j < tx.vin.size(); j++) {
2392 prevheights[j] = view.AccessCoin(tx.vin[j].prevout).GetHeight();
2393 }
2394
2395 if (!SequenceLocks(tx, nLockTimeFlags, prevheights, *pindex)) {
2396 LogPrintf("ERROR: %s: contains a non-BIP68-final transaction\n",
2397 __func__);
2399 "bad-txns-nonfinal");
2400 }
2401
2402 // Don't cache results if we're actually connecting blocks (still
2403 // consult the cache, though).
2404 bool fCacheResults = fJustCheck;
2405
2406 const bool fEnforceSigCheck = flags & SCRIPT_ENFORCE_SIGCHECKS;
2407 if (!fEnforceSigCheck) {
2408 // Historically, there has been transactions with a very high
2409 // sigcheck count, so we need to disable this check for such
2410 // transactions.
2411 nSigChecksTxLimiters[txIndex] = TxSigCheckLimiter::getDisabled();
2412 }
2413
2414 std::vector<CScriptCheck> vChecks;
2415 TxValidationState tx_state;
2416 if (fScriptChecks &&
2417 !CheckInputScripts(tx, tx_state, view, flags, fCacheResults,
2418 fCacheResults, PrecomputedTransactionData(tx),
2419 nSigChecksRet, nSigChecksTxLimiters[txIndex],
2420 &nSigChecksBlockLimiter, &vChecks)) {
2421 // Any transaction validation failure in ConnectBlock is a block
2422 // consensus failure
2424 tx_state.GetRejectReason(),
2425 tx_state.GetDebugMessage());
2426 return error(
2427 "ConnectBlock(): CheckInputScripts on %s failed with %s",
2428 tx.GetId().ToString(), state.ToString());
2429 }
2430
2431 control.Add(std::move(vChecks));
2432
2433 // Note: this must execute in the same iteration as CheckTxInputs (not
2434 // in a separate loop) in order to detect double spends. However,
2435 // this does not prevent double-spending by duplicated transaction
2436 // inputs in the same transaction (cf. CVE-2018-17144) -- that check is
2437 // done in CheckBlock (CheckRegularTransaction).
2438 SpendCoins(view, tx, blockundo.vtxundo.at(txIndex), pindex->nHeight);
2439 txIndex++;
2440 }
2441
2442 int64_t nTime3 = GetTimeMicros();
2443 nTimeConnect += nTime3 - nTime2;
2445 " - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) "
2446 "[%.2fs (%.2fms/blk)]\n",
2447 (unsigned)block.vtx.size(), MILLI * (nTime3 - nTime2),
2448 MILLI * (nTime3 - nTime2) / block.vtx.size(),
2449 nInputs <= 1 ? 0 : MILLI * (nTime3 - nTime2) / (nInputs - 1),
2451
2452 const Amount blockReward =
2453 nFees + GetBlockSubsidy(pindex->nHeight, consensusParams);
2454 if (block.vtx[0]->GetValueOut() > blockReward) {
2455 LogPrintf("ERROR: ConnectBlock(): coinbase pays too much (actual=%d vs "
2456 "limit=%d)\n",
2457 block.vtx[0]->GetValueOut(), blockReward);
2459 "bad-cb-amount");
2460 }
2461
2462 if (blockFees) {
2463 *blockFees = nFees;
2464 }
2465
2466 if (!control.Wait()) {
2468 "blk-bad-inputs", "parallel script check failed");
2469 }
2470
2471 int64_t nTime4 = GetTimeMicros();
2472 nTimeVerify += nTime4 - nTime2;
2473 LogPrint(
2475 " - Verify %u txins: %.2fms (%.3fms/txin) [%.2fs (%.2fms/blk)]\n",
2476 nInputs - 1, MILLI * (nTime4 - nTime2),
2477 nInputs <= 1 ? 0 : MILLI * (nTime4 - nTime2) / (nInputs - 1),
2479
2480 if (fJustCheck) {
2481 return true;
2482 }
2483
2484 if (!m_blockman.WriteUndoDataForBlock(blockundo, state, *pindex)) {
2485 return false;
2486 }
2487
2488 if (!pindex->IsValid(BlockValidity::SCRIPTS)) {
2490 m_blockman.m_dirty_blockindex.insert(pindex);
2491 }
2492
2493 // add this block to the view's block chain
2494 view.SetBestBlock(pindex->GetBlockHash());
2495
2496 int64_t nTime5 = GetTimeMicros();
2497 nTimeIndex += nTime5 - nTime4;
2498 LogPrint(BCLog::BENCH, " - Index writing: %.2fms [%.2fs (%.2fms/blk)]\n",
2499 MILLI * (nTime5 - nTime4), nTimeIndex * MICRO,
2501
2502 TRACE6(validation, block_connected, block_hash.data(), pindex->nHeight,
2503 block.vtx.size(), nInputs, nSigChecksRet,
2504 // in microseconds (µs)
2505 nTime5 - nTimeStart);
2506
2507 return true;
2508}
2509
2510CoinsCacheSizeState Chainstate::GetCoinsCacheSizeState() {
2512 return this->GetCoinsCacheSizeState(m_coinstip_cache_size_bytes,
2514 : 0);
2515}
2516
2518Chainstate::GetCoinsCacheSizeState(size_t max_coins_cache_size_bytes,
2519 size_t max_mempool_size_bytes) {
2521 int64_t nMempoolUsage = m_mempool ? m_mempool->DynamicMemoryUsage() : 0;
2522 int64_t cacheSize = CoinsTip().DynamicMemoryUsage();
2523 int64_t nTotalSpace =
2524 max_coins_cache_size_bytes +
2525 std::max<int64_t>(int64_t(max_mempool_size_bytes) - nMempoolUsage, 0);
2526
2528 static constexpr int64_t MAX_BLOCK_COINSDB_USAGE_BYTES =
2529 10 * 1024 * 1024; // 10MB
2530 int64_t large_threshold = std::max(
2531 (9 * nTotalSpace) / 10, nTotalSpace - MAX_BLOCK_COINSDB_USAGE_BYTES);
2532
2533 if (cacheSize > nTotalSpace) {
2534 LogPrintf("Cache size (%s) exceeds total space (%s)\n", cacheSize,
2535 nTotalSpace);
2537 } else if (cacheSize > large_threshold) {
2539 }
2541}
2542
2544 FlushStateMode mode, int nManualPruneHeight) {
2545 LOCK(cs_main);
2546 assert(this->CanFlushToDisk());
2547 std::set<int> setFilesToPrune;
2548 bool full_flush_completed = false;
2549
2550 const size_t coins_count = CoinsTip().GetCacheSize();
2551 const size_t coins_mem_usage = CoinsTip().DynamicMemoryUsage();
2552
2553 try {
2554 {
2555 bool fFlushForPrune = false;
2556 bool fDoFullFlush = false;
2557
2558 CoinsCacheSizeState cache_state = GetCoinsCacheSizeState();
2560 if (m_blockman.IsPruneMode() &&
2561 (m_blockman.m_check_for_pruning || nManualPruneHeight > 0) &&
2562 !fReindex) {
2563 // Make sure we don't prune any of the prune locks bestblocks.
2564 // Pruning is height-based.
2565 int last_prune{m_chain.Height()};
2566 // prune lock that actually was the limiting factor, only used
2567 // for logging
2568 std::optional<std::string> limiting_lock;
2569
2570 for (const auto &prune_lock : m_blockman.m_prune_locks) {
2571 if (prune_lock.second.height_first ==
2572 std::numeric_limits<int>::max()) {
2573 continue;
2574 }
2575 // Remove the buffer and one additional block here to get
2576 // actual height that is outside of the buffer
2577 const int lock_height{prune_lock.second.height_first -
2578 PRUNE_LOCK_BUFFER - 1};
2579 last_prune = std::max(1, std::min(last_prune, lock_height));
2580 if (last_prune == lock_height) {
2581 limiting_lock = prune_lock.first;
2582 }
2583 }
2584
2585 if (limiting_lock) {
2586 LogPrint(BCLog::PRUNE, "%s limited pruning to height %d\n",
2587 limiting_lock.value(), last_prune);
2588 }
2589
2590 if (nManualPruneHeight > 0) {
2592 "find files to prune (manual)", BCLog::BENCH);
2594 setFilesToPrune,
2595 std::min(last_prune, nManualPruneHeight), *this,
2596 m_chainman);
2597 } else {
2598 LOG_TIME_MILLIS_WITH_CATEGORY("find files to prune",
2599 BCLog::BENCH);
2600 m_blockman.FindFilesToPrune(setFilesToPrune, last_prune,
2601 *this, m_chainman);
2603 }
2604 if (!setFilesToPrune.empty()) {
2605 fFlushForPrune = true;
2607 m_blockman.m_block_tree_db->WriteFlag(
2608 "prunedblockfiles", true);
2610 }
2611 }
2612 }
2613 const auto nNow = GetTime<std::chrono::microseconds>();
2614 // Avoid writing/flushing immediately after startup.
2615 if (m_last_write.count() == 0) {
2616 m_last_write = nNow;
2617 }
2618 if (m_last_flush.count() == 0) {
2619 m_last_flush = nNow;
2620 }
2621 // The cache is large and we're within 10% and 10 MiB of the limit,
2622 // but we have time now (not in the middle of a block processing).
2623 bool fCacheLarge = mode == FlushStateMode::PERIODIC &&
2624 cache_state >= CoinsCacheSizeState::LARGE;
2625 // The cache is over the limit, we have to write now.
2626 bool fCacheCritical = mode == FlushStateMode::IF_NEEDED &&
2627 cache_state >= CoinsCacheSizeState::CRITICAL;
2628 // It's been a while since we wrote the block index to disk. Do this
2629 // frequently, so we don't need to redownload after a crash.
2630 bool fPeriodicWrite = mode == FlushStateMode::PERIODIC &&
2632 // It's been very long since we flushed the cache. Do this
2633 // infrequently, to optimize cache usage.
2634 bool fPeriodicFlush = mode == FlushStateMode::PERIODIC &&
2636 // Combine all conditions that result in a full cache flush.
2637 fDoFullFlush = (mode == FlushStateMode::ALWAYS) || fCacheLarge ||
2638 fCacheCritical || fPeriodicFlush || fFlushForPrune;
2639 // Write blocks and block index to disk.
2640 if (fDoFullFlush || fPeriodicWrite) {
2641 // Ensure we can write block index
2643 return AbortNode(state, "Disk space is too low!",
2644 _("Disk space is too low!"));
2645 }
2646
2647 {
2649 "write block and undo data to disk", BCLog::BENCH);
2650
2651 // First make sure all block and undo data is flushed to
2652 // disk.
2653 // TODO: Handle return error, or add detailed comment why
2654 // it is safe to not return an error upon failure.
2656 m_chain.Height())) {
2658 "%s: Failed to flush block file.\n",
2659 __func__);
2660 }
2661 }
2662 // Then update all block file information (which may refer to
2663 // block and undo files).
2664 {
2665 LOG_TIME_MILLIS_WITH_CATEGORY("write block index to disk",
2666 BCLog::BENCH);
2667
2668 if (!m_blockman.WriteBlockIndexDB()) {
2669 return AbortNode(
2670 state, "Failed to write to block index database");
2671 }
2672 }
2673
2674 // Finally remove any pruned files
2675 if (fFlushForPrune) {
2676 LOG_TIME_MILLIS_WITH_CATEGORY("unlink pruned files",
2677 BCLog::BENCH);
2678
2679 m_blockman.UnlinkPrunedFiles(setFilesToPrune);
2680 }
2681 m_last_write = nNow;
2682 }
2683 // Flush best chain related state. This can only be done if the
2684 // blocks / block index write was also done.
2685 if (fDoFullFlush && !CoinsTip().GetBestBlock().IsNull()) {
2687 strprintf("write coins cache to disk (%d coins, %.2fkB)",
2688 coins_count, coins_mem_usage / 1000),
2689 BCLog::BENCH);
2690
2691 // Typical Coin structures on disk are around 48 bytes in size.
2692 // Pushing a new one to the database can cause it to be written
2693 // twice (once in the log, and once in the tables). This is
2694 // already an overestimation, as most will delete an existing
2695 // entry or overwrite one. Still, use a conservative safety
2696 // factor of 2.
2698 48 * 2 * 2 * CoinsTip().GetCacheSize())) {
2699 return AbortNode(state, "Disk space is too low!",
2700 _("Disk space is too low!"));
2701 }
2702
2703 // Flush the chainstate (which may refer to block index
2704 // entries).
2705 if (!CoinsTip().Flush()) {
2706 return AbortNode(state, "Failed to write to coin database");
2707 }
2708 m_last_flush = nNow;
2709 full_flush_completed = true;
2710 }
2711
2712 TRACE5(utxocache, flush,
2713 // in microseconds (µs)
2714 GetTimeMicros() - nNow.count(), uint32_t(mode), coins_count,
2715 uint64_t(coins_mem_usage), fFlushForPrune);
2716 }
2717
2718 if (full_flush_completed) {
2719 // Update best block in wallet (so we can detect restored wallets).
2720 GetMainSignals().ChainStateFlushed(this->GetRole(),
2722 }
2723 } catch (const std::runtime_error &e) {
2724 return AbortNode(state, std::string("System error while flushing: ") +
2725 e.what());
2726 }
2727 return true;
2728}
2729
2732 if (!this->FlushStateToDisk(state, FlushStateMode::ALWAYS)) {
2733 LogPrintf("%s: failed to flush state (%s)\n", __func__,
2734 state.ToString());
2735 }
2736}
2737
2741 if (!this->FlushStateToDisk(state, FlushStateMode::NONE)) {
2742 LogPrintf("%s: failed to flush state (%s)\n", __func__,
2743 state.ToString());
2744 }
2745}
2746
2747static void UpdateTipLog(const CCoinsViewCache &coins_tip,
2748 const CBlockIndex *tip, const CChainParams &params,
2749 const std::string &func_name,
2750 const std::string &prefix)
2753 LogPrintf("%s%s: new best=%s height=%d version=0x%08x log2_work=%f tx=%ld "
2754 "date='%s' progress=%f cache=%.1fMiB(%utxo)\n",
2755 prefix, func_name, tip->GetBlockHash().ToString(), tip->nHeight,
2756 tip->nVersion, log(tip->nChainWork.getdouble()) / log(2.0),
2757 tip->GetChainTxCount(),
2759 GuessVerificationProgress(params.TxData(), tip),
2760 coins_tip.DynamicMemoryUsage() * (1.0 / (1 << 20)),
2761 coins_tip.GetCacheSize());
2762}
2763
2764void Chainstate::UpdateTip(const CBlockIndex *pindexNew) {
2766 const auto &coins_tip = CoinsTip();
2767
2768 const CChainParams &params{m_chainman.GetParams()};
2769
2770 // The remainder of the function isn't relevant if we are not acting on
2771 // the active chainstate, so return if need be.
2772 if (this != &m_chainman.ActiveChainstate()) {
2773 // Only log every so often so that we don't bury log messages at the
2774 // tip.
2775 constexpr int BACKGROUND_LOG_INTERVAL = 2000;
2776 if (pindexNew->nHeight % BACKGROUND_LOG_INTERVAL == 0) {
2777 UpdateTipLog(coins_tip, pindexNew, params, __func__,
2778 "[background validation] ");
2779 }
2780 return;
2781 }
2782
2783 // New best block
2784 if (m_mempool) {
2786 }
2787
2788 {
2790 g_best_block = pindexNew;
2791 g_best_block_cv.notify_all();
2792 }
2793
2794 UpdateTipLog(coins_tip, pindexNew, params, __func__, "");
2795}
2796
2809 DisconnectedBlockTransactions *disconnectpool) {
2811 if (m_mempool) {
2813 }
2814
2815 CBlockIndex *pindexDelete = m_chain.Tip();
2816
2817 assert(pindexDelete);
2818 assert(pindexDelete->pprev);
2819
2820 // Read block from disk.
2821 std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
2822 CBlock &block = *pblock;
2823 if (!m_blockman.ReadBlockFromDisk(block, *pindexDelete)) {
2824 return error("DisconnectTip(): Failed to read block");
2825 }
2826
2827 // Apply the block atomically to the chain state.
2828 int64_t nStart = GetTimeMicros();
2829 {
2830 CCoinsViewCache view(&CoinsTip());
2831 assert(view.GetBestBlock() == pindexDelete->GetBlockHash());
2832 if (DisconnectBlock(block, pindexDelete, view) !=
2834 return error("DisconnectTip(): DisconnectBlock %s failed",
2835 pindexDelete->GetBlockHash().ToString());
2836 }
2837
2838 bool flushed = view.Flush();
2839 assert(flushed);
2840 }
2841
2842 LogPrint(BCLog::BENCH, "- Disconnect block: %.2fms\n",
2843 (GetTimeMicros() - nStart) * MILLI);
2844
2845 {
2846 // Prune locks that began at or after the tip should be moved backward
2847 // so they get a chance to reorg
2848 const int max_height_first{pindexDelete->nHeight - 1};
2849 for (auto &prune_lock : m_blockman.m_prune_locks) {
2850 if (prune_lock.second.height_first <= max_height_first) {
2851 continue;
2852 }
2853
2854 prune_lock.second.height_first = max_height_first;
2855 LogPrint(BCLog::PRUNE, "%s prune lock moved back to %d\n",
2856 prune_lock.first, max_height_first);
2857 }
2858 }
2859
2860 // Write the chain state to disk, if necessary.
2862 return false;
2863 }
2864
2865 if (m_mempool) {
2866 // If this block is deactivating a fork, we move all mempool
2867 // transactions in front of disconnectpool for reprocessing in a future
2868 // updateMempoolForReorg call
2869 if (pindexDelete->pprev != nullptr &&
2870 GetNextBlockScriptFlags(pindexDelete, m_chainman) !=
2871 GetNextBlockScriptFlags(pindexDelete->pprev, m_chainman)) {
2873 "Disconnecting mempool due to rewind of upgrade block\n");
2874 if (disconnectpool) {
2875 disconnectpool->importMempool(*m_mempool);
2876 }
2877 m_mempool->clear();
2878 }
2879
2880 if (disconnectpool) {
2881 disconnectpool->addForBlock(block.vtx, *m_mempool);
2882 }
2883 }
2884
2885 m_chain.SetTip(*pindexDelete->pprev);
2886
2887 UpdateTip(pindexDelete->pprev);
2888 // Let wallets know transactions went from 1-confirmed to
2889 // 0-confirmed or conflicted:
2890 GetMainSignals().BlockDisconnected(pblock, pindexDelete);
2891 return true;
2892}
2893
2894static int64_t nTimeReadFromDisk = 0;
2895static int64_t nTimeConnectTotal = 0;
2896static int64_t nTimeFlush = 0;
2897static int64_t nTimeChainState = 0;
2898static int64_t nTimePostConnect = 0;
2899
2905 BlockPolicyValidationState &blockPolicyState,
2906 CBlockIndex *pindexNew,
2907 const std::shared_ptr<const CBlock> &pblock,
2908 DisconnectedBlockTransactions &disconnectpool,
2909 const avalanche::Processor *const avalanche,
2910 const ChainstateRole chainstate_role) {
2912 if (m_mempool) {
2914 }
2915
2916 const Consensus::Params &consensusParams = m_chainman.GetConsensus();
2917
2918 assert(pindexNew->pprev == m_chain.Tip());
2919 // Read block from disk.
2920 int64_t nTime1 = GetTimeMicros();
2921 std::shared_ptr<const CBlock> pthisBlock;
2922 if (!pblock) {
2923 std::shared_ptr<CBlock> pblockNew = std::make_shared<CBlock>();
2924 if (!m_blockman.ReadBlockFromDisk(*pblockNew, *pindexNew)) {
2925 return AbortNode(state, "Failed to read block");
2926 }
2927 pthisBlock = pblockNew;
2928 } else {
2929 pthisBlock = pblock;
2930 }
2931
2932 const CBlock &blockConnecting = *pthisBlock;
2933
2934 // Apply the block atomically to the chain state.
2935 int64_t nTime2 = GetTimeMicros();
2936 nTimeReadFromDisk += nTime2 - nTime1;
2937 int64_t nTime3;
2938 LogPrint(BCLog::BENCH, " - Load block from disk: %.2fms [%.2fs]\n",
2939 (nTime2 - nTime1) * MILLI, nTimeReadFromDisk * MICRO);
2940 {
2941 Amount blockFees{Amount::zero()};
2942 CCoinsViewCache view(&CoinsTip());
2943 bool rv = ConnectBlock(blockConnecting, state, pindexNew, view,
2945 &blockFees);
2946 GetMainSignals().BlockChecked(blockConnecting, state);
2947 if (!rv) {
2948 if (state.IsInvalid()) {
2949 InvalidBlockFound(pindexNew, state);
2950 }
2951
2952 return error("%s: ConnectBlock %s failed, %s", __func__,
2953 pindexNew->GetBlockHash().ToString(),
2954 state.ToString());
2955 }
2956
2968 const BlockHash blockhash = pindexNew->GetBlockHash();
2972
2973 const Amount blockReward =
2974 blockFees +
2975 GetBlockSubsidy(pindexNew->nHeight, consensusParams);
2976
2977 std::vector<std::unique_ptr<ParkingPolicy>> parkingPolicies;
2978 parkingPolicies.emplace_back(std::make_unique<MinerFundPolicy>(
2979 consensusParams, *pindexNew, blockConnecting, blockReward));
2980
2981 if (avalanche) {
2982 // Only enable the RTT policy if the node already finalized a
2983 // block. This is because it's very possible that new blocks
2984 // will be parked after a node restart (but after IBD) if the
2985 // node is behind by a few blocks. We want to make sure that the
2986 // node will be able to switch back to the right tip in this
2987 // case.
2988 if (avalanche->hasFinalizedTip()) {
2989 // Special case for testnet, don't reject blocks mined with
2990 // the min difficulty
2991 if (!consensusParams.fPowAllowMinDifficultyBlocks ||
2992 (blockConnecting.GetBlockTime() <=
2993 pindexNew->pprev->GetBlockTime() +
2994 2 * consensusParams.nPowTargetSpacing)) {
2995 parkingPolicies.emplace_back(
2996 std::make_unique<RTTPolicy>(consensusParams,
2997 *pindexNew));
2998 }
2999 }
3000
3001 parkingPolicies.emplace_back(
3002 std::make_unique<StakingRewardsPolicy>(
3003 *avalanche, consensusParams, *pindexNew,
3004 blockConnecting, blockReward));
3005
3006 if (m_mempool) {
3007 parkingPolicies.emplace_back(
3008 std::make_unique<PreConsensusPolicy>(
3009 *pindexNew, blockConnecting, m_mempool));
3010 }
3011 }
3012
3013 // If any block policy is violated, bail on the first one found
3014 if (std::find_if_not(parkingPolicies.begin(), parkingPolicies.end(),
3015 [&](const auto &policy) {
3016 bool ret = (*policy)(blockPolicyState);
3017 if (!ret) {
3018 LogPrintf(
3019 "Park block because it "
3020 "violated a block policy: %s\n",
3021 blockPolicyState.ToString());
3022 }
3023 return ret;
3024 }) != parkingPolicies.end()) {
3025 pindexNew->nStatus = pindexNew->nStatus.withParked();
3026 m_blockman.m_dirty_blockindex.insert(pindexNew);
3027 return false;
3028 }
3029 }
3030
3031 nTime3 = GetTimeMicros();
3032 nTimeConnectTotal += nTime3 - nTime2;
3033 assert(nBlocksTotal > 0);
3035 " - Connect total: %.2fms [%.2fs (%.2fms/blk)]\n",
3036 (nTime3 - nTime2) * MILLI, nTimeConnectTotal * MICRO,
3038 bool flushed = view.Flush();
3039 assert(flushed);
3040 }
3041
3042 int64_t nTime4 = GetTimeMicros();
3043 nTimeFlush += nTime4 - nTime3;
3044 LogPrint(BCLog::BENCH, " - Flush: %.2fms [%.2fs (%.2fms/blk)]\n",
3045 (nTime4 - nTime3) * MILLI, nTimeFlush * MICRO,
3047
3048 // Write the chain state to disk, if necessary.
3049 if (!FlushStateToDisk(state, FlushStateMode::IF_NEEDED)) {
3050 return false;
3051 }
3052
3053 int64_t nTime5 = GetTimeMicros();
3054 nTimeChainState += nTime5 - nTime4;
3056 " - Writing chainstate: %.2fms [%.2fs (%.2fms/blk)]\n",
3057 (nTime5 - nTime4) * MILLI, nTimeChainState * MICRO,
3059
3060 // Remove conflicting transactions from the mempool;
3061 if (m_mempool) {
3062 disconnectpool.removeForBlock(blockConnecting.vtx, *m_mempool);
3063
3064 // If this block is activating a fork, we move all mempool transactions
3065 // in front of disconnectpool for reprocessing in a future
3066 // updateMempoolForReorg call
3067 if (pindexNew->pprev != nullptr &&
3068 GetNextBlockScriptFlags(pindexNew, m_chainman) !=
3069 GetNextBlockScriptFlags(pindexNew->pprev, m_chainman)) {
3070 LogPrint(
3072 "Disconnecting mempool due to acceptance of upgrade block\n");
3073 disconnectpool.importMempool(*m_mempool);
3074 }
3075 }
3076
3077 // Update m_chain & related variables.
3078 m_chain.SetTip(*pindexNew);
3079 UpdateTip(pindexNew);
3080
3081 int64_t nTime6 = GetTimeMicros();
3082 nTimePostConnect += nTime6 - nTime5;
3083 nTimeTotal += nTime6 - nTime1;
3085 " - Connect postprocess: %.2fms [%.2fs (%.2fms/blk)]\n",
3086 (nTime6 - nTime5) * MILLI, nTimePostConnect * MICRO,
3088 LogPrint(BCLog::BENCH, "- Connect block: %.2fms [%.2fs (%.2fms/blk)]\n",
3089 (nTime6 - nTime1) * MILLI, nTimeTotal * MICRO,
3091
3092 // If we are the background validation chainstate, check to see if we are
3093 // done validating the snapshot (i.e. our tip has reached the snapshot's
3094 // base block).
3095 if (this != &m_chainman.ActiveChainstate()) {
3096 // This call may set `m_disabled`, which is referenced immediately
3097 // afterwards in ActivateBestChain, so that we stop connecting blocks
3098 // past the snapshot base.
3099 m_chainman.MaybeCompleteSnapshotValidation();
3100 }
3101
3102 GetMainSignals().BlockConnected(chainstate_role, pthisBlock, pindexNew);
3103 return true;
3104}
3105
3111 std::vector<const CBlockIndex *> &blocksToReconcile, bool fAutoUnpark) {
3113 do {
3114 CBlockIndex *pindexNew = nullptr;
3115
3116 // Find the best candidate header.
3117 {
3118 std::set<CBlockIndex *, CBlockIndexWorkComparator>::reverse_iterator
3119 it = setBlockIndexCandidates.rbegin();
3120 if (it == setBlockIndexCandidates.rend()) {
3121 return nullptr;
3122 }
3123 pindexNew = *it;
3124 }
3125
3126 // If this block will cause an avalanche finalized block to be reorged,
3127 // then we park it.
3128 {
3130 if (m_avalancheFinalizedBlockIndex &&
3131 !AreOnTheSameFork(pindexNew, m_avalancheFinalizedBlockIndex)) {
3132 LogPrintf("Park block %s because it forks prior to the "
3133 "avalanche finalized chaintip.\n",
3134 pindexNew->GetBlockHash().ToString());
3135 pindexNew->nStatus = pindexNew->nStatus.withParked();
3136 m_blockman.m_dirty_blockindex.insert(pindexNew);
3137 }
3138 }
3139
3140 const CBlockIndex *pindexFork = m_chain.FindFork(pindexNew);
3141
3142 // Check whether all blocks on the path between the currently active
3143 // chain and the candidate are valid. Just going until the active chain
3144 // is an optimization, as we know all blocks in it are valid already.
3145 CBlockIndex *pindexTest = pindexNew;
3146 bool hasValidAncestor = true;
3147 while (hasValidAncestor && pindexTest && pindexTest != pindexFork) {
3148 assert(pindexTest->HaveNumChainTxs() || pindexTest->nHeight == 0);
3149
3150 // If this is a parked chain, but it has enough PoW, clear the park
3151 // state.
3152 bool fParkedChain = pindexTest->nStatus.isOnParkedChain();
3153 if (fAutoUnpark && fParkedChain) {
3154 const CBlockIndex *pindexTip = m_chain.Tip();
3155
3156 // During initialization, pindexTip and/or pindexFork may be
3157 // null. In this case, we just ignore the fact that the chain is
3158 // parked.
3159 if (!pindexTip || !pindexFork) {
3160 UnparkBlock(pindexTest);
3161 continue;
3162 }
3163
3164 // A parked chain can be unparked if it has twice as much PoW
3165 // accumulated as the main chain has since the fork block.
3166 CBlockIndex const *pindexExtraPow = pindexTip;
3167 arith_uint256 requiredWork = pindexTip->nChainWork;
3168 switch (pindexTip->nHeight - pindexFork->nHeight) {
3169 // Limit the penality for depth 1, 2 and 3 to half a block
3170 // worth of work to ensure we don't fork accidentally.
3171 case 3:
3172 case 2:
3173 pindexExtraPow = pindexExtraPow->pprev;
3174 // FALLTHROUGH
3175 case 1: {
3176 const arith_uint256 deltaWork =
3177 pindexExtraPow->nChainWork - pindexFork->nChainWork;
3178 requiredWork += (deltaWork >> 1);
3179 break;
3180 }
3181 default:
3182 requiredWork +=
3183 pindexExtraPow->nChainWork - pindexFork->nChainWork;
3184 break;
3185 }
3186
3187 if (pindexNew->nChainWork > requiredWork) {
3188 // We have enough, clear the parked state.
3189 LogPrintf("Unpark chain up to block %s as it has "
3190 "accumulated enough PoW.\n",
3191 pindexNew->GetBlockHash().ToString());
3192 fParkedChain = false;
3193 UnparkBlock(pindexTest);
3194 }
3195 }
3196
3197 // Pruned nodes may have entries in setBlockIndexCandidates for
3198 // which block files have been deleted. Remove those as candidates
3199 // for the most work chain if we come across them; we can't switch
3200 // to a chain unless we have all the non-active-chain parent blocks.
3201 bool fInvalidChain = pindexTest->nStatus.isInvalid();
3202 bool fMissingData = !pindexTest->nStatus.hasData();
3203 if (!(fInvalidChain || fParkedChain || fMissingData)) {
3204 // The current block is acceptable, move to the parent, up to
3205 // the fork point.
3206 pindexTest = pindexTest->pprev;
3207 continue;
3208 }
3209
3210 // Candidate chain is not usable (either invalid or parked or
3211 // missing data)
3212 hasValidAncestor = false;
3213 setBlockIndexCandidates.erase(pindexTest);
3214
3215 if (fInvalidChain && (m_chainman.m_best_invalid == nullptr ||
3216 pindexNew->nChainWork >
3217 m_chainman.m_best_invalid->nChainWork)) {
3218 m_chainman.m_best_invalid = pindexNew;
3219 }
3220
3221 if (fParkedChain && (m_chainman.m_best_parked == nullptr ||
3222 pindexNew->nChainWork >
3223 m_chainman.m_best_parked->nChainWork)) {
3224 m_chainman.m_best_parked = pindexNew;
3225 }
3226
3227 LogPrintf("Considered switching to better tip %s but that chain "
3228 "contains a%s%s%s block.\n",
3229 pindexNew->GetBlockHash().ToString(),
3230 fInvalidChain ? "n invalid" : "",
3231 fParkedChain ? " parked" : "",
3232 fMissingData ? " missing-data" : "");
3233
3234 CBlockIndex *pindexFailed = pindexNew;
3235 // Remove the entire chain from the set.
3236 while (pindexTest != pindexFailed) {
3237 if (fInvalidChain || fParkedChain) {
3238 pindexFailed->nStatus =
3239 pindexFailed->nStatus.withFailedParent(fInvalidChain)
3240 .withParkedParent(fParkedChain);
3241 } else if (fMissingData) {
3242 // If we're missing data, then add back to
3243 // m_blocks_unlinked, so that if the block arrives in the
3244 // future we can try adding to setBlockIndexCandidates
3245 // again.
3247 std::make_pair(pindexFailed->pprev, pindexFailed));
3248 }
3249 setBlockIndexCandidates.erase(pindexFailed);
3250 pindexFailed = pindexFailed->pprev;
3251 }
3252
3253 if (fInvalidChain || fParkedChain) {
3254 // We discovered a new chain tip that is either parked or
3255 // invalid, we may want to warn.
3257 }
3258 }
3259
3260 blocksToReconcile.push_back(pindexNew);
3261
3262 // We found a candidate that has valid ancestors. This is our guy.
3263 if (hasValidAncestor) {
3264 return pindexNew;
3265 }
3266 } while (true);
3267}
3268
3274 // Note that we can't delete the current block itself, as we may need to
3275 // return to it later in case a reorganization to a better block fails.
3276 auto it = setBlockIndexCandidates.begin();
3277 while (it != setBlockIndexCandidates.end() &&
3278 setBlockIndexCandidates.value_comp()(*it, m_chain.Tip())) {
3279 setBlockIndexCandidates.erase(it++);
3280 }
3281
3282 // Either the current tip or a successor of it we're working towards is left
3283 // in setBlockIndexCandidates.
3285}
3286
3295 BlockValidationState &state, CBlockIndex *pindexMostWork,
3296 const std::shared_ptr<const CBlock> &pblock, bool &fInvalidFound,
3297 const avalanche::Processor *const avalanche,
3298 const ChainstateRole chainstate_role) {
3300 if (m_mempool) {
3302 }
3303
3304 const CBlockIndex *pindexOldTip = m_chain.Tip();
3305 const CBlockIndex *pindexFork = m_chain.FindFork(pindexMostWork);
3306
3307 // Disconnect active blocks which are no longer in the best chain.
3308 bool fBlocksDisconnected = false;
3309 DisconnectedBlockTransactions disconnectpool;
3310 while (m_chain.Tip() && m_chain.Tip() != pindexFork) {
3311 if (m_mempool && !fBlocksDisconnected) {
3312 // Import and clear mempool; we must do this to preserve
3313 // topological ordering in the mempool index. This is ok since
3314 // inserts into the mempool are very fast now in our new
3315 // implementation.
3316 disconnectpool.importMempool(*m_mempool);
3317 }
3318
3319 if (!DisconnectTip(state, &disconnectpool)) {
3320 // This is likely a fatal error, but keep the mempool consistent,
3321 // just in case. Only remove from the mempool in this case.
3322 if (m_mempool) {
3323 disconnectpool.updateMempoolForReorg(*this, false, *m_mempool);
3324 }
3325
3326 // If we're unable to disconnect a block during normal operation,
3327 // then that is a failure of our local system -- we should abort
3328 // rather than stay on a less work chain.
3329 AbortNode(state,
3330 "Failed to disconnect block; see debug.log for details");
3331 return false;
3332 }
3333
3334 fBlocksDisconnected = true;
3335 }
3336
3337 // Build list of new blocks to connect.
3338 std::vector<CBlockIndex *> vpindexToConnect;
3339 bool fContinue = true;
3340 int nHeight = pindexFork ? pindexFork->nHeight : -1;
3341 while (fContinue && nHeight != pindexMostWork->nHeight) {
3342 // Don't iterate the entire list of potential improvements toward the
3343 // best tip, as we likely only need a few blocks along the way.
3344 int nTargetHeight = std::min(nHeight + 32, pindexMostWork->nHeight);
3345 vpindexToConnect.clear();
3346 vpindexToConnect.reserve(nTargetHeight - nHeight);
3347 CBlockIndex *pindexIter = pindexMostWork->GetAncestor(nTargetHeight);
3348 while (pindexIter && pindexIter->nHeight != nHeight) {
3349 vpindexToConnect.push_back(pindexIter);
3350 pindexIter = pindexIter->pprev;
3351 }
3352
3353 nHeight = nTargetHeight;
3354
3355 // Connect new blocks.
3356 for (CBlockIndex *pindexConnect : reverse_iterate(vpindexToConnect)) {
3357 BlockPolicyValidationState blockPolicyState;
3358 if (!ConnectTip(state, blockPolicyState, pindexConnect,
3359 pindexConnect == pindexMostWork
3360 ? pblock
3361 : std::shared_ptr<const CBlock>(),
3362 disconnectpool, avalanche, chainstate_role)) {
3363 if (state.IsInvalid()) {
3364 // The block violates a consensus rule.
3365 if (state.GetResult() !=
3367 InvalidChainFound(vpindexToConnect.front());
3368 }
3369 state = BlockValidationState();
3370 fInvalidFound = true;
3371 fContinue = false;
3372 break;
3373 }
3374
3375 if (blockPolicyState.IsInvalid()) {
3376 // The block violates a policy rule.
3377 fContinue = false;
3378 break;
3379 }
3380
3381 // A system error occurred (disk space, database error, ...).
3382 // Make the mempool consistent with the current tip, just in
3383 // case any observers try to use it before shutdown.
3384 if (m_mempool) {
3385 disconnectpool.updateMempoolForReorg(*this, false,
3386 *m_mempool);
3387 }
3388 return false;
3389 } else {
3391 if (!pindexOldTip ||
3392 m_chain.Tip()->nChainWork > pindexOldTip->nChainWork) {
3393 // We're in a better position than we were. Return
3394 // temporarily to release the lock.
3395 fContinue = false;
3396 break;
3397 }
3398 }
3399 }
3400 }
3401
3402 if (m_mempool) {
3403 if (fBlocksDisconnected || !disconnectpool.isEmpty()) {
3404 // If any blocks were disconnected, we need to update the mempool
3405 // even if disconnectpool is empty. The disconnectpool may also be
3406 // non-empty if the mempool was imported due to new validation rules
3407 // being in effect.
3409 "Updating mempool due to reorganization or "
3410 "rules upgrade/downgrade\n");
3411 disconnectpool.updateMempoolForReorg(*this, true, *m_mempool);
3412 }
3413
3414 m_mempool->check(this->CoinsTip(), this->m_chain.Height() + 1);
3415 }
3416
3417 // Callbacks/notifications for a new best chain.
3418 if (fInvalidFound) {
3420 } else {
3422 }
3423
3424 return true;
3425}
3426
3428 if (!init) {
3430 }
3431 if (::fReindex) {
3433 }
3435}
3436
3439 bool fNotify = false;
3440 bool fInitialBlockDownload = false;
3441 static CBlockIndex *pindexHeaderOld = nullptr;
3442 CBlockIndex *pindexHeader = nullptr;
3443 {
3444 LOCK(cs_main);
3445 pindexHeader = chainman.m_best_header;
3446
3447 if (pindexHeader != pindexHeaderOld) {
3448 fNotify = true;
3449 fInitialBlockDownload = chainman.IsInitialBlockDownload();
3450 pindexHeaderOld = pindexHeader;
3451 }
3452 }
3453
3454 // Send block tip changed notifications without cs_main
3455 if (fNotify) {
3456 chainman.GetNotifications().headerTip(
3457 GetSynchronizationState(fInitialBlockDownload),
3458 pindexHeader->nHeight, pindexHeader->nTime, false);
3459 }
3460 return fNotify;
3461}
3462
3465
3466 if (GetMainSignals().CallbacksPending() > 10) {
3468 }
3469}
3470
3472 std::shared_ptr<const CBlock> pblock,
3475
3476 // Note that while we're often called here from ProcessNewBlock, this is
3477 // far from a guarantee. Things in the P2P/RPC will often end up calling
3478 // us in the middle of ProcessNewBlock - do not assume pblock is set
3479 // sanely for performance or correctness!
3481
3482 // ABC maintains a fair degree of expensive-to-calculate internal state
3483 // because this function periodically releases cs_main so that it does not
3484 // lock up other threads for too long during large connects - and to allow
3485 // for e.g. the callback queue to drain we use m_chainstate_mutex to enforce
3486 // mutual exclusion so that only one caller may execute this function at a
3487 // time
3489
3490 // Belt-and-suspenders check that we aren't attempting to advance the
3491 // background chainstate past the snapshot base block.
3492 if (WITH_LOCK(::cs_main, return m_disabled)) {
3493 LogPrintf("m_disabled is set - this chainstate should not be in "
3494 "operation. Please report this as a bug. %s\n",
3495 PACKAGE_BUGREPORT);
3496 return false;
3497 }
3498
3499 CBlockIndex *pindexMostWork = nullptr;
3500 CBlockIndex *pindexNewTip = nullptr;
3501 int nStopAtHeight = gArgs.GetIntArg("-stopatheight", DEFAULT_STOPATHEIGHT);
3502 bool exited_ibd{false};
3503 do {
3504 // Block until the validation queue drains. This should largely
3505 // never happen in normal operation, however may happen during
3506 // reindex, causing memory blowup if we run too far ahead.
3507 // Note that if a validationinterface callback ends up calling
3508 // ActivateBestChain this may lead to a deadlock! We should
3509 // probably have a DEBUG_LOCKORDER test for this in the future.
3511
3512 std::vector<const CBlockIndex *> blocksToReconcile;
3513 bool blocks_connected = false;
3514
3515 const bool fAutoUnpark =
3516 gArgs.GetBoolArg("-automaticunparking", !avalanche);
3517
3518 {
3519 LOCK(cs_main);
3520 // Lock transaction pool for at least as long as it takes for
3521 // updateMempoolForReorg to be executed if needed
3522 LOCK(MempoolMutex());
3523 const bool was_in_ibd = m_chainman.IsInitialBlockDownload();
3524 CBlockIndex *starting_tip = m_chain.Tip();
3525 do {
3526 // We absolutely may not unlock cs_main until we've made forward
3527 // progress (with the exception of shutdown due to hardware
3528 // issues, low disk space, etc).
3529
3530 if (pindexMostWork == nullptr) {
3531 pindexMostWork =
3532 FindMostWorkChain(blocksToReconcile, fAutoUnpark);
3533 }
3534
3535 // Whether we have anything to do at all.
3536 if (pindexMostWork == nullptr ||
3537 pindexMostWork == m_chain.Tip()) {
3538 break;
3539 }
3540
3541 bool fInvalidFound = false;
3542 std::shared_ptr<const CBlock> nullBlockPtr;
3543 // BlockConnected signals must be sent for the original role;
3544 // in case snapshot validation is completed during
3545 // ActivateBestChainStep, the result of GetRole() changes from
3546 // BACKGROUND to NORMAL.
3547 const ChainstateRole chainstate_role{this->GetRole()};
3549 state, pindexMostWork,
3550 pblock && pblock->GetHash() ==
3551 pindexMostWork->GetBlockHash()
3552 ? pblock
3553 : nullBlockPtr,
3554 fInvalidFound, avalanche, chainstate_role)) {
3555 // A system error occurred
3556 return false;
3557 }
3558 blocks_connected = true;
3559
3560 if (fInvalidFound ||
3561 (pindexMostWork && pindexMostWork->nStatus.isParked())) {
3562 // Wipe cache, we may need another branch now.
3563 pindexMostWork = nullptr;
3564 }
3565
3566 pindexNewTip = m_chain.Tip();
3567
3568 // This will have been toggled in
3569 // ActivateBestChainStep -> ConnectTip ->
3570 // MaybeCompleteSnapshotValidation, if at all, so we should
3571 // catch it here.
3572 //
3573 // Break this do-while to ensure we don't advance past the base
3574 // snapshot.
3575 if (m_disabled) {
3576 break;
3577 }
3578 } while (!m_chain.Tip() ||
3579 (starting_tip && CBlockIndexWorkComparator()(
3580 m_chain.Tip(), starting_tip)));
3581
3582 // Check the index once we're done with the above loop, since
3583 // we're going to release cs_main soon. If the index is in a bad
3584 // state now, then it's better to know immediately rather than
3585 // randomly have it cause a problem in a race.
3587
3588 if (blocks_connected) {
3589 const CBlockIndex *pindexFork = m_chain.FindFork(starting_tip);
3590 bool still_in_ibd = m_chainman.IsInitialBlockDownload();
3591
3592 if (was_in_ibd && !still_in_ibd) {
3593 // Active chainstate has exited IBD
3594 exited_ibd = true;
3595 }
3596
3597 // Notify external listeners about the new tip.
3598 // Enqueue while holding cs_main to ensure that UpdatedBlockTip
3599 // is called in the order in which blocks are connected
3600 if (this == &m_chainman.ActiveChainstate() &&
3601 pindexFork != pindexNewTip) {
3602 // Notify ValidationInterface subscribers
3603 GetMainSignals().UpdatedBlockTip(pindexNewTip, pindexFork,
3604 still_in_ibd);
3605
3606 // Always notify the UI if a new block tip was connected
3608 GetSynchronizationState(still_in_ibd), *pindexNewTip);
3609 }
3610 }
3611 }
3612 // When we reach this point, we switched to a new tip (stored in
3613 // pindexNewTip).
3614 if (avalanche) {
3615 const CBlockIndex *pfinalized =
3617 return m_avalancheFinalizedBlockIndex);
3618 for (const CBlockIndex *pindex : blocksToReconcile) {
3619 avalanche->addToReconcile(pindex);
3620
3621 // Compute staking rewards for all blocks with more chainwork to
3622 // just after the finalized block. We could stop at the fork
3623 // point, but this is more robust.
3624 if (blocks_connected) {
3625 const CBlockIndex *pindexTest = pindex;
3626 while (pindexTest && pindexTest != pfinalized) {
3627 if (pindexTest->nHeight < pindex->nHeight - 3) {
3628 // Only compute up to some max depth
3629 break;
3630 }
3631 avalanche->computeStakingReward(pindexTest);
3632 pindexTest = pindexTest->pprev;
3633 }
3634 }
3635 }
3636 }
3637
3638 if (!blocks_connected) {
3639 return true;
3640 }
3641
3642 if (nStopAtHeight && pindexNewTip &&
3643 pindexNewTip->nHeight >= nStopAtHeight) {
3644 StartShutdown();
3645 }
3646
3647 if (exited_ibd) {
3648 // If a background chainstate is in use, we may need to rebalance
3649 // our allocation of caches once a chainstate exits initial block
3650 // download.
3651 LOCK(::cs_main);
3652 m_chainman.MaybeRebalanceCaches();
3653 }
3654
3655 if (WITH_LOCK(::cs_main, return m_disabled)) {
3656 // Background chainstate has reached the snapshot base block, so
3657 // exit.
3658
3659 // Restart indexes to resume indexing for all blocks unique to the
3660 // snapshot chain. This resumes indexing "in order" from where the
3661 // indexing on the background validation chain left off.
3662 //
3663 // This cannot be done while holding cs_main (within
3664 // MaybeCompleteSnapshotValidation) or a cs_main deadlock will
3665 // occur.
3668 }
3669 break;
3670 }
3671
3672 // We check shutdown only after giving ActivateBestChainStep a chance to
3673 // run once so that we never shutdown before connecting the genesis
3674 // block during LoadChainTip(). Previously this caused an assert()
3675 // failure during shutdown in such cases as the UTXO DB flushing checks
3676 // that the best block hash is non-null.
3677 if (ShutdownRequested()) {
3678 break;
3679 }
3680 } while (pindexNewTip != pindexMostWork);
3681
3682 // Write changes periodically to disk, after relay.
3684 return false;
3685 }
3686
3687 return true;
3688}
3689
3694 {
3695 LOCK(cs_main);
3696 if (pindex->nChainWork < m_chain.Tip()->nChainWork) {
3697 // Nothing to do, this block is not at the tip.
3698 return true;
3699 }
3700
3702 // The chain has been extended since the last call, reset the
3703 // counter.
3705 }
3706
3708 setBlockIndexCandidates.erase(pindex);
3711 std::numeric_limits<int32_t>::min()) {
3712 // We can't keep reducing the counter if somebody really wants to
3713 // call preciousblock 2**31-1 times on the same set of tips...
3715 }
3716
3717 // In case this was parked, unpark it.
3718 UnparkBlock(pindex);
3719
3720 // Make sure it is added to the candidate list if appropriate.
3721 if (pindex->IsValid(BlockValidity::TRANSACTIONS) &&
3722 pindex->HaveNumChainTxs()) {
3723 setBlockIndexCandidates.insert(pindex);
3725 }
3726 }
3727
3728 return ActivateBestChain(state, /*pblock=*/nullptr, avalanche);
3729}
3730
3731namespace {
3732// Leverage RAII to run a functor at scope end
3733template <typename Func> struct Defer {
3734 Func func;
3735 Defer(Func &&f) : func(std::move(f)) {}
3736 ~Defer() { func(); }
3737};
3738} // namespace
3739
3741 bool invalidate) {
3742 // Genesis block can't be invalidated or parked
3743 assert(pindex);
3744 if (pindex->nHeight == 0) {
3745 return false;
3746 }
3747
3748 CBlockIndex *to_mark_failed_or_parked = pindex;
3749 bool pindex_was_in_chain = false;
3750 int disconnected = 0;
3751
3752 // We do not allow ActivateBestChain() to run while UnwindBlock() is
3753 // running, as that could cause the tip to change while we disconnect
3754 // blocks. (Note for backport of Core PR16849: we acquire
3755 // LOCK(m_chainstate_mutex) in the Park, Invalidate and FinalizeBlock
3756 // functions due to differences in our code)
3758
3759 // We'll be acquiring and releasing cs_main below, to allow the validation
3760 // callbacks to run. However, we should keep the block index in a
3761 // consistent state as we disconnect blocks -- in particular we need to
3762 // add equal-work blocks to setBlockIndexCandidates as we disconnect.
3763 // To avoid walking the block index repeatedly in search of candidates,
3764 // build a map once so that we can look up candidate blocks by chain
3765 // work as we go.
3766 std::multimap<const arith_uint256, CBlockIndex *> candidate_blocks_by_work;
3767
3768 {
3769 LOCK(cs_main);
3770 for (auto &entry : m_blockman.m_block_index) {
3771 CBlockIndex *candidate = &entry.second;
3772 // We don't need to put anything in our active chain into the
3773 // multimap, because those candidates will be found and considered
3774 // as we disconnect.
3775 // Instead, consider only non-active-chain blocks that have at
3776 // least as much work as where we expect the new tip to end up.
3777 if (!m_chain.Contains(candidate) &&
3778 !CBlockIndexWorkComparator()(candidate, pindex->pprev) &&
3780 candidate->HaveNumChainTxs()) {
3781 candidate_blocks_by_work.insert(
3782 std::make_pair(candidate->nChainWork, candidate));
3783 }
3784 }
3785 }
3786
3787 {
3788 LOCK(cs_main);
3789 // Lock for as long as disconnectpool is in scope to make sure
3790 // UpdateMempoolForReorg is called after DisconnectTip without unlocking
3791 // in between
3792 LOCK(MempoolMutex());
3793
3794 constexpr int maxDisconnectPoolBlocks = 10;
3795 bool ret = false;
3796 DisconnectedBlockTransactions disconnectpool;
3797 // After 10 blocks this becomes nullptr, so that DisconnectTip will
3798 // stop giving us unwound block txs if we are doing a deep unwind.
3799 DisconnectedBlockTransactions *optDisconnectPool = &disconnectpool;
3800
3801 // Disable thread safety analysis because we can't require m_mempool->cs
3802 // as m_mempool can be null. We keep the runtime analysis though.
3803 Defer deferred([&]() NO_THREAD_SAFETY_ANALYSIS {
3805 if (m_mempool && !disconnectpool.isEmpty()) {
3807 // DisconnectTip will add transactions to disconnectpool.
3808 // When all unwinding is done and we are on a new tip, we must
3809 // add all transactions back to the mempool against the new tip.
3810 disconnectpool.updateMempoolForReorg(*this,
3811 /* fAddToMempool = */ ret,
3812 *m_mempool);
3813 }
3814 });
3815
3816 // Disconnect (descendants of) pindex, and mark them invalid.
3817 while (true) {
3818 if (ShutdownRequested()) {
3819 break;
3820 }
3821
3822 // Make sure the queue of validation callbacks doesn't grow
3823 // unboundedly.
3824 // FIXME this commented code is a regression and could cause OOM if
3825 // a very old block is invalidated via the invalidateblock RPC.
3826 // This can be uncommented if the main signals are moved away from
3827 // cs_main or this code is refactored so that cs_main can be
3828 // released at this point.
3829 //
3830 // LimitValidationInterfaceQueue();
3831
3832 if (!m_chain.Contains(pindex)) {
3833 break;
3834 }
3835
3836 if (m_mempool && disconnected == 0) {
3837 // On first iteration, we grab all the mempool txs to preserve
3838 // topological ordering. This has the side-effect of temporarily
3839 // clearing the mempool, but we will re-add later in
3840 // updateMempoolForReorg() (above). This technique guarantees
3841 // mempool consistency as well as ensures that our topological
3842 // entry_id index is always correct.
3843 disconnectpool.importMempool(*m_mempool);
3844 }
3845
3846 pindex_was_in_chain = true;
3847 CBlockIndex *invalid_walk_tip = m_chain.Tip();
3848
3849 // ActivateBestChain considers blocks already in m_chain
3850 // unconditionally valid already, so force disconnect away from it.
3851
3852 ret = DisconnectTip(state, optDisconnectPool);
3853 ++disconnected;
3854
3855 if (optDisconnectPool && disconnected > maxDisconnectPoolBlocks) {
3856 // Stop using the disconnect pool after 10 blocks. After 10
3857 // blocks we no longer add block tx's to the disconnectpool.
3858 // However, when this scope ends we will reconcile what's
3859 // in the pool with the new tip (in the deferred d'tor above).
3860 optDisconnectPool = nullptr;
3861 }
3862
3863 if (!ret) {
3864 return false;
3865 }
3866
3867 assert(invalid_walk_tip->pprev == m_chain.Tip());
3868
3869 // We immediately mark the disconnected blocks as invalid.
3870 // This prevents a case where pruned nodes may fail to
3871 // invalidateblock and be left unable to start as they have no tip
3872 // candidates (as there are no blocks that meet the "have data and
3873 // are not invalid per nStatus" criteria for inclusion in
3874 // setBlockIndexCandidates).
3875
3876 invalid_walk_tip->nStatus =
3877 invalidate ? invalid_walk_tip->nStatus.withFailed()
3878 : invalid_walk_tip->nStatus.withParked();
3879
3880 m_blockman.m_dirty_blockindex.insert(invalid_walk_tip);
3881 setBlockIndexCandidates.insert(invalid_walk_tip->pprev);
3882
3883 if (invalid_walk_tip == to_mark_failed_or_parked->pprev &&
3884 (invalidate ? to_mark_failed_or_parked->nStatus.hasFailed()
3885 : to_mark_failed_or_parked->nStatus.isParked())) {
3886 // We only want to mark the last disconnected block as
3887 // Failed (or Parked); its children need to be FailedParent (or
3888 // ParkedParent) instead.
3889 to_mark_failed_or_parked->nStatus =
3890 (invalidate
3891 ? to_mark_failed_or_parked->nStatus.withFailed(false)
3892 .withFailedParent()
3893 : to_mark_failed_or_parked->nStatus.withParked(false)
3894 .withParkedParent());
3895
3896 m_blockman.m_dirty_blockindex.insert(to_mark_failed_or_parked);
3897 }
3898
3899 // Add any equal or more work headers to setBlockIndexCandidates
3900 auto candidate_it = candidate_blocks_by_work.lower_bound(
3901 invalid_walk_tip->pprev->nChainWork);
3902 while (candidate_it != candidate_blocks_by_work.end()) {
3903 if (!CBlockIndexWorkComparator()(candidate_it->second,
3904 invalid_walk_tip->pprev)) {
3905 setBlockIndexCandidates.insert(candidate_it->second);
3906 candidate_it = candidate_blocks_by_work.erase(candidate_it);
3907 } else {
3908 ++candidate_it;
3909 }
3910 }
3911
3912 // Track the last disconnected block, so we can correct its
3913 // FailedParent (or ParkedParent) status in future iterations, or,
3914 // if it's the last one, call InvalidChainFound on it.
3915 to_mark_failed_or_parked = invalid_walk_tip;
3916 }
3917 }
3918
3920
3921 {
3922 LOCK(cs_main);
3923 if (m_chain.Contains(to_mark_failed_or_parked)) {
3924 // If the to-be-marked invalid block is in the active chain,
3925 // something is interfering and we can't proceed.
3926 return false;
3927 }
3928
3929 // Mark pindex (or the last disconnected block) as invalid (or parked),
3930 // even when it never was in the main chain.
3931 to_mark_failed_or_parked->nStatus =
3932 invalidate ? to_mark_failed_or_parked->nStatus.withFailed()
3933 : to_mark_failed_or_parked->nStatus.withParked();
3934 m_blockman.m_dirty_blockindex.insert(to_mark_failed_or_parked);
3935 if (invalidate) {
3936 m_chainman.m_failed_blocks.insert(to_mark_failed_or_parked);
3937 }
3938
3939 // If any new blocks somehow arrived while we were disconnecting
3940 // (above), then the pre-calculation of what should go into
3941 // setBlockIndexCandidates may have missed entries. This would
3942 // technically be an inconsistency in the block index, but if we clean
3943 // it up here, this should be an essentially unobservable error.
3944 // Loop back over all block index entries and add any missing entries
3945 // to setBlockIndexCandidates.
3946 for (auto &[_, block_index] : m_blockman.m_block_index) {
3947 if (block_index.IsValid(BlockValidity::TRANSACTIONS) &&
3948 block_index.HaveNumChainTxs() &&
3949 !setBlockIndexCandidates.value_comp()(&block_index,
3950 m_chain.Tip())) {
3951 setBlockIndexCandidates.insert(&block_index);
3952 }
3953 }
3954
3955 if (invalidate) {
3956 InvalidChainFound(to_mark_failed_or_parked);
3957 }
3958 }
3959
3960 // Only notify about a new block tip if the active chain was modified.
3961 if (pindex_was_in_chain) {
3964 *to_mark_failed_or_parked->pprev);
3965 }
3966 return true;
3967}
3968
3970 CBlockIndex *pindex) {
3973 // See 'Note for backport of Core PR16849' in Chainstate::UnwindBlock
3975
3976 return UnwindBlock(state, pindex, true);
3977}
3978
3982 // See 'Note for backport of Core PR16849' in Chainstate::UnwindBlock
3984
3985 return UnwindBlock(state, pindex, false);
3986}
3987
3988template <typename F>
3990 CBlockIndex *pindex, F f) {
3991 BlockStatus newStatus = f(pindex->nStatus);
3992 if (pindex->nStatus != newStatus &&
3993 (!pindexBase ||
3994 pindex->GetAncestor(pindexBase->nHeight) == pindexBase)) {
3995 pindex->nStatus = newStatus;
3996 m_blockman.m_dirty_blockindex.insert(pindex);
3997 if (newStatus.isValid()) {
3998 m_chainman.m_failed_blocks.erase(pindex);
3999 }
4000
4001 if (pindex->IsValid(BlockValidity::TRANSACTIONS) &&
4002 pindex->HaveNumChainTxs() &&
4003 setBlockIndexCandidates.value_comp()(m_chain.Tip(), pindex)) {
4004 setBlockIndexCandidates.insert(pindex);
4005 }
4006 return true;
4007 }
4008 return false;
4009}
4010
4011template <typename F, typename C, typename AC>
4013 F f, C fChild, AC fAncestorWasChanged) {
4015
4016 // Update the current block and ancestors; while we're doing this, identify
4017 // which was the deepest ancestor we changed.
4018 CBlockIndex *pindexDeepestChanged = pindex;
4019 for (auto pindexAncestor = pindex; pindexAncestor != nullptr;
4020 pindexAncestor = pindexAncestor->pprev) {
4021 if (UpdateFlagsForBlock(nullptr, pindexAncestor, f)) {
4022 pindexDeepestChanged = pindexAncestor;
4023 }
4024 }
4025
4026 if (pindexReset &&
4027 pindexReset->GetAncestor(pindexDeepestChanged->nHeight) ==
4028 pindexDeepestChanged) {
4029 // reset pindexReset if it had a modified ancestor.
4030 pindexReset = nullptr;
4031 }
4032
4033 // Update all blocks under modified blocks.
4034 for (auto &[_, block_index] : m_blockman.m_block_index) {
4035 UpdateFlagsForBlock(pindex, &block_index, fChild);
4036 UpdateFlagsForBlock(pindexDeepestChanged, &block_index,
4037 fAncestorWasChanged);
4038 }
4039}
4040
4041void Chainstate::SetBlockFailureFlags(CBlockIndex *invalid_block) {
4043
4044 for (auto &[_, block_index] : m_blockman.m_block_index) {
4045 if (block_index.GetAncestor(invalid_block->nHeight) == invalid_block &&
4046 !block_index.nStatus.isInvalid()) {
4047 block_index.nStatus = block_index.nStatus.withFailedParent();
4048 }
4049 }
4050}
4051
4054
4056 pindex, m_chainman.m_best_invalid,
4057 [](const BlockStatus status) {
4058 return status.withClearedFailureFlags();
4059 },
4060 [](const BlockStatus status) {
4061 return status.withClearedFailureFlags();
4062 },
4063 [](const BlockStatus status) {
4064 return status.withFailedParent(false);
4065 });
4066}
4067
4070 // The block only is a candidate for the most-work-chain if it has the same
4071 // or more work than our current tip.
4072 if (m_chain.Tip() != nullptr &&
4073 setBlockIndexCandidates.value_comp()(pindex, m_chain.Tip())) {
4074 return;
4075 }
4076
4077 bool is_active_chainstate = this == &m_chainman.ActiveChainstate();
4078 if (is_active_chainstate) {
4079 // The active chainstate should always add entries that have more
4080 // work than the tip.
4081 setBlockIndexCandidates.insert(pindex);
4082 } else if (!m_disabled) {
4083 // For the background chainstate, we only consider connecting blocks
4084 // towards the snapshot base (which can't be nullptr or else we'll
4085 // never make progress).
4086 const CBlockIndex *snapshot_base{
4087 Assert(m_chainman.GetSnapshotBaseBlock())};
4088 if (snapshot_base->GetAncestor(pindex->nHeight) == pindex) {
4089 setBlockIndexCandidates.insert(pindex);
4090 }
4091 }
4092}
4093
4094void Chainstate::UnparkBlockImpl(CBlockIndex *pindex, bool fClearChildren) {
4096
4098 pindex, m_chainman.m_best_parked,
4099 [](const BlockStatus status) {
4100 return status.withClearedParkedFlags();
4101 },
4102 [fClearChildren](const BlockStatus status) {
4103 return fClearChildren ? status.withClearedParkedFlags()
4104 : status.withParkedParent(false);
4105 },
4106 [](const BlockStatus status) {
4107 return status.withParkedParent(false);
4108 });
4109}
4110
4112 return UnparkBlockImpl(pindex, true);
4113}
4114
4116 return UnparkBlockImpl(pindex, false);
4117}
4118
4122
4123 if (!pindex) {
4124 return false;
4125 }
4126
4127 if (!m_chain.Contains(pindex)) {
4129 "The block to mark finalized by avalanche is not on the "
4130 "active chain: %s\n",
4131 pindex->GetBlockHash().ToString());
4132 return false;
4133 }
4134
4135 avalanche.cleanupStakingRewards(pindex->nHeight);
4136
4137 if (IsBlockAvalancheFinalized(pindex)) {
4138 return true;
4139 }
4140
4141 {
4143 m_avalancheFinalizedBlockIndex = pindex;
4144 }
4145
4146 WITH_LOCK(cs_main, GetMainSignals().BlockFinalized(pindex));
4147
4148 return true;
4149}
4150
4153 m_avalancheFinalizedBlockIndex = nullptr;
4154}
4155
4158 return pindex && m_avalancheFinalizedBlockIndex &&
4159 m_avalancheFinalizedBlockIndex->GetAncestor(pindex->nHeight) ==
4160 pindex;
4161}
4162
4168 CBlockIndex *pindexNew,
4169 const FlatFilePos &pos) {
4170 pindexNew->nTx = block.vtx.size();
4171 // Typically nChainTX will be 0 at this point, but it can be nonzero if this
4172 // is a pruned block which is being downloaded again, or if this is an
4173 // assumeutxo snapshot block which has a hardcoded m_chain_tx_count value
4174 // from the snapshot metadata. If the pindex is not the snapshot block and
4175 // the nChainTx value is not zero, assert that value is actually correct.
4176 auto prev_tx_sum = [](CBlockIndex &block) {
4177 return block.nTx + (block.pprev ? block.pprev->nChainTx : 0);
4178 };
4179 if (!Assume(pindexNew->nChainTx == 0 ||
4180 pindexNew->nChainTx == prev_tx_sum(*pindexNew) ||
4181 pindexNew == GetSnapshotBaseBlock())) {
4182 LogPrintf("Internal bug detected: block %d has unexpected nChainTx %i "
4183 "that should be %i. Please report this issue here: %s\n",
4184 pindexNew->nHeight, pindexNew->nChainTx,
4185 prev_tx_sum(*pindexNew), PACKAGE_BUGREPORT);
4186 pindexNew->nChainTx = 0;
4187 }
4188 pindexNew->nSize = ::GetSerializeSize(block, PROTOCOL_VERSION);
4189 pindexNew->nFile = pos.nFile;
4190 pindexNew->nDataPos = pos.nPos;
4191 pindexNew->nUndoPos = 0;
4192 pindexNew->nStatus = pindexNew->nStatus.withData();
4194 m_blockman.m_dirty_blockindex.insert(pindexNew);
4195
4196 if (pindexNew->pprev == nullptr || pindexNew->pprev->HaveNumChainTxs()) {
4197 // If pindexNew is the genesis block or all parents are
4198 // BLOCK_VALID_TRANSACTIONS.
4199 std::deque<CBlockIndex *> queue;
4200 queue.push_back(pindexNew);
4201
4202 // Recursively process any descendant blocks that now may be eligible to
4203 // be connected.
4204 while (!queue.empty()) {
4205 CBlockIndex *pindex = queue.front();
4206 queue.pop_front();
4207 // Before setting nChainTx, assert that it is 0 or already set to
4208 // the correct value. This assert will fail after receiving the
4209 // assumeutxo snapshot block if assumeutxo snapshot metadata has an
4210 // incorrect hardcoded AssumeutxoData::nChainTx value.
4211 if (!Assume(pindex->nChainTx == 0 ||
4212 pindex->nChainTx == prev_tx_sum(*pindex))) {
4213 LogPrintf(
4214 "Internal bug detected: block %d has unexpected nChainTx "
4215 "%i that should be %i. Please report this issue here: %s\n",
4216 pindex->nHeight, pindex->nChainTx, prev_tx_sum(*pindex),
4217 PACKAGE_BUGREPORT);
4218 }
4219 pindex->nChainTx = prev_tx_sum(*pindex);
4220 if (pindex->nSequenceId == 0) {
4221 // We assign a sequence is when transaction are received to
4222 // prevent a miner from being able to broadcast a block but not
4223 // its content. However, a sequence id may have been set
4224 // manually, for instance via PreciousBlock, in which case, we
4225 // don't need to assign one.
4226 pindex->nSequenceId = nBlockSequenceId++;
4227 }
4228 for (Chainstate *c : GetAll()) {
4229 c->TryAddBlockIndexCandidate(pindex);
4230 }
4231
4232 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
4233 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
4234 range = m_blockman.m_blocks_unlinked.equal_range(pindex);
4235 while (range.first != range.second) {
4236 std::multimap<CBlockIndex *, CBlockIndex *>::iterator it =
4237 range.first;
4238 queue.push_back(it->second);
4239 range.first++;
4240 m_blockman.m_blocks_unlinked.erase(it);
4241 }
4242 }
4243 } else if (pindexNew->pprev &&
4244 pindexNew->pprev->IsValid(BlockValidity::TREE)) {
4246 std::make_pair(pindexNew->pprev, pindexNew));
4247 }
4248}
4249
4258static bool CheckBlockHeader(const CBlockHeader &block,
4259 BlockValidationState &state,
4260 const Consensus::Params &params,
4261 BlockValidationOptions validationOptions) {
4262 // Check proof of work matches claimed amount
4263 if (validationOptions.shouldValidatePoW() &&
4264 !CheckProofOfWork(block.GetHash(), block.nBits, params)) {
4266 "high-hash", "proof of work failed");
4267 }
4268
4269 return true;
4270}
4271
4272static bool CheckMerkleRoot(const CBlock &block, BlockValidationState &state) {
4273 if (block.m_checked_merkle_root) {
4274 return true;
4275 }
4276
4277 bool mutated;
4278 uint256 merkle_root = BlockMerkleRoot(block, &mutated);
4279 if (block.hashMerkleRoot != merkle_root) {
4280 return state.Invalid(
4282 /*reject_reason=*/"bad-txnmrklroot",
4283 /*debug_message=*/"hashMerkleRoot mismatch");
4284 }
4285
4286 // Check for merkle tree malleability (CVE-2012-2459): repeating sequences
4287 // of transactions in a block without affecting the merkle root of a block,
4288 // while still invalidating it.
4289 if (mutated) {
4290 return state.Invalid(
4292 /*reject_reason=*/"bad-txns-duplicate",
4293 /*debug_message=*/"duplicate transaction");
4294 }
4295
4296 block.m_checked_merkle_root = true;
4297 return true;
4298}
4299
4300bool CheckBlock(const CBlock &block, BlockValidationState &state,
4301 const Consensus::Params &params,
4302 BlockValidationOptions validationOptions) {
4303 // These are checks that are independent of context.
4304 if (block.fChecked) {
4305 return true;
4306 }
4307
4308 // Check that the header is valid (particularly PoW). This is mostly
4309 // redundant with the call in AcceptBlockHeader.
4310 if (!CheckBlockHeader(block, state, params, validationOptions)) {
4311 return false;
4312 }
4313
4314 // Check the merkle root.
4315 if (validationOptions.shouldValidateMerkleRoot() &&
4316 !CheckMerkleRoot(block, state)) {
4317 return false;
4318 }
4319
4320 // All potential-corruption validation must be done before we do any
4321 // transaction validation, as otherwise we may mark the header as invalid
4322 // because we receive the wrong transactions for it.
4323
4324 // First transaction must be coinbase.
4325 if (block.vtx.empty()) {
4327 "bad-cb-missing", "first tx is not coinbase");
4328 }
4329
4330 // Size limits.
4331 auto nMaxBlockSize = validationOptions.getExcessiveBlockSize();
4332
4333 // Bail early if there is no way this block is of reasonable size.
4334 if ((block.vtx.size() * MIN_TRANSACTION_SIZE) > nMaxBlockSize) {
4336 "bad-blk-length", "size limits failed");
4337 }
4338
4339 auto currentBlockSize = ::GetSerializeSize(block, PROTOCOL_VERSION);
4340 if (currentBlockSize > nMaxBlockSize) {
4342 "bad-blk-length", "size limits failed");
4343 }
4344
4345 // And a valid coinbase.
4346 TxValidationState tx_state;
4347 if (!CheckCoinbase(*block.vtx[0], tx_state)) {
4349 tx_state.GetRejectReason(),
4350 strprintf("Coinbase check failed (txid %s) %s",
4351 block.vtx[0]->GetId().ToString(),
4352 tx_state.GetDebugMessage()));
4353 }
4354
4355 // Check transactions for regularity, skipping the first. Note that this
4356 // is the first time we check that all after the first are !IsCoinBase.
4357 for (size_t i = 1; i < block.vtx.size(); i++) {
4358 auto *tx = block.vtx[i].get();
4359 if (!CheckRegularTransaction(*tx, tx_state)) {
4360 return state.Invalid(
4362 tx_state.GetRejectReason(),
4363 strprintf("Transaction check failed (txid %s) %s",
4364 tx->GetId().ToString(), tx_state.GetDebugMessage()));
4365 }
4366 }
4367
4368 if (validationOptions.shouldValidatePoW() &&
4369 validationOptions.shouldValidateMerkleRoot()) {
4370 block.fChecked = true;
4371 }
4372
4373 return true;
4374}
4375
4376bool HasValidProofOfWork(const std::vector<CBlockHeader> &headers,
4377 const Consensus::Params &consensusParams) {
4378 return std::all_of(headers.cbegin(), headers.cend(),
4379 [&](const auto &header) {
4380 return CheckProofOfWork(
4381 header.GetHash(), header.nBits, consensusParams);
4382 });
4383}
4384
4385bool IsBlockMutated(const CBlock &block) {
4387 if (!CheckMerkleRoot(block, state)) {
4389 "Block mutated: %s\n", state.ToString());
4390 return true;
4391 }
4392
4393 if (block.vtx.empty() || !block.vtx[0]->IsCoinBase()) {
4394 // Consider the block mutated if any transaction is 64 bytes in size
4395 // (see 3.1 in "Weaknesses in Bitcoin’s Merkle Root Construction":
4396 // https://lists.linuxfoundation.org/pipermail/bitcoin-dev/attachments/20190225/a27d8837/attachment-0001.pdf).
4397 //
4398 // Note: This is not a consensus change as this only applies to blocks
4399 // that don't have a coinbase transaction and would therefore already be
4400 // invalid.
4401 return std::any_of(block.vtx.begin(), block.vtx.end(), [](auto &tx) {
4402 return GetSerializeSize(tx, PROTOCOL_VERSION) == 64;
4403 });
4404 } else {
4405 // Theoretically it is still possible for a block with a 64 byte
4406 // coinbase transaction to be mutated but we neglect that possibility
4407 // here as it requires at least 224 bits of work.
4408 }
4409
4410 return false;
4411}
4412
4413arith_uint256 CalculateHeadersWork(const std::vector<CBlockHeader> &headers) {
4414 arith_uint256 total_work{0};
4415 for (const CBlockHeader &header : headers) {
4416 CBlockIndex dummy(header);
4417 total_work += GetBlockProof(dummy);
4418 }
4419 return total_work;
4420}
4421
4433 const CBlockHeader &block, BlockValidationState &state,
4434 BlockManager &blockman, ChainstateManager &chainman,
4435 const CBlockIndex *pindexPrev, NodeClock::time_point now,
4436 const std::optional<CCheckpointData> &test_checkpoints = std::nullopt)
4439 assert(pindexPrev != nullptr);
4440 const int nHeight = pindexPrev->nHeight + 1;
4441
4442 const CChainParams &params = chainman.GetParams();
4443
4444 // Check proof of work
4445 if (block.nBits != GetNextWorkRequired(pindexPrev, &block, params)) {
4446 LogPrintf("bad bits after height: %d\n", pindexPrev->nHeight);
4448 "bad-diffbits", "incorrect proof of work");
4449 }
4450
4451 // Check against checkpoints
4452 if (chainman.m_options.checkpoints_enabled) {
4453 const CCheckpointData &checkpoints =
4454 test_checkpoints ? test_checkpoints.value() : params.Checkpoints();
4455
4456 // Check that the block chain matches the known block chain up to a
4457 // checkpoint.
4458 if (!Checkpoints::CheckBlock(checkpoints, nHeight, block.GetHash())) {
4460 "ERROR: %s: rejected by checkpoint lock-in at %d\n",
4461 __func__, nHeight);
4463 "checkpoint mismatch");
4464 }
4465
4466 // Don't accept any forks from the main chain prior to last checkpoint.
4467 // GetLastCheckpoint finds the last checkpoint in MapCheckpoints that's
4468 // in our BlockIndex().
4469
4470 const CBlockIndex *pcheckpoint =
4471 blockman.GetLastCheckpoint(checkpoints);
4472 if (pcheckpoint && nHeight < pcheckpoint->nHeight) {
4474 "ERROR: %s: forked chain older than last checkpoint "
4475 "(height %d)\n",
4476 __func__, nHeight);
4478 "bad-fork-prior-to-checkpoint");
4479 }
4480 }
4481
4482 // Check timestamp against prev
4483 if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast()) {
4485 "time-too-old", "block's timestamp is too early");
4486 }
4487
4488 // Check timestamp
4489 if (block.Time() > now + std::chrono::seconds{MAX_FUTURE_BLOCK_TIME}) {
4491 "time-too-new",
4492 "block timestamp too far in the future");
4493 }
4494
4495 // Reject blocks with outdated version
4496 if ((block.nVersion < 2 &&
4497 DeploymentActiveAfter(pindexPrev, chainman,
4499 (block.nVersion < 3 &&
4500 DeploymentActiveAfter(pindexPrev, chainman,
4502 (block.nVersion < 4 &&
4503 DeploymentActiveAfter(pindexPrev, chainman,
4505 return state.Invalid(
4507 strprintf("bad-version(0x%08x)", block.nVersion),
4508 strprintf("rejected nVersion=0x%08x block", block.nVersion));
4509 }
4510
4511 return true;
4512}
4513
4515 const CBlockIndex &active_chain_tip, const Consensus::Params &params,
4516 const CTransaction &tx, TxValidationState &state) {
4518
4519 // ContextualCheckTransactionForCurrentBlock() uses
4520 // active_chain_tip.Height()+1 to evaluate nLockTime because when
4521 // IsFinalTx() is called within AcceptBlock(), the height of the
4522 // block *being* evaluated is what is used. Thus if we want to know if a
4523 // transaction can be part of the *next* block, we need to call
4524 // ContextualCheckTransaction() with one more than
4525 // active_chain_tip.Height().
4526 const int nBlockHeight = active_chain_tip.nHeight + 1;
4527
4528 // BIP113 will require that time-locked transactions have nLockTime set to
4529 // less than the median time of the previous block they're contained in.
4530 // When the next block is created its previous block will be the current
4531 // chain tip, so we use that to calculate the median time passed to
4532 // ContextualCheckTransaction().
4533 // This time can also be used for consensus upgrades.
4534 const int64_t nMedianTimePast{active_chain_tip.GetMedianTimePast()};
4535
4536 return ContextualCheckTransaction(params, tx, state, nBlockHeight,
4537 nMedianTimePast);
4538}
4539
4547static bool ContextualCheckBlock(const CBlock &block,
4548 BlockValidationState &state,
4549 const ChainstateManager &chainman,
4550 const CBlockIndex *pindexPrev) {
4551 const int nHeight = pindexPrev == nullptr ? 0 : pindexPrev->nHeight + 1;
4552
4553 // Enforce BIP113 (Median Time Past).
4554 bool enforce_locktime_median_time_past{false};
4555 if (DeploymentActiveAfter(pindexPrev, chainman,
4557 assert(pindexPrev != nullptr);
4558 enforce_locktime_median_time_past = true;
4559 }
4560
4561 const int64_t nMedianTimePast =
4562 pindexPrev == nullptr ? 0 : pindexPrev->GetMedianTimePast();
4563
4564 const int64_t nLockTimeCutoff{enforce_locktime_median_time_past
4565 ? nMedianTimePast
4566 : block.GetBlockTime()};
4567
4568 const Consensus::Params params = chainman.GetConsensus();
4569 const bool fIsMagneticAnomalyEnabled =
4570 IsMagneticAnomalyEnabled(params, pindexPrev);
4571
4572 // Check transactions:
4573 // - canonical ordering
4574 // - ensure they are finalized
4575 // - check they have the minimum size
4576 const CTransaction *prevTx = nullptr;
4577 for (const auto &ptx : block.vtx) {
4578 const CTransaction &tx = *ptx;
4579 if (fIsMagneticAnomalyEnabled) {
4580 if (prevTx && (tx.GetId() <= prevTx->GetId())) {
4581 if (tx.GetId() == prevTx->GetId()) {
4583 "tx-duplicate",
4584 strprintf("Duplicated transaction %s",
4585 tx.GetId().ToString()));
4586 }
4587
4588 return state.Invalid(
4590 strprintf("Transaction order is invalid (%s < %s)",
4591 tx.GetId().ToString(),
4592 prevTx->GetId().ToString()));
4593 }
4594
4595 if (prevTx || !tx.IsCoinBase()) {
4596 prevTx = &tx;
4597 }
4598 }
4599
4600 TxValidationState tx_state;
4601 if (!ContextualCheckTransaction(params, tx, tx_state, nHeight,
4602 nLockTimeCutoff)) {
4604 tx_state.GetRejectReason(),
4605 tx_state.GetDebugMessage());
4606 }
4607 }
4608
4609 // Enforce rule that the coinbase starts with serialized block height
4610 if (DeploymentActiveAfter(pindexPrev, chainman,
4612 CScript expect = CScript() << nHeight;
4613 if (block.vtx[0]->vin[0].scriptSig.size() < expect.size() ||
4614 !std::equal(expect.begin(), expect.end(),
4615 block.vtx[0]->vin[0].scriptSig.begin())) {
4617 "bad-cb-height",
4618 "block height mismatch in coinbase");
4619 }
4620 }
4621
4622 return true;
4623}
4624
4631 const CBlockHeader &block, BlockValidationState &state,
4632 CBlockIndex **ppindex, bool min_pow_checked,
4633 const std::optional<CCheckpointData> &test_checkpoints) {
4635 const Config &config = this->GetConfig();
4636 const CChainParams &chainparams = config.GetChainParams();
4637
4638 // Check for duplicate
4639 BlockHash hash = block.GetHash();
4640 BlockMap::iterator miSelf{m_blockman.m_block_index.find(hash)};
4641 if (hash != chainparams.GetConsensus().hashGenesisBlock) {
4642 if (miSelf != m_blockman.m_block_index.end()) {
4643 // Block header is already known.
4644 CBlockIndex *pindex = &(miSelf->second);
4645 if (ppindex) {
4646 *ppindex = pindex;
4647 }
4648
4649 if (pindex->nStatus.isInvalid()) {
4650 LogPrint(BCLog::VALIDATION, "%s: block %s is marked invalid\n",
4651 __func__, hash.ToString());
4652 return state.Invalid(
4654 }
4655
4656 return true;
4657 }
4658
4659 if (!CheckBlockHeader(block, state, chainparams.GetConsensus(),
4660 BlockValidationOptions(config))) {
4662 "%s: Consensus::CheckBlockHeader: %s, %s\n", __func__,
4663 hash.ToString(), state.ToString());
4664 return false;
4665 }
4666
4667 // Get prev block index
4668 BlockMap::iterator mi{
4669 m_blockman.m_block_index.find(block.hashPrevBlock)};
4670 if (mi == m_blockman.m_block_index.end()) {
4672 "header %s has prev block not found: %s\n",
4673 hash.ToString(), block.hashPrevBlock.ToString());
4675 "prev-blk-not-found");
4676 }
4677
4678 CBlockIndex *pindexPrev = &((*mi).second);
4679 assert(pindexPrev);
4680 if (pindexPrev->nStatus.isInvalid()) {
4682 "header %s has prev block invalid: %s\n", hash.ToString(),
4683 block.hashPrevBlock.ToString());
4685 "bad-prevblk");
4686 }
4687
4689 block, state, m_blockman, *this, pindexPrev,
4690 m_options.adjusted_time_callback(), test_checkpoints)) {
4692 "%s: Consensus::ContextualCheckBlockHeader: %s, %s\n",
4693 __func__, hash.ToString(), state.ToString());
4694 return false;
4695 }
4696
4697 /* Determine if this block descends from any block which has been found
4698 * invalid (m_failed_blocks), then mark pindexPrev and any blocks
4699 * between them as failed. For example:
4700 *
4701 * D3
4702 * /
4703 * B2 - C2
4704 * / \
4705 * A D2 - E2 - F2
4706 * \
4707 * B1 - C1 - D1 - E1
4708 *
4709 * In the case that we attempted to reorg from E1 to F2, only to find
4710 * C2 to be invalid, we would mark D2, E2, and F2 as BLOCK_FAILED_CHILD
4711 * but NOT D3 (it was not in any of our candidate sets at the time).
4712 *
4713 * In any case D3 will also be marked as BLOCK_FAILED_CHILD at restart
4714 * in LoadBlockIndex.
4715 */
4716 if (!pindexPrev->IsValid(BlockValidity::SCRIPTS)) {
4717 // The above does not mean "invalid": it checks if the previous
4718 // block hasn't been validated up to BlockValidity::SCRIPTS. This is
4719 // a performance optimization, in the common case of adding a new
4720 // block to the tip, we don't need to iterate over the failed blocks
4721 // list.
4722 for (const CBlockIndex *failedit : m_failed_blocks) {
4723 if (pindexPrev->GetAncestor(failedit->nHeight) == failedit) {
4724 assert(failedit->nStatus.hasFailed());
4725 CBlockIndex *invalid_walk = pindexPrev;
4726 while (invalid_walk != failedit) {
4727 invalid_walk->nStatus =
4728 invalid_walk->nStatus.withFailedParent();
4729 m_blockman.m_dirty_blockindex.insert(invalid_walk);
4730 invalid_walk = invalid_walk->pprev;
4731 }
4733 "header %s has prev block invalid: %s\n",
4734 hash.ToString(), block.hashPrevBlock.ToString());
4735 return state.Invalid(
4737 "bad-prevblk");
4738 }
4739 }
4740 }
4741 }
4742 if (!min_pow_checked) {
4744 "%s: not adding new block header %s, missing anti-dos "
4745 "proof-of-work validation\n",
4746 __func__, hash.ToString());
4748 "too-little-chainwork");
4749 }
4750 CBlockIndex *pindex{m_blockman.AddToBlockIndex(block, m_best_header)};
4751
4752 if (ppindex) {
4753 *ppindex = pindex;
4754 }
4755
4756 // Since this is the earliest point at which we have determined that a
4757 // header is both new and valid, log here.
4758 //
4759 // These messages are valuable for detecting potential selfish mining
4760 // behavior; if multiple displacing headers are seen near simultaneously
4761 // across many nodes in the network, this might be an indication of selfish
4762 // mining. Having this log by default when not in IBD ensures broad
4763 // availability of this data in case investigation is merited.
4764 const auto msg = strprintf("Saw new header hash=%s height=%d",
4765 hash.ToString(), pindex->nHeight);
4766
4767 if (IsInitialBlockDownload()) {
4769 } else {
4770 LogPrintf("%s\n", msg);
4771 }
4772
4773 return true;
4774}
4775
4776// Exposed wrapper for AcceptBlockHeader
4778 const std::vector<CBlockHeader> &headers, bool min_pow_checked,
4779 BlockValidationState &state, const CBlockIndex **ppindex,
4780 const std::optional<CCheckpointData> &test_checkpoints) {
4782 {
4783 LOCK(cs_main);
4784 for (const CBlockHeader &header : headers) {
4785 // Use a temp pindex instead of ppindex to avoid a const_cast
4786 CBlockIndex *pindex = nullptr;
4787 bool accepted = AcceptBlockHeader(
4788 header, state, &pindex, min_pow_checked, test_checkpoints);
4790
4791 if (!accepted) {
4792 return false;
4793 }
4794
4795 if (ppindex) {
4796 *ppindex = pindex;
4797 }
4798 }
4799 }
4800
4801 if (NotifyHeaderTip(*this)) {
4802 if (IsInitialBlockDownload() && ppindex && *ppindex) {
4803 const CBlockIndex &last_accepted{**ppindex};
4804 const int64_t blocks_left{
4805 (GetTime() - last_accepted.GetBlockTime()) /
4807 const double progress{100.0 * last_accepted.nHeight /
4808 (last_accepted.nHeight + blocks_left)};
4809 LogPrintf("Synchronizing blockheaders, height: %d (~%.2f%%)\n",
4810 last_accepted.nHeight, progress);
4811 }
4812 }
4813 return true;
4814}
4815
4817 int64_t height,
4818 int64_t timestamp) {
4820 {
4821 LOCK(cs_main);
4822 // Don't report headers presync progress if we already have a
4823 // post-minchainwork header chain.
4824 // This means we lose reporting for potentially legimate, but unlikely,
4825 // deep reorgs, but prevent attackers that spam low-work headers from
4826 // filling our logs.
4827 if (m_best_header->nChainWork >=
4828 UintToArith256(GetConsensus().nMinimumChainWork)) {
4829 return;
4830 }
4831 // Rate limit headers presync updates to 4 per second, as these are not
4832 // subject to DoS protection.
4833 auto now = Now<SteadyMilliseconds>();
4834 if (now < m_last_presync_update + 250ms) {
4835 return;
4836 }
4837 m_last_presync_update = now;
4838 }
4839 bool initial_download = IsInitialBlockDownload();
4841 height, timestamp, /*presync=*/true);
4842 if (initial_download) {
4843 const int64_t blocks_left{(GetTime() - timestamp) /
4845 const double progress{100.0 * height / (height + blocks_left)};
4846 LogPrintf("Pre-synchronizing blockheaders, height: %d (~%.2f%%)\n",
4847 height, progress);
4848 }
4849}
4850
4851bool ChainstateManager::AcceptBlock(const std::shared_ptr<const CBlock> &pblock,
4852 BlockValidationState &state,
4853 bool fRequested, const FlatFilePos *dbp,
4854 bool *fNewBlock, bool min_pow_checked) {
4856
4857 const CBlock &block = *pblock;
4858 if (fNewBlock) {
4859 *fNewBlock = false;
4860 }
4861
4862 CBlockIndex *pindex = nullptr;
4863
4864 bool accepted_header{
4865 AcceptBlockHeader(block, state, &pindex, min_pow_checked)};
4867
4868 if (!accepted_header) {
4869 return false;
4870 }
4871
4872 // Check all requested blocks that we do not already have for validity and
4873 // save them to disk. Skip processing of unrequested blocks as an anti-DoS
4874 // measure, unless the blocks have more work than the active chain tip, and
4875 // aren't too far ahead of it, so are likely to be attached soon.
4876 bool fAlreadyHave = pindex->nStatus.hasData();
4877
4878 // TODO: deal better with return value and error conditions for duplicate
4879 // and unrequested blocks.
4880 if (fAlreadyHave) {
4881 return true;
4882 }
4883
4884 // Compare block header timestamps and received times of the block and the
4885 // chaintip. If they have the same chain height, use these diffs as a
4886 // tie-breaker, attempting to pick the more honestly-mined block.
4887 int64_t newBlockTimeDiff = std::llabs(pindex->GetReceivedTimeDiff());
4888 int64_t chainTipTimeDiff =
4889 ActiveTip() ? std::llabs(ActiveTip()->GetReceivedTimeDiff()) : 0;
4890
4891 bool isSameHeight =
4892 ActiveTip() && (pindex->nChainWork == ActiveTip()->nChainWork);
4893 if (isSameHeight) {
4894 LogPrintf("Chain tip timestamp-to-received-time difference: hash=%s, "
4895 "diff=%d\n",
4896 ActiveTip()->GetBlockHash().ToString(), chainTipTimeDiff);
4897 LogPrintf("New block timestamp-to-received-time difference: hash=%s, "
4898 "diff=%d\n",
4899 pindex->GetBlockHash().ToString(), newBlockTimeDiff);
4900 }
4901
4902 bool fHasMoreOrSameWork =
4903 (ActiveTip() ? pindex->nChainWork >= ActiveTip()->nChainWork : true);
4904
4905 // Blocks that are too out-of-order needlessly limit the effectiveness of
4906 // pruning, because pruning will not delete block files that contain any
4907 // blocks which are too close in height to the tip. Apply this test
4908 // regardless of whether pruning is enabled; it should generally be safe to
4909 // not process unrequested blocks.
4910 bool fTooFarAhead{pindex->nHeight >
4912
4913 // TODO: Decouple this function from the block download logic by removing
4914 // fRequested
4915 // This requires some new chain data structure to efficiently look up if a
4916 // block is in a chain leading to a candidate for best tip, despite not
4917 // being such a candidate itself.
4918 // Note that this would break the getblockfrompeer RPC
4919
4920 // If we didn't ask for it:
4921 if (!fRequested) {
4922 // This is a previously-processed block that was pruned.
4923 if (pindex->nTx != 0) {
4924 return true;
4925 }
4926
4927 // Don't process less-work chains.
4928 if (!fHasMoreOrSameWork) {
4929 return true;
4930 }
4931
4932 // Block height is too high.
4933 if (fTooFarAhead) {
4934 return true;
4935 }
4936
4937 // Protect against DoS attacks from low-work chains.
4938 // If our tip is behind, a peer could try to send us
4939 // low-work blocks on a fake chain that we would never
4940 // request; don't process these.
4941 if (pindex->nChainWork < MinimumChainWork()) {
4942 return true;
4943 }
4944 }
4945
4946 if (!CheckBlock(block, state,
4949 !ContextualCheckBlock(block, state, *this, pindex->pprev)) {
4950 if (state.IsInvalid() &&
4952 pindex->nStatus = pindex->nStatus.withFailed();
4953 m_blockman.m_dirty_blockindex.insert(pindex);
4954 }
4955
4956 return error("%s: %s (block %s)", __func__, state.ToString(),
4957 block.GetHash().ToString());
4958 }
4959
4960 // If connecting the new block would require rewinding more than one block
4961 // from the active chain (i.e., a "deep reorg"), then mark the new block as
4962 // parked. If it has enough work then it will be automatically unparked
4963 // later, during FindMostWorkChain. We mark the block as parked at the very
4964 // last minute so we can make sure everything is ready to be reorged if
4965 // needed.
4966 if (gArgs.GetBoolArg("-parkdeepreorg", true)) {
4967 // Blocks that are below the snapshot height can't cause reorgs, as the
4968 // active tip is at least thousands of blocks higher. Don't park them,
4969 // they will most likely connect on the tip of the background chain.
4970 std::optional<int> snapshot_base_height = GetSnapshotBaseHeight();
4971 const bool is_background_block =
4972 snapshot_base_height && BackgroundSyncInProgress() &&
4973 pindex->nHeight <= snapshot_base_height;
4974 const CBlockIndex *pindexFork = ActiveChain().FindFork(pindex);
4975 if (!is_background_block && pindexFork &&
4976 pindexFork->nHeight + 1 < ActiveHeight()) {
4977 LogPrintf("Park block %s as it would cause a deep reorg.\n",
4978 pindex->GetBlockHash().ToString());
4979 pindex->nStatus = pindex->nStatus.withParked();
4980 m_blockman.m_dirty_blockindex.insert(pindex);
4981 }
4982 }
4983
4984 // Header is valid/has work and the merkle tree is good.
4985 // Relay now, but if it does not build on our best tip, let the
4986 // SendMessages loop relay it.
4987 if (!IsInitialBlockDownload() && ActiveTip() == pindex->pprev) {
4988 GetMainSignals().NewPoWValidBlock(pindex, pblock);
4989 }
4990
4991 // Write block to history file
4992 if (fNewBlock) {
4993 *fNewBlock = true;
4994 }
4995 try {
4996 FlatFilePos blockPos{
4997 m_blockman.SaveBlockToDisk(block, pindex->nHeight, dbp)};
4998 if (blockPos.IsNull()) {
4999 state.Error(strprintf(
5000 "%s: Failed to find position to write new block to disk",
5001 __func__));
5002 return false;
5003 }
5004 ReceivedBlockTransactions(block, pindex, blockPos);
5005 } catch (const std::runtime_error &e) {
5006 return AbortNode(state, std::string("System error: ") + e.what());
5007 }
5008
5009 // TODO: FlushStateToDisk() handles flushing of both block and chainstate
5010 // data, so we should move this to ChainstateManager so that we can be more
5011 // intelligent about how we flush.
5012 // For now, since FlushStateMode::NONE is used, all that can happen is that
5013 // the block files may be pruned, so we can just call this on one
5014 // chainstate (particularly if we haven't implemented pruning with
5015 // background validation yet).
5016 ActiveChainstate().FlushStateToDisk(state, FlushStateMode::NONE);
5017
5019
5020 return true;
5021}
5022
5024 const std::shared_ptr<const CBlock> &block, bool force_processing,
5025 bool min_pow_checked, bool *new_block,
5028
5029 {
5030 if (new_block) {
5031 *new_block = false;
5032 }
5033
5035
5036 // CheckBlock() does not support multi-threaded block validation
5037 // because CBlock::fChecked can cause data race.
5038 // Therefore, the following critical section must include the
5039 // CheckBlock() call as well.
5040 LOCK(cs_main);
5041
5042 // Skipping AcceptBlock() for CheckBlock() failures means that we will
5043 // never mark a block as invalid if CheckBlock() fails. This is
5044 // protective against consensus failure if there are any unknown form
5045 // s of block malleability that cause CheckBlock() to fail; see e.g.
5046 // CVE-2012-2459 and
5047 // https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2019-February/016697.html.
5048 // Because CheckBlock() is not very expensive, the anti-DoS benefits of
5049 // caching failure (of a definitely-invalid block) are not substantial.
5050 bool ret = CheckBlock(*block, state, this->GetConsensus(),
5052 if (ret) {
5053 // Store to disk
5054 ret = AcceptBlock(block, state, force_processing, nullptr,
5055 new_block, min_pow_checked);
5056 }
5057
5058 if (!ret) {
5059 GetMainSignals().BlockChecked(*block, state);
5060 return error("%s: AcceptBlock FAILED (%s)", __func__,
5061 state.ToString());
5062 }
5063 }
5064
5065 NotifyHeaderTip(*this);
5066
5067 // Only used to report errors, not invalidity - ignore it
5069 if (!ActiveChainstate().ActivateBestChain(state, block, avalanche)) {
5070 return error("%s: ActivateBestChain failed (%s)", __func__,
5071 state.ToString());
5072 }
5073
5075 ? m_ibd_chainstate.get()
5076 : nullptr)};
5077 BlockValidationState bg_state;
5078 if (bg_chain && !bg_chain->ActivateBestChain(bg_state, block)) {
5079 return error("%s: [background] ActivateBestChain failed (%s)", __func__,
5080 bg_state.ToString());
5081 }
5082
5083 return true;
5084}
5085
5088 bool test_accept) {
5090 Chainstate &active_chainstate = ActiveChainstate();
5091 if (!active_chainstate.GetMempool()) {
5092 TxValidationState state;
5093 state.Invalid(TxValidationResult::TX_NO_MEMPOOL, "no-mempool");
5094 return MempoolAcceptResult::Failure(state);
5095 }
5096 auto result = AcceptToMemoryPool(active_chainstate, tx, GetTime(),
5097 /*bypass_limits=*/false, test_accept);
5098 active_chainstate.GetMempool()->check(
5099 active_chainstate.CoinsTip(), active_chainstate.m_chain.Height() + 1);
5100 return result;
5101}
5102
5104 BlockValidationState &state, const CChainParams &params,
5105 Chainstate &chainstate, const CBlock &block, CBlockIndex *pindexPrev,
5106 const std::function<NodeClock::time_point()> &adjusted_time_callback,
5107 BlockValidationOptions validationOptions) {
5109 assert(pindexPrev && pindexPrev == chainstate.m_chain.Tip());
5110 CCoinsViewCache viewNew(&chainstate.CoinsTip());
5111 BlockHash block_hash(block.GetHash());
5112 CBlockIndex indexDummy(block);
5113 indexDummy.pprev = pindexPrev;
5114 indexDummy.nHeight = pindexPrev->nHeight + 1;
5115 indexDummy.phashBlock = &block_hash;
5116
5117 // NOTE: CheckBlockHeader is called by CheckBlock
5118 if (!ContextualCheckBlockHeader(block, state, chainstate.m_blockman,
5119 chainstate.m_chainman, pindexPrev,
5120 adjusted_time_callback())) {
5121 return error("%s: Consensus::ContextualCheckBlockHeader: %s", __func__,
5122 state.ToString());
5123 }
5124
5125 if (!CheckBlock(block, state, params.GetConsensus(), validationOptions)) {
5126 return error("%s: Consensus::CheckBlock: %s", __func__,
5127 state.ToString());
5128 }
5129
5130 if (!ContextualCheckBlock(block, state, chainstate.m_chainman,
5131 pindexPrev)) {
5132 return error("%s: Consensus::ContextualCheckBlock: %s", __func__,
5133 state.ToString());
5134 }
5135
5136 if (!chainstate.ConnectBlock(block, state, &indexDummy, viewNew,
5137 validationOptions, nullptr, true)) {
5138 return false;
5139 }
5140
5141 assert(state.IsValid());
5142 return true;
5143}
5144
5145/* This function is called from the RPC code for pruneblockchain */
5146void PruneBlockFilesManual(Chainstate &active_chainstate,
5147 int nManualPruneHeight) {
5149 if (active_chainstate.FlushStateToDisk(state, FlushStateMode::NONE,
5150 nManualPruneHeight)) {
5151 LogPrintf("%s: failed to flush state (%s)\n", __func__,
5152 state.ToString());
5153 }
5154}
5155
5156void Chainstate::LoadMempool(const fs::path &load_path,
5157 FopenFn mockable_fopen_function) {
5158 if (!m_mempool) {
5159 return;
5160 }
5161 ::LoadMempool(*m_mempool, load_path, *this, mockable_fopen_function);
5163}
5164
5167 const CCoinsViewCache &coins_cache = CoinsTip();
5168 // Never called when the coins view is empty
5169 assert(!coins_cache.GetBestBlock().IsNull());
5170 const CBlockIndex *tip = m_chain.Tip();
5171
5172 if (tip && tip->GetBlockHash() == coins_cache.GetBestBlock()) {
5173 return true;
5174 }
5175
5176 // Load pointer to end of best chain
5177 CBlockIndex *pindex =
5179 if (!pindex) {
5180 return false;
5181 }
5182 m_chain.SetTip(*pindex);
5184
5185 tip = m_chain.Tip();
5186 LogPrintf(
5187 "Loaded best chain: hashBestChain=%s height=%d date=%s progress=%f\n",
5188 tip->GetBlockHash().ToString(), m_chain.Height(),
5191 return true;
5192}
5193
5195 : m_notifications{notifications} {
5196 m_notifications.progress(_("Verifying blocks…"), 0, false);
5197}
5198
5200 m_notifications.progress(bilingual_str{}, 100, false);
5201}
5202
5204 CCoinsView &coinsview, int nCheckLevel,
5205 int nCheckDepth) {
5207
5208 const Config &config = chainstate.m_chainman.GetConfig();
5209 const CChainParams &params = config.GetChainParams();
5210 const Consensus::Params &consensusParams = params.GetConsensus();
5211
5212 if (chainstate.m_chain.Tip() == nullptr ||
5213 chainstate.m_chain.Tip()->pprev == nullptr) {
5215 }
5216
5217 // Verify blocks in the best chain
5218 if (nCheckDepth <= 0 || nCheckDepth > chainstate.m_chain.Height()) {
5219 nCheckDepth = chainstate.m_chain.Height();
5220 }
5221
5222 nCheckLevel = std::max(0, std::min(4, nCheckLevel));
5223 LogPrintf("Verifying last %i blocks at level %i\n", nCheckDepth,
5224 nCheckLevel);
5225
5226 CCoinsViewCache coins(&coinsview);
5227 CBlockIndex *pindex;
5228 CBlockIndex *pindexFailure = nullptr;
5229 int nGoodTransactions = 0;
5231 int reportDone = 0;
5232 bool skipped_no_block_data{false};
5233 bool skipped_l3_checks{false};
5234 LogPrintf("Verification progress: 0%%\n");
5235
5236 const bool is_snapshot_cs{chainstate.m_from_snapshot_blockhash};
5237
5238 for (pindex = chainstate.m_chain.Tip(); pindex && pindex->pprev;
5239 pindex = pindex->pprev) {
5240 const int percentageDone = std::max(
5241 1, std::min(99, (int)(((double)(chainstate.m_chain.Height() -
5242 pindex->nHeight)) /
5243 (double)nCheckDepth *
5244 (nCheckLevel >= 4 ? 50 : 100))));
5245 if (reportDone < percentageDone / 10) {
5246 // report every 10% step
5247 LogPrintf("Verification progress: %d%%\n", percentageDone);
5248 reportDone = percentageDone / 10;
5249 }
5250
5251 m_notifications.progress(_("Verifying blocks…"), percentageDone, false);
5252 if (pindex->nHeight <= chainstate.m_chain.Height() - nCheckDepth) {
5253 break;
5254 }
5255
5256 if ((chainstate.m_blockman.IsPruneMode() || is_snapshot_cs) &&
5257 !pindex->nStatus.hasData()) {
5258 // If pruning or running under an assumeutxo snapshot, only go
5259 // back as far as we have data.
5260 LogPrintf("VerifyDB(): block verification stopping at height %d "
5261 "(no data). This could be due to pruning or use of an "
5262 "assumeutxo snapshot.\n",
5263 pindex->nHeight);
5264 skipped_no_block_data = true;
5265 break;
5266 }
5267
5268 CBlock block;
5269
5270 // check level 0: read from disk
5271 if (!chainstate.m_blockman.ReadBlockFromDisk(block, *pindex)) {
5272 LogPrintf(
5273 "Verification error: ReadBlockFromDisk failed at %d, hash=%s\n",
5274 pindex->nHeight, pindex->GetBlockHash().ToString());
5276 }
5277
5278 // check level 1: verify block validity
5279 if (nCheckLevel >= 1 && !CheckBlock(block, state, consensusParams,
5280 BlockValidationOptions(config))) {
5281 LogPrintf(
5282 "Verification error: found bad block at %d, hash=%s (%s)\n",
5283 pindex->nHeight, pindex->GetBlockHash().ToString(),
5284 state.ToString());
5286 }
5287
5288 // check level 2: verify undo validity
5289 if (nCheckLevel >= 2 && pindex) {
5290 CBlockUndo undo;
5291 if (!pindex->GetUndoPos().IsNull()) {
5292 if (!chainstate.m_blockman.UndoReadFromDisk(undo, *pindex)) {
5293 LogPrintf("Verification error: found bad undo data at %d, "
5294 "hash=%s\n",
5295 pindex->nHeight,
5296 pindex->GetBlockHash().ToString());
5298 }
5299 }
5300 }
5301 // check level 3: check for inconsistencies during memory-only
5302 // disconnect of tip blocks
5303 size_t curr_coins_usage = coins.DynamicMemoryUsage() +
5304 chainstate.CoinsTip().DynamicMemoryUsage();
5305
5306 if (nCheckLevel >= 3) {
5307 if (curr_coins_usage <= chainstate.m_coinstip_cache_size_bytes) {
5308 assert(coins.GetBestBlock() == pindex->GetBlockHash());
5309 DisconnectResult res =
5310 chainstate.DisconnectBlock(block, pindex, coins);
5311 if (res == DisconnectResult::FAILED) {
5312 LogPrintf("Verification error: irrecoverable inconsistency "
5313 "in block data at %d, hash=%s\n",
5314 pindex->nHeight,
5315 pindex->GetBlockHash().ToString());
5317 }
5318 if (res == DisconnectResult::UNCLEAN) {
5319 nGoodTransactions = 0;
5320 pindexFailure = pindex;
5321 } else {
5322 nGoodTransactions += block.vtx.size();
5323 }
5324 } else {
5325 skipped_l3_checks = true;
5326 }
5327 }
5328
5329 if (ShutdownRequested()) {
5331 }
5332 }
5333
5334 if (pindexFailure) {
5335 LogPrintf("Verification error: coin database inconsistencies found "
5336 "(last %i blocks, %i good transactions before that)\n",
5337 chainstate.m_chain.Height() - pindexFailure->nHeight + 1,
5338 nGoodTransactions);
5340 }
5341 if (skipped_l3_checks) {
5342 LogPrintf("Skipped verification of level >=3 (insufficient database "
5343 "cache size). Consider increasing -dbcache.\n");
5344 }
5345
5346 // store block count as we move pindex at check level >= 4
5347 int block_count = chainstate.m_chain.Height() - pindex->nHeight;
5348
5349 // check level 4: try reconnecting blocks
5350 if (nCheckLevel >= 4 && !skipped_l3_checks) {
5351 while (pindex != chainstate.m_chain.Tip()) {
5352 const int percentageDone = std::max(
5353 1, std::min(99, 100 - int(double(chainstate.m_chain.Height() -
5354 pindex->nHeight) /
5355 double(nCheckDepth) * 50)));
5356 if (reportDone < percentageDone / 10) {
5357 // report every 10% step
5358 LogPrintf("Verification progress: %d%%\n", percentageDone);
5359 reportDone = percentageDone / 10;
5360 }
5361 m_notifications.progress(_("Verifying blocks…"), percentageDone,
5362 false);
5363 pindex = chainstate.m_chain.Next(pindex);
5364 CBlock block;
5365 if (!chainstate.m_blockman.ReadBlockFromDisk(block, *pindex)) {
5366 LogPrintf("Verification error: ReadBlockFromDisk failed at %d, "
5367 "hash=%s\n",
5368 pindex->nHeight, pindex->GetBlockHash().ToString());
5370 }
5371 if (!chainstate.ConnectBlock(block, state, pindex, coins,
5372 BlockValidationOptions(config))) {
5373 LogPrintf("Verification error: found unconnectable block at "
5374 "%d, hash=%s (%s)\n",
5375 pindex->nHeight, pindex->GetBlockHash().ToString(),
5376 state.ToString());
5378 }
5379 if (ShutdownRequested()) {
5381 }
5382 }
5383 }
5384
5385 LogPrintf("Verification: No coin database inconsistencies in last %i "
5386 "blocks (%i transactions)\n",
5387 block_count, nGoodTransactions);
5388
5389 if (skipped_l3_checks) {
5391 }
5392 if (skipped_no_block_data) {
5394 }
5396}
5397
5403 CCoinsViewCache &view) {
5405 // TODO: merge with ConnectBlock
5406 CBlock block;
5407 if (!m_blockman.ReadBlockFromDisk(block, *pindex)) {
5408 return error("ReplayBlock(): ReadBlockFromDisk failed at %d, hash=%s",
5409 pindex->nHeight, pindex->GetBlockHash().ToString());
5410 }
5411
5412 for (const CTransactionRef &tx : block.vtx) {
5413 // Pass check = true as every addition may be an overwrite.
5414 AddCoins(view, *tx, pindex->nHeight, true);
5415 }
5416
5417 for (const CTransactionRef &tx : block.vtx) {
5418 if (tx->IsCoinBase()) {
5419 continue;
5420 }
5421
5422 for (const CTxIn &txin : tx->vin) {
5423 view.SpendCoin(txin.prevout);
5424 }
5425 }
5426
5427 return true;
5428}
5429
5431 LOCK(cs_main);
5432
5433 CCoinsView &db = this->CoinsDB();
5434 CCoinsViewCache cache(&db);
5435
5436 std::vector<BlockHash> hashHeads = db.GetHeadBlocks();
5437 if (hashHeads.empty()) {
5438 // We're already in a consistent state.
5439 return true;
5440 }
5441 if (hashHeads.size() != 2) {
5442 return error("ReplayBlocks(): unknown inconsistent state");
5443 }
5444
5445 m_chainman.GetNotifications().progress(_("Replaying blocks…"), 0, false);
5446 LogPrintf("Replaying blocks\n");
5447
5448 // Old tip during the interrupted flush.
5449 const CBlockIndex *pindexOld = nullptr;
5450 // New tip during the interrupted flush.
5451 const CBlockIndex *pindexNew;
5452 // Latest block common to both the old and the new tip.
5453 const CBlockIndex *pindexFork = nullptr;
5454
5455 if (m_blockman.m_block_index.count(hashHeads[0]) == 0) {
5456 return error(
5457 "ReplayBlocks(): reorganization to unknown block requested");
5458 }
5459
5460 pindexNew = &(m_blockman.m_block_index[hashHeads[0]]);
5461
5462 if (!hashHeads[1].IsNull()) {
5463 // The old tip is allowed to be 0, indicating it's the first flush.
5464 if (m_blockman.m_block_index.count(hashHeads[1]) == 0) {
5465 return error(
5466 "ReplayBlocks(): reorganization from unknown block requested");
5467 }
5468
5469 pindexOld = &(m_blockman.m_block_index[hashHeads[1]]);
5470 pindexFork = LastCommonAncestor(pindexOld, pindexNew);
5471 assert(pindexFork != nullptr);
5472 }
5473
5474 // Rollback along the old branch.
5475 while (pindexOld != pindexFork) {
5476 if (pindexOld->nHeight > 0) {
5477 // Never disconnect the genesis block.
5478 CBlock block;
5479 if (!m_blockman.ReadBlockFromDisk(block, *pindexOld)) {
5480 return error("RollbackBlock(): ReadBlockFromDisk() failed at "
5481 "%d, hash=%s",
5482 pindexOld->nHeight,
5483 pindexOld->GetBlockHash().ToString());
5484 }
5485
5486 LogPrintf("Rolling back %s (%i)\n",
5487 pindexOld->GetBlockHash().ToString(), pindexOld->nHeight);
5488 DisconnectResult res = DisconnectBlock(block, pindexOld, cache);
5489 if (res == DisconnectResult::FAILED) {
5490 return error(
5491 "RollbackBlock(): DisconnectBlock failed at %d, hash=%s",
5492 pindexOld->nHeight, pindexOld->GetBlockHash().ToString());
5493 }
5494
5495 // If DisconnectResult::UNCLEAN is returned, it means a non-existing
5496 // UTXO was deleted, or an existing UTXO was overwritten. It
5497 // corresponds to cases where the block-to-be-disconnect never had
5498 // all its operations applied to the UTXO set. However, as both
5499 // writing a UTXO and deleting a UTXO are idempotent operations, the
5500 // result is still a version of the UTXO set with the effects of
5501 // that block undone.
5502 }
5503 pindexOld = pindexOld->pprev;
5504 }
5505
5506 // Roll forward from the forking point to the new tip.
5507 int nForkHeight = pindexFork ? pindexFork->nHeight : 0;
5508 for (int nHeight = nForkHeight + 1; nHeight <= pindexNew->nHeight;
5509 ++nHeight) {
5510 const CBlockIndex &pindex{*Assert(pindexNew->GetAncestor(nHeight))};
5511 LogPrintf("Rolling forward %s (%i)\n", pindex.GetBlockHash().ToString(),
5512 nHeight);
5514 _("Replaying blocks…"),
5515 (int)((nHeight - nForkHeight) * 100.0 /
5516 (pindexNew->nHeight - nForkHeight)),
5517 false);
5518 if (!RollforwardBlock(&pindex, cache)) {
5519 return false;
5520 }
5521 }
5522
5523 cache.SetBestBlock(pindexNew->GetBlockHash());
5524 cache.Flush();
5526 return true;
5527}
5528
5529// May NOT be used after any connections are up as much of the peer-processing
5530// logic assumes a consistent block index state
5531void Chainstate::ClearBlockIndexCandidates() {
5533 m_best_fork_tip = nullptr;
5534 m_best_fork_base = nullptr;
5536}
5537
5538bool ChainstateManager::DumpRecentHeadersTime(const fs::path &filePath) const {
5540
5542 return false;
5543 }
5544
5545 // Dump enough headers for RTT computation, with a few extras in case a
5546 // reorg occurs.
5547 const uint64_t numHeaders{20};
5548
5549 try {
5550 const fs::path filePathTmp = filePath + ".new";
5551 FILE *filestr = fsbridge::fopen(filePathTmp, "wb");
5552 if (!filestr) {
5553 return false;
5554 }
5555
5556 CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);
5557 file << HEADERS_TIME_VERSION;
5558 file << numHeaders;
5559
5560 const CBlockIndex *index = ActiveTip();
5561 bool missingIndex{false};
5562 for (uint64_t i = 0; i < numHeaders; i++) {
5563 if (!index) {
5564 LogPrintf("Missing block index, stopping the headers time "
5565 "dumping after %d blocks.\n",
5566 i);
5567 missingIndex = true;
5568 break;
5569 }
5570
5571 file << index->GetBlockHash();
5572 file << index->GetHeaderReceivedTime();
5573
5574 index = index->pprev;
5575 }
5576
5577 if (!FileCommit(file.Get())) {
5578 throw std::runtime_error(strprintf("Failed to commit to file %s",
5579 PathToString(filePathTmp)));
5580 }
5581 file.fclose();
5582
5583 if (missingIndex) {
5584 fs::remove(filePathTmp);
5585 return false;
5586 }
5587
5588 if (!RenameOver(filePathTmp, filePath)) {
5589 throw std::runtime_error(strprintf("Rename failed from %s to %s",
5590 PathToString(filePathTmp),
5591 PathToString(filePath)));
5592 }
5593 } catch (const std::exception &e) {
5594 LogPrintf("Failed to dump the headers time: %s.\n", e.what());
5595 return false;
5596 }
5597
5598 LogPrintf("Successfully dumped the last %d headers time to %s.\n",
5599 numHeaders, PathToString(filePath));
5600
5601 return true;
5602}
5603
5606
5608 return false;
5609 }
5610
5611 FILE *filestr = fsbridge::fopen(filePath, "rb");
5612 CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);
5613 if (file.IsNull()) {
5614 LogPrintf("Failed to open header times from disk, skipping.\n");
5615 return false;
5616 }
5617
5618 try {
5619 uint64_t version;
5620 file >> version;
5621
5622 if (version != HEADERS_TIME_VERSION) {
5623 LogPrintf("Unsupported header times file version, skipping.\n");
5624 return false;
5625 }
5626
5627 uint64_t numBlocks;
5628 file >> numBlocks;
5629
5630 for (uint64_t i = 0; i < numBlocks; i++) {
5631 BlockHash blockHash;
5632 int64_t receiveTime;
5633
5634 file >> blockHash;
5635 file >> receiveTime;
5636
5637 CBlockIndex *index = m_blockman.LookupBlockIndex(blockHash);
5638 if (!index) {
5639 LogPrintf("Missing index for block %s, stopping the headers "
5640 "time loading after %d blocks.\n",
5641 blockHash.ToString(), i);
5642 return false;
5643 }
5644
5645 index->nTimeReceived = receiveTime;
5646 }
5647 } catch (const std::exception &e) {
5648 LogPrintf("Failed to read the headers time file data on disk: %s.\n",
5649 e.what());
5650 return false;
5651 }
5652
5653 return true;
5654}
5655
5658 // Load block index from databases
5659 bool needs_init = fReindex;
5660 if (!fReindex) {
5661 bool ret{m_blockman.LoadBlockIndexDB(SnapshotBlockhash())};
5662 if (!ret) {
5663 return false;
5664 }
5665
5666 m_blockman.ScanAndUnlinkAlreadyPrunedFiles();
5667
5668 std::vector<CBlockIndex *> vSortedByHeight{
5669 m_blockman.GetAllBlockIndices()};
5670 std::sort(vSortedByHeight.begin(), vSortedByHeight.end(),
5672
5673 for (CBlockIndex *pindex : vSortedByHeight) {
5674 if (ShutdownRequested()) {
5675 return false;
5676 }
5677 // If we have an assumeutxo-based chainstate, then the snapshot
5678 // block will be a candidate for the tip, but it may not be
5679 // VALID_TRANSACTIONS (eg if we haven't yet downloaded the block),
5680 // so we special-case the snapshot block as a potential candidate
5681 // here.
5682 if (pindex == GetSnapshotBaseBlock() ||
5684 (pindex->HaveNumChainTxs() || pindex->pprev == nullptr))) {
5685 for (Chainstate *chainstate : GetAll()) {
5686 chainstate->TryAddBlockIndexCandidate(pindex);
5687 }
5688 }
5689
5690 if (pindex->nStatus.isInvalid() &&
5691 (!m_best_invalid ||
5692 pindex->nChainWork > m_best_invalid->nChainWork)) {
5693 m_best_invalid = pindex;
5694 }
5695
5696 if (pindex->nStatus.isOnParkedChain() &&
5697 (!m_best_parked ||
5698 pindex->nChainWork > m_best_parked->nChainWork)) {
5699 m_best_parked = pindex;
5700 }
5701
5702 if (pindex->IsValid(BlockValidity::TREE) &&
5703 (m_best_header == nullptr ||
5704 CBlockIndexWorkComparator()(m_best_header, pindex))) {
5705 m_best_header = pindex;
5706 }
5707 }
5708
5709 needs_init = m_blockman.m_block_index.empty();
5710 }
5711
5712 if (needs_init) {
5713 // Everything here is for *new* reindex/DBs. Thus, though
5714 // LoadBlockIndexDB may have set fReindex if we shut down
5715 // mid-reindex previously, we don't check fReindex and
5716 // instead only check it prior to LoadBlockIndexDB to set
5717 // needs_init.
5718
5719 LogPrintf("Initializing databases...\n");
5720 }
5721 return true;
5722}
5723
5725 LOCK(cs_main);
5726
5727 const CChainParams &params{m_chainman.GetParams()};
5728
5729 // Check whether we're already initialized by checking for genesis in
5730 // m_blockman.m_block_index. Note that we can't use m_chain here, since it
5731 // is set based on the coins db, not the block index db, which is the only
5732 // thing loaded at this point.
5733 if (m_blockman.m_block_index.count(params.GenesisBlock().GetHash())) {
5734 return true;
5735 }
5736
5737 try {
5738 const CBlock &block = params.GenesisBlock();
5739 FlatFilePos blockPos{m_blockman.SaveBlockToDisk(block, 0, nullptr)};
5740 if (blockPos.IsNull()) {
5741 return error("%s: writing genesis block to disk failed", __func__);
5742 }
5743 CBlockIndex *pindex =
5744 m_blockman.AddToBlockIndex(block, m_chainman.m_best_header);
5745 m_chainman.ReceivedBlockTransactions(block, pindex, blockPos);
5746 } catch (const std::runtime_error &e) {
5747 return error("%s: failed to write genesis block: %s", __func__,
5748 e.what());
5749 }
5750
5751 return true;
5752}
5753
5755 FILE *fileIn, FlatFilePos *dbp,
5756 std::multimap<BlockHash, FlatFilePos> *blocks_with_unknown_parent,
5758 // Either both should be specified (-reindex), or neither (-loadblock).
5759 assert(!dbp == !blocks_with_unknown_parent);
5760
5761 int64_t nStart = GetTimeMillis();
5762 const CChainParams &params{GetParams()};
5763
5764 int nLoaded = 0;
5765 try {
5766 // This takes over fileIn and calls fclose() on it in the CBufferedFile
5767 // destructor. Make sure we have at least 2*MAX_TX_SIZE space in there
5768 // so any transaction can fit in the buffer.
5769 CBufferedFile blkdat(fileIn, 2 * MAX_TX_SIZE, MAX_TX_SIZE + 8, SER_DISK,
5771 // nRewind indicates where to resume scanning in case something goes
5772 // wrong, such as a block fails to deserialize.
5773 uint64_t nRewind = blkdat.GetPos();
5774 while (!blkdat.eof()) {
5775 if (ShutdownRequested()) {
5776 return;
5777 }
5778
5779 blkdat.SetPos(nRewind);
5780 // Start one byte further next time, in case of failure.
5781 nRewind++;
5782 // Remove former limit.
5783 blkdat.SetLimit();
5784 unsigned int nSize = 0;
5785 try {
5786 // Locate a header.
5788 blkdat.FindByte(std::byte(params.DiskMagic()[0]));
5789 nRewind = blkdat.GetPos() + 1;
5790 blkdat >> buf;
5791 if (memcmp(buf, params.DiskMagic().data(),
5793 continue;
5794 }
5795
5796 // Read size.
5797 blkdat >> nSize;
5798 if (nSize < 80) {
5799 continue;
5800 }
5801 } catch (const std::exception &) {
5802 // No valid block header found; don't complain.
5803 // (this happens at the end of every blk.dat file)
5804 break;
5805 }
5806
5807 try {
5808 // read block header
5809 const uint64_t nBlockPos{blkdat.GetPos()};
5810 if (dbp) {
5811 dbp->nPos = nBlockPos;
5812 }
5813 blkdat.SetLimit(nBlockPos + nSize);
5814 CBlockHeader header;
5815 blkdat >> header;
5816 const BlockHash hash{header.GetHash()};
5817 // Skip the rest of this block (this may read from disk
5818 // into memory); position to the marker before the next block,
5819 // but it's still possible to rewind to the start of the
5820 // current block (without a disk read).
5821 nRewind = nBlockPos + nSize;
5822 blkdat.SkipTo(nRewind);
5823
5824 // needs to remain available after the cs_main lock is released
5825 // to avoid duplicate reads from disk
5826 std::shared_ptr<CBlock> pblock{};
5827
5828 {
5829 LOCK(cs_main);
5830 // detect out of order blocks, and store them for later
5831 if (hash != params.GetConsensus().hashGenesisBlock &&
5833 LogPrint(
5835 "%s: Out of order block %s, parent %s not known\n",
5836 __func__, hash.ToString(),
5837 header.hashPrevBlock.ToString());
5838 if (dbp && blocks_with_unknown_parent) {
5839 blocks_with_unknown_parent->emplace(
5840 header.hashPrevBlock, *dbp);
5841 }
5842 continue;
5843 }
5844
5845 // process in case the block isn't known yet
5846 const CBlockIndex *pindex =
5848 if (!pindex || !pindex->nStatus.hasData()) {
5849 // This block can be processed immediately; rewind to
5850 // its start, read and deserialize it.
5851 blkdat.SetPos(nBlockPos);
5852 pblock = std::make_shared<CBlock>();
5853 blkdat >> *pblock;
5854 nRewind = blkdat.GetPos();
5855
5857 if (AcceptBlock(pblock, state, true, dbp, nullptr,
5858 true)) {
5859 nLoaded++;
5860 }
5861 if (state.IsError()) {
5862 break;
5863 }
5864 } else if (hash != params.GetConsensus().hashGenesisBlock &&
5865 pindex->nHeight % 1000 == 0) {
5866 LogPrint(
5868 "Block Import: already had block %s at height %d\n",
5869 hash.ToString(), pindex->nHeight);
5870 }
5871 }
5872
5873 // Activate the genesis block so normal node progress can
5874 // continue
5875 if (hash == params.GetConsensus().hashGenesisBlock) {
5876 bool genesis_activation_failure = false;
5877 for (auto c : GetAll()) {
5879 if (!c->ActivateBestChain(state, nullptr, avalanche)) {
5880 genesis_activation_failure = true;
5881 break;
5882 }
5883 }
5884 if (genesis_activation_failure) {
5885 break;
5886 }
5887 }
5888
5889 if (m_blockman.IsPruneMode() && !fReindex && pblock) {
5890 // Must update the tip for pruning to work while importing
5891 // with -loadblock. This is a tradeoff to conserve disk
5892 // space at the expense of time spent updating the tip to be
5893 // able to prune. Otherwise, ActivateBestChain won't be
5894 // called by the import process until after all of the block
5895 // files are loaded. ActivateBestChain can be called by
5896 // concurrent network message processing, but that is not
5897 // reliable for the purpose of pruning while importing.
5898 bool activation_failure = false;
5899 for (auto c : GetAll()) {
5901 if (!c->ActivateBestChain(state, pblock, avalanche)) {
5903 "failed to activate chain (%s)\n",
5904 state.ToString());
5905 activation_failure = true;
5906 break;
5907 }
5908 }
5909 if (activation_failure) {
5910 break;
5911 }
5912 }
5913
5914 NotifyHeaderTip(*this);
5915
5916 if (!blocks_with_unknown_parent) {
5917 continue;
5918 }
5919
5920 // Recursively process earlier encountered successors of this
5921 // block
5922 std::deque<BlockHash> queue;
5923 queue.push_back(hash);
5924 while (!queue.empty()) {
5925 BlockHash head = queue.front();
5926 queue.pop_front();
5927 auto range = blocks_with_unknown_parent->equal_range(head);
5928 while (range.first != range.second) {
5929 std::multimap<BlockHash, FlatFilePos>::iterator it =
5930 range.first;
5931 std::shared_ptr<CBlock> pblockrecursive =
5932 std::make_shared<CBlock>();
5933 if (m_blockman.ReadBlockFromDisk(*pblockrecursive,
5934 it->second)) {
5935 LogPrint(
5937 "%s: Processing out of order child %s of %s\n",
5938 __func__, pblockrecursive->GetHash().ToString(),
5939 head.ToString());
5940 LOCK(cs_main);
5942 if (AcceptBlock(pblockrecursive, dummy, true,
5943 &it->second, nullptr, true)) {
5944 nLoaded++;
5945 queue.push_back(pblockrecursive->GetHash());
5946 }
5947 }
5948 range.first++;
5949 blocks_with_unknown_parent->erase(it);
5950 NotifyHeaderTip(*this);
5951 }
5952 }
5953 } catch (const std::exception &e) {
5954 // Historical bugs added extra data to the block files that does
5955 // not deserialize cleanly. Commonly this data is between
5956 // readable blocks, but it does not really matter. Such data is
5957 // not fatal to the import process. The code that reads the
5958 // block files deals with invalid data by simply ignoring it. It
5959 // continues to search for the next {4 byte magic message start
5960 // bytes + 4 byte length + block} that does deserialize cleanly
5961 // and passes all of the other block validation checks dealing
5962 // with POW and the merkle root, etc... We merely note with this
5963 // informational log message when unexpected data is
5964 // encountered. We could also be experiencing a storage system
5965 // read error, or a read of a previous bad write. These are
5966 // possible, but less likely scenarios. We don't have enough
5967 // information to tell a difference here. The reindex process is
5968 // not the place to attempt to clean and/or compact the block
5969 // files. If so desired, a studious node operator may use
5970 // knowledge of the fact that the block files are not entirely
5971 // pristine in order to prepare a set of pristine, and perhaps
5972 // ordered, block files for later reindexing.
5974 "%s: unexpected data at file offset 0x%x - %s. "
5975 "continuing\n",
5976 __func__, (nRewind - 1), e.what());
5977 }
5978 }
5979 } catch (const std::runtime_error &e) {
5980 AbortNode(std::string("System error: ") + e.what());
5981 }
5982
5983 LogPrintf("Loaded %i blocks from external file in %dms\n", nLoaded,
5984 GetTimeMillis() - nStart);
5985}
5986
5988 if (!ShouldCheckBlockIndex()) {
5989 return;
5990 }
5991
5992 LOCK(cs_main);
5993
5994 // During a reindex, we read the genesis block and call CheckBlockIndex
5995 // before ActivateBestChain, so we have the genesis block in
5996 // m_blockman.m_block_index but no active chain. (A few of the tests when
5997 // iterating the block tree require that m_chain has been initialized.)
5998 if (ActiveChain().Height() < 0) {
5999 assert(m_blockman.m_block_index.size() <= 1);
6000 return;
6001 }
6002
6003 // Build forward-pointing map of the entire block tree.
6004 std::multimap<CBlockIndex *, CBlockIndex *> forward;
6005 for (auto &[_, block_index] : m_blockman.m_block_index) {
6006 forward.emplace(block_index.pprev, &block_index);
6007 }
6008
6009 assert(forward.size() == m_blockman.m_block_index.size());
6010
6011 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
6012 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
6013 rangeGenesis = forward.equal_range(nullptr);
6014 CBlockIndex *pindex = rangeGenesis.first->second;
6015 rangeGenesis.first++;
6016 // There is only one index entry with parent nullptr.
6017 assert(rangeGenesis.first == rangeGenesis.second);
6018
6019 // Iterate over the entire block tree, using depth-first search.
6020 // Along the way, remember whether there are blocks on the path from genesis
6021 // block being explored which are the first to have certain properties.
6022 size_t nNodes = 0;
6023 int nHeight = 0;
6024 // Oldest ancestor of pindex which is invalid.
6025 CBlockIndex *pindexFirstInvalid = nullptr;
6026 // Oldest ancestor of pindex which is parked.
6027 CBlockIndex *pindexFirstParked = nullptr;
6028 // Oldest ancestor of pindex which does not have data available, since
6029 // assumeutxo snapshot if used.
6030 CBlockIndex *pindexFirstMissing = nullptr;
6031 // Oldest ancestor of pindex for which nTx == 0, since assumeutxo snapshot
6032 // if used..
6033 CBlockIndex *pindexFirstNeverProcessed = nullptr;
6034 // Oldest ancestor of pindex which does not have BLOCK_VALID_TREE
6035 // (regardless of being valid or not).
6036 CBlockIndex *pindexFirstNotTreeValid = nullptr;
6037 // Oldest ancestor of pindex which does not have BLOCK_VALID_TRANSACTIONS
6038 // (regardless of being valid or not), since assumeutxo snapshot if used.
6039 CBlockIndex *pindexFirstNotTransactionsValid = nullptr;
6040 // Oldest ancestor of pindex which does not have BLOCK_VALID_CHAIN
6041 // (regardless of being valid or not), since assumeutxo snapshot if used.
6042 CBlockIndex *pindexFirstNotChainValid = nullptr;
6043 // Oldest ancestor of pindex which does not have BLOCK_VALID_SCRIPTS
6044 // (regardless of being valid or not), since assumeutxo snapshot if used.
6045 CBlockIndex *pindexFirstNotScriptsValid = nullptr;
6046
6047 // After checking an assumeutxo snapshot block, reset pindexFirst pointers
6048 // to earlier blocks that have not been downloaded or validated yet, so
6049 // checks for later blocks can assume the earlier blocks were validated and
6050 // be stricter, testing for more requirements.
6051 const CBlockIndex *snap_base{GetSnapshotBaseBlock()};
6052 CBlockIndex *snap_first_missing{}, *snap_first_notx{}, *snap_first_notv{},
6053 *snap_first_nocv{}, *snap_first_nosv{};
6054 auto snap_update_firsts = [&] {
6055 if (pindex == snap_base) {
6056 std::swap(snap_first_missing, pindexFirstMissing);
6057 std::swap(snap_first_notx, pindexFirstNeverProcessed);
6058 std::swap(snap_first_notv, pindexFirstNotTransactionsValid);
6059 std::swap(snap_first_nocv, pindexFirstNotChainValid);
6060 std::swap(snap_first_nosv, pindexFirstNotScriptsValid);
6061 }
6062 };
6063
6064 while (pindex != nullptr) {
6065 nNodes++;
6066 if (pindexFirstInvalid == nullptr && pindex->nStatus.hasFailed()) {
6067 pindexFirstInvalid = pindex;
6068 }
6069 if (pindexFirstParked == nullptr && pindex->nStatus.isParked()) {
6070 pindexFirstParked = pindex;
6071 }
6072 if (pindexFirstMissing == nullptr && !pindex->nStatus.hasData()) {
6073 pindexFirstMissing = pindex;
6074 }
6075 if (pindexFirstNeverProcessed == nullptr && pindex->nTx == 0) {
6076 pindexFirstNeverProcessed = pindex;
6077 }
6078 if (pindex->pprev != nullptr && pindexFirstNotTreeValid == nullptr &&
6079 pindex->nStatus.getValidity() < BlockValidity::TREE) {
6080 pindexFirstNotTreeValid = pindex;
6081 }
6082 if (pindex->pprev != nullptr) {
6083 if (pindexFirstNotTransactionsValid == nullptr &&
6084 pindex->nStatus.getValidity() < BlockValidity::TRANSACTIONS) {
6085 pindexFirstNotTransactionsValid = pindex;
6086 }
6087 if (pindexFirstNotChainValid == nullptr &&
6088 pindex->nStatus.getValidity() < BlockValidity::CHAIN) {
6089 pindexFirstNotChainValid = pindex;
6090 }
6091 if (pindexFirstNotScriptsValid == nullptr &&
6092 pindex->nStatus.getValidity() < BlockValidity::SCRIPTS) {
6093 pindexFirstNotScriptsValid = pindex;
6094 }
6095 }
6096
6097 // Begin: actual consistency checks.
6098 if (pindex->pprev == nullptr) {
6099 // Genesis block checks.
6100 // Genesis block's hash must match.
6101 assert(pindex->GetBlockHash() == GetConsensus().hashGenesisBlock);
6102 for (auto c : GetAll()) {
6103 if (c->m_chain.Genesis() != nullptr) {
6104 // The chain's genesis block must be this block.
6105 assert(pindex == c->m_chain.Genesis());
6106 }
6107 }
6108 }
6109 if (!pindex->HaveNumChainTxs()) {
6110 // nSequenceId can't be set positive for blocks that aren't linked
6111 // (negative is used for preciousblock)
6112 assert(pindex->nSequenceId <= 0);
6113 }
6114 // VALID_TRANSACTIONS is equivalent to nTx > 0 for all nodes (whether or
6115 // not pruning has occurred). HAVE_DATA is only equivalent to nTx > 0
6116 // (or VALID_TRANSACTIONS) if no pruning has occurred.
6118 // If we've never pruned, then HAVE_DATA should be equivalent to nTx
6119 // > 0
6120 assert(pindex->nStatus.hasData() == (pindex->nTx > 0));
6121 assert(pindexFirstMissing == pindexFirstNeverProcessed);
6122 } else if (pindex->nStatus.hasData()) {
6123 // If we have pruned, then we can only say that HAVE_DATA implies
6124 // nTx > 0
6125 assert(pindex->nTx > 0);
6126 }
6127 if (pindex->nStatus.hasUndo()) {
6128 assert(pindex->nStatus.hasData());
6129 }
6130 if (snap_base && snap_base->GetAncestor(pindex->nHeight) == pindex) {
6131 // Assumed-valid blocks should connect to the main chain.
6132 assert(pindex->nStatus.getValidity() >= BlockValidity::TREE);
6133 }
6134 // There should only be an nTx value if we have
6135 // actually seen a block's transactions.
6136 // This is pruning-independent.
6137 assert((pindex->nStatus.getValidity() >= BlockValidity::TRANSACTIONS) ==
6138 (pindex->nTx > 0));
6139 // All parents having had data (at some point) is equivalent to all
6140 // parents being VALID_TRANSACTIONS, which is equivalent to
6141 // HaveNumChainTxs().
6142 assert((pindexFirstNeverProcessed == nullptr || pindex == snap_base) ==
6143 (pindex->HaveNumChainTxs()));
6144 assert((pindexFirstNotTransactionsValid == nullptr ||
6145 pindex == snap_base) == (pindex->HaveNumChainTxs()));
6146 // nHeight must be consistent.
6147 assert(pindex->nHeight == nHeight);
6148 // For every block except the genesis block, the chainwork must be
6149 // larger than the parent's.
6150 assert(pindex->pprev == nullptr ||
6151 pindex->nChainWork >= pindex->pprev->nChainWork);
6152 // The pskip pointer must point back for all but the first 2 blocks.
6153 assert(nHeight < 2 ||
6154 (pindex->pskip && (pindex->pskip->nHeight < nHeight)));
6155 // All m_blockman.m_block_index entries must at least be TREE valid
6156 assert(pindexFirstNotTreeValid == nullptr);
6157 if (pindex->nStatus.getValidity() >= BlockValidity::TREE) {
6158 // TREE valid implies all parents are TREE valid
6159 assert(pindexFirstNotTreeValid == nullptr);
6160 }
6161 if (pindex->nStatus.getValidity() >= BlockValidity::CHAIN) {
6162 // CHAIN valid implies all parents are CHAIN valid
6163 assert(pindexFirstNotChainValid == nullptr);
6164 }
6165 if (pindex->nStatus.getValidity() >= BlockValidity::SCRIPTS) {
6166 // SCRIPTS valid implies all parents are SCRIPTS valid
6167 assert(pindexFirstNotScriptsValid == nullptr);
6168 }
6169 if (pindexFirstInvalid == nullptr) {
6170 // Checks for not-invalid blocks.
6171 // The failed mask cannot be set for blocks without invalid parents.
6172 assert(!pindex->nStatus.isInvalid());
6173 }
6174 if (pindexFirstParked == nullptr) {
6175 // Checks for not-parked blocks.
6176 // The parked mask cannot be set for blocks without parked parents.
6177 // (i.e., hasParkedParent only if an ancestor is properly parked).
6178 assert(!pindex->nStatus.isOnParkedChain());
6179 }
6180 // Make sure nChainTx sum is correctly computed.
6181 if (!pindex->pprev) {
6182 // If no previous block, nTx and nChainTx must be the same.
6183 assert(pindex->nChainTx == pindex->nTx);
6184 } else if (pindex->pprev->nChainTx > 0 && pindex->nTx > 0) {
6185 // If previous nChainTx is set and number of transactions in block
6186 // is known, sum must be set.
6187 assert(pindex->nChainTx == pindex->nTx + pindex->pprev->nChainTx);
6188 } else {
6189 // Otherwise nChainTx should only be set if this is a snapshot
6190 // block, and must be set if it is.
6191 assert((pindex->nChainTx != 0) == (pindex == snap_base));
6192 }
6193
6194 // Chainstate-specific checks on setBlockIndexCandidates
6195 for (auto c : GetAll()) {
6196 if (c->m_chain.Tip() == nullptr) {
6197 continue;
6198 }
6199 // Two main factors determine whether pindex is a candidate in
6200 // setBlockIndexCandidates:
6201 //
6202 // - If pindex has less work than the chain tip, it should not be a
6203 // candidate, and this will be asserted below. Otherwise it is a
6204 // potential candidate.
6205 //
6206 // - If pindex or one of its parent blocks back to the genesis block
6207 // or an assumeutxo snapshot never downloaded transactions
6208 // (pindexFirstNeverProcessed is non-null), it should not be a
6209 // candidate, and this will be asserted below. The only exception
6210 // is if pindex itself is an assumeutxo snapshot block. Then it is
6211 // also a potential candidate.
6212 if (!CBlockIndexWorkComparator()(pindex, c->m_chain.Tip()) &&
6213 (pindexFirstNeverProcessed == nullptr || pindex == snap_base)) {
6214 // If pindex was detected as invalid (pindexFirstInvalid is
6215 // non-null), it is not required to be in
6216 // setBlockIndexCandidates.
6217 if (pindexFirstInvalid == nullptr) {
6218 // If this chainstate is the active chainstate, pindex
6219 // must be in setBlockIndexCandidates. Otherwise, this
6220 // chainstate is a background validation chainstate, and
6221 // pindex only needs to be added if it is an ancestor of
6222 // the snapshot that is being validated.
6223 if (c == &ActiveChainstate() ||
6224 GetSnapshotBaseBlock()->GetAncestor(pindex->nHeight) ==
6225 pindex) {
6226 // If pindex and all its parents back to the genesis
6227 // block or an assumeutxo snapshot block downloaded
6228 // transactions, transactions, and the transactions were
6229 // not pruned (pindexFirstMissing is null), it is a
6230 // potential candidate or was parked. The check excludes
6231 // pruned blocks, because if any blocks were pruned
6232 // between pindex the current chain tip, pindex will
6233 // only temporarily be added to setBlockIndexCandidates,
6234 // before being moved to m_blocks_unlinked. This check
6235 // could be improved to verify that if all blocks
6236 // between the chain tip and pindex have data, pindex
6237 // must be a candidate.
6238 if (pindexFirstMissing == nullptr) {
6239 assert(pindex->nStatus.isOnParkedChain() ||
6240 c->setBlockIndexCandidates.count(pindex));
6241 }
6242 // If pindex is the chain tip, it also is a potential
6243 // candidate.
6244 //
6245 // If the chainstate was loaded from a snapshot and
6246 // pindex is the base of the snapshot, pindex is also a
6247 // potential candidate.
6248 if (pindex == c->m_chain.Tip() ||
6249 pindex == c->SnapshotBase()) {
6250 assert(c->setBlockIndexCandidates.count(pindex));
6251 }
6252 }
6253 // If some parent is missing, then it could be that this
6254 // block was in setBlockIndexCandidates but had to be
6255 // removed because of the missing data. In this case it must
6256 // be in m_blocks_unlinked -- see test below.
6257 }
6258 } else {
6259 // If this block sorts worse than the current tip or some
6260 // ancestor's block has never been seen, it cannot be in
6261 // setBlockIndexCandidates.
6262 assert(c->setBlockIndexCandidates.count(pindex) == 0);
6263 }
6264 }
6265 // Check whether this block is in m_blocks_unlinked.
6266 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
6267 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
6268 rangeUnlinked =
6269 m_blockman.m_blocks_unlinked.equal_range(pindex->pprev);
6270 bool foundInUnlinked = false;
6271 while (rangeUnlinked.first != rangeUnlinked.second) {
6272 assert(rangeUnlinked.first->first == pindex->pprev);
6273 if (rangeUnlinked.first->second == pindex) {
6274 foundInUnlinked = true;
6275 break;
6276 }
6277 rangeUnlinked.first++;
6278 }
6279 if (pindex->pprev && pindex->nStatus.hasData() &&
6280 pindexFirstNeverProcessed != nullptr &&
6281 pindexFirstInvalid == nullptr) {
6282 // If this block has block data available, some parent was never
6283 // received, and has no invalid parents, it must be in
6284 // m_blocks_unlinked.
6285 assert(foundInUnlinked);
6286 }
6287 if (!pindex->nStatus.hasData()) {
6288 // Can't be in m_blocks_unlinked if we don't HAVE_DATA
6289 assert(!foundInUnlinked);
6290 }
6291 if (pindexFirstMissing == nullptr) {
6292 // We aren't missing data for any parent -- cannot be in
6293 // m_blocks_unlinked.
6294 assert(!foundInUnlinked);
6295 }
6296 if (pindex->pprev && pindex->nStatus.hasData() &&
6297 pindexFirstNeverProcessed == nullptr &&
6298 pindexFirstMissing != nullptr) {
6299 // We HAVE_DATA for this block, have received data for all parents
6300 // at some point, but we're currently missing data for some parent.
6302 // This block may have entered m_blocks_unlinked if:
6303 // - it has a descendant that at some point had more work than the
6304 // tip, and
6305 // - we tried switching to that descendant but were missing
6306 // data for some intermediate block between m_chain and the
6307 // tip.
6308 // So if this block is itself better than any m_chain.Tip() and it
6309 // wasn't in setBlockIndexCandidates, then it must be in
6310 // m_blocks_unlinked.
6311 for (auto c : GetAll()) {
6312 const bool is_active = c == &ActiveChainstate();
6313 if (!CBlockIndexWorkComparator()(pindex, c->m_chain.Tip()) &&
6314 c->setBlockIndexCandidates.count(pindex) == 0) {
6315 if (pindexFirstInvalid == nullptr) {
6316 if (is_active ||
6317 snap_base->GetAncestor(pindex->nHeight) == pindex) {
6318 assert(foundInUnlinked);
6319 }
6320 }
6321 }
6322 }
6323 }
6324 // Perhaps too slow
6325 // assert(pindex->GetBlockHash() == pindex->GetBlockHeader().GetHash());
6326 // End: actual consistency checks.
6327
6328 // Try descending into the first subnode.
6329 snap_update_firsts();
6330 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
6331 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
6332 range = forward.equal_range(pindex);
6333 if (range.first != range.second) {
6334 // A subnode was found.
6335 pindex = range.first->second;
6336 nHeight++;
6337 continue;
6338 }
6339 // This is a leaf node. Move upwards until we reach a node of which we
6340 // have not yet visited the last child.
6341 while (pindex) {
6342 // We are going to either move to a parent or a sibling of pindex.
6343 snap_update_firsts();
6344 // If pindex was the first with a certain property, unset the
6345 // corresponding variable.
6346 if (pindex == pindexFirstInvalid) {
6347 pindexFirstInvalid = nullptr;
6348 }
6349 if (pindex == pindexFirstParked) {
6350 pindexFirstParked = nullptr;
6351 }
6352 if (pindex == pindexFirstMissing) {
6353 pindexFirstMissing = nullptr;
6354 }
6355 if (pindex == pindexFirstNeverProcessed) {
6356 pindexFirstNeverProcessed = nullptr;
6357 }
6358 if (pindex == pindexFirstNotTreeValid) {
6359 pindexFirstNotTreeValid = nullptr;
6360 }
6361 if (pindex == pindexFirstNotTransactionsValid) {
6362 pindexFirstNotTransactionsValid = nullptr;
6363 }
6364 if (pindex == pindexFirstNotChainValid) {
6365 pindexFirstNotChainValid = nullptr;
6366 }
6367 if (pindex == pindexFirstNotScriptsValid) {
6368 pindexFirstNotScriptsValid = nullptr;
6369 }
6370 // Find our parent.
6371 CBlockIndex *pindexPar = pindex->pprev;
6372 // Find which child we just visited.
6373 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
6374 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
6375 rangePar = forward.equal_range(pindexPar);
6376 while (rangePar.first->second != pindex) {
6377 // Our parent must have at least the node we're coming from as
6378 // child.
6379 assert(rangePar.first != rangePar.second);
6380 rangePar.first++;
6381 }
6382 // Proceed to the next one.
6383 rangePar.first++;
6384 if (rangePar.first != rangePar.second) {
6385 // Move to the sibling.
6386 pindex = rangePar.first->second;
6387 break;
6388 } else {
6389 // Move up further.
6390 pindex = pindexPar;
6391 nHeight--;
6392 continue;
6393 }
6394 }
6395 }
6396
6397 // Check that we actually traversed the entire map.
6398 assert(nNodes == forward.size());
6399}
6400
6401std::string Chainstate::ToString() {
6403 CBlockIndex *tip = m_chain.Tip();
6404 return strprintf("Chainstate [%s] @ height %d (%s)",
6405 m_from_snapshot_blockhash ? "snapshot" : "ibd",
6406 tip ? tip->nHeight : -1,
6407 tip ? tip->GetBlockHash().ToString() : "null");
6408}
6409
6410bool Chainstate::ResizeCoinsCaches(size_t coinstip_size, size_t coinsdb_size) {
6412 if (coinstip_size == m_coinstip_cache_size_bytes &&
6413 coinsdb_size == m_coinsdb_cache_size_bytes) {
6414 // Cache sizes are unchanged, no need to continue.
6415 return true;
6416 }
6417 size_t old_coinstip_size = m_coinstip_cache_size_bytes;
6418 m_coinstip_cache_size_bytes = coinstip_size;
6419 m_coinsdb_cache_size_bytes = coinsdb_size;
6420 CoinsDB().ResizeCache(coinsdb_size);
6421
6422 LogPrintf("[%s] resized coinsdb cache to %.1f MiB\n", this->ToString(),
6423 coinsdb_size * (1.0 / 1024 / 1024));
6424 LogPrintf("[%s] resized coinstip cache to %.1f MiB\n", this->ToString(),
6425 coinstip_size * (1.0 / 1024 / 1024));
6426
6428 bool ret;
6429
6430 if (coinstip_size > old_coinstip_size) {
6431 // Likely no need to flush if cache sizes have grown.
6433 } else {
6434 // Otherwise, flush state to disk and deallocate the in-memory coins
6435 // map.
6437 }
6438 return ret;
6439}
6440
6446 const CBlockIndex *pindex) {
6447 if (pindex == nullptr) {
6448 return 0.0;
6449 }
6450 if (pindex->nChainTx == 0) {
6452 "Block %d has unset m_chain_tx_count. Unable to "
6453 "estimate verification progress.\n",
6454 pindex->nHeight);
6455 return 0.0;
6456 }
6457
6458 int64_t nNow = time(nullptr);
6459
6460 double fTxTotal;
6461 if (pindex->GetChainTxCount() <= data.nTxCount) {
6462 fTxTotal = data.nTxCount + (nNow - data.nTime) * data.dTxRate;
6463 } else {
6464 fTxTotal = pindex->GetChainTxCount() +
6465 (nNow - pindex->GetBlockTime()) * data.dTxRate;
6466 }
6467
6468 return std::min<double>(pindex->GetChainTxCount() / fTxTotal, 1.0);
6469}
6470
6471std::optional<BlockHash> ChainstateManager::SnapshotBlockhash() const {
6472 LOCK(::cs_main);
6473 if (m_active_chainstate && m_active_chainstate->m_from_snapshot_blockhash) {
6474 // If a snapshot chainstate exists, it will always be our active.
6475 return m_active_chainstate->m_from_snapshot_blockhash;
6476 }
6477 return std::nullopt;
6478}
6479
6480std::vector<Chainstate *> ChainstateManager::GetAll() {
6481 LOCK(::cs_main);
6482 std::vector<Chainstate *> out;
6483
6484 for (Chainstate *pchainstate :
6485 {m_ibd_chainstate.get(), m_snapshot_chainstate.get()}) {
6486 if (this->IsUsable(pchainstate)) {
6487 out.push_back(pchainstate);
6488 }
6489 }
6490
6491 return out;
6492}
6493
6494Chainstate &ChainstateManager::InitializeChainstate(CTxMemPool *mempool) {
6496 assert(!m_ibd_chainstate);
6497 assert(!m_active_chainstate);
6498
6499 m_ibd_chainstate = std::make_unique<Chainstate>(mempool, m_blockman, *this);
6500 m_active_chainstate = m_ibd_chainstate.get();
6501 return *m_active_chainstate;
6502}
6503
6504[[nodiscard]] static bool DeleteCoinsDBFromDisk(const fs::path &db_path,
6505 bool is_snapshot)
6508
6509 if (is_snapshot) {
6510 fs::path base_blockhash_path =
6512
6513 try {
6514 const bool existed{fs::remove(base_blockhash_path)};
6515 if (!existed) {
6516 LogPrintf("[snapshot] snapshot chainstate dir being removed "
6517 "lacks %s file\n",
6519 }
6520 } catch (const fs::filesystem_error &e) {
6521 LogPrintf("[snapshot] failed to remove file %s: %s\n",
6522 fs::PathToString(base_blockhash_path),
6524 }
6525 }
6526
6527 std::string path_str = fs::PathToString(db_path);
6528 LogPrintf("Removing leveldb dir at %s\n", path_str);
6529
6530 // We have to destruct before this call leveldb::DB in order to release the
6531 // db lock, otherwise `DestroyDB` will fail. See `leveldb::~DBImpl()`.
6532 const bool destroyed = dbwrapper::DestroyDB(path_str, {}).ok();
6533
6534 if (!destroyed) {
6535 LogPrintf("error: leveldb DestroyDB call failed on %s\n", path_str);
6536 }
6537
6538 // Datadir should be removed from filesystem; otherwise initialization may
6539 // detect it on subsequent statups and get confused.
6540 //
6541 // If the base_blockhash_path removal above fails in the case of snapshot
6542 // chainstates, this will return false since leveldb won't remove a
6543 // non-empty directory.
6544 return destroyed && !fs::exists(db_path);
6545}
6546
6548 AutoFile &coins_file, const SnapshotMetadata &metadata, bool in_memory) {
6549 BlockHash base_blockhash = metadata.m_base_blockhash;
6550
6551 if (this->SnapshotBlockhash()) {
6553 "Can't activate a snapshot-based chainstate more than once")};
6554 }
6555
6556 CBlockIndex *snapshot_start_block{};
6557
6558 {
6559 LOCK(::cs_main);
6560
6561 if (!GetParams().AssumeutxoForBlockhash(base_blockhash).has_value()) {
6562 auto available_heights = GetParams().GetAvailableSnapshotHeights();
6563 std::string heights_formatted =
6564 Join(available_heights, ", ",
6565 [&](const auto &i) { return ToString(i); });
6566 return util::Error{strprintf(
6567 Untranslated("assumeutxo block hash in snapshot metadata not "
6568 "recognized (hash: %s). The following "
6569 "snapshot heights are available: %s."),
6570 base_blockhash.ToString(), heights_formatted)};
6571 }
6572
6573 snapshot_start_block = m_blockman.LookupBlockIndex(base_blockhash);
6574 if (!snapshot_start_block) {
6575 return util::Error{strprintf(
6576 Untranslated("The base block header (%s) must appear in the "
6577 "headers chain. Make sure all headers are "
6578 "syncing, and call loadtxoutset again."),
6579 base_blockhash.ToString())};
6580 }
6581
6582 if (snapshot_start_block->nStatus.isInvalid()) {
6583 return util::Error{strprintf(
6585 "The base block header (%s) is part of an invalid chain"),
6586 base_blockhash.ToString())};
6587 }
6588
6589 if (!m_best_header ||
6590 m_best_header->GetAncestor(snapshot_start_block->nHeight) !=
6591 snapshot_start_block) {
6593 "A forked headers-chain with more work than the chain with the "
6594 "snapshot base block header exists. Please proceed to sync "
6595 "without AssumeUtxo.")};
6596 }
6597
6598 if (Assert(m_active_chainstate->GetMempool())->size() > 0) {
6600 "Can't activate a snapshot when mempool not empty.")};
6601 }
6602 }
6603
6604 int64_t current_coinsdb_cache_size{0};
6605 int64_t current_coinstip_cache_size{0};
6606
6607 // Cache percentages to allocate to each chainstate.
6608 //
6609 // These particular percentages don't matter so much since they will only be
6610 // relevant during snapshot activation; caches are rebalanced at the
6611 // conclusion of this function. We want to give (essentially) all available
6612 // cache capacity to the snapshot to aid the bulk load later in this
6613 // function.
6614 static constexpr double IBD_CACHE_PERC = 0.01;
6615 static constexpr double SNAPSHOT_CACHE_PERC = 0.99;
6616
6617 {
6618 LOCK(::cs_main);
6619 // Resize the coins caches to ensure we're not exceeding memory limits.
6620 //
6621 // Allocate the majority of the cache to the incoming snapshot
6622 // chainstate, since (optimistically) getting to its tip will be the top
6623 // priority. We'll need to call `MaybeRebalanceCaches()` once we're done
6624 // with this function to ensure the right allocation (including the
6625 // possibility that no snapshot was activated and that we should restore
6626 // the active chainstate caches to their original size).
6627 //
6628 current_coinsdb_cache_size =
6629 this->ActiveChainstate().m_coinsdb_cache_size_bytes;
6630 current_coinstip_cache_size =
6631 this->ActiveChainstate().m_coinstip_cache_size_bytes;
6632
6633 // Temporarily resize the active coins cache to make room for the
6634 // newly-created snapshot chain.
6635 this->ActiveChainstate().ResizeCoinsCaches(
6636 static_cast<size_t>(current_coinstip_cache_size * IBD_CACHE_PERC),
6637 static_cast<size_t>(current_coinsdb_cache_size * IBD_CACHE_PERC));
6638 }
6639
6640 auto snapshot_chainstate =
6641 WITH_LOCK(::cs_main, return std::make_unique<Chainstate>(
6642 /* mempool */ nullptr, m_blockman, *this,
6643 base_blockhash));
6644
6645 {
6646 LOCK(::cs_main);
6647 snapshot_chainstate->InitCoinsDB(
6648 static_cast<size_t>(current_coinsdb_cache_size *
6649 SNAPSHOT_CACHE_PERC),
6650 in_memory, false, "chainstate");
6651 snapshot_chainstate->InitCoinsCache(static_cast<size_t>(
6652 current_coinstip_cache_size * SNAPSHOT_CACHE_PERC));
6653 }
6654
6655 auto cleanup_bad_snapshot =
6657 this->MaybeRebalanceCaches();
6658
6659 // PopulateAndValidateSnapshot can return (in error) before the
6660 // leveldb datadir has been created, so only attempt removal if we
6661 // got that far.
6662 if (auto snapshot_datadir = node::FindSnapshotChainstateDir()) {
6663 // We have to destruct leveldb::DB in order to release the db
6664 // lock, otherwise DestroyDB() (in DeleteCoinsDBFromDisk()) will
6665 // fail. See `leveldb::~DBImpl()`. Destructing the chainstate
6666 // (and so resetting the coinsviews object) does this.
6667 snapshot_chainstate.reset();
6668 bool removed = DeleteCoinsDBFromDisk(*snapshot_datadir,
6669 /*is_snapshot=*/true);
6670 if (!removed) {
6672 "Failed to remove snapshot chainstate dir (%s). "
6673 "Manually remove it before restarting.\n",
6674 fs::PathToString(*snapshot_datadir)));
6675 }
6676 }
6677 return util::Error{std::move(reason)};
6678 };
6679
6680 if (!this->PopulateAndValidateSnapshot(*snapshot_chainstate, coins_file,
6681 metadata)) {
6682 LOCK(::cs_main);
6683 return cleanup_bad_snapshot(Untranslated("population failed"));
6684 }
6685
6686 // cs_main required for rest of snapshot activation.
6687 LOCK(::cs_main);
6688
6689 // Do a final check to ensure that the snapshot chainstate is actually a
6690 // more work chain than the active chainstate; a user could have loaded a
6691 // snapshot very late in the IBD process, and we wouldn't want to load a
6692 // useless chainstate.
6694 snapshot_chainstate->m_chain.Tip())) {
6695 return cleanup_bad_snapshot(
6696 Untranslated("work does not exceed active chainstate"));
6697 }
6698 // If not in-memory, persist the base blockhash for use during subsequent
6699 // initialization.
6700 if (!in_memory) {
6701 if (!node::WriteSnapshotBaseBlockhash(*snapshot_chainstate)) {
6702 return cleanup_bad_snapshot(
6703 Untranslated("could not write base blockhash"));
6704 }
6705 }
6706
6707 assert(!m_snapshot_chainstate);
6708 m_snapshot_chainstate.swap(snapshot_chainstate);
6709 const bool chaintip_loaded = m_snapshot_chainstate->LoadChainTip();
6710 assert(chaintip_loaded);
6711
6712 // Transfer possession of the mempool to the snapshot chainstate.
6713 // Mempool is empty at this point because we're still in IBD.
6714 Assert(m_active_chainstate->m_mempool->size() == 0);
6715 Assert(!m_snapshot_chainstate->m_mempool);
6716 m_snapshot_chainstate->m_mempool = m_active_chainstate->m_mempool;
6717 m_active_chainstate->m_mempool = nullptr;
6718 m_active_chainstate = m_snapshot_chainstate.get();
6719 m_blockman.m_snapshot_height = this->GetSnapshotBaseHeight();
6720
6721 LogPrintf("[snapshot] successfully activated snapshot %s\n",
6722 base_blockhash.ToString());
6723 LogPrintf("[snapshot] (%.2f MB)\n",
6724 m_snapshot_chainstate->CoinsTip().DynamicMemoryUsage() /
6725 (1000 * 1000));
6726
6727 this->MaybeRebalanceCaches();
6728 return snapshot_start_block;
6729}
6730
6731static void FlushSnapshotToDisk(CCoinsViewCache &coins_cache,
6732 bool snapshot_loaded) {
6734 strprintf("%s (%.2f MB)",
6735 snapshot_loaded ? "saving snapshot chainstate"
6736 : "flushing coins cache",
6737 coins_cache.DynamicMemoryUsage() / (1000 * 1000)),
6738 BCLog::LogFlags::ALL);
6739
6740 coins_cache.Flush();
6741}
6742
6743struct StopHashingException : public std::exception {
6744 const char *what() const throw() override {
6745 return "ComputeUTXOStats interrupted by shutdown.";
6746 }
6747};
6748
6750 if (ShutdownRequested()) {
6751 throw StopHashingException();
6752 }
6753}
6754
6756 Chainstate &snapshot_chainstate, AutoFile &coins_file,
6757 const SnapshotMetadata &metadata) {
6758 // It's okay to release cs_main before we're done using `coins_cache`
6759 // because we know that nothing else will be referencing the newly created
6760 // snapshot_chainstate yet.
6761 CCoinsViewCache &coins_cache =
6762 *WITH_LOCK(::cs_main, return &snapshot_chainstate.CoinsTip());
6763
6764 BlockHash base_blockhash = metadata.m_base_blockhash;
6765
6766 CBlockIndex *snapshot_start_block = WITH_LOCK(
6767 ::cs_main, return m_blockman.LookupBlockIndex(base_blockhash));
6768
6769 if (!snapshot_start_block) {
6770 // Needed for ComputeUTXOStats to determine the
6771 // height and to avoid a crash when base_blockhash.IsNull()
6772 LogPrintf("[snapshot] Did not find snapshot start blockheader %s\n",
6773 base_blockhash.ToString());
6774 return false;
6775 }
6776
6777 int base_height = snapshot_start_block->nHeight;
6778 const auto &maybe_au_data = GetParams().AssumeutxoForHeight(base_height);
6779
6780 if (!maybe_au_data) {
6781 LogPrintf("[snapshot] assumeutxo height in snapshot metadata not "
6782 "recognized (%d) - refusing to load snapshot\n",
6783 base_height);
6784 return false;
6785 }
6786
6787 const AssumeutxoData &au_data = *maybe_au_data;
6788
6789 // This work comparison is a duplicate check with the one performed later in
6790 // ActivateSnapshot(), but is done so that we avoid doing the long work of
6791 // staging a snapshot that isn't actually usable.
6793 ActiveTip(), snapshot_start_block))) {
6794 LogPrintf("[snapshot] activation failed - work does not exceed active "
6795 "chainstate\n");
6796 return false;
6797 }
6798
6799 const uint64_t coins_count = metadata.m_coins_count;
6800 uint64_t coins_left = metadata.m_coins_count;
6801
6802 LogPrintf("[snapshot] loading %d coins from snapshot %s\n", coins_left,
6803 base_blockhash.ToString());
6804 int64_t coins_processed{0};
6805
6806 while (coins_left > 0) {
6807 try {
6808 TxId txid;
6809 coins_file >> txid;
6810 size_t coins_per_txid{0};
6811 coins_per_txid = ReadCompactSize(coins_file);
6812
6813 if (coins_per_txid > coins_left) {
6814 LogPrintf("[snapshot] mismatch in coins count in snapshot "
6815 "metadata and actual snapshot data\n");
6816 return false;
6817 }
6818
6819 for (size_t i = 0; i < coins_per_txid; i++) {
6820 Coin coin;
6821 COutPoint outpoint{
6822 txid, static_cast<uint32_t>(ReadCompactSize(coins_file))};
6823 coins_file >> coin;
6824 // Avoid integer wrap-around in coinstats.cpp:ApplyHash
6825 if (coin.GetHeight() > uint32_t(base_height) ||
6826 outpoint.GetN() >=
6827 std::numeric_limits<decltype(outpoint.GetN())>::max()) {
6828 LogPrintf("[snapshot] bad snapshot data after "
6829 "deserializing %d coins\n",
6830 coins_count - coins_left);
6831 return false;
6832 }
6833 if (!MoneyRange(coin.GetTxOut().nValue)) {
6834 LogPrintf("[snapshot] bad snapshot data after "
6835 "deserializing %d coins - bad tx out value\n",
6836 coins_count - coins_left);
6837 return false;
6838 }
6839 coins_cache.EmplaceCoinInternalDANGER(std::move(outpoint),
6840 std::move(coin));
6841
6842 --coins_left;
6843 ++coins_processed;
6844
6845 if (coins_processed % 1000000 == 0) {
6846 LogPrintf("[snapshot] %d coins loaded (%.2f%%, %.2f MB)\n",
6847 coins_processed,
6848 static_cast<float>(coins_processed) * 100 /
6849 static_cast<float>(coins_count),
6850 coins_cache.DynamicMemoryUsage() / (1000 * 1000));
6851 }
6852
6853 // Batch write and flush (if we need to) every so often.
6854 //
6855 // If our average Coin size is roughly 41 bytes, checking every
6856 // 120,000 coins means <5MB of memory imprecision.
6857 if (coins_processed % 120000 == 0) {
6858 if (ShutdownRequested()) {
6859 return false;
6860 }
6861
6862 const auto snapshot_cache_state = WITH_LOCK(
6863 ::cs_main,
6864 return snapshot_chainstate.GetCoinsCacheSizeState());
6865
6866 if (snapshot_cache_state >= CoinsCacheSizeState::CRITICAL) {
6867 // This is a hack - we don't know what the actual best
6868 // block is, but that doesn't matter for the purposes of
6869 // flushing the cache here. We'll set this to its
6870 // correct value (`base_blockhash`) below after the
6871 // coins are loaded.
6872 coins_cache.SetBestBlock(BlockHash{GetRandHash()});
6873
6874 // No need to acquire cs_main since this chainstate
6875 // isn't being used yet.
6876 FlushSnapshotToDisk(coins_cache,
6877 /*snapshot_loaded=*/false);
6878 }
6879 }
6880 }
6881 } catch (const std::ios_base::failure &) {
6882 LogPrintf("[snapshot] bad snapshot format or truncated snapshot "
6883 "after deserializing %d coins\n",
6884 coins_processed);
6885 return false;
6886 }
6887 }
6888
6889 // Important that we set this. This and the coins_cache accesses above are
6890 // sort of a layer violation, but either we reach into the innards of
6891 // CCoinsViewCache here or we have to invert some of the Chainstate to
6892 // embed them in a snapshot-activation-specific CCoinsViewCache bulk load
6893 // method.
6894 coins_cache.SetBestBlock(base_blockhash);
6895
6896 bool out_of_coins{false};
6897 try {
6898 TxId txid;
6899 coins_file >> txid;
6900 } catch (const std::ios_base::failure &) {
6901 // We expect an exception since we should be out of coins.
6902 out_of_coins = true;
6903 }
6904 if (!out_of_coins) {
6905 LogPrintf("[snapshot] bad snapshot - coins left over after "
6906 "deserializing %d coins\n",
6907 coins_count);
6908 return false;
6909 }
6910
6911 LogPrintf("[snapshot] loaded %d (%.2f MB) coins from snapshot %s\n",
6912 coins_count, coins_cache.DynamicMemoryUsage() / (1000 * 1000),
6913 base_blockhash.ToString());
6914
6915 // No need to acquire cs_main since this chainstate isn't being used yet.
6916 FlushSnapshotToDisk(coins_cache, /*snapshot_loaded=*/true);
6917
6918 assert(coins_cache.GetBestBlock() == base_blockhash);
6919
6920 // As above, okay to immediately release cs_main here since no other context
6921 // knows about the snapshot_chainstate.
6922 CCoinsViewDB *snapshot_coinsdb =
6923 WITH_LOCK(::cs_main, return &snapshot_chainstate.CoinsDB());
6924
6925 std::optional<CCoinsStats> maybe_stats;
6926
6927 try {
6928 maybe_stats = ComputeUTXOStats(CoinStatsHashType::HASH_SERIALIZED,
6929 snapshot_coinsdb, m_blockman,
6931 } catch (StopHashingException const &) {
6932 return false;
6933 }
6934 if (!maybe_stats.has_value()) {
6935 LogPrintf("[snapshot] failed to generate coins stats\n");
6936 return false;
6937 }
6938
6939 // Assert that the deserialized chainstate contents match the expected
6940 // assumeutxo value.
6941 if (AssumeutxoHash{maybe_stats->hashSerialized} !=
6942 au_data.hash_serialized) {
6943 LogPrintf("[snapshot] bad snapshot content hash: expected %s, got %s\n",
6944 au_data.hash_serialized.ToString(),
6945 maybe_stats->hashSerialized.ToString());
6946 return false;
6947 }
6948
6949 snapshot_chainstate.m_chain.SetTip(*snapshot_start_block);
6950
6951 // The remainder of this function requires modifying data protected by
6952 // cs_main.
6953 LOCK(::cs_main);
6954
6955 // Fake various pieces of CBlockIndex state:
6956 CBlockIndex *index = nullptr;
6957
6958 // Don't make any modifications to the genesis block since it shouldn't be
6959 // necessary, and since the genesis block doesn't have normal flags like
6960 // BLOCK_VALID_SCRIPTS set.
6961 constexpr int AFTER_GENESIS_START{1};
6962
6963 for (int i = AFTER_GENESIS_START; i <= snapshot_chainstate.m_chain.Height();
6964 ++i) {
6965 index = snapshot_chainstate.m_chain[i];
6966
6967 m_blockman.m_dirty_blockindex.insert(index);
6968 // Changes to the block index will be flushed to disk after this call
6969 // returns in `ActivateSnapshot()`, when `MaybeRebalanceCaches()` is
6970 // called, since we've added a snapshot chainstate and therefore will
6971 // have to downsize the IBD chainstate, which will result in a call to
6972 // `FlushStateToDisk(ALWAYS)`.
6973 }
6974
6975 assert(index);
6976 assert(index == snapshot_start_block);
6977 index->nChainTx = au_data.nChainTx;
6978 snapshot_chainstate.setBlockIndexCandidates.insert(snapshot_start_block);
6979
6980 LogPrintf("[snapshot] validated snapshot (%.2f MB)\n",
6981 coins_cache.DynamicMemoryUsage() / (1000 * 1000));
6982 return true;
6983}
6984
6985// Currently, this function holds cs_main for its duration, which could be for
6986// multiple minutes due to the ComputeUTXOStats call. This hold is necessary
6987// because we need to avoid advancing the background validation chainstate
6988// farther than the snapshot base block - and this function is also invoked
6989// from within ConnectTip, i.e. from within ActivateBestChain, so cs_main is
6990// held anyway.
6991//
6992// Eventually (TODO), we could somehow separate this function's runtime from
6993// maintenance of the active chain, but that will either require
6994//
6995// (i) setting `m_disabled` immediately and ensuring all chainstate accesses go
6996// through IsUsable() checks, or
6997//
6998// (ii) giving each chainstate its own lock instead of using cs_main for
6999// everything.
7000SnapshotCompletionResult ChainstateManager::MaybeCompleteSnapshotValidation(
7001 std::function<void(bilingual_str)> shutdown_fnc) {
7003 if (m_ibd_chainstate.get() == &this->ActiveChainstate() ||
7004 !this->IsUsable(m_snapshot_chainstate.get()) ||
7005 !this->IsUsable(m_ibd_chainstate.get()) ||
7006 !m_ibd_chainstate->m_chain.Tip()) {
7007 // Nothing to do - this function only applies to the background
7008 // validation chainstate.
7010 }
7011 const int snapshot_tip_height = this->ActiveHeight();
7012 const int snapshot_base_height = *Assert(this->GetSnapshotBaseHeight());
7013 const CBlockIndex &index_new = *Assert(m_ibd_chainstate->m_chain.Tip());
7014
7015 if (index_new.nHeight < snapshot_base_height) {
7016 // Background IBD not complete yet.
7018 }
7019
7021 BlockHash snapshot_blockhash = *Assert(SnapshotBlockhash());
7022
7023 auto handle_invalid_snapshot = [&]() EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
7024 bilingual_str user_error = strprintf(
7025 _("%s failed to validate the -assumeutxo snapshot state. "
7026 "This indicates a hardware problem, or a bug in the software, or "
7027 "a bad software modification that allowed an invalid snapshot to "
7028 "be loaded. As a result of this, the node will shut down and "
7029 "stop using any state that was built on the snapshot, resetting "
7030 "the chain height from %d to %d. On the next restart, the node "
7031 "will resume syncing from %d without using any snapshot data. "
7032 "Please report this incident to %s, including how you obtained "
7033 "the snapshot. The invalid snapshot chainstate will be left on "
7034 "disk in case it is helpful in diagnosing the issue that caused "
7035 "this error."),
7036 PACKAGE_NAME, snapshot_tip_height, snapshot_base_height,
7037 snapshot_base_height, PACKAGE_BUGREPORT);
7038
7039 LogPrintf("[snapshot] !!! %s\n", user_error.original);
7040 LogPrintf("[snapshot] deleting snapshot, reverting to validated chain, "
7041 "and stopping node\n");
7042
7043 m_active_chainstate = m_ibd_chainstate.get();
7044 m_snapshot_chainstate->m_disabled = true;
7045 assert(!this->IsUsable(m_snapshot_chainstate.get()));
7046 assert(this->IsUsable(m_ibd_chainstate.get()));
7047
7048 auto rename_result = m_snapshot_chainstate->InvalidateCoinsDBOnDisk();
7049 if (!rename_result) {
7050 user_error = strprintf(Untranslated("%s\n%s"), user_error,
7051 util::ErrorString(rename_result));
7052 }
7053
7054 shutdown_fnc(user_error);
7055 };
7056
7057 if (index_new.GetBlockHash() != snapshot_blockhash) {
7058 LogPrintf(
7059 "[snapshot] supposed base block %s does not match the "
7060 "snapshot base block %s (height %d). Snapshot is not valid.\n",
7061 index_new.ToString(), snapshot_blockhash.ToString(),
7062 snapshot_base_height);
7063 handle_invalid_snapshot();
7065 }
7066
7067 assert(index_new.nHeight == snapshot_base_height);
7068
7069 int curr_height = m_ibd_chainstate->m_chain.Height();
7070
7071 assert(snapshot_base_height == curr_height);
7072 assert(snapshot_base_height == index_new.nHeight);
7073 assert(this->IsUsable(m_snapshot_chainstate.get()));
7074 assert(this->GetAll().size() == 2);
7075
7076 CCoinsViewDB &ibd_coins_db = m_ibd_chainstate->CoinsDB();
7077 m_ibd_chainstate->ForceFlushStateToDisk();
7078
7079 const auto &maybe_au_data =
7080 this->GetParams().AssumeutxoForHeight(curr_height);
7081 if (!maybe_au_data) {
7082 LogPrintf("[snapshot] assumeutxo data not found for height "
7083 "(%d) - refusing to validate snapshot\n",
7084 curr_height);
7085 handle_invalid_snapshot();
7087 }
7088
7089 const AssumeutxoData &au_data = *maybe_au_data;
7090 std::optional<CCoinsStats> maybe_ibd_stats;
7091 LogPrintf(
7092 "[snapshot] computing UTXO stats for background chainstate to validate "
7093 "snapshot - this could take a few minutes\n");
7094 try {
7095 maybe_ibd_stats =
7096 ComputeUTXOStats(CoinStatsHashType::HASH_SERIALIZED, &ibd_coins_db,
7098 } catch (StopHashingException const &) {
7100 }
7101
7102 if (!maybe_ibd_stats) {
7103 LogPrintf(
7104 "[snapshot] failed to generate stats for validation coins db\n");
7105 // While this isn't a problem with the snapshot per se, this condition
7106 // prevents us from validating the snapshot, so we should shut down and
7107 // let the user handle the issue manually.
7108 handle_invalid_snapshot();
7110 }
7111 const auto &ibd_stats = *maybe_ibd_stats;
7112
7113 // Compare the background validation chainstate's UTXO set hash against the
7114 // hard-coded assumeutxo hash we expect.
7115 //
7116 // TODO: For belt-and-suspenders, we could cache the UTXO set
7117 // hash for the snapshot when it's loaded in its chainstate's leveldb. We
7118 // could then reference that here for an additional check.
7119 if (AssumeutxoHash{ibd_stats.hashSerialized} != au_data.hash_serialized) {
7120 LogPrintf("[snapshot] hash mismatch: actual=%s, expected=%s\n",
7121 ibd_stats.hashSerialized.ToString(),
7122 au_data.hash_serialized.ToString());
7123 handle_invalid_snapshot();
7125 }
7126
7127 LogPrintf("[snapshot] snapshot beginning at %s has been fully validated\n",
7128 snapshot_blockhash.ToString());
7129
7130 m_ibd_chainstate->m_disabled = true;
7131 this->MaybeRebalanceCaches();
7132
7134}
7135
7137 LOCK(::cs_main);
7138 assert(m_active_chainstate);
7139 return *m_active_chainstate;
7140}
7141
7143 LOCK(::cs_main);
7144 return m_snapshot_chainstate &&
7145 m_active_chainstate == m_snapshot_chainstate.get();
7146}
7147void ChainstateManager::MaybeRebalanceCaches() {
7149 bool ibd_usable = this->IsUsable(m_ibd_chainstate.get());
7150 bool snapshot_usable = this->IsUsable(m_snapshot_chainstate.get());
7151 assert(ibd_usable || snapshot_usable);
7152
7153 if (ibd_usable && !snapshot_usable) {
7154 LogPrintf("[snapshot] allocating all cache to the IBD chainstate\n");
7155 // Allocate everything to the IBD chainstate.
7156 m_ibd_chainstate->ResizeCoinsCaches(m_total_coinstip_cache,
7158 } else if (snapshot_usable && !ibd_usable) {
7159 // If background validation has completed and snapshot is our active
7160 // chain...
7161 LogPrintf(
7162 "[snapshot] allocating all cache to the snapshot chainstate\n");
7163 // Allocate everything to the snapshot chainstate.
7164 m_snapshot_chainstate->ResizeCoinsCaches(m_total_coinstip_cache,
7166 } else if (ibd_usable && snapshot_usable) {
7167 // If both chainstates exist, determine who needs more cache based on
7168 // IBD status.
7169 //
7170 // Note: shrink caches first so that we don't inadvertently overwhelm
7171 // available memory.
7172 if (IsInitialBlockDownload()) {
7173 m_ibd_chainstate->ResizeCoinsCaches(m_total_coinstip_cache * 0.05,
7174 m_total_coinsdb_cache * 0.05);
7175 m_snapshot_chainstate->ResizeCoinsCaches(
7177 } else {
7178 m_snapshot_chainstate->ResizeCoinsCaches(
7180 m_ibd_chainstate->ResizeCoinsCaches(m_total_coinstip_cache * 0.95,
7181 m_total_coinsdb_cache * 0.95);
7182 }
7183 }
7184}
7185
7186void ChainstateManager::ResetChainstates() {
7187 m_ibd_chainstate.reset();
7188 m_snapshot_chainstate.reset();
7189 m_active_chainstate = nullptr;
7190}
7191
7198 if (!opts.check_block_index.has_value()) {
7199 opts.check_block_index =
7200 opts.config.GetChainParams().DefaultConsistencyChecks();
7201 }
7202
7203 if (!opts.minimum_chain_work.has_value()) {
7204 opts.minimum_chain_work = UintToArith256(
7205 opts.config.GetChainParams().GetConsensus().nMinimumChainWork);
7206 }
7207 if (!opts.assumed_valid_block.has_value()) {
7208 opts.assumed_valid_block =
7209 opts.config.GetChainParams().GetConsensus().defaultAssumeValid;
7210 }
7211 Assert(opts.adjusted_time_callback);
7212 return std::move(opts);
7213}
7214
7216 Options options, node::BlockManager::Options blockman_options)
7217 : m_options{Flatten(std::move(options))},
7218 m_blockman{std::move(blockman_options)} {}
7219
7220bool ChainstateManager::DetectSnapshotChainstate(CTxMemPool *mempool) {
7221 assert(!m_snapshot_chainstate);
7222 std::optional<fs::path> path = node::FindSnapshotChainstateDir();
7223 if (!path) {
7224 return false;
7225 }
7226 std::optional<BlockHash> base_blockhash =
7228 if (!base_blockhash) {
7229 return false;
7230 }
7231 LogPrintf("[snapshot] detected active snapshot chainstate (%s) - loading\n",
7232 fs::PathToString(*path));
7233
7234 this->ActivateExistingSnapshot(*base_blockhash);
7235 return true;
7236}
7237
7238Chainstate &
7239ChainstateManager::ActivateExistingSnapshot(BlockHash base_blockhash) {
7240 assert(!m_snapshot_chainstate);
7241 m_snapshot_chainstate = std::make_unique<Chainstate>(nullptr, m_blockman,
7242 *this, base_blockhash);
7243 LogPrintf("[snapshot] switching active chainstate to %s\n",
7244 m_snapshot_chainstate->ToString());
7245
7246 // Mempool is empty at this point because we're still in IBD.
7247 Assert(m_active_chainstate->m_mempool->size() == 0);
7248 Assert(!m_snapshot_chainstate->m_mempool);
7249 m_snapshot_chainstate->m_mempool = m_active_chainstate->m_mempool;
7250 m_active_chainstate->m_mempool = nullptr;
7251 m_active_chainstate = m_snapshot_chainstate.get();
7252 return *m_snapshot_chainstate;
7253}
7254
7258 // Should never be called on a non-snapshot chainstate.
7259 assert(cs.m_from_snapshot_blockhash);
7260 auto storage_path_maybe = cs.CoinsDB().StoragePath();
7261 // Should never be called with a non-existent storage path.
7262 assert(storage_path_maybe);
7263 return *storage_path_maybe;
7264}
7265
7266util::Result<void> Chainstate::InvalidateCoinsDBOnDisk() {
7267 fs::path snapshot_datadir = GetSnapshotCoinsDBPath(*this);
7268
7269 // Coins views no longer usable.
7270 m_coins_views.reset();
7271
7272 auto invalid_path = snapshot_datadir + "_INVALID";
7273 std::string dbpath = fs::PathToString(snapshot_datadir);
7274 std::string target = fs::PathToString(invalid_path);
7275 LogPrintf("[snapshot] renaming snapshot datadir %s to %s\n", dbpath,
7276 target);
7277
7278 // The invalid snapshot datadir is simply moved and not deleted because we
7279 // may want to do forensics later during issue investigation. The user is
7280 // instructed accordingly in MaybeCompleteSnapshotValidation().
7281 try {
7282 fs::rename(snapshot_datadir, invalid_path);
7283 } catch (const fs::filesystem_error &e) {
7284 auto src_str = fs::PathToString(snapshot_datadir);
7285 auto dest_str = fs::PathToString(invalid_path);
7286
7287 LogPrintf("%s: error renaming file '%s' -> '%s': %s\n", __func__,
7288 src_str, dest_str, e.what());
7289 return util::Error{strprintf(_("Rename of '%s' -> '%s' failed. "
7290 "You should resolve this by manually "
7291 "moving or deleting the invalid "
7292 "snapshot directory %s, otherwise you "
7293 "will encounter the same error again "
7294 "on the next startup."),
7295 src_str, dest_str, src_str)};
7296 }
7297 return {};
7298}
7299
7300bool ChainstateManager::DeleteSnapshotChainstate() {
7302 Assert(m_snapshot_chainstate);
7303 Assert(m_ibd_chainstate);
7304
7305 fs::path snapshot_datadir =
7307 if (!DeleteCoinsDBFromDisk(snapshot_datadir, /*is_snapshot=*/true)) {
7308 LogPrintf("Deletion of %s failed. Please remove it manually to "
7309 "continue reindexing.\n",
7310 fs::PathToString(snapshot_datadir));
7311 return false;
7312 }
7313 m_active_chainstate = m_ibd_chainstate.get();
7314 m_active_chainstate->m_mempool = m_snapshot_chainstate->m_mempool;
7315 m_snapshot_chainstate.reset();
7316 return true;
7317}
7318
7319ChainstateRole Chainstate::GetRole() const {
7320 if (m_chainman.GetAll().size() <= 1) {
7322 }
7323 return (this != &m_chainman.ActiveChainstate())
7326}
7327const CBlockIndex *ChainstateManager::GetSnapshotBaseBlock() const {
7328 return m_active_chainstate ? m_active_chainstate->SnapshotBase() : nullptr;
7329}
7330
7331std::optional<int> ChainstateManager::GetSnapshotBaseHeight() const {
7332 const CBlockIndex *base = this->GetSnapshotBaseBlock();
7333 return base ? std::make_optional(base->nHeight) : std::nullopt;
7334}
7335
7336void ChainstateManager::RecalculateBestHeader() {
7338 m_best_header = ActiveChain().Tip();
7339 for (auto &entry : m_blockman.m_block_index) {
7340 if (!(entry.second.nStatus.isInvalid()) &&
7341 m_best_header->nChainWork < entry.second.nChainWork) {
7342 m_best_header = &entry.second;
7343 }
7344 }
7345}
7346
7347bool ChainstateManager::ValidatedSnapshotCleanup() {
7349 auto get_storage_path = [](auto &chainstate) EXCLUSIVE_LOCKS_REQUIRED(
7350 ::cs_main) -> std::optional<fs::path> {
7351 if (!(chainstate && chainstate->HasCoinsViews())) {
7352 return {};
7353 }
7354 return chainstate->CoinsDB().StoragePath();
7355 };
7356 std::optional<fs::path> ibd_chainstate_path_maybe =
7357 get_storage_path(m_ibd_chainstate);
7358 std::optional<fs::path> snapshot_chainstate_path_maybe =
7359 get_storage_path(m_snapshot_chainstate);
7360
7361 if (!this->IsSnapshotValidated()) {
7362 // No need to clean up.
7363 return false;
7364 }
7365 // If either path doesn't exist, that means at least one of the chainstates
7366 // is in-memory, in which case we can't do on-disk cleanup. You'd better be
7367 // in a unittest!
7368 if (!ibd_chainstate_path_maybe || !snapshot_chainstate_path_maybe) {
7369 LogPrintf("[snapshot] snapshot chainstate cleanup cannot happen with "
7370 "in-memory chainstates. You are testing, right?\n");
7371 return false;
7372 }
7373
7374 const auto &snapshot_chainstate_path = *snapshot_chainstate_path_maybe;
7375 const auto &ibd_chainstate_path = *ibd_chainstate_path_maybe;
7376
7377 // Since we're going to be moving around the underlying leveldb filesystem
7378 // content for each chainstate, make sure that the chainstates (and their
7379 // constituent CoinsViews members) have been destructed first.
7380 //
7381 // The caller of this method will be responsible for reinitializing
7382 // chainstates if they want to continue operation.
7383 this->ResetChainstates();
7384
7385 // No chainstates should be considered usable.
7386 assert(this->GetAll().size() == 0);
7387
7388 LogPrintf("[snapshot] deleting background chainstate directory (now "
7389 "unnecessary) (%s)\n",
7390 fs::PathToString(ibd_chainstate_path));
7391
7392 fs::path tmp_old{ibd_chainstate_path + "_todelete"};
7393
7394 auto rename_failed_abort = [](fs::path p_old, fs::path p_new,
7395 const fs::filesystem_error &err) {
7396 LogPrintf("Error renaming file (%s): %s\n", fs::PathToString(p_old),
7397 err.what());
7399 "Rename of '%s' -> '%s' failed. "
7400 "Cannot clean up the background chainstate leveldb directory.",
7401 fs::PathToString(p_old), fs::PathToString(p_new)));
7402 };
7403
7404 try {
7405 fs::rename(ibd_chainstate_path, tmp_old);
7406 } catch (const fs::filesystem_error &e) {
7407 rename_failed_abort(ibd_chainstate_path, tmp_old, e);
7408 throw;
7409 }
7410
7411 LogPrintf("[snapshot] moving snapshot chainstate (%s) to "
7412 "default chainstate directory (%s)\n",
7413 fs::PathToString(snapshot_chainstate_path),
7414 fs::PathToString(ibd_chainstate_path));
7415
7416 try {
7417 fs::rename(snapshot_chainstate_path, ibd_chainstate_path);
7418 } catch (const fs::filesystem_error &e) {
7419 rename_failed_abort(snapshot_chainstate_path, ibd_chainstate_path, e);
7420 throw;
7421 }
7422
7423 if (!DeleteCoinsDBFromDisk(tmp_old, /*is_snapshot=*/false)) {
7424 // No need to AbortNode because once the unneeded bg chainstate data is
7425 // moved, it will not interfere with subsequent initialization.
7426 LogPrintf("Deletion of %s failed. Please remove it manually, as the "
7427 "directory is now unnecessary.\n",
7428 fs::PathToString(tmp_old));
7429 } else {
7430 LogPrintf("[snapshot] deleted background chainstate directory (%s)\n",
7431 fs::PathToString(ibd_chainstate_path));
7432 }
7433 return true;
7434}
7435
7436Chainstate &ChainstateManager::GetChainstateForIndexing() {
7437 // We can't always return `m_ibd_chainstate` because after background
7438 // validation has completed,
7439 // `m_snapshot_chainstate == m_active_chainstate`, but it can be indexed.
7440 return (this->GetAll().size() > 1) ? *m_ibd_chainstate
7441 : *m_active_chainstate;
7442}
7443
7444std::pair<int, int>
7445ChainstateManager::GetPruneRange(const Chainstate &chainstate,
7446 int last_height_can_prune) {
7447 if (chainstate.m_chain.Height() <= 0) {
7448 return {0, 0};
7449 }
7450 int prune_start{0};
7451
7452 if (this->GetAll().size() > 1 &&
7453 m_snapshot_chainstate.get() == &chainstate) {
7454 // Leave the blocks in the background IBD chain alone if we're pruning
7455 // the snapshot chain.
7456 prune_start = *Assert(GetSnapshotBaseHeight()) + 1;
7457 }
7458
7459 int max_prune = std::max<int>(0, chainstate.m_chain.Height() -
7460 static_cast<int>(MIN_BLOCKS_TO_KEEP));
7461
7462 // last block to prune is the lesser of (caller-specified height,
7463 // MIN_BLOCKS_TO_KEEP from the tip)
7464 //
7465 // While you might be tempted to prune the background chainstate more
7466 // aggressively (i.e. fewer MIN_BLOCKS_TO_KEEP), this won't work with index
7467 // building - specifically blockfilterindex requires undo data, and if
7468 // we don't maintain this trailing window, we hit indexing failures.
7469 int prune_end = std::min(last_height_can_prune, max_prune);
7470
7471 return {prune_start, prune_end};
7472}
bool IsDAAEnabled(const Consensus::Params &params, int nHeight)
Definition: activation.cpp:24
bool IsUAHFenabled(const Consensus::Params &params, int nHeight)
Definition: activation.cpp:11
static bool IsPhononEnabled(const Consensus::Params &params, int32_t nHeight)
Definition: activation.cpp:65
static bool IsGravitonEnabled(const Consensus::Params &params, int32_t nHeight)
Definition: activation.cpp:51
bool IsMagneticAnomalyEnabled(const Consensus::Params &params, int32_t nHeight)
Check if Nov 15, 2018 HF has activated using block height.
Definition: activation.cpp:37
bool MoneyRange(const Amount nValue)
Definition: amount.h:166
static constexpr Amount SATOSHI
Definition: amount.h:143
static constexpr Amount COIN
Definition: amount.h:144
ArgsManager gArgs
Definition: args.cpp:40
arith_uint256 UintToArith256(const uint256 &a)
int flags
Definition: bitcoin-tx.cpp:542
@ CHAIN
Outputs do not overspend inputs, no double spends, coinbase output ok, no immature coinbase spends,...
@ TRANSACTIONS
Only first tx is coinbase, 2 <= coinbase input script length <= 100, transactions valid,...
@ SCRIPTS
Scripts & signatures ok.
@ TREE
All parent headers found, difficulty matches, timestamp >= median previous, checkpoint.
arith_uint256 GetBlockProof(const CBlockIndex &block)
Definition: chain.cpp:74
int64_t GetBlockProofEquivalentTime(const CBlockIndex &to, const CBlockIndex &from, const CBlockIndex &tip, const Consensus::Params &params)
Return the time it would take to redo the work difference between from and to, assuming the current h...
Definition: chain.cpp:89
const CBlockIndex * LastCommonAncestor(const CBlockIndex *pa, const CBlockIndex *pb)
Find the last common ancestor two blocks have.
Definition: chain.cpp:112
bool AreOnTheSameFork(const CBlockIndex *pa, const CBlockIndex *pb)
Check if two block index are on the same fork.
Definition: chain.cpp:136
#define Assert(val)
Identity function.
Definition: check.h:84
#define Assume(val)
Assume is the identity function.
Definition: check.h:97
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: args.h:217
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
Definition: args.cpp:490
fs::path GetBlocksDirPath() const
Get blocks directory path.
Definition: args.cpp:293
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: args.cpp:520
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:528
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Definition: streams.h:570
std::string ToString() const
Definition: hash_type.h:28
uint64_t getExcessiveBlockSize() const
Definition: validation.h:155
BlockValidationOptions withCheckPoW(bool _checkPoW=true) const
Definition: validation.h:140
BlockValidationOptions withCheckMerkleRoot(bool _checkMerkleRoot=true) const
Definition: validation.h:147
BlockValidationOptions(const Config &config)
Definition: validation.cpp:122
bool shouldValidatePoW() const
Definition: validation.h:153
bool shouldValidateMerkleRoot() const
Definition: validation.h:154
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:23
BlockHash GetHash() const
Definition: block.cpp:11
NodeSeconds Time() const
Definition: block.h:53
uint32_t nBits
Definition: block.h:30
BlockHash hashPrevBlock
Definition: block.h:27
int64_t GetBlockTime() const
Definition: block.h:57
int32_t nVersion
Definition: block.h:26
uint256 hashMerkleRoot
Definition: block.h:28
Definition: block.h:60
bool m_checked_merkle_root
Definition: block.h:69
std::vector< CTransactionRef > vtx
Definition: block.h:63
bool fChecked
Definition: block.h:67
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:25
bool IsValid(enum BlockValidity nUpTo=BlockValidity::TRANSACTIONS) const EXCLUSIVE_LOCKS_REQUIRED(
Check whether this block index entry is valid up to the passed validity level.
Definition: blockindex.h:191
std::string ToString() const
Definition: blockindex.cpp:30
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: blockindex.h:32
int64_t GetHeaderReceivedTime() const
Definition: blockindex.h:164
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
Definition: blockindex.h:51
const BlockHash * phashBlock
pointer to the hash of the block, if any.
Definition: blockindex.h:29
int64_t GetChainTxCount() const
Get the number of transaction in the chain so far.
Definition: blockindex.h:138
bool HaveNumChainTxs() const
Check whether this block and all previous blocks back to the genesis block or an assumeutxo snapshot ...
Definition: blockindex.h:154
uint32_t nTime
Definition: blockindex.h:76
int32_t nSequenceId
(memory only) Sequential id assigned to distinguish order in which blocks are received.
Definition: blockindex.h:82
int64_t GetReceivedTimeDiff() const
Definition: blockindex.h:166
int64_t GetBlockTime() const
Definition: blockindex.h:160
int64_t GetMedianTimePast() const
Definition: blockindex.h:172
FlatFilePos GetUndoPos() const EXCLUSIVE_LOCKS_REQUIRED(
Definition: blockindex.h:107
CBlockIndex * pskip
pointer to the index of some further predecessor of this block
Definition: blockindex.h:35
unsigned int nTx
Number of transactions in this block.
Definition: blockindex.h:55
bool RaiseValidity(enum BlockValidity nUpTo) EXCLUSIVE_LOCKS_REQUIRED(
Raise the validity level of this block index entry.
Definition: blockindex.h:199
int32_t nVersion
block header
Definition: blockindex.h:74
int64_t nTimeReceived
(memory only) block header metadata
Definition: blockindex.h:85
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
Definition: blockindex.cpp:62
BlockHash GetBlockHash() const
Definition: blockindex.h:130
unsigned int nSize
Size of this block.
Definition: blockindex.h:60
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: blockindex.h:38
unsigned int nChainTx
(memory only) Number of transactions in the chain up to and including this block.
Definition: blockindex.h:68
Undo information for a CBlock.
Definition: undo.h:73
std::vector< CTxUndo > vtxundo
Definition: undo.h:76
Non-refcounted RAII wrapper around a FILE* that implements a ring buffer to deserialize from.
Definition: streams.h:671
bool SetLimit(uint64_t nPos=std::numeric_limits< uint64_t >::max())
Prevent reading beyond a certain position.
Definition: streams.h:808
uint64_t GetPos() const
return the current reading position
Definition: streams.h:787
void FindByte(std::byte byte)
search for a given byte in the stream, and remain positioned on it
Definition: streams.h:823
void SkipTo(const uint64_t file_pos)
Move the read position ahead in the stream to the given position.
Definition: streams.h:779
bool SetPos(uint64_t nPos)
rewind to a given reading position
Definition: streams.h:790
bool eof() const
check whether we're at the end of the source file
Definition: streams.h:766
An in-memory indexed chain of blocks.
Definition: chain.h:134
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:150
void SetTip(CBlockIndex &block)
Set/initialize a chain with a given tip.
Definition: chain.cpp:8
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
Definition: chain.h:143
CBlockIndex * Next(const CBlockIndex *pindex) const
Find the successor of a block in this chain, or nullptr if the given index is not found or is the tip...
Definition: chain.h:174
int Height() const
Return the maximal height in the chain.
Definition: chain.h:186
const CBlockIndex * FindFork(const CBlockIndex *pindex) const
Find the last common block between this chain and a block index entry.
Definition: chain.cpp:49
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
Definition: chain.h:166
CBlockLocator GetLocator() const
Return a CBlockLocator that refers to the tip of this chain.
Definition: chain.cpp:45
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:86
const CBlock & GenesisBlock() const
Definition: chainparams.h:112
std::vector< int > GetAvailableSnapshotHeights() const
const CMessageHeader::MessageMagic & DiskMagic() const
Definition: chainparams.h:99
const ChainTxData & TxData() const
Definition: chainparams.h:158
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:98
std::optional< AssumeutxoData > AssumeutxoForHeight(int height) const
Definition: chainparams.h:147
const CCheckpointData & Checkpoints() const
Definition: chainparams.h:145
RAII-style controller object for a CCheckQueue that guarantees the passed queue is finished before co...
Definition: checkqueue.h:198
void Add(std::vector< T > &&vChecks)
Definition: checkqueue.h:224
Queue for verifications that have to be performed.
Definition: checkqueue.h:28
void SetBackend(CCoinsView &viewIn)
Definition: coins.cpp:47
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:221
void AddCoin(const COutPoint &outpoint, Coin coin, bool possible_overwrite)
Add a coin.
Definition: coins.cpp:104
BlockHash GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: coins.cpp:214
bool SpendCoin(const COutPoint &outpoint, Coin *moveto=nullptr)
Spend a coin.
Definition: coins.cpp:172
void Uncache(const COutPoint &outpoint)
Removes the UTXO with the given outpoint from the cache, if it is not modified.
Definition: coins.cpp:330
void SetBestBlock(const BlockHash &hashBlock)
Definition: coins.cpp:221
unsigned int GetCacheSize() const
Calculate the size of the cache (in number of transaction outputs)
Definition: coins.cpp:342
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: coins.cpp:95
bool HaveCoinInCache(const COutPoint &outpoint) const
Check if we have the given utxo already loaded in this cache.
Definition: coins.cpp:209
bool Flush()
Push the modifications applied to this cache to its base and wipe local state.
Definition: coins.cpp:301
size_t DynamicMemoryUsage() const
Calculate the size of the cache (in bytes)
Definition: coins.cpp:67
void EmplaceCoinInternalDANGER(COutPoint &&outpoint, Coin &&coin)
Emplace a coin into cacheCoins without performing any checks, marking the emplaced coin as dirty.
Definition: coins.cpp:148
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition: coins.cpp:204
const Coin & AccessCoin(const COutPoint &output) const
Return a reference to Coin in the cache, or coinEmpty if not found.
Definition: coins.cpp:196
CCoinsView backed by the coin database (chainstate/)
Definition: txdb.h:65
std::optional< fs::path > StoragePath()
Definition: txdb.h:92
void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Dynamically alter the underlying leveldb cache size.
Definition: txdb.cpp:82
Abstract view on the open txout dataset.
Definition: coins.h:163
virtual bool GetCoin(const COutPoint &outpoint, Coin &coin) const
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: coins.cpp:13
CCoinsView that brings transactions from a mempool into view.
Definition: txmempool.h:636
Fee rate in satoshis per kilobyte: Amount / kB.
Definition: feerate.h:21
void TransactionAddedToMempool(const CTransactionRef &, std::shared_ptr< const std::vector< Coin > >, uint64_t mempool_sequence)
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)
void BlockConnected(ChainstateRole, const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
void BlockDisconnected(const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
void BlockChecked(const CBlock &, const BlockValidationState &)
void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr< const CBlock > &)
void ChainStateFlushed(ChainstateRole, const CBlockLocator &)
static constexpr size_t MESSAGE_START_SIZE
Definition: protocol.h:36
void insert(Span< const uint8_t > vKey)
Definition: bloom.cpp:215
bool contains(Span< const uint8_t > vKey) const
Definition: bloom.cpp:249
Closure representing one script verification.
Definition: validation.h:526
bool operator()()
ScriptError GetScriptError() const
Definition: validation.h:557
ScriptExecutionMetrics GetScriptExecutionMetrics() const
Definition: validation.h:559
uint32_t nFlags
Definition: validation.h:531
TxSigCheckLimiter * pTxLimitSigChecks
Definition: validation.h:536
ScriptExecutionMetrics metrics
Definition: validation.h:534
CTxOut m_tx_out
Definition: validation.h:528
bool cacheStore
Definition: validation.h:532
ScriptError error
Definition: validation.h:533
PrecomputedTransactionData txdata
Definition: validation.h:535
const CTransaction * ptxTo
Definition: validation.h:529
unsigned int nIn
Definition: validation.h:530
CheckInputsLimiter * pBlockLimitSigChecks
Definition: validation.h:537
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: mempool_entry.h:65
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:214
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:310
void AddTransactionsUpdated(unsigned int n)
Definition: txmempool.cpp:145
const int64_t m_max_size_bytes
Definition: txmempool.h:347
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:760
void clear()
Definition: txmempool.cpp:357
void SetLoadTried(bool load_tried)
Set whether or not we've made an attempt to load the mempool (regardless of whether the attempt was s...
Definition: txmempool.cpp:972
CScript scriptPubKey
Definition: transaction.h:131
Amount nValue
Definition: transaction.h:130
Restore the UTXO in a Coin at a given COutPoint.
Definition: undo.h:62
std::vector< Coin > vprevout
Definition: undo.h:65
VerifyDBResult VerifyDB(Chainstate &chainstate, CCoinsView &coinsview, int nCheckLevel, int nCheckDepth) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
kernel::Notifications & m_notifications
Definition: validation.h:628
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:705
bool IsBlockAvalancheFinalized(const CBlockIndex *pindex) const EXCLUSIVE_LOCKS_REQUIRED(!cs_avalancheFinalizedBlockIndex)
Checks if a block is finalized by avalanche voting.
const std::optional< BlockHash > m_from_snapshot_blockhash
The blockhash which is the base of the snapshot this chainstate was created from.
Definition: validation.h:812
void InitCoinsCache(size_t cache_size_bytes) EXCLUSIVE_LOCKS_REQUIRED(bool CanFlushToDisk() const EXCLUSIVE_LOCKS_REQUIRED(
Initialize the in-memory coins cache (to be done after the health of the on-disk database is verified...
Definition: validation.h:797
void CheckForkWarningConditionsOnNewFork(CBlockIndex *pindexNewForkTip) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
bool ActivateBestChain(BlockValidationState &state, std::shared_ptr< const CBlock > pblock=nullptr, avalanche::Processor *const avalanche=nullptr) EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex
Find the best known block, and make it the tip of the block chain.
Mutex m_chainstate_mutex
The ChainState Mutex.
Definition: validation.h:711
bool ConnectTip(BlockValidationState &state, BlockPolicyValidationState &blockPolicyState, CBlockIndex *pindexNew, const std::shared_ptr< const CBlock > &pblock, DisconnectedBlockTransactions &disconnectpool, const avalanche::Processor *const avalanche=nullptr, ChainstateRole chainstate_role=ChainstateRole::NORMAL) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Connect a new block to m_chain.
void UpdateFlags(CBlockIndex *pindex, CBlockIndex *&pindexReset, F f, C fChild, AC fAncestorWasChanged) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
CChain m_chain
The current chain of blockheaders we consult and build on.
Definition: validation.h:804
bool HasCoinsViews() const
Does this chainstate have a UTXO set attached?
Definition: validation.h:858
CTxMemPool * GetMempool()
Definition: validation.h:844
bool RollforwardBlock(const CBlockIndex *pindex, CCoinsViewCache &inputs) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Apply the effects of a block on the utxo cache, ignoring that it may already have been applied.
size_t m_coinstip_cache_size_bytes
The cache size of the in-memory coins view.
Definition: validation.h:864
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(
Definition: validation.h:831
bool LoadChainTip() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Update the chain tip based on database information, i.e.
size_t m_coinsdb_cache_size_bytes
The cache size of the on-disk coins view.
Definition: validation.h:861
void SetBlockFailureFlags(CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(voi ResetBlockFailureFlags)(CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Set invalidity status to all descendants of a block.
Definition: validation.h:981
void UnparkBlockImpl(CBlockIndex *pindex, bool fClearChildren) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
void CheckForkWarningConditions() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Chainstate(CTxMemPool *mempool, node::BlockManager &blockman, ChainstateManager &chainman, std::optional< BlockHash > from_snapshot_blockhash=std::nullopt)
void InvalidBlockFound(CBlockIndex *pindex, const BlockValidationState &state) EXCLUSIVE_LOCKS_REQUIRED(cs_main
bool ActivateBestChainStep(BlockValidationState &state, CBlockIndex *pindexMostWork, const std::shared_ptr< const CBlock > &pblock, bool &fInvalidFound, const avalanche::Processor *const avalanche=nullptr, ChainstateRole=ChainstateRole::NORMAL) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Try to make some progress towards making pindexMostWork the active block.
Mutex cs_avalancheFinalizedBlockIndex
Definition: validation.h:735
void ForceFlushStateToDisk()
Unconditionally flush all changes to disk.
bool LoadGenesisBlock()
Ensures we have a genesis block in the block tree, possibly writing one to disk.
void UpdateTip(const CBlockIndex *pindexNew) EXCLUSIVE_LOCKS_REQUIRED(std::chrono::microsecond m_last_write)
Check warning conditions and do some notifications on new chain tip set.
Definition: validation.h:1097
void UnparkBlockAndChildren(CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Remove parked status from a block and its descendants.
CTxMemPool * m_mempool
Optional mempool that is kept in sync with the chain.
Definition: validation.h:715
void LoadMempool(const fs::path &load_path, fsbridge::FopenFn mockable_fopen_function=fsbridge::fopen)
Load the persisted mempool from disk.
CCoinsViewDB & CoinsDB() EXCLUSIVE_LOCKS_REQUIRED(
Definition: validation.h:838
std::chrono::microseconds m_last_flush
Definition: validation.h:1098
bool DisconnectTip(BlockValidationState &state, DisconnectedBlockTransactions *disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Disconnect m_chain's tip.
bool UnwindBlock(BlockValidationState &state, CBlockIndex *pindex, bool invalidate) EXCLUSIVE_LOCKS_REQUIRED(m_chainstate_mutex
bool InvalidateBlock(BlockValidationState &state, CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex
Mark a block as invalid.
ChainstateManager & m_chainman
The chainstate manager that owns this chainstate.
Definition: validation.h:767
std::unique_ptr< CoinsViews > m_coins_views
Manages the UTXO set, which is a reflection of the contents of m_chain.
Definition: validation.h:719
const CBlockIndex *SnapshotBase() EXCLUSIVE_LOCKS_REQUIRED(std::set< CBlockIndex *, CBlockIndexWorkComparator > setBlockIndexCandidates
The base of the snapshot this chainstate was created from.
Definition: validation.h:819
CRollingBloomFilter m_filterParkingPoliciesApplied
Filter to prevent parking a block due to block policies more than once.
Definition: validation.h:750
bool ReplayBlocks()
Replay blocks that aren't fully applied to the database.
bool AvalancheFinalizeBlock(CBlockIndex *pindex, avalanche::Processor &avalanche) EXCLUSIVE_LOCKS_REQUIRED(!cs_avalancheFinalizedBlockIndex)
Mark a block as finalized by avalanche.
void PruneBlockIndexCandidates()
Delete all entries in setBlockIndexCandidates that are worse than the current tip.
DisconnectResult DisconnectBlock(const CBlock &block, const CBlockIndex *pindex, CCoinsViewCache &view) EXCLUSIVE_LOCKS_REQUIRED(boo ConnectBlock)(const CBlock &block, BlockValidationState &state, CBlockIndex *pindex, CCoinsViewCache &view, BlockValidationOptions options, Amount *blockFees=nullptr, bool fJustCheck=false) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Apply the effects of this block (with given index) on the UTXO set represented by coins.
Definition: validation.h:924
CBlockIndex const * m_best_fork_tip
Definition: validation.h:753
void TryAddBlockIndexCandidate(CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
void PruneAndFlush()
Prune blockfiles from the disk if necessary and then flush chainstate changes if we pruned.
bool ResizeCoinsCaches(size_t coinstip_size, size_t coinsdb_size) EXCLUSIVE_LOCKS_REQUIRED(bool FlushStateToDisk(BlockValidationState &state, FlushStateMode mode, int nManualPruneHeight=0)
Resize the CoinsViews caches dynamically and flush state to disk.
node::BlockManager & m_blockman
Reference to a BlockManager instance which itself is shared across all Chainstate instances.
Definition: validation.h:762
ChainstateRole GetRole() const EXCLUSIVE_LOCKS_REQUIRED(void InitCoinsDB(size_t cache_size_bytes, bool in_memory, bool should_wipe, std::string leveldb_name="chainstate")
Return the current role of the chainstate.
CBlockIndex const * m_best_fork_base
Definition: validation.h:754
void InvalidChainFound(CBlockIndex *pindexNew) EXCLUSIVE_LOCKS_REQUIRED(cs_main
void UnparkBlock(CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Remove parked status from a block.
bool PreciousBlock(BlockValidationState &state, CBlockIndex *pindex, avalanche::Processor *const avalanche=nullptr) EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex
Mark a block as precious and reorganize.
void ClearAvalancheFinalizedBlock() EXCLUSIVE_LOCKS_REQUIRED(!cs_avalancheFinalizedBlockIndex)
Clear avalanche finalization.
void ClearBlockIndexCandidates() EXCLUSIVE_LOCKS_REQUIRED(const CBlockIndex * FindForkInGlobalIndex(const CBlockLocator &locator) const EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Find the last common block of this chain and a locator.
Definition: validation.cpp:127
CBlockIndex * FindMostWorkChain(std::vector< const CBlockIndex * > &blocksToReconcile, bool fAutoUnpark) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Return the tip of the chain with the most work in it, that isn't known to be invalid (it's however fa...
bool UpdateFlagsForBlock(CBlockIndex *pindexBase, CBlockIndex *pindex, F f) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
bool ParkBlock(BlockValidationState &state, CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex
Park a block.
CoinsCacheSizeState GetCoinsCacheSizeState() EXCLUSIVE_LOCKS_REQUIRED(CoinsCacheSizeState GetCoinsCacheSizeState(size_t max_coins_cache_size_bytes, size_t max_mempool_size_bytes) EXCLUSIVE_LOCKS_REQUIRED(std::string ToString() EXCLUSIVE_LOCKS_REQUIRED(RecursiveMutex * MempoolMutex() const LOCK_RETURNED(m_mempool -> cs)
Dictates whether we need to flush the cache to disk or not.
Definition: validation.h:1042
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:1158
SnapshotCompletionResult MaybeCompleteSnapshotValidation(std::function< void(bilingual_str)> shutdown_fnc=[](bilingual_str msg) { AbortNode(msg.original, msg);}) EXCLUSIVE_LOCKS_REQUIRED(const CBlockIndex *GetSnapshotBaseBlock() const EXCLUSIVE_LOCKS_REQUIRED(Chainstate ActiveChainstate)() const
Once the background validation chainstate has reached the height which is the base of the UTXO snapsh...
Definition: validation.h:1406
std::atomic< int32_t > nBlockSequenceId
Every received block is assigned a unique and increasing identifier, so we know which one to give pri...
Definition: validation.h:1312
bool DetectSnapshotChainstate(CTxMemPool *mempool) EXCLUSIVE_LOCKS_REQUIRED(void ResetChainstates() EXCLUSIVE_LOCKS_REQUIRED(bool DeleteSnapshotChainstate() EXCLUSIVE_LOCKS_REQUIRED(Chainstate &ActivateExistingSnapshot(BlockHash base_blockhash) EXCLUSIVE_LOCKS_REQUIRED(bool ValidatedSnapshotCleanup() EXCLUSIVE_LOCKS_REQUIRED(Chainstate &GetChainstateForIndexing() EXCLUSIVE_LOCKS_REQUIRED(std::pair< int, int > GetPruneRange(const Chainstate &chainstate, int last_height_can_prune) EXCLUSIVE_LOCKS_REQUIRED(std::optional< int > GetSnapshotBaseHeight() const EXCLUSIVE_LOCKS_REQUIRED(void RecalculateBestHeader() EXCLUSIVE_LOCKS_REQUIRED(boo DumpRecentHeadersTime)(const fs::path &filePath) const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
When starting up, search the datadir for a chainstate based on a UTXO snapshot that is in the process...
Definition: validation.h:1655
const Config & GetConfig() const
Definition: validation.h:1248
int64_t m_total_coinstip_cache
The total number of bytes available for us to use across all in-memory coins caches.
Definition: validation.h:1356
MempoolAcceptResult ProcessTransaction(const CTransactionRef &tx, bool test_accept=false) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Try to add a transaction to the memory pool.
int64_t m_total_coinsdb_cache
The total number of bytes available for us to use across all leveldb coins databases.
Definition: validation.h:1360
bool AcceptBlockHeader(const CBlockHeader &block, BlockValidationState &state, CBlockIndex **ppindex, bool min_pow_checked, const std::optional< CCheckpointData > &test_checkpoints=std::nullopt) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
If a block header hasn't already been seen, call CheckBlockHeader on it, ensure that it doesn't desce...
kernel::Notifications & GetNotifications() const
Definition: validation.h:1265
bool IsInitialBlockDownload() const
Check whether we are doing an initial block download (synchronizing from disk or network)
void ReceivedBlockTransactions(const CBlock &block, CBlockIndex *pindexNew, const FlatFilePos &pos) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Mark a block as having its data received and checked (up to BLOCK_VALID_TRANSACTIONS).
bool ShouldCheckBlockIndex() const
Definition: validation.h:1256
bool ProcessNewBlock(const std::shared_ptr< const CBlock > &block, bool force_processing, bool min_pow_checked, bool *new_block, avalanche::Processor *const avalanche=nullptr) LOCKS_EXCLUDED(cs_main)
Process an incoming block.
bool LoadRecentHeadersTime(const fs::path &filePath) EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Load the recent block headers reception time from a file.
void LoadExternalBlockFile(FILE *fileIn, FlatFilePos *dbp=nullptr, std::multimap< BlockHash, FlatFilePos > *blocks_with_unknown_parent=nullptr, avalanche::Processor *const avalanche=nullptr)
Import blocks from an external file.
std::optional< BlockHash > SnapshotBlockhash() const
bool IsSnapshotValidated() const EXCLUSIVE_LOCKS_REQUIRED(
Is there a snapshot in use and has it been fully validated?
Definition: validation.h:1442
bool IsUsable(const Chainstate *const pchainstate) const EXCLUSIVE_LOCKS_REQUIRED(
Return true if a chainstate is considered usable.
Definition: validation.h:1230
CBlockIndex * ActiveTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1413
bool BackgroundSyncInProgress() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
The state of a background sync (for net processing)
Definition: validation.h:1418
std::atomic< bool > m_cached_finished_ibd
Whether initial block download has ended and IsInitialBlockDownload should return false from now on.
Definition: validation.h:1305
bool PopulateAndValidateSnapshot(Chainstate &snapshot_chainstate, AutoFile &coins_file, const node::SnapshotMetadata &metadata)
Internal helper for ActivateSnapshot().
int ActiveHeight() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1410
bool IsSnapshotActive() const
bool AcceptBlock(const std::shared_ptr< const CBlock > &pblock, BlockValidationState &state, bool fRequested, const FlatFilePos *dbp, bool *fNewBlock, bool min_pow_checked) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Sufficiently validate a block for disk storage (and store on disk).
std::function< void()> snapshot_download_completed
Function to restart active indexes; set dynamically to avoid a circular dependency on base/index....
Definition: validation.h:1246
const CChainParams & GetParams() const
Definition: validation.h:1250
bool ProcessNewBlockHeaders(const std::vector< CBlockHeader > &block, bool min_pow_checked, BlockValidationState &state, const CBlockIndex **ppindex=nullptr, const std::optional< CCheckpointData > &test_checkpoints=std::nullopt) LOCKS_EXCLUDED(cs_main)
Process incoming block headers.
const Consensus::Params & GetConsensus() const
Definition: validation.h:1253
const arith_uint256 & MinimumChainWork() const
Definition: validation.h:1259
void CheckBlockIndex()
Make various assertions about the state of the block index.
util::Result< CBlockIndex * > ActivateSnapshot(AutoFile &coins_file, const node::SnapshotMetadata &metadata, bool in_memory)
Construct and activate a Chainstate on the basis of UTXO snapshot data.
const Options m_options
Definition: validation.h:1292
bool LoadBlockIndex() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Load the block tree and coins database from disk, initializing state if we're running with -reindex.
CChain & ActiveChain() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1407
void MaybeRebalanceCaches() EXCLUSIVE_LOCKS_REQUIRED(void ReportHeadersPresync(const arith_uint256 &work, int64_t height, int64_t timestamp)
Check to see if caches are out of balance and if so, call ResizeCoinsCaches() as needed.
arith_uint256 nLastPreciousChainwork
chainwork for the last block that preciousblock has been applied to.
Definition: validation.h:1317
const BlockHash & AssumedValidBlock() const
Definition: validation.h:1262
ChainstateManager(Options options, node::BlockManager::Options blockman_options)
Chainstate &InitializeChainstate(CTxMemPool *mempool) EXCLUSIVE_LOCKS_REQUIRED(std::vector< Chainstate * GetAll)()
Instantiate a new chainstate.
Definition: validation.h:1370
std::set< CBlockIndex * > m_failed_blocks
In order to efficiently track invalidity of headers, we keep the set of blocks which we tried to conn...
Definition: validation.h:1346
int32_t nBlockReverseSequenceId
Decreasing counter (used by subsequent preciousblock calls).
Definition: validation.h:1315
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
Definition: validation.h:1296
Simple class for regulating resource usage during CheckInputScripts (and CScriptCheck),...
Definition: validation.h:384
bool consume_and_check(int consumed)
Definition: validation.h:391
A UTXO entry.
Definition: coins.h:28
uint32_t GetHeight() const
Definition: coins.h:45
bool IsCoinBase() const
Definition: coins.h:46
CTxOut & GetTxOut()
Definition: coins.h:49
bool IsSpent() const
Definition: coins.h:47
CoinsViews(DBParams db_params, CoinsViewOptions options)
This constructor initializes CCoinsViewDB and CCoinsViewErrorCatcher instances, but it does not creat...
Definition: config.h:19
virtual const CChainParams & GetChainParams() const =0
void updateMempoolForReorg(Chainstate &active_chainstate, bool fAddToMempool, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Make mempool consistent after a reorg, by re-adding or recursively erasing disconnected block transac...
void addForBlock(const std::vector< CTransactionRef > &vtx, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(pool.cs)
void importMempool(CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(pool.cs)
Different type to mark Mutex at global scope.
Definition: sync.h:144
static RCUPtr acquire(T *&ptrIn)
Acquire ownership of some pointer.
Definition: rcu.h:103
The script cache is a map using a key/value element, that caches the success of executing a specific ...
Definition: scriptcache.h:25
static TxSigCheckLimiter getDisabled()
Definition: validation.h:412
bool IsValid() const
Definition: validation.h:119
std::string GetRejectReason() const
Definition: validation.h:123
std::string GetDebugMessage() const
Definition: validation.h:124
bool Error(const std::string &reject_reason)
Definition: validation.h:112
bool Invalid(Result result, const std::string &reject_reason="", const std::string &debug_message="")
Definition: validation.h:101
bool IsError() const
Definition: validation.h:121
Result GetResult() const
Definition: validation.h:122
std::string ToString() const
Definition: validation.h:125
bool IsInvalid() const
Definition: validation.h:120
256-bit unsigned big integer.
std::string ToString() const
Definition: uint256.h:80
bool IsNull() const
Definition: uint256.h:32
double getdouble() const
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:30
A base class defining functions for notifying about certain kernel events.
virtual void headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync)
virtual void warning(const std::string &warning)
virtual void progress(const bilingual_str &title, int progress_percent, bool resume_possible)
virtual void blockTip(SynchronizationState state, CBlockIndex &index)
Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-w...
Definition: blockstorage.h:104
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos) const
Functions for disk access for blocks.
RecursiveMutex cs_LastBlockFile
Definition: blockstorage.h:182
bool CheckBlockDataAvailability(const CBlockIndex &upper_block LIFETIMEBOUND, const CBlockIndex &lower_block LIFETIMEBOUND) EXCLUSIVE_LOCKS_REQUIRED(const CBlockIndex *GetFirstBlock(const CBlockIndex &upper_block LIFETIMEBOUND, std::function< bool(BlockStatus)> status_test, const CBlockIndex *lower_block=nullptr) const EXCLUSIVE_LOCKS_REQUIRED(boo m_have_pruned)
Check if all blocks in the [upper_block, lower_block] range have data available.
Definition: blockstorage.h:379
bool WriteUndoDataForBlock(const CBlockUndo &blockundo, BlockValidationState &state, CBlockIndex &block) EXCLUSIVE_LOCKS_REQUIRED(FlatFilePos SaveBlockToDisk(const CBlock &block, int nHeight, const FlatFilePos *dbp)
Store block on disk.
Definition: blockstorage.h:313
bool FlushChainstateBlockFile(int tip_height)
void FindFilesToPrune(std::set< int > &setFilesToPrune, int last_prune, const Chainstate &chain, ChainstateManager &chainman)
Prune block and undo files (blk???.dat and undo???.dat) so that the disk space used is less than a us...
CBlockIndex * LookupBlockIndex(const BlockHash &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
bool LoadingBlocks() const
Definition: blockstorage.h:326
bool UndoReadFromDisk(CBlockUndo &blockundo, const CBlockIndex &index) const
void UnlinkPrunedFiles(const std::set< int > &setFilesToPrune) const
Actually unlink the specified files.
bool WriteBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(bool LoadBlockIndexDB(const std::optional< BlockHash > &snapshot_blockhash) EXCLUSIVE_LOCKS_REQUIRED(void ScanAndUnlinkAlreadyPrunedFiles() EXCLUSIVE_LOCKS_REQUIRED(CBlockIndex * AddToBlockIndex(const CBlockHeader &block, CBlockIndex *&best_header) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Remove any pruned block & undo files that are still on disk.
Definition: blockstorage.h:286
std::set< CBlockIndex * > m_dirty_blockindex
Dirty block index entries.
Definition: blockstorage.h:220
bool m_check_for_pruning
Global flag to indicate we should check to see if there are block/undo files that should be deleted.
Definition: blockstorage.h:215
bool IsPruneMode() const
Whether running in -prune mode.
Definition: blockstorage.h:317
void FindFilesToPruneManual(std::set< int > &setFilesToPrune, int nManualPruneHeight, const Chainstate &chain, ChainstateManager &chainman)
Calculate the block/rev files to delete based on height specified by user with RPC command pruneblock...
std::optional< int > m_snapshot_height
The height of the base block of an assumeutxo snapshot, if one is in use.
Definition: blockstorage.h:261
std::vector< CBlockIndex * > GetAllBlockIndices() EXCLUSIVE_LOCKS_REQUIRED(std::multimap< CBlockIndex *, CBlockIndex * > m_blocks_unlinked
All pairs A->B, where A (or one of its ancestors) misses transactions, but B has transactions.
Definition: blockstorage.h:263
Metadata describing a serialized version of a UTXO set from which an assumeutxo Chainstate can be con...
Definition: utxo_snapshot.h:30
uint64_t m_coins_count
The number of coins in the UTXO set contained in this snapshot.
Definition: utxo_snapshot.h:41
BlockHash m_base_blockhash
The hash of the block that reflects the tip of the chain for the UTXO set contained in this snapshot.
Definition: utxo_snapshot.h:37
256-bit opaque blob.
Definition: uint256.h:129
static constexpr int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:38
const Coin & AccessByTxid(const CCoinsViewCache &view, const TxId &txid)
Utility function to find any unspent output with a given txid.
Definition: coins.cpp:397
void AddCoins(CCoinsViewCache &cache, const CTransaction &tx, int nHeight, bool check_for_overwrite)
Utility function to add all of a transaction's outputs to a cache.
Definition: coins.cpp:156
@ BLOCK_CHECKPOINT
the block failed to meet one of our checkpoints
@ BLOCK_HEADER_LOW_WORK
the block header may be on a too-little-work chain
@ BLOCK_INVALID_HEADER
invalid proof of work or time too old
@ BLOCK_CACHED_INVALID
this block was cached as being invalid and we didn't store the reason why
@ BLOCK_CONSENSUS
invalid by consensus rules (excluding any below reasons)
@ BLOCK_MISSING_PREV
We don't have the previous block the checked one is built on.
@ BLOCK_INVALID_PREV
A block this one builds on is invalid.
@ BLOCK_MUTATED
the block's data didn't match the data committed to by the PoW
@ BLOCK_TIME_FUTURE
block timestamp was > 2 hours in the future (or our clock is bad)
@ TX_MISSING_INPUTS
transaction was missing some of its inputs
@ TX_CHILD_BEFORE_PARENT
This tx outputs are already spent in the mempool.
@ TX_MEMPOOL_POLICY
violated mempool's fee/size/descendant/etc limits
@ TX_PACKAGE_RECONSIDERABLE
fails some policy, but might be acceptable if submitted in a (different) package
@ TX_PREMATURE_SPEND
transaction spends a coinbase too early, or violates locktime/sequence locks
@ TX_DUPLICATE
Tx already in mempool or in the chain.
@ TX_INPUTS_NOT_STANDARD
inputs failed policy rules
@ TX_CONFLICT
Tx conflicts with a finalized tx, i.e.
@ TX_NOT_STANDARD
otherwise didn't meet our local policy rules
@ TX_AVALANCHE_RECONSIDERABLE
fails some policy, but might be reconsidered by avalanche voting
@ TX_NO_MEMPOOL
this node does not have a mempool so can't validate the transaction
@ TX_CONSENSUS
invalid by consensus rules
static constexpr unsigned int LOCKTIME_VERIFY_SEQUENCE
Flags for nSequence and nLockTime locks.
Definition: consensus.h:38
static const uint64_t MAX_TX_SIZE
The maximum allowed size for a transaction, in bytes.
Definition: consensus.h:14
uint64_t GetMaxBlockSigChecksCount(uint64_t maxBlockSize)
Compute the maximum number of sigchecks that can be contained in a block given the MAXIMUM block size...
Definition: consensus.h:47
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:7
bool DeploymentActiveAfter(const CBlockIndex *pindexPrev, const Consensus::Params &params, Consensus::BuriedDeployment dep)
Determine if a deployment is active for the next block.
bool DeploymentActiveAt(const CBlockIndex &index, const Consensus::Params &params, Consensus::BuriedDeployment dep)
Determine if a deployment is active for this block.
DisconnectResult
volatile double sum
Definition: examples.cpp:10
bool RenameOver(fs::path src, fs::path dest)
Definition: fs_helpers.cpp:272
bool CheckDiskSpace(const fs::path &dir, uint64_t additional_bytes)
Definition: fs_helpers.cpp:111
bool FileCommit(FILE *file)
Ensure file contents are fully committed to disk, using a platform-specific feature analogous to fsyn...
Definition: fs_helpers.cpp:125
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, uint32_t flags, const BaseSignatureChecker &checker, ScriptExecutionMetrics &metricsOut, ScriptError *serror)
Execute an unlocking and locking script together.
ChainstateRole
This enum describes the various roles a specific Chainstate instance can take.
Definition: chain.h:14
bool error(const char *fmt, const Args &...args)
Definition: logging.h:263
#define LogPrintLevel(category, level,...)
Definition: logging.h:247
#define LogPrint(category,...)
Definition: logging.h:238
#define LogPrintf(...)
Definition: logging.h:227
unsigned int nHeight
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
Compute the Merkle root of the transactions in a block.
Definition: merkle.cpp:69
@ AVALANCHE
Definition: logging.h:62
@ REINDEX
Definition: logging.h:51
@ VALIDATION
Definition: logging.h:61
@ PRUNE
Definition: logging.h:54
@ MEMPOOL
Definition: logging.h:42
@ BENCH
Definition: logging.h:44
bool CheckBlock(const CCheckpointData &data, int nHeight, const BlockHash &hash)
Returns true if block passes checkpoint checks.
Definition: checkpoints.cpp:11
@ DEPLOYMENT_DERSIG
Definition: params.h:23
@ DEPLOYMENT_P2SH
Definition: params.h:20
@ DEPLOYMENT_CSV
Definition: params.h:24
@ DEPLOYMENT_HEIGHTINCB
Definition: params.h:21
@ DEPLOYMENT_CLTV
Definition: params.h:22
bool CheckTxInputs(const CTransaction &tx, TxValidationState &state, const CCoinsViewCache &inputs, int nSpendHeight, Amount &txfee)
Check whether all inputs of this transaction are valid (no double spends and amounts).
Definition: tx_verify.cpp:168
static bool exists(const path &p)
Definition: fs.h:102
static std::string PathToString(const path &path)
Convert path object to byte string.
Definition: fs.h:142
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:30
std::string get_filesystem_error_message(const fs::filesystem_error &e)
Definition: fs.cpp:142
std::function< FILE *(const fs::path &, const char *)> FopenFn
Definition: fs.h:198
Definition: common.cpp:20
static bool ComputeUTXOStats(CCoinsView *view, CCoinsStats &stats, T hash_obj, const std::function< void()> &interruption_point)
Calculate statistics about the unspent transaction output set.
Definition: coinstats.cpp:92
CoinStatsHashType
Definition: coinstats.h:23
bool LoadMempool(CTxMemPool &pool, const fs::path &load_path, Chainstate &active_chainstate, FopenFn mockable_fopen_function)
static const unsigned int UNDOFILE_CHUNK_SIZE
The pre-allocation chunk size for rev?????.dat files (since 0.8)
Definition: blockstorage.h:48
const fs::path SNAPSHOT_BLOCKHASH_FILENAME
The file in the snapshot chainstate dir which stores the base blockhash.
bool WriteSnapshotBaseBlockhash(Chainstate &snapshot_chainstate)
std::optional< fs::path > FindSnapshotChainstateDir()
Return a path to the snapshot-based chainstate dir, if one exists.
std::unordered_map< BlockHash, CBlockIndex, BlockHasher > BlockMap
Definition: blockstorage.h:62
std::optional< BlockHash > ReadSnapshotBaseBlockhash(const fs::path &chaindir)
static constexpr unsigned int BLOCKFILE_CHUNK_SIZE
The pre-allocation chunk size for blk?????.dat files (since 0.8)
Definition: blockstorage.h:46
bool WriteSnapshotBaseBlockhash(Chainstate &snapshot_chainstate) EXCLUSIVE_LOCKS_REQUIRED(std::optional< BlockHash > ReadSnapshotBaseBlockhash(const fs::path &chaindir) EXCLUSIVE_LOCKS_REQUIRED(constexpr std::string_view SNAPSHOT_CHAINSTATE_SUFFIX
Write out the blockhash of the snapshot base block that was used to construct this chainstate.
std::atomic_bool fReindex
bool Func(const std::string &str, Span< const char > &sp)
Parse a function call.
Definition: spanparsing.cpp:23
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Definition: rcu.h:259
bilingual_str ErrorString(const Result< T > &result)
Definition: result.h:90
std::shared_ptr< Chain::Notifications > m_notifications
Definition: interfaces.cpp:467
bool IsChildWithParents(const Package &package)
Context-free check that a package is exactly one child and its parents; not all parents need to be pr...
Definition: packages.cpp:86
bool CheckPackage(const Package &txns, PackageValidationState &state)
Context-free package policy checks:
Definition: packages.cpp:14
std::vector< CTransactionRef > Package
A package is an ordered list of transactions.
Definition: packages.h:40
@ PCKG_POLICY
The package itself is invalid (e.g. too many transactions).
@ PCKG_MEMPOOL_ERROR
Mempool logic error.
@ PCKG_TX
At least one tx is invalid.
bool AreInputsStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs, uint32_t flags)
Check transaction inputs to mitigate two potential denial-of-service attacks:
Definition: policy.cpp:145
bool IsStandardTx(const CTransaction &tx, const std::optional< unsigned > &max_datacarrier_bytes, bool permit_bare_multisig, const CFeeRate &dust_relay_fee, std::string &reason)
Check for standard transaction types.
Definition: policy.cpp:66
static constexpr uint32_t STANDARD_SCRIPT_VERIFY_FLAGS
Standard script verification flags that standard transactions will comply with.
Definition: policy.h:91
static constexpr uint32_t STANDARD_LOCKTIME_VERIFY_FLAGS
Used as the flags parameter to sequence and nLocktime checks in non-consensus code.
Definition: policy.h:108
bool CheckProofOfWork(const BlockHash &hash, uint32_t nBits, const Consensus::Params &params)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
Definition: pow.cpp:87
uint32_t GetNextWorkRequired(const CBlockIndex *pindexPrev, const CBlockHeader *pblock, const CChainParams &chainParams)
Definition: pow.cpp:21
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:315
uint256 GetRandHash() noexcept
Definition: random.cpp:659
const char * prefix
Definition: rest.cpp:817
reverse_range< T > reverse_iterate(T &x)
std::string ScriptErrorString(const ScriptError serror)
ScriptError
Definition: script_error.h:11
@ SIGCHECKS_LIMIT_EXCEEDED
@ SCRIPT_VERIFY_P2SH
Definition: script_flags.h:16
@ SCRIPT_VERIFY_SIGPUSHONLY
Definition: script_flags.h:35
@ SCRIPT_VERIFY_LOW_S
Definition: script_flags.h:31
@ SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY
Definition: script_flags.h:68
@ SCRIPT_ENABLE_REPLAY_PROTECTION
Definition: script_flags.h:89
@ SCRIPT_ENABLE_SCHNORR_MULTISIG
Definition: script_flags.h:97
@ SCRIPT_VERIFY_STRICTENC
Definition: script_flags.h:22
@ SCRIPT_VERIFY_NULLFAIL
Definition: script_flags.h:81
@ SCRIPT_VERIFY_DERSIG
Definition: script_flags.h:26
@ SCRIPT_ENFORCE_SIGCHECKS
Definition: script_flags.h:106
@ SCRIPT_VERIFY_CLEANSTACK
Definition: script_flags.h:63
@ SCRIPT_VERIFY_NONE
Definition: script_flags.h:12
@ SCRIPT_VERIFY_MINIMALDATA
Definition: script_flags.h:43
@ SCRIPT_VERIFY_CHECKSEQUENCEVERIFY
Definition: script_flags.h:73
@ SCRIPT_ENABLE_SIGHASH_FORKID
Definition: script_flags.h:85
void AddKeyInScriptCache(ScriptCacheKey key, int nSigChecks)
Add an entry in the cache.
bool IsKeyInScriptCache(ScriptCacheKey key, bool erase, int &nSigChecksOut)
Check if a given key is in the cache, and if so, return its values.
CAddrDb db
Definition: main.cpp:35
@ SER_DISK
Definition: serialize.h:153
uint64_t ReadCompactSize(Stream &is, bool range_check=true)
Decode a CompactSize-encoded variable-length integer.
Definition: serialize.h:413
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1258
bool ShutdownRequested()
Returns true if a shutdown is requested, false otherwise.
Definition: shutdown.cpp:85
void StartShutdown()
Request shutdown of the application.
Definition: shutdown.cpp:55
auto Join(const std::vector< T > &list, const BaseType &separator, UnaryOp unary_op) -> decltype(unary_op(list.at(0)))
Join a list of items.
Definition: string.h:63
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:108
Definition: amount.h:19
static constexpr Amount zero() noexcept
Definition: amount.h:32
Holds configuration for use during UTXO snapshot load and validation.
Definition: chainparams.h:48
AssumeutxoHash hash_serialized
The expected hash of the deserialized UTXO set.
Definition: chainparams.h:52
unsigned int nChainTx
Used to populate the nChainTx value, which is used during BlockManager::LoadBlockIndex().
Definition: chainparams.h:60
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
bool isValid(enum BlockValidity nUpTo=BlockValidity::TRANSACTIONS) const
Check whether this block index entry is valid up to the passed validity level.
Definition: blockstatus.h:99
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:109
std::vector< BlockHash > vHave
Definition: block.h:110
Holds various statistics on transactions within a chain.
Definition: chainparams.h:73
double dTxRate
Definition: chainparams.h:76
int64_t nTime
Definition: chainparams.h:74
int64_t nTxCount
Definition: chainparams.h:75
User-controlled performance and debug options.
Definition: txdb.h:56
Parameters that influence chain consensus.
Definition: params.h:34
int shibusawaActivationTime
Unix time used for MTP activation of 15 Nov 2025 12:00:00 UTC upgrade.
Definition: params.h:67
BlockHash BIP34Hash
Definition: params.h:41
int BIP34Height
Block height and hash at which BIP34 becomes active.
Definition: params.h:40
int nSubsidyHalvingInterval
Definition: params.h:36
BlockHash hashGenesisBlock
Definition: params.h:35
int64_t nPowTargetSpacing
Definition: params.h:80
bool fPowAllowMinDifficultyBlocks
Definition: params.h:77
Application-specific storage settings.
Definition: dbwrapper.h:32
fs::path path
Location in the filesystem where leveldb data will be stored.
Definition: dbwrapper.h:34
int nFile
Definition: flatfile.h:15
unsigned int nPos
Definition: flatfile.h:16
bool IsNull() const
Definition: flatfile.h:40
int64_t time
Definition: mempool_entry.h:27
Validation result for a transaction evaluated by MemPoolAccept (single or package).
Definition: validation.h:208
const ResultType m_result_type
Result type.
Definition: validation.h:219
@ VALID
Fully validated, valid.
static MempoolAcceptResult Failure(TxValidationState state)
Definition: validation.h:247
static MempoolAcceptResult FeeFailure(TxValidationState state, CFeeRate effective_feerate, const std::vector< TxId > &txids_fee_calculations)
Definition: validation.h:252
static MempoolAcceptResult Success(int64_t vsize, Amount fees, CFeeRate effective_feerate, const std::vector< TxId > &txids_fee_calculations)
Constructor for success case.
Definition: validation.h:260
static MempoolAcceptResult MempoolTx(int64_t vsize, Amount fees)
Constructor for already-in-mempool case.
Definition: validation.h:270
std::chrono::time_point< NodeClock > time_point
Definition: time.h:19
Validation result for package mempool acceptance.
Definition: validation.h:311
Precompute sighash midstate to avoid quadratic hashing.
Definition: transaction.h:325
const char * what() const override
A TxId is the identifier of a transaction.
Definition: txid.h:14
Bilingual messages:
Definition: translation.h:17
std::string original
Definition: translation.h:18
An options struct for BlockManager, more ergonomically referred to as BlockManager::Options due to th...
An options struct for ChainstateManager, more ergonomically referred to as ChainstateManager::Options...
const std::function< NodeClock::time_point()> adjusted_time_callback
std::optional< bool > check_block_index
std::chrono::seconds max_tip_age
If the tip is older than this, the node is considered to be in initial block download.
bool store_recent_headers_time
If set, store and load the last few block headers reception time to speed up RTT bootstraping.
#define AssertLockNotHeld(cs)
Definition: sync.h:163
#define LOCK(cs)
Definition: sync.h:306
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:357
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
#define LOCKS_EXCLUDED(...)
Definition: threadsafety.h:55
#define NO_THREAD_SAFETY_ANALYSIS
Definition: threadsafety.h:58
int64_t GetTimeMicros()
Returns the system time (not mockable)
Definition: time.cpp:105
int64_t GetTimeMillis()
Returns the system time (not mockable)
Definition: time.cpp:101
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
Definition: time.cpp:109
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
Definition: time.cpp:113
#define LOG_TIME_MILLIS_WITH_CATEGORY(end_msg, log_category)
Definition: timer.h:97
#define LOG_TIME_MILLIS_WITH_CATEGORY_MSG_ONCE(end_msg, log_category)
Definition: timer.h:100
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1202
#define TRACE6(context, event, a, b, c, d, e, f)
Definition: trace.h:45
#define TRACE5(context, event, a, b, c, d, e)
Definition: trace.h:44
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:68
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:36
bool CheckRegularTransaction(const CTransaction &tx, TxValidationState &state)
Context-independent validity checks for coinbase and non-coinbase transactions.
Definition: tx_check.cpp:75
bool CheckCoinbase(const CTransaction &tx, TxValidationState &state)
Definition: tx_check.cpp:56
bool EvaluateSequenceLocks(const CBlockIndex &block, std::pair< int, int64_t > lockPair)
Definition: tx_verify.cpp:150
std::pair< int, int64_t > CalculateSequenceLocks(const CTransaction &tx, int flags, std::vector< int > &prevHeights, const CBlockIndex &block)
Calculates the block height and previous block's median time past at which the transaction will be co...
Definition: tx_verify.cpp:78
bool SequenceLocks(const CTransaction &tx, int flags, std::vector< int > &prevHeights, const CBlockIndex &block)
Check if transaction is final per BIP 68 sequence numbers and can be included in a block.
Definition: tx_verify.cpp:161
bool ContextualCheckTransaction(const Consensus::Params &params, const CTransaction &tx, TxValidationState &state, int nHeight, int64_t nMedianTimePast)
Context dependent validity checks for non coinbase transactions.
Definition: tx_verify.cpp:41
static const uint32_t MEMPOOL_HEIGHT
Fake height value used in Coins to signify they are only in the memory pool(since 0....
Definition: txmempool.h:50
uint256 uint256S(const char *str)
uint256 from const char *.
Definition: uint256.h:143
#define expect(bit)
static bool DeleteCoinsDBFromDisk(const fs::path &db_path, bool is_snapshot) EXCLUSIVE_LOCKS_REQUIRED(
static bool NotifyHeaderTip(ChainstateManager &chainman) LOCKS_EXCLUDED(cs_main)
void StartScriptCheckWorkerThreads(int threads_num)
Run instances of script checking worker threads.
static int64_t nTimeConnectTotal
GlobalMutex g_best_block_mutex
Definition: validation.cpp:118
Amount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
std::condition_variable g_best_block_cv
Definition: validation.cpp:119
std::optional< LockPoints > CalculateLockPointsAtTip(CBlockIndex *tip, const CCoinsView &coins_view, const CTransaction &tx)
Calculate LockPoints required to check if transaction will be BIP68 final in the next block to be cre...
Definition: validation.cpp:185
static bool pool cs
Definition: validation.cpp:252
arith_uint256 CalculateHeadersWork(const std::vector< CBlockHeader > &headers)
Return the sum of the work on a given set of headers.
DisconnectResult ApplyBlockUndo(CBlockUndo &&blockUndo, const CBlock &block, const CBlockIndex *pindex, CCoinsViewCache &view)
Undo a block from the block and the undoblock data.
double GuessVerificationProgress(const ChainTxData &data, const CBlockIndex *pindex)
Guess how far we are in the verification process at the given block index require cs_main if pindex h...
MempoolAcceptResult AcceptToMemoryPool(Chainstate &active_chainstate, const CTransactionRef &tx, int64_t accept_time, bool bypass_limits, bool test_accept, unsigned int heightOverride)
Try to add a transaction to the mempool.
#define MICRO
Definition: validation.cpp:93
static int64_t nBlocksTotal
static int64_t nTimePostConnect
static bool CheckBlockHeader(const CBlockHeader &block, BlockValidationState &state, const Consensus::Params &params, BlockValidationOptions validationOptions)
Return true if the provided block header is valid.
static SynchronizationState GetSynchronizationState(bool init)
static void SnapshotUTXOHashBreakpoint()
static int64_t nTimeFlush
static bool ContextualCheckBlock(const CBlock &block, BlockValidationState &state, const ChainstateManager &chainman, const CBlockIndex *pindexPrev)
NOTE: This function is not currently invoked by ConnectBlock(), so we should consider upgrade issues ...
bool CheckSequenceLocksAtTip(CBlockIndex *tip, const LockPoints &lock_points)
Check if transaction will be BIP68 final in the next block to be created on top of tip.
Definition: validation.cpp:209
static uint32_t GetNextBlockScriptFlags(const CBlockIndex *pindex, const ChainstateManager &chainman)
const CBlockIndex * g_best_block
Used to notify getblocktemplate RPC of new tips.
Definition: validation.cpp:120
bool HasValidProofOfWork(const std::vector< CBlockHeader > &headers, const Consensus::Params &consensusParams)
Check with the proof of work on each blockheader matches the value in nBits.
static constexpr std::chrono::hours DATABASE_FLUSH_INTERVAL
Time to wait between flushing chainstate to disk.
Definition: validation.cpp:99
PackageMempoolAcceptResult ProcessNewPackage(Chainstate &active_chainstate, CTxMemPool &pool, const Package &package, bool test_accept)
Validate (and maybe submit) a package to the mempool.
static CCheckQueue< CScriptCheck > scriptcheckqueue(128)
static ChainstateManager::Options && Flatten(ChainstateManager::Options &&opts)
Apply default chain params to nullopt members.
static bool CheckMerkleRoot(const CBlock &block, BlockValidationState &state)
static constexpr int PRUNE_LOCK_BUFFER
The number of blocks to keep below the deepest prune lock.
Definition: validation.cpp:114
static int64_t nTimeVerify
void StopScriptCheckWorkerThreads()
Stop all of the script checking worker threads.
static void LimitValidationInterfaceQueue() LOCKS_EXCLUDED(cs_main)
return CheckInputScripts(tx, state, view, flags, true, true, txdata, nSigChecksOut)
static bool CheckInputsFromMempoolAndCache(const CTransaction &tx, TxValidationState &state, const CCoinsViewCache &view, const CTxMemPool &pool, const uint32_t flags, PrecomputedTransactionData &txdata, int &nSigChecksOut, CCoinsViewCache &coins_tip) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Checks to avoid mempool polluting consensus critical paths since cached signature and script validity...
void SpendCoins(CCoinsViewCache &view, const CTransaction &tx, CTxUndo &txundo, int nHeight)
Mark all the coins corresponding to a given transaction inputs as spent.
static int64_t nTimeTotal
static int64_t nTimeConnect
bool CheckBlock(const CBlock &block, BlockValidationState &state, const Consensus::Params &params, BlockValidationOptions validationOptions)
Functions for validating blocks and updating the block tree.
bool ContextualCheckTransactionForCurrentBlock(const CBlockIndex &active_chain_tip, const Consensus::Params &params, const CTransaction &tx, TxValidationState &state)
const std::vector< std::string > CHECKLEVEL_DOC
Documentation for argument 'checklevel'.
Definition: validation.cpp:100
DisconnectResult UndoCoinSpend(Coin &&undo, CCoinsViewCache &view, const COutPoint &out)
Restore the UTXO in a Coin at a given COutPoint.
static int64_t nTimeIndex
bool TestBlockValidity(BlockValidationState &state, const CChainParams &params, Chainstate &chainstate, const CBlock &block, CBlockIndex *pindexPrev, const std::function< NodeClock::time_point()> &adjusted_time_callback, BlockValidationOptions validationOptions)
void PruneBlockFilesManual(Chainstate &active_chainstate, int nManualPruneHeight)
Prune block files up to a given height.
static void FlushSnapshotToDisk(CCoinsViewCache &coins_cache, bool snapshot_loaded)
bool IsBlockMutated(const CBlock &block)
Check if a block has been mutated (with respect to its merkle root).
AssertLockHeld(pool.cs)
bool AbortNode(BlockValidationState &state, const std::string &strMessage, const bilingual_str &userMessage)
void UpdateCoins(CCoinsViewCache &view, const CTransaction &tx, CTxUndo &txundo, int nHeight)
Apply the effects of this transaction on the UTXO set represented by view.
static bool ContextualCheckBlockHeader(const CBlockHeader &block, BlockValidationState &state, BlockManager &blockman, ChainstateManager &chainman, const CBlockIndex *pindexPrev, NodeClock::time_point now, const std::optional< CCheckpointData > &test_checkpoints=std::nullopt) EXCLUSIVE_LOCKS_REQUIRED(
Context-dependent validity checks.
static int64_t nTimeForks
static constexpr uint64_t HEADERS_TIME_VERSION
Definition: validation.cpp:116
static int64_t nTimeCheck
#define MILLI
Definition: validation.cpp:94
static fs::path GetSnapshotCoinsDBPath(Chainstate &cs) EXCLUSIVE_LOCKS_REQUIRED(
static void UpdateTipLog(const CCoinsViewCache &coins_tip, const CBlockIndex *tip, const CChainParams &params, const std::string &func_name, const std::string &prefix) EXCLUSIVE_LOCKS_REQUIRED(
static constexpr std::chrono::hours DATABASE_WRITE_INTERVAL
Time to wait between writing blocks/block index to disk.
Definition: validation.cpp:97
static int64_t nTimeChainState
assert(!tx.IsCoinBase())
static int64_t nTimeReadFromDisk
static bool IsReplayProtectionEnabled(const Consensus::Params &params, int64_t nMedianTimePast)
Definition: validation.cpp:228
#define MIN_TRANSACTION_SIZE
Definition: validation.h:80
static const unsigned int MIN_BLOCKS_TO_KEEP
Block files containing a block-height within MIN_BLOCKS_TO_KEEP of ActiveChain().Tip() will not be pr...
Definition: validation.h:96
SnapshotCompletionResult
Definition: validation.h:1110
SynchronizationState
Current sync state passed to tip changed callbacks.
Definition: validation.h:115
VerifyDBResult
Definition: validation.h:614
CoinsCacheSizeState
Definition: validation.h:683
@ LARGE
The cache is at >= 90% capacity.
@ CRITICAL
The coins cache is in immediate need of a flush.
FlushStateMode
Definition: validation.h:644
static const int DEFAULT_STOPATHEIGHT
Default for -stopatheight.
Definition: validation.h:91
CMainSignals & GetMainSignals()
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11
void SetfLargeWorkInvalidChainFound(bool flag)
Definition: warnings.cpp:36
void SetfLargeWorkForkFound(bool flag)
Definition: warnings.cpp:26
bool GetfLargeWorkForkFound()
Definition: warnings.cpp:31