Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
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
10#include <kernel/coinstats.h>
14
15#include <arith_uint256.h>
16#include <avalanche/avalanche.h>
17#include <avalanche/processor.h>
18#include <blockvalidity.h>
19#include <chainparams.h>
20#include <checkpoints.h>
21#include <checkqueue.h>
22#include <common/args.h>
23#include <config.h>
25#include <consensus/amount.h>
26#include <consensus/merkle.h>
27#include <consensus/tx_check.h>
28#include <consensus/tx_verify.h>
30#include <hash.h>
32#include <logging.h>
33#include <logging/timer.h>
34#include <minerfund.h>
35#include <node/blockstorage.h>
36#include <node/utxo_snapshot.h>
39#include <policy/block/rtt.h>
41#include <policy/policy.h>
42#include <policy/settings.h>
43#include <pow/pow.h>
44#include <primitives/block.h>
46#include <random.h>
47#include <reverse_iterator.h>
48#include <script/script.h>
49#include <script/scriptcache.h>
50#include <script/sigcache.h>
51#include <shutdown.h>
52#include <tinyformat.h>
53#include <txdb.h>
54#include <txmempool.h>
55#include <undo.h>
56#include <util/check.h> // For NDEBUG compile time check
57#include <util/fs.h>
58#include <util/fs_helpers.h>
59#include <util/strencodings.h>
60#include <util/string.h>
61#include <util/time.h>
62#include <util/trace.h>
63#include <util/translation.h>
64#include <validationinterface.h>
65#include <warnings.h>
66
67#include <algorithm>
68#include <atomic>
69#include <cassert>
70#include <chrono>
71#include <deque>
72#include <numeric>
73#include <optional>
74#include <string>
75#include <thread>
76
82
86using node::BlockMap;
87using node::fReindex;
90
91#define MICRO 0.000001
92#define MILLI 0.001
93
95static constexpr std::chrono::hours DATABASE_WRITE_INTERVAL{1};
97static constexpr std::chrono::hours DATABASE_FLUSH_INTERVAL{24};
98const std::vector<std::string> CHECKLEVEL_DOC{
99 "level 0 reads the blocks from disk",
100 "level 1 verifies block validity",
101 "level 2 verifies undo data",
102 "level 3 checks disconnection of tip blocks",
103 "level 4 tries to reconnect the blocks",
104 "each level includes the checks of the previous levels",
105};
112static constexpr int PRUNE_LOCK_BUFFER{10};
113
114static constexpr uint64_t HEADERS_TIME_VERSION{1};
115
117std::condition_variable g_best_block_cv;
119
121 : excessiveBlockSize(config.GetMaxBlockSize()), checkPoW(true),
122 checkMerkleRoot(true) {}
123
124const CBlockIndex *
127
128 // Find the latest block common to locator and chain - we expect that
129 // locator.vHave is sorted descending by height.
130 for (const BlockHash &hash : locator.vHave) {
131 const CBlockIndex *pindex{m_blockman.LookupBlockIndex(hash)};
132 if (pindex) {
133 if (m_chain.Contains(pindex)) {
134 return pindex;
135 }
136 if (pindex->GetAncestor(m_chain.Height()) == m_chain.Tip()) {
137 return m_chain.Tip();
138 }
139 }
140 }
141 return m_chain.Genesis();
142}
143
144static uint32_t GetNextBlockScriptFlags(const CBlockIndex *pindex,
145 const ChainstateManager &chainman);
146
147namespace {
159std::optional<std::vector<int>> CalculatePrevHeights(const CBlockIndex &tip,
160 const CCoinsView &coins,
161 const CTransaction &tx) {
162 std::vector<int> prev_heights;
163 prev_heights.resize(tx.vin.size());
164 for (size_t i = 0; i < tx.vin.size(); ++i) {
165 const CTxIn &txin = tx.vin[i];
166 Coin coin;
167 if (!coins.GetCoin(txin.prevout, coin)) {
168 LogPrintf("ERROR: %s: Missing input %d in transaction \'%s\'\n",
169 __func__, i, tx.GetId().GetHex());
170 return std::nullopt;
171 }
172 if (coin.GetHeight() == MEMPOOL_HEIGHT) {
173 // Assume all mempool transaction confirm in the next block.
174 prev_heights[i] = tip.nHeight + 1;
175 } else {
176 prev_heights[i] = coin.GetHeight();
177 }
178 }
179 return prev_heights;
180}
181} // namespace
182
183std::optional<LockPoints> CalculateLockPointsAtTip(CBlockIndex *tip,
184 const CCoinsView &coins_view,
185 const CTransaction &tx) {
186 assert(tip);
187
188 auto prev_heights{CalculatePrevHeights(*tip, coins_view, tx)};
189 if (!prev_heights.has_value()) {
190 return std::nullopt;
191 }
192
195 // When SequenceLocks() is called within ConnectBlock(), the height
196 // of the block *being* evaluated is what is used.
197 // Thus if we want to know if a transaction can be part of the
198 // *next* block, we need to use one more than
199 // active_chainstate.m_chain.Height()
200 next_tip.nHeight = tip->nHeight + 1;
201 const auto [min_height, min_time] = CalculateSequenceLocks(
203
204 return LockPoints{min_height, min_time};
205}
206
208 assert(tip != nullptr);
209
210 CBlockIndex index;
211 index.pprev = tip;
212 // CheckSequenceLocksAtTip() uses active_chainstate.m_chain.Height()+1 to
213 // evaluate height based locks because when SequenceLocks() is called within
214 // ConnectBlock(), the height of the block *being* evaluated is what is
215 // used. Thus if we want to know if a transaction can be part of the *next*
216 // block, we need to use one more than active_chainstate.m_chain.Height()
217 index.nHeight = tip->nHeight + 1;
218
219 return EvaluateSequenceLocks(index, {lock_points.height, lock_points.time});
220}
221
222// Command-line argument "-replayprotectionactivationtime=<timestamp>" will
223// cause the node to switch to replay protected SigHash ForkID value when the
224// median timestamp of the previous 11 blocks is greater than or equal to
225// <timestamp>. Defaults to the pre-defined timestamp when not set.
228 return nMedianTimePast >= gArgs.GetIntArg("-replayprotectionactivationtime",
230}
231
233 const CBlockIndex *pindexPrev) {
234 if (pindexPrev == nullptr) {
235 return false;
236 }
237
238 return IsReplayProtectionEnabled(params, pindexPrev->GetMedianTimePast());
239}
240
247 const CTransaction &tx, TxValidationState &state,
248 const CCoinsViewCache &view, const CTxMemPool &pool, const uint32_t flags,
253
255 for (const CTxIn &txin : tx.vin) {
256 const Coin &coin = view.AccessCoin(txin.prevout);
257
258 // This coin was checked in PreChecks and MemPoolAccept
259 // has been holding cs_main since then.
260 Assume(!coin.IsSpent());
261 if (coin.IsSpent()) {
262 return false;
263 }
264
265 // If the Coin is available, there are 2 possibilities:
266 // it is available in our current ChainstateActive UTXO set,
267 // or it's a UTXO provided by a transaction in our mempool.
268 // Ensure the scriptPubKeys in Coins from CoinsView are correct.
269 const CTransactionRef &txFrom = pool.get(txin.prevout.GetTxId());
270 if (txFrom) {
271 assert(txFrom->GetId() == txin.prevout.GetTxId());
272 assert(txFrom->vout.size() > txin.prevout.GetN());
273 assert(txFrom->vout[txin.prevout.GetN()] == coin.GetTxOut());
274 } else {
275 const Coin &coinFromUTXOSet = coins_tip.AccessCoin(txin.prevout);
276 assert(!coinFromUTXOSet.IsSpent());
277 assert(coinFromUTXOSet.GetTxOut() == coin.GetTxOut());
278 }
279 }
280
281 // Call CheckInputScripts() to cache signature and script validity against
282 // current tip consensus rules.
283 return CheckInputScripts(tx, state, view, flags, /*sigCacheStore=*/true,
284 /*scriptCacheStore=*/true, txdata, nSigChecksOut);
285}
286
287namespace {
288
289class MemPoolAccept {
290public:
291 MemPoolAccept(CTxMemPool &mempool, Chainstate &active_chainstate)
292 : m_pool(mempool), m_view(&m_dummy),
293 m_viewmempool(&active_chainstate.CoinsTip(), m_pool),
294 m_active_chainstate(active_chainstate) {}
295
296 // We put the arguments we're handed into a struct, so we can pass them
297 // around easier.
298 struct ATMPArgs {
299 const Config &m_config;
300 const int64_t m_accept_time;
301 const bool m_bypass_limits;
302 /*
303 * Return any outpoints which were not previously present in the coins
304 * cache, but were added as a result of validating the tx for mempool
305 * acceptance. This allows the caller to optionally remove the cache
306 * additions if the associated transaction ends up being rejected by
307 * the mempool.
308 */
309 std::vector<COutPoint> &m_coins_to_uncache;
310 const bool m_test_accept;
311 const unsigned int m_heightOverride;
317 const bool m_package_submission;
323 const bool m_package_feerates;
324
326 static ATMPArgs SingleAccept(const Config &config, int64_t accept_time,
327 bool bypass_limits,
328 std::vector<COutPoint> &coins_to_uncache,
329 bool test_accept,
330 unsigned int heightOverride) {
331 return ATMPArgs{
332 config,
338 /*package_submission=*/false,
339 /*package_feerates=*/false,
340 };
341 }
342
347 static ATMPArgs
349 std::vector<COutPoint> &coins_to_uncache) {
350 return ATMPArgs{
351 config,
353 /*bypass_limits=*/false,
355 /*test_accept=*/true,
356 /*height_override=*/0,
357 // not submitting to mempool
358 /*package_submission=*/false,
359 /*package_feerates=*/false,
360 };
361 }
362
364 static ATMPArgs
366 std::vector<COutPoint> &coins_to_uncache) {
367 return ATMPArgs{
368 config,
370 /*bypass_limits=*/false,
372 /*test_accept=*/false,
373 /*height_override=*/0,
374 /*package_submission=*/true,
375 /*package_feerates=*/true,
376 };
377 }
378
380 static ATMPArgs SingleInPackageAccept(const ATMPArgs &package_args) {
381 return ATMPArgs{
382 /*config=*/package_args.m_config,
383 /*accept_time=*/package_args.m_accept_time,
384 /*bypass_limits=*/false,
385 /*coins_to_uncache=*/package_args.m_coins_to_uncache,
386 /*test_accept=*/package_args.m_test_accept,
387 /*height_override=*/package_args.m_heightOverride,
388 // do not LimitMempoolSize in Finalize()
389 /*package_submission=*/true,
390 // only 1 transaction
391 /*package_feerates=*/false,
392 };
393 }
394
395 private:
396 // Private ctor to avoid exposing details to clients and allowing the
397 // possibility of mixing up the order of the arguments. Use static
398 // functions above instead.
399 ATMPArgs(const Config &config, int64_t accept_time, bool bypass_limits,
400 std::vector<COutPoint> &coins_to_uncache, bool test_accept,
401 unsigned int height_override, bool package_submission,
402 bool package_feerates)
403 : m_config{config}, m_accept_time{accept_time},
404 m_bypass_limits{bypass_limits},
405 m_coins_to_uncache{coins_to_uncache}, m_test_accept{test_accept},
406 m_heightOverride{height_override},
407 m_package_submission{package_submission},
408 m_package_feerates(package_feerates) {}
409 };
410
411 // Single transaction acceptance
413 ATMPArgs &args)
415
423 AcceptMultipleTransactions(const std::vector<CTransactionRef> &txns,
424 ATMPArgs &args)
426
440 AcceptSubPackage(const std::vector<CTransactionRef> &subpackage,
441 ATMPArgs &args)
443
450 ATMPArgs &args)
452
453private:
454 // All the intermediate state that gets passed between the various levels
455 // of checking a given transaction.
456 struct Workspace {
457 Workspace(const CTransactionRef &ptx,
459 : m_ptx(ptx),
460 m_next_block_script_verify_flags(next_block_script_verify_flags) {
461 }
467 std::unique_ptr<CTxMemPoolEntry> m_entry;
468
473 int64_t m_vsize;
478 Amount m_base_fees;
479
484 Amount m_modified_fees;
485
492 CFeeRate m_package_feerate{Amount::zero()};
493
494 const CTransactionRef &m_ptx;
495 TxValidationState m_state;
501 PrecomputedTransactionData m_precomputed_txdata;
502
503 // ABC specific flags that are used in both PreChecks and
504 // ConsensusScriptChecks
505 const uint32_t m_next_block_script_verify_flags;
506 int m_sig_checks_standard;
507 };
508
509 // Run the policy checks on a given transaction, excluding any script
510 // checks. Looks up inputs, calculates feerate, considers replacement,
511 // evaluates package limits, etc. As this function can be invoked for "free"
512 // by a peer, only tests that are fast should be done here (to avoid CPU
513 // DoS).
514 bool PreChecks(ATMPArgs &args, Workspace &ws)
516
517 // Re-run the script checks, using consensus flags, and try to cache the
518 // result in the scriptcache. This should be done after
519 // PolicyScriptChecks(). This requires that all inputs either be in our
520 // utxo set or in the mempool.
521 bool ConsensusScriptChecks(const ATMPArgs &args, Workspace &ws)
523
524 // Try to add the transaction to the mempool, removing any conflicts first.
525 // Returns true if the transaction is in the mempool after any size
526 // limiting is performed, false otherwise.
527 bool Finalize(const ATMPArgs &args, Workspace &ws)
529
530 // Submit all transactions to the mempool and call ConsensusScriptChecks to
531 // add to the script cache - should only be called after successful
532 // validation of all transactions in the package.
533 // Does not call LimitMempoolSize(), so mempool max_size_bytes may be
534 // temporarily exceeded.
535 bool SubmitPackage(const ATMPArgs &args, std::vector<Workspace> &workspaces,
537 std::map<TxId, MempoolAcceptResult> &results)
539
540 // Compare a package's feerate against minimum allowed.
541 bool CheckFeeRate(size_t package_size, size_t package_vsize,
545 AssertLockHeld(m_pool.cs);
546
548 m_pool.GetMinFee().GetFee(package_vsize);
549
552 return state.Invalid(
554 "mempool min fee not met",
556 }
557
558 // Do not change this to use virtualsize without coordinating a network
559 // policy upgrade.
560 if (package_fee < m_pool.m_min_relay_feerate.GetFee(package_size)) {
561 return state.Invalid(
563 "min relay fee not met",
564 strprintf("%d < %d", package_fee,
565 m_pool.m_min_relay_feerate.GetFee(package_size)));
566 }
567
568 return true;
569 }
570
571private:
572 CTxMemPool &m_pool;
573 CCoinsViewCache m_view;
574 CCoinsViewMemPool m_viewmempool;
575 CCoinsView m_dummy;
576
577 Chainstate &m_active_chainstate;
578};
579
580bool MemPoolAccept::PreChecks(ATMPArgs &args, Workspace &ws) {
582 AssertLockHeld(m_pool.cs);
583 const CTransactionRef &ptx = ws.m_ptx;
584 const CTransaction &tx = *ws.m_ptx;
585 const TxId &txid = ws.m_ptx->GetId();
586
587 // Copy/alias what we need out of args
588 const int64_t nAcceptTime = args.m_accept_time;
589 const bool bypass_limits = args.m_bypass_limits;
590 std::vector<COutPoint> &coins_to_uncache = args.m_coins_to_uncache;
591 const unsigned int heightOverride = args.m_heightOverride;
592
593 // Alias what we need out of ws
594 TxValidationState &state = ws.m_state;
595 // Coinbase is only valid in a block, not as a loose transaction.
596 if (!CheckRegularTransaction(tx, state)) {
597 // state filled in by CheckRegularTransaction.
598 return false;
599 }
600
601 // Rather not work on nonstandard transactions (unless -testnet)
602 std::string reason;
603 if (m_pool.m_require_standard &&
604 !IsStandardTx(tx, m_pool.m_max_datacarrier_bytes,
605 m_pool.m_permit_bare_multisig,
606 m_pool.m_dust_relay_feerate, reason)) {
607 return state.Invalid(TxValidationResult::TX_NOT_STANDARD, reason);
608 }
609
610 // Only accept nLockTime-using transactions that can be mined in the next
611 // block; we don't want our mempool filled up with transactions that can't
612 // be mined yet.
615 *Assert(m_active_chainstate.m_chain.Tip()),
616 args.m_config.GetChainParams().GetConsensus(), tx, ctxState)) {
617 // We copy the state from a dummy to ensure we don't increase the
618 // ban score of peer for transaction that could be valid in the future.
620 ctxState.GetRejectReason(),
621 ctxState.GetDebugMessage());
622 }
623
624 // Is it already in the memory pool?
625 if (m_pool.exists(txid)) {
627 "txn-already-in-mempool");
628 }
629
630 // Check for conflicts with in-memory transactions
631 for (const CTxIn &txin : tx.vin) {
632 if (const auto ptxConflicting = m_pool.GetConflictTx(txin.prevout)) {
633 if (m_pool.isAvalancheFinalized(ptxConflicting->GetId())) {
635 "finalized-tx-conflict");
636 }
637
638 return state.Invalid(
640 "txn-mempool-conflict");
641 }
642 }
643
644 m_view.SetBackend(m_viewmempool);
645
646 const CCoinsViewCache &coins_cache = m_active_chainstate.CoinsTip();
647 // Do all inputs exist?
648 for (const CTxIn &txin : tx.vin) {
649 if (!coins_cache.HaveCoinInCache(txin.prevout)) {
650 coins_to_uncache.push_back(txin.prevout);
651 }
652
653 // Note: this call may add txin.prevout to the coins cache
654 // (coins_cache.cacheCoins) by way of FetchCoin(). It should be
655 // removed later (via coins_to_uncache) if this tx turns out to be
656 // invalid.
657 if (!m_view.HaveCoin(txin.prevout)) {
658 // Are inputs missing because we already have the tx?
659 for (size_t out = 0; out < tx.vout.size(); out++) {
660 // Optimistically just do efficient check of cache for
661 // outputs.
662 if (coins_cache.HaveCoinInCache(COutPoint(txid, out))) {
664 "txn-already-known");
665 }
666 }
667
668 // Otherwise assume this might be an orphan tx for which we just
669 // haven't seen parents yet.
671 "bad-txns-inputs-missingorspent");
672 }
673 }
674
675 // Are the actual inputs available?
676 if (!m_view.HaveInputs(tx)) {
678 "bad-txns-inputs-spent");
679 }
680
681 // Bring the best block into scope.
682 m_view.GetBestBlock();
683
684 // we have all inputs cached now, so switch back to dummy (to protect
685 // against bugs where we pull more inputs from disk that miss being
686 // added to coins_to_uncache)
687 m_view.SetBackend(m_dummy);
688
689 assert(m_active_chainstate.m_blockman.LookupBlockIndex(
690 m_view.GetBestBlock()) == m_active_chainstate.m_chain.Tip());
691
692 // Only accept BIP68 sequence locked transactions that can be mined in
693 // the next block; we don't want our mempool filled up with transactions
694 // that can't be mined yet.
695 // Pass in m_view which has all of the relevant inputs cached. Note that,
696 // since m_view's backend was removed, it no longer pulls coins from the
697 // mempool.
698 const std::optional<LockPoints> lock_points{CalculateLockPointsAtTip(
699 m_active_chainstate.m_chain.Tip(), m_view, tx)};
700 if (!lock_points.has_value() ||
701 !CheckSequenceLocksAtTip(m_active_chainstate.m_chain.Tip(),
702 *lock_points)) {
704 "non-BIP68-final");
705 }
706
707 // The mempool holds txs for the next block, so pass height+1 to
708 // CheckTxInputs
709 if (!Consensus::CheckTxInputs(tx, state, m_view,
710 m_active_chainstate.m_chain.Height() + 1,
711 ws.m_base_fees)) {
712 // state filled in by CheckTxInputs
713 return false;
714 }
715
716 // Check for non-standard pay-to-script-hash in inputs
717 if (m_pool.m_require_standard &&
718 !AreInputsStandard(tx, m_view, ws.m_next_block_script_verify_flags)) {
720 "bad-txns-nonstandard-inputs");
721 }
722
723 // ws.m_modified_fess includes any fee deltas from PrioritiseTransaction
724 ws.m_modified_fees = ws.m_base_fees;
725 m_pool.ApplyDelta(txid, ws.m_modified_fees);
726
727 unsigned int nSize = tx.GetTotalSize();
728
729 // Validate input scripts against standard script flags.
731 ws.m_next_block_script_verify_flags | STANDARD_SCRIPT_VERIFY_FLAGS;
732 ws.m_precomputed_txdata = PrecomputedTransactionData{tx};
733 if (!CheckInputScripts(tx, state, m_view, scriptVerifyFlags, true, false,
734 ws.m_precomputed_txdata, ws.m_sig_checks_standard)) {
735 // State filled in by CheckInputScripts
736 return false;
737 }
738
739 ws.m_entry = std::make_unique<CTxMemPoolEntry>(
740 ptx, ws.m_base_fees, nAcceptTime,
741 heightOverride ? heightOverride : m_active_chainstate.m_chain.Height(),
742 ws.m_sig_checks_standard, lock_points.value());
743
744 ws.m_vsize = ws.m_entry->GetTxVirtualSize();
745
746 // No individual transactions are allowed below the min relay feerate except
747 // from disconnected blocks. This requirement, unlike CheckFeeRate, cannot
748 // be bypassed using m_package_feerates because, while a tx could be package
749 // CPFP'd when entering the mempool, we do not have a DoS-resistant method
750 // of ensuring the tx remains bumped. For example, the fee-bumping child
751 // could disappear due to a replacement.
752 if (!bypass_limits &&
753 ws.m_modified_fees <
754 m_pool.m_min_relay_feerate.GetFee(ws.m_ptx->GetTotalSize())) {
755 // Even though this is a fee-related failure, this result is
756 // TX_MEMPOOL_POLICY, not TX_PACKAGE_RECONSIDERABLE, because it cannot
757 // be bypassed using package validation.
758 return state.Invalid(
759 TxValidationResult::TX_MEMPOOL_POLICY, "min relay fee not met",
760 strprintf("%d < %d", ws.m_modified_fees,
761 m_pool.m_min_relay_feerate.GetFee(nSize)));
762 }
763 // No individual transactions are allowed below the mempool min feerate
764 // except from disconnected blocks and transactions in a package. Package
765 // transactions will be checked using package feerate later.
766 if (!bypass_limits && !args.m_package_feerates &&
767 !CheckFeeRate(nSize, ws.m_vsize, ws.m_modified_fees, state)) {
768 return false;
769 }
770
771 return true;
772}
773
774bool MemPoolAccept::ConsensusScriptChecks(const ATMPArgs &args, Workspace &ws) {
776 AssertLockHeld(m_pool.cs);
777 const CTransaction &tx = *ws.m_ptx;
778 const TxId &txid = tx.GetId();
779 TxValidationState &state = ws.m_state;
780
781 // Check again against the next block's script verification flags
782 // to cache our script execution flags.
783 //
784 // This is also useful in case of bugs in the standard flags that cause
785 // transactions to pass as valid when they're actually invalid. For
786 // instance the STRICTENC flag was incorrectly allowing certain CHECKSIG
787 // NOT scripts to pass, even though they were invalid.
788 //
789 // There is a similar check in CreateNewBlock() to prevent creating
790 // invalid blocks (using TestBlockValidity), however allowing such
791 // transactions into the mempool can be exploited as a DoS attack.
794 tx, state, m_view, m_pool, ws.m_next_block_script_verify_flags,
795 ws.m_precomputed_txdata, nSigChecksConsensus,
796 m_active_chainstate.CoinsTip())) {
797 // This can occur under some circumstances, if the node receives an
798 // unrequested tx which is invalid due to new consensus rules not
799 // being activated yet (during IBD).
800 LogPrintf("BUG! PLEASE REPORT THIS! CheckInputScripts failed against "
801 "latest-block but not STANDARD flags %s, %s\n",
802 txid.ToString(), state.ToString());
803 return Assume(false);
804 }
805
806 if (ws.m_sig_checks_standard != nSigChecksConsensus) {
807 // We can't accept this transaction as we've used the standard count
808 // for the mempool/mining, but the consensus count will be enforced
809 // in validation (we don't want to produce bad block templates).
810 return error(
811 "%s: BUG! PLEASE REPORT THIS! SigChecks count differed between "
812 "standard and consensus flags in %s",
813 __func__, txid.ToString());
814 }
815 return true;
816}
817
818bool MemPoolAccept::Finalize(const ATMPArgs &args, Workspace &ws) {
820 AssertLockHeld(m_pool.cs);
821 const TxId &txid = ws.m_ptx->GetId();
822 TxValidationState &state = ws.m_state;
823 const bool bypass_limits = args.m_bypass_limits;
824
825 // Store transaction in memory
826 CTxMemPoolEntry *pentry = ws.m_entry.release();
828 m_pool.addUnchecked(entry);
829
830 // Trim mempool and check if tx was trimmed.
831 // If we are validating a package, don't trim here because we could evict a
832 // previous transaction in the package. LimitMempoolSize() should be called
833 // at the very end to make sure the mempool is still within limits and
834 // package submission happens atomically.
835 if (!args.m_package_submission && !bypass_limits) {
836 m_pool.LimitSize(m_active_chainstate.CoinsTip());
837 if (!m_pool.exists(txid)) {
838 // The tx no longer meets our (new) mempool minimum feerate but
839 // could be reconsidered in a package.
841 "mempool full");
842 }
843 }
844 return true;
845}
846
847// Get the coins spent by ptx from the coins_view. Assumes coins are present.
848static std::vector<Coin> getSpentCoins(const CTransactionRef &ptx,
849 const CCoinsViewCache &coins_view) {
850 std::vector<Coin> spent_coins;
851 spent_coins.reserve(ptx->vin.size());
852 for (const CTxIn &input : ptx->vin) {
853 Coin coin;
854 const bool coinFound = coins_view.GetCoin(input.prevout, coin);
856 spent_coins.push_back(std::move(coin));
857 }
858 return spent_coins;
859}
860
861bool MemPoolAccept::SubmitPackage(
862 const ATMPArgs &args, std::vector<Workspace> &workspaces,
864 std::map<TxId, MempoolAcceptResult> &results) {
866 AssertLockHeld(m_pool.cs);
867 // Sanity check: none of the transactions should be in the mempool.
868 assert(std::all_of(
869 workspaces.cbegin(), workspaces.cend(),
870 [this](const auto &ws) { return !m_pool.exists(ws.m_ptx->GetId()); }));
871
872 bool all_submitted = true;
873 // ConsensusScriptChecks adds to the script cache and is therefore
874 // consensus-critical; CheckInputsFromMempoolAndCache asserts that
875 // transactions only spend coins available from the mempool or UTXO set.
876 // Submit each transaction to the mempool immediately after calling
877 // ConsensusScriptChecks to make the outputs available for subsequent
878 // transactions.
879 for (Workspace &ws : workspaces) {
880 if (!ConsensusScriptChecks(args, ws)) {
881 results.emplace(ws.m_ptx->GetId(),
883 // Since PreChecks() passed, this should never fail.
884 all_submitted = false;
885 package_state.Invalid(
887 strprintf("BUG! PolicyScriptChecks succeeded but "
888 "ConsensusScriptChecks failed: %s",
889 ws.m_ptx->GetId().ToString()));
890 }
891
892 // If we call LimitMempoolSize() for each individual Finalize(), the
893 // mempool will not take the transaction's descendant feerate into
894 // account because it hasn't seen them yet. Also, we risk evicting a
895 // transaction that a subsequent package transaction depends on.
896 // Instead, allow the mempool to temporarily bypass limits, the maximum
897 // package size) while submitting transactions individually and then
898 // trim at the very end.
899 if (!Finalize(args, ws)) {
900 results.emplace(ws.m_ptx->GetId(),
902 // Since LimitMempoolSize() won't be called, this should never fail.
903 all_submitted = false;
905 strprintf("BUG! Adding to mempool failed: %s",
906 ws.m_ptx->GetId().ToString()));
907 }
908 }
909
910 // It may or may not be the case that all the transactions made it into the
911 // mempool. Regardless, make sure we haven't exceeded max mempool size.
912 m_pool.LimitSize(m_active_chainstate.CoinsTip());
913
914 std::vector<TxId> all_package_txids;
915 all_package_txids.reserve(workspaces.size());
916 std::transform(workspaces.cbegin(), workspaces.cend(),
917 std::back_inserter(all_package_txids),
918 [](const auto &ws) { return ws.m_ptx->GetId(); });
919
920 // Add successful results. The returned results may change later if
921 // LimitMempoolSize() evicts them.
922 for (Workspace &ws : workspaces) {
923 const auto effective_feerate =
924 args.m_package_feerates
925 ? ws.m_package_feerate
926 : CFeeRate{ws.m_modified_fees,
927 static_cast<uint32_t>(ws.m_vsize)};
928 const auto effective_feerate_txids =
929 args.m_package_feerates ? all_package_txids
930 : std::vector<TxId>({ws.m_ptx->GetId()});
931 results.emplace(ws.m_ptx->GetId(),
932 MempoolAcceptResult::Success(ws.m_vsize, ws.m_base_fees,
936 ws.m_ptx,
937 std::make_shared<const std::vector<Coin>>(
938 getSpentCoins(ws.m_ptx, m_view)),
939 m_pool.GetAndIncrementSequence());
940 }
941 return all_submitted;
942}
943
945MemPoolAccept::AcceptSingleTransaction(const CTransactionRef &ptx,
946 ATMPArgs &args) {
948 // mempool "read lock" (held through
949 // GetMainSignals().TransactionAddedToMempool())
950 LOCK(m_pool.cs);
951
952 const CBlockIndex *tip = m_active_chainstate.m_chain.Tip();
953
954 Workspace ws(ptx,
955 GetNextBlockScriptFlags(tip, m_active_chainstate.m_chainman));
956
957 const std::vector<TxId> single_txid{ws.m_ptx->GetId()};
958
959 // Perform the inexpensive checks first and avoid hashing and signature
960 // verification unless those checks pass, to mitigate CPU exhaustion
961 // denial-of-service attacks.
962 if (!PreChecks(args, ws)) {
963 if (ws.m_state.GetResult() ==
965 // Failed for fee reasons. Provide the effective feerate and which
966 // tx was included.
968 ws.m_state, CFeeRate(ws.m_modified_fees, ws.m_vsize),
970 }
971 return MempoolAcceptResult::Failure(ws.m_state);
972 }
973
974 if (!ConsensusScriptChecks(args, ws)) {
975 return MempoolAcceptResult::Failure(ws.m_state);
976 }
977
978 const TxId txid = ptx->GetId();
979
980 // Mempool sanity check -- in our new mempool no tx can be added if its
981 // outputs are already spent in the mempool (that is, no children before
982 // parents allowed; the mempool must be consistent at all times).
983 //
984 // This means that on reorg, the disconnectpool *must* always import
985 // the existing mempool tx's, clear the mempool, and then re-add
986 // remaining tx's in topological order via this function. Our new mempool
987 // has fast adds, so this is ok.
988 if (auto it = m_pool.mapNextTx.lower_bound(COutPoint{txid, 0});
989 it != m_pool.mapNextTx.end() && it->first->GetTxId() == txid) {
990 LogPrintf("%s: BUG! PLEASE REPORT THIS! Attempt to add txid %s, but "
991 "its outputs are already spent in the "
992 "mempool\n",
993 __func__, txid.ToString());
995 "txn-child-before-parent");
996 return MempoolAcceptResult::Failure(ws.m_state);
997 }
998
999 const CFeeRate effective_feerate{ws.m_modified_fees,
1000 static_cast<uint32_t>(ws.m_vsize)};
1001 // Tx was accepted, but not added
1002 if (args.m_test_accept) {
1003 return MempoolAcceptResult::Success(ws.m_vsize, ws.m_base_fees,
1005 }
1006
1007 if (!Finalize(args, ws)) {
1008 // The only possible failure reason is fee-related (mempool full).
1009 // Failed for fee reasons. Provide the effective feerate and which txns
1010 // were included.
1011 Assume(ws.m_state.GetResult() ==
1014 ws.m_state, CFeeRate(ws.m_modified_fees, ws.m_vsize), single_txid);
1015 }
1016
1018 ptx,
1019 std::make_shared<const std::vector<Coin>>(getSpentCoins(ptx, m_view)),
1020 m_pool.GetAndIncrementSequence());
1021
1022 return MempoolAcceptResult::Success(ws.m_vsize, ws.m_base_fees,
1024}
1025
1026PackageMempoolAcceptResult MemPoolAccept::AcceptMultipleTransactions(
1027 const std::vector<CTransactionRef> &txns, ATMPArgs &args) {
1029
1030 // These context-free package limits can be done before taking the mempool
1031 // lock.
1035 }
1036
1037 std::vector<Workspace> workspaces{};
1038 workspaces.reserve(txns.size());
1039 std::transform(
1040 txns.cbegin(), txns.cend(), std::back_inserter(workspaces),
1041 [this](const auto &tx) {
1042 return Workspace(
1043 tx, GetNextBlockScriptFlags(m_active_chainstate.m_chain.Tip(),
1044 m_active_chainstate.m_chainman));
1045 });
1046 std::map<TxId, MempoolAcceptResult> results;
1047
1048 LOCK(m_pool.cs);
1049
1050 // Do all PreChecks first and fail fast to avoid running expensive script
1051 // checks when unnecessary.
1052 std::vector<TxId> valid_txids;
1053 for (Workspace &ws : workspaces) {
1054 if (!PreChecks(args, ws)) {
1056 "transaction failed");
1057 // Exit early to avoid doing pointless work. Update the failed tx
1058 // result; the rest are unfinished.
1059 results.emplace(ws.m_ptx->GetId(),
1062 std::move(results));
1063 }
1064 // Make the coins created by this transaction available for subsequent
1065 // transactions in the package to spend.
1066 m_viewmempool.PackageAddTransaction(ws.m_ptx);
1067 valid_txids.push_back(ws.m_ptx->GetId());
1068 }
1069
1070 // Transactions must meet two minimum feerates: the mempool minimum fee and
1071 // min relay fee. For transactions consisting of exactly one child and its
1072 // parents, it suffices to use the package feerate
1073 // (total modified fees / total size or vsize) to check this requirement.
1074 // Note that this is an aggregate feerate; this function has not checked
1075 // that there are transactions too low feerate to pay for themselves, or
1076 // that the child transactions are higher feerate than their parents. Using
1077 // aggregate feerate may allow "parents pay for child" behavior and permit
1078 // a child that is below mempool minimum feerate. To avoid these behaviors,
1079 // callers of AcceptMultipleTransactions need to restrict txns topology
1080 // (e.g. to ancestor sets) and check the feerates of individuals and
1081 // subsets.
1082 const auto m_total_size = std::accumulate(
1083 workspaces.cbegin(), workspaces.cend(), int64_t{0},
1084 [](int64_t sum, auto &ws) { return sum + ws.m_ptx->GetTotalSize(); });
1085 const auto m_total_vsize =
1086 std::accumulate(workspaces.cbegin(), workspaces.cend(), int64_t{0},
1087 [](int64_t sum, auto &ws) { return sum + ws.m_vsize; });
1088 const auto m_total_modified_fees = std::accumulate(
1089 workspaces.cbegin(), workspaces.cend(), Amount::zero(),
1090 [](Amount sum, auto &ws) { return sum + ws.m_modified_fees; });
1092 std::vector<TxId> all_package_txids;
1093 all_package_txids.reserve(workspaces.size());
1094 std::transform(workspaces.cbegin(), workspaces.cend(),
1095 std::back_inserter(all_package_txids),
1096 [](const auto &ws) { return ws.m_ptx->GetId(); });
1098 if (args.m_package_feerates &&
1102 "transaction failed");
1104 package_state, {{workspaces.back().m_ptx->GetId(),
1109 }
1110
1111 for (Workspace &ws : workspaces) {
1112 ws.m_package_feerate = package_feerate;
1113 const TxId &ws_txid = ws.m_ptx->GetId();
1114 if (args.m_test_accept &&
1115 std::find(valid_txids.begin(), valid_txids.end(), ws_txid) !=
1116 valid_txids.end()) {
1117 const auto effective_feerate =
1118 args.m_package_feerates
1119 ? ws.m_package_feerate
1120 : CFeeRate{ws.m_modified_fees,
1121 static_cast<uint32_t>(ws.m_vsize)};
1122 const auto effective_feerate_txids =
1123 args.m_package_feerates ? all_package_txids
1124 : std::vector<TxId>{ws.m_ptx->GetId()};
1125 // When test_accept=true, transactions that pass PreChecks
1126 // are valid because there are no further mempool checks (passing
1127 // PreChecks implies passing ConsensusScriptChecks).
1128 results.emplace(ws_txid,
1130 ws.m_vsize, ws.m_base_fees, effective_feerate,
1132 }
1133 }
1134
1135 if (args.m_test_accept) {
1137 }
1138
1140 // PackageValidationState filled in by SubmitPackage().
1142 }
1143
1145}
1146
1148MemPoolAccept::AcceptSubPackage(const std::vector<CTransactionRef> &subpackage,
1149 ATMPArgs &args) {
1151 AssertLockHeld(m_pool.cs);
1152
1153 auto result = [&]() EXCLUSIVE_LOCKS_REQUIRED(::cs_main, m_pool.cs) {
1154 if (subpackage.size() > 1) {
1156 }
1157 const auto &tx = subpackage.front();
1158 ATMPArgs single_args = ATMPArgs::SingleInPackageAccept(args);
1161 if (single_res.m_result_type !=
1164 "transaction failed");
1165 }
1167 {{tx->GetId(), single_res}});
1168 }();
1169
1170 // Clean up m_view and m_viewmempool so that other subpackage evaluations
1171 // don't have access to coins they shouldn't. Keep some coins in order to
1172 // minimize re-fetching coins from the UTXO set.
1173 //
1174 // There are 3 kinds of coins in m_view:
1175 // (1) Temporary coins from the transactions in subpackage, constructed by
1176 // m_viewmempool.
1177 // (2) Mempool coins from transactions in the mempool, constructed by
1178 // m_viewmempool.
1179 // (3) Confirmed coins fetched from our current UTXO set.
1180 //
1181 // (1) Temporary coins need to be removed, regardless of whether the
1182 // transaction was submitted. If the transaction was submitted to the
1183 // mempool, m_viewmempool will be able to fetch them from there. If it
1184 // wasn't submitted to mempool, it is incorrect to keep them - future calls
1185 // may try to spend those coins that don't actually exist.
1186 // (2) Mempool coins also need to be removed. If the mempool contents have
1187 // changed as a result of submitting or replacing transactions, coins
1188 // previously fetched from mempool may now be spent or nonexistent. Those
1189 // coins need to be deleted from m_view.
1190 // (3) Confirmed coins don't need to be removed. The chainstate has not
1191 // changed (we are holding cs_main and no blocks have been processed) so the
1192 // confirmed tx cannot disappear like a mempool tx can. The coin may now be
1193 // spent after we submitted a tx to mempool, but we have already checked
1194 // that the package does not have 2 transactions spending the same coin.
1195 // Keeping them in m_view is an optimization to not re-fetch confirmed coins
1196 // if we later look up inputs for this transaction again.
1197 for (const auto &outpoint : m_viewmempool.GetNonBaseCoins()) {
1198 // In addition to resetting m_viewmempool, we also need to manually
1199 // delete these coins from m_view because it caches copies of the coins
1200 // it fetched from m_viewmempool previously.
1201 m_view.Uncache(outpoint);
1202 }
1203 // This deletes the temporary and mempool coins.
1204 m_viewmempool.Reset();
1205 return result;
1206}
1207
1208PackageMempoolAcceptResult MemPoolAccept::AcceptPackage(const Package &package,
1209 ATMPArgs &args) {
1211 // Used if returning a PackageMempoolAcceptResult directly from this
1212 // function.
1214
1215 // Check that the package is well-formed. If it isn't, we won't try to
1216 // validate any of the transactions and thus won't return any
1217 // MempoolAcceptResults, just a package-wide error.
1218
1219 // Context-free package checks.
1222 }
1223
1224 // All transactions in the package must be a parent of the last transaction.
1225 // This is just an opportunity for us to fail fast on a context-free check
1226 // without taking the mempool lock.
1229 "package-not-child-with-parents");
1231 }
1232
1233 // IsChildWithParents() guarantees the package is > 1 transactions.
1234 assert(package.size() > 1);
1235 // The package must be 1 child with all of its unconfirmed parents. The
1236 // package is expected to be sorted, so the last transaction is the child.
1237 const auto &child = package.back();
1238 std::unordered_set<TxId, SaltedTxIdHasher> unconfirmed_parent_txids;
1239 std::transform(
1240 package.cbegin(), package.cend() - 1,
1242 [](const auto &tx) { return tx->GetId(); });
1243
1244 // All child inputs must refer to a preceding package transaction or a
1245 // confirmed UTXO. The only way to verify this is to look up the child's
1246 // inputs in our current coins view (not including mempool), and enforce
1247 // that all parents not present in the package be available at chain tip.
1248 // Since this check can bring new coins into the coins cache, keep track of
1249 // these coins and uncache them if we don't end up submitting this package
1250 // to the mempool.
1251 const CCoinsViewCache &coins_tip_cache = m_active_chainstate.CoinsTip();
1252 for (const auto &input : child->vin) {
1253 if (!coins_tip_cache.HaveCoinInCache(input.prevout)) {
1254 args.m_coins_to_uncache.push_back(input.prevout);
1255 }
1256 }
1257 // Using the MemPoolAccept m_view cache allows us to look up these same
1258 // coins faster later. This should be connecting directly to CoinsTip, not
1259 // to m_viewmempool, because we specifically require inputs to be confirmed
1260 // if they aren't in the package.
1261 m_view.SetBackend(m_active_chainstate.CoinsTip());
1262 const auto package_or_confirmed = [this, &unconfirmed_parent_txids](
1263 const auto &input) {
1264 return unconfirmed_parent_txids.count(input.prevout.GetTxId()) > 0 ||
1265 m_view.HaveCoin(input.prevout);
1266 };
1267 if (!std::all_of(child->vin.cbegin(), child->vin.cend(),
1271 "package-not-child-with-unconfirmed-parents");
1273 }
1274 // Protect against bugs where we pull more inputs from disk that miss being
1275 // added to coins_to_uncache. The backend will be connected again when
1276 // needed in PreChecks.
1277 m_view.SetBackend(m_dummy);
1278
1279 LOCK(m_pool.cs);
1280 // Stores results from which we will create the returned
1281 // PackageMempoolAcceptResult. A result may be changed if a mempool
1282 // transaction is evicted later due to LimitMempoolSize().
1283 std::map<TxId, MempoolAcceptResult> results_final;
1284 // Results from individual validation which will be returned if no other
1285 // result is available for this transaction. "Nonfinal" because if a
1286 // transaction fails by itself but succeeds later (i.e. when evaluated with
1287 // a fee-bumping child), the result in this map may be discarded.
1288 std::map<TxId, MempoolAcceptResult> individual_results_nonfinal;
1289 bool quit_early{false};
1290 std::vector<CTransactionRef> txns_package_eval;
1291 for (const auto &tx : package) {
1292 const auto &txid = tx->GetId();
1293 // An already confirmed tx is treated as one not in mempool, because all
1294 // we know is that the inputs aren't available.
1295 if (m_pool.exists(txid)) {
1296 // Exact transaction already exists in the mempool.
1297 // Node operators are free to set their mempool policies however
1298 // they please, nodes may receive transactions in different orders,
1299 // and malicious counterparties may try to take advantage of policy
1300 // differences to pin or delay propagation of transactions. As such,
1301 // it's possible for some package transaction(s) to already be in
1302 // the mempool, and we don't want to reject the entire package in
1303 // that case (as that could be a censorship vector). De-duplicate
1304 // the transactions that are already in the mempool, and only call
1305 // AcceptMultipleTransactions() with the new transactions. This
1306 // ensures we don't double-count transaction counts and sizes when
1307 // checking ancestor/descendant limits, or double-count transaction
1308 // fees for fee-related policy.
1309 auto iter = m_pool.GetIter(txid);
1310 assert(iter != std::nullopt);
1312 (*iter.value())->GetTxSize(),
1313 (*iter.value())->GetFee()));
1314 } else {
1315 // Transaction does not already exist in the mempool.
1316 // Try submitting the transaction on its own.
1317 const auto single_package_res = AcceptSubPackage({tx}, args);
1318 const auto &single_res = single_package_res.m_tx_results.at(txid);
1319 if (single_res.m_result_type ==
1321 // The transaction succeeded on its own and is now in the
1322 // mempool. Don't include it in package validation, because its
1323 // fees should only be "used" once.
1324 assert(m_pool.exists(txid));
1325 results_final.emplace(txid, single_res);
1326 } else if (single_res.m_state.GetResult() !=
1328 single_res.m_state.GetResult() !=
1330 // Package validation policy only differs from individual policy
1331 // in its evaluation of feerate. For example, if a transaction
1332 // fails here due to violation of a consensus rule, the result
1333 // will not change when it is submitted as part of a package. To
1334 // minimize the amount of repeated work, unless the transaction
1335 // fails due to feerate or missing inputs (its parent is a
1336 // previous transaction in the package that failed due to
1337 // feerate), don't run package validation. Note that this
1338 // decision might not make sense if different types of packages
1339 // are allowed in the future. Continue individually validating
1340 // the rest of the transactions, because some of them may still
1341 // be valid.
1342 quit_early = true;
1344 PackageValidationResult::PCKG_TX, "transaction failed");
1346 } else {
1348 txns_package_eval.push_back(tx);
1349 }
1350 }
1351 }
1352
1354 quit_early || txns_package_eval.empty()
1359
1360 // Make sure we haven't exceeded max mempool size.
1361 // Package transactions that were submitted to mempool or already in mempool
1362 // may be evicted.
1363 m_pool.LimitSize(m_active_chainstate.CoinsTip());
1364
1365 for (const auto &tx : package) {
1366 const auto &txid = tx->GetId();
1367 if (multi_submission_result.m_tx_results.count(txid) > 0) {
1368 // We shouldn't have re-submitted if the tx result was already in
1369 // results_final.
1370 Assume(results_final.count(txid) == 0);
1371 // If it was submitted, check to see if the tx is still in the
1372 // mempool. It could have been evicted due to LimitMempoolSize()
1373 // above.
1374 const auto &txresult =
1375 multi_submission_result.m_tx_results.at(txid);
1376 if (txresult.m_result_type ==
1378 !m_pool.exists(txid)) {
1380 "transaction failed");
1384 results_final.emplace(
1386 } else {
1387 results_final.emplace(txid, txresult);
1388 }
1389 } else if (const auto final_it{results_final.find(txid)};
1390 final_it != results_final.end()) {
1391 // Already-in-mempool transaction. Check to see if it's still there,
1392 // as it could have been evicted when LimitMempoolSize() was called.
1393 Assume(final_it->second.m_result_type !=
1395 Assume(individual_results_nonfinal.count(txid) == 0);
1396 if (!m_pool.exists(tx->GetId())) {
1398 "transaction failed");
1402 // Replace the previous result.
1403 results_final.erase(txid);
1404 results_final.emplace(
1406 }
1407 } else if (const auto non_final_it{
1408 individual_results_nonfinal.find(txid)};
1410 Assume(non_final_it->second.m_result_type ==
1412 // Interesting result from previous processing.
1413 results_final.emplace(txid, non_final_it->second);
1414 }
1415 }
1416 Assume(results_final.size() == package.size());
1418 std::move(results_final));
1419}
1420} // namespace
1421
1423 const CTransactionRef &tx,
1425 bool test_accept,
1426 unsigned int heightOverride) {
1428 assert(active_chainstate.GetMempool() != nullptr);
1429 CTxMemPool &pool{*active_chainstate.GetMempool()};
1430
1431 std::vector<COutPoint> coins_to_uncache;
1432 auto args = MemPoolAccept::ATMPArgs::SingleAccept(
1433 active_chainstate.m_chainman.GetConfig(), accept_time, bypass_limits,
1435 const MempoolAcceptResult result = MemPoolAccept(pool, active_chainstate)
1436 .AcceptSingleTransaction(tx, args);
1438 // Remove coins that were not present in the coins cache before calling
1439 // ATMPW; this is to prevent memory DoS in case we receive a large
1440 // number of invalid transactions that attempt to overrun the in-memory
1441 // coins cache
1442 // (`CCoinsViewCache::cacheCoins`).
1443
1444 for (const COutPoint &outpoint : coins_to_uncache) {
1445 active_chainstate.CoinsTip().Uncache(outpoint);
1446 }
1447 }
1448
1449 // After we've (potentially) uncached entries, ensure our coins cache is
1450 // still within its size limits
1453 return result;
1454}
1455
1457 CTxMemPool &pool,
1458 const Package &package,
1459 bool test_accept) {
1461 assert(!package.empty());
1462 assert(std::all_of(package.cbegin(), package.cend(),
1463 [](const auto &tx) { return tx != nullptr; }));
1464
1465 const Config &config = active_chainstate.m_chainman.GetConfig();
1466
1467 std::vector<COutPoint> coins_to_uncache;
1468 const auto result = [&]() EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
1470 if (test_accept) {
1471 auto args = MemPoolAccept::ATMPArgs::PackageTestAccept(
1472 config, GetTime(), coins_to_uncache);
1473 return MemPoolAccept(pool, active_chainstate)
1474 .AcceptMultipleTransactions(package, args);
1475 } else {
1476 auto args = MemPoolAccept::ATMPArgs::PackageChildWithParents(
1477 config, GetTime(), coins_to_uncache);
1478 return MemPoolAccept(pool, active_chainstate)
1479 .AcceptPackage(package, args);
1480 }
1481 }();
1482
1483 // Uncache coins pertaining to transactions that were not submitted to the
1484 // mempool.
1485 if (test_accept || result.m_state.IsInvalid()) {
1486 for (const COutPoint &hashTx : coins_to_uncache) {
1487 active_chainstate.CoinsTip().Uncache(hashTx);
1488 }
1489 }
1490 // Ensure the coins cache is still within limits.
1493 return result;
1494}
1495
1497 int halvings = nHeight / consensusParams.nSubsidyHalvingInterval;
1498 // Force block reward to zero when right shift is undefined.
1499 if (halvings >= 64) {
1500 return Amount::zero();
1501 }
1502
1503 Amount nSubsidy = 50 * COIN;
1504 // Subsidy is cut in half every 210,000 blocks which will occur
1505 // approximately every 4 years.
1506 return ((nSubsidy / SATOSHI) >> halvings) * SATOSHI;
1507}
1508
1512
1513void CoinsViews::InitCache() {
1515 m_cacheview = std::make_unique<CCoinsViewCache>(&m_catcherview);
1516}
1517
1519 ChainstateManager &chainman,
1520 std::optional<BlockHash> from_snapshot_blockhash)
1521 : m_mempool(mempool), m_blockman(blockman), m_chainman(chainman),
1522 m_from_snapshot_blockhash(from_snapshot_blockhash) {}
1523
1525 bool should_wipe, std::string leveldb_name) {
1528 }
1529
1530 m_coins_views = std::make_unique<CoinsViews>(
1532 .cache_bytes = cache_size_bytes,
1533 .memory_only = in_memory,
1534 .wipe_data = should_wipe,
1535 .obfuscate = true,
1536 .options = m_chainman.m_options.coins_db},
1538}
1539
1540void Chainstate::InitCoinsCache(size_t cache_size_bytes) {
1542 assert(m_coins_views != nullptr);
1544 m_coins_views->InitCache();
1545}
1546
1547// Note that though this is marked const, we may end up modifying
1548// `m_cached_finished_ibd`, which is a performance-related implementation
1549// detail. This function must be marked `const` so that `CValidationInterface`
1550// clients (which are given a `const Chainstate*`) can call it.
1551//
1553 // Optimization: pre-test latch before taking the lock.
1554 if (m_cached_finished_ibd.load(std::memory_order_relaxed)) {
1555 return false;
1556 }
1557
1558 LOCK(cs_main);
1559 if (m_cached_finished_ibd.load(std::memory_order_relaxed)) {
1560 return false;
1561 }
1563 return true;
1564 }
1565 if (m_chain.Tip() == nullptr) {
1566 return true;
1567 }
1569 return true;
1570 }
1571 if (m_chain.Tip()->Time() <
1573 return true;
1574 }
1575 LogPrintf("Leaving InitialBlockDownload (latching to false)\n");
1576 m_cached_finished_ibd.store(true, std::memory_order_relaxed);
1577 return false;
1578}
1579
1582
1583 // Before we get past initial download, we cannot reliably alert about forks
1584 // (we assume we don't get stuck on a fork before finishing our initial
1585 // sync)
1586 if (IsInitialBlockDownload()) {
1587 return;
1588 }
1589
1590 // If our best fork is no longer within 72 blocks (+/- 12 hours if no one
1591 // mines it) of our head, or if it is back on the active chain, drop it
1594 m_best_fork_tip = nullptr;
1595 }
1596
1597 if (m_best_fork_tip ||
1598 (m_chainman.m_best_invalid &&
1599 m_chainman.m_best_invalid->nChainWork >
1600 m_chain.Tip()->nChainWork + (GetBlockProof(*m_chain.Tip()) * 6))) {
1602 std::string warning =
1603 std::string("'Warning: Large-work fork detected, forking after "
1604 "block ") +
1605 m_best_fork_base->phashBlock->ToString() + std::string("'");
1607 }
1608
1610 LogPrintf("%s: Warning: Large fork found\n forking the "
1611 "chain at height %d (%s)\n lasting to height %d "
1612 "(%s).\nChain state database corruption likely.\n",
1618 } else {
1619 LogPrintf("%s: Warning: Found invalid chain at least ~6 blocks "
1620 "longer than our best chain.\nChain state database "
1621 "corruption likely.\n",
1622 __func__);
1624 }
1625 } else {
1628 }
1629}
1630
1634
1635 // If we are on a fork that is sufficiently large, set a warning flag.
1637
1638 // We define a condition where we should warn the user about as a fork of at
1639 // least 7 blocks with a tip within 72 blocks (+/- 12 hours if no one mines
1640 // it) of ours. We use 7 blocks rather arbitrarily as it represents just
1641 // under 10% of sustained network hash rate operating on the fork, or a
1642 // chain that is entirely longer than ours and invalid (note that this
1643 // should be detected by both). We define it this way because it allows us
1644 // to only store the highest fork tip (+ base) which meets the 7-block
1645 // condition and from this always have the most-likely-to-cause-warning fork
1646 if (pfork &&
1647 (!m_best_fork_tip ||
1649 pindexNewForkTip->nChainWork - pfork->nChainWork >
1650 (GetBlockProof(*pfork) * 7) &&
1651 m_chain.Height() - pindexNewForkTip->nHeight < 72) {
1654 }
1655
1657}
1658
1659// Called both upon regular invalid block discovery *and* InvalidateBlock
1662 if (!m_chainman.m_best_invalid ||
1663 pindexNew->nChainWork > m_chainman.m_best_invalid->nChainWork) {
1664 m_chainman.m_best_invalid = pindexNew;
1665 }
1666 if (m_chainman.m_best_header != nullptr &&
1667 m_chainman.m_best_header->GetAncestor(pindexNew->nHeight) ==
1668 pindexNew) {
1669 m_chainman.m_best_header = m_chain.Tip();
1670 }
1671
1672 // If the invalid chain found is supposed to be finalized, we need to move
1673 // back the finalization point.
1677 }
1678
1679 LogPrintf("%s: invalid block=%s height=%d log2_work=%f date=%s\n",
1680 __func__, pindexNew->GetBlockHash().ToString(),
1681 pindexNew->nHeight,
1682 log(pindexNew->nChainWork.getdouble()) / log(2.0),
1683 FormatISO8601DateTime(pindexNew->GetBlockTime()));
1685 assert(tip);
1686 LogPrintf("%s: current best=%s height=%d log2_work=%f date=%s\n",
1687 __func__, tip->GetBlockHash().ToString(), m_chain.Height(),
1688 log(tip->nChainWork.getdouble()) / log(2.0),
1689 FormatISO8601DateTime(tip->GetBlockTime()));
1690}
1691
1692// Same as InvalidChainFound, above, except not called directly from
1693// InvalidateBlock, which does its own setBlockIndexCandidates management.
1695 const BlockValidationState &state) {
1698 pindex->nStatus = pindex->nStatus.withFailed();
1699 m_chainman.m_failed_blocks.insert(pindex);
1700 m_blockman.m_dirty_blockindex.insert(pindex);
1701 InvalidChainFound(pindex);
1702 }
1703}
1704
1706 int nHeight) {
1707 // Mark inputs spent.
1708 if (tx.IsCoinBase()) {
1709 return;
1710 }
1711
1712 txundo.vprevout.reserve(tx.vin.size());
1713 for (const CTxIn &txin : tx.vin) {
1714 txundo.vprevout.emplace_back();
1715 bool is_spent = view.SpendCoin(txin.prevout, &txundo.vprevout.back());
1716 assert(is_spent);
1717 }
1718}
1719
1721 int nHeight) {
1723 AddCoins(view, tx, nHeight);
1724}
1725
1727 const CScript &scriptSig = ptxTo->vin[nIn].scriptSig;
1728 if (!VerifyScript(scriptSig, m_tx_out.scriptPubKey, nFlags,
1731 metrics, &error)) {
1732 return false;
1733 }
1734 if ((pTxLimitSigChecks &&
1738 // we can't assign a meaningful script error (since the script
1739 // succeeded), but remove the ScriptError::OK which could be
1740 // misinterpreted.
1742 return false;
1743 }
1744 return true;
1745}
1746
1748 const CCoinsViewCache &inputs, const uint32_t flags,
1750 const PrecomputedTransactionData &txdata,
1752 CheckInputsLimiter *pBlockLimitSigChecks,
1753 std::vector<CScriptCheck> *pvChecks) {
1755 assert(!tx.IsCoinBase());
1756
1757 if (pvChecks) {
1758 pvChecks->reserve(tx.vin.size());
1759 }
1760
1761 // First check if script executions have been cached with the same flags.
1762 // Note that this assumes that the inputs provided are correct (ie that the
1763 // transaction hash which is in tx's prevouts properly commits to the
1764 // scriptPubKey in the inputs view of that transaction).
1767 if (!txLimitSigChecks.consume_and_check(nSigChecksOut) ||
1768 (pBlockLimitSigChecks &&
1769 !pBlockLimitSigChecks->consume_and_check(nSigChecksOut))) {
1771 "too-many-sigchecks");
1772 }
1773 return true;
1774 }
1775
1776 int nSigChecksTotal = 0;
1777
1778 for (size_t i = 0; i < tx.vin.size(); i++) {
1779 const COutPoint &prevout = tx.vin[i].prevout;
1780 const Coin &coin = inputs.AccessCoin(prevout);
1781 assert(!coin.IsSpent());
1782
1783 // We very carefully only pass in things to CScriptCheck which are
1784 // clearly committed to by tx's hash. This provides a sanity
1785 // check that our caching is not introducing consensus failures through
1786 // additional data in, eg, the coins being spent being checked as a part
1787 // of CScriptCheck.
1788
1789 // Verify signature
1790 CScriptCheck check(coin.GetTxOut(), tx, i, flags, sigCacheStore, txdata,
1791 &txLimitSigChecks, pBlockLimitSigChecks);
1792
1793 // If pvChecks is not null, defer the check execution to the caller.
1794 if (pvChecks) {
1795 pvChecks->push_back(std::move(check));
1796 continue;
1797 }
1798
1799 if (!check()) {
1800 ScriptError scriptError = check.GetScriptError();
1801 // Compute flags without the optional standardness flags.
1802 // This differs from MANDATORY_SCRIPT_VERIFY_FLAGS as it contains
1803 // additional upgrade flags (see AcceptToMemoryPoolWorker variable
1804 // extraFlags).
1807 if (flags != mandatoryFlags) {
1808 // Check whether the failure was caused by a non-mandatory
1809 // script verification check. If so, ensure we return
1810 // NOT_STANDARD instead of CONSENSUS to avoid downstream users
1811 // splitting the network between upgraded and non-upgraded nodes
1812 // by banning CONSENSUS-failing data providers.
1814 sigCacheStore, txdata);
1815 if (check2()) {
1816 return state.Invalid(
1818 strprintf("non-mandatory-script-verify-flag (%s)",
1819 ScriptErrorString(scriptError)));
1820 }
1821 // update the error message to reflect the mandatory violation.
1822 scriptError = check2.GetScriptError();
1823 }
1824
1825 // MANDATORY flag failures correspond to
1826 // TxValidationResult::TX_CONSENSUS. Because CONSENSUS failures are
1827 // the most serious case of validation failures, we may need to
1828 // consider using RECENT_CONSENSUS_CHANGE for any script failure
1829 // that could be due to non-upgraded nodes which we may want to
1830 // support, to avoid splitting the network (but this depends on the
1831 // details of how net_processing handles such errors).
1832 return state.Invalid(
1834 strprintf("mandatory-script-verify-flag-failed (%s)",
1835 ScriptErrorString(scriptError)));
1836 }
1837
1839 }
1840
1842
1843 if (scriptCacheStore && !pvChecks) {
1844 // We executed all of the provided scripts, and were told to cache the
1845 // result. Do so now.
1847 }
1848
1849 return true;
1850}
1851
1852bool AbortNode(BlockValidationState &state, const std::string &strMessage,
1853 const bilingual_str &userMessage) {
1855 return state.Error(strMessage);
1856}
1857
1860 const COutPoint &out) {
1861 bool fClean = true;
1862
1863 if (view.HaveCoin(out)) {
1864 // Overwriting transaction output.
1865 fClean = false;
1866 }
1867
1868 if (undo.GetHeight() == 0) {
1869 // Missing undo metadata (height and coinbase). Older versions included
1870 // this information only in undo records for the last spend of a
1871 // transactions' outputs. This implies that it must be present for some
1872 // other output of the same tx.
1873 const Coin &alternate = AccessByTxid(view, out.GetTxId());
1874 if (alternate.IsSpent()) {
1875 // Adding output for transaction without known metadata
1877 }
1878
1879 // This is somewhat ugly, but hopefully utility is limited. This is only
1880 // useful when working from legacy on disck data. In any case, putting
1881 // the correct information in there doesn't hurt.
1882 const_cast<Coin &>(undo) = Coin(undo.GetTxOut(), alternate.GetHeight(),
1883 alternate.IsCoinBase());
1884 }
1885
1886 // If the coin already exists as an unspent coin in the cache, then the
1887 // possible_overwrite parameter to AddCoin must be set to true. We have
1888 // already checked whether an unspent coin exists above using HaveCoin, so
1889 // we don't need to guess. When fClean is false, an unspent coin already
1890 // existed and it is an overwrite.
1891 view.AddCoin(out, std::move(undo), !fClean);
1892
1894}
1895
1900DisconnectResult Chainstate::DisconnectBlock(const CBlock &block,
1901 const CBlockIndex *pindex,
1905 if (!m_blockman.UndoReadFromDisk(blockUndo, *pindex)) {
1906 error("DisconnectBlock(): failure reading undo data");
1908 }
1909
1910 return ApplyBlockUndo(std::move(blockUndo), block, pindex, view);
1911}
1912
1914 const CBlockIndex *pindex,
1916 bool fClean = true;
1917
1918 if (blockUndo.vtxundo.size() + 1 != block.vtx.size()) {
1919 error("DisconnectBlock(): block and undo data inconsistent");
1921 }
1922
1923 // First, restore inputs.
1924 for (size_t i = 1; i < block.vtx.size(); i++) {
1925 const CTransaction &tx = *(block.vtx[i]);
1926 CTxUndo &txundo = blockUndo.vtxundo[i - 1];
1927 if (txundo.vprevout.size() != tx.vin.size()) {
1928 error("DisconnectBlock(): transaction and undo data inconsistent");
1930 }
1931
1932 for (size_t j = 0; j < tx.vin.size(); j++) {
1933 const COutPoint &out = tx.vin[j].prevout;
1935 UndoCoinSpend(std::move(txundo.vprevout[j]), view, out);
1938 }
1940 }
1941 // At this point, all of txundo.vprevout should have been moved out.
1942 }
1943
1944 // Second, revert created outputs.
1945 for (const auto &ptx : block.vtx) {
1946 const CTransaction &tx = *ptx;
1947 const TxId &txid = tx.GetId();
1948 const bool is_coinbase = tx.IsCoinBase();
1949
1950 // Check that all outputs are available and match the outputs in the
1951 // block itself exactly.
1952 for (size_t o = 0; o < tx.vout.size(); o++) {
1953 if (tx.vout[o].scriptPubKey.IsUnspendable()) {
1954 continue;
1955 }
1956
1957 COutPoint out(txid, o);
1958 Coin coin;
1959 bool is_spent = view.SpendCoin(out, &coin);
1960 if (!is_spent || tx.vout[o] != coin.GetTxOut() ||
1961 uint32_t(pindex->nHeight) != coin.GetHeight() ||
1962 is_coinbase != coin.IsCoinBase()) {
1963 // transaction output mismatch
1964 fClean = false;
1965 }
1966 }
1967 }
1968
1969 // Move best block pointer to previous block.
1970 view.SetBestBlock(block.hashPrevBlock);
1971
1973}
1974
1976
1978 scriptcheckqueue.StartWorkerThreads(threads_num);
1979}
1980
1982 scriptcheckqueue.StopWorkerThreads();
1983}
1984
1985// Returns the script flags which should be checked for the block after
1986// the given block.
1988 const ChainstateManager &chainman) {
1990
1992
1993 // Enforce P2SH (BIP16)
1994 if (DeploymentActiveAfter(pindex, chainman, Consensus::DEPLOYMENT_P2SH)) {
1996 }
1997
1998 // Enforce the DERSIG (BIP66) rule.
1999 if (DeploymentActiveAfter(pindex, chainman, Consensus::DEPLOYMENT_DERSIG)) {
2001 }
2002
2003 // Start enforcing CHECKLOCKTIMEVERIFY (BIP65) rule.
2004 if (DeploymentActiveAfter(pindex, chainman, Consensus::DEPLOYMENT_CLTV)) {
2006 }
2007
2008 // Start enforcing CSV (BIP68, BIP112 and BIP113) rule.
2009 if (DeploymentActiveAfter(pindex, chainman, Consensus::DEPLOYMENT_CSV)) {
2011 }
2012
2013 // If the UAHF is enabled, we start accepting replay protected txns
2014 if (IsUAHFenabled(consensusparams, pindex)) {
2017 }
2018
2019 // If the DAA HF is enabled, we start rejecting transaction that use a high
2020 // s in their signature. We also make sure that signature that are supposed
2021 // to fail (for instance in multisig or other forms of smart contracts) are
2022 // null.
2023 if (IsDAAEnabled(consensusparams, pindex)) {
2026 }
2027
2028 // When the magnetic anomaly fork is enabled, we start accepting
2029 // transactions using the OP_CHECKDATASIG opcode and it's verify
2030 // alternative. We also start enforcing push only signatures and
2031 // clean stack.
2035 }
2036
2037 if (IsGravitonEnabled(consensusparams, pindex)) {
2040 }
2041
2042 if (IsPhononEnabled(consensusparams, pindex)) {
2044 }
2045
2046 // We make sure this node will have replay protection during the next hard
2047 // fork.
2050 }
2051
2052 return flags;
2053}
2054
2062
2069bool Chainstate::ConnectBlock(const CBlock &block, BlockValidationState &state,
2072 bool fJustCheck) {
2074 assert(pindex);
2075
2076 const BlockHash block_hash{block.GetHash()};
2077 assert(*pindex->phashBlock == block_hash);
2078
2080
2081 const CChainParams &params{m_chainman.GetParams()};
2082 const Consensus::Params &consensusParams = params.GetConsensus();
2083
2084 // Check it again in case a previous version let a bad block in
2085 // NOTE: We don't currently (re-)invoke ContextualCheckBlock() or
2086 // ContextualCheckBlockHeader() here. This means that if we add a new
2087 // consensus rule that is enforced in one of those two functions, then we
2088 // may have let in a block that violates the rule prior to updating the
2089 // software, and we would NOT be enforcing the rule here. Fully solving
2090 // upgrade from one software version to the next after a consensus rule
2091 // change is potentially tricky and issue-specific.
2092 // Also, currently the rule against blocks more than 2 hours in the future
2093 // is enforced in ContextualCheckBlockHeader(); we wouldn't want to
2094 // re-enforce that rule here (at least until we make it impossible for
2095 // m_adjusted_time_callback() to go backward).
2096 if (!CheckBlock(block, state, consensusParams,
2097 options.withCheckPoW(!fJustCheck)
2100 // We don't write down blocks to disk if they may have been
2101 // corrupted, so this should be impossible unless we're having
2102 // hardware problems.
2103 return AbortNode(state, "Corrupt block found indicating potential "
2104 "hardware failure; shutting down");
2105 }
2106 return error("%s: Consensus::CheckBlock: %s", __func__,
2107 state.ToString());
2108 }
2109
2110 // Verify that the view's current state corresponds to the previous block
2111 BlockHash hashPrevBlock =
2112 pindex->pprev == nullptr ? BlockHash() : pindex->pprev->GetBlockHash();
2113 assert(hashPrevBlock == view.GetBestBlock());
2114
2115 nBlocksTotal++;
2116
2117 // Special case for the genesis block, skipping connection of its
2118 // transactions (its coinbase is unspendable)
2119 if (block_hash == consensusParams.hashGenesisBlock) {
2120 if (!fJustCheck) {
2121 view.SetBestBlock(pindex->GetBlockHash());
2122 }
2123
2124 return true;
2125 }
2126
2127 bool fScriptChecks = true;
2129 // We've been configured with the hash of a block which has been
2130 // externally verified to have a valid history. A suitable default value
2131 // is included with the software and updated from time to time. Because
2132 // validity relative to a piece of software is an objective fact these
2133 // defaults can be easily reviewed. This setting doesn't force the
2134 // selection of any particular chain but makes validating some faster by
2135 // effectively caching the result of part of the verification.
2136 BlockMap::const_iterator it{
2137 m_blockman.m_block_index.find(m_chainman.AssumedValidBlock())};
2138 if (it != m_blockman.m_block_index.end()) {
2139 if (it->second.GetAncestor(pindex->nHeight) == pindex &&
2140 m_chainman.m_best_header->GetAncestor(pindex->nHeight) ==
2141 pindex &&
2142 m_chainman.m_best_header->nChainWork >=
2144 // This block is a member of the assumed verified chain and an
2145 // ancestor of the best header.
2146 // Script verification is skipped when connecting blocks under
2147 // the assumevalid block. Assuming the assumevalid block is
2148 // valid this is safe because block merkle hashes are still
2149 // computed and checked, Of course, if an assumed valid block is
2150 // invalid due to false scriptSigs this optimization would allow
2151 // an invalid chain to be accepted.
2152 // The equivalent time check discourages hash power from
2153 // extorting the network via DOS attack into accepting an
2154 // invalid block through telling users they must manually set
2155 // assumevalid. Requiring a software change or burying the
2156 // invalid block, regardless of the setting, makes it hard to
2157 // hide the implication of the demand. This also avoids having
2158 // release candidates that are hardly doing any signature
2159 // verification at all in testing without having to artificially
2160 // set the default assumed verified block further back. The test
2161 // against the minimum chain work prevents the skipping when
2162 // denied access to any chain at least as good as the expected
2163 // chain.
2165 *m_chainman.m_best_header, *pindex,
2166 *m_chainman.m_best_header,
2167 consensusParams) <= 60 * 60 * 24 * 7 * 2);
2168 }
2169 }
2170 }
2171
2174 LogPrint(BCLog::BENCH, " - Sanity checks: %.2fms [%.2fs (%.2fms/blk)]\n",
2177
2178 // Do not allow blocks that contain transactions which 'overwrite' older
2179 // transactions, unless those are already completely spent. If such
2180 // overwrites are allowed, coinbases and transactions depending upon those
2181 // can be duplicated to remove the ability to spend the first instance --
2182 // even after being sent to another address.
2183 // See BIP30, CVE-2012-1909, and http://r6.ca/blog/20120206T005236Z.html
2184 // for more information. This rule was originally applied to all blocks
2185 // with a timestamp after March 15, 2012, 0:00 UTC. Now that the whole
2186 // chain is irreversibly beyond that time it is applied to all blocks
2187 // except the two in the chain that violate it. This prevents exploiting
2188 // the issue against nodes during their initial block download.
2189 bool fEnforceBIP30 = !((pindex->nHeight == 91842 &&
2190 pindex->GetBlockHash() ==
2191 uint256S("0x00000000000a4d0a398161ffc163c503763"
2192 "b1f4360639393e0e4c8e300e0caec")) ||
2193 (pindex->nHeight == 91880 &&
2194 pindex->GetBlockHash() ==
2195 uint256S("0x00000000000743f190a18c5577a3c2d2a1f"
2196 "610ae9601ac046a38084ccb7cd721")));
2197
2198 // Once BIP34 activated it was not possible to create new duplicate
2199 // coinbases and thus other than starting with the 2 existing duplicate
2200 // coinbase pairs, not possible to create overwriting txs. But by the time
2201 // BIP34 activated, in each of the existing pairs the duplicate coinbase had
2202 // overwritten the first before the first had been spent. Since those
2203 // coinbases are sufficiently buried it's no longer possible to create
2204 // further duplicate transactions descending from the known pairs either. If
2205 // we're on the known chain at height greater than where BIP34 activated, we
2206 // can save the db accesses needed for the BIP30 check.
2207
2208 // BIP34 requires that a block at height X (block X) has its coinbase
2209 // scriptSig start with a CScriptNum of X (indicated height X). The above
2210 // logic of no longer requiring BIP30 once BIP34 activates is flawed in the
2211 // case that there is a block X before the BIP34 height of 227,931 which has
2212 // an indicated height Y where Y is greater than X. The coinbase for block
2213 // X would also be a valid coinbase for block Y, which could be a BIP30
2214 // violation. An exhaustive search of all mainnet coinbases before the
2215 // BIP34 height which have an indicated height greater than the block height
2216 // reveals many occurrences. The 3 lowest indicated heights found are
2217 // 209,921, 490,897, and 1,983,702 and thus coinbases for blocks at these 3
2218 // heights would be the first opportunity for BIP30 to be violated.
2219
2220 // The search reveals a great many blocks which have an indicated height
2221 // greater than 1,983,702, so we simply remove the optimization to skip
2222 // BIP30 checking for blocks at height 1,983,702 or higher. Before we reach
2223 // that block in another 25 years or so, we should take advantage of a
2224 // future consensus change to do a new and improved version of BIP34 that
2225 // will actually prevent ever creating any duplicate coinbases in the
2226 // future.
2227 static constexpr int BIP34_IMPLIES_BIP30_LIMIT = 1983702;
2228
2229 // There is no potential to create a duplicate coinbase at block 209,921
2230 // because this is still before the BIP34 height and so explicit BIP30
2231 // checking is still active.
2232
2233 // The final case is block 176,684 which has an indicated height of
2234 // 490,897. Unfortunately, this issue was not discovered until about 2 weeks
2235 // before block 490,897 so there was not much opportunity to address this
2236 // case other than to carefully analyze it and determine it would not be a
2237 // problem. Block 490,897 was, in fact, mined with a different coinbase than
2238 // block 176,684, but it is important to note that even if it hadn't been or
2239 // is remined on an alternate fork with a duplicate coinbase, we would still
2240 // not run into a BIP30 violation. This is because the coinbase for 176,684
2241 // is spent in block 185,956 in transaction
2242 // d4f7fbbf92f4a3014a230b2dc70b8058d02eb36ac06b4a0736d9d60eaa9e8781. This
2243 // spending transaction can't be duplicated because it also spends coinbase
2244 // 0328dd85c331237f18e781d692c92de57649529bd5edf1d01036daea32ffde29. This
2245 // coinbase has an indicated height of over 4.2 billion, and wouldn't be
2246 // duplicatable until that height, and it's currently impossible to create a
2247 // chain that long. Nevertheless we may wish to consider a future soft fork
2248 // which retroactively prevents block 490,897 from creating a duplicate
2249 // coinbase. The two historical BIP30 violations often provide a confusing
2250 // edge case when manipulating the UTXO and it would be simpler not to have
2251 // another edge case to deal with.
2252
2253 // testnet3 has no blocks before the BIP34 height with indicated heights
2254 // post BIP34 before approximately height 486,000,000 and presumably will
2255 // be reset before it reaches block 1,983,702 and starts doing unnecessary
2256 // BIP30 checking again.
2257 assert(pindex->pprev);
2259 pindex->pprev->GetAncestor(consensusParams.BIP34Height);
2260 // Only continue to enforce if we're below BIP34 activation height or the
2261 // block hash at that height doesn't correspond.
2263 fEnforceBIP30 &&
2265 !(pindexBIP34height->GetBlockHash() == consensusParams.BIP34Hash));
2266
2267 // TODO: Remove BIP30 checking from block height 1,983,702 on, once we have
2268 // a consensus change that ensures coinbases at those heights can not
2269 // duplicate earlier coinbases.
2271 for (const auto &tx : block.vtx) {
2272 for (size_t o = 0; o < tx->vout.size(); o++) {
2273 if (view.HaveCoin(COutPoint(tx->GetId(), o))) {
2274 LogPrintf("ERROR: ConnectBlock(): tried to overwrite "
2275 "transaction\n");
2277 "bad-txns-BIP30");
2278 }
2279 }
2280 }
2281 }
2282
2283 // Enforce BIP68 (sequence locks).
2284 int nLockTimeFlags = 0;
2288 }
2289
2291
2294 LogPrint(BCLog::BENCH, " - Fork checks: %.2fms [%.2fs (%.2fms/blk)]\n",
2297
2298 std::vector<int> prevheights;
2299 Amount nFees = Amount::zero();
2300 int nInputs = 0;
2301
2302 // Limit the total executed signature operations in the block, a consensus
2303 // rule. Tracking during the CPU-consuming part (validation of uncached
2304 // inputs) is per-input atomic and validation in each thread stops very
2305 // quickly after the limit is exceeded, so an adversary cannot cause us to
2306 // exceed the limit by much at all.
2309
2310 std::vector<TxSigCheckLimiter> nSigChecksTxLimiters;
2311 nSigChecksTxLimiters.resize(block.vtx.size() - 1);
2312
2314 blockundo.vtxundo.resize(block.vtx.size() - 1);
2315
2317 : nullptr);
2318
2319 // Add all outputs
2320 try {
2321 for (const auto &ptx : block.vtx) {
2322 AddCoins(view, *ptx, pindex->nHeight);
2323 }
2324 } catch (const std::logic_error &e) {
2325 // This error will be thrown from AddCoin if we try to connect a block
2326 // containing duplicate transactions. Such a thing should normally be
2327 // caught early nowadays (due to ContextualCheckBlock's CTOR
2328 // enforcement) however some edge cases can escape that:
2329 // - ContextualCheckBlock does not get re-run after saving the block to
2330 // disk, and older versions may have saved a weird block.
2331 // - its checks are not applied to pre-CTOR chains, which we might visit
2332 // with checkpointing off.
2333 LogPrintf("ERROR: ConnectBlock(): tried to overwrite transaction\n");
2335 "tx-duplicate");
2336 }
2337
2338 size_t txIndex = 0;
2339 // nSigChecksRet may be accurate (found in cache) or 0 (checks were
2340 // deferred into vChecks).
2341 int nSigChecksRet;
2342 for (const auto &ptx : block.vtx) {
2343 const CTransaction &tx = *ptx;
2344 const bool isCoinBase = tx.IsCoinBase();
2345 nInputs += tx.vin.size();
2346
2347 {
2350 if (!isCoinBase &&
2352 txfee)) {
2353 // Any transaction validation failure in ConnectBlock is a block
2354 // consensus failure.
2356 tx_state.GetRejectReason(),
2357 tx_state.GetDebugMessage());
2358
2359 return error("%s: Consensus::CheckTxInputs: %s, %s", __func__,
2360 tx.GetId().ToString(), state.ToString());
2361 }
2362 nFees += txfee;
2363 }
2364
2365 if (!MoneyRange(nFees)) {
2366 LogPrintf("ERROR: %s: accumulated fee in the block out of range.\n",
2367 __func__);
2369 "bad-txns-accumulated-fee-outofrange");
2370 }
2371
2372 // The following checks do not apply to the coinbase.
2373 if (isCoinBase) {
2374 continue;
2375 }
2376
2377 // Check that transaction is BIP68 final BIP68 lock checks (as
2378 // opposed to nLockTime checks) must be in ConnectBlock because they
2379 // require the UTXO set.
2380 prevheights.resize(tx.vin.size());
2381 for (size_t j = 0; j < tx.vin.size(); j++) {
2382 prevheights[j] = view.AccessCoin(tx.vin[j].prevout).GetHeight();
2383 }
2384
2385 if (!SequenceLocks(tx, nLockTimeFlags, prevheights, *pindex)) {
2386 LogPrintf("ERROR: %s: contains a non-BIP68-final transaction\n",
2387 __func__);
2389 "bad-txns-nonfinal");
2390 }
2391
2392 // Don't cache results if we're actually connecting blocks (still
2393 // consult the cache, though).
2395
2397 if (!fEnforceSigCheck) {
2398 // Historically, there has been transactions with a very high
2399 // sigcheck count, so we need to disable this check for such
2400 // transactions.
2402 }
2403
2404 std::vector<CScriptCheck> vChecks;
2406 if (fScriptChecks &&
2411 // Any transaction validation failure in ConnectBlock is a block
2412 // consensus failure
2414 tx_state.GetRejectReason(),
2415 tx_state.GetDebugMessage());
2416 return error(
2417 "ConnectBlock(): CheckInputScripts on %s failed with %s",
2418 tx.GetId().ToString(), state.ToString());
2419 }
2420
2421 control.Add(std::move(vChecks));
2422
2423 // Note: this must execute in the same iteration as CheckTxInputs (not
2424 // in a separate loop) in order to detect double spends. However,
2425 // this does not prevent double-spending by duplicated transaction
2426 // inputs in the same transaction (cf. CVE-2018-17144) -- that check is
2427 // done in CheckBlock (CheckRegularTransaction).
2428 SpendCoins(view, tx, blockundo.vtxundo.at(txIndex), pindex->nHeight);
2429 txIndex++;
2430 }
2431
2435 " - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) "
2436 "[%.2fs (%.2fms/blk)]\n",
2437 (unsigned)block.vtx.size(), MILLI * (nTime3 - nTime2),
2438 MILLI * (nTime3 - nTime2) / block.vtx.size(),
2439 nInputs <= 1 ? 0 : MILLI * (nTime3 - nTime2) / (nInputs - 1),
2441
2442 const Amount blockReward =
2443 nFees + GetBlockSubsidy(pindex->nHeight, consensusParams);
2444 if (block.vtx[0]->GetValueOut() > blockReward) {
2445 LogPrintf("ERROR: ConnectBlock(): coinbase pays too much (actual=%d vs "
2446 "limit=%d)\n",
2447 block.vtx[0]->GetValueOut(), blockReward);
2449 "bad-cb-amount");
2450 }
2451
2452 if (blockFees) {
2453 *blockFees = nFees;
2454 }
2455
2456 if (!control.Wait()) {
2458 "blk-bad-inputs", "parallel script check failed");
2459 }
2460
2463 LogPrint(
2465 " - Verify %u txins: %.2fms (%.3fms/txin) [%.2fs (%.2fms/blk)]\n",
2466 nInputs - 1, MILLI * (nTime4 - nTime2),
2467 nInputs <= 1 ? 0 : MILLI * (nTime4 - nTime2) / (nInputs - 1),
2469
2470 if (fJustCheck) {
2471 return true;
2472 }
2473
2474 if (!m_blockman.WriteUndoDataForBlock(blockundo, state, *pindex)) {
2475 return false;
2476 }
2477
2478 if (!pindex->IsValid(BlockValidity::SCRIPTS)) {
2480 m_blockman.m_dirty_blockindex.insert(pindex);
2481 }
2482
2483 // add this block to the view's block chain
2484 view.SetBestBlock(pindex->GetBlockHash());
2485
2488 LogPrint(BCLog::BENCH, " - Index writing: %.2fms [%.2fs (%.2fms/blk)]\n",
2491
2492 TRACE6(validation, block_connected, block_hash.data(), pindex->nHeight,
2493 block.vtx.size(), nInputs, nSigChecksRet,
2494 // in microseconds (µs)
2495 nTime5 - nTimeStart);
2496
2497 return true;
2498}
2499
2500CoinsCacheSizeState Chainstate::GetCoinsCacheSizeState() {
2504 : 0);
2505}
2506
2508Chainstate::GetCoinsCacheSizeState(size_t max_coins_cache_size_bytes,
2509 size_t max_mempool_size_bytes) {
2515 std::max<int64_t>(int64_t(max_mempool_size_bytes) - nMempoolUsage, 0);
2516
2518 static constexpr int64_t MAX_BLOCK_COINSDB_USAGE_BYTES =
2519 10 * 1024 * 1024; // 10MB
2520 int64_t large_threshold = std::max(
2522
2523 if (cacheSize > nTotalSpace) {
2524 LogPrintf("Cache size (%s) exceeds total space (%s)\n", cacheSize,
2525 nTotalSpace);
2527 } else if (cacheSize > large_threshold) {
2529 }
2531}
2532
2535 LOCK(cs_main);
2536 assert(this->CanFlushToDisk());
2537 std::set<int> setFilesToPrune;
2538 bool full_flush_completed = false;
2539
2540 const size_t coins_count = CoinsTip().GetCacheSize();
2541 const size_t coins_mem_usage = CoinsTip().DynamicMemoryUsage();
2542
2543 try {
2544 {
2545 bool fFlushForPrune = false;
2546 bool fDoFullFlush = false;
2547
2550 if (m_blockman.IsPruneMode() &&
2552 !fReindex) {
2553 // Make sure we don't prune any of the prune locks bestblocks.
2554 // Pruning is height-based.
2555 int last_prune{m_chain.Height()};
2556 // prune lock that actually was the limiting factor, only used
2557 // for logging
2558 std::optional<std::string> limiting_lock;
2559
2560 for (const auto &prune_lock : m_blockman.m_prune_locks) {
2561 if (prune_lock.second.height_first ==
2562 std::numeric_limits<int>::max()) {
2563 continue;
2564 }
2565 // Remove the buffer and one additional block here to get
2566 // actual height that is outside of the buffer
2567 const int lock_height{prune_lock.second.height_first -
2568 PRUNE_LOCK_BUFFER - 1};
2569 last_prune = std::max(1, std::min(last_prune, lock_height));
2570 if (last_prune == lock_height) {
2571 limiting_lock = prune_lock.first;
2572 }
2573 }
2574
2575 if (limiting_lock) {
2576 LogPrint(BCLog::PRUNE, "%s limited pruning to height %d\n",
2577 limiting_lock.value(), last_prune);
2578 }
2579
2580 if (nManualPruneHeight > 0) {
2582 "find files to prune (manual)", BCLog::BENCH);
2585 std::min(last_prune, nManualPruneHeight),
2586 m_chain.Height());
2587 } else {
2588 LOG_TIME_MILLIS_WITH_CATEGORY("find files to prune",
2589 BCLog::BENCH);
2595 }
2596 if (!setFilesToPrune.empty()) {
2597 fFlushForPrune = true;
2599 m_blockman.m_block_tree_db->WriteFlag(
2600 "prunedblockfiles", true);
2602 }
2603 }
2604 }
2606 // Avoid writing/flushing immediately after startup.
2607 if (m_last_write.count() == 0) {
2609 }
2610 if (m_last_flush.count() == 0) {
2612 }
2613 // The cache is large and we're within 10% and 10 MiB of the limit,
2614 // but we have time now (not in the middle of a block processing).
2615 bool fCacheLarge = mode == FlushStateMode::PERIODIC &&
2617 // The cache is over the limit, we have to write now.
2620 // It's been a while since we wrote the block index to disk. Do this
2621 // frequently, so we don't need to redownload after a crash.
2624 // It's been very long since we flushed the cache. Do this
2625 // infrequently, to optimize cache usage.
2628 // Combine all conditions that result in a full cache flush.
2631 // Write blocks and block index to disk.
2633 // Ensure we can write block index
2635 return AbortNode(state, "Disk space is too low!",
2636 _("Disk space is too low!"));
2637 }
2638
2639 {
2641 "write block and undo data to disk", BCLog::BENCH);
2642
2643 // First make sure all block and undo data is flushed to
2644 // disk.
2646 }
2647 // Then update all block file information (which may refer to
2648 // block and undo files).
2649 {
2650 LOG_TIME_MILLIS_WITH_CATEGORY("write block index to disk",
2651 BCLog::BENCH);
2652
2653 if (!m_blockman.WriteBlockIndexDB()) {
2654 return AbortNode(
2655 state, "Failed to write to block index database");
2656 }
2657 }
2658
2659 // Finally remove any pruned files
2660 if (fFlushForPrune) {
2661 LOG_TIME_MILLIS_WITH_CATEGORY("unlink pruned files",
2662 BCLog::BENCH);
2663
2665 }
2667 }
2668 // Flush best chain related state. This can only be done if the
2669 // blocks / block index write was also done.
2670 if (fDoFullFlush && !CoinsTip().GetBestBlock().IsNull()) {
2672 strprintf("write coins cache to disk (%d coins, %.2fkB)",
2673 coins_count, coins_mem_usage / 1000),
2674 BCLog::BENCH);
2675
2676 // Typical Coin structures on disk are around 48 bytes in size.
2677 // Pushing a new one to the database can cause it to be written
2678 // twice (once in the log, and once in the tables). This is
2679 // already an overestimation, as most will delete an existing
2680 // entry or overwrite one. Still, use a conservative safety
2681 // factor of 2.
2683 48 * 2 * 2 * CoinsTip().GetCacheSize())) {
2684 return AbortNode(state, "Disk space is too low!",
2685 _("Disk space is too low!"));
2686 }
2687
2688 // Flush the chainstate (which may refer to block index
2689 // entries).
2690 if (!CoinsTip().Flush()) {
2691 return AbortNode(state, "Failed to write to coin database");
2692 }
2694 full_flush_completed = true;
2695 }
2696
2697 TRACE5(utxocache, flush,
2698 // in microseconds (µs)
2699 GetTimeMicros() - nNow.count(), uint32_t(mode), coins_count,
2701 }
2702
2704 // Update best block in wallet (so we can detect restored wallets).
2706 }
2707 } catch (const std::runtime_error &e) {
2708 return AbortNode(state, std::string("System error while flushing: ") +
2709 e.what());
2710 }
2711 return true;
2712}
2713
2716 if (!this->FlushStateToDisk(state, FlushStateMode::ALWAYS)) {
2717 LogPrintf("%s: failed to flush state (%s)\n", __func__,
2718 state.ToString());
2719 }
2720}
2721
2725 if (!this->FlushStateToDisk(state, FlushStateMode::NONE)) {
2726 LogPrintf("%s: failed to flush state (%s)\n", __func__,
2727 state.ToString());
2728 }
2729}
2730
2732 const CBlockIndex *tip, const CChainParams &params,
2733 const std::string &func_name,
2734 const std::string &prefix)
2737 LogPrintf("%s%s: new best=%s height=%d version=0x%08x log2_work=%f tx=%ld "
2738 "date='%s' progress=%f cache=%.1fMiB(%utxo)\n",
2739 prefix, func_name, tip->GetBlockHash().ToString(), tip->nHeight,
2740 tip->nVersion, log(tip->nChainWork.getdouble()) / log(2.0),
2741 tip->GetChainTxCount(),
2742 FormatISO8601DateTime(tip->GetBlockTime()),
2743 GuessVerificationProgress(params.TxData(), tip),
2744 coins_tip.DynamicMemoryUsage() * (1.0 / (1 << 20)),
2745 coins_tip.GetCacheSize());
2746}
2747
2748void Chainstate::UpdateTip(const CBlockIndex *pindexNew) {
2750 const auto &coins_tip = CoinsTip();
2751
2752 const CChainParams &params{m_chainman.GetParams()};
2753
2754 // The remainder of the function isn't relevant if we are not acting on
2755 // the active chainstate, so return if need be.
2756 if (this != &m_chainman.ActiveChainstate()) {
2757 // Only log every so often so that we don't bury log messages at the
2758 // tip.
2759 constexpr int BACKGROUND_LOG_INTERVAL = 2000;
2760 if (pindexNew->nHeight % BACKGROUND_LOG_INTERVAL == 0) {
2762 "[background validation] ");
2763 }
2764 return;
2765 }
2766
2767 // New best block
2768 if (m_mempool) {
2770 }
2771
2772 {
2775 g_best_block_cv.notify_all();
2776 }
2777
2779}
2780
2795 if (m_mempool) {
2797 }
2798
2800
2802 assert(pindexDelete->pprev);
2803
2804 // Read block from disk.
2805 std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
2806 CBlock &block = *pblock;
2808 return error("DisconnectTip(): Failed to read block");
2809 }
2810
2811 // Apply the block atomically to the chain state.
2813 {
2815 assert(view.GetBestBlock() == pindexDelete->GetBlockHash());
2816 if (DisconnectBlock(block, pindexDelete, view) !=
2818 return error("DisconnectTip(): DisconnectBlock %s failed",
2819 pindexDelete->GetBlockHash().ToString());
2820 }
2821
2822 bool flushed = view.Flush();
2823 assert(flushed);
2824 }
2825
2826 LogPrint(BCLog::BENCH, "- Disconnect block: %.2fms\n",
2827 (GetTimeMicros() - nStart) * MILLI);
2828
2829 {
2830 // Prune locks that began at or after the tip should be moved backward
2831 // so they get a chance to reorg
2832 const int max_height_first{pindexDelete->nHeight - 1};
2833 for (auto &prune_lock : m_blockman.m_prune_locks) {
2834 if (prune_lock.second.height_first <= max_height_first) {
2835 continue;
2836 }
2837
2838 prune_lock.second.height_first = max_height_first;
2839 LogPrint(BCLog::PRUNE, "%s prune lock moved back to %d\n",
2841 }
2842 }
2843
2844 // Write the chain state to disk, if necessary.
2846 return false;
2847 }
2848
2849 if (m_mempool) {
2850 // If this block is deactivating a fork, we move all mempool
2851 // transactions in front of disconnectpool for reprocessing in a future
2852 // updateMempoolForReorg call
2853 if (pindexDelete->pprev != nullptr &&
2857 "Disconnecting mempool due to rewind of upgrade block\n");
2858 if (disconnectpool) {
2859 disconnectpool->importMempool(*m_mempool);
2860 }
2861 m_mempool->clear();
2862 }
2863
2864 if (disconnectpool) {
2865 disconnectpool->addForBlock(block.vtx, *m_mempool);
2866 }
2867 }
2868
2869 m_chain.SetTip(*pindexDelete->pprev);
2870
2871 UpdateTip(pindexDelete->pprev);
2872 // Let wallets know transactions went from 1-confirmed to
2873 // 0-confirmed or conflicted:
2875 return true;
2876}
2877
2883
2891 const std::shared_ptr<const CBlock> &pblock,
2893 const avalanche::Processor *const avalanche) {
2895 if (m_mempool) {
2897 }
2898
2900
2901 assert(pindexNew->pprev == m_chain.Tip());
2902 // Read block from disk.
2904 std::shared_ptr<const CBlock> pthisBlock;
2905 if (!pblock) {
2906 std::shared_ptr<CBlock> pblockNew = std::make_shared<CBlock>();
2908 return AbortNode(state, "Failed to read block");
2909 }
2911 } else {
2913 }
2914
2916
2917 // Apply the block atomically to the chain state.
2921 LogPrint(BCLog::BENCH, " - Load block from disk: %.2fms [%.2fs]\n",
2923 {
2928 &blockFees);
2930 if (!rv) {
2931 if (state.IsInvalid()) {
2933 }
2934
2935 return error("%s: ConnectBlock %s failed, %s", __func__,
2936 pindexNew->GetBlockHash().ToString(),
2937 state.ToString());
2938 }
2939
2951 const BlockHash blockhash = pindexNew->GetBlockHash();
2952 if (!IsInitialBlockDownload() &&
2955
2956 const Amount blockReward =
2957 blockFees +
2959
2960 std::vector<std::unique_ptr<ParkingPolicy>> parkingPolicies;
2961 parkingPolicies.emplace_back(std::make_unique<MinerFundPolicy>(
2963
2964 if (avalanche) {
2965 // Only enable the RTT policy if the node already finalized a
2966 // block. This is because it's very possible that new blocks
2967 // will be parked after a node restart (but after IBD) if the
2968 // node is behind by a few blocks. We want to make sure that the
2969 // node will be able to switch back to the right tip in this
2970 // case.
2971 if (avalanche->hasFinalizedTip()) {
2972 // Special case for testnet, don't reject blocks mined with
2973 // the min difficulty
2974 if (!consensusParams.fPowAllowMinDifficultyBlocks ||
2975 (blockConnecting.GetBlockTime() <=
2976 pindexNew->pprev->GetBlockTime() +
2977 2 * consensusParams.nPowTargetSpacing)) {
2978 parkingPolicies.emplace_back(
2979 std::make_unique<RTTPolicy>(consensusParams,
2980 *pindexNew));
2981 }
2982 }
2983
2984 parkingPolicies.emplace_back(
2985 std::make_unique<StakingRewardsPolicy>(
2988
2989 if (m_mempool) {
2990 parkingPolicies.emplace_back(
2991 std::make_unique<PreConsensusPolicy>(
2993 }
2994 }
2995
2996 // If any block policy is violated, bail on the first one found
2997 if (std::find_if_not(parkingPolicies.begin(), parkingPolicies.end(),
2998 [&](const auto &policy) {
2999 bool ret = (*policy)(blockPolicyState);
3000 if (!ret) {
3001 LogPrintf(
3002 "Park block because it "
3003 "violated a block policy: %s\n",
3004 blockPolicyState.ToString());
3005 }
3006 return ret;
3007 }) != parkingPolicies.end()) {
3008 pindexNew->nStatus = pindexNew->nStatus.withParked();
3010 return false;
3011 }
3012 }
3013
3016 assert(nBlocksTotal > 0);
3018 " - Connect total: %.2fms [%.2fs (%.2fms/blk)]\n",
3021 bool flushed = view.Flush();
3022 assert(flushed);
3023 }
3024
3027 LogPrint(BCLog::BENCH, " - Flush: %.2fms [%.2fs (%.2fms/blk)]\n",
3030
3031 // Write the chain state to disk, if necessary.
3032 if (!FlushStateToDisk(state, FlushStateMode::IF_NEEDED)) {
3033 return false;
3034 }
3035
3039 " - Writing chainstate: %.2fms [%.2fs (%.2fms/blk)]\n",
3042
3043 // Remove conflicting transactions from the mempool;
3044 if (m_mempool) {
3045 disconnectpool.removeForBlock(blockConnecting.vtx, *m_mempool);
3046
3047 // If this block is activating a fork, we move all mempool transactions
3048 // in front of disconnectpool for reprocessing in a future
3049 // updateMempoolForReorg call
3050 if (pindexNew->pprev != nullptr &&
3051 GetNextBlockScriptFlags(pindexNew, m_chainman) !=
3052 GetNextBlockScriptFlags(pindexNew->pprev, m_chainman)) {
3053 LogPrint(
3055 "Disconnecting mempool due to acceptance of upgrade block\n");
3056 disconnectpool.importMempool(*m_mempool);
3057 }
3058 }
3059
3060 // Update m_chain & related variables.
3061 m_chain.SetTip(*pindexNew);
3063
3068 " - Connect postprocess: %.2fms [%.2fs (%.2fms/blk)]\n",
3071 LogPrint(BCLog::BENCH, "- Connect block: %.2fms [%.2fs (%.2fms/blk)]\n",
3074
3075 // If we are the background validation chainstate, check to see if we are
3076 // done validating the snapshot (i.e. our tip has reached the snapshot's
3077 // base block).
3078 if (this != &m_chainman.ActiveChainstate()) {
3079 // This call may set `m_disabled`, which is referenced immediately
3080 // afterwards in ActivateBestChain, so that we stop connecting blocks
3081 // past the snapshot base.
3082 m_chainman.MaybeCompleteSnapshotValidation();
3083 }
3084
3086 return true;
3087}
3088
3094 std::vector<const CBlockIndex *> &blocksToReconcile, bool fAutoUnpark) {
3096 do {
3097 CBlockIndex *pindexNew = nullptr;
3098
3099 // Find the best candidate header.
3100 {
3101 std::set<CBlockIndex *, CBlockIndexWorkComparator>::reverse_iterator
3102 it = setBlockIndexCandidates.rbegin();
3103 if (it == setBlockIndexCandidates.rend()) {
3104 return nullptr;
3105 }
3106 pindexNew = *it;
3107 }
3108
3109 // If this block will cause an avalanche finalized block to be reorged,
3110 // then we park it.
3111 {
3115 LogPrintf("Park block %s because it forks prior to the "
3116 "avalanche finalized chaintip.\n",
3117 pindexNew->GetBlockHash().ToString());
3118 pindexNew->nStatus = pindexNew->nStatus.withParked();
3120 }
3121 }
3122
3124
3125 // Check whether all blocks on the path between the currently active
3126 // chain and the candidate are valid. Just going until the active chain
3127 // is an optimization, as we know all blocks in it are valid already.
3129 bool hasValidAncestor = true;
3131 assert(pindexTest->HaveTxsDownloaded() || pindexTest->nHeight == 0);
3132
3133 // If this is a parked chain, but it has enough PoW, clear the park
3134 // state.
3135 bool fParkedChain = pindexTest->nStatus.isOnParkedChain();
3136 if (fAutoUnpark && fParkedChain) {
3137 const CBlockIndex *pindexTip = m_chain.Tip();
3138
3139 // During initialization, pindexTip and/or pindexFork may be
3140 // null. In this case, we just ignore the fact that the chain is
3141 // parked.
3142 if (!pindexTip || !pindexFork) {
3144 continue;
3145 }
3146
3147 // A parked chain can be unparked if it has twice as much PoW
3148 // accumulated as the main chain has since the fork block.
3150 arith_uint256 requiredWork = pindexTip->nChainWork;
3151 switch (pindexTip->nHeight - pindexFork->nHeight) {
3152 // Limit the penality for depth 1, 2 and 3 to half a block
3153 // worth of work to ensure we don't fork accidentally.
3154 case 3:
3155 case 2:
3157 // FALLTHROUGH
3158 case 1: {
3159 const arith_uint256 deltaWork =
3160 pindexExtraPow->nChainWork - pindexFork->nChainWork;
3161 requiredWork += (deltaWork >> 1);
3162 break;
3163 }
3164 default:
3165 requiredWork +=
3166 pindexExtraPow->nChainWork - pindexFork->nChainWork;
3167 break;
3168 }
3169
3170 if (pindexNew->nChainWork > requiredWork) {
3171 // We have enough, clear the parked state.
3172 LogPrintf("Unpark chain up to block %s as it has "
3173 "accumulated enough PoW.\n",
3174 pindexNew->GetBlockHash().ToString());
3175 fParkedChain = false;
3177 }
3178 }
3179
3180 // Pruned nodes may have entries in setBlockIndexCandidates for
3181 // which block files have been deleted. Remove those as candidates
3182 // for the most work chain if we come across them; we can't switch
3183 // to a chain unless we have all the non-active-chain parent blocks.
3184 bool fInvalidChain = pindexTest->nStatus.isInvalid();
3185 bool fMissingData = !pindexTest->nStatus.hasData();
3187 // The current block is acceptable, move to the parent, up to
3188 // the fork point.
3189 pindexTest = pindexTest->pprev;
3190 continue;
3191 }
3192
3193 // Candidate chain is not usable (either invalid or parked or
3194 // missing data)
3195 hasValidAncestor = false;
3197
3198 if (fInvalidChain && (m_chainman.m_best_invalid == nullptr ||
3199 pindexNew->nChainWork >
3200 m_chainman.m_best_invalid->nChainWork)) {
3201 m_chainman.m_best_invalid = pindexNew;
3202 }
3203
3204 if (fParkedChain && (m_chainman.m_best_parked == nullptr ||
3205 pindexNew->nChainWork >
3206 m_chainman.m_best_parked->nChainWork)) {
3207 m_chainman.m_best_parked = pindexNew;
3208 }
3209
3210 LogPrintf("Considered switching to better tip %s but that chain "
3211 "contains a%s%s%s block.\n",
3212 pindexNew->GetBlockHash().ToString(),
3213 fInvalidChain ? "n invalid" : "",
3214 fParkedChain ? " parked" : "",
3215 fMissingData ? " missing-data" : "");
3216
3218 // Remove the entire chain from the set.
3219 while (pindexTest != pindexFailed) {
3220 if (fInvalidChain || fParkedChain) {
3221 pindexFailed->nStatus =
3222 pindexFailed->nStatus.withFailedParent(fInvalidChain)
3223 .withParkedParent(fParkedChain);
3224 } else if (fMissingData) {
3225 // If we're missing data, then add back to
3226 // m_blocks_unlinked, so that if the block arrives in the
3227 // future we can try adding to setBlockIndexCandidates
3228 // again.
3230 std::make_pair(pindexFailed->pprev, pindexFailed));
3231 }
3233 pindexFailed = pindexFailed->pprev;
3234 }
3235
3236 if (fInvalidChain || fParkedChain) {
3237 // We discovered a new chain tip that is either parked or
3238 // invalid, we may want to warn.
3240 }
3241 }
3242
3243 blocksToReconcile.push_back(pindexNew);
3244
3245 // We found a candidate that has valid ancestors. This is our guy.
3246 if (hasValidAncestor) {
3247 return pindexNew;
3248 }
3249 } while (true);
3250}
3251
3257 // Note that we can't delete the current block itself, as we may need to
3258 // return to it later in case a reorganization to a better block fails.
3259 auto it = setBlockIndexCandidates.begin();
3260 while (it != setBlockIndexCandidates.end() &&
3261 setBlockIndexCandidates.value_comp()(*it, m_chain.Tip())) {
3262 setBlockIndexCandidates.erase(it++);
3263 }
3264
3265 // Either the current tip or a successor of it we're working towards is left
3266 // in setBlockIndexCandidates.
3268}
3269
3279 const std::shared_ptr<const CBlock> &pblock, bool &fInvalidFound,
3280 const avalanche::Processor *const avalanche) {
3282 if (m_mempool) {
3284 }
3285
3288
3289 // Disconnect active blocks which are no longer in the best chain.
3290 bool fBlocksDisconnected = false;
3292 while (m_chain.Tip() && m_chain.Tip() != pindexFork) {
3293 if (!fBlocksDisconnected) {
3294 // Import and clear mempool; we must do this to preserve
3295 // topological ordering in the mempool index. This is ok since
3296 // inserts into the mempool are very fast now in our new
3297 // implementation.
3299 }
3300
3301 if (!DisconnectTip(state, &disconnectpool)) {
3302 // This is likely a fatal error, but keep the mempool consistent,
3303 // just in case. Only remove from the mempool in this case.
3304 if (m_mempool) {
3305 disconnectpool.updateMempoolForReorg(*this, false, *m_mempool);
3306 }
3307
3308 // If we're unable to disconnect a block during normal operation,
3309 // then that is a failure of our local system -- we should abort
3310 // rather than stay on a less work chain.
3311 AbortNode(state,
3312 "Failed to disconnect block; see debug.log for details");
3313 return false;
3314 }
3315
3316 fBlocksDisconnected = true;
3317 }
3318
3319 // Build list of new blocks to connect.
3320 std::vector<CBlockIndex *> vpindexToConnect;
3321 bool fContinue = true;
3322 int nHeight = pindexFork ? pindexFork->nHeight : -1;
3323 while (fContinue && nHeight != pindexMostWork->nHeight) {
3324 // Don't iterate the entire list of potential improvements toward the
3325 // best tip, as we likely only need a few blocks along the way.
3326 int nTargetHeight = std::min(nHeight + 32, pindexMostWork->nHeight);
3327 vpindexToConnect.clear();
3330 while (pindexIter && pindexIter->nHeight != nHeight) {
3331 vpindexToConnect.push_back(pindexIter);
3333 }
3334
3336
3337 // Connect new blocks.
3342 ? pblock
3343 : std::shared_ptr<const CBlock>(),
3345 if (state.IsInvalid()) {
3346 // The block violates a consensus rule.
3347 if (state.GetResult() !=
3350 }
3351 state = BlockValidationState();
3352 fInvalidFound = true;
3353 fContinue = false;
3354 break;
3355 }
3356
3357 if (blockPolicyState.IsInvalid()) {
3358 // The block violates a policy rule.
3359 fContinue = false;
3360 break;
3361 }
3362
3363 // A system error occurred (disk space, database error, ...).
3364 // Make the mempool consistent with the current tip, just in
3365 // case any observers try to use it before shutdown.
3366 if (m_mempool) {
3367 disconnectpool.updateMempoolForReorg(*this, false,
3368 *m_mempool);
3369 }
3370 return false;
3371 } else {
3373 if (!pindexOldTip ||
3374 m_chain.Tip()->nChainWork > pindexOldTip->nChainWork) {
3375 // We're in a better position than we were. Return
3376 // temporarily to release the lock.
3377 fContinue = false;
3378 break;
3379 }
3380 }
3381 }
3382 }
3383
3384 if (m_mempool) {
3385 if (fBlocksDisconnected || !disconnectpool.isEmpty()) {
3386 // If any blocks were disconnected, we need to update the mempool
3387 // even if disconnectpool is empty. The disconnectpool may also be
3388 // non-empty if the mempool was imported due to new validation rules
3389 // being in effect.
3391 "Updating mempool due to reorganization or "
3392 "rules upgrade/downgrade\n");
3393 disconnectpool.updateMempoolForReorg(*this, true, *m_mempool);
3394 }
3395
3396 m_mempool->check(this->CoinsTip(), this->m_chain.Height() + 1);
3397 }
3398
3399 // Callbacks/notifications for a new best chain.
3400 if (fInvalidFound) {
3402 } else {
3404 }
3405
3406 return true;
3407}
3408
3410 if (!init) {
3412 }
3413 if (::fReindex) {
3415 }
3417}
3418
3420 bool fNotify = false;
3421 bool fInitialBlockDownload = false;
3422 static CBlockIndex *pindexHeaderOld = nullptr;
3423 CBlockIndex *pindexHeader = nullptr;
3424 {
3425 LOCK(cs_main);
3426 pindexHeader = chainstate.m_chainman.m_best_header;
3427
3429 fNotify = true;
3430 fInitialBlockDownload = chainstate.IsInitialBlockDownload();
3432 }
3433 }
3434
3435 // Send block tip changed notifications without cs_main
3436 if (fNotify) {
3437 chainstate.m_chainman.GetNotifications().headerTip(
3439 pindexHeader->nHeight, pindexHeader->nTime, false);
3440 }
3441 return fNotify;
3442}
3443
3446
3447 if (GetMainSignals().CallbacksPending() > 10) {
3449 }
3450}
3451
3453 std::shared_ptr<const CBlock> pblock,
3455 bool skip_checkblockindex) {
3457
3458 // Note that while we're often called here from ProcessNewBlock, this is
3459 // far from a guarantee. Things in the P2P/RPC will often end up calling
3460 // us in the middle of ProcessNewBlock - do not assume pblock is set
3461 // sanely for performance or correctness!
3463
3464 // ABC maintains a fair degree of expensive-to-calculate internal state
3465 // because this function periodically releases cs_main so that it does not
3466 // lock up other threads for too long during large connects - and to allow
3467 // for e.g. the callback queue to drain we use m_chainstate_mutex to enforce
3468 // mutual exclusion so that only one caller may execute this function at a
3469 // time
3471
3472 // Belt-and-suspenders check that we aren't attempting to advance the
3473 // background chainstate past the snapshot base block.
3474 if (WITH_LOCK(::cs_main, return m_disabled)) {
3475 LogPrintf("m_disabled is set - this chainstate should not be in "
3476 "operation. Please report this as a bug. %s\n",
3478 return false;
3479 }
3480
3481 CBlockIndex *pindexMostWork = nullptr;
3482 CBlockIndex *pindexNewTip = nullptr;
3483 int nStopAtHeight = gArgs.GetIntArg("-stopatheight", DEFAULT_STOPATHEIGHT);
3484 do {
3485 // Block until the validation queue drains. This should largely
3486 // never happen in normal operation, however may happen during
3487 // reindex, causing memory blowup if we run too far ahead.
3488 // Note that if a validationinterface callback ends up calling
3489 // ActivateBestChain this may lead to a deadlock! We should
3490 // probably have a DEBUG_LOCKORDER test for this in the future.
3492
3493 std::vector<const CBlockIndex *> blocksToReconcile;
3494 bool blocks_connected = false;
3495
3496 const bool fAutoUnpark =
3497 gArgs.GetBoolArg("-automaticunparking", !avalanche);
3498
3499 {
3500 LOCK(cs_main);
3501 // Lock transaction pool for at least as long as it takes for
3502 // updateMempoolForReorg to be executed if needed
3503 LOCK(MempoolMutex());
3505 do {
3506 // We absolutely may not unlock cs_main until we've made forward
3507 // progress (with the exception of shutdown due to hardware
3508 // issues, low disk space, etc).
3509
3510 if (pindexMostWork == nullptr) {
3513 }
3514
3515 // Whether we have anything to do at all.
3516 if (pindexMostWork == nullptr ||
3517 pindexMostWork == m_chain.Tip()) {
3518 break;
3519 }
3520
3521 bool fInvalidFound = false;
3522 std::shared_ptr<const CBlock> nullBlockPtr;
3524 state, pindexMostWork,
3525 pblock && pblock->GetHash() ==
3526 pindexMostWork->GetBlockHash()
3527 ? pblock
3528 : nullBlockPtr,
3530 // A system error occurred
3531 return false;
3532 }
3533 blocks_connected = true;
3534
3535 if (fInvalidFound ||
3536 (pindexMostWork && pindexMostWork->nStatus.isParked())) {
3537 // Wipe cache, we may need another branch now.
3538 pindexMostWork = nullptr;
3539 }
3540
3542
3543 // This will have been toggled in
3544 // ActivateBestChainStep -> ConnectTip ->
3545 // MaybeCompleteSnapshotValidation, if at all, so we should
3546 // catch it here.
3547 //
3548 // Break this do-while to ensure we don't advance past the base
3549 // snapshot.
3550 if (m_disabled) {
3551 break;
3552 }
3553 } while (!m_chain.Tip() ||
3555 m_chain.Tip(), starting_tip)));
3556
3557 // Check the index once we're done with the above loop, since
3558 // we're going to release cs_main soon. If the index is in a bad
3559 // state now, then it's better to know immediately rather than
3560 // randomly have it cause a problem in a race.
3561 if (!skip_checkblockindex) {
3563 }
3564
3565 if (blocks_connected) {
3568
3569 // Notify external listeners about the new tip.
3570 // Enqueue while holding cs_main to ensure that UpdatedBlockTip
3571 // is called in the order in which blocks are connected
3572 if (pindexFork != pindexNewTip) {
3573 // Notify ValidationInterface subscribers
3576
3577 // Always notify the UI if a new block tip was connected
3580 *pindexNewTip);
3581 }
3582 }
3583 }
3584 // When we reach this point, we switched to a new tip (stored in
3585 // pindexNewTip).
3586 if (avalanche) {
3587 const CBlockIndex *pfinalized =
3590 for (const CBlockIndex *pindex : blocksToReconcile) {
3591 avalanche->addToReconcile(pindex);
3592
3593 // Compute staking rewards for all blocks with more chainwork to
3594 // just after the finalized block. We could stop at the fork
3595 // point, but this is more robust.
3596 if (blocks_connected) {
3597 const CBlockIndex *pindexTest = pindex;
3598 while (pindexTest && pindexTest != pfinalized) {
3599 if (pindexTest->nHeight < pindex->nHeight - 3) {
3600 // Only compute up to some max depth
3601 break;
3602 }
3603 avalanche->computeStakingReward(pindexTest);
3604 pindexTest = pindexTest->pprev;
3605 }
3606 }
3607 }
3608 }
3609
3610 if (!blocks_connected) {
3611 return true;
3612 }
3613
3614 if (nStopAtHeight && pindexNewTip &&
3615 pindexNewTip->nHeight >= nStopAtHeight) {
3616 StartShutdown();
3617 }
3618
3619 if (WITH_LOCK(::cs_main, return m_disabled)) {
3620 // Background chainstate has reached the snapshot base block, so
3621 // exit.
3622 break;
3623 }
3624
3625 // We check shutdown only after giving ActivateBestChainStep a chance to
3626 // run once so that we never shutdown before connecting the genesis
3627 // block during LoadChainTip(). Previously this caused an assert()
3628 // failure during shutdown in such cases as the UTXO DB flushing checks
3629 // that the best block hash is non-null.
3630 if (ShutdownRequested()) {
3631 break;
3632 }
3633 } while (pindexNewTip != pindexMostWork);
3634
3635 // Write changes periodically to disk, after relay.
3637 return false;
3638 }
3639
3640 return true;
3641}
3642
3647 {
3648 LOCK(cs_main);
3649 if (pindex->nChainWork < m_chain.Tip()->nChainWork) {
3650 // Nothing to do, this block is not at the tip.
3651 return true;
3652 }
3653
3655 // The chain has been extended since the last call, reset the
3656 // counter.
3658 }
3659
3661 setBlockIndexCandidates.erase(pindex);
3663 if (nBlockReverseSequenceId > std::numeric_limits<int32_t>::min()) {
3664 // We can't keep reducing the counter if somebody really wants to
3665 // call preciousblock 2**31-1 times on the same set of tips...
3667 }
3668
3669 // In case this was parked, unpark it.
3670 UnparkBlock(pindex);
3671
3672 // Make sure it is added to the candidate list if appropriate.
3673 if (pindex->IsValid(BlockValidity::TRANSACTIONS) &&
3674 pindex->HaveTxsDownloaded()) {
3675 setBlockIndexCandidates.insert(pindex);
3677 }
3678 }
3679
3680 return ActivateBestChain(state, /*pblock=*/nullptr, avalanche);
3681}
3682
3683namespace {
3684// Leverage RAII to run a functor at scope end
3685template <typename Func> struct Defer {
3686 Func func;
3687 Defer(Func &&f) : func(std::move(f)) {}
3688 ~Defer() { func(); }
3689};
3690} // namespace
3691
3693 bool invalidate) {
3694 // Genesis block can't be invalidated or parked
3695 assert(pindex);
3696 if (pindex->nHeight == 0) {
3697 return false;
3698 }
3699
3701 bool pindex_was_in_chain = false;
3702 int disconnected = 0;
3703
3704 // We do not allow ActivateBestChain() to run while UnwindBlock() is
3705 // running, as that could cause the tip to change while we disconnect
3706 // blocks. (Note for backport of Core PR16849: we acquire
3707 // LOCK(m_chainstate_mutex) in the Park, Invalidate and FinalizeBlock
3708 // functions due to differences in our code)
3710
3711 // We'll be acquiring and releasing cs_main below, to allow the validation
3712 // callbacks to run. However, we should keep the block index in a
3713 // consistent state as we disconnect blocks -- in particular we need to
3714 // add equal-work blocks to setBlockIndexCandidates as we disconnect.
3715 // To avoid walking the block index repeatedly in search of candidates,
3716 // build a map once so that we can look up candidate blocks by chain
3717 // work as we go.
3718 std::multimap<const arith_uint256, CBlockIndex *> candidate_blocks_by_work;
3719
3720 {
3721 LOCK(cs_main);
3722 for (auto &entry : m_blockman.m_block_index) {
3723 CBlockIndex *candidate = &entry.second;
3724 // We don't need to put anything in our active chain into the
3725 // multimap, because those candidates will be found and considered
3726 // as we disconnect.
3727 // Instead, consider only non-active-chain blocks that have at
3728 // least as much work as where we expect the new tip to end up.
3729 if (!m_chain.Contains(candidate) &&
3732 candidate->HaveTxsDownloaded()) {
3734 std::make_pair(candidate->nChainWork, candidate));
3735 }
3736 }
3737 }
3738
3739 {
3740 LOCK(cs_main);
3741 // Lock for as long as disconnectpool is in scope to make sure
3742 // UpdateMempoolForReorg is called after DisconnectTip without unlocking
3743 // in between
3744 LOCK(MempoolMutex());
3745
3746 constexpr int maxDisconnectPoolBlocks = 10;
3747 bool ret = false;
3749 // After 10 blocks this becomes nullptr, so that DisconnectTip will
3750 // stop giving us unwound block txs if we are doing a deep unwind.
3752
3753 // Disable thread safety analysis because we can't require m_mempool->cs
3754 // as m_mempool can be null. We keep the runtime analysis though.
3757 if (m_mempool && !disconnectpool.isEmpty()) {
3759 // DisconnectTip will add transactions to disconnectpool.
3760 // When all unwinding is done and we are on a new tip, we must
3761 // add all transactions back to the mempool against the new tip.
3762 disconnectpool.updateMempoolForReorg(*this,
3763 /* fAddToMempool = */ ret,
3764 *m_mempool);
3765 }
3766 });
3767
3768 // Disconnect (descendants of) pindex, and mark them invalid.
3769 while (true) {
3770 if (ShutdownRequested()) {
3771 break;
3772 }
3773
3774 // Make sure the queue of validation callbacks doesn't grow
3775 // unboundedly.
3776 // FIXME this commented code is a regression and could cause OOM if
3777 // a very old block is invalidated via the invalidateblock RPC.
3778 // This can be uncommented if the main signals are moved away from
3779 // cs_main or this code is refactored so that cs_main can be
3780 // released at this point.
3781 //
3782 // LimitValidationInterfaceQueue();
3783
3784 if (!m_chain.Contains(pindex)) {
3785 break;
3786 }
3787
3788 if (m_mempool && disconnected == 0) {
3789 // On first iteration, we grab all the mempool txs to preserve
3790 // topological ordering. This has the side-effect of temporarily
3791 // clearing the mempool, but we will re-add later in
3792 // updateMempoolForReorg() (above). This technique guarantees
3793 // mempool consistency as well as ensures that our topological
3794 // entry_id index is always correct.
3796 }
3797
3798 pindex_was_in_chain = true;
3800
3801 // ActivateBestChain considers blocks already in m_chain
3802 // unconditionally valid already, so force disconnect away from it.
3803
3805 ++disconnected;
3806
3807 if (optDisconnectPool && disconnected > maxDisconnectPoolBlocks) {
3808 // Stop using the disconnect pool after 10 blocks. After 10
3809 // blocks we no longer add block tx's to the disconnectpool.
3810 // However, when this scope ends we will reconcile what's
3811 // in the pool with the new tip (in the deferred d'tor above).
3812 optDisconnectPool = nullptr;
3813 }
3814
3815 if (!ret) {
3816 return false;
3817 }
3818
3819 assert(invalid_walk_tip->pprev == m_chain.Tip());
3820
3821 // We immediately mark the disconnected blocks as invalid.
3822 // This prevents a case where pruned nodes may fail to
3823 // invalidateblock and be left unable to start as they have no tip
3824 // candidates (as there are no blocks that meet the "have data and
3825 // are not invalid per nStatus" criteria for inclusion in
3826 // setBlockIndexCandidates).
3827
3828 invalid_walk_tip->nStatus =
3829 invalidate ? invalid_walk_tip->nStatus.withFailed()
3830 : invalid_walk_tip->nStatus.withParked();
3831
3834
3836 (invalidate ? to_mark_failed_or_parked->nStatus.hasFailed()
3837 : to_mark_failed_or_parked->nStatus.isParked())) {
3838 // We only want to mark the last disconnected block as
3839 // Failed (or Parked); its children need to be FailedParent (or
3840 // ParkedParent) instead.
3841 to_mark_failed_or_parked->nStatus =
3842 (invalidate
3843 ? to_mark_failed_or_parked->nStatus.withFailed(false)
3844 .withFailedParent()
3845 : to_mark_failed_or_parked->nStatus.withParked(false)
3846 .withParkedParent());
3847
3849 }
3850
3851 // Add any equal or more work headers to setBlockIndexCandidates
3852 auto candidate_it = candidate_blocks_by_work.lower_bound(
3853 invalid_walk_tip->pprev->nChainWork);
3854 while (candidate_it != candidate_blocks_by_work.end()) {
3856 invalid_walk_tip->pprev)) {
3857 setBlockIndexCandidates.insert(candidate_it->second);
3859 } else {
3860 ++candidate_it;
3861 }
3862 }
3863
3864 // Track the last disconnected block, so we can correct its
3865 // FailedParent (or ParkedParent) status in future iterations, or,
3866 // if it's the last one, call InvalidChainFound on it.
3868 }
3869 }
3870
3872
3873 {
3874 LOCK(cs_main);
3876 // If the to-be-marked invalid block is in the active chain,
3877 // something is interfering and we can't proceed.
3878 return false;
3879 }
3880
3881 // Mark pindex (or the last disconnected block) as invalid (or parked),
3882 // even when it never was in the main chain.
3883 to_mark_failed_or_parked->nStatus =
3884 invalidate ? to_mark_failed_or_parked->nStatus.withFailed()
3885 : to_mark_failed_or_parked->nStatus.withParked();
3887 if (invalidate) {
3889 }
3890
3891 // If any new blocks somehow arrived while we were disconnecting
3892 // (above), then the pre-calculation of what should go into
3893 // setBlockIndexCandidates may have missed entries. This would
3894 // technically be an inconsistency in the block index, but if we clean
3895 // it up here, this should be an essentially unobservable error.
3896 // Loop back over all block index entries and add any missing entries
3897 // to setBlockIndexCandidates.
3898 for (auto &[_, block_index] : m_blockman.m_block_index) {
3900 block_index.HaveTxsDownloaded() &&
3901 !setBlockIndexCandidates.value_comp()(&block_index,
3902 m_chain.Tip())) {
3904 }
3905 }
3906
3907 if (invalidate) {
3909 }
3910 }
3911
3912 // Only notify about a new block tip if the active chain was modified.
3913 if (pindex_was_in_chain) {
3916 *to_mark_failed_or_parked->pprev);
3917 }
3918 return true;
3919}
3920
3922 CBlockIndex *pindex) {
3925 // See 'Note for backport of Core PR16849' in Chainstate::UnwindBlock
3927
3928 return UnwindBlock(state, pindex, true);
3929}
3930
3934 // See 'Note for backport of Core PR16849' in Chainstate::UnwindBlock
3936
3937 return UnwindBlock(state, pindex, false);
3938}
3939
3940template <typename F>
3942 CBlockIndex *pindex, F f) {
3943 BlockStatus newStatus = f(pindex->nStatus);
3944 if (pindex->nStatus != newStatus &&
3945 (!pindexBase ||
3946 pindex->GetAncestor(pindexBase->nHeight) == pindexBase)) {
3947 pindex->nStatus = newStatus;
3948 m_blockman.m_dirty_blockindex.insert(pindex);
3949 if (newStatus.isValid()) {
3950 m_chainman.m_failed_blocks.erase(pindex);
3951 }
3952
3953 if (pindex->IsValid(BlockValidity::TRANSACTIONS) &&
3954 pindex->HaveTxsDownloaded() &&
3955 setBlockIndexCandidates.value_comp()(m_chain.Tip(), pindex)) {
3956 setBlockIndexCandidates.insert(pindex);
3957 }
3958 return true;
3959 }
3960 return false;
3961}
3962
3963template <typename F, typename C, typename AC>
3967
3968 // Update the current block and ancestors; while we're doing this, identify
3969 // which was the deepest ancestor we changed.
3971 for (auto pindexAncestor = pindex; pindexAncestor != nullptr;
3973 if (UpdateFlagsForBlock(nullptr, pindexAncestor, f)) {
3975 }
3976 }
3977
3978 if (pindexReset &&
3979 pindexReset->GetAncestor(pindexDeepestChanged->nHeight) ==
3981 // reset pindexReset if it had a modified ancestor.
3982 pindexReset = nullptr;
3983 }
3984
3985 // Update all blocks under modified blocks.
3986 for (auto &[_, block_index] : m_blockman.m_block_index) {
3990 }
3991}
3992
3995
3997 pindex, m_chainman.m_best_invalid,
3998 [](const BlockStatus status) {
3999 return status.withClearedFailureFlags();
4000 },
4001 [](const BlockStatus status) {
4002 return status.withClearedFailureFlags();
4003 },
4004 [](const BlockStatus status) {
4005 return status.withFailedParent(false);
4006 });
4007}
4008
4011
4013 pindex, m_chainman.m_best_parked,
4014 [](const BlockStatus status) {
4015 return status.withClearedParkedFlags();
4016 },
4017 [fClearChildren](const BlockStatus status) {
4018 return fClearChildren ? status.withClearedParkedFlags()
4019 : status.withParkedParent(false);
4020 },
4021 [](const BlockStatus status) {
4022 return status.withParkedParent(false);
4023 });
4024}
4025
4027 return UnparkBlockImpl(pindex, true);
4028}
4029
4031 return UnparkBlockImpl(pindex, false);
4032}
4033
4036 if (!pindex) {
4037 return false;
4038 }
4039
4040 if (!m_chain.Contains(pindex)) {
4042 "The block to mark finalized by avalanche is not on the "
4043 "active chain: %s\n",
4044 pindex->GetBlockHash().ToString());
4045 return false;
4046 }
4047
4048 avalanche.cleanupStakingRewards(pindex->nHeight);
4049
4050 if (IsBlockAvalancheFinalized(pindex)) {
4051 return true;
4052 }
4053
4054 {
4057 }
4058
4059 WITH_LOCK(cs_main, GetMainSignals().BlockFinalized(pindex));
4060
4061 return true;
4062}
4063
4068
4075
4082 const FlatFilePos &pos) {
4083 pindexNew->nTx = block.vtx.size();
4085 pindexNew->nFile = pos.nFile;
4086 pindexNew->nDataPos = pos.nPos;
4087 pindexNew->nUndoPos = 0;
4088 pindexNew->nStatus = pindexNew->nStatus.withData();
4089 pindexNew->RaiseValidity(BlockValidity::TRANSACTIONS);
4091
4092 if (pindexNew->UpdateChainStats()) {
4093 // If pindexNew is the genesis block or all parents are
4094 // BLOCK_VALID_TRANSACTIONS.
4095 std::deque<CBlockIndex *> queue;
4096 queue.push_back(pindexNew);
4097
4098 // Recursively process any descendant blocks that now may be eligible to
4099 // be connected.
4100 while (!queue.empty()) {
4101 CBlockIndex *pindex = queue.front();
4102 queue.pop_front();
4103 pindex->UpdateChainStats();
4104 if (pindex->nSequenceId == 0) {
4105 // We assign a sequence is when transaction are received to
4106 // prevent a miner from being able to broadcast a block but not
4107 // its content. However, a sequence id may have been set
4108 // manually, for instance via PreciousBlock, in which case, we
4109 // don't need to assign one.
4110 pindex->nSequenceId = nBlockSequenceId++;
4111 }
4112
4113 if (m_chain.Tip() == nullptr ||
4114 !setBlockIndexCandidates.value_comp()(pindex, m_chain.Tip())) {
4115 setBlockIndexCandidates.insert(pindex);
4116 }
4117
4118 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
4119 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
4120 range = m_blockman.m_blocks_unlinked.equal_range(pindex);
4121 while (range.first != range.second) {
4122 std::multimap<CBlockIndex *, CBlockIndex *>::iterator it =
4123 range.first;
4124 queue.push_back(it->second);
4125 range.first++;
4126 m_blockman.m_blocks_unlinked.erase(it);
4127 }
4128 }
4129 } else if (pindexNew->pprev &&
4130 pindexNew->pprev->IsValid(BlockValidity::TREE)) {
4132 std::make_pair(pindexNew->pprev, pindexNew));
4133 }
4134}
4135
4144static bool CheckBlockHeader(const CBlockHeader &block,
4145 BlockValidationState &state,
4146 const Consensus::Params &params,
4148 // Check proof of work matches claimed amount
4149 if (validationOptions.shouldValidatePoW() &&
4150 !CheckProofOfWork(block.GetHash(), block.nBits, params)) {
4152 "high-hash", "proof of work failed");
4153 }
4154
4155 return true;
4156}
4157
4158bool CheckBlock(const CBlock &block, BlockValidationState &state,
4159 const Consensus::Params &params,
4161 // These are checks that are independent of context.
4162 if (block.fChecked) {
4163 return true;
4164 }
4165
4166 // Check that the header is valid (particularly PoW). This is mostly
4167 // redundant with the call in AcceptBlockHeader.
4168 if (!CheckBlockHeader(block, state, params, validationOptions)) {
4169 return false;
4170 }
4171
4172 // Check the merkle root.
4173 if (validationOptions.shouldValidateMerkleRoot()) {
4174 bool mutated;
4176 if (block.hashMerkleRoot != hashMerkleRoot2) {
4178 "bad-txnmrklroot", "hashMerkleRoot mismatch");
4179 }
4180
4181 // Check for merkle tree malleability (CVE-2012-2459): repeating
4182 // sequences of transactions in a block without affecting the merkle
4183 // root of a block, while still invalidating it.
4184 if (mutated) {
4186 "bad-txns-duplicate", "duplicate transaction");
4187 }
4188 }
4189
4190 // All potential-corruption validation must be done before we do any
4191 // transaction validation, as otherwise we may mark the header as invalid
4192 // because we receive the wrong transactions for it.
4193
4194 // First transaction must be coinbase.
4195 if (block.vtx.empty()) {
4197 "bad-cb-missing", "first tx is not coinbase");
4198 }
4199
4200 // Size limits.
4201 auto nMaxBlockSize = validationOptions.getExcessiveBlockSize();
4202
4203 // Bail early if there is no way this block is of reasonable size.
4204 if ((block.vtx.size() * MIN_TRANSACTION_SIZE) > nMaxBlockSize) {
4206 "bad-blk-length", "size limits failed");
4207 }
4208
4210 if (currentBlockSize > nMaxBlockSize) {
4212 "bad-blk-length", "size limits failed");
4213 }
4214
4215 // And a valid coinbase.
4217 if (!CheckCoinbase(*block.vtx[0], tx_state)) {
4219 tx_state.GetRejectReason(),
4220 strprintf("Coinbase check failed (txid %s) %s",
4221 block.vtx[0]->GetId().ToString(),
4222 tx_state.GetDebugMessage()));
4223 }
4224
4225 // Check transactions for regularity, skipping the first. Note that this
4226 // is the first time we check that all after the first are !IsCoinBase.
4227 for (size_t i = 1; i < block.vtx.size(); i++) {
4228 auto *tx = block.vtx[i].get();
4229 if (!CheckRegularTransaction(*tx, tx_state)) {
4230 return state.Invalid(
4232 tx_state.GetRejectReason(),
4233 strprintf("Transaction check failed (txid %s) %s",
4234 tx->GetId().ToString(), tx_state.GetDebugMessage()));
4235 }
4236 }
4237
4238 if (validationOptions.shouldValidatePoW() &&
4239 validationOptions.shouldValidateMerkleRoot()) {
4240 block.fChecked = true;
4241 }
4242
4243 return true;
4244}
4245
4246bool HasValidProofOfWork(const std::vector<CBlockHeader> &headers,
4248 return std::all_of(headers.cbegin(), headers.cend(),
4249 [&](const auto &header) {
4250 return CheckProofOfWork(
4251 header.GetHash(), header.nBits, consensusParams);
4252 });
4253}
4254
4255arith_uint256 CalculateHeadersWork(const std::vector<CBlockHeader> &headers) {
4257 for (const CBlockHeader &header : headers) {
4258 CBlockIndex dummy(header);
4259 total_work += GetBlockProof(dummy);
4260 }
4261 return total_work;
4262}
4263
4275 const CBlockHeader &block, BlockValidationState &state,
4278 const std::optional<CCheckpointData> &test_checkpoints = std::nullopt)
4281 assert(pindexPrev != nullptr);
4282 const int nHeight = pindexPrev->nHeight + 1;
4283
4284 const CChainParams &params = chainman.GetParams();
4285
4286 // Check proof of work
4287 if (block.nBits != GetNextWorkRequired(pindexPrev, &block, params)) {
4288 LogPrintf("bad bits after height: %d\n", pindexPrev->nHeight);
4290 "bad-diffbits", "incorrect proof of work");
4291 }
4292
4293 // Check against checkpoints
4294 if (chainman.m_options.checkpoints_enabled) {
4296 test_checkpoints ? test_checkpoints.value() : params.Checkpoints();
4297
4298 // Check that the block chain matches the known block chain up to a
4299 // checkpoint.
4302 "ERROR: %s: rejected by checkpoint lock-in at %d\n",
4303 __func__, nHeight);
4305 "checkpoint mismatch");
4306 }
4307
4308 // Don't accept any forks from the main chain prior to last checkpoint.
4309 // GetLastCheckpoint finds the last checkpoint in MapCheckpoints that's
4310 // in our BlockIndex().
4311
4312 const CBlockIndex *pcheckpoint =
4313 blockman.GetLastCheckpoint(checkpoints);
4316 "ERROR: %s: forked chain older than last checkpoint "
4317 "(height %d)\n",
4318 __func__, nHeight);
4320 "bad-fork-prior-to-checkpoint");
4321 }
4322 }
4323
4324 // Check timestamp against prev
4325 if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast()) {
4327 "time-too-old", "block's timestamp is too early");
4328 }
4329
4330 // Check timestamp
4331 if (block.Time() > now + std::chrono::seconds{MAX_FUTURE_BLOCK_TIME}) {
4333 "time-too-new",
4334 "block timestamp too far in the future");
4335 }
4336
4337 // Reject blocks with outdated version
4338 if ((block.nVersion < 2 &&
4341 (block.nVersion < 3 &&
4344 (block.nVersion < 4 &&
4347 return state.Invalid(
4349 strprintf("bad-version(0x%08x)", block.nVersion),
4350 strprintf("rejected nVersion=0x%08x block", block.nVersion));
4351 }
4352
4353 return true;
4354}
4355
4357 const CBlockIndex &active_chain_tip, const Consensus::Params &params,
4358 const CTransaction &tx, TxValidationState &state) {
4360
4361 // ContextualCheckTransactionForCurrentBlock() uses
4362 // active_chain_tip.Height()+1 to evaluate nLockTime because when
4363 // IsFinalTx() is called within AcceptBlock(), the height of the
4364 // block *being* evaluated is what is used. Thus if we want to know if a
4365 // transaction can be part of the *next* block, we need to call
4366 // ContextualCheckTransaction() with one more than
4367 // active_chain_tip.Height().
4368 const int nBlockHeight = active_chain_tip.nHeight + 1;
4369
4370 // BIP113 will require that time-locked transactions have nLockTime set to
4371 // less than the median time of the previous block they're contained in.
4372 // When the next block is created its previous block will be the current
4373 // chain tip, so we use that to calculate the median time passed to
4374 // ContextualCheckTransaction().
4375 // This time can also be used for consensus upgrades.
4376 const int64_t nMedianTimePast{active_chain_tip.GetMedianTimePast()};
4377
4378 return ContextualCheckTransaction(params, tx, state, nBlockHeight,
4380}
4381
4389static bool ContextualCheckBlock(const CBlock &block,
4390 BlockValidationState &state,
4391 const ChainstateManager &chainman,
4392 const CBlockIndex *pindexPrev) {
4393 const int nHeight = pindexPrev == nullptr ? 0 : pindexPrev->nHeight + 1;
4394
4395 // Enforce BIP113 (Median Time Past).
4397 if (DeploymentActiveAfter(pindexPrev, chainman,
4399 assert(pindexPrev != nullptr);
4401 }
4402
4403 const int64_t nMedianTimePast =
4404 pindexPrev == nullptr ? 0 : pindexPrev->GetMedianTimePast();
4405
4408 : block.GetBlockTime()};
4409
4410 const Consensus::Params params = chainman.GetConsensus();
4411 const bool fIsMagneticAnomalyEnabled =
4413
4414 // Check transactions:
4415 // - canonical ordering
4416 // - ensure they are finalized
4417 // - check they have the minimum size
4418 const CTransaction *prevTx = nullptr;
4419 for (const auto &ptx : block.vtx) {
4420 const CTransaction &tx = *ptx;
4422 if (prevTx && (tx.GetId() <= prevTx->GetId())) {
4423 if (tx.GetId() == prevTx->GetId()) {
4425 "tx-duplicate",
4426 strprintf("Duplicated transaction %s",
4427 tx.GetId().ToString()));
4428 }
4429
4430 return state.Invalid(
4432 strprintf("Transaction order is invalid (%s < %s)",
4433 tx.GetId().ToString(),
4434 prevTx->GetId().ToString()));
4435 }
4436
4437 if (prevTx || !tx.IsCoinBase()) {
4438 prevTx = &tx;
4439 }
4440 }
4441
4444 nLockTimeCutoff)) {
4446 tx_state.GetRejectReason(),
4447 tx_state.GetDebugMessage());
4448 }
4449 }
4450
4451 // Enforce rule that the coinbase starts with serialized block height
4452 if (DeploymentActiveAfter(pindexPrev, chainman,
4455 if (block.vtx[0]->vin[0].scriptSig.size() < expect.size() ||
4456 !std::equal(expect.begin(), expect.end(),
4457 block.vtx[0]->vin[0].scriptSig.begin())) {
4459 "bad-cb-height",
4460 "block height mismatch in coinbase");
4461 }
4462 }
4463
4464 return true;
4465}
4466
4473 const CBlockHeader &block, BlockValidationState &state,
4475 const std::optional<CCheckpointData> &test_checkpoints) {
4477 const Config &config = this->GetConfig();
4478 const CChainParams &chainparams = config.GetChainParams();
4479
4480 // Check for duplicate
4481 BlockHash hash = block.GetHash();
4482 BlockMap::iterator miSelf{m_blockman.m_block_index.find(hash)};
4483 if (hash != chainparams.GetConsensus().hashGenesisBlock) {
4484 if (miSelf != m_blockman.m_block_index.end()) {
4485 // Block header is already known.
4486 CBlockIndex *pindex = &(miSelf->second);
4487 if (ppindex) {
4488 *ppindex = pindex;
4489 }
4490
4491 if (pindex->nStatus.isInvalid()) {
4492 LogPrint(BCLog::VALIDATION, "%s: block %s is marked invalid\n",
4493 __func__, hash.ToString());
4494 return state.Invalid(
4496 }
4497
4498 return true;
4499 }
4500
4501 if (!CheckBlockHeader(block, state, chainparams.GetConsensus(),
4502 BlockValidationOptions(config))) {
4504 "%s: Consensus::CheckBlockHeader: %s, %s\n", __func__,
4505 hash.ToString(), state.ToString());
4506 return false;
4507 }
4508
4509 // Get prev block index
4510 BlockMap::iterator mi{
4511 m_blockman.m_block_index.find(block.hashPrevBlock)};
4512 if (mi == m_blockman.m_block_index.end()) {
4514 "header %s has prev block not found: %s\n",
4515 hash.ToString(), block.hashPrevBlock.ToString());
4517 "prev-blk-not-found");
4518 }
4519
4520 CBlockIndex *pindexPrev = &((*mi).second);
4522 if (pindexPrev->nStatus.isInvalid()) {
4524 "header %s has prev block invalid: %s\n", hash.ToString(),
4525 block.hashPrevBlock.ToString());
4527 "bad-prevblk");
4528 }
4529
4531 block, state, m_blockman, *this, pindexPrev,
4534 "%s: Consensus::ContextualCheckBlockHeader: %s, %s\n",
4535 __func__, hash.ToString(), state.ToString());
4536 return false;
4537 }
4538
4539 /* Determine if this block descends from any block which has been found
4540 * invalid (m_failed_blocks), then mark pindexPrev and any blocks
4541 * between them as failed. For example:
4542 *
4543 * D3
4544 * /
4545 * B2 - C2
4546 * / \
4547 * A D2 - E2 - F2
4548 * \
4549 * B1 - C1 - D1 - E1
4550 *
4551 * In the case that we attempted to reorg from E1 to F2, only to find
4552 * C2 to be invalid, we would mark D2, E2, and F2 as BLOCK_FAILED_CHILD
4553 * but NOT D3 (it was not in any of our candidate sets at the time).
4554 *
4555 * In any case D3 will also be marked as BLOCK_FAILED_CHILD at restart
4556 * in LoadBlockIndex.
4557 */
4558 if (!pindexPrev->IsValid(BlockValidity::SCRIPTS)) {
4559 // The above does not mean "invalid": it checks if the previous
4560 // block hasn't been validated up to BlockValidity::SCRIPTS. This is
4561 // a performance optimization, in the common case of adding a new
4562 // block to the tip, we don't need to iterate over the failed blocks
4563 // list.
4564 for (const CBlockIndex *failedit : m_failed_blocks) {
4565 if (pindexPrev->GetAncestor(failedit->nHeight) == failedit) {
4566 assert(failedit->nStatus.hasFailed());
4568 while (invalid_walk != failedit) {
4569 invalid_walk->nStatus =
4570 invalid_walk->nStatus.withFailedParent();
4572 invalid_walk = invalid_walk->pprev;
4573 }
4575 "header %s has prev block invalid: %s\n",
4576 hash.ToString(), block.hashPrevBlock.ToString());
4577 return state.Invalid(
4579 "bad-prevblk");
4580 }
4581 }
4582 }
4583 }
4584 if (!min_pow_checked) {
4586 "%s: not adding new block header %s, missing anti-dos "
4587 "proof-of-work validation\n",
4588 __func__, hash.ToString());
4590 "too-little-chainwork");
4591 }
4593
4594 if (ppindex) {
4595 *ppindex = pindex;
4596 }
4597
4598 // Since this is the earliest point at which we have determined that a
4599 // header is both new and valid, log here.
4600 //
4601 // These messages are valuable for detecting potential selfish mining
4602 // behavior; if multiple displacing headers are seen near simultaneously
4603 // across many nodes in the network, this might be an indication of selfish
4604 // mining. Having this log by default when not in IBD ensures broad
4605 // availability of this data in case investigation is merited.
4606 const auto msg = strprintf("Saw new header hash=%s height=%d",
4607 hash.ToString(), pindex->nHeight);
4608
4609 if (ActiveChainstate().IsInitialBlockDownload()) {
4611 } else {
4612 LogPrintf("%s\n", msg);
4613 }
4614
4615 return true;
4616}
4617
4618// Exposed wrapper for AcceptBlockHeader
4620 const std::vector<CBlockHeader> &headers, bool min_pow_checked,
4622 const std::optional<CCheckpointData> &test_checkpoints) {
4624 {
4625 LOCK(cs_main);
4626 for (const CBlockHeader &header : headers) {
4627 // Use a temp pindex instead of ppindex to avoid a const_cast
4628 CBlockIndex *pindex = nullptr;
4630 header, state, &pindex, min_pow_checked, test_checkpoints);
4631 ActiveChainstate().CheckBlockIndex();
4632
4633 if (!accepted) {
4634 return false;
4635 }
4636
4637 if (ppindex) {
4638 *ppindex = pindex;
4639 }
4640 }
4641 }
4642
4644 if (ActiveChainstate().IsInitialBlockDownload() && ppindex &&
4645 *ppindex) {
4647 const int64_t blocks_left{
4648 (GetTime() - last_accepted.GetBlockTime()) /
4650 const double progress{100.0 * last_accepted.nHeight /
4651 (last_accepted.nHeight + blocks_left)};
4652 LogPrintf("Synchronizing blockheaders, height: %d (~%.2f%%)\n",
4653 last_accepted.nHeight, progress);
4654 }
4655 }
4656 return true;
4657}
4658
4660 int64_t height,
4663 const auto &chainstate = ActiveChainstate();
4664 {
4665 LOCK(cs_main);
4666 // Don't report headers presync progress if we already have a
4667 // post-minchainwork header chain.
4668 // This means we lose reporting for potentially legimate, but unlikely,
4669 // deep reorgs, but prevent attackers that spam low-work headers from
4670 // filling our logs.
4671 if (m_best_header->nChainWork >=
4672 UintToArith256(GetConsensus().nMinimumChainWork)) {
4673 return;
4674 }
4675 // Rate limit headers presync updates to 4 per second, as these are not
4676 // subject to DoS protection.
4677 auto now = Now<SteadyMilliseconds>();
4678 if (now < m_last_presync_update + 250ms) {
4679 return;
4680 }
4682 }
4683 bool initial_download = chainstate.IsInitialBlockDownload();
4685 height, timestamp, /*presync=*/true);
4686 if (initial_download) {
4687 const int64_t blocks_left{(GetTime() - timestamp) /
4689 const double progress{100.0 * height / (height + blocks_left)};
4690 LogPrintf("Pre-synchronizing blockheaders, height: %d (~%.2f%%)\n",
4691 height, progress);
4692 }
4693}
4694
4707bool Chainstate::AcceptBlock(const std::shared_ptr<const CBlock> &pblock,
4708 BlockValidationState &state, bool fRequested,
4709 const FlatFilePos *dbp, bool *fNewBlock,
4710 bool min_pow_checked) {
4712
4713 const CBlock &block = *pblock;
4714 if (fNewBlock) {
4715 *fNewBlock = false;
4716 }
4717
4718 CBlockIndex *pindex = nullptr;
4719
4720 bool accepted_header{
4721 m_chainman.AcceptBlockHeader(block, state, &pindex, min_pow_checked)};
4723
4724 if (!accepted_header) {
4725 return false;
4726 }
4727
4728 // Try to process all requested blocks that we don't have, but only
4729 // process an unrequested block if it's new and has enough work to
4730 // advance our tip, and isn't too many blocks ahead.
4731 bool fAlreadyHave = pindex->nStatus.hasData();
4732
4733 // TODO: deal better with return value and error conditions for duplicate
4734 // and unrequested blocks.
4735 if (fAlreadyHave) {
4736 return true;
4737 }
4738
4739 // Compare block header timestamps and received times of the block and the
4740 // chaintip. If they have the same chain height, use these diffs as a
4741 // tie-breaker, attempting to pick the more honestly-mined block.
4742 int64_t newBlockTimeDiff = std::llabs(pindex->GetReceivedTimeDiff());
4744 m_chain.Tip() ? std::llabs(m_chain.Tip()->GetReceivedTimeDiff()) : 0;
4745
4746 bool isSameHeight =
4747 m_chain.Tip() && (pindex->nChainWork == m_chain.Tip()->nChainWork);
4748 if (isSameHeight) {
4749 LogPrintf("Chain tip timestamp-to-received-time difference: hash=%s, "
4750 "diff=%d\n",
4752 LogPrintf("New block timestamp-to-received-time difference: hash=%s, "
4753 "diff=%d\n",
4755 }
4756
4757 bool fHasMoreOrSameWork =
4758 (m_chain.Tip() ? pindex->nChainWork >= m_chain.Tip()->nChainWork
4759 : true);
4760
4761 // Blocks that are too out-of-order needlessly limit the effectiveness of
4762 // pruning, because pruning will not delete block files that contain any
4763 // blocks which are too close in height to the tip. Apply this test
4764 // regardless of whether pruning is enabled; it should generally be safe to
4765 // not process unrequested blocks.
4766 bool fTooFarAhead{pindex->nHeight >
4768
4769 // TODO: Decouple this function from the block download logic by removing
4770 // fRequested
4771 // This requires some new chain data structure to efficiently look up if a
4772 // block is in a chain leading to a candidate for best tip, despite not
4773 // being such a candidate itself.
4774 // Note that this would break the getblockfrompeer RPC
4775
4776 // If we didn't ask for it:
4777 if (!fRequested) {
4778 // This is a previously-processed block that was pruned.
4779 if (pindex->nTx != 0) {
4780 return true;
4781 }
4782
4783 // Don't process less-work chains.
4784 if (!fHasMoreOrSameWork) {
4785 return true;
4786 }
4787
4788 // Block height is too high.
4789 if (fTooFarAhead) {
4790 return true;
4791 }
4792
4793 // Protect against DoS attacks from low-work chains.
4794 // If our tip is behind, a peer could try to send us
4795 // low-work blocks on a fake chain that we would never
4796 // request; don't process these.
4797 if (pindex->nChainWork < m_chainman.MinimumChainWork()) {
4798 return true;
4799 }
4800 }
4801
4802 const CChainParams &params{m_chainman.GetParams()};
4803 const Consensus::Params &consensusParams = params.GetConsensus();
4804
4805 if (!CheckBlock(block, state, consensusParams,
4807 !ContextualCheckBlock(block, state, m_chainman, pindex->pprev)) {
4808 if (state.IsInvalid() &&
4810 pindex->nStatus = pindex->nStatus.withFailed();
4811 m_blockman.m_dirty_blockindex.insert(pindex);
4812 }
4813
4814 return error("%s: %s (block %s)", __func__, state.ToString(),
4815 block.GetHash().ToString());
4816 }
4817
4818 // If connecting the new block would require rewinding more than one block
4819 // from the active chain (i.e., a "deep reorg"), then mark the new block as
4820 // parked. If it has enough work then it will be automatically unparked
4821 // later, during FindMostWorkChain. We mark the block as parked at the very
4822 // last minute so we can make sure everything is ready to be reorged if
4823 // needed.
4824 if (gArgs.GetBoolArg("-parkdeepreorg", true)) {
4825 const CBlockIndex *pindexFork = m_chain.FindFork(pindex);
4826 if (pindexFork && pindexFork->nHeight + 1 < m_chain.Height()) {
4827 LogPrintf("Park block %s as it would cause a deep reorg.\n",
4828 pindex->GetBlockHash().ToString());
4829 pindex->nStatus = pindex->nStatus.withParked();
4830 m_blockman.m_dirty_blockindex.insert(pindex);
4831 }
4832 }
4833
4834 // Header is valid/has work and the merkle tree is good.
4835 // Relay now, but if it does not build on our best tip, let the
4836 // SendMessages loop relay it.
4837 if (!IsInitialBlockDownload() && m_chain.Tip() == pindex->pprev) {
4839 }
4840
4841 // Write block to history file
4842 if (fNewBlock) {
4843 *fNewBlock = true;
4844 }
4845 try {
4847 m_blockman.SaveBlockToDisk(block, pindex->nHeight, m_chain, dbp)};
4848 if (blockPos.IsNull()) {
4849 state.Error(strprintf(
4850 "%s: Failed to find position to write new block to disk",
4851 __func__));
4852 return false;
4853 }
4854 ReceivedBlockTransactions(block, pindex, blockPos);
4855 } catch (const std::runtime_error &e) {
4856 return AbortNode(state, std::string("System error: ") + e.what());
4857 }
4858
4860
4862
4863 return true;
4864}
4865
4867 const std::shared_ptr<const CBlock> &block, bool force_processing,
4868 bool min_pow_checked, bool *new_block,
4871
4872 {
4873 if (new_block) {
4874 *new_block = false;
4875 }
4876
4878
4879 // CheckBlock() does not support multi-threaded block validation
4880 // because CBlock::fChecked can cause data race.
4881 // Therefore, the following critical section must include the
4882 // CheckBlock() call as well.
4883 LOCK(cs_main);
4884
4885 // Skipping AcceptBlock() for CheckBlock() failures means that we will
4886 // never mark a block as invalid if CheckBlock() fails. This is
4887 // protective against consensus failure if there are any unknown form
4888 // s of block malleability that cause CheckBlock() to fail; see e.g.
4889 // CVE-2012-2459 and
4890 // https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2019-February/016697.html.
4891 // Because CheckBlock() is not very expensive, the anti-DoS benefits of
4892 // caching failure (of a definitely-invalid block) are not substantial.
4893 bool ret = CheckBlock(*block, state, this->GetConsensus(),
4895 if (ret) {
4896 // Store to disk
4897 ret = ActiveChainstate().AcceptBlock(block, state, force_processing,
4898 nullptr, new_block,
4900 }
4901
4902 if (!ret) {
4903 GetMainSignals().BlockChecked(*block, state);
4904 return error("%s: AcceptBlock FAILED (%s)", __func__,
4905 state.ToString());
4906 }
4907 }
4908
4910
4911 // Only used to report errors, not invalidity - ignore it
4913 if (!ActiveChainstate().ActivateBestChain(state, block, avalanche)) {
4914 return error("%s: ActivateBestChain failed (%s)", __func__,
4915 state.ToString());
4916 }
4917
4918 return true;
4919}
4920
4923 bool test_accept) {
4926 if (!active_chainstate.GetMempool()) {
4927 TxValidationState state;
4928 state.Invalid(TxValidationResult::TX_NO_MEMPOOL, "no-mempool");
4929 return MempoolAcceptResult::Failure(state);
4930 }
4931 auto result = AcceptToMemoryPool(active_chainstate, tx, GetTime(),
4932 /*bypass_limits=*/false, test_accept);
4933 active_chainstate.GetMempool()->check(
4934 active_chainstate.CoinsTip(), active_chainstate.m_chain.Height() + 1);
4935 return result;
4936}
4937
4939 BlockValidationState &state, const CChainParams &params,
4941 const std::function<NodeClock::time_point()> &adjusted_time_callback,
4944 assert(pindexPrev && pindexPrev == chainstate.m_chain.Tip());
4945 CCoinsViewCache viewNew(&chainstate.CoinsTip());
4946 BlockHash block_hash(block.GetHash());
4947 CBlockIndex indexDummy(block);
4948 indexDummy.pprev = pindexPrev;
4949 indexDummy.nHeight = pindexPrev->nHeight + 1;
4950 indexDummy.phashBlock = &block_hash;
4951
4952 // NOTE: CheckBlockHeader is called by CheckBlock
4953 if (!ContextualCheckBlockHeader(block, state, chainstate.m_blockman,
4954 chainstate.m_chainman, pindexPrev,
4955 adjusted_time_callback())) {
4956 return error("%s: Consensus::ContextualCheckBlockHeader: %s", __func__,
4957 state.ToString());
4958 }
4959
4960 if (!CheckBlock(block, state, params.GetConsensus(), validationOptions)) {
4961 return error("%s: Consensus::CheckBlock: %s", __func__,
4962 state.ToString());
4963 }
4964
4965 if (!ContextualCheckBlock(block, state, chainstate.m_chainman,
4966 pindexPrev)) {
4967 return error("%s: Consensus::ContextualCheckBlock: %s", __func__,
4968 state.ToString());
4969 }
4970
4971 if (!chainstate.ConnectBlock(block, state, &indexDummy, viewNew,
4972 validationOptions, nullptr, true)) {
4973 return false;
4974 }
4975
4976 assert(state.IsValid());
4977 return true;
4978}
4979
4980/* This function is called from the RPC code for pruneblockchain */
4982 int nManualPruneHeight) {
4984 if (active_chainstate.FlushStateToDisk(state, FlushStateMode::NONE,
4986 LogPrintf("%s: failed to flush state (%s)\n", __func__,
4987 state.ToString());
4988 }
4989}
4990
4992 FopenFn mockable_fopen_function) {
4993 if (!m_mempool) {
4994 return;
4995 }
4996 ::LoadMempool(*m_mempool, load_path, *this, mockable_fopen_function);
4998}
4999
5003 // Never called when the coins view is empty
5004 assert(!coins_cache.GetBestBlock().IsNull());
5005 const CBlockIndex *tip = m_chain.Tip();
5006
5007 if (tip && tip->GetBlockHash() == coins_cache.GetBestBlock()) {
5008 return true;
5009 }
5010
5011 // Load pointer to end of best chain
5012 CBlockIndex *pindex =
5013 m_blockman.LookupBlockIndex(coins_cache.GetBestBlock());
5014 if (!pindex) {
5015 return false;
5016 }
5017 m_chain.SetTip(*pindex);
5019
5020 tip = m_chain.Tip();
5021 LogPrintf(
5022 "Loaded best chain: hashBestChain=%s height=%d date=%s progress=%f\n",
5023 tip->GetBlockHash().ToString(), m_chain.Height(),
5024 FormatISO8601DateTime(tip->GetBlockTime()),
5026 return true;
5027}
5028
5030 : m_notifications{notifications} {
5031 m_notifications.progress(_("Verifying blocks…"), 0, false);
5032}
5033
5037
5040 int nCheckDepth) {
5042
5043 const Config &config = chainstate.m_chainman.GetConfig();
5044 const CChainParams &params = config.GetChainParams();
5046
5047 if (chainstate.m_chain.Tip() == nullptr ||
5048 chainstate.m_chain.Tip()->pprev == nullptr) {
5050 }
5051
5052 // Verify blocks in the best chain
5053 if (nCheckDepth <= 0 || nCheckDepth > chainstate.m_chain.Height()) {
5054 nCheckDepth = chainstate.m_chain.Height();
5055 }
5056
5057 nCheckLevel = std::max(0, std::min(4, nCheckLevel));
5058 LogPrintf("Verifying last %i blocks at level %i\n", nCheckDepth,
5059 nCheckLevel);
5060
5061 CCoinsViewCache coins(&coinsview);
5062 CBlockIndex *pindex;
5063 CBlockIndex *pindexFailure = nullptr;
5064 int nGoodTransactions = 0;
5066 int reportDone = 0;
5067 bool skipped_no_block_data{false};
5068 bool skipped_l3_checks{false};
5069 LogPrintf("Verification progress: 0%%\n");
5070
5071 const bool is_snapshot_cs{!chainstate.m_from_snapshot_blockhash};
5072
5073 for (pindex = chainstate.m_chain.Tip(); pindex && pindex->pprev;
5074 pindex = pindex->pprev) {
5075 const int percentageDone = std::max(
5076 1, std::min(99, (int)(((double)(chainstate.m_chain.Height() -
5077 pindex->nHeight)) /
5078 (double)nCheckDepth *
5079 (nCheckLevel >= 4 ? 50 : 100))));
5080 if (reportDone < percentageDone / 10) {
5081 // report every 10% step
5082 LogPrintf("Verification progress: %d%%\n", percentageDone);
5084 }
5085
5086 m_notifications.progress(_("Verifying blocks…"), percentageDone, false);
5087 if (pindex->nHeight <= chainstate.m_chain.Height() - nCheckDepth) {
5088 break;
5089 }
5090
5091 if ((chainstate.m_blockman.IsPruneMode() || is_snapshot_cs) &&
5092 !pindex->nStatus.hasData()) {
5093 // If pruning or running under an assumeutxo snapshot, only go
5094 // back as far as we have data.
5095 LogPrintf("VerifyDB(): block verification stopping at height %d "
5096 "(no data). This could be due to pruning or use of an "
5097 "assumeutxo snapshot.\n",
5098 pindex->nHeight);
5099 skipped_no_block_data = true;
5100 break;
5101 }
5102
5103 CBlock block;
5104
5105 // check level 0: read from disk
5106 if (!chainstate.m_blockman.ReadBlockFromDisk(block, *pindex)) {
5107 LogPrintf(
5108 "Verification error: ReadBlockFromDisk failed at %d, hash=%s\n",
5109 pindex->nHeight, pindex->GetBlockHash().ToString());
5111 }
5112
5113 // check level 1: verify block validity
5114 if (nCheckLevel >= 1 && !CheckBlock(block, state, consensusParams,
5115 BlockValidationOptions(config))) {
5116 LogPrintf(
5117 "Verification error: found bad block at %d, hash=%s (%s)\n",
5118 pindex->nHeight, pindex->GetBlockHash().ToString(),
5119 state.ToString());
5121 }
5122
5123 // check level 2: verify undo validity
5124 if (nCheckLevel >= 2 && pindex) {
5126 if (!pindex->GetUndoPos().IsNull()) {
5127 if (!chainstate.m_blockman.UndoReadFromDisk(undo, *pindex)) {
5128 LogPrintf("Verification error: found bad undo data at %d, "
5129 "hash=%s\n",
5130 pindex->nHeight,
5131 pindex->GetBlockHash().ToString());
5133 }
5134 }
5135 }
5136 // check level 3: check for inconsistencies during memory-only
5137 // disconnect of tip blocks
5138 size_t curr_coins_usage = coins.DynamicMemoryUsage() +
5139 chainstate.CoinsTip().DynamicMemoryUsage();
5140
5141 if (nCheckLevel >= 3) {
5142 if (curr_coins_usage <= chainstate.m_coinstip_cache_size_bytes) {
5143 assert(coins.GetBestBlock() == pindex->GetBlockHash());
5145 chainstate.DisconnectBlock(block, pindex, coins);
5147 LogPrintf("Verification error: irrecoverable inconsistency "
5148 "in block data at %d, hash=%s\n",
5149 pindex->nHeight,
5150 pindex->GetBlockHash().ToString());
5152 }
5155 pindexFailure = pindex;
5156 } else {
5157 nGoodTransactions += block.vtx.size();
5158 }
5159 } else {
5160 skipped_l3_checks = true;
5161 }
5162 }
5163
5164 if (ShutdownRequested()) {
5166 }
5167 }
5168
5169 if (pindexFailure) {
5170 LogPrintf("Verification error: coin database inconsistencies found "
5171 "(last %i blocks, %i good transactions before that)\n",
5172 chainstate.m_chain.Height() - pindexFailure->nHeight + 1,
5175 }
5176 if (skipped_l3_checks) {
5177 LogPrintf("Skipped verification of level >=3 (insufficient database "
5178 "cache size). Consider increasing -dbcache.\n");
5179 }
5180
5181 // store block count as we move pindex at check level >= 4
5182 int block_count = chainstate.m_chain.Height() - pindex->nHeight;
5183
5184 // check level 4: try reconnecting blocks
5185 if (nCheckLevel >= 4 && !skipped_l3_checks) {
5186 while (pindex != chainstate.m_chain.Tip()) {
5187 const int percentageDone = std::max(
5188 1, std::min(99, 100 - int(double(chainstate.m_chain.Height() -
5189 pindex->nHeight) /
5190 double(nCheckDepth) * 50)));
5191 if (reportDone < percentageDone / 10) {
5192 // report every 10% step
5193 LogPrintf("Verification progress: %d%%\n", percentageDone);
5195 }
5196 m_notifications.progress(_("Verifying blocks…"), percentageDone,
5197 false);
5198 pindex = chainstate.m_chain.Next(pindex);
5199 CBlock block;
5200 if (!chainstate.m_blockman.ReadBlockFromDisk(block, *pindex)) {
5201 LogPrintf("Verification error: ReadBlockFromDisk failed at %d, "
5202 "hash=%s\n",
5203 pindex->nHeight, pindex->GetBlockHash().ToString());
5205 }
5206 if (!chainstate.ConnectBlock(block, state, pindex, coins,
5207 BlockValidationOptions(config))) {
5208 LogPrintf("Verification error: found unconnectable block at "
5209 "%d, hash=%s (%s)\n",
5210 pindex->nHeight, pindex->GetBlockHash().ToString(),
5211 state.ToString());
5213 }
5214 if (ShutdownRequested()) {
5216 }
5217 }
5218 }
5219
5220 LogPrintf("Verification: No coin database inconsistencies in last %i "
5221 "blocks (%i transactions)\n",
5223
5224 if (skipped_l3_checks) {
5226 }
5229 }
5231}
5232
5240 // TODO: merge with ConnectBlock
5241 CBlock block;
5242 if (!m_blockman.ReadBlockFromDisk(block, *pindex)) {
5243 return error("ReplayBlock(): ReadBlockFromDisk failed at %d, hash=%s",
5244 pindex->nHeight, pindex->GetBlockHash().ToString());
5245 }
5246
5247 for (const CTransactionRef &tx : block.vtx) {
5248 // Pass check = true as every addition may be an overwrite.
5249 AddCoins(view, *tx, pindex->nHeight, true);
5250 }
5251
5252 for (const CTransactionRef &tx : block.vtx) {
5253 if (tx->IsCoinBase()) {
5254 continue;
5255 }
5256
5257 for (const CTxIn &txin : tx->vin) {
5258 view.SpendCoin(txin.prevout);
5259 }
5260 }
5261
5262 return true;
5263}
5264
5266 LOCK(cs_main);
5267
5268 CCoinsView &db = this->CoinsDB();
5269 CCoinsViewCache cache(&db);
5270
5271 std::vector<BlockHash> hashHeads = db.GetHeadBlocks();
5272 if (hashHeads.empty()) {
5273 // We're already in a consistent state.
5274 return true;
5275 }
5276 if (hashHeads.size() != 2) {
5277 return error("ReplayBlocks(): unknown inconsistent state");
5278 }
5279
5280 m_chainman.GetNotifications().progress(_("Replaying blocks…"), 0, false);
5281 LogPrintf("Replaying blocks\n");
5282
5283 // Old tip during the interrupted flush.
5284 const CBlockIndex *pindexOld = nullptr;
5285 // New tip during the interrupted flush.
5286 const CBlockIndex *pindexNew;
5287 // Latest block common to both the old and the new tip.
5288 const CBlockIndex *pindexFork = nullptr;
5289
5290 if (m_blockman.m_block_index.count(hashHeads[0]) == 0) {
5291 return error(
5292 "ReplayBlocks(): reorganization to unknown block requested");
5293 }
5294
5295 pindexNew = &(m_blockman.m_block_index[hashHeads[0]]);
5296
5297 if (!hashHeads[1].IsNull()) {
5298 // The old tip is allowed to be 0, indicating it's the first flush.
5299 if (m_blockman.m_block_index.count(hashHeads[1]) == 0) {
5300 return error(
5301 "ReplayBlocks(): reorganization from unknown block requested");
5302 }
5303
5304 pindexOld = &(m_blockman.m_block_index[hashHeads[1]]);
5306 assert(pindexFork != nullptr);
5307 }
5308
5309 // Rollback along the old branch.
5310 while (pindexOld != pindexFork) {
5311 if (pindexOld->nHeight > 0) {
5312 // Never disconnect the genesis block.
5313 CBlock block;
5314 if (!m_blockman.ReadBlockFromDisk(block, *pindexOld)) {
5315 return error("RollbackBlock(): ReadBlockFromDisk() failed at "
5316 "%d, hash=%s",
5317 pindexOld->nHeight,
5318 pindexOld->GetBlockHash().ToString());
5319 }
5320
5321 LogPrintf("Rolling back %s (%i)\n",
5322 pindexOld->GetBlockHash().ToString(), pindexOld->nHeight);
5325 return error(
5326 "RollbackBlock(): DisconnectBlock failed at %d, hash=%s",
5327 pindexOld->nHeight, pindexOld->GetBlockHash().ToString());
5328 }
5329
5330 // If DisconnectResult::UNCLEAN is returned, it means a non-existing
5331 // UTXO was deleted, or an existing UTXO was overwritten. It
5332 // corresponds to cases where the block-to-be-disconnect never had
5333 // all its operations applied to the UTXO set. However, as both
5334 // writing a UTXO and deleting a UTXO are idempotent operations, the
5335 // result is still a version of the UTXO set with the effects of
5336 // that block undone.
5337 }
5338 pindexOld = pindexOld->pprev;
5339 }
5340
5341 // Roll forward from the forking point to the new tip.
5342 int nForkHeight = pindexFork ? pindexFork->nHeight : 0;
5343 for (int nHeight = nForkHeight + 1; nHeight <= pindexNew->nHeight;
5344 ++nHeight) {
5345 const CBlockIndex &pindex{*Assert(pindexNew->GetAncestor(nHeight))};
5346 LogPrintf("Rolling forward %s (%i)\n", pindex.GetBlockHash().ToString(),
5347 nHeight);
5349 _("Replaying blocks…"),
5350 (int)((nHeight - nForkHeight) * 100.0 /
5351 (pindexNew->nHeight - nForkHeight)),
5352 false);
5353 if (!RollforwardBlock(&pindex, cache)) {
5354 return false;
5355 }
5356 }
5357
5358 cache.SetBestBlock(pindexNew->GetBlockHash());
5359 cache.Flush();
5361 return true;
5362}
5363
5364// May NOT be used after any connections are up as much of the peer-processing
5365// logic assumes a consistent block index state
5368 nBlockSequenceId = 1;
5369 m_best_fork_tip = nullptr;
5370 m_best_fork_base = nullptr;
5372}
5373
5376
5378 return false;
5379 }
5380
5381 // Dump enough headers for RTT computation, with a few extras in case a
5382 // reorg occurs.
5383 const uint64_t numHeaders{20};
5384
5385 try {
5386 const fs::path filePathTmp = filePath + ".new";
5388 if (!filestr) {
5389 return false;
5390 }
5391
5393 file << HEADERS_TIME_VERSION;
5394 file << numHeaders;
5395
5396 const CBlockIndex *index = ActiveTip();
5397 bool missingIndex{false};
5398 for (uint64_t i = 0; i < numHeaders; i++) {
5399 if (!index) {
5400 LogPrintf("Missing block index, stopping the headers time "
5401 "dumping after %d blocks.\n",
5402 i);
5403 missingIndex = true;
5404 break;
5405 }
5406
5407 file << index->GetBlockHash();
5408 file << index->GetHeaderReceivedTime();
5409
5410 index = index->pprev;
5411 }
5412
5413 if (!FileCommit(file.Get())) {
5414 throw std::runtime_error(strprintf("Failed to commit to file %s",
5415 PathToString(filePathTmp)));
5416 }
5417 file.fclose();
5418
5419 if (missingIndex) {
5420 fs::remove(filePathTmp);
5421 return false;
5422 }
5423
5425 throw std::runtime_error(strprintf("Rename failed from %s to %s",
5426 PathToString(filePathTmp),
5427 PathToString(filePath)));
5428 }
5429 } catch (const std::exception &e) {
5430 LogPrintf("Failed to dump the headers time: %s.\n", e.what());
5431 return false;
5432 }
5433
5434 LogPrintf("Successfully dumped the last %d headers time to %s.\n",
5435 numHeaders, PathToString(filePath));
5436
5437 return true;
5438}
5439
5442
5444 return false;
5445 }
5446
5449 if (file.IsNull()) {
5450 LogPrintf("Failed to open header times from disk, skipping.\n");
5451 return false;
5452 }
5453
5454 try {
5455 uint64_t version;
5456 file >> version;
5457
5458 if (version != HEADERS_TIME_VERSION) {
5459 LogPrintf("Unsupported header times file version, skipping.\n");
5460 return false;
5461 }
5462
5464 file >> numBlocks;
5465
5466 for (uint64_t i = 0; i < numBlocks; i++) {
5467 BlockHash blockHash;
5469
5470 file >> blockHash;
5471 file >> receiveTime;
5472
5473 CBlockIndex *index = m_blockman.LookupBlockIndex(blockHash);
5474 if (!index) {
5475 LogPrintf("Missing index for block %s, stopping the headers "
5476 "time loading after %d blocks.\n",
5477 blockHash.ToString(), i);
5478 return false;
5479 }
5480
5481 index->nTimeReceived = receiveTime;
5482 }
5483 } catch (const std::exception &e) {
5484 LogPrintf("Failed to read the headers time file data on disk: %s.\n",
5485 e.what());
5486 return false;
5487 }
5488
5489 return true;
5490}
5491
5494 // Load block index from databases
5495 bool needs_init = fReindex;
5496 if (!fReindex) {
5497 bool ret = m_blockman.LoadBlockIndexDB();
5498 if (!ret) {
5499 return false;
5500 }
5501
5502 m_blockman.ScanAndUnlinkAlreadyPrunedFiles();
5503
5504 std::vector<CBlockIndex *> vSortedByHeight{
5505 m_blockman.GetAllBlockIndices()};
5506 std::sort(vSortedByHeight.begin(), vSortedByHeight.end(),
5508
5509 // Find start of assumed-valid region.
5510 int first_assumed_valid_height = std::numeric_limits<int>::max();
5511
5512 for (const CBlockIndex *block : vSortedByHeight) {
5513 if (block->IsAssumedValid()) {
5514 auto chainstates = GetAll();
5515
5516 // If we encounter an assumed-valid block index entry, ensure
5517 // that we have one chainstate that tolerates assumed-valid
5518 // entries and another that does not (i.e. the background
5519 // validation chainstate), since assumed-valid entries should
5520 // always be pending validation by a fully-validated chainstate.
5521 auto any_chain = [&](auto fnc) {
5522 return std::any_of(chainstates.cbegin(), chainstates.cend(),
5523 fnc);
5524 };
5525 assert(any_chain([](auto chainstate) {
5526 return chainstate->reliesOnAssumedValid();
5527 }));
5528 assert(any_chain([](auto chainstate) {
5529 return !chainstate->reliesOnAssumedValid();
5530 }));
5531
5532 first_assumed_valid_height = block->nHeight;
5533 LogPrintf("Saw first assumedvalid block at height %d (%s)\n",
5535 break;
5536 }
5537 }
5538
5539 for (CBlockIndex *pindex : vSortedByHeight) {
5540 if (ShutdownRequested()) {
5541 return false;
5542 }
5543 if (pindex->IsAssumedValid() ||
5545 (pindex->HaveTxsDownloaded() || pindex->pprev == nullptr))) {
5546 // Fill each chainstate's block candidate set. Only add
5547 // assumed-valid blocks to the tip candidate set if the
5548 // chainstate is allowed to rely on assumed-valid blocks.
5549 //
5550 // If all setBlockIndexCandidates contained the assumed-valid
5551 // blocks, the background chainstate's ActivateBestChain() call
5552 // would add assumed-valid blocks to the chain (based on how
5553 // FindMostWorkChain() works). Obviously we don't want this
5554 // since the purpose of the background validation chain is to
5555 // validate assumed-valid blocks.
5556 //
5557 // Note: This is considering all blocks whose height is greater
5558 // or equal to the first assumed-valid block to be assumed-valid
5559 // blocks, and excluding them from the background chainstate's
5560 // setBlockIndexCandidates set. This does mean that some blocks
5561 // which are not technically assumed-valid (later blocks on a
5562 // fork beginning before the first assumed-valid block) might
5563 // not get added to the background chainstate, but this is ok,
5564 // because they will still be attached to the active chainstate
5565 // if they actually contain more work.
5566 //
5567 // Instead of this height-based approach, an earlier attempt was
5568 // made at detecting "holistically" whether the block index
5569 // under consideration relied on an assumed-valid ancestor, but
5570 // this proved to be too slow to be practical.
5571 for (Chainstate *chainstate : GetAll()) {
5572 if (chainstate->reliesOnAssumedValid() ||
5574 chainstate->setBlockIndexCandidates.insert(pindex);
5575 }
5576 }
5577 }
5578
5579 if (pindex->nStatus.isInvalid() &&
5580 (!m_best_invalid ||
5581 pindex->nChainWork > m_best_invalid->nChainWork)) {
5582 m_best_invalid = pindex;
5583 }
5584
5585 if (pindex->nStatus.isOnParkedChain() &&
5586 (!m_best_parked ||
5587 pindex->nChainWork > m_best_parked->nChainWork)) {
5588 m_best_parked = pindex;
5589 }
5590
5591 if (pindex->IsValid(BlockValidity::TREE) &&
5592 (m_best_header == nullptr ||
5594 m_best_header = pindex;
5595 }
5596 }
5597
5598 needs_init = m_blockman.m_block_index.empty();
5599 }
5600
5601 if (needs_init) {
5602 // Everything here is for *new* reindex/DBs. Thus, though
5603 // LoadBlockIndexDB may have set fReindex if we shut down
5604 // mid-reindex previously, we don't check fReindex and
5605 // instead only check it prior to LoadBlockIndexDB to set
5606 // needs_init.
5607
5608 LogPrintf("Initializing databases...\n");
5609 }
5610 return true;
5611}
5612
5614 LOCK(cs_main);
5615
5616 const CChainParams &params{m_chainman.GetParams()};
5617
5618 // Check whether we're already initialized by checking for genesis in
5619 // m_blockman.m_block_index. Note that we can't use m_chain here, since it
5620 // is set based on the coins db, not the block index db, which is the only
5621 // thing loaded at this point.
5622 if (m_blockman.m_block_index.count(params.GenesisBlock().GetHash())) {
5623 return true;
5624 }
5625
5626 try {
5627 const CBlock &block = params.GenesisBlock();
5629 m_blockman.SaveBlockToDisk(block, 0, m_chain, nullptr)};
5630 if (blockPos.IsNull()) {
5631 return error("%s: writing genesis block to disk failed", __func__);
5632 }
5633 CBlockIndex *pindex =
5634 m_blockman.AddToBlockIndex(block, m_chainman.m_best_header);
5635 ReceivedBlockTransactions(block, pindex, blockPos);
5636 } catch (const std::runtime_error &e) {
5637 return error("%s: failed to write genesis block: %s", __func__,
5638 e.what());
5639 }
5640
5641 return true;
5642}
5643
5646 std::multimap<BlockHash, FlatFilePos> *blocks_with_unknown_parent,
5649
5650 // Either both should be specified (-reindex), or neither (-loadblock).
5652
5654 const CChainParams &params{m_chainman.GetParams()};
5655
5656 int nLoaded = 0;
5657 try {
5658 // This takes over fileIn and calls fclose() on it in the CBufferedFile
5659 // destructor. Make sure we have at least 2*MAX_TX_SIZE space in there
5660 // so any transaction can fit in the buffer.
5663 // nRewind indicates where to resume scanning in case something goes
5664 // wrong, such as a block fails to deserialize.
5665 uint64_t nRewind = blkdat.GetPos();
5666 while (!blkdat.eof()) {
5667 if (ShutdownRequested()) {
5668 return;
5669 }
5670
5671 blkdat.SetPos(nRewind);
5672 // Start one byte further next time, in case of failure.
5673 nRewind++;
5674 // Remove former limit.
5675 blkdat.SetLimit();
5676 unsigned int nSize = 0;
5677 try {
5678 // Locate a header.
5680 blkdat.FindByte(std::byte(params.DiskMagic()[0]));
5681 nRewind = blkdat.GetPos() + 1;
5682 blkdat >> buf;
5683 if (memcmp(buf, params.DiskMagic().data(),
5685 continue;
5686 }
5687
5688 // Read size.
5689 blkdat >> nSize;
5690 if (nSize < 80) {
5691 continue;
5692 }
5693 } catch (const std::exception &) {
5694 // No valid block header found; don't complain.
5695 // (this happens at the end of every blk.dat file)
5696 break;
5697 }
5698
5699 try {
5700 // read block header
5701 const uint64_t nBlockPos{blkdat.GetPos()};
5702 if (dbp) {
5703 dbp->nPos = nBlockPos;
5704 }
5705 blkdat.SetLimit(nBlockPos + nSize);
5706 CBlockHeader header;
5707 blkdat >> header;
5708 const BlockHash hash{header.GetHash()};
5709 // Skip the rest of this block (this may read from disk
5710 // into memory); position to the marker before the next block,
5711 // but it's still possible to rewind to the start of the
5712 // current block (without a disk read).
5713 nRewind = nBlockPos + nSize;
5714 blkdat.SkipTo(nRewind);
5715
5716 // needs to remain available after the cs_main lock is released
5717 // to avoid duplicate reads from disk
5718 std::shared_ptr<CBlock> pblock{};
5719
5720 {
5721 LOCK(cs_main);
5722 // detect out of order blocks, and store them for later
5723 if (hash != params.GetConsensus().hashGenesisBlock &&
5725 LogPrint(
5727 "%s: Out of order block %s, parent %s not known\n",
5728 __func__, hash.ToString(),
5729 header.hashPrevBlock.ToString());
5732 header.hashPrevBlock, *dbp);
5733 }
5734 continue;
5735 }
5736
5737 // process in case the block isn't known yet
5738 const CBlockIndex *pindex =
5740 if (!pindex || !pindex->nStatus.hasData()) {
5741 // This block can be processed immediately; rewind to
5742 // its start, read and deserialize it.
5743 blkdat.SetPos(nBlockPos);
5744 pblock = std::make_shared<CBlock>();
5745 blkdat >> *pblock;
5746 nRewind = blkdat.GetPos();
5747
5749 if (AcceptBlock(pblock, state, true, dbp, nullptr,
5750 true)) {
5751 nLoaded++;
5752 }
5753 if (state.IsError()) {
5754 break;
5755 }
5756 } else if (hash != params.GetConsensus().hashGenesisBlock &&
5757 pindex->nHeight % 1000 == 0) {
5758 LogPrint(
5760 "Block Import: already had block %s at height %d\n",
5761 hash.ToString(), pindex->nHeight);
5762 }
5763 }
5764
5765 // Activate the genesis block so normal node progress can
5766 // continue
5767 if (hash == params.GetConsensus().hashGenesisBlock) {
5769 if (!ActivateBestChain(state, nullptr, avalanche)) {
5770 break;
5771 }
5772 }
5773
5774 if (m_blockman.IsPruneMode() && !fReindex && pblock) {
5775 // Must update the tip for pruning to work while importing
5776 // with -loadblock. This is a tradeoff to conserve disk
5777 // space at the expense of time spent updating the tip to be
5778 // able to prune. Otherwise, ActivateBestChain won't be
5779 // called by the import process until after all of the block
5780 // files are loaded. ActivateBestChain can be called by
5781 // concurrent network message processing, but that is not
5782 // reliable for the purpose of pruning while importing.
5784 if (!ActivateBestChain(state, pblock, avalanche)) {
5786 "failed to activate chain (%s)\n",
5787 state.ToString());
5788 break;
5789 }
5790 }
5791
5792 NotifyHeaderTip(*this);
5793
5795 continue;
5796 }
5797
5798 // Recursively process earlier encountered successors of this
5799 // block
5800 std::deque<BlockHash> queue;
5801 queue.push_back(hash);
5802 while (!queue.empty()) {
5803 BlockHash head = queue.front();
5804 queue.pop_front();
5805 auto range = blocks_with_unknown_parent->equal_range(head);
5806 while (range.first != range.second) {
5807 std::multimap<BlockHash, FlatFilePos>::iterator it =
5808 range.first;
5809 std::shared_ptr<CBlock> pblockrecursive =
5810 std::make_shared<CBlock>();
5812 it->second)) {
5813 LogPrint(
5815 "%s: Processing out of order child %s of %s\n",
5816 __func__, pblockrecursive->GetHash().ToString(),
5817 head.ToString());
5818 LOCK(cs_main);
5820 if (AcceptBlock(pblockrecursive, dummy, true,
5821 &it->second, nullptr, true)) {
5822 nLoaded++;
5823 queue.push_back(pblockrecursive->GetHash());
5824 }
5825 }
5826 range.first++;
5827 blocks_with_unknown_parent->erase(it);
5828 NotifyHeaderTip(*this);
5829 }
5830 }
5831 } catch (const std::exception &e) {
5832 // Historical bugs added extra data to the block files that does
5833 // not deserialize cleanly. Commonly this data is between
5834 // readable blocks, but it does not really matter. Such data is
5835 // not fatal to the import process. The code that reads the
5836 // block files deals with invalid data by simply ignoring it. It
5837 // continues to search for the next {4 byte magic message start
5838 // bytes + 4 byte length + block} that does deserialize cleanly
5839 // and passes all of the other block validation checks dealing
5840 // with POW and the merkle root, etc... We merely note with this
5841 // informational log message when unexpected data is
5842 // encountered. We could also be experiencing a storage system
5843 // read error, or a read of a previous bad write. These are
5844 // possible, but less likely scenarios. We don't have enough
5845 // information to tell a difference here. The reindex process is
5846 // not the place to attempt to clean and/or compact the block
5847 // files. If so desired, a studious node operator may use
5848 // knowledge of the fact that the block files are not entirely
5849 // pristine in order to prepare a set of pristine, and perhaps
5850 // ordered, block files for later reindexing.
5852 "%s: unexpected data at file offset 0x%x - %s. "
5853 "continuing\n",
5854 __func__, (nRewind - 1), e.what());
5855 }
5856 }
5857 } catch (const std::runtime_error &e) {
5858 AbortNode(std::string("System error: ") + e.what());
5859 }
5860
5861 LogPrintf("Loaded %i blocks from external file in %dms\n", nLoaded,
5862 GetTimeMillis() - nStart);
5863}
5864
5867 return;
5868 }
5869
5870 LOCK(cs_main);
5871
5872 // During a reindex, we read the genesis block and call CheckBlockIndex
5873 // before ActivateBestChain, so we have the genesis block in
5874 // m_blockman.m_block_index but no active chain. (A few of the tests when
5875 // iterating the block tree require that m_chain has been initialized.)
5876 if (m_chain.Height() < 0) {
5877 assert(m_blockman.m_block_index.size() <= 1);
5878 return;
5879 }
5880
5881 // Build forward-pointing map of the entire block tree.
5882 std::multimap<CBlockIndex *, CBlockIndex *> forward;
5883 for (auto &[_, block_index] : m_blockman.m_block_index) {
5884 forward.emplace(block_index.pprev, &block_index);
5885 }
5886
5887 assert(forward.size() == m_blockman.m_block_index.size());
5888
5889 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
5890 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
5891 rangeGenesis = forward.equal_range(nullptr);
5892 CBlockIndex *pindex = rangeGenesis.first->second;
5893 rangeGenesis.first++;
5894 // There is only one index entry with parent nullptr.
5895 assert(rangeGenesis.first == rangeGenesis.second);
5896
5897 // Iterate over the entire block tree, using depth-first search.
5898 // Along the way, remember whether there are blocks on the path from genesis
5899 // block being explored which are the first to have certain properties.
5900 size_t nNodes = 0;
5901 int nHeight = 0;
5902 // Oldest ancestor of pindex which is invalid.
5904 // Oldest ancestor of pindex which is parked.
5905 CBlockIndex *pindexFirstParked = nullptr;
5906 // Oldest ancestor of pindex which does not have data available.
5908 // Oldest ancestor of pindex for which nTx == 0.
5910 // Oldest ancestor of pindex which does not have BLOCK_VALID_TREE
5911 // (regardless of being valid or not).
5913 // Oldest ancestor of pindex which does not have BLOCK_VALID_TRANSACTIONS
5914 // (regardless of being valid or not).
5916 // Oldest ancestor of pindex which does not have BLOCK_VALID_CHAIN
5917 // (regardless of being valid or not).
5919 // Oldest ancestor of pindex which does not have BLOCK_VALID_SCRIPTS
5920 // (regardless of being valid or not).
5922 while (pindex != nullptr) {
5923 nNodes++;
5924 if (pindexFirstInvalid == nullptr && pindex->nStatus.hasFailed()) {
5925 pindexFirstInvalid = pindex;
5926 }
5927 if (pindexFirstParked == nullptr && pindex->nStatus.isParked()) {
5928 pindexFirstParked = pindex;
5929 }
5930 // Assumed-valid index entries will not have data since we haven't
5931 // downloaded the full block yet.
5932 if (pindexFirstMissing == nullptr && !pindex->nStatus.hasData() &&
5933 !pindex->IsAssumedValid()) {
5934 pindexFirstMissing = pindex;
5935 }
5936 if (pindexFirstNeverProcessed == nullptr && pindex->nTx == 0) {
5938 }
5939 if (pindex->pprev != nullptr && pindexFirstNotTreeValid == nullptr &&
5940 pindex->nStatus.getValidity() < BlockValidity::TREE) {
5941 pindexFirstNotTreeValid = pindex;
5942 }
5943 if (pindex->pprev != nullptr && !pindex->IsAssumedValid()) {
5944 if (pindexFirstNotTransactionsValid == nullptr &&
5945 pindex->nStatus.getValidity() < BlockValidity::TRANSACTIONS) {
5947 }
5948 if (pindexFirstNotChainValid == nullptr &&
5949 pindex->nStatus.getValidity() < BlockValidity::CHAIN) {
5950 pindexFirstNotChainValid = pindex;
5951 }
5952 if (pindexFirstNotScriptsValid == nullptr &&
5953 pindex->nStatus.getValidity() < BlockValidity::SCRIPTS) {
5955 }
5956 }
5957
5958 // Begin: actual consistency checks.
5959 if (pindex->pprev == nullptr) {
5960 // Genesis block checks.
5961 // Genesis block's hash must match.
5962 assert(pindex->GetBlockHash() ==
5964 // The current active chain's genesis block must be this block.
5965 assert(pindex == m_chain.Genesis());
5966 }
5967 if (!pindex->HaveTxsDownloaded()) {
5968 // nSequenceId can't be set positive for blocks that aren't linked
5969 // (negative is used for preciousblock)
5970 assert(pindex->nSequenceId <= 0);
5971 }
5972 // VALID_TRANSACTIONS is equivalent to nTx > 0 for all nodes (whether or
5973 // not pruning has occurred). HAVE_DATA is only equivalent to nTx > 0
5974 // (or VALID_TRANSACTIONS) if no pruning has occurred.
5975 // Unless these indexes are assumed valid and pending block download on
5976 // a background chainstate.
5977 if (!m_blockman.m_have_pruned && !pindex->IsAssumedValid()) {
5978 // If we've never pruned, then HAVE_DATA should be equivalent to nTx
5979 // > 0
5980 assert(pindex->nStatus.hasData() == (pindex->nTx > 0));
5982 } else if (pindex->nStatus.hasData()) {
5983 // If we have pruned, then we can only say that HAVE_DATA implies
5984 // nTx > 0
5985 assert(pindex->nTx > 0);
5986 }
5987 if (pindex->nStatus.hasUndo()) {
5988 assert(pindex->nStatus.hasData());
5989 }
5990 if (pindex->IsAssumedValid()) {
5991 // Assumed-valid blocks should have some nTx value.
5992 assert(pindex->nTx > 0);
5993 // Assumed-valid blocks should connect to the main chain.
5994 assert(pindex->nStatus.getValidity() >= BlockValidity::TREE);
5995 } else {
5996 // Otherwise there should only be an nTx value if we have
5997 // actually seen a block's transactions.
5998 // This is pruning-independent.
5999 assert((pindex->nStatus.getValidity() >=
6000 BlockValidity::TRANSACTIONS) == (pindex->nTx > 0));
6001 }
6002 // All parents having had data (at some point) is equivalent to all
6003 // parents being VALID_TRANSACTIONS, which is equivalent to
6004 // HaveTxsDownloaded(). All parents having had data (at some point) is
6005 // equivalent to all parents being VALID_TRANSACTIONS, which is
6006 // equivalent to HaveTxsDownloaded().
6007 assert((pindexFirstNeverProcessed == nullptr) ==
6008 (pindex->HaveTxsDownloaded()));
6010 (pindex->HaveTxsDownloaded()));
6011 // nHeight must be consistent.
6012 assert(pindex->nHeight == nHeight);
6013 // For every block except the genesis block, the chainwork must be
6014 // larger than the parent's.
6015 assert(pindex->pprev == nullptr ||
6016 pindex->nChainWork >= pindex->pprev->nChainWork);
6017 // The pskip pointer must point back for all but the first 2 blocks.
6018 assert(nHeight < 2 ||
6019 (pindex->pskip && (pindex->pskip->nHeight < nHeight)));
6020 // All m_blockman.m_block_index entries must at least be TREE valid
6021 assert(pindexFirstNotTreeValid == nullptr);
6022 if (pindex->nStatus.getValidity() >= BlockValidity::TREE) {
6023 // TREE valid implies all parents are TREE valid
6024 assert(pindexFirstNotTreeValid == nullptr);
6025 }
6026 if (pindex->nStatus.getValidity() >= BlockValidity::CHAIN) {
6027 // CHAIN valid implies all parents are CHAIN valid
6028 assert(pindexFirstNotChainValid == nullptr);
6029 }
6030 if (pindex->nStatus.getValidity() >= BlockValidity::SCRIPTS) {
6031 // SCRIPTS valid implies all parents are SCRIPTS valid
6033 }
6034 if (pindexFirstInvalid == nullptr) {
6035 // Checks for not-invalid blocks.
6036 // The failed mask cannot be set for blocks without invalid parents.
6037 assert(!pindex->nStatus.isInvalid());
6038 }
6039 if (pindexFirstParked == nullptr) {
6040 // Checks for not-parked blocks.
6041 // The parked mask cannot be set for blocks without parked parents.
6042 // (i.e., hasParkedParent only if an ancestor is properly parked).
6043 assert(!pindex->nStatus.isOnParkedChain());
6044 }
6045 if (!CBlockIndexWorkComparator()(pindex, m_chain.Tip()) &&
6046 pindexFirstNeverProcessed == nullptr) {
6047 if (pindexFirstInvalid == nullptr) {
6048 // Don't perform this check for the background chainstate since
6049 // its setBlockIndexCandidates shouldn't have some entries (i.e.
6050 // those past the snapshot block) which do exist in the block
6051 // index for the active chainstate.
6052 if (this == &m_chainman.ActiveChainstate()) {
6053 // If this block sorts at least as good as the current tip
6054 // and is valid and we have all data for its parents, it
6055 // must be in setBlockIndexCandidates or be parked.
6056 if (pindexFirstMissing == nullptr) {
6057 assert(pindex->nStatus.isOnParkedChain() ||
6058 setBlockIndexCandidates.count(pindex));
6059 }
6060 // m_chain.Tip() must also be there even if some data has
6061 // been pruned.
6062 if (pindex == m_chain.Tip()) {
6063 assert(setBlockIndexCandidates.count(pindex));
6064 }
6065 }
6066 // If some parent is missing, then it could be that this block
6067 // was in setBlockIndexCandidates but had to be removed because
6068 // of the missing data. In this case it must be in
6069 // m_blocks_unlinked -- see test below.
6070 }
6071 } else {
6072 // If this block sorts worse than the current tip or some ancestor's
6073 // block has never been seen, it cannot be in
6074 // setBlockIndexCandidates.
6075 assert(setBlockIndexCandidates.count(pindex) == 0);
6076 }
6077 // Check whether this block is in m_blocks_unlinked.
6078 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
6079 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
6081 m_blockman.m_blocks_unlinked.equal_range(pindex->pprev);
6082 bool foundInUnlinked = false;
6083 while (rangeUnlinked.first != rangeUnlinked.second) {
6084 assert(rangeUnlinked.first->first == pindex->pprev);
6085 if (rangeUnlinked.first->second == pindex) {
6086 foundInUnlinked = true;
6087 break;
6088 }
6089 rangeUnlinked.first++;
6090 }
6091 if (pindex->pprev && pindex->nStatus.hasData() &&
6092 pindexFirstNeverProcessed != nullptr &&
6093 pindexFirstInvalid == nullptr) {
6094 // If this block has block data available, some parent was never
6095 // received, and has no invalid parents, it must be in
6096 // m_blocks_unlinked.
6098 }
6099 if (!pindex->nStatus.hasData()) {
6100 // Can't be in m_blocks_unlinked if we don't HAVE_DATA
6102 }
6103 if (pindexFirstMissing == nullptr) {
6104 // We aren't missing data for any parent -- cannot be in
6105 // m_blocks_unlinked.
6107 }
6108 if (pindex->pprev && pindex->nStatus.hasData() &&
6109 pindexFirstNeverProcessed == nullptr &&
6110 pindexFirstMissing != nullptr) {
6111 // We HAVE_DATA for this block, have received data for all parents
6112 // at some point, but we're currently missing data for some parent.
6113 // We must have pruned.
6115 // This block may have entered m_blocks_unlinked if:
6116 // - it has a descendant that at some point had more work than the
6117 // tip, and
6118 // - we tried switching to that descendant but were missing
6119 // data for some intermediate block between m_chain and the
6120 // tip.
6121 // So if this block is itself better than m_chain.Tip() and it
6122 // wasn't in
6123 // setBlockIndexCandidates, then it must be in m_blocks_unlinked.
6124 if (!CBlockIndexWorkComparator()(pindex, m_chain.Tip()) &&
6125 setBlockIndexCandidates.count(pindex) == 0) {
6126 if (pindexFirstInvalid == nullptr) {
6128 }
6129 }
6130 }
6131 // Perhaps too slow
6132 // assert(pindex->GetBlockHash() == pindex->GetBlockHeader().GetHash());
6133 // End: actual consistency checks.
6134
6135 // Try descending into the first subnode.
6136 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
6137 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
6138 range = forward.equal_range(pindex);
6139 if (range.first != range.second) {
6140 // A subnode was found.
6141 pindex = range.first->second;
6142 nHeight++;
6143 continue;
6144 }
6145 // This is a leaf node. Move upwards until we reach a node of which we
6146 // have not yet visited the last child.
6147 while (pindex) {
6148 // We are going to either move to a parent or a sibling of pindex.
6149 // If pindex was the first with a certain property, unset the
6150 // corresponding variable.
6151 if (pindex == pindexFirstInvalid) {
6152 pindexFirstInvalid = nullptr;
6153 }
6154 if (pindex == pindexFirstParked) {
6155 pindexFirstParked = nullptr;
6156 }
6157 if (pindex == pindexFirstMissing) {
6158 pindexFirstMissing = nullptr;
6159 }
6160 if (pindex == pindexFirstNeverProcessed) {
6161 pindexFirstNeverProcessed = nullptr;
6162 }
6163 if (pindex == pindexFirstNotTreeValid) {
6164 pindexFirstNotTreeValid = nullptr;
6165 }
6166 if (pindex == pindexFirstNotTransactionsValid) {
6168 }
6169 if (pindex == pindexFirstNotChainValid) {
6170 pindexFirstNotChainValid = nullptr;
6171 }
6172 if (pindex == pindexFirstNotScriptsValid) {
6174 }
6175 // Find our parent.
6176 CBlockIndex *pindexPar = pindex->pprev;
6177 // Find which child we just visited.
6178 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
6179 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
6180 rangePar = forward.equal_range(pindexPar);
6181 while (rangePar.first->second != pindex) {
6182 // Our parent must have at least the node we're coming from as
6183 // child.
6184 assert(rangePar.first != rangePar.second);
6185 rangePar.first++;
6186 }
6187 // Proceed to the next one.
6188 rangePar.first++;
6189 if (rangePar.first != rangePar.second) {
6190 // Move to the sibling.
6191 pindex = rangePar.first->second;
6192 break;
6193 } else {
6194 // Move up further.
6195 pindex = pindexPar;
6196 nHeight--;
6197 continue;
6198 }
6199 }
6200 }
6201
6202 // Check that we actually traversed the entire map.
6203 assert(nNodes == forward.size());
6204}
6205
6206std::string Chainstate::ToString() {
6209 return strprintf("Chainstate [%s] @ height %d (%s)",
6210 m_from_snapshot_blockhash ? "snapshot" : "ibd",
6211 tip ? tip->nHeight : -1,
6212 tip ? tip->GetBlockHash().ToString() : "null");
6213}
6214
6215bool Chainstate::ResizeCoinsCaches(size_t coinstip_size, size_t coinsdb_size) {
6219 // Cache sizes are unchanged, no need to continue.
6220 return true;
6221 }
6226
6227 LogPrintf("[%s] resized coinsdb cache to %.1f MiB\n", this->ToString(),
6228 coinsdb_size * (1.0 / 1024 / 1024));
6229 LogPrintf("[%s] resized coinstip cache to %.1f MiB\n", this->ToString(),
6230 coinstip_size * (1.0 / 1024 / 1024));
6231
6233 bool ret;
6234
6236 // Likely no need to flush if cache sizes have grown.
6238 } else {
6239 // Otherwise, flush state to disk and deallocate the in-memory coins
6240 // map.
6242 }
6243 return ret;
6244}
6245
6251 const CBlockIndex *pindex) {
6252 if (pindex == nullptr) {
6253 return 0.0;
6254 }
6255
6256 int64_t nNow = time(nullptr);
6257
6258 double fTxTotal;
6259 if (pindex->GetChainTxCount() <= data.nTxCount) {
6260 fTxTotal = data.nTxCount + (nNow - data.nTime) * data.dTxRate;
6261 } else {
6262 fTxTotal = pindex->GetChainTxCount() +
6263 (nNow - pindex->GetBlockTime()) * data.dTxRate;
6264 }
6265
6266 return std::min<double>(pindex->GetChainTxCount() / fTxTotal, 1.0);
6267}
6268
6269std::optional<BlockHash> ChainstateManager::SnapshotBlockhash() const {
6270 LOCK(::cs_main);
6271 if (m_active_chainstate && m_active_chainstate->m_from_snapshot_blockhash) {
6272 // If a snapshot chainstate exists, it will always be our active.
6273 return m_active_chainstate->m_from_snapshot_blockhash;
6274 }
6275 return std::nullopt;
6276}
6277
6278std::vector<Chainstate *> ChainstateManager::GetAll() {
6279 LOCK(::cs_main);
6280 std::vector<Chainstate *> out;
6281
6282 for (Chainstate *pchainstate :
6283 {m_ibd_chainstate.get(), m_snapshot_chainstate.get()}) {
6284 if (this->IsUsable(pchainstate)) {
6285 out.push_back(pchainstate);
6286 }
6287 }
6288
6289 return out;
6290}
6291
6292Chainstate &ChainstateManager::InitializeChainstate(CTxMemPool *mempool) {
6295 assert(!m_active_chainstate);
6296
6297 m_ibd_chainstate = std::make_unique<Chainstate>(mempool, m_blockman, *this);
6298 m_active_chainstate = m_ibd_chainstate.get();
6299 return *m_active_chainstate;
6300}
6301
6302const AssumeutxoData *ExpectedAssumeutxo(const int height,
6303 const CChainParams &chainparams) {
6304 const MapAssumeutxo &valid_assumeutxos_map = chainparams.Assumeutxo();
6305 const auto assumeutxo_found = valid_assumeutxos_map.find(height);
6306
6308 return &assumeutxo_found->second;
6309 }
6310 return nullptr;
6311}
6312
6316
6317 if (is_snapshot) {
6320
6321 try {
6322 const bool existed{fs::remove(base_blockhash_path)};
6323 if (!existed) {
6324 LogPrintf("[snapshot] snapshot chainstate dir being removed "
6325 "lacks %s file\n",
6327 }
6328 } catch (const fs::filesystem_error &e) {
6329 LogPrintf("[snapshot] failed to remove file %s: %s\n",
6332 }
6333 }
6334
6335 std::string path_str = fs::PathToString(db_path);
6336 LogPrintf("Removing leveldb dir at %s\n", path_str);
6337
6338 // We have to destruct before this call leveldb::DB in order to release the
6339 // db lock, otherwise `DestroyDB` will fail. See `leveldb::~DBImpl()`.
6340 const bool destroyed = dbwrapper::DestroyDB(path_str, {}).ok();
6341
6342 if (!destroyed) {
6343 LogPrintf("error: leveldb DestroyDB call failed on %s\n", path_str);
6344 }
6345
6346 // Datadir should be removed from filesystem; otherwise initialization may
6347 // detect it on subsequent statups and get confused.
6348 //
6349 // If the base_blockhash_path removal above fails in the case of snapshot
6350 // chainstates, this will return false since leveldb won't remove a
6351 // non-empty directory.
6352 return destroyed && !fs::exists(db_path);
6353}
6354
6357 bool in_memory) {
6358 BlockHash base_blockhash = metadata.m_base_blockhash;
6359
6360 if (this->SnapshotBlockhash()) {
6361 LogPrintf("[snapshot] can't activate a snapshot-based chainstate more "
6362 "than once\n");
6363 return false;
6364 }
6365
6368
6369 // Cache percentages to allocate to each chainstate.
6370 //
6371 // These particular percentages don't matter so much since they will only be
6372 // relevant during snapshot activation; caches are rebalanced at the
6373 // conclusion of this function. We want to give (essentially) all available
6374 // cache capacity to the snapshot to aid the bulk load later in this
6375 // function.
6376 static constexpr double IBD_CACHE_PERC = 0.01;
6377 static constexpr double SNAPSHOT_CACHE_PERC = 0.99;
6378
6379 {
6380 LOCK(::cs_main);
6381 // Resize the coins caches to ensure we're not exceeding memory limits.
6382 //
6383 // Allocate the majority of the cache to the incoming snapshot
6384 // chainstate, since (optimistically) getting to its tip will be the top
6385 // priority. We'll need to call `MaybeRebalanceCaches()` once we're done
6386 // with this function to ensure the right allocation (including the
6387 // possibility that no snapshot was activated and that we should restore
6388 // the active chainstate caches to their original size).
6389 //
6391 this->ActiveChainstate().m_coinsdb_cache_size_bytes;
6393 this->ActiveChainstate().m_coinstip_cache_size_bytes;
6394
6395 // Temporarily resize the active coins cache to make room for the
6396 // newly-created snapshot chain.
6397 this->ActiveChainstate().ResizeCoinsCaches(
6398 static_cast<size_t>(current_coinstip_cache_size * IBD_CACHE_PERC),
6399 static_cast<size_t>(current_coinsdb_cache_size * IBD_CACHE_PERC));
6400 }
6401
6402 auto snapshot_chainstate =
6403 WITH_LOCK(::cs_main, return std::make_unique<Chainstate>(
6404 /* mempool */ nullptr, m_blockman, *this,
6406
6407 {
6408 LOCK(::cs_main);
6409 snapshot_chainstate->InitCoinsDB(
6410 static_cast<size_t>(current_coinsdb_cache_size *
6412 in_memory, false, "chainstate");
6413 snapshot_chainstate->InitCoinsCache(static_cast<size_t>(
6415 }
6416
6418 coins_file, metadata);
6419
6420 // If not in-memory, persist the base blockhash for use during subsequent
6421 // initialization.
6422 if (!in_memory) {
6423 LOCK(::cs_main);
6425 snapshot_ok = false;
6426 }
6427 }
6428 if (!snapshot_ok) {
6429 LOCK(::cs_main);
6430 this->MaybeRebalanceCaches();
6431
6432 // PopulateAndValidateSnapshot can return (in error) before the leveldb
6433 // datadir has been created, so only attempt removal if we got that far.
6435 // We have to destruct leveldb::DB in order to release the db lock,
6436 // otherwise DestroyDB() (in DeleteCoinsDBFromDisk()) will fail. See
6437 // `leveldb::~DBImpl()`. Destructing the chainstate (and so
6438 // resetting the coinsviews object) does this.
6439 snapshot_chainstate.reset();
6440 bool removed =
6441 DeleteCoinsDBFromDisk(*snapshot_datadir, /*is_snapshot=*/true);
6442 if (!removed) {
6443 AbortNode(
6444 strprintf("Failed to remove snapshot chainstate dir (%s). "
6445 "Manually remove it before restarting.\n",
6447 }
6448 }
6449 return false;
6450 }
6451
6452 {
6453 LOCK(::cs_main);
6456 const bool chaintip_loaded = m_snapshot_chainstate->LoadChainTip();
6458
6459 m_active_chainstate = m_snapshot_chainstate.get();
6460
6461 LogPrintf("[snapshot] successfully activated snapshot %s\n",
6462 base_blockhash.ToString());
6463 LogPrintf("[snapshot] (%.2f MB)\n",
6464 m_snapshot_chainstate->CoinsTip().DynamicMemoryUsage() /
6465 (1000 * 1000));
6466
6467 this->MaybeRebalanceCaches();
6468 }
6469 return true;
6470}
6471
6473 bool snapshot_loaded) {
6475 strprintf("%s (%.2f MB)",
6476 snapshot_loaded ? "saving snapshot chainstate"
6477 : "flushing coins cache",
6478 coins_cache.DynamicMemoryUsage() / (1000 * 1000)),
6480
6481 coins_cache.Flush();
6482}
6483
6484struct StopHashingException : public std::exception {
6485 const char *what() const throw() override {
6486 return "ComputeUTXOStats interrupted by shutdown.";
6487 }
6488};
6489
6491 if (ShutdownRequested()) {
6492 throw StopHashingException();
6493 }
6494}
6495
6498 const SnapshotMetadata &metadata) {
6499 // It's okay to release cs_main before we're done using `coins_cache`
6500 // because we know that nothing else will be referencing the newly created
6501 // snapshot_chainstate yet.
6503 *WITH_LOCK(::cs_main, return &snapshot_chainstate.CoinsTip());
6504
6505 BlockHash base_blockhash = metadata.m_base_blockhash;
6506
6509
6510 if (!snapshot_start_block) {
6511 // Needed for ComputeUTXOStats and ExpectedAssumeutxo to determine the
6512 // height and to avoid a crash when base_blockhash.IsNull()
6513 LogPrintf("[snapshot] Did not find snapshot start blockheader %s\n",
6514 base_blockhash.ToString());
6515 return false;
6516 }
6517
6518 int base_height = snapshot_start_block->nHeight;
6520
6521 if (!maybe_au_data) {
6522 LogPrintf("[snapshot] assumeutxo height in snapshot metadata not "
6523 "recognized (%d) - refusing to load snapshot\n",
6524 base_height);
6525 return false;
6526 }
6527
6529
6530 COutPoint outpoint;
6531 Coin coin;
6532 const uint64_t coins_count = metadata.m_coins_count;
6533 uint64_t coins_left = metadata.m_coins_count;
6534
6535 LogPrintf("[snapshot] loading coins from snapshot %s\n",
6536 base_blockhash.ToString());
6538
6539 while (coins_left > 0) {
6540 try {
6541 coins_file >> outpoint;
6542 coins_file >> coin;
6543 } catch (const std::ios_base::failure &) {
6544 LogPrintf("[snapshot] bad snapshot format or truncated snapshot "
6545 "after deserializing %d coins\n",
6546 coins_count - coins_left);
6547 return false;
6548 }
6549 if (coin.GetHeight() > uint32_t(base_height) ||
6550 // Avoid integer wrap-around in coinstats.cpp:ApplyHash
6551 outpoint.GetN() >=
6552 std::numeric_limits<decltype(outpoint.GetN())>::max()) {
6553 LogPrintf(
6554 "[snapshot] bad snapshot data after deserializing %d coins\n",
6555 coins_count - coins_left);
6556 return false;
6557 }
6558 coins_cache.EmplaceCoinInternalDANGER(std::move(outpoint),
6559 std::move(coin));
6560
6561 --coins_left;
6563
6564 if (coins_processed % 1000000 == 0) {
6565 LogPrintf("[snapshot] %d coins loaded (%.2f%%, %.2f MB)\n",
6567 static_cast<float>(coins_processed) * 100 /
6568 static_cast<float>(coins_count),
6569 coins_cache.DynamicMemoryUsage() / (1000 * 1000));
6570 }
6571
6572 // Batch write and flush (if we need to) every so often.
6573 //
6574 // If our average Coin size is roughly 41 bytes, checking every 120,000
6575 // coins means <5MB of memory imprecision.
6576 if (coins_processed % 120000 == 0) {
6577 if (ShutdownRequested()) {
6578 return false;
6579 }
6580
6581 const auto snapshot_cache_state = WITH_LOCK(
6582 ::cs_main, return snapshot_chainstate.GetCoinsCacheSizeState());
6583
6585 // This is a hack - we don't know what the actual best block is,
6586 // but that doesn't matter for the purposes of flushing the
6587 // cache here. We'll set this to its correct value
6588 // (`base_blockhash`) below after the coins are loaded.
6589 coins_cache.SetBestBlock(BlockHash{GetRandHash()});
6590
6591 // No need to acquire cs_main since this chainstate isn't being
6592 // used yet.
6593 FlushSnapshotToDisk(coins_cache, /*snapshot_loaded=*/false);
6594 }
6595 }
6596 }
6597
6598 // Important that we set this. This and the coins_cache accesses above are
6599 // sort of a layer violation, but either we reach into the innards of
6600 // CCoinsViewCache here or we have to invert some of the Chainstate to
6601 // embed them in a snapshot-activation-specific CCoinsViewCache bulk load
6602 // method.
6603 coins_cache.SetBestBlock(base_blockhash);
6604
6605 bool out_of_coins{false};
6606 try {
6607 coins_file >> outpoint;
6608 } catch (const std::ios_base::failure &) {
6609 // We expect an exception since we should be out of coins.
6610 out_of_coins = true;
6611 }
6612 if (!out_of_coins) {
6613 LogPrintf("[snapshot] bad snapshot - coins left over after "
6614 "deserializing %d coins\n",
6615 coins_count);
6616 return false;
6617 }
6618
6619 LogPrintf("[snapshot] loaded %d (%.2f MB) coins from snapshot %s\n",
6620 coins_count, coins_cache.DynamicMemoryUsage() / (1000 * 1000),
6621 base_blockhash.ToString());
6622
6623 // No need to acquire cs_main since this chainstate isn't being used yet.
6624 FlushSnapshotToDisk(coins_cache, /*snapshot_loaded=*/true);
6625
6626 assert(coins_cache.GetBestBlock() == base_blockhash);
6627
6628 // As above, okay to immediately release cs_main here since no other context
6629 // knows about the snapshot_chainstate.
6631 WITH_LOCK(::cs_main, return &snapshot_chainstate.CoinsDB());
6632
6633 std::optional<CCoinsStats> maybe_stats;
6634
6635 try {
6636 maybe_stats = ComputeUTXOStats(CoinStatsHashType::HASH_SERIALIZED,
6639 } catch (StopHashingException const &) {
6640 return false;
6641 }
6642 if (!maybe_stats.has_value()) {
6643 LogPrintf("[snapshot] failed to generate coins stats\n");
6644 return false;
6645 }
6646
6647 // Assert that the deserialized chainstate contents match the expected
6648 // assumeutxo value.
6649 if (AssumeutxoHash{maybe_stats->hashSerialized} !=
6650 au_data.hash_serialized) {
6651 LogPrintf("[snapshot] bad snapshot content hash: expected %s, got %s\n",
6652 au_data.hash_serialized.ToString(),
6653 maybe_stats->hashSerialized.ToString());
6654 return false;
6655 }
6656
6658
6659 // The remainder of this function requires modifying data protected by
6660 // cs_main.
6661 LOCK(::cs_main);
6662
6663 // Fake various pieces of CBlockIndex state:
6664 CBlockIndex *index = nullptr;
6665
6666 // Don't make any modifications to the genesis block.
6667 // This is especially important because we don't want to erroneously
6668 // apply ASSUMED_VALID_FLAG to genesis, which would happen if we didn't
6669 // skip it here (since it apparently isn't BlockValidity::SCRIPTS).
6670 constexpr int AFTER_GENESIS_START{1};
6671
6672 for (int i = AFTER_GENESIS_START; i <= snapshot_chainstate.m_chain.Height();
6673 ++i) {
6674 index = snapshot_chainstate.m_chain[i];
6675
6676 // Fake nTx so that LoadBlockIndex() loads assumed-valid CBlockIndex
6677 // entries (among other things)
6678 if (!index->nTx) {
6679 index->nTx = 1;
6680 }
6681 // Fake nChainTx so that GuessVerificationProgress reports accurately
6682 index->nChainTx = index->pprev->nChainTx + index->nTx;
6683
6684 // Mark unvalidated block index entries beneath the snapshot base block
6685 // as assumed-valid.
6686 if (!index->IsValid(BlockValidity::SCRIPTS)) {
6687 // This flag will be removed once the block is fully validated by a
6688 // background chainstate.
6689 index->nStatus = index->nStatus.withAssumedValid();
6690 }
6691
6692 m_blockman.m_dirty_blockindex.insert(index);
6693 // Changes to the block index will be flushed to disk after this call
6694 // returns in `ActivateSnapshot()`, when `MaybeRebalanceCaches()` is
6695 // called, since we've added a snapshot chainstate and therefore will
6696 // have to downsize the IBD chainstate, which will result in a call to
6697 // `FlushStateToDisk(ALWAYS)`.
6698 }
6699
6700 assert(index);
6701 index->nChainTx = au_data.nChainTx;
6702 snapshot_chainstate.setBlockIndexCandidates.insert(snapshot_start_block);
6703
6704 LogPrintf("[snapshot] validated snapshot (%.2f MB)\n",
6705 coins_cache.DynamicMemoryUsage() / (1000 * 1000));
6706 return true;
6707}
6708
6709// Currently, this function holds cs_main for its duration, which could be for
6710// multiple minutes due to the ComputeUTXOStats call. This hold is necessary
6711// because we need to avoid advancing the background validation chainstate
6712// farther than the snapshot base block - and this function is also invoked
6713// from within ConnectTip, i.e. from within ActivateBestChain, so cs_main is
6714// held anyway.
6715//
6716// Eventually (TODO), we could somehow separate this function's runtime from
6717// maintenance of the active chain, but that will either require
6718//
6719// (i) setting `m_disabled` immediately and ensuring all chainstate accesses go
6720// through IsUsable() checks, or
6721//
6722// (ii) giving each chainstate its own lock instead of using cs_main for
6723// everything.
6724SnapshotCompletionResult ChainstateManager::MaybeCompleteSnapshotValidation(
6725 std::function<void(bilingual_str)> shutdown_fnc) {
6727 if (m_ibd_chainstate.get() == &this->ActiveChainstate() ||
6728 !this->IsUsable(m_snapshot_chainstate.get()) ||
6729 !this->IsUsable(m_ibd_chainstate.get()) ||
6730 !m_ibd_chainstate->m_chain.Tip()) {
6731 // Nothing to do - this function only applies to the background
6732 // validation chainstate.
6734 }
6735 const int snapshot_tip_height = this->ActiveHeight();
6737 const CBlockIndex &index_new = *Assert(m_ibd_chainstate->m_chain.Tip());
6738
6739 if (index_new.nHeight < snapshot_base_height) {
6740 // Background IBD not complete yet.
6742 }
6743
6746
6749 _("%s failed to validate the -assumeutxo snapshot state. "
6750 "This indicates a hardware problem, or a bug in the software, or "
6751 "a bad software modification that allowed an invalid snapshot to "
6752 "be loaded. As a result of this, the node will shut down and "
6753 "stop using any state that was built on the snapshot, resetting "
6754 "the chain height from %d to %d. On the next restart, the node "
6755 "will resume syncing from %d without using any snapshot data. "
6756 "Please report this incident to %s, including how you obtained "
6757 "the snapshot. The invalid snapshot chainstate will be left on "
6758 "disk in case it is helpful in diagnosing the issue that caused "
6759 "this error."),
6762
6763 LogPrintf("[snapshot] !!! %s\n", user_error.original);
6764 LogPrintf("[snapshot] deleting snapshot, reverting to validated chain, "
6765 "and stopping node\n");
6766
6767 m_active_chainstate = m_ibd_chainstate.get();
6768 m_snapshot_chainstate->m_disabled = true;
6769 assert(!this->IsUsable(m_snapshot_chainstate.get()));
6770 assert(this->IsUsable(m_ibd_chainstate.get()));
6771
6772 auto rename_result = m_snapshot_chainstate->InvalidateCoinsDBOnDisk();
6773 if (!rename_result) {
6774 user_error = strprintf(Untranslated("%s\n%s"), user_error,
6776 }
6777
6779 };
6780
6781 if (index_new.GetBlockHash() != snapshot_blockhash) {
6782 LogPrintf(
6783 "[snapshot] supposed base block %s does not match the "
6784 "snapshot base block %s (height %d). Snapshot is not valid.\n",
6785 index_new.ToString(), snapshot_blockhash.ToString(),
6789 }
6790
6792
6793 int curr_height = m_ibd_chainstate->m_chain.Height();
6794
6798 assert(this->GetAll().size() == 2);
6799
6801 m_ibd_chainstate->ForceFlushStateToDisk();
6802
6804 if (!maybe_au_data) {
6805 LogPrintf("[snapshot] assumeutxo data not found for height "
6806 "(%d) - refusing to validate snapshot\n",
6807 curr_height);
6810 }
6811
6813 std::optional<CCoinsStats> maybe_ibd_stats;
6814 LogPrintf(
6815 "[snapshot] computing UTXO stats for background chainstate to validate "
6816 "snapshot - this could take a few minutes\n");
6817 try {
6819 ComputeUTXOStats(CoinStatsHashType::HASH_SERIALIZED, &ibd_coins_db,
6821 } catch (StopHashingException const &) {
6823 }
6824
6825 if (!maybe_ibd_stats) {
6826 LogPrintf(
6827 "[snapshot] failed to generate stats for validation coins db\n");
6828 // While this isn't a problem with the snapshot per se, this condition
6829 // prevents us from validating the snapshot, so we should shut down and
6830 // let the user handle the issue manually.
6833 }
6834 const auto &ibd_stats = *maybe_ibd_stats;
6835
6836 // Compare the background validation chainstate's UTXO set hash against the
6837 // hard-coded assumeutxo hash we expect.
6838 //
6839 // TODO: For belt-and-suspenders, we could cache the UTXO set
6840 // hash for the snapshot when it's loaded in its chainstate's leveldb. We
6841 // could then reference that here for an additional check.
6842 if (AssumeutxoHash{ibd_stats.hashSerialized} != au_data.hash_serialized) {
6843 LogPrintf("[snapshot] hash mismatch: actual=%s, expected=%s\n",
6844 ibd_stats.hashSerialized.ToString(),
6845 au_data.hash_serialized.ToString());
6848 }
6849
6850 LogPrintf("[snapshot] snapshot beginning at %s has been fully validated\n",
6851 snapshot_blockhash.ToString());
6852
6853 m_ibd_chainstate->m_disabled = true;
6854 this->MaybeRebalanceCaches();
6855
6857}
6858
6860 LOCK(::cs_main);
6861 assert(m_active_chainstate);
6862 return *m_active_chainstate;
6863}
6864
6866 LOCK(::cs_main);
6867 return m_snapshot_chainstate &&
6868 m_active_chainstate == m_snapshot_chainstate.get();
6869}
6870void ChainstateManager::MaybeRebalanceCaches() {
6872 bool ibd_usable = this->IsUsable(m_ibd_chainstate.get());
6874 assert(ibd_usable || snapshot_usable);
6875
6876 if (ibd_usable && !snapshot_usable) {
6877 LogPrintf("[snapshot] allocating all cache to the IBD chainstate\n");
6878 // Allocate everything to the IBD chainstate.
6879 m_ibd_chainstate->ResizeCoinsCaches(m_total_coinstip_cache,
6881 } else if (snapshot_usable && !ibd_usable) {
6882 // If background validation has completed and snapshot is our active
6883 // chain...
6884 LogPrintf(
6885 "[snapshot] allocating all cache to the snapshot chainstate\n");
6886 // Allocate everything to the snapshot chainstate.
6889 } else if (ibd_usable && snapshot_usable) {
6890 // If both chainstates exist, determine who needs more cache based on
6891 // IBD status.
6892 //
6893 // Note: shrink caches first so that we don't inadvertently overwhelm
6894 // available memory.
6895 if (m_snapshot_chainstate->IsInitialBlockDownload()) {
6896 m_ibd_chainstate->ResizeCoinsCaches(m_total_coinstip_cache * 0.05,
6897 m_total_coinsdb_cache * 0.05);
6898 m_snapshot_chainstate->ResizeCoinsCaches(
6900 } else {
6901 m_snapshot_chainstate->ResizeCoinsCaches(
6903 m_ibd_chainstate->ResizeCoinsCaches(m_total_coinstip_cache * 0.95,
6904 m_total_coinsdb_cache * 0.95);
6905 }
6906 }
6907}
6908
6909void ChainstateManager::ResetChainstates() {
6910 m_ibd_chainstate.reset();
6911 m_snapshot_chainstate.reset();
6912 m_active_chainstate = nullptr;
6913}
6914
6921 if (!opts.check_block_index.has_value()) {
6922 opts.check_block_index =
6923 opts.config.GetChainParams().DefaultConsistencyChecks();
6924 }
6925
6926 if (!opts.minimum_chain_work.has_value()) {
6927 opts.minimum_chain_work = UintToArith256(
6928 opts.config.GetChainParams().GetConsensus().nMinimumChainWork);
6929 }
6930 if (!opts.assumed_valid_block.has_value()) {
6931 opts.assumed_valid_block =
6932 opts.config.GetChainParams().GetConsensus().defaultAssumeValid;
6933 }
6934 Assert(opts.adjusted_time_callback);
6935 return std::move(opts);
6936}
6937
6942
6943bool ChainstateManager::DetectSnapshotChainstate(CTxMemPool *mempool) {
6945 std::optional<fs::path> path = node::FindSnapshotChainstateDir();
6946 if (!path) {
6947 return false;
6948 }
6949 std::optional<BlockHash> base_blockhash =
6951 if (!base_blockhash) {
6952 return false;
6953 }
6954 LogPrintf("[snapshot] detected active snapshot chainstate (%s) - loading\n",
6955 fs::PathToString(*path));
6956
6958 return true;
6959}
6960
6961Chainstate &
6962ChainstateManager::ActivateExistingSnapshot(CTxMemPool *mempool,
6965 m_snapshot_chainstate = std::make_unique<Chainstate>(mempool, m_blockman,
6966 *this, base_blockhash);
6967 LogPrintf("[snapshot] switching active chainstate to %s\n",
6968 m_snapshot_chainstate->ToString());
6969 m_active_chainstate = m_snapshot_chainstate.get();
6970 return *m_snapshot_chainstate;
6971}
6972
6973util::Result<void> Chainstate::InvalidateCoinsDBOnDisk() {
6975 // Should never be called on a non-snapshot chainstate.
6977 auto storage_path_maybe = this->CoinsDB().StoragePath();
6978 // Should never be called with a non-existent storage path.
6981
6982 // Coins views no longer usable.
6983 m_coins_views.reset();
6984
6985 auto invalid_path = snapshot_datadir + "_INVALID";
6987 std::string target = fs::PathToString(invalid_path);
6988 LogPrintf("[snapshot] renaming snapshot datadir %s to %s\n", dbpath,
6989 target);
6990
6991 // The invalid snapshot datadir is simply moved and not deleted because we
6992 // may want to do forensics later during issue investigation. The user is
6993 // instructed accordingly in MaybeCompleteSnapshotValidation().
6994 try {
6995 fs::rename(snapshot_datadir, invalid_path);
6996 } catch (const fs::filesystem_error &e) {
6999
7000 LogPrintf("%s: error renaming file '%s' -> '%s': %s\n", __func__,
7001 src_str, dest_str, e.what());
7002 return util::Error{strprintf(_("Rename of '%s' -> '%s' failed. "
7003 "You should resolve this by manually "
7004 "moving or deleting the invalid "
7005 "snapshot directory %s, otherwise you "
7006 "will encounter the same error again "
7007 "on the next startup."),
7009 }
7010 return {};
7011}
7012
7013const CBlockIndex *ChainstateManager::GetSnapshotBaseBlock() const {
7014 const auto blockhash_op = this->SnapshotBlockhash();
7015 if (!blockhash_op) {
7016 return nullptr;
7017 }
7019}
7020
7021std::optional<int> ChainstateManager::GetSnapshotBaseHeight() const {
7022 const CBlockIndex *base = this->GetSnapshotBaseBlock();
7023 return base ? std::make_optional(base->nHeight) : std::nullopt;
7024}
7025
7026bool ChainstateManager::ValidatedSnapshotCleanup() {
7029 ::cs_main) -> std::optional<fs::path> {
7030 if (!(chainstate && chainstate->HasCoinsViews())) {
7031 return {};
7032 }
7033 return chainstate->CoinsDB().StoragePath();
7034 };
7035 std::optional<fs::path> ibd_chainstate_path_maybe =
7037 std::optional<fs::path> snapshot_chainstate_path_maybe =
7039
7040 if (!this->IsSnapshotValidated()) {
7041 // No need to clean up.
7042 return false;
7043 }
7044 // If either path doesn't exist, that means at least one of the chainstates
7045 // is in-memory, in which case we can't do on-disk cleanup. You'd better be
7046 // in a unittest!
7047 if (!ibd_chainstate_path_maybe || !snapshot_chainstate_path_maybe) {
7048 LogPrintf("[snapshot] snapshot chainstate cleanup cannot happen with "
7049 "in-memory chainstates. You are testing, right?\n");
7050 return false;
7051 }
7052
7055
7056 // Since we're going to be moving around the underlying leveldb filesystem
7057 // content for each chainstate, make sure that the chainstates (and their
7058 // constituent CoinsViews members) have been destructed first.
7059 //
7060 // The caller of this method will be responsible for reinitializing
7061 // chainstates if they want to continue operation.
7062 this->ResetChainstates();
7063
7064 // No chainstates should be considered usable.
7065 assert(this->GetAll().size() == 0);
7066
7067 LogPrintf("[snapshot] deleting background chainstate directory (now "
7068 "unnecessary) (%s)\n",
7070
7071 fs::path tmp_old{ibd_chainstate_path + "_todelete"};
7072
7074 const fs::filesystem_error &err) {
7075 LogPrintf("Error renaming file (%s): %s\n", fs::PathToString(p_old),
7076 err.what());
7078 "Rename of '%s' -> '%s' failed. "
7079 "Cannot clean up the background chainstate leveldb directory.",
7081 };
7082
7083 try {
7084 fs::rename(ibd_chainstate_path, tmp_old);
7085 } catch (const fs::filesystem_error &e) {
7087 throw;
7088 }
7089
7090 LogPrintf("[snapshot] moving snapshot chainstate (%s) to "
7091 "default chainstate directory (%s)\n",
7094
7095 try {
7097 } catch (const fs::filesystem_error &e) {
7099 throw;
7100 }
7101
7102 if (!DeleteCoinsDBFromDisk(tmp_old, /*is_snapshot=*/false)) {
7103 // No need to AbortNode because once the unneeded bg chainstate data is
7104 // moved, it will not interfere with subsequent initialization.
7105 LogPrintf("Deletion of %s failed. Please remove it manually, as the "
7106 "directory is now unnecessary.\n",
7108 } else {
7109 LogPrintf("[snapshot] deleted background chainstate directory (%s)\n",
7111 }
7112 return true;
7113}
bool IsDAAEnabled(const Consensus::Params &params, int nHeight)
bool IsUAHFenabled(const Consensus::Params &params, int nHeight)
static bool IsPhononEnabled(const Consensus::Params &params, int32_t nHeight)
static bool IsGravitonEnabled(const Consensus::Params &params, int32_t nHeight)
bool IsMagneticAnomalyEnabled(const Consensus::Params &params, int32_t nHeight)
Check if Nov 15, 2018 HF has activated using block height.
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:38
arith_uint256 UintToArith256(const uint256 &a)
int flags
@ 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:215
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
Definition args.cpp:526
fs::path GetBlocksDirPath() const
Get blocks directory path.
Definition args.cpp:289
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition args.cpp:556
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
uint64_t getExcessiveBlockSize() const
Definition validation.h:154
BlockValidationOptions withCheckPoW(bool _checkPoW=true) const
Definition validation.h:139
BlockValidationOptions withCheckMerkleRoot(bool _checkMerkleRoot=true) const
Definition validation.h:146
BlockValidationOptions(const Config &config)
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
std::string ToString() const
Definition block.cpp:15
std::vector< CTransactionRef > vtx
Definition block.h:63
bool fChecked
Definition block.h:66
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:211
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition blockindex.h:32
bool IsAssumedValid() const EXCLUSIVE_LOCKS_REQUIRED(
Definition blockindex.h:219
int64_t GetHeaderReceivedTime() const
Definition blockindex.h:184
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
bool HaveTxsDownloaded() const
Check whether this block's and all previous blocks' transactions have been downloaded (and stored to ...
Definition blockindex.h:174
int64_t GetChainTxCount() const
Get the number of transaction in the chain so far.
Definition blockindex.h:154
int32_t nSequenceId
(memory only) Sequential id assigned to distinguish order in which blocks are received.
Definition blockindex.h:98
int64_t GetReceivedTimeDiff() const
Definition blockindex.h:186
int64_t GetBlockTime() const
Definition blockindex.h:180
FlatFilePos GetUndoPos() const EXCLUSIVE_LOCKS_REQUIRED(
Definition blockindex.h:123
bool UpdateChainStats()
Update chain tx stats.
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:60
bool RaiseValidity(enum BlockValidity nUpTo) EXCLUSIVE_LOCKS_REQUIRED(
Raise the validity level of this block index entry.
Definition blockindex.h:226
NodeSeconds Time() const
Definition blockindex.h:176
int64_t nTimeReceived
(memory only) block header metadata
Definition blockindex.h:101
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
BlockHash GetBlockHash() const
Definition blockindex.h:146
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:77
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
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
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:80
const CBlock & GenesisBlock() const
const CMessageHeader::MessageMagic & DiskMagic() const
Definition chainparams.h:93
const ChainTxData & TxData() const
const Consensus::Params & GetConsensus() const
Definition chainparams.h:92
uint64_t PruneAfterHeight() const
const MapAssumeutxo & Assumeutxo() const
Get allowed assumeutxo configuration.
const CCheckpointData & Checkpoints() const
RAII-style controller object for a CCheckQueue that guarantees the passed queue is finished before co...
Definition checkqueue.h:198
Queue for verifications that have to be performed.
Definition checkqueue.h:28
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition coins.h:221
BlockHash GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Definition coins.cpp:214
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 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
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:618
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 BlockConnected(const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)
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(const CBlockLocator &)
static constexpr size_t MESSAGE_START_SIZE
Definition protocol.h:36
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition transaction.h:20
const TxId & GetTxId() const
Definition transaction.h:35
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:525
ScriptError GetScriptError() const
Definition validation.h:556
ScriptExecutionMetrics GetScriptExecutionMetrics() const
Definition validation.h:558
uint32_t nFlags
Definition validation.h:530
TxSigCheckLimiter * pTxLimitSigChecks
Definition validation.h:535
ScriptExecutionMetrics metrics
Definition validation.h:533
CTxOut m_tx_out
Definition validation.h:527
ScriptError error
Definition validation.h:532
PrecomputedTransactionData txdata
Definition validation.h:534
const CTransaction * ptxTo
Definition validation.h:528
unsigned int nIn
Definition validation.h:529
CheckInputsLimiter * pBlockLimitSigChecks
Definition validation.h:536
Serialized script, used inside transaction inputs and outputs.
Definition script.h:424
The basic transaction that is broadcasted on the network and contained in blocks.
const std::vector< CTxOut > vout
unsigned int GetTotalSize() const
Get the total transaction size in bytes.
bool IsCoinBase() const
const std::vector< CTxIn > vin
const TxId GetId() const
An input of a transaction.
Definition transaction.h:59
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition txmempool.h:212
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition txmempool.h:307
void AddTransactionsUpdated(unsigned int n)
const int64_t m_max_size_bytes
Definition txmempool.h:344
size_t DynamicMemoryUsage() const
void clear()
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...
CScript scriptPubKey
Amount nValue
Restore the UTXO in a Coin at a given COutPoint.
Definition undo.h:62
VerifyDBResult VerifyDB(Chainstate &chainstate, CCoinsView &coinsview, int nCheckLevel, int nCheckDepth) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
kernel::Notifications & m_notifications
Definition validation.h:622
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition validation.h:699
std::set< CBlockIndex *, CBlockIndexWorkComparator > setBlockIndexCandidates
The set of all CBlockIndex entries with either BLOCK_VALID_TRANSACTIONS (for itself and all ancestors...
Definition validation.h:831
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:816
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:801
void CheckForkWarningConditionsOnNewFork(CBlockIndex *pindexNewForkTip) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Mutex m_chainstate_mutex
The ChainState Mutex.
Definition validation.h:705
void UnloadBlockIndex() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
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:808
bool IsInitialBlockDownload() const
Check whether we are doing an initial block download (synchronizing from disk or network)
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:867
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(
Definition validation.h:834
bool ActivateBestChainStep(BlockValidationState &state, CBlockIndex *pindexMostWork, const std::shared_ptr< const CBlock > &pblock, bool &fInvalidFound, const avalanche::Processor *const avalanche=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Try to make some progress towards making pindexMostWork the active block.
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:864
int32_t nBlockReverseSequenceId
Decreasing counter (used by subsequent preciousblock calls).
Definition validation.h:714
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
Mutex cs_avalancheFinalizedBlockIndex
Definition validation.h:748
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.
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:728
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:841
arith_uint256 nLastPreciousChainwork
chainwork for the last block that preciousblock has been applied to.
Definition validation.h:716
std::chrono::microseconds m_last_flush
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:777
std::unique_ptr< CoinsViews > m_coins_views
Manages the UTXO set, which is a reflection of the contents of m_chain.
Definition validation.h:732
CRollingBloomFilter m_filterParkingPoliciesApplied
Filter to prevent parking a block due to block policies more than once.
Definition validation.h:763
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 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).
void ResetBlockFailureFlags(CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Remove invalidity status from a block and its descendants.
bool ResizeCoinsCaches(size_t coinstip_size, size_t coinsdb_size) EXCLUSIVE_LOCKS_REQUIRED(void LoadExternalBlockFile(FILE *fileIn, FlatFilePos *dbp=nullptr, std::multimap< BlockHash, FlatFilePos > *blocks_with_unknown_parent=nullptr, avalanche::Processor *const avalanche=nullptr) EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex
Resize the CoinsViews caches dynamically and flush state to disk.
void CheckBlockIndex()
Make various assertions about the state of the block index.
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:977
CBlockIndex const * m_best_fork_tip
Definition validation.h:766
bool ConnectTip(BlockValidationState &state, BlockPolicyValidationState &blockPolicyState, CBlockIndex *pindexNew, const std::shared_ptr< const CBlock > &pblock, DisconnectedBlockTransactions &disconnectpool, const avalanche::Processor *const avalanche=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Connect a new block to m_chain.
void PruneAndFlush()
Prune blockfiles from the disk if necessary and then flush chainstate changes if we pruned.
bool FlushStateToDisk(BlockValidationState &state, FlushStateMode mode, int nManualPruneHeight=0)
Update the on-disk chain state.
node::BlockManager & m_blockman
Reference to a BlockManager instance which itself is shared across all Chainstate instances.
Definition validation.h:772
void InitCoinsDB(size_t cache_size_bytes, bool in_memory, bool should_wipe, std::string leveldb_name="chainstate")
Initialize the CoinsViews UTXO set database management data structures.
CBlockIndex const * m_best_fork_base
Definition validation.h:767
void InvalidChainFound(CBlockIndex *pindexNew) EXCLUSIVE_LOCKS_REQUIRED(cs_main
std::atomic< bool > m_cached_finished_ibd
Whether this chainstate is undergoing initial block download.
Definition validation.h:724
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.
bool ActivateBestChain(BlockValidationState &state, std::shared_ptr< const CBlock > pblock=nullptr, avalanche::Processor *const avalanche=nullptr, bool skip_checkblockindex=false) EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex
Find the best known block, and make it the tip of the block chain.
void ClearAvalancheFinalizedBlock() EXCLUSIVE_LOCKS_REQUIRED(!cs_avalancheFinalizedBlockIndex)
Clear avalanche finalization.
const CBlockIndex * FindForkInGlobalIndex(const CBlockLocator &locator) const EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Find the last common block of this chain and a locator.
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:712
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 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)
Store a block on disk.
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.
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
const CBlockIndex *GetSnapshotBaseBlock() const EXCLUSIVE_LOCKS_REQUIRED(std::optional< int > GetSnapshotBaseHeight() const EXCLUSIVE_LOCKS_REQUIRED(bool IsUsable(const Chainstate *const pchainstate) const EXCLUSIVE_LOCKS_REQUIRED(
Returns nullptr if no snapshot has been loaded.
bool DetectSnapshotChainstate(CTxMemPool *mempool) EXCLUSIVE_LOCKS_REQUIRED(void ResetChainstates() EXCLUSIVE_LOCKS_REQUIRED(Chainstate &ActivateExistingSnapshot(CTxMemPool *mempool, BlockHash base_blockhash) EXCLUSIVE_LOCKS_REQUIRED(bool ValidatedSnapshotCleanup() 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...
const Config & GetConfig() const
int64_t m_total_coinstip_cache
The total number of bytes available for us to use across all in-memory coins caches.
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.
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
bool ShouldCheckBlockIndex() const
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.
std::optional< BlockHash > SnapshotBlockhash() const
bool IsSnapshotValidated() const EXCLUSIVE_LOCKS_REQUIRED(
Is there a snapshot in use and has it been fully validated?
CBlockIndex * ActiveTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
SnapshotCompletionResult MaybeCompleteSnapshotValidation(std::function< void(bilingual_str)> shutdown_fnc=[](bilingual_str msg) { AbortNode(msg.original, msg);}) EXCLUSIVE_LOCKS_REQUIRED(Chainstate & ActiveChainstate() const
Once the background validation chainstate has reached the height which is the base of the UTXO snapsh...
bool PopulateAndValidateSnapshot(Chainstate &snapshot_chainstate, AutoFile &coins_file, const node::SnapshotMetadata &metadata)
Internal helper for ActivateSnapshot().
int ActiveHeight() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
bool ActivateSnapshot(AutoFile &coins_file, const node::SnapshotMetadata &metadata, bool in_memory)
Construct and activate a Chainstate on the basis of UTXO snapshot data.
bool IsSnapshotActive() const
const CChainParams & GetParams() const
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
const arith_uint256 & MinimumChainWork() const
const Options m_options
bool LoadBlockIndex() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Load the block tree and coins database from disk, initializing state if we're running with -reindex.
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.
const BlockHash & AssumedValidBlock() const
ChainstateManager(Options options, node::BlockManager::Options blockman_options)
Chainstate &InitializeChainstate(CTxMemPool *mempool) EXCLUSIVE_LOCKS_REQUIRED(std::vector< Chainstate * GetAll)()
Instantiate a new chainstate.
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...
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
Simple class for regulating resource usage during CheckInputScripts (and CScriptCheck),...
Definition validation.h:383
bool consume_and_check(int consumed)
Definition validation.h:390
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...
virtual const CChainParams & GetChainParams() const =0
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:411
bool IsValid() const
Definition validation.h:119
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
std::string GetHex() const
Definition uint256.cpp:16
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...
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos) const
Functions for disk access for blocks.
RecursiveMutex cs_LastBlockFile
void FindFilesToPrune(std::set< int > &setFilesToPrune, uint64_t nPruneAfterHeight, int chain_tip_height, int prune_height, bool is_ibd)
Prune block and undo files (blk???.dat and undo???.dat) so that the disk space used is less than a us...
void FindFilesToPruneManual(std::set< int > &setFilesToPrune, int nManualPruneHeight, int chain_tip_height)
Calculate the block/rev files to delete based on height specified by user with RPC command pruneblock...
const CBlockIndex *GetFirstStoredBlock(const CBlockIndex &start_block) EXCLUSIVE_LOCKS_REQUIRED(bool m_have_pruned
Find the first block that is not pruned.
CBlockIndex * LookupBlockIndex(const BlockHash &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
bool WriteUndoDataForBlock(const CBlockUndo &blockundo, BlockValidationState &state, CBlockIndex &block) EXCLUSIVE_LOCKS_REQUIRED(FlatFilePos SaveBlockToDisk(const CBlock &block, int nHeight, CChain &active_chain, const FlatFilePos *dbp)
Store block on disk.
bool LoadingBlocks() const
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() 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.
std::set< CBlockIndex * > m_dirty_blockindex
Dirty block index entries.
bool m_check_for_pruning
Global flag to indicate we should check to see if there are block/undo files that should be deleted.
bool IsPruneMode() const
Whether running in -prune mode.
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.
void FlushBlockFile(bool fFinalize=false, bool finalize_undo=false)
Metadata describing a serialized version of a UTXO set from which an assumeutxo Chainstate can be con...
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.
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.
DisconnectResult
volatile double sum
Definition examples.cpp:10
bool RenameOver(fs::path src, fs::path dest)
bool CheckDiskSpace(const fs::path &dir, uint64_t additional_bytes)
bool FileCommit(FILE *file)
Ensure file contents are fully committed to disk, using a platform-specific feature analogous to fsyn...
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.
std::map< int, const AssumeutxoData > MapAssumeutxo
Definition chainparams.h:59
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
@ ALL
Definition logging.h:71
@ 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.
@ 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).
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
std::optional< CCoinsStats > ComputeUTXOStats(CoinStatsHashType hash_type, CCoinsView *view, node::BlockManager &blockman, const std::function< void()> &interruption_point)
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)
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
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)
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
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:78
std::shared_ptr< Chain::Notifications > m_notifications
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
uint256 GetRandHash() noexcept
Definition random.cpp:659
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
const char * prefix
Definition rest.cpp:817
reverse_range< T > reverse_iterate(T &x)
std::string ScriptErrorString(const ScriptError serror)
ScriptError
@ SIGCHECKS_LIMIT_EXCEEDED
@ SCRIPT_VERIFY_P2SH
@ SCRIPT_VERIFY_SIGPUSHONLY
@ SCRIPT_VERIFY_LOW_S
@ SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY
@ SCRIPT_ENABLE_REPLAY_PROTECTION
@ SCRIPT_ENABLE_SCHNORR_MULTISIG
@ SCRIPT_VERIFY_STRICTENC
@ SCRIPT_VERIFY_NULLFAIL
@ SCRIPT_VERIFY_DERSIG
@ SCRIPT_ENFORCE_SIGCHECKS
@ SCRIPT_VERIFY_CLEANSTACK
@ SCRIPT_VERIFY_NONE
@ SCRIPT_VERIFY_MINIMALDATA
@ SCRIPT_VERIFY_CHECKSEQUENCEVERIFY
@ SCRIPT_ENABLE_SIGHASH_FORKID
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.
static std::string ToString(const CService &ip)
Definition db.h:37
CAddrDb db
Definition main.cpp:35
@ SER_DISK
Definition serialize.h:153
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
static constexpr Amount zero() noexcept
Definition amount.h:32
Holds configuration for use during UTXO snapshot load and validation.
Definition chainparams.h:46
A BlockHash is a unqiue identifier for a block.
Definition blockhash.h:13
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition block.h:105
Holds various statistics on transactions within a chain.
Definition chainparams.h:67
double dTxRate
Definition chainparams.h:70
int64_t nTime
Definition chainparams.h:68
int64_t nTxCount
Definition chainparams.h:69
User-controlled performance and debug options.
Definition txdb.h:56
Parameters that influence chain consensus.
Definition params.h:34
BlockHash hashGenesisBlock
Definition params.h:35
int schumpeterActivationTime
Unix time used for MTP activation of 15 May 2025 12:00:00 UTC upgrade.
Definition params.h:67
int64_t nPowTargetSpacing
Definition params.h:80
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
unsigned int nPos
Definition flatfile.h:16
bool IsNull() const
Definition flatfile.h:40
Validation result for a transaction evaluated by MemPoolAccept (single or package).
Definition validation.h:207
const ResultType m_result_type
Result type.
Definition validation.h:218
@ VALID
Fully validated, valid.
static MempoolAcceptResult Failure(TxValidationState state)
Definition validation.h:246
static MempoolAcceptResult FeeFailure(TxValidationState state, CFeeRate effective_feerate, const std::vector< TxId > &txids_fee_calculations)
Definition validation.h:251
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:259
static MempoolAcceptResult MempoolTx(int64_t vsize, Amount fees)
Constructor for already-in-mempool case.
Definition validation.h:269
std::chrono::time_point< NodeClock > time_point
Definition time.h:19
Validation result for package mempool acceptance.
Definition validation.h:310
Precompute sighash midstate to avoid quadratic hashing.
const char * what() const override
A TxId is the identifier of a transaction.
Definition txid.h:14
Bilingual messages:
Definition translation.h:17
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 AssertLockHeld(cs)
Definition sync.h:146
#define EXCLUSIVE_LOCKS_REQUIRED(...)
#define LOCKS_EXCLUDED(...)
#define NO_THREAD_SAFETY_ANALYSIS
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...
#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)
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.
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:48
uint256 uint256S(const char *str)
uint256 from const char *.
Definition uint256.h:143
DisconnectResult ApplyBlockUndo(CBlockUndo &&blockUndo, const CBlock &block, const CBlockIndex *pindex, CCoinsViewCache &coins)
Undo a block from the block and the undoblock data.
#define expect(bit)
static bool DeleteCoinsDBFromDisk(const fs::path &db_path, bool is_snapshot) EXCLUSIVE_LOCKS_REQUIRED(
void StartScriptCheckWorkerThreads(int threads_num)
Run instances of script checking worker threads.
static int64_t nTimeConnectTotal
GlobalMutex g_best_block_mutex
Amount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
std::condition_variable g_best_block_cv
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...
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
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.
static uint32_t GetNextBlockScriptFlags(const CBlockIndex *pindex, const ChainstateManager &chainman)
const CBlockIndex * g_best_block
Used to notify getblocktemplate RPC of new tips.
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.
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.
const AssumeutxoData * ExpectedAssumeutxo(const int height, const CChainParams &chainparams)
Return the expected assumeutxo value for a given height, if one exists.
static constexpr int PRUNE_LOCK_BUFFER
The number of blocks to keep below the deepest prune lock.
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
static bool NotifyHeaderTip(Chainstate &chainstate) LOCKS_EXCLUDED(cs_main)
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'.
DisconnectResult UndoCoinSpend(Coin &&undo, CCoinsViewCache &view, const COutPoint &out)
Restore the UTXO in a Coin at a given COutPoint.
static int64_t nTimeIndex
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 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
static int64_t nTimeCheck
#define MILLI
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.
static int64_t nTimeChainState
assert(!tx.IsCoinBase())
static int64_t nTimeReadFromDisk
static bool IsReplayProtectionEnabled(const Consensus::Params &params, int64_t nMedianTimePast)
bool ContextualCheckTransactionForCurrentBlock(const CBlockIndex &active_chain_tip, const Consensus::Params &params, const CTransaction &tx, TxValidationState &state) EXCLUSIVE_LOCKS_REQUIRED(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) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
This is a variant of ContextualCheckTransaction which computes the contextual check for a transaction...
Definition validation.h:593
GlobalMutex g_best_block_mutex
Amount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
std::condition_variable g_best_block_cv
double GuessVerificationProgress(const ChainTxData &data, const CBlockIndex *pindex)
Guess verification progress (as a fraction between 0.0=genesis and 1.0=current tip).
#define MIN_TRANSACTION_SIZE
Definition validation.h:79
bool CheckInputScripts(const CTransaction &tx, TxValidationState &state, const CCoinsViewCache &view, const uint32_t flags, bool sigCacheStore, bool scriptCacheStore, const PrecomputedTransactionData &txdata, int &nSigChecksOut, TxSigCheckLimiter &txLimitSigChecks, CheckInputsLimiter *pBlockLimitSigChecks, std::vector< CScriptCheck > *pvChecks) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Check whether all of this transaction's input scripts succeed.
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:95
const CBlockIndex * g_best_block
Used to notify getblocktemplate RPC of new tips.
SnapshotCompletionResult
SynchronizationState
Current sync state passed to tip changed callbacks.
Definition validation.h:114
bool AbortNode(BlockValidationState &state, const std::string &strMessage, const bilingual_str &userMessage=bilingual_str{})
MempoolAcceptResult AcceptToMemoryPool(Chainstate &active_chainstate, const CTransactionRef &tx, int64_t accept_time, bool bypass_limits, bool test_accept=false, unsigned int heightOverride=0) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Try to add a transaction to the mempool.
VerifyDBResult
Definition validation.h:608
void SpendCoins(CCoinsViewCache &view, const CTransaction &tx, CTxUndo &txundo, int nHeight)
Mark all the coins corresponding to a given transaction inputs as spent.
bool CheckBlock(const CBlock &block, BlockValidationState &state, const Consensus::Params &params, BlockValidationOptions validationOptions)
Functions for validating blocks and updating the block tree.
CoinsCacheSizeState
Definition validation.h:677
@ LARGE
The cache is at >= 90% capacity.
@ CRITICAL
The coins cache is in immediate need of a flush.
bool DeploymentActiveAt(const CBlockIndex &index, const ChainstateManager &chainman, DEP dep)
FlushStateMode
Definition validation.h:638
const AssumeutxoData * ExpectedAssumeutxo(const int height, const CChainParams &params)
Return the expected assumeutxo value for a given height, if one exists.
static const int DEFAULT_STOPATHEIGHT
Default for -stopatheight.
Definition validation.h:90
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