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 for (const CBlockIndex *pindex : blocksToReconcile) {
3588 avalanche->addToReconcile(pindex);
3589 avalanche->computeStakingReward(pindex);
3590 }
3591 }
3592
3593 if (!blocks_connected) {
3594 return true;
3595 }
3596
3597 if (nStopAtHeight && pindexNewTip &&
3598 pindexNewTip->nHeight >= nStopAtHeight) {
3599 StartShutdown();
3600 }
3601
3602 if (WITH_LOCK(::cs_main, return m_disabled)) {
3603 // Background chainstate has reached the snapshot base block, so
3604 // exit.
3605 break;
3606 }
3607
3608 // We check shutdown only after giving ActivateBestChainStep a chance to
3609 // run once so that we never shutdown before connecting the genesis
3610 // block during LoadChainTip(). Previously this caused an assert()
3611 // failure during shutdown in such cases as the UTXO DB flushing checks
3612 // that the best block hash is non-null.
3613 if (ShutdownRequested()) {
3614 break;
3615 }
3616 } while (pindexNewTip != pindexMostWork);
3617
3618 // Write changes periodically to disk, after relay.
3620 return false;
3621 }
3622
3623 return true;
3624}
3625
3630 {
3631 LOCK(cs_main);
3632 if (pindex->nChainWork < m_chain.Tip()->nChainWork) {
3633 // Nothing to do, this block is not at the tip.
3634 return true;
3635 }
3636
3638 // The chain has been extended since the last call, reset the
3639 // counter.
3641 }
3642
3644 setBlockIndexCandidates.erase(pindex);
3646 if (nBlockReverseSequenceId > std::numeric_limits<int32_t>::min()) {
3647 // We can't keep reducing the counter if somebody really wants to
3648 // call preciousblock 2**31-1 times on the same set of tips...
3650 }
3651
3652 // In case this was parked, unpark it.
3653 UnparkBlock(pindex);
3654
3655 // Make sure it is added to the candidate list if appropriate.
3656 if (pindex->IsValid(BlockValidity::TRANSACTIONS) &&
3657 pindex->HaveTxsDownloaded()) {
3658 setBlockIndexCandidates.insert(pindex);
3660 }
3661 }
3662
3663 return ActivateBestChain(state, /*pblock=*/nullptr, avalanche);
3664}
3665
3666namespace {
3667// Leverage RAII to run a functor at scope end
3668template <typename Func> struct Defer {
3669 Func func;
3670 Defer(Func &&f) : func(std::move(f)) {}
3671 ~Defer() { func(); }
3672};
3673} // namespace
3674
3676 bool invalidate) {
3677 // Genesis block can't be invalidated or parked
3678 assert(pindex);
3679 if (pindex->nHeight == 0) {
3680 return false;
3681 }
3682
3684 bool pindex_was_in_chain = false;
3685 int disconnected = 0;
3686
3687 // We do not allow ActivateBestChain() to run while UnwindBlock() is
3688 // running, as that could cause the tip to change while we disconnect
3689 // blocks. (Note for backport of Core PR16849: we acquire
3690 // LOCK(m_chainstate_mutex) in the Park, Invalidate and FinalizeBlock
3691 // functions due to differences in our code)
3693
3694 // We'll be acquiring and releasing cs_main below, to allow the validation
3695 // callbacks to run. However, we should keep the block index in a
3696 // consistent state as we disconnect blocks -- in particular we need to
3697 // add equal-work blocks to setBlockIndexCandidates as we disconnect.
3698 // To avoid walking the block index repeatedly in search of candidates,
3699 // build a map once so that we can look up candidate blocks by chain
3700 // work as we go.
3701 std::multimap<const arith_uint256, CBlockIndex *> candidate_blocks_by_work;
3702
3703 {
3704 LOCK(cs_main);
3705 for (auto &entry : m_blockman.m_block_index) {
3706 CBlockIndex *candidate = &entry.second;
3707 // We don't need to put anything in our active chain into the
3708 // multimap, because those candidates will be found and considered
3709 // as we disconnect.
3710 // Instead, consider only non-active-chain blocks that have at
3711 // least as much work as where we expect the new tip to end up.
3712 if (!m_chain.Contains(candidate) &&
3715 candidate->HaveTxsDownloaded()) {
3717 std::make_pair(candidate->nChainWork, candidate));
3718 }
3719 }
3720 }
3721
3722 {
3723 LOCK(cs_main);
3724 // Lock for as long as disconnectpool is in scope to make sure
3725 // UpdateMempoolForReorg is called after DisconnectTip without unlocking
3726 // in between
3727 LOCK(MempoolMutex());
3728
3729 constexpr int maxDisconnectPoolBlocks = 10;
3730 bool ret = false;
3732 // After 10 blocks this becomes nullptr, so that DisconnectTip will
3733 // stop giving us unwound block txs if we are doing a deep unwind.
3735
3736 // Disable thread safety analysis because we can't require m_mempool->cs
3737 // as m_mempool can be null. We keep the runtime analysis though.
3740 if (m_mempool && !disconnectpool.isEmpty()) {
3742 // DisconnectTip will add transactions to disconnectpool.
3743 // When all unwinding is done and we are on a new tip, we must
3744 // add all transactions back to the mempool against the new tip.
3745 disconnectpool.updateMempoolForReorg(*this,
3746 /* fAddToMempool = */ ret,
3747 *m_mempool);
3748 }
3749 });
3750
3751 // Disconnect (descendants of) pindex, and mark them invalid.
3752 while (true) {
3753 if (ShutdownRequested()) {
3754 break;
3755 }
3756
3757 // Make sure the queue of validation callbacks doesn't grow
3758 // unboundedly.
3759 // FIXME this commented code is a regression and could cause OOM if
3760 // a very old block is invalidated via the invalidateblock RPC.
3761 // This can be uncommented if the main signals are moved away from
3762 // cs_main or this code is refactored so that cs_main can be
3763 // released at this point.
3764 //
3765 // LimitValidationInterfaceQueue();
3766
3767 if (!m_chain.Contains(pindex)) {
3768 break;
3769 }
3770
3771 if (m_mempool && disconnected == 0) {
3772 // On first iteration, we grab all the mempool txs to preserve
3773 // topological ordering. This has the side-effect of temporarily
3774 // clearing the mempool, but we will re-add later in
3775 // updateMempoolForReorg() (above). This technique guarantees
3776 // mempool consistency as well as ensures that our topological
3777 // entry_id index is always correct.
3779 }
3780
3781 pindex_was_in_chain = true;
3783
3784 // ActivateBestChain considers blocks already in m_chain
3785 // unconditionally valid already, so force disconnect away from it.
3786
3788 ++disconnected;
3789
3790 if (optDisconnectPool && disconnected > maxDisconnectPoolBlocks) {
3791 // Stop using the disconnect pool after 10 blocks. After 10
3792 // blocks we no longer add block tx's to the disconnectpool.
3793 // However, when this scope ends we will reconcile what's
3794 // in the pool with the new tip (in the deferred d'tor above).
3795 optDisconnectPool = nullptr;
3796 }
3797
3798 if (!ret) {
3799 return false;
3800 }
3801
3802 assert(invalid_walk_tip->pprev == m_chain.Tip());
3803
3804 // We immediately mark the disconnected blocks as invalid.
3805 // This prevents a case where pruned nodes may fail to
3806 // invalidateblock and be left unable to start as they have no tip
3807 // candidates (as there are no blocks that meet the "have data and
3808 // are not invalid per nStatus" criteria for inclusion in
3809 // setBlockIndexCandidates).
3810
3811 invalid_walk_tip->nStatus =
3812 invalidate ? invalid_walk_tip->nStatus.withFailed()
3813 : invalid_walk_tip->nStatus.withParked();
3814
3817
3819 (invalidate ? to_mark_failed_or_parked->nStatus.hasFailed()
3820 : to_mark_failed_or_parked->nStatus.isParked())) {
3821 // We only want to mark the last disconnected block as
3822 // Failed (or Parked); its children need to be FailedParent (or
3823 // ParkedParent) instead.
3824 to_mark_failed_or_parked->nStatus =
3825 (invalidate
3826 ? to_mark_failed_or_parked->nStatus.withFailed(false)
3827 .withFailedParent()
3828 : to_mark_failed_or_parked->nStatus.withParked(false)
3829 .withParkedParent());
3830
3832 }
3833
3834 // Add any equal or more work headers to setBlockIndexCandidates
3835 auto candidate_it = candidate_blocks_by_work.lower_bound(
3836 invalid_walk_tip->pprev->nChainWork);
3837 while (candidate_it != candidate_blocks_by_work.end()) {
3839 invalid_walk_tip->pprev)) {
3840 setBlockIndexCandidates.insert(candidate_it->second);
3842 } else {
3843 ++candidate_it;
3844 }
3845 }
3846
3847 // Track the last disconnected block, so we can correct its
3848 // FailedParent (or ParkedParent) status in future iterations, or,
3849 // if it's the last one, call InvalidChainFound on it.
3851 }
3852 }
3853
3855
3856 {
3857 LOCK(cs_main);
3859 // If the to-be-marked invalid block is in the active chain,
3860 // something is interfering and we can't proceed.
3861 return false;
3862 }
3863
3864 // Mark pindex (or the last disconnected block) as invalid (or parked),
3865 // even when it never was in the main chain.
3866 to_mark_failed_or_parked->nStatus =
3867 invalidate ? to_mark_failed_or_parked->nStatus.withFailed()
3868 : to_mark_failed_or_parked->nStatus.withParked();
3870 if (invalidate) {
3872 }
3873
3874 // If any new blocks somehow arrived while we were disconnecting
3875 // (above), then the pre-calculation of what should go into
3876 // setBlockIndexCandidates may have missed entries. This would
3877 // technically be an inconsistency in the block index, but if we clean
3878 // it up here, this should be an essentially unobservable error.
3879 // Loop back over all block index entries and add any missing entries
3880 // to setBlockIndexCandidates.
3881 for (auto &[_, block_index] : m_blockman.m_block_index) {
3883 block_index.HaveTxsDownloaded() &&
3884 !setBlockIndexCandidates.value_comp()(&block_index,
3885 m_chain.Tip())) {
3887 }
3888 }
3889
3890 if (invalidate) {
3892 }
3893 }
3894
3895 // Only notify about a new block tip if the active chain was modified.
3896 if (pindex_was_in_chain) {
3899 *to_mark_failed_or_parked->pprev);
3900 }
3901 return true;
3902}
3903
3905 CBlockIndex *pindex) {
3908 // See 'Note for backport of Core PR16849' in Chainstate::UnwindBlock
3910
3911 return UnwindBlock(state, pindex, true);
3912}
3913
3917 // See 'Note for backport of Core PR16849' in Chainstate::UnwindBlock
3919
3920 return UnwindBlock(state, pindex, false);
3921}
3922
3923template <typename F>
3925 CBlockIndex *pindex, F f) {
3926 BlockStatus newStatus = f(pindex->nStatus);
3927 if (pindex->nStatus != newStatus &&
3928 (!pindexBase ||
3929 pindex->GetAncestor(pindexBase->nHeight) == pindexBase)) {
3930 pindex->nStatus = newStatus;
3931 m_blockman.m_dirty_blockindex.insert(pindex);
3932 if (newStatus.isValid()) {
3933 m_chainman.m_failed_blocks.erase(pindex);
3934 }
3935
3936 if (pindex->IsValid(BlockValidity::TRANSACTIONS) &&
3937 pindex->HaveTxsDownloaded() &&
3938 setBlockIndexCandidates.value_comp()(m_chain.Tip(), pindex)) {
3939 setBlockIndexCandidates.insert(pindex);
3940 }
3941 return true;
3942 }
3943 return false;
3944}
3945
3946template <typename F, typename C, typename AC>
3950
3951 // Update the current block and ancestors; while we're doing this, identify
3952 // which was the deepest ancestor we changed.
3954 for (auto pindexAncestor = pindex; pindexAncestor != nullptr;
3956 if (UpdateFlagsForBlock(nullptr, pindexAncestor, f)) {
3958 }
3959 }
3960
3961 if (pindexReset &&
3962 pindexReset->GetAncestor(pindexDeepestChanged->nHeight) ==
3964 // reset pindexReset if it had a modified ancestor.
3965 pindexReset = nullptr;
3966 }
3967
3968 // Update all blocks under modified blocks.
3969 for (auto &[_, block_index] : m_blockman.m_block_index) {
3973 }
3974}
3975
3978
3980 pindex, m_chainman.m_best_invalid,
3981 [](const BlockStatus status) {
3982 return status.withClearedFailureFlags();
3983 },
3984 [](const BlockStatus status) {
3985 return status.withClearedFailureFlags();
3986 },
3987 [](const BlockStatus status) {
3988 return status.withFailedParent(false);
3989 });
3990}
3991
3994
3996 pindex, m_chainman.m_best_parked,
3997 [](const BlockStatus status) {
3998 return status.withClearedParkedFlags();
3999 },
4000 [fClearChildren](const BlockStatus status) {
4001 return fClearChildren ? status.withClearedParkedFlags()
4002 : status.withParkedParent(false);
4003 },
4004 [](const BlockStatus status) {
4005 return status.withParkedParent(false);
4006 });
4007}
4008
4010 return UnparkBlockImpl(pindex, true);
4011}
4012
4014 return UnparkBlockImpl(pindex, false);
4015}
4016
4019 if (!pindex) {
4020 return false;
4021 }
4022
4023 if (!m_chain.Contains(pindex)) {
4025 "The block to mark finalized by avalanche is not on the "
4026 "active chain: %s\n",
4027 pindex->GetBlockHash().ToString());
4028 return false;
4029 }
4030
4031 avalanche.cleanupStakingRewards(pindex->nHeight);
4032
4033 if (IsBlockAvalancheFinalized(pindex)) {
4034 return true;
4035 }
4036
4037 {
4040 }
4041
4042 WITH_LOCK(cs_main, GetMainSignals().BlockFinalized(pindex));
4043
4044 return true;
4045}
4046
4051
4058
4065 const FlatFilePos &pos) {
4066 pindexNew->nTx = block.vtx.size();
4068 pindexNew->nFile = pos.nFile;
4069 pindexNew->nDataPos = pos.nPos;
4070 pindexNew->nUndoPos = 0;
4071 pindexNew->nStatus = pindexNew->nStatus.withData();
4072 pindexNew->RaiseValidity(BlockValidity::TRANSACTIONS);
4074
4075 if (pindexNew->UpdateChainStats()) {
4076 // If pindexNew is the genesis block or all parents are
4077 // BLOCK_VALID_TRANSACTIONS.
4078 std::deque<CBlockIndex *> queue;
4079 queue.push_back(pindexNew);
4080
4081 // Recursively process any descendant blocks that now may be eligible to
4082 // be connected.
4083 while (!queue.empty()) {
4084 CBlockIndex *pindex = queue.front();
4085 queue.pop_front();
4086 pindex->UpdateChainStats();
4087 if (pindex->nSequenceId == 0) {
4088 // We assign a sequence is when transaction are received to
4089 // prevent a miner from being able to broadcast a block but not
4090 // its content. However, a sequence id may have been set
4091 // manually, for instance via PreciousBlock, in which case, we
4092 // don't need to assign one.
4093 pindex->nSequenceId = nBlockSequenceId++;
4094 }
4095
4096 if (m_chain.Tip() == nullptr ||
4097 !setBlockIndexCandidates.value_comp()(pindex, m_chain.Tip())) {
4098 setBlockIndexCandidates.insert(pindex);
4099 }
4100
4101 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
4102 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
4103 range = m_blockman.m_blocks_unlinked.equal_range(pindex);
4104 while (range.first != range.second) {
4105 std::multimap<CBlockIndex *, CBlockIndex *>::iterator it =
4106 range.first;
4107 queue.push_back(it->second);
4108 range.first++;
4109 m_blockman.m_blocks_unlinked.erase(it);
4110 }
4111 }
4112 } else if (pindexNew->pprev &&
4113 pindexNew->pprev->IsValid(BlockValidity::TREE)) {
4115 std::make_pair(pindexNew->pprev, pindexNew));
4116 }
4117}
4118
4127static bool CheckBlockHeader(const CBlockHeader &block,
4128 BlockValidationState &state,
4129 const Consensus::Params &params,
4131 // Check proof of work matches claimed amount
4132 if (validationOptions.shouldValidatePoW() &&
4133 !CheckProofOfWork(block.GetHash(), block.nBits, params)) {
4135 "high-hash", "proof of work failed");
4136 }
4137
4138 return true;
4139}
4140
4141bool CheckBlock(const CBlock &block, BlockValidationState &state,
4142 const Consensus::Params &params,
4144 // These are checks that are independent of context.
4145 if (block.fChecked) {
4146 return true;
4147 }
4148
4149 // Check that the header is valid (particularly PoW). This is mostly
4150 // redundant with the call in AcceptBlockHeader.
4151 if (!CheckBlockHeader(block, state, params, validationOptions)) {
4152 return false;
4153 }
4154
4155 // Check the merkle root.
4156 if (validationOptions.shouldValidateMerkleRoot()) {
4157 bool mutated;
4159 if (block.hashMerkleRoot != hashMerkleRoot2) {
4161 "bad-txnmrklroot", "hashMerkleRoot mismatch");
4162 }
4163
4164 // Check for merkle tree malleability (CVE-2012-2459): repeating
4165 // sequences of transactions in a block without affecting the merkle
4166 // root of a block, while still invalidating it.
4167 if (mutated) {
4169 "bad-txns-duplicate", "duplicate transaction");
4170 }
4171 }
4172
4173 // All potential-corruption validation must be done before we do any
4174 // transaction validation, as otherwise we may mark the header as invalid
4175 // because we receive the wrong transactions for it.
4176
4177 // First transaction must be coinbase.
4178 if (block.vtx.empty()) {
4180 "bad-cb-missing", "first tx is not coinbase");
4181 }
4182
4183 // Size limits.
4184 auto nMaxBlockSize = validationOptions.getExcessiveBlockSize();
4185
4186 // Bail early if there is no way this block is of reasonable size.
4187 if ((block.vtx.size() * MIN_TRANSACTION_SIZE) > nMaxBlockSize) {
4189 "bad-blk-length", "size limits failed");
4190 }
4191
4193 if (currentBlockSize > nMaxBlockSize) {
4195 "bad-blk-length", "size limits failed");
4196 }
4197
4198 // And a valid coinbase.
4200 if (!CheckCoinbase(*block.vtx[0], tx_state)) {
4202 tx_state.GetRejectReason(),
4203 strprintf("Coinbase check failed (txid %s) %s",
4204 block.vtx[0]->GetId().ToString(),
4205 tx_state.GetDebugMessage()));
4206 }
4207
4208 // Check transactions for regularity, skipping the first. Note that this
4209 // is the first time we check that all after the first are !IsCoinBase.
4210 for (size_t i = 1; i < block.vtx.size(); i++) {
4211 auto *tx = block.vtx[i].get();
4212 if (!CheckRegularTransaction(*tx, tx_state)) {
4213 return state.Invalid(
4215 tx_state.GetRejectReason(),
4216 strprintf("Transaction check failed (txid %s) %s",
4217 tx->GetId().ToString(), tx_state.GetDebugMessage()));
4218 }
4219 }
4220
4221 if (validationOptions.shouldValidatePoW() &&
4222 validationOptions.shouldValidateMerkleRoot()) {
4223 block.fChecked = true;
4224 }
4225
4226 return true;
4227}
4228
4229bool HasValidProofOfWork(const std::vector<CBlockHeader> &headers,
4231 return std::all_of(headers.cbegin(), headers.cend(),
4232 [&](const auto &header) {
4233 return CheckProofOfWork(
4234 header.GetHash(), header.nBits, consensusParams);
4235 });
4236}
4237
4238arith_uint256 CalculateHeadersWork(const std::vector<CBlockHeader> &headers) {
4240 for (const CBlockHeader &header : headers) {
4241 CBlockIndex dummy(header);
4242 total_work += GetBlockProof(dummy);
4243 }
4244 return total_work;
4245}
4246
4258 const CBlockHeader &block, BlockValidationState &state,
4261 const std::optional<CCheckpointData> &test_checkpoints = std::nullopt)
4264 assert(pindexPrev != nullptr);
4265 const int nHeight = pindexPrev->nHeight + 1;
4266
4267 const CChainParams &params = chainman.GetParams();
4268
4269 // Check proof of work
4270 if (block.nBits != GetNextWorkRequired(pindexPrev, &block, params)) {
4271 LogPrintf("bad bits after height: %d\n", pindexPrev->nHeight);
4273 "bad-diffbits", "incorrect proof of work");
4274 }
4275
4276 // Check against checkpoints
4277 if (chainman.m_options.checkpoints_enabled) {
4279 test_checkpoints ? test_checkpoints.value() : params.Checkpoints();
4280
4281 // Check that the block chain matches the known block chain up to a
4282 // checkpoint.
4285 "ERROR: %s: rejected by checkpoint lock-in at %d\n",
4286 __func__, nHeight);
4288 "checkpoint mismatch");
4289 }
4290
4291 // Don't accept any forks from the main chain prior to last checkpoint.
4292 // GetLastCheckpoint finds the last checkpoint in MapCheckpoints that's
4293 // in our BlockIndex().
4294
4295 const CBlockIndex *pcheckpoint =
4296 blockman.GetLastCheckpoint(checkpoints);
4299 "ERROR: %s: forked chain older than last checkpoint "
4300 "(height %d)\n",
4301 __func__, nHeight);
4303 "bad-fork-prior-to-checkpoint");
4304 }
4305 }
4306
4307 // Check timestamp against prev
4308 if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast()) {
4310 "time-too-old", "block's timestamp is too early");
4311 }
4312
4313 // Check timestamp
4314 if (block.Time() > now + std::chrono::seconds{MAX_FUTURE_BLOCK_TIME}) {
4316 "time-too-new",
4317 "block timestamp too far in the future");
4318 }
4319
4320 // Reject blocks with outdated version
4321 if ((block.nVersion < 2 &&
4324 (block.nVersion < 3 &&
4327 (block.nVersion < 4 &&
4330 return state.Invalid(
4332 strprintf("bad-version(0x%08x)", block.nVersion),
4333 strprintf("rejected nVersion=0x%08x block", block.nVersion));
4334 }
4335
4336 return true;
4337}
4338
4340 const CBlockIndex &active_chain_tip, const Consensus::Params &params,
4341 const CTransaction &tx, TxValidationState &state) {
4343
4344 // ContextualCheckTransactionForCurrentBlock() uses
4345 // active_chain_tip.Height()+1 to evaluate nLockTime because when
4346 // IsFinalTx() is called within AcceptBlock(), the height of the
4347 // block *being* evaluated is what is used. Thus if we want to know if a
4348 // transaction can be part of the *next* block, we need to call
4349 // ContextualCheckTransaction() with one more than
4350 // active_chain_tip.Height().
4351 const int nBlockHeight = active_chain_tip.nHeight + 1;
4352
4353 // BIP113 will require that time-locked transactions have nLockTime set to
4354 // less than the median time of the previous block they're contained in.
4355 // When the next block is created its previous block will be the current
4356 // chain tip, so we use that to calculate the median time passed to
4357 // ContextualCheckTransaction().
4358 // This time can also be used for consensus upgrades.
4359 const int64_t nMedianTimePast{active_chain_tip.GetMedianTimePast()};
4360
4361 return ContextualCheckTransaction(params, tx, state, nBlockHeight,
4363}
4364
4372static bool ContextualCheckBlock(const CBlock &block,
4373 BlockValidationState &state,
4374 const ChainstateManager &chainman,
4375 const CBlockIndex *pindexPrev) {
4376 const int nHeight = pindexPrev == nullptr ? 0 : pindexPrev->nHeight + 1;
4377
4378 // Enforce BIP113 (Median Time Past).
4380 if (DeploymentActiveAfter(pindexPrev, chainman,
4382 assert(pindexPrev != nullptr);
4384 }
4385
4386 const int64_t nMedianTimePast =
4387 pindexPrev == nullptr ? 0 : pindexPrev->GetMedianTimePast();
4388
4391 : block.GetBlockTime()};
4392
4393 const Consensus::Params params = chainman.GetConsensus();
4394 const bool fIsMagneticAnomalyEnabled =
4396
4397 // Check transactions:
4398 // - canonical ordering
4399 // - ensure they are finalized
4400 // - check they have the minimum size
4401 const CTransaction *prevTx = nullptr;
4402 for (const auto &ptx : block.vtx) {
4403 const CTransaction &tx = *ptx;
4405 if (prevTx && (tx.GetId() <= prevTx->GetId())) {
4406 if (tx.GetId() == prevTx->GetId()) {
4408 "tx-duplicate",
4409 strprintf("Duplicated transaction %s",
4410 tx.GetId().ToString()));
4411 }
4412
4413 return state.Invalid(
4415 strprintf("Transaction order is invalid (%s < %s)",
4416 tx.GetId().ToString(),
4417 prevTx->GetId().ToString()));
4418 }
4419
4420 if (prevTx || !tx.IsCoinBase()) {
4421 prevTx = &tx;
4422 }
4423 }
4424
4427 nLockTimeCutoff)) {
4429 tx_state.GetRejectReason(),
4430 tx_state.GetDebugMessage());
4431 }
4432 }
4433
4434 // Enforce rule that the coinbase starts with serialized block height
4435 if (DeploymentActiveAfter(pindexPrev, chainman,
4438 if (block.vtx[0]->vin[0].scriptSig.size() < expect.size() ||
4439 !std::equal(expect.begin(), expect.end(),
4440 block.vtx[0]->vin[0].scriptSig.begin())) {
4442 "bad-cb-height",
4443 "block height mismatch in coinbase");
4444 }
4445 }
4446
4447 return true;
4448}
4449
4456 const CBlockHeader &block, BlockValidationState &state,
4458 const std::optional<CCheckpointData> &test_checkpoints) {
4460 const Config &config = this->GetConfig();
4461 const CChainParams &chainparams = config.GetChainParams();
4462
4463 // Check for duplicate
4464 BlockHash hash = block.GetHash();
4465 BlockMap::iterator miSelf{m_blockman.m_block_index.find(hash)};
4466 if (hash != chainparams.GetConsensus().hashGenesisBlock) {
4467 if (miSelf != m_blockman.m_block_index.end()) {
4468 // Block header is already known.
4469 CBlockIndex *pindex = &(miSelf->second);
4470 if (ppindex) {
4471 *ppindex = pindex;
4472 }
4473
4474 if (pindex->nStatus.isInvalid()) {
4475 LogPrint(BCLog::VALIDATION, "%s: block %s is marked invalid\n",
4476 __func__, hash.ToString());
4477 return state.Invalid(
4479 }
4480
4481 return true;
4482 }
4483
4484 if (!CheckBlockHeader(block, state, chainparams.GetConsensus(),
4485 BlockValidationOptions(config))) {
4487 "%s: Consensus::CheckBlockHeader: %s, %s\n", __func__,
4488 hash.ToString(), state.ToString());
4489 return false;
4490 }
4491
4492 // Get prev block index
4493 BlockMap::iterator mi{
4494 m_blockman.m_block_index.find(block.hashPrevBlock)};
4495 if (mi == m_blockman.m_block_index.end()) {
4497 "header %s has prev block not found: %s\n",
4498 hash.ToString(), block.hashPrevBlock.ToString());
4500 "prev-blk-not-found");
4501 }
4502
4503 CBlockIndex *pindexPrev = &((*mi).second);
4505 if (pindexPrev->nStatus.isInvalid()) {
4507 "header %s has prev block invalid: %s\n", hash.ToString(),
4508 block.hashPrevBlock.ToString());
4510 "bad-prevblk");
4511 }
4512
4514 block, state, m_blockman, *this, pindexPrev,
4517 "%s: Consensus::ContextualCheckBlockHeader: %s, %s\n",
4518 __func__, hash.ToString(), state.ToString());
4519 return false;
4520 }
4521
4522 /* Determine if this block descends from any block which has been found
4523 * invalid (m_failed_blocks), then mark pindexPrev and any blocks
4524 * between them as failed. For example:
4525 *
4526 * D3
4527 * /
4528 * B2 - C2
4529 * / \
4530 * A D2 - E2 - F2
4531 * \
4532 * B1 - C1 - D1 - E1
4533 *
4534 * In the case that we attempted to reorg from E1 to F2, only to find
4535 * C2 to be invalid, we would mark D2, E2, and F2 as BLOCK_FAILED_CHILD
4536 * but NOT D3 (it was not in any of our candidate sets at the time).
4537 *
4538 * In any case D3 will also be marked as BLOCK_FAILED_CHILD at restart
4539 * in LoadBlockIndex.
4540 */
4541 if (!pindexPrev->IsValid(BlockValidity::SCRIPTS)) {
4542 // The above does not mean "invalid": it checks if the previous
4543 // block hasn't been validated up to BlockValidity::SCRIPTS. This is
4544 // a performance optimization, in the common case of adding a new
4545 // block to the tip, we don't need to iterate over the failed blocks
4546 // list.
4547 for (const CBlockIndex *failedit : m_failed_blocks) {
4548 if (pindexPrev->GetAncestor(failedit->nHeight) == failedit) {
4549 assert(failedit->nStatus.hasFailed());
4551 while (invalid_walk != failedit) {
4552 invalid_walk->nStatus =
4553 invalid_walk->nStatus.withFailedParent();
4555 invalid_walk = invalid_walk->pprev;
4556 }
4558 "header %s has prev block invalid: %s\n",
4559 hash.ToString(), block.hashPrevBlock.ToString());
4560 return state.Invalid(
4562 "bad-prevblk");
4563 }
4564 }
4565 }
4566 }
4567 if (!min_pow_checked) {
4569 "%s: not adding new block header %s, missing anti-dos "
4570 "proof-of-work validation\n",
4571 __func__, hash.ToString());
4573 "too-little-chainwork");
4574 }
4576
4577 if (ppindex) {
4578 *ppindex = pindex;
4579 }
4580
4581 return true;
4582}
4583
4584// Exposed wrapper for AcceptBlockHeader
4586 const std::vector<CBlockHeader> &headers, bool min_pow_checked,
4588 const std::optional<CCheckpointData> &test_checkpoints) {
4590 {
4591 LOCK(cs_main);
4592 for (const CBlockHeader &header : headers) {
4593 // Use a temp pindex instead of ppindex to avoid a const_cast
4594 CBlockIndex *pindex = nullptr;
4596 header, state, &pindex, min_pow_checked, test_checkpoints);
4597 ActiveChainstate().CheckBlockIndex();
4598
4599 if (!accepted) {
4600 return false;
4601 }
4602
4603 if (ppindex) {
4604 *ppindex = pindex;
4605 }
4606 }
4607 }
4608
4610 if (ActiveChainstate().IsInitialBlockDownload() && ppindex &&
4611 *ppindex) {
4613 const int64_t blocks_left{
4614 (GetTime() - last_accepted.GetBlockTime()) /
4616 const double progress{100.0 * last_accepted.nHeight /
4617 (last_accepted.nHeight + blocks_left)};
4618 LogPrintf("Synchronizing blockheaders, height: %d (~%.2f%%)\n",
4619 last_accepted.nHeight, progress);
4620 }
4621 }
4622 return true;
4623}
4624
4626 int64_t height,
4629 const auto &chainstate = ActiveChainstate();
4630 {
4631 LOCK(cs_main);
4632 // Don't report headers presync progress if we already have a
4633 // post-minchainwork header chain.
4634 // This means we lose reporting for potentially legimate, but unlikely,
4635 // deep reorgs, but prevent attackers that spam low-work headers from
4636 // filling our logs.
4637 if (m_best_header->nChainWork >=
4638 UintToArith256(GetConsensus().nMinimumChainWork)) {
4639 return;
4640 }
4641 // Rate limit headers presync updates to 4 per second, as these are not
4642 // subject to DoS protection.
4643 auto now = Now<SteadyMilliseconds>();
4644 if (now < m_last_presync_update + 250ms) {
4645 return;
4646 }
4648 }
4649 bool initial_download = chainstate.IsInitialBlockDownload();
4651 height, timestamp, /*presync=*/true);
4652 if (initial_download) {
4653 const int64_t blocks_left{(GetTime() - timestamp) /
4655 const double progress{100.0 * height / (height + blocks_left)};
4656 LogPrintf("Pre-synchronizing blockheaders, height: %d (~%.2f%%)\n",
4657 height, progress);
4658 }
4659}
4660
4673bool Chainstate::AcceptBlock(const std::shared_ptr<const CBlock> &pblock,
4674 BlockValidationState &state, bool fRequested,
4675 const FlatFilePos *dbp, bool *fNewBlock,
4676 bool min_pow_checked) {
4678
4679 const CBlock &block = *pblock;
4680 if (fNewBlock) {
4681 *fNewBlock = false;
4682 }
4683
4684 CBlockIndex *pindex = nullptr;
4685
4686 bool accepted_header{
4687 m_chainman.AcceptBlockHeader(block, state, &pindex, min_pow_checked)};
4689
4690 if (!accepted_header) {
4691 return false;
4692 }
4693
4694 // Try to process all requested blocks that we don't have, but only
4695 // process an unrequested block if it's new and has enough work to
4696 // advance our tip, and isn't too many blocks ahead.
4697 bool fAlreadyHave = pindex->nStatus.hasData();
4698
4699 // TODO: deal better with return value and error conditions for duplicate
4700 // and unrequested blocks.
4701 if (fAlreadyHave) {
4702 return true;
4703 }
4704
4705 // Compare block header timestamps and received times of the block and the
4706 // chaintip. If they have the same chain height, use these diffs as a
4707 // tie-breaker, attempting to pick the more honestly-mined block.
4708 int64_t newBlockTimeDiff = std::llabs(pindex->GetReceivedTimeDiff());
4710 m_chain.Tip() ? std::llabs(m_chain.Tip()->GetReceivedTimeDiff()) : 0;
4711
4712 bool isSameHeight =
4713 m_chain.Tip() && (pindex->nChainWork == m_chain.Tip()->nChainWork);
4714 if (isSameHeight) {
4715 LogPrintf("Chain tip timestamp-to-received-time difference: hash=%s, "
4716 "diff=%d\n",
4718 LogPrintf("New block timestamp-to-received-time difference: hash=%s, "
4719 "diff=%d\n",
4721 }
4722
4723 bool fHasMoreOrSameWork =
4724 (m_chain.Tip() ? pindex->nChainWork >= m_chain.Tip()->nChainWork
4725 : true);
4726
4727 // Blocks that are too out-of-order needlessly limit the effectiveness of
4728 // pruning, because pruning will not delete block files that contain any
4729 // blocks which are too close in height to the tip. Apply this test
4730 // regardless of whether pruning is enabled; it should generally be safe to
4731 // not process unrequested blocks.
4732 bool fTooFarAhead{pindex->nHeight >
4734
4735 // TODO: Decouple this function from the block download logic by removing
4736 // fRequested
4737 // This requires some new chain data structure to efficiently look up if a
4738 // block is in a chain leading to a candidate for best tip, despite not
4739 // being such a candidate itself.
4740 // Note that this would break the getblockfrompeer RPC
4741
4742 // If we didn't ask for it:
4743 if (!fRequested) {
4744 // This is a previously-processed block that was pruned.
4745 if (pindex->nTx != 0) {
4746 return true;
4747 }
4748
4749 // Don't process less-work chains.
4750 if (!fHasMoreOrSameWork) {
4751 return true;
4752 }
4753
4754 // Block height is too high.
4755 if (fTooFarAhead) {
4756 return true;
4757 }
4758
4759 // Protect against DoS attacks from low-work chains.
4760 // If our tip is behind, a peer could try to send us
4761 // low-work blocks on a fake chain that we would never
4762 // request; don't process these.
4763 if (pindex->nChainWork < m_chainman.MinimumChainWork()) {
4764 return true;
4765 }
4766 }
4767
4768 const CChainParams &params{m_chainman.GetParams()};
4769 const Consensus::Params &consensusParams = params.GetConsensus();
4770
4771 if (!CheckBlock(block, state, consensusParams,
4773 !ContextualCheckBlock(block, state, m_chainman, pindex->pprev)) {
4774 if (state.IsInvalid() &&
4776 pindex->nStatus = pindex->nStatus.withFailed();
4777 m_blockman.m_dirty_blockindex.insert(pindex);
4778 }
4779
4780 return error("%s: %s (block %s)", __func__, state.ToString(),
4781 block.GetHash().ToString());
4782 }
4783
4784 // If connecting the new block would require rewinding more than one block
4785 // from the active chain (i.e., a "deep reorg"), then mark the new block as
4786 // parked. If it has enough work then it will be automatically unparked
4787 // later, during FindMostWorkChain. We mark the block as parked at the very
4788 // last minute so we can make sure everything is ready to be reorged if
4789 // needed.
4790 if (gArgs.GetBoolArg("-parkdeepreorg", true)) {
4791 const CBlockIndex *pindexFork = m_chain.FindFork(pindex);
4792 if (pindexFork && pindexFork->nHeight + 1 < m_chain.Height()) {
4793 LogPrintf("Park block %s as it would cause a deep reorg.\n",
4794 pindex->GetBlockHash().ToString());
4795 pindex->nStatus = pindex->nStatus.withParked();
4796 m_blockman.m_dirty_blockindex.insert(pindex);
4797 }
4798 }
4799
4800 // Header is valid/has work and the merkle tree is good.
4801 // Relay now, but if it does not build on our best tip, let the
4802 // SendMessages loop relay it.
4803 if (!IsInitialBlockDownload() && m_chain.Tip() == pindex->pprev) {
4805 }
4806
4807 // Write block to history file
4808 if (fNewBlock) {
4809 *fNewBlock = true;
4810 }
4811 try {
4813 m_blockman.SaveBlockToDisk(block, pindex->nHeight, m_chain, dbp)};
4814 if (blockPos.IsNull()) {
4815 state.Error(strprintf(
4816 "%s: Failed to find position to write new block to disk",
4817 __func__));
4818 return false;
4819 }
4820 ReceivedBlockTransactions(block, pindex, blockPos);
4821 } catch (const std::runtime_error &e) {
4822 return AbortNode(state, std::string("System error: ") + e.what());
4823 }
4824
4826
4828
4829 return true;
4830}
4831
4833 const std::shared_ptr<const CBlock> &block, bool force_processing,
4834 bool min_pow_checked, bool *new_block,
4837
4838 {
4839 if (new_block) {
4840 *new_block = false;
4841 }
4842
4844
4845 // CheckBlock() does not support multi-threaded block validation
4846 // because CBlock::fChecked can cause data race.
4847 // Therefore, the following critical section must include the
4848 // CheckBlock() call as well.
4849 LOCK(cs_main);
4850
4851 // Skipping AcceptBlock() for CheckBlock() failures means that we will
4852 // never mark a block as invalid if CheckBlock() fails. This is
4853 // protective against consensus failure if there are any unknown form
4854 // s of block malleability that cause CheckBlock() to fail; see e.g.
4855 // CVE-2012-2459 and
4856 // https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2019-February/016697.html.
4857 // Because CheckBlock() is not very expensive, the anti-DoS benefits of
4858 // caching failure (of a definitely-invalid block) are not substantial.
4859 bool ret = CheckBlock(*block, state, this->GetConsensus(),
4861 if (ret) {
4862 // Store to disk
4863 ret = ActiveChainstate().AcceptBlock(block, state, force_processing,
4864 nullptr, new_block,
4866 }
4867
4868 if (!ret) {
4869 GetMainSignals().BlockChecked(*block, state);
4870 return error("%s: AcceptBlock FAILED (%s)", __func__,
4871 state.ToString());
4872 }
4873 }
4874
4876
4877 // Only used to report errors, not invalidity - ignore it
4879 if (!ActiveChainstate().ActivateBestChain(state, block, avalanche)) {
4880 return error("%s: ActivateBestChain failed (%s)", __func__,
4881 state.ToString());
4882 }
4883
4884 return true;
4885}
4886
4889 bool test_accept) {
4892 if (!active_chainstate.GetMempool()) {
4893 TxValidationState state;
4894 state.Invalid(TxValidationResult::TX_NO_MEMPOOL, "no-mempool");
4895 return MempoolAcceptResult::Failure(state);
4896 }
4897 auto result = AcceptToMemoryPool(active_chainstate, tx, GetTime(),
4898 /*bypass_limits=*/false, test_accept);
4899 active_chainstate.GetMempool()->check(
4900 active_chainstate.CoinsTip(), active_chainstate.m_chain.Height() + 1);
4901 return result;
4902}
4903
4905 BlockValidationState &state, const CChainParams &params,
4907 const std::function<NodeClock::time_point()> &adjusted_time_callback,
4910 assert(pindexPrev && pindexPrev == chainstate.m_chain.Tip());
4911 CCoinsViewCache viewNew(&chainstate.CoinsTip());
4912 BlockHash block_hash(block.GetHash());
4913 CBlockIndex indexDummy(block);
4914 indexDummy.pprev = pindexPrev;
4915 indexDummy.nHeight = pindexPrev->nHeight + 1;
4916 indexDummy.phashBlock = &block_hash;
4917
4918 // NOTE: CheckBlockHeader is called by CheckBlock
4919 if (!ContextualCheckBlockHeader(block, state, chainstate.m_blockman,
4920 chainstate.m_chainman, pindexPrev,
4921 adjusted_time_callback())) {
4922 return error("%s: Consensus::ContextualCheckBlockHeader: %s", __func__,
4923 state.ToString());
4924 }
4925
4926 if (!CheckBlock(block, state, params.GetConsensus(), validationOptions)) {
4927 return error("%s: Consensus::CheckBlock: %s", __func__,
4928 state.ToString());
4929 }
4930
4931 if (!ContextualCheckBlock(block, state, chainstate.m_chainman,
4932 pindexPrev)) {
4933 return error("%s: Consensus::ContextualCheckBlock: %s", __func__,
4934 state.ToString());
4935 }
4936
4937 if (!chainstate.ConnectBlock(block, state, &indexDummy, viewNew,
4938 validationOptions, nullptr, true)) {
4939 return false;
4940 }
4941
4942 assert(state.IsValid());
4943 return true;
4944}
4945
4946/* This function is called from the RPC code for pruneblockchain */
4948 int nManualPruneHeight) {
4950 if (active_chainstate.FlushStateToDisk(state, FlushStateMode::NONE,
4952 LogPrintf("%s: failed to flush state (%s)\n", __func__,
4953 state.ToString());
4954 }
4955}
4956
4958 FopenFn mockable_fopen_function) {
4959 if (!m_mempool) {
4960 return;
4961 }
4962 ::LoadMempool(*m_mempool, load_path, *this, mockable_fopen_function);
4964}
4965
4969 // Never called when the coins view is empty
4970 assert(!coins_cache.GetBestBlock().IsNull());
4971 const CBlockIndex *tip = m_chain.Tip();
4972
4973 if (tip && tip->GetBlockHash() == coins_cache.GetBestBlock()) {
4974 return true;
4975 }
4976
4977 // Load pointer to end of best chain
4978 CBlockIndex *pindex =
4979 m_blockman.LookupBlockIndex(coins_cache.GetBestBlock());
4980 if (!pindex) {
4981 return false;
4982 }
4983 m_chain.SetTip(*pindex);
4985
4986 tip = m_chain.Tip();
4987 LogPrintf(
4988 "Loaded best chain: hashBestChain=%s height=%d date=%s progress=%f\n",
4989 tip->GetBlockHash().ToString(), m_chain.Height(),
4990 FormatISO8601DateTime(tip->GetBlockTime()),
4992 return true;
4993}
4994
4996 : m_notifications{notifications} {
4997 m_notifications.progress(_("Verifying blocks…"), 0, false);
4998}
4999
5003
5006 int nCheckDepth) {
5008
5009 const Config &config = chainstate.m_chainman.GetConfig();
5010 const CChainParams &params = config.GetChainParams();
5012
5013 if (chainstate.m_chain.Tip() == nullptr ||
5014 chainstate.m_chain.Tip()->pprev == nullptr) {
5016 }
5017
5018 // Verify blocks in the best chain
5019 if (nCheckDepth <= 0 || nCheckDepth > chainstate.m_chain.Height()) {
5020 nCheckDepth = chainstate.m_chain.Height();
5021 }
5022
5023 nCheckLevel = std::max(0, std::min(4, nCheckLevel));
5024 LogPrintf("Verifying last %i blocks at level %i\n", nCheckDepth,
5025 nCheckLevel);
5026
5027 CCoinsViewCache coins(&coinsview);
5028 CBlockIndex *pindex;
5029 CBlockIndex *pindexFailure = nullptr;
5030 int nGoodTransactions = 0;
5032 int reportDone = 0;
5033 bool skipped_no_block_data{false};
5034 bool skipped_l3_checks{false};
5035 LogPrintf("Verification progress: 0%%\n");
5036
5037 const bool is_snapshot_cs{!chainstate.m_from_snapshot_blockhash};
5038
5039 for (pindex = chainstate.m_chain.Tip(); pindex && pindex->pprev;
5040 pindex = pindex->pprev) {
5041 const int percentageDone = std::max(
5042 1, std::min(99, (int)(((double)(chainstate.m_chain.Height() -
5043 pindex->nHeight)) /
5044 (double)nCheckDepth *
5045 (nCheckLevel >= 4 ? 50 : 100))));
5046 if (reportDone < percentageDone / 10) {
5047 // report every 10% step
5048 LogPrintf("Verification progress: %d%%\n", percentageDone);
5050 }
5051
5052 m_notifications.progress(_("Verifying blocks…"), percentageDone, false);
5053 if (pindex->nHeight <= chainstate.m_chain.Height() - nCheckDepth) {
5054 break;
5055 }
5056
5057 if ((chainstate.m_blockman.IsPruneMode() || is_snapshot_cs) &&
5058 !pindex->nStatus.hasData()) {
5059 // If pruning or running under an assumeutxo snapshot, only go
5060 // back as far as we have data.
5061 LogPrintf("VerifyDB(): block verification stopping at height %d "
5062 "(no data). This could be due to pruning or use of an "
5063 "assumeutxo snapshot.\n",
5064 pindex->nHeight);
5065 skipped_no_block_data = true;
5066 break;
5067 }
5068
5069 CBlock block;
5070
5071 // check level 0: read from disk
5072 if (!chainstate.m_blockman.ReadBlockFromDisk(block, *pindex)) {
5073 LogPrintf(
5074 "Verification error: ReadBlockFromDisk failed at %d, hash=%s\n",
5075 pindex->nHeight, pindex->GetBlockHash().ToString());
5077 }
5078
5079 // check level 1: verify block validity
5080 if (nCheckLevel >= 1 && !CheckBlock(block, state, consensusParams,
5081 BlockValidationOptions(config))) {
5082 LogPrintf(
5083 "Verification error: found bad block at %d, hash=%s (%s)\n",
5084 pindex->nHeight, pindex->GetBlockHash().ToString(),
5085 state.ToString());
5087 }
5088
5089 // check level 2: verify undo validity
5090 if (nCheckLevel >= 2 && pindex) {
5092 if (!pindex->GetUndoPos().IsNull()) {
5093 if (!chainstate.m_blockman.UndoReadFromDisk(undo, *pindex)) {
5094 LogPrintf("Verification error: found bad undo data at %d, "
5095 "hash=%s\n",
5096 pindex->nHeight,
5097 pindex->GetBlockHash().ToString());
5099 }
5100 }
5101 }
5102 // check level 3: check for inconsistencies during memory-only
5103 // disconnect of tip blocks
5104 size_t curr_coins_usage = coins.DynamicMemoryUsage() +
5105 chainstate.CoinsTip().DynamicMemoryUsage();
5106
5107 if (nCheckLevel >= 3) {
5108 if (curr_coins_usage <= chainstate.m_coinstip_cache_size_bytes) {
5109 assert(coins.GetBestBlock() == pindex->GetBlockHash());
5111 chainstate.DisconnectBlock(block, pindex, coins);
5113 LogPrintf("Verification error: irrecoverable inconsistency "
5114 "in block data at %d, hash=%s\n",
5115 pindex->nHeight,
5116 pindex->GetBlockHash().ToString());
5118 }
5121 pindexFailure = pindex;
5122 } else {
5123 nGoodTransactions += block.vtx.size();
5124 }
5125 } else {
5126 skipped_l3_checks = true;
5127 }
5128 }
5129
5130 if (ShutdownRequested()) {
5132 }
5133 }
5134
5135 if (pindexFailure) {
5136 LogPrintf("Verification error: coin database inconsistencies found "
5137 "(last %i blocks, %i good transactions before that)\n",
5138 chainstate.m_chain.Height() - pindexFailure->nHeight + 1,
5141 }
5142 if (skipped_l3_checks) {
5143 LogPrintf("Skipped verification of level >=3 (insufficient database "
5144 "cache size). Consider increasing -dbcache.\n");
5145 }
5146
5147 // store block count as we move pindex at check level >= 4
5148 int block_count = chainstate.m_chain.Height() - pindex->nHeight;
5149
5150 // check level 4: try reconnecting blocks
5151 if (nCheckLevel >= 4 && !skipped_l3_checks) {
5152 while (pindex != chainstate.m_chain.Tip()) {
5153 const int percentageDone = std::max(
5154 1, std::min(99, 100 - int(double(chainstate.m_chain.Height() -
5155 pindex->nHeight) /
5156 double(nCheckDepth) * 50)));
5157 if (reportDone < percentageDone / 10) {
5158 // report every 10% step
5159 LogPrintf("Verification progress: %d%%\n", percentageDone);
5161 }
5162 m_notifications.progress(_("Verifying blocks…"), percentageDone,
5163 false);
5164 pindex = chainstate.m_chain.Next(pindex);
5165 CBlock block;
5166 if (!chainstate.m_blockman.ReadBlockFromDisk(block, *pindex)) {
5167 LogPrintf("Verification error: ReadBlockFromDisk failed at %d, "
5168 "hash=%s\n",
5169 pindex->nHeight, pindex->GetBlockHash().ToString());
5171 }
5172 if (!chainstate.ConnectBlock(block, state, pindex, coins,
5173 BlockValidationOptions(config))) {
5174 LogPrintf("Verification error: found unconnectable block at "
5175 "%d, hash=%s (%s)\n",
5176 pindex->nHeight, pindex->GetBlockHash().ToString(),
5177 state.ToString());
5179 }
5180 if (ShutdownRequested()) {
5182 }
5183 }
5184 }
5185
5186 LogPrintf("Verification: No coin database inconsistencies in last %i "
5187 "blocks (%i transactions)\n",
5189
5190 if (skipped_l3_checks) {
5192 }
5195 }
5197}
5198
5206 // TODO: merge with ConnectBlock
5207 CBlock block;
5208 if (!m_blockman.ReadBlockFromDisk(block, *pindex)) {
5209 return error("ReplayBlock(): ReadBlockFromDisk failed at %d, hash=%s",
5210 pindex->nHeight, pindex->GetBlockHash().ToString());
5211 }
5212
5213 for (const CTransactionRef &tx : block.vtx) {
5214 // Pass check = true as every addition may be an overwrite.
5215 AddCoins(view, *tx, pindex->nHeight, true);
5216 }
5217
5218 for (const CTransactionRef &tx : block.vtx) {
5219 if (tx->IsCoinBase()) {
5220 continue;
5221 }
5222
5223 for (const CTxIn &txin : tx->vin) {
5224 view.SpendCoin(txin.prevout);
5225 }
5226 }
5227
5228 return true;
5229}
5230
5232 LOCK(cs_main);
5233
5234 CCoinsView &db = this->CoinsDB();
5235 CCoinsViewCache cache(&db);
5236
5237 std::vector<BlockHash> hashHeads = db.GetHeadBlocks();
5238 if (hashHeads.empty()) {
5239 // We're already in a consistent state.
5240 return true;
5241 }
5242 if (hashHeads.size() != 2) {
5243 return error("ReplayBlocks(): unknown inconsistent state");
5244 }
5245
5246 m_chainman.GetNotifications().progress(_("Replaying blocks…"), 0, false);
5247 LogPrintf("Replaying blocks\n");
5248
5249 // Old tip during the interrupted flush.
5250 const CBlockIndex *pindexOld = nullptr;
5251 // New tip during the interrupted flush.
5252 const CBlockIndex *pindexNew;
5253 // Latest block common to both the old and the new tip.
5254 const CBlockIndex *pindexFork = nullptr;
5255
5256 if (m_blockman.m_block_index.count(hashHeads[0]) == 0) {
5257 return error(
5258 "ReplayBlocks(): reorganization to unknown block requested");
5259 }
5260
5261 pindexNew = &(m_blockman.m_block_index[hashHeads[0]]);
5262
5263 if (!hashHeads[1].IsNull()) {
5264 // The old tip is allowed to be 0, indicating it's the first flush.
5265 if (m_blockman.m_block_index.count(hashHeads[1]) == 0) {
5266 return error(
5267 "ReplayBlocks(): reorganization from unknown block requested");
5268 }
5269
5270 pindexOld = &(m_blockman.m_block_index[hashHeads[1]]);
5272 assert(pindexFork != nullptr);
5273 }
5274
5275 // Rollback along the old branch.
5276 while (pindexOld != pindexFork) {
5277 if (pindexOld->nHeight > 0) {
5278 // Never disconnect the genesis block.
5279 CBlock block;
5280 if (!m_blockman.ReadBlockFromDisk(block, *pindexOld)) {
5281 return error("RollbackBlock(): ReadBlockFromDisk() failed at "
5282 "%d, hash=%s",
5283 pindexOld->nHeight,
5284 pindexOld->GetBlockHash().ToString());
5285 }
5286
5287 LogPrintf("Rolling back %s (%i)\n",
5288 pindexOld->GetBlockHash().ToString(), pindexOld->nHeight);
5291 return error(
5292 "RollbackBlock(): DisconnectBlock failed at %d, hash=%s",
5293 pindexOld->nHeight, pindexOld->GetBlockHash().ToString());
5294 }
5295
5296 // If DisconnectResult::UNCLEAN is returned, it means a non-existing
5297 // UTXO was deleted, or an existing UTXO was overwritten. It
5298 // corresponds to cases where the block-to-be-disconnect never had
5299 // all its operations applied to the UTXO set. However, as both
5300 // writing a UTXO and deleting a UTXO are idempotent operations, the
5301 // result is still a version of the UTXO set with the effects of
5302 // that block undone.
5303 }
5304 pindexOld = pindexOld->pprev;
5305 }
5306
5307 // Roll forward from the forking point to the new tip.
5308 int nForkHeight = pindexFork ? pindexFork->nHeight : 0;
5309 for (int nHeight = nForkHeight + 1; nHeight <= pindexNew->nHeight;
5310 ++nHeight) {
5311 const CBlockIndex &pindex{*Assert(pindexNew->GetAncestor(nHeight))};
5312 LogPrintf("Rolling forward %s (%i)\n", pindex.GetBlockHash().ToString(),
5313 nHeight);
5315 _("Replaying blocks…"),
5316 (int)((nHeight - nForkHeight) * 100.0 /
5317 (pindexNew->nHeight - nForkHeight)),
5318 false);
5319 if (!RollforwardBlock(&pindex, cache)) {
5320 return false;
5321 }
5322 }
5323
5324 cache.SetBestBlock(pindexNew->GetBlockHash());
5325 cache.Flush();
5327 return true;
5328}
5329
5330// May NOT be used after any connections are up as much of the peer-processing
5331// logic assumes a consistent block index state
5334 nBlockSequenceId = 1;
5335 m_best_fork_tip = nullptr;
5336 m_best_fork_base = nullptr;
5338}
5339
5342
5344 return false;
5345 }
5346
5347 // Dump enough headers for RTT computation, with a few extras in case a
5348 // reorg occurs.
5349 const uint64_t numHeaders{20};
5350
5351 try {
5352 const fs::path filePathTmp = filePath + ".new";
5354 if (!filestr) {
5355 return false;
5356 }
5357
5359 file << HEADERS_TIME_VERSION;
5360 file << numHeaders;
5361
5362 const CBlockIndex *index = ActiveTip();
5363 bool missingIndex{false};
5364 for (uint64_t i = 0; i < numHeaders; i++) {
5365 if (!index) {
5366 LogPrintf("Missing block index, stopping the headers time "
5367 "dumping after %d blocks.\n",
5368 i);
5369 missingIndex = true;
5370 break;
5371 }
5372
5373 file << index->GetBlockHash();
5374 file << index->GetHeaderReceivedTime();
5375
5376 index = index->pprev;
5377 }
5378
5379 if (!FileCommit(file.Get())) {
5380 throw std::runtime_error(strprintf("Failed to commit to file %s",
5381 PathToString(filePathTmp)));
5382 }
5383 file.fclose();
5384
5385 if (missingIndex) {
5386 fs::remove(filePathTmp);
5387 return false;
5388 }
5389
5391 throw std::runtime_error(strprintf("Rename failed from %s to %s",
5392 PathToString(filePathTmp),
5393 PathToString(filePath)));
5394 }
5395 } catch (const std::exception &e) {
5396 LogPrintf("Failed to dump the headers time: %s.\n", e.what());
5397 return false;
5398 }
5399
5400 LogPrintf("Successfully dumped the last %d headers time to %s.\n",
5401 numHeaders, PathToString(filePath));
5402
5403 return true;
5404}
5405
5408
5410 return false;
5411 }
5412
5415 if (file.IsNull()) {
5416 LogPrintf("Failed to open header times from disk, skipping.\n");
5417 return false;
5418 }
5419
5420 try {
5421 uint64_t version;
5422 file >> version;
5423
5424 if (version != HEADERS_TIME_VERSION) {
5425 LogPrintf("Unsupported header times file version, skipping.\n");
5426 return false;
5427 }
5428
5430 file >> numBlocks;
5431
5432 for (uint64_t i = 0; i < numBlocks; i++) {
5433 BlockHash blockHash;
5435
5436 file >> blockHash;
5437 file >> receiveTime;
5438
5439 CBlockIndex *index = m_blockman.LookupBlockIndex(blockHash);
5440 if (!index) {
5441 LogPrintf("Missing index for block %s, stopping the headers "
5442 "time loading after %d blocks.\n",
5443 blockHash.ToString(), i);
5444 return false;
5445 }
5446
5447 index->nTimeReceived = receiveTime;
5448 }
5449 } catch (const std::exception &e) {
5450 LogPrintf("Failed to read the headers time file data on disk: %s.\n",
5451 e.what());
5452 return false;
5453 }
5454
5455 return true;
5456}
5457
5460 // Load block index from databases
5461 bool needs_init = fReindex;
5462 if (!fReindex) {
5463 bool ret = m_blockman.LoadBlockIndexDB();
5464 if (!ret) {
5465 return false;
5466 }
5467
5468 m_blockman.ScanAndUnlinkAlreadyPrunedFiles();
5469
5470 std::vector<CBlockIndex *> vSortedByHeight{
5471 m_blockman.GetAllBlockIndices()};
5472 std::sort(vSortedByHeight.begin(), vSortedByHeight.end(),
5474
5475 // Find start of assumed-valid region.
5476 int first_assumed_valid_height = std::numeric_limits<int>::max();
5477
5478 for (const CBlockIndex *block : vSortedByHeight) {
5479 if (block->IsAssumedValid()) {
5480 auto chainstates = GetAll();
5481
5482 // If we encounter an assumed-valid block index entry, ensure
5483 // that we have one chainstate that tolerates assumed-valid
5484 // entries and another that does not (i.e. the background
5485 // validation chainstate), since assumed-valid entries should
5486 // always be pending validation by a fully-validated chainstate.
5487 auto any_chain = [&](auto fnc) {
5488 return std::any_of(chainstates.cbegin(), chainstates.cend(),
5489 fnc);
5490 };
5491 assert(any_chain([](auto chainstate) {
5492 return chainstate->reliesOnAssumedValid();
5493 }));
5494 assert(any_chain([](auto chainstate) {
5495 return !chainstate->reliesOnAssumedValid();
5496 }));
5497
5498 first_assumed_valid_height = block->nHeight;
5499 LogPrintf("Saw first assumedvalid block at height %d (%s)\n",
5501 break;
5502 }
5503 }
5504
5505 for (CBlockIndex *pindex : vSortedByHeight) {
5506 if (ShutdownRequested()) {
5507 return false;
5508 }
5509 if (pindex->IsAssumedValid() ||
5511 (pindex->HaveTxsDownloaded() || pindex->pprev == nullptr))) {
5512 // Fill each chainstate's block candidate set. Only add
5513 // assumed-valid blocks to the tip candidate set if the
5514 // chainstate is allowed to rely on assumed-valid blocks.
5515 //
5516 // If all setBlockIndexCandidates contained the assumed-valid
5517 // blocks, the background chainstate's ActivateBestChain() call
5518 // would add assumed-valid blocks to the chain (based on how
5519 // FindMostWorkChain() works). Obviously we don't want this
5520 // since the purpose of the background validation chain is to
5521 // validate assumed-valid blocks.
5522 //
5523 // Note: This is considering all blocks whose height is greater
5524 // or equal to the first assumed-valid block to be assumed-valid
5525 // blocks, and excluding them from the background chainstate's
5526 // setBlockIndexCandidates set. This does mean that some blocks
5527 // which are not technically assumed-valid (later blocks on a
5528 // fork beginning before the first assumed-valid block) might
5529 // not get added to the background chainstate, but this is ok,
5530 // because they will still be attached to the active chainstate
5531 // if they actually contain more work.
5532 //
5533 // Instead of this height-based approach, an earlier attempt was
5534 // made at detecting "holistically" whether the block index
5535 // under consideration relied on an assumed-valid ancestor, but
5536 // this proved to be too slow to be practical.
5537 for (Chainstate *chainstate : GetAll()) {
5538 if (chainstate->reliesOnAssumedValid() ||
5540 chainstate->setBlockIndexCandidates.insert(pindex);
5541 }
5542 }
5543 }
5544
5545 if (pindex->nStatus.isInvalid() &&
5546 (!m_best_invalid ||
5547 pindex->nChainWork > m_best_invalid->nChainWork)) {
5548 m_best_invalid = pindex;
5549 }
5550
5551 if (pindex->nStatus.isOnParkedChain() &&
5552 (!m_best_parked ||
5553 pindex->nChainWork > m_best_parked->nChainWork)) {
5554 m_best_parked = pindex;
5555 }
5556
5557 if (pindex->IsValid(BlockValidity::TREE) &&
5558 (m_best_header == nullptr ||
5560 m_best_header = pindex;
5561 }
5562 }
5563
5564 needs_init = m_blockman.m_block_index.empty();
5565 }
5566
5567 if (needs_init) {
5568 // Everything here is for *new* reindex/DBs. Thus, though
5569 // LoadBlockIndexDB may have set fReindex if we shut down
5570 // mid-reindex previously, we don't check fReindex and
5571 // instead only check it prior to LoadBlockIndexDB to set
5572 // needs_init.
5573
5574 LogPrintf("Initializing databases...\n");
5575 }
5576 return true;
5577}
5578
5580 LOCK(cs_main);
5581
5582 const CChainParams &params{m_chainman.GetParams()};
5583
5584 // Check whether we're already initialized by checking for genesis in
5585 // m_blockman.m_block_index. Note that we can't use m_chain here, since it
5586 // is set based on the coins db, not the block index db, which is the only
5587 // thing loaded at this point.
5588 if (m_blockman.m_block_index.count(params.GenesisBlock().GetHash())) {
5589 return true;
5590 }
5591
5592 try {
5593 const CBlock &block = params.GenesisBlock();
5595 m_blockman.SaveBlockToDisk(block, 0, m_chain, nullptr)};
5596 if (blockPos.IsNull()) {
5597 return error("%s: writing genesis block to disk failed", __func__);
5598 }
5599 CBlockIndex *pindex =
5600 m_blockman.AddToBlockIndex(block, m_chainman.m_best_header);
5601 ReceivedBlockTransactions(block, pindex, blockPos);
5602 } catch (const std::runtime_error &e) {
5603 return error("%s: failed to write genesis block: %s", __func__,
5604 e.what());
5605 }
5606
5607 return true;
5608}
5609
5612 std::multimap<BlockHash, FlatFilePos> *blocks_with_unknown_parent,
5615
5616 // Either both should be specified (-reindex), or neither (-loadblock).
5618
5620 const CChainParams &params{m_chainman.GetParams()};
5621
5622 int nLoaded = 0;
5623 try {
5624 // This takes over fileIn and calls fclose() on it in the CBufferedFile
5625 // destructor. Make sure we have at least 2*MAX_TX_SIZE space in there
5626 // so any transaction can fit in the buffer.
5629 // nRewind indicates where to resume scanning in case something goes
5630 // wrong, such as a block fails to deserialize.
5631 uint64_t nRewind = blkdat.GetPos();
5632 while (!blkdat.eof()) {
5633 if (ShutdownRequested()) {
5634 return;
5635 }
5636
5637 blkdat.SetPos(nRewind);
5638 // Start one byte further next time, in case of failure.
5639 nRewind++;
5640 // Remove former limit.
5641 blkdat.SetLimit();
5642 unsigned int nSize = 0;
5643 try {
5644 // Locate a header.
5646 blkdat.FindByte(std::byte(params.DiskMagic()[0]));
5647 nRewind = blkdat.GetPos() + 1;
5648 blkdat >> buf;
5649 if (memcmp(buf, params.DiskMagic().data(),
5651 continue;
5652 }
5653
5654 // Read size.
5655 blkdat >> nSize;
5656 if (nSize < 80) {
5657 continue;
5658 }
5659 } catch (const std::exception &) {
5660 // No valid block header found; don't complain.
5661 // (this happens at the end of every blk.dat file)
5662 break;
5663 }
5664
5665 try {
5666 // read block header
5667 const uint64_t nBlockPos{blkdat.GetPos()};
5668 if (dbp) {
5669 dbp->nPos = nBlockPos;
5670 }
5671 blkdat.SetLimit(nBlockPos + nSize);
5672 CBlockHeader header;
5673 blkdat >> header;
5674 const BlockHash hash{header.GetHash()};
5675 // Skip the rest of this block (this may read from disk
5676 // into memory); position to the marker before the next block,
5677 // but it's still possible to rewind to the start of the
5678 // current block (without a disk read).
5679 nRewind = nBlockPos + nSize;
5680 blkdat.SkipTo(nRewind);
5681
5682 // needs to remain available after the cs_main lock is released
5683 // to avoid duplicate reads from disk
5684 std::shared_ptr<CBlock> pblock{};
5685
5686 {
5687 LOCK(cs_main);
5688 // detect out of order blocks, and store them for later
5689 if (hash != params.GetConsensus().hashGenesisBlock &&
5691 LogPrint(
5693 "%s: Out of order block %s, parent %s not known\n",
5694 __func__, hash.ToString(),
5695 header.hashPrevBlock.ToString());
5698 header.hashPrevBlock, *dbp);
5699 }
5700 continue;
5701 }
5702
5703 // process in case the block isn't known yet
5704 const CBlockIndex *pindex =
5706 if (!pindex || !pindex->nStatus.hasData()) {
5707 // This block can be processed immediately; rewind to
5708 // its start, read and deserialize it.
5709 blkdat.SetPos(nBlockPos);
5710 pblock = std::make_shared<CBlock>();
5711 blkdat >> *pblock;
5712 nRewind = blkdat.GetPos();
5713
5715 if (AcceptBlock(pblock, state, true, dbp, nullptr,
5716 true)) {
5717 nLoaded++;
5718 }
5719 if (state.IsError()) {
5720 break;
5721 }
5722 } else if (hash != params.GetConsensus().hashGenesisBlock &&
5723 pindex->nHeight % 1000 == 0) {
5724 LogPrint(
5726 "Block Import: already had block %s at height %d\n",
5727 hash.ToString(), pindex->nHeight);
5728 }
5729 }
5730
5731 // Activate the genesis block so normal node progress can
5732 // continue
5733 if (hash == params.GetConsensus().hashGenesisBlock) {
5735 if (!ActivateBestChain(state, nullptr, avalanche)) {
5736 break;
5737 }
5738 }
5739
5740 if (m_blockman.IsPruneMode() && !fReindex && pblock) {
5741 // Must update the tip for pruning to work while importing
5742 // with -loadblock. This is a tradeoff to conserve disk
5743 // space at the expense of time spent updating the tip to be
5744 // able to prune. Otherwise, ActivateBestChain won't be
5745 // called by the import process until after all of the block
5746 // files are loaded. ActivateBestChain can be called by
5747 // concurrent network message processing, but that is not
5748 // reliable for the purpose of pruning while importing.
5750 if (!ActivateBestChain(state, pblock, avalanche)) {
5752 "failed to activate chain (%s)\n",
5753 state.ToString());
5754 break;
5755 }
5756 }
5757
5758 NotifyHeaderTip(*this);
5759
5761 continue;
5762 }
5763
5764 // Recursively process earlier encountered successors of this
5765 // block
5766 std::deque<BlockHash> queue;
5767 queue.push_back(hash);
5768 while (!queue.empty()) {
5769 BlockHash head = queue.front();
5770 queue.pop_front();
5771 auto range = blocks_with_unknown_parent->equal_range(head);
5772 while (range.first != range.second) {
5773 std::multimap<BlockHash, FlatFilePos>::iterator it =
5774 range.first;
5775 std::shared_ptr<CBlock> pblockrecursive =
5776 std::make_shared<CBlock>();
5778 it->second)) {
5779 LogPrint(
5781 "%s: Processing out of order child %s of %s\n",
5782 __func__, pblockrecursive->GetHash().ToString(),
5783 head.ToString());
5784 LOCK(cs_main);
5786 if (AcceptBlock(pblockrecursive, dummy, true,
5787 &it->second, nullptr, true)) {
5788 nLoaded++;
5789 queue.push_back(pblockrecursive->GetHash());
5790 }
5791 }
5792 range.first++;
5793 blocks_with_unknown_parent->erase(it);
5794 NotifyHeaderTip(*this);
5795 }
5796 }
5797 } catch (const std::exception &e) {
5798 // Historical bugs added extra data to the block files that does
5799 // not deserialize cleanly. Commonly this data is between
5800 // readable blocks, but it does not really matter. Such data is
5801 // not fatal to the import process. The code that reads the
5802 // block files deals with invalid data by simply ignoring it. It
5803 // continues to search for the next {4 byte magic message start
5804 // bytes + 4 byte length + block} that does deserialize cleanly
5805 // and passes all of the other block validation checks dealing
5806 // with POW and the merkle root, etc... We merely note with this
5807 // informational log message when unexpected data is
5808 // encountered. We could also be experiencing a storage system
5809 // read error, or a read of a previous bad write. These are
5810 // possible, but less likely scenarios. We don't have enough
5811 // information to tell a difference here. The reindex process is
5812 // not the place to attempt to clean and/or compact the block
5813 // files. If so desired, a studious node operator may use
5814 // knowledge of the fact that the block files are not entirely
5815 // pristine in order to prepare a set of pristine, and perhaps
5816 // ordered, block files for later reindexing.
5818 "%s: unexpected data at file offset 0x%x - %s. "
5819 "continuing\n",
5820 __func__, (nRewind - 1), e.what());
5821 }
5822 }
5823 } catch (const std::runtime_error &e) {
5824 AbortNode(std::string("System error: ") + e.what());
5825 }
5826
5827 LogPrintf("Loaded %i blocks from external file in %dms\n", nLoaded,
5828 GetTimeMillis() - nStart);
5829}
5830
5833 return;
5834 }
5835
5836 LOCK(cs_main);
5837
5838 // During a reindex, we read the genesis block and call CheckBlockIndex
5839 // before ActivateBestChain, so we have the genesis block in
5840 // m_blockman.m_block_index but no active chain. (A few of the tests when
5841 // iterating the block tree require that m_chain has been initialized.)
5842 if (m_chain.Height() < 0) {
5843 assert(m_blockman.m_block_index.size() <= 1);
5844 return;
5845 }
5846
5847 // Build forward-pointing map of the entire block tree.
5848 std::multimap<CBlockIndex *, CBlockIndex *> forward;
5849 for (auto &[_, block_index] : m_blockman.m_block_index) {
5850 forward.emplace(block_index.pprev, &block_index);
5851 }
5852
5853 assert(forward.size() == m_blockman.m_block_index.size());
5854
5855 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
5856 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
5857 rangeGenesis = forward.equal_range(nullptr);
5858 CBlockIndex *pindex = rangeGenesis.first->second;
5859 rangeGenesis.first++;
5860 // There is only one index entry with parent nullptr.
5861 assert(rangeGenesis.first == rangeGenesis.second);
5862
5863 // Iterate over the entire block tree, using depth-first search.
5864 // Along the way, remember whether there are blocks on the path from genesis
5865 // block being explored which are the first to have certain properties.
5866 size_t nNodes = 0;
5867 int nHeight = 0;
5868 // Oldest ancestor of pindex which is invalid.
5870 // Oldest ancestor of pindex which is parked.
5871 CBlockIndex *pindexFirstParked = nullptr;
5872 // Oldest ancestor of pindex which does not have data available.
5874 // Oldest ancestor of pindex for which nTx == 0.
5876 // Oldest ancestor of pindex which does not have BLOCK_VALID_TREE
5877 // (regardless of being valid or not).
5879 // Oldest ancestor of pindex which does not have BLOCK_VALID_TRANSACTIONS
5880 // (regardless of being valid or not).
5882 // Oldest ancestor of pindex which does not have BLOCK_VALID_CHAIN
5883 // (regardless of being valid or not).
5885 // Oldest ancestor of pindex which does not have BLOCK_VALID_SCRIPTS
5886 // (regardless of being valid or not).
5888 while (pindex != nullptr) {
5889 nNodes++;
5890 if (pindexFirstInvalid == nullptr && pindex->nStatus.hasFailed()) {
5891 pindexFirstInvalid = pindex;
5892 }
5893 if (pindexFirstParked == nullptr && pindex->nStatus.isParked()) {
5894 pindexFirstParked = pindex;
5895 }
5896 // Assumed-valid index entries will not have data since we haven't
5897 // downloaded the full block yet.
5898 if (pindexFirstMissing == nullptr && !pindex->nStatus.hasData() &&
5899 !pindex->IsAssumedValid()) {
5900 pindexFirstMissing = pindex;
5901 }
5902 if (pindexFirstNeverProcessed == nullptr && pindex->nTx == 0) {
5904 }
5905 if (pindex->pprev != nullptr && pindexFirstNotTreeValid == nullptr &&
5906 pindex->nStatus.getValidity() < BlockValidity::TREE) {
5907 pindexFirstNotTreeValid = pindex;
5908 }
5909 if (pindex->pprev != nullptr && !pindex->IsAssumedValid()) {
5910 if (pindexFirstNotTransactionsValid == nullptr &&
5911 pindex->nStatus.getValidity() < BlockValidity::TRANSACTIONS) {
5913 }
5914 if (pindexFirstNotChainValid == nullptr &&
5915 pindex->nStatus.getValidity() < BlockValidity::CHAIN) {
5916 pindexFirstNotChainValid = pindex;
5917 }
5918 if (pindexFirstNotScriptsValid == nullptr &&
5919 pindex->nStatus.getValidity() < BlockValidity::SCRIPTS) {
5921 }
5922 }
5923
5924 // Begin: actual consistency checks.
5925 if (pindex->pprev == nullptr) {
5926 // Genesis block checks.
5927 // Genesis block's hash must match.
5928 assert(pindex->GetBlockHash() ==
5930 // The current active chain's genesis block must be this block.
5931 assert(pindex == m_chain.Genesis());
5932 }
5933 if (!pindex->HaveTxsDownloaded()) {
5934 // nSequenceId can't be set positive for blocks that aren't linked
5935 // (negative is used for preciousblock)
5936 assert(pindex->nSequenceId <= 0);
5937 }
5938 // VALID_TRANSACTIONS is equivalent to nTx > 0 for all nodes (whether or
5939 // not pruning has occurred). HAVE_DATA is only equivalent to nTx > 0
5940 // (or VALID_TRANSACTIONS) if no pruning has occurred.
5941 // Unless these indexes are assumed valid and pending block download on
5942 // a background chainstate.
5943 if (!m_blockman.m_have_pruned && !pindex->IsAssumedValid()) {
5944 // If we've never pruned, then HAVE_DATA should be equivalent to nTx
5945 // > 0
5946 assert(pindex->nStatus.hasData() == (pindex->nTx > 0));
5948 } else if (pindex->nStatus.hasData()) {
5949 // If we have pruned, then we can only say that HAVE_DATA implies
5950 // nTx > 0
5951 assert(pindex->nTx > 0);
5952 }
5953 if (pindex->nStatus.hasUndo()) {
5954 assert(pindex->nStatus.hasData());
5955 }
5956 if (pindex->IsAssumedValid()) {
5957 // Assumed-valid blocks should have some nTx value.
5958 assert(pindex->nTx > 0);
5959 // Assumed-valid blocks should connect to the main chain.
5960 assert(pindex->nStatus.getValidity() >= BlockValidity::TREE);
5961 } else {
5962 // Otherwise there should only be an nTx value if we have
5963 // actually seen a block's transactions.
5964 // This is pruning-independent.
5965 assert((pindex->nStatus.getValidity() >=
5966 BlockValidity::TRANSACTIONS) == (pindex->nTx > 0));
5967 }
5968 // All parents having had data (at some point) is equivalent to all
5969 // parents being VALID_TRANSACTIONS, which is equivalent to
5970 // HaveTxsDownloaded(). All parents having had data (at some point) is
5971 // equivalent to all parents being VALID_TRANSACTIONS, which is
5972 // equivalent to HaveTxsDownloaded().
5973 assert((pindexFirstNeverProcessed == nullptr) ==
5974 (pindex->HaveTxsDownloaded()));
5976 (pindex->HaveTxsDownloaded()));
5977 // nHeight must be consistent.
5978 assert(pindex->nHeight == nHeight);
5979 // For every block except the genesis block, the chainwork must be
5980 // larger than the parent's.
5981 assert(pindex->pprev == nullptr ||
5982 pindex->nChainWork >= pindex->pprev->nChainWork);
5983 // The pskip pointer must point back for all but the first 2 blocks.
5984 assert(nHeight < 2 ||
5985 (pindex->pskip && (pindex->pskip->nHeight < nHeight)));
5986 // All m_blockman.m_block_index entries must at least be TREE valid
5987 assert(pindexFirstNotTreeValid == nullptr);
5988 if (pindex->nStatus.getValidity() >= BlockValidity::TREE) {
5989 // TREE valid implies all parents are TREE valid
5990 assert(pindexFirstNotTreeValid == nullptr);
5991 }
5992 if (pindex->nStatus.getValidity() >= BlockValidity::CHAIN) {
5993 // CHAIN valid implies all parents are CHAIN valid
5994 assert(pindexFirstNotChainValid == nullptr);
5995 }
5996 if (pindex->nStatus.getValidity() >= BlockValidity::SCRIPTS) {
5997 // SCRIPTS valid implies all parents are SCRIPTS valid
5999 }
6000 if (pindexFirstInvalid == nullptr) {
6001 // Checks for not-invalid blocks.
6002 // The failed mask cannot be set for blocks without invalid parents.
6003 assert(!pindex->nStatus.isInvalid());
6004 }
6005 if (pindexFirstParked == nullptr) {
6006 // Checks for not-parked blocks.
6007 // The parked mask cannot be set for blocks without parked parents.
6008 // (i.e., hasParkedParent only if an ancestor is properly parked).
6009 assert(!pindex->nStatus.isOnParkedChain());
6010 }
6011 if (!CBlockIndexWorkComparator()(pindex, m_chain.Tip()) &&
6012 pindexFirstNeverProcessed == nullptr) {
6013 if (pindexFirstInvalid == nullptr) {
6014 // Don't perform this check for the background chainstate since
6015 // its setBlockIndexCandidates shouldn't have some entries (i.e.
6016 // those past the snapshot block) which do exist in the block
6017 // index for the active chainstate.
6018 if (this == &m_chainman.ActiveChainstate()) {
6019 // If this block sorts at least as good as the current tip
6020 // and is valid and we have all data for its parents, it
6021 // must be in setBlockIndexCandidates or be parked.
6022 if (pindexFirstMissing == nullptr) {
6023 assert(pindex->nStatus.isOnParkedChain() ||
6024 setBlockIndexCandidates.count(pindex));
6025 }
6026 // m_chain.Tip() must also be there even if some data has
6027 // been pruned.
6028 if (pindex == m_chain.Tip()) {
6029 assert(setBlockIndexCandidates.count(pindex));
6030 }
6031 }
6032 // If some parent is missing, then it could be that this block
6033 // was in setBlockIndexCandidates but had to be removed because
6034 // of the missing data. In this case it must be in
6035 // m_blocks_unlinked -- see test below.
6036 }
6037 } else {
6038 // If this block sorts worse than the current tip or some ancestor's
6039 // block has never been seen, it cannot be in
6040 // setBlockIndexCandidates.
6041 assert(setBlockIndexCandidates.count(pindex) == 0);
6042 }
6043 // Check whether this block is in m_blocks_unlinked.
6044 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
6045 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
6047 m_blockman.m_blocks_unlinked.equal_range(pindex->pprev);
6048 bool foundInUnlinked = false;
6049 while (rangeUnlinked.first != rangeUnlinked.second) {
6050 assert(rangeUnlinked.first->first == pindex->pprev);
6051 if (rangeUnlinked.first->second == pindex) {
6052 foundInUnlinked = true;
6053 break;
6054 }
6055 rangeUnlinked.first++;
6056 }
6057 if (pindex->pprev && pindex->nStatus.hasData() &&
6058 pindexFirstNeverProcessed != nullptr &&
6059 pindexFirstInvalid == nullptr) {
6060 // If this block has block data available, some parent was never
6061 // received, and has no invalid parents, it must be in
6062 // m_blocks_unlinked.
6064 }
6065 if (!pindex->nStatus.hasData()) {
6066 // Can't be in m_blocks_unlinked if we don't HAVE_DATA
6068 }
6069 if (pindexFirstMissing == nullptr) {
6070 // We aren't missing data for any parent -- cannot be in
6071 // m_blocks_unlinked.
6073 }
6074 if (pindex->pprev && pindex->nStatus.hasData() &&
6075 pindexFirstNeverProcessed == nullptr &&
6076 pindexFirstMissing != nullptr) {
6077 // We HAVE_DATA for this block, have received data for all parents
6078 // at some point, but we're currently missing data for some parent.
6079 // We must have pruned.
6081 // This block may have entered m_blocks_unlinked if:
6082 // - it has a descendant that at some point had more work than the
6083 // tip, and
6084 // - we tried switching to that descendant but were missing
6085 // data for some intermediate block between m_chain and the
6086 // tip.
6087 // So if this block is itself better than m_chain.Tip() and it
6088 // wasn't in
6089 // setBlockIndexCandidates, then it must be in m_blocks_unlinked.
6090 if (!CBlockIndexWorkComparator()(pindex, m_chain.Tip()) &&
6091 setBlockIndexCandidates.count(pindex) == 0) {
6092 if (pindexFirstInvalid == nullptr) {
6094 }
6095 }
6096 }
6097 // Perhaps too slow
6098 // assert(pindex->GetBlockHash() == pindex->GetBlockHeader().GetHash());
6099 // End: actual consistency checks.
6100
6101 // Try descending into the first subnode.
6102 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
6103 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
6104 range = forward.equal_range(pindex);
6105 if (range.first != range.second) {
6106 // A subnode was found.
6107 pindex = range.first->second;
6108 nHeight++;
6109 continue;
6110 }
6111 // This is a leaf node. Move upwards until we reach a node of which we
6112 // have not yet visited the last child.
6113 while (pindex) {
6114 // We are going to either move to a parent or a sibling of pindex.
6115 // If pindex was the first with a certain property, unset the
6116 // corresponding variable.
6117 if (pindex == pindexFirstInvalid) {
6118 pindexFirstInvalid = nullptr;
6119 }
6120 if (pindex == pindexFirstParked) {
6121 pindexFirstParked = nullptr;
6122 }
6123 if (pindex == pindexFirstMissing) {
6124 pindexFirstMissing = nullptr;
6125 }
6126 if (pindex == pindexFirstNeverProcessed) {
6127 pindexFirstNeverProcessed = nullptr;
6128 }
6129 if (pindex == pindexFirstNotTreeValid) {
6130 pindexFirstNotTreeValid = nullptr;
6131 }
6132 if (pindex == pindexFirstNotTransactionsValid) {
6134 }
6135 if (pindex == pindexFirstNotChainValid) {
6136 pindexFirstNotChainValid = nullptr;
6137 }
6138 if (pindex == pindexFirstNotScriptsValid) {
6140 }
6141 // Find our parent.
6142 CBlockIndex *pindexPar = pindex->pprev;
6143 // Find which child we just visited.
6144 std::pair<std::multimap<CBlockIndex *, CBlockIndex *>::iterator,
6145 std::multimap<CBlockIndex *, CBlockIndex *>::iterator>
6146 rangePar = forward.equal_range(pindexPar);
6147 while (rangePar.first->second != pindex) {
6148 // Our parent must have at least the node we're coming from as
6149 // child.
6150 assert(rangePar.first != rangePar.second);
6151 rangePar.first++;
6152 }
6153 // Proceed to the next one.
6154 rangePar.first++;
6155 if (rangePar.first != rangePar.second) {
6156 // Move to the sibling.
6157 pindex = rangePar.first->second;
6158 break;
6159 } else {
6160 // Move up further.
6161 pindex = pindexPar;
6162 nHeight--;
6163 continue;
6164 }
6165 }
6166 }
6167
6168 // Check that we actually traversed the entire map.
6169 assert(nNodes == forward.size());
6170}
6171
6172std::string Chainstate::ToString() {
6175 return strprintf("Chainstate [%s] @ height %d (%s)",
6176 m_from_snapshot_blockhash ? "snapshot" : "ibd",
6177 tip ? tip->nHeight : -1,
6178 tip ? tip->GetBlockHash().ToString() : "null");
6179}
6180
6181bool Chainstate::ResizeCoinsCaches(size_t coinstip_size, size_t coinsdb_size) {
6185 // Cache sizes are unchanged, no need to continue.
6186 return true;
6187 }
6192
6193 LogPrintf("[%s] resized coinsdb cache to %.1f MiB\n", this->ToString(),
6194 coinsdb_size * (1.0 / 1024 / 1024));
6195 LogPrintf("[%s] resized coinstip cache to %.1f MiB\n", this->ToString(),
6196 coinstip_size * (1.0 / 1024 / 1024));
6197
6199 bool ret;
6200
6202 // Likely no need to flush if cache sizes have grown.
6204 } else {
6205 // Otherwise, flush state to disk and deallocate the in-memory coins
6206 // map.
6208 }
6209 return ret;
6210}
6211
6217 const CBlockIndex *pindex) {
6218 if (pindex == nullptr) {
6219 return 0.0;
6220 }
6221
6222 int64_t nNow = time(nullptr);
6223
6224 double fTxTotal;
6225 if (pindex->GetChainTxCount() <= data.nTxCount) {
6226 fTxTotal = data.nTxCount + (nNow - data.nTime) * data.dTxRate;
6227 } else {
6228 fTxTotal = pindex->GetChainTxCount() +
6229 (nNow - pindex->GetBlockTime()) * data.dTxRate;
6230 }
6231
6232 return std::min<double>(pindex->GetChainTxCount() / fTxTotal, 1.0);
6233}
6234
6235std::optional<BlockHash> ChainstateManager::SnapshotBlockhash() const {
6236 LOCK(::cs_main);
6237 if (m_active_chainstate && m_active_chainstate->m_from_snapshot_blockhash) {
6238 // If a snapshot chainstate exists, it will always be our active.
6239 return m_active_chainstate->m_from_snapshot_blockhash;
6240 }
6241 return std::nullopt;
6242}
6243
6244std::vector<Chainstate *> ChainstateManager::GetAll() {
6245 LOCK(::cs_main);
6246 std::vector<Chainstate *> out;
6247
6248 for (Chainstate *pchainstate :
6249 {m_ibd_chainstate.get(), m_snapshot_chainstate.get()}) {
6250 if (this->IsUsable(pchainstate)) {
6251 out.push_back(pchainstate);
6252 }
6253 }
6254
6255 return out;
6256}
6257
6258Chainstate &ChainstateManager::InitializeChainstate(CTxMemPool *mempool) {
6261 assert(!m_active_chainstate);
6262
6263 m_ibd_chainstate = std::make_unique<Chainstate>(mempool, m_blockman, *this);
6264 m_active_chainstate = m_ibd_chainstate.get();
6265 return *m_active_chainstate;
6266}
6267
6268const AssumeutxoData *ExpectedAssumeutxo(const int height,
6269 const CChainParams &chainparams) {
6270 const MapAssumeutxo &valid_assumeutxos_map = chainparams.Assumeutxo();
6271 const auto assumeutxo_found = valid_assumeutxos_map.find(height);
6272
6274 return &assumeutxo_found->second;
6275 }
6276 return nullptr;
6277}
6278
6282
6283 if (is_snapshot) {
6286
6287 try {
6288 const bool existed{fs::remove(base_blockhash_path)};
6289 if (!existed) {
6290 LogPrintf("[snapshot] snapshot chainstate dir being removed "
6291 "lacks %s file\n",
6293 }
6294 } catch (const fs::filesystem_error &e) {
6295 LogPrintf("[snapshot] failed to remove file %s: %s\n",
6298 }
6299 }
6300
6301 std::string path_str = fs::PathToString(db_path);
6302 LogPrintf("Removing leveldb dir at %s\n", path_str);
6303
6304 // We have to destruct before this call leveldb::DB in order to release the
6305 // db lock, otherwise `DestroyDB` will fail. See `leveldb::~DBImpl()`.
6306 const bool destroyed = dbwrapper::DestroyDB(path_str, {}).ok();
6307
6308 if (!destroyed) {
6309 LogPrintf("error: leveldb DestroyDB call failed on %s\n", path_str);
6310 }
6311
6312 // Datadir should be removed from filesystem; otherwise initialization may
6313 // detect it on subsequent statups and get confused.
6314 //
6315 // If the base_blockhash_path removal above fails in the case of snapshot
6316 // chainstates, this will return false since leveldb won't remove a
6317 // non-empty directory.
6318 return destroyed && !fs::exists(db_path);
6319}
6320
6323 bool in_memory) {
6324 BlockHash base_blockhash = metadata.m_base_blockhash;
6325
6326 if (this->SnapshotBlockhash()) {
6327 LogPrintf("[snapshot] can't activate a snapshot-based chainstate more "
6328 "than once\n");
6329 return false;
6330 }
6331
6334
6335 // Cache percentages to allocate to each chainstate.
6336 //
6337 // These particular percentages don't matter so much since they will only be
6338 // relevant during snapshot activation; caches are rebalanced at the
6339 // conclusion of this function. We want to give (essentially) all available
6340 // cache capacity to the snapshot to aid the bulk load later in this
6341 // function.
6342 static constexpr double IBD_CACHE_PERC = 0.01;
6343 static constexpr double SNAPSHOT_CACHE_PERC = 0.99;
6344
6345 {
6346 LOCK(::cs_main);
6347 // Resize the coins caches to ensure we're not exceeding memory limits.
6348 //
6349 // Allocate the majority of the cache to the incoming snapshot
6350 // chainstate, since (optimistically) getting to its tip will be the top
6351 // priority. We'll need to call `MaybeRebalanceCaches()` once we're done
6352 // with this function to ensure the right allocation (including the
6353 // possibility that no snapshot was activated and that we should restore
6354 // the active chainstate caches to their original size).
6355 //
6357 this->ActiveChainstate().m_coinsdb_cache_size_bytes;
6359 this->ActiveChainstate().m_coinstip_cache_size_bytes;
6360
6361 // Temporarily resize the active coins cache to make room for the
6362 // newly-created snapshot chain.
6363 this->ActiveChainstate().ResizeCoinsCaches(
6364 static_cast<size_t>(current_coinstip_cache_size * IBD_CACHE_PERC),
6365 static_cast<size_t>(current_coinsdb_cache_size * IBD_CACHE_PERC));
6366 }
6367
6368 auto snapshot_chainstate =
6369 WITH_LOCK(::cs_main, return std::make_unique<Chainstate>(
6370 /* mempool */ nullptr, m_blockman, *this,
6372
6373 {
6374 LOCK(::cs_main);
6375 snapshot_chainstate->InitCoinsDB(
6376 static_cast<size_t>(current_coinsdb_cache_size *
6378 in_memory, false, "chainstate");
6379 snapshot_chainstate->InitCoinsCache(static_cast<size_t>(
6381 }
6382
6384 coins_file, metadata);
6385
6386 // If not in-memory, persist the base blockhash for use during subsequent
6387 // initialization.
6388 if (!in_memory) {
6389 LOCK(::cs_main);
6391 snapshot_ok = false;
6392 }
6393 }
6394 if (!snapshot_ok) {
6395 LOCK(::cs_main);
6396 this->MaybeRebalanceCaches();
6397
6398 // PopulateAndValidateSnapshot can return (in error) before the leveldb
6399 // datadir has been created, so only attempt removal if we got that far.
6401 // We have to destruct leveldb::DB in order to release the db lock,
6402 // otherwise DestroyDB() (in DeleteCoinsDBFromDisk()) will fail. See
6403 // `leveldb::~DBImpl()`. Destructing the chainstate (and so
6404 // resetting the coinsviews object) does this.
6405 snapshot_chainstate.reset();
6406 bool removed =
6407 DeleteCoinsDBFromDisk(*snapshot_datadir, /*is_snapshot=*/true);
6408 if (!removed) {
6409 AbortNode(
6410 strprintf("Failed to remove snapshot chainstate dir (%s). "
6411 "Manually remove it before restarting.\n",
6413 }
6414 }
6415 return false;
6416 }
6417
6418 {
6419 LOCK(::cs_main);
6422 const bool chaintip_loaded = m_snapshot_chainstate->LoadChainTip();
6424
6425 m_active_chainstate = m_snapshot_chainstate.get();
6426
6427 LogPrintf("[snapshot] successfully activated snapshot %s\n",
6428 base_blockhash.ToString());
6429 LogPrintf("[snapshot] (%.2f MB)\n",
6430 m_snapshot_chainstate->CoinsTip().DynamicMemoryUsage() /
6431 (1000 * 1000));
6432
6433 this->MaybeRebalanceCaches();
6434 }
6435 return true;
6436}
6437
6439 bool snapshot_loaded) {
6441 strprintf("%s (%.2f MB)",
6442 snapshot_loaded ? "saving snapshot chainstate"
6443 : "flushing coins cache",
6444 coins_cache.DynamicMemoryUsage() / (1000 * 1000)),
6446
6447 coins_cache.Flush();
6448}
6449
6450struct StopHashingException : public std::exception {
6451 const char *what() const throw() override {
6452 return "ComputeUTXOStats interrupted by shutdown.";
6453 }
6454};
6455
6457 if (ShutdownRequested()) {
6458 throw StopHashingException();
6459 }
6460}
6461
6464 const SnapshotMetadata &metadata) {
6465 // It's okay to release cs_main before we're done using `coins_cache`
6466 // because we know that nothing else will be referencing the newly created
6467 // snapshot_chainstate yet.
6469 *WITH_LOCK(::cs_main, return &snapshot_chainstate.CoinsTip());
6470
6471 BlockHash base_blockhash = metadata.m_base_blockhash;
6472
6475
6476 if (!snapshot_start_block) {
6477 // Needed for ComputeUTXOStats and ExpectedAssumeutxo to determine the
6478 // height and to avoid a crash when base_blockhash.IsNull()
6479 LogPrintf("[snapshot] Did not find snapshot start blockheader %s\n",
6480 base_blockhash.ToString());
6481 return false;
6482 }
6483
6484 int base_height = snapshot_start_block->nHeight;
6486
6487 if (!maybe_au_data) {
6488 LogPrintf("[snapshot] assumeutxo height in snapshot metadata not "
6489 "recognized (%d) - refusing to load snapshot\n",
6490 base_height);
6491 return false;
6492 }
6493
6495
6496 COutPoint outpoint;
6497 Coin coin;
6498 const uint64_t coins_count = metadata.m_coins_count;
6499 uint64_t coins_left = metadata.m_coins_count;
6500
6501 LogPrintf("[snapshot] loading coins from snapshot %s\n",
6502 base_blockhash.ToString());
6504
6505 while (coins_left > 0) {
6506 try {
6507 coins_file >> outpoint;
6508 coins_file >> coin;
6509 } catch (const std::ios_base::failure &) {
6510 LogPrintf("[snapshot] bad snapshot format or truncated snapshot "
6511 "after deserializing %d coins\n",
6512 coins_count - coins_left);
6513 return false;
6514 }
6515 if (coin.GetHeight() > uint32_t(base_height) ||
6516 // Avoid integer wrap-around in coinstats.cpp:ApplyHash
6517 outpoint.GetN() >=
6518 std::numeric_limits<decltype(outpoint.GetN())>::max()) {
6519 LogPrintf(
6520 "[snapshot] bad snapshot data after deserializing %d coins\n",
6521 coins_count - coins_left);
6522 return false;
6523 }
6524 coins_cache.EmplaceCoinInternalDANGER(std::move(outpoint),
6525 std::move(coin));
6526
6527 --coins_left;
6529
6530 if (coins_processed % 1000000 == 0) {
6531 LogPrintf("[snapshot] %d coins loaded (%.2f%%, %.2f MB)\n",
6533 static_cast<float>(coins_processed) * 100 /
6534 static_cast<float>(coins_count),
6535 coins_cache.DynamicMemoryUsage() / (1000 * 1000));
6536 }
6537
6538 // Batch write and flush (if we need to) every so often.
6539 //
6540 // If our average Coin size is roughly 41 bytes, checking every 120,000
6541 // coins means <5MB of memory imprecision.
6542 if (coins_processed % 120000 == 0) {
6543 if (ShutdownRequested()) {
6544 return false;
6545 }
6546
6547 const auto snapshot_cache_state = WITH_LOCK(
6548 ::cs_main, return snapshot_chainstate.GetCoinsCacheSizeState());
6549
6551 // This is a hack - we don't know what the actual best block is,
6552 // but that doesn't matter for the purposes of flushing the
6553 // cache here. We'll set this to its correct value
6554 // (`base_blockhash`) below after the coins are loaded.
6555 coins_cache.SetBestBlock(BlockHash{GetRandHash()});
6556
6557 // No need to acquire cs_main since this chainstate isn't being
6558 // used yet.
6559 FlushSnapshotToDisk(coins_cache, /*snapshot_loaded=*/false);
6560 }
6561 }
6562 }
6563
6564 // Important that we set this. This and the coins_cache accesses above are
6565 // sort of a layer violation, but either we reach into the innards of
6566 // CCoinsViewCache here or we have to invert some of the Chainstate to
6567 // embed them in a snapshot-activation-specific CCoinsViewCache bulk load
6568 // method.
6569 coins_cache.SetBestBlock(base_blockhash);
6570
6571 bool out_of_coins{false};
6572 try {
6573 coins_file >> outpoint;
6574 } catch (const std::ios_base::failure &) {
6575 // We expect an exception since we should be out of coins.
6576 out_of_coins = true;
6577 }
6578 if (!out_of_coins) {
6579 LogPrintf("[snapshot] bad snapshot - coins left over after "
6580 "deserializing %d coins\n",
6581 coins_count);
6582 return false;
6583 }
6584
6585 LogPrintf("[snapshot] loaded %d (%.2f MB) coins from snapshot %s\n",
6586 coins_count, coins_cache.DynamicMemoryUsage() / (1000 * 1000),
6587 base_blockhash.ToString());
6588
6589 // No need to acquire cs_main since this chainstate isn't being used yet.
6590 FlushSnapshotToDisk(coins_cache, /*snapshot_loaded=*/true);
6591
6592 assert(coins_cache.GetBestBlock() == base_blockhash);
6593
6594 // As above, okay to immediately release cs_main here since no other context
6595 // knows about the snapshot_chainstate.
6597 WITH_LOCK(::cs_main, return &snapshot_chainstate.CoinsDB());
6598
6599 std::optional<CCoinsStats> maybe_stats;
6600
6601 try {
6602 maybe_stats = ComputeUTXOStats(CoinStatsHashType::HASH_SERIALIZED,
6605 } catch (StopHashingException const &) {
6606 return false;
6607 }
6608 if (!maybe_stats.has_value()) {
6609 LogPrintf("[snapshot] failed to generate coins stats\n");
6610 return false;
6611 }
6612
6613 // Assert that the deserialized chainstate contents match the expected
6614 // assumeutxo value.
6615 if (AssumeutxoHash{maybe_stats->hashSerialized} !=
6616 au_data.hash_serialized) {
6617 LogPrintf("[snapshot] bad snapshot content hash: expected %s, got %s\n",
6618 au_data.hash_serialized.ToString(),
6619 maybe_stats->hashSerialized.ToString());
6620 return false;
6621 }
6622
6624
6625 // The remainder of this function requires modifying data protected by
6626 // cs_main.
6627 LOCK(::cs_main);
6628
6629 // Fake various pieces of CBlockIndex state:
6630 CBlockIndex *index = nullptr;
6631
6632 // Don't make any modifications to the genesis block.
6633 // This is especially important because we don't want to erroneously
6634 // apply ASSUMED_VALID_FLAG to genesis, which would happen if we didn't
6635 // skip it here (since it apparently isn't BlockValidity::SCRIPTS).
6636 constexpr int AFTER_GENESIS_START{1};
6637
6638 for (int i = AFTER_GENESIS_START; i <= snapshot_chainstate.m_chain.Height();
6639 ++i) {
6640 index = snapshot_chainstate.m_chain[i];
6641
6642 // Fake nTx so that LoadBlockIndex() loads assumed-valid CBlockIndex
6643 // entries (among other things)
6644 if (!index->nTx) {
6645 index->nTx = 1;
6646 }
6647 // Fake nChainTx so that GuessVerificationProgress reports accurately
6648 index->nChainTx = index->pprev->nChainTx + index->nTx;
6649
6650 // Mark unvalidated block index entries beneath the snapshot base block
6651 // as assumed-valid.
6652 if (!index->IsValid(BlockValidity::SCRIPTS)) {
6653 // This flag will be removed once the block is fully validated by a
6654 // background chainstate.
6655 index->nStatus = index->nStatus.withAssumedValid();
6656 }
6657
6658 m_blockman.m_dirty_blockindex.insert(index);
6659 // Changes to the block index will be flushed to disk after this call
6660 // returns in `ActivateSnapshot()`, when `MaybeRebalanceCaches()` is
6661 // called, since we've added a snapshot chainstate and therefore will
6662 // have to downsize the IBD chainstate, which will result in a call to
6663 // `FlushStateToDisk(ALWAYS)`.
6664 }
6665
6666 assert(index);
6667 index->nChainTx = au_data.nChainTx;
6668 snapshot_chainstate.setBlockIndexCandidates.insert(snapshot_start_block);
6669
6670 LogPrintf("[snapshot] validated snapshot (%.2f MB)\n",
6671 coins_cache.DynamicMemoryUsage() / (1000 * 1000));
6672 return true;
6673}
6674
6675// Currently, this function holds cs_main for its duration, which could be for
6676// multiple minutes due to the ComputeUTXOStats call. This hold is necessary
6677// because we need to avoid advancing the background validation chainstate
6678// farther than the snapshot base block - and this function is also invoked
6679// from within ConnectTip, i.e. from within ActivateBestChain, so cs_main is
6680// held anyway.
6681//
6682// Eventually (TODO), we could somehow separate this function's runtime from
6683// maintenance of the active chain, but that will either require
6684//
6685// (i) setting `m_disabled` immediately and ensuring all chainstate accesses go
6686// through IsUsable() checks, or
6687//
6688// (ii) giving each chainstate its own lock instead of using cs_main for
6689// everything.
6690SnapshotCompletionResult ChainstateManager::MaybeCompleteSnapshotValidation(
6691 std::function<void(bilingual_str)> shutdown_fnc) {
6693 if (m_ibd_chainstate.get() == &this->ActiveChainstate() ||
6694 !this->IsUsable(m_snapshot_chainstate.get()) ||
6695 !this->IsUsable(m_ibd_chainstate.get()) ||
6696 !m_ibd_chainstate->m_chain.Tip()) {
6697 // Nothing to do - this function only applies to the background
6698 // validation chainstate.
6700 }
6701 const int snapshot_tip_height = this->ActiveHeight();
6703 const CBlockIndex &index_new = *Assert(m_ibd_chainstate->m_chain.Tip());
6704
6705 if (index_new.nHeight < snapshot_base_height) {
6706 // Background IBD not complete yet.
6708 }
6709
6712
6715 _("%s failed to validate the -assumeutxo snapshot state. "
6716 "This indicates a hardware problem, or a bug in the software, or "
6717 "a bad software modification that allowed an invalid snapshot to "
6718 "be loaded. As a result of this, the node will shut down and "
6719 "stop using any state that was built on the snapshot, resetting "
6720 "the chain height from %d to %d. On the next restart, the node "
6721 "will resume syncing from %d without using any snapshot data. "
6722 "Please report this incident to %s, including how you obtained "
6723 "the snapshot. The invalid snapshot chainstate will be left on "
6724 "disk in case it is helpful in diagnosing the issue that caused "
6725 "this error."),
6728
6729 LogPrintf("[snapshot] !!! %s\n", user_error.original);
6730 LogPrintf("[snapshot] deleting snapshot, reverting to validated chain, "
6731 "and stopping node\n");
6732
6733 m_active_chainstate = m_ibd_chainstate.get();
6734 m_snapshot_chainstate->m_disabled = true;
6735 assert(!this->IsUsable(m_snapshot_chainstate.get()));
6736 assert(this->IsUsable(m_ibd_chainstate.get()));
6737
6738 auto rename_result = m_snapshot_chainstate->InvalidateCoinsDBOnDisk();
6739 if (!rename_result) {
6740 user_error = strprintf(Untranslated("%s\n%s"), user_error,
6742 }
6743
6745 };
6746
6747 if (index_new.GetBlockHash() != snapshot_blockhash) {
6748 LogPrintf(
6749 "[snapshot] supposed base block %s does not match the "
6750 "snapshot base block %s (height %d). Snapshot is not valid.\n",
6751 index_new.ToString(), snapshot_blockhash.ToString(),
6755 }
6756
6758
6759 int curr_height = m_ibd_chainstate->m_chain.Height();
6760
6764 assert(this->GetAll().size() == 2);
6765
6767 m_ibd_chainstate->ForceFlushStateToDisk();
6768
6770 if (!maybe_au_data) {
6771 LogPrintf("[snapshot] assumeutxo data not found for height "
6772 "(%d) - refusing to validate snapshot\n",
6773 curr_height);
6776 }
6777
6779 std::optional<CCoinsStats> maybe_ibd_stats;
6780 LogPrintf(
6781 "[snapshot] computing UTXO stats for background chainstate to validate "
6782 "snapshot - this could take a few minutes\n");
6783 try {
6785 ComputeUTXOStats(CoinStatsHashType::HASH_SERIALIZED, &ibd_coins_db,
6787 } catch (StopHashingException const &) {
6789 }
6790
6791 if (!maybe_ibd_stats) {
6792 LogPrintf(
6793 "[snapshot] failed to generate stats for validation coins db\n");
6794 // While this isn't a problem with the snapshot per se, this condition
6795 // prevents us from validating the snapshot, so we should shut down and
6796 // let the user handle the issue manually.
6799 }
6800 const auto &ibd_stats = *maybe_ibd_stats;
6801
6802 // Compare the background validation chainstate's UTXO set hash against the
6803 // hard-coded assumeutxo hash we expect.
6804 //
6805 // TODO: For belt-and-suspenders, we could cache the UTXO set
6806 // hash for the snapshot when it's loaded in its chainstate's leveldb. We
6807 // could then reference that here for an additional check.
6808 if (AssumeutxoHash{ibd_stats.hashSerialized} != au_data.hash_serialized) {
6809 LogPrintf("[snapshot] hash mismatch: actual=%s, expected=%s\n",
6810 ibd_stats.hashSerialized.ToString(),
6811 au_data.hash_serialized.ToString());
6814 }
6815
6816 LogPrintf("[snapshot] snapshot beginning at %s has been fully validated\n",
6817 snapshot_blockhash.ToString());
6818
6819 m_ibd_chainstate->m_disabled = true;
6820 this->MaybeRebalanceCaches();
6821
6823}
6824
6826 LOCK(::cs_main);
6827 assert(m_active_chainstate);
6828 return *m_active_chainstate;
6829}
6830
6832 LOCK(::cs_main);
6833 return m_snapshot_chainstate &&
6834 m_active_chainstate == m_snapshot_chainstate.get();
6835}
6836void ChainstateManager::MaybeRebalanceCaches() {
6838 bool ibd_usable = this->IsUsable(m_ibd_chainstate.get());
6840 assert(ibd_usable || snapshot_usable);
6841
6842 if (ibd_usable && !snapshot_usable) {
6843 LogPrintf("[snapshot] allocating all cache to the IBD chainstate\n");
6844 // Allocate everything to the IBD chainstate.
6845 m_ibd_chainstate->ResizeCoinsCaches(m_total_coinstip_cache,
6847 } else if (snapshot_usable && !ibd_usable) {
6848 // If background validation has completed and snapshot is our active
6849 // chain...
6850 LogPrintf(
6851 "[snapshot] allocating all cache to the snapshot chainstate\n");
6852 // Allocate everything to the snapshot chainstate.
6855 } else if (ibd_usable && snapshot_usable) {
6856 // If both chainstates exist, determine who needs more cache based on
6857 // IBD status.
6858 //
6859 // Note: shrink caches first so that we don't inadvertently overwhelm
6860 // available memory.
6861 if (m_snapshot_chainstate->IsInitialBlockDownload()) {
6862 m_ibd_chainstate->ResizeCoinsCaches(m_total_coinstip_cache * 0.05,
6863 m_total_coinsdb_cache * 0.05);
6864 m_snapshot_chainstate->ResizeCoinsCaches(
6866 } else {
6867 m_snapshot_chainstate->ResizeCoinsCaches(
6869 m_ibd_chainstate->ResizeCoinsCaches(m_total_coinstip_cache * 0.95,
6870 m_total_coinsdb_cache * 0.95);
6871 }
6872 }
6873}
6874
6875void ChainstateManager::ResetChainstates() {
6876 m_ibd_chainstate.reset();
6877 m_snapshot_chainstate.reset();
6878 m_active_chainstate = nullptr;
6879}
6880
6887 if (!opts.check_block_index.has_value()) {
6888 opts.check_block_index =
6889 opts.config.GetChainParams().DefaultConsistencyChecks();
6890 }
6891
6892 if (!opts.minimum_chain_work.has_value()) {
6893 opts.minimum_chain_work = UintToArith256(
6894 opts.config.GetChainParams().GetConsensus().nMinimumChainWork);
6895 }
6896 if (!opts.assumed_valid_block.has_value()) {
6897 opts.assumed_valid_block =
6898 opts.config.GetChainParams().GetConsensus().defaultAssumeValid;
6899 }
6900 Assert(opts.adjusted_time_callback);
6901 return std::move(opts);
6902}
6903
6908
6909bool ChainstateManager::DetectSnapshotChainstate(CTxMemPool *mempool) {
6911 std::optional<fs::path> path = node::FindSnapshotChainstateDir();
6912 if (!path) {
6913 return false;
6914 }
6915 std::optional<BlockHash> base_blockhash =
6917 if (!base_blockhash) {
6918 return false;
6919 }
6920 LogPrintf("[snapshot] detected active snapshot chainstate (%s) - loading\n",
6921 fs::PathToString(*path));
6922
6924 return true;
6925}
6926
6927Chainstate &
6928ChainstateManager::ActivateExistingSnapshot(CTxMemPool *mempool,
6931 m_snapshot_chainstate = std::make_unique<Chainstate>(mempool, m_blockman,
6932 *this, base_blockhash);
6933 LogPrintf("[snapshot] switching active chainstate to %s\n",
6934 m_snapshot_chainstate->ToString());
6935 m_active_chainstate = m_snapshot_chainstate.get();
6936 return *m_snapshot_chainstate;
6937}
6938
6939util::Result<void> Chainstate::InvalidateCoinsDBOnDisk() {
6941 // Should never be called on a non-snapshot chainstate.
6943 auto storage_path_maybe = this->CoinsDB().StoragePath();
6944 // Should never be called with a non-existent storage path.
6947
6948 // Coins views no longer usable.
6949 m_coins_views.reset();
6950
6951 auto invalid_path = snapshot_datadir + "_INVALID";
6953 std::string target = fs::PathToString(invalid_path);
6954 LogPrintf("[snapshot] renaming snapshot datadir %s to %s\n", dbpath,
6955 target);
6956
6957 // The invalid snapshot datadir is simply moved and not deleted because we
6958 // may want to do forensics later during issue investigation. The user is
6959 // instructed accordingly in MaybeCompleteSnapshotValidation().
6960 try {
6961 fs::rename(snapshot_datadir, invalid_path);
6962 } catch (const fs::filesystem_error &e) {
6965
6966 LogPrintf("%s: error renaming file '%s' -> '%s': %s\n", __func__,
6967 src_str, dest_str, e.what());
6968 return util::Error{strprintf(_("Rename of '%s' -> '%s' failed. "
6969 "You should resolve this by manually "
6970 "moving or deleting the invalid "
6971 "snapshot directory %s, otherwise you "
6972 "will encounter the same error again "
6973 "on the next startup."),
6975 }
6976 return {};
6977}
6978
6979const CBlockIndex *ChainstateManager::GetSnapshotBaseBlock() const {
6980 const auto blockhash_op = this->SnapshotBlockhash();
6981 if (!blockhash_op) {
6982 return nullptr;
6983 }
6985}
6986
6987std::optional<int> ChainstateManager::GetSnapshotBaseHeight() const {
6988 const CBlockIndex *base = this->GetSnapshotBaseBlock();
6989 return base ? std::make_optional(base->nHeight) : std::nullopt;
6990}
6991
6992bool ChainstateManager::ValidatedSnapshotCleanup() {
6995 ::cs_main) -> std::optional<fs::path> {
6996 if (!(chainstate && chainstate->HasCoinsViews())) {
6997 return {};
6998 }
6999 return chainstate->CoinsDB().StoragePath();
7000 };
7001 std::optional<fs::path> ibd_chainstate_path_maybe =
7003 std::optional<fs::path> snapshot_chainstate_path_maybe =
7005
7006 if (!this->IsSnapshotValidated()) {
7007 // No need to clean up.
7008 return false;
7009 }
7010 // If either path doesn't exist, that means at least one of the chainstates
7011 // is in-memory, in which case we can't do on-disk cleanup. You'd better be
7012 // in a unittest!
7013 if (!ibd_chainstate_path_maybe || !snapshot_chainstate_path_maybe) {
7014 LogPrintf("[snapshot] snapshot chainstate cleanup cannot happen with "
7015 "in-memory chainstates. You are testing, right?\n");
7016 return false;
7017 }
7018
7021
7022 // Since we're going to be moving around the underlying leveldb filesystem
7023 // content for each chainstate, make sure that the chainstates (and their
7024 // constituent CoinsViews members) have been destructed first.
7025 //
7026 // The caller of this method will be responsible for reinitializing
7027 // chainstates if they want to continue operation.
7028 this->ResetChainstates();
7029
7030 // No chainstates should be considered usable.
7031 assert(this->GetAll().size() == 0);
7032
7033 LogPrintf("[snapshot] deleting background chainstate directory (now "
7034 "unnecessary) (%s)\n",
7036
7037 fs::path tmp_old{ibd_chainstate_path + "_todelete"};
7038
7040 const fs::filesystem_error &err) {
7041 LogPrintf("Error renaming file (%s): %s\n", fs::PathToString(p_old),
7042 err.what());
7044 "Rename of '%s' -> '%s' failed. "
7045 "Cannot clean up the background chainstate leveldb directory.",
7047 };
7048
7049 try {
7050 fs::rename(ibd_chainstate_path, tmp_old);
7051 } catch (const fs::filesystem_error &e) {
7053 throw;
7054 }
7055
7056 LogPrintf("[snapshot] moving snapshot chainstate (%s) to "
7057 "default chainstate directory (%s)\n",
7060
7061 try {
7063 } catch (const fs::filesystem_error &e) {
7065 throw;
7066 }
7067
7068 if (!DeleteCoinsDBFromDisk(tmp_old, /*is_snapshot=*/false)) {
7069 // No need to AbortNode because once the unneeded bg chainstate data is
7070 // moved, it will not interfere with subsequent initialization.
7071 LogPrintf("Deletion of %s failed. Please remove it manually, as the "
7072 "directory is now unnecessary.\n",
7074 } else {
7075 LogPrintf("[snapshot] deleted background chainstate directory (%s)\n",
7077 }
7078 return true;
7079}
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:431
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:226
#define LogPrint(category,...)
Definition logging.h:211
#define LogPrintf(...)
Definition logging.h:207
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