LCOV - code coverage report
Current view: top level - src/test - validation_chainstatemanager_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 96.3 % 562 541
Test Date: 2026-10-11 07:59:32 Functions: 100.0 % 72 72
Branches: 51.5 % 2416 1245

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2019-present The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : //
       5                 :             : #include <chainparams.h>
       6                 :             : #include <consensus/validation.h>
       7                 :             : #include <kernel/disconnected_transactions.h>
       8                 :             : #include <node/block_template_manager.h>
       9                 :             : #include <node/chainstatemanager_args.h>
      10                 :             : #include <node/kernel_notifications.h>
      11                 :             : #include <node/utxo_snapshot.h>
      12                 :             : #include <random.h>
      13                 :             : #include <rpc/blockchain.h>
      14                 :             : #include <sync.h>
      15                 :             : #include <test/util/chainstate.h>
      16                 :             : #include <test/util/common.h>
      17                 :             : #include <test/util/logging.h>
      18                 :             : #include <test/util/random.h>
      19                 :             : #include <test/util/setup_common.h>
      20                 :             : #include <test/util/validation.h>
      21                 :             : #include <uint256.h>
      22                 :             : #include <util/byte_units.h>
      23                 :             : #include <util/result.h>
      24                 :             : #include <util/vector.h>
      25                 :             : #include <validation.h>
      26                 :             : #include <validationinterface.h>
      27                 :             : 
      28                 :             : #include <tinyformat.h>
      29                 :             : 
      30                 :             : #include <vector>
      31                 :             : 
      32                 :             : #include <boost/test/unit_test.hpp>
      33                 :             : 
      34                 :             : using node::BlockManager;
      35                 :             : using node::KernelNotifications;
      36                 :             : using node::SnapshotMetadata;
      37                 :             : 
      38                 :             : BOOST_FIXTURE_TEST_SUITE(validation_chainstatemanager_tests, TestingSetup)
      39                 :             : 
      40                 :             : //! Basic tests for ChainstateManager.
      41                 :             : //!
      42                 :             : //! First create a legacy (IBD) chainstate, then create a snapshot chainstate.
      43   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(chainstatemanager, TestChain100Setup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      44                 :             : {
      45                 :           1 :     ChainstateManager& manager = *m_node.chainman;
      46                 :             : 
      47   [ +  -  +  - ]:           3 :     BOOST_CHECK(WITH_LOCK(::cs_main, return !manager.CurrentChainstate().m_from_snapshot_blockhash));
      48                 :             : 
      49                 :             :     // Create a legacy (IBD) chainstate.
      50                 :             :     //
      51                 :           1 :     Chainstate& c1 = manager.ActiveChainstate();
      52                 :             : 
      53   [ +  -  +  - ]:           3 :     BOOST_CHECK(WITH_LOCK(::cs_main, return !manager.CurrentChainstate().m_from_snapshot_blockhash));
      54                 :           1 :     {
      55                 :           1 :         LOCK(manager.GetMutex());
      56   [ +  -  -  +  :           1 :         BOOST_CHECK_EQUAL(manager.m_chainstates.size(), 1);
                   +  - ]
      57   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(manager.m_chainstates[0].get(), &c1);
                   +  - ]
      58                 :           0 :     }
      59                 :             : 
      60   [ +  -  +  - ]:           3 :     auto& active_chain = WITH_LOCK(manager.GetMutex(), return manager.ActiveChain());
      61         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(&active_chain, &c1.m_chain);
      62                 :             : 
      63                 :             :     // Get to a valid assumeutxo tip (per chainparams);
      64                 :           1 :     mineBlocks(10);
      65   [ +  -  +  - ]:           3 :     BOOST_CHECK_EQUAL(WITH_LOCK(manager.GetMutex(), return manager.ActiveHeight()), 110);
      66   [ +  -  +  - ]:           3 :     auto active_tip = WITH_LOCK(manager.GetMutex(), return manager.ActiveTip());
      67         [ -  + ]:           1 :     auto exp_tip = c1.m_chain.Tip();
      68         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(active_tip, exp_tip);
      69                 :             : 
      70   [ +  -  +  - ]:           3 :     BOOST_CHECK(WITH_LOCK(::cs_main, return !manager.CurrentChainstate().m_from_snapshot_blockhash));
      71                 :             : 
      72                 :             :     // Create a snapshot-based chainstate.
      73                 :             :     //
      74                 :           1 :     const uint256 snapshot_blockhash = active_tip->GetBlockHash();
      75   [ +  -  +  -  :           3 :     Chainstate& c2{WITH_LOCK(::cs_main, return manager.AddChainstate(std::make_unique<Chainstate>(nullptr, manager.m_blockman, manager, snapshot_blockhash)))};
                   +  - ]
      76                 :           1 :     c2.InitCoinsDB(
      77                 :             :         /*cache_size_bytes=*/8_MiB, /*in_memory=*/true, /*should_wipe=*/false);
      78                 :           1 :     {
      79                 :           1 :         LOCK(::cs_main);
      80         [ +  - ]:           1 :         c2.InitCoinsCache(8_MiB);
      81   [ +  -  +  - ]:           1 :         c2.CoinsTip().SetBestBlock(active_tip->GetBlockHash());
      82         [ +  + ]:           3 :         for (const auto& cs : manager.m_chainstates) {
      83         [ +  - ]:           2 :             cs->ClearBlockIndexCandidates();
      84                 :             :         }
      85         [ +  - ]:           1 :         c2.LoadChainTip();
      86         [ +  + ]:           3 :         for (const auto& cs : manager.m_chainstates) {
      87         [ +  - ]:           2 :             cs->PopulateBlockIndexCandidates();
      88                 :             :         }
      89                 :           0 :     }
      90         [ +  - ]:           1 :     BlockValidationState _;
      91   [ +  -  +  -  :           2 :     BOOST_CHECK(c2.ActivateBestChain(_, nullptr));
          +  -  -  +  +  
                      - ]
      92                 :             : 
      93   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(WITH_LOCK(::cs_main, return *manager.CurrentChainstate().m_from_snapshot_blockhash), snapshot_blockhash);
                   +  - ]
      94   [ +  -  +  -  :           3 :     BOOST_CHECK(WITH_LOCK(::cs_main, return manager.CurrentChainstate().m_assumeutxo == Assumeutxo::UNVALIDATED));
             +  -  +  - ]
      95   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(&c2, &manager.ActiveChainstate());
                   +  - ]
      96   [ +  -  +  -  :           2 :     BOOST_CHECK(&c1 != &manager.ActiveChainstate());
             +  -  +  - ]
      97                 :           1 :     {
      98         [ +  - ]:           1 :         LOCK(manager.GetMutex());
      99   [ +  -  -  +  :           1 :         BOOST_CHECK_EQUAL(manager.m_chainstates.size(), 2);
                   +  - ]
     100   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(manager.m_chainstates[0].get(), &c1);
     101   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(manager.m_chainstates[1].get(), &c2);
                   +  - ]
     102                 :           0 :     }
     103                 :             : 
     104   [ +  -  +  - ]:           3 :     auto& active_chain2 = WITH_LOCK(manager.GetMutex(), return manager.ActiveChain());
     105   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(&active_chain2, &c2.m_chain);
     106                 :             : 
     107   [ +  -  +  -  :           3 :     BOOST_CHECK_EQUAL(WITH_LOCK(manager.GetMutex(), return manager.ActiveHeight()), 110);
                   +  - ]
     108         [ +  - ]:           1 :     mineBlocks(1);
     109   [ +  -  +  -  :           3 :     BOOST_CHECK_EQUAL(WITH_LOCK(manager.GetMutex(), return manager.ActiveHeight()), 111);
                   +  - ]
     110   [ +  +  +  -  :           2 :     BOOST_CHECK_EQUAL(WITH_LOCK(manager.GetMutex(), return c1.m_chain.Height()), 110);
                   +  - ]
     111                 :             : 
     112   [ +  -  +  - ]:           3 :     auto active_tip2 = WITH_LOCK(manager.GetMutex(), return manager.ActiveTip());
     113   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(active_tip, active_tip2->pprev);
     114   [ +  -  -  +  :           2 :     BOOST_CHECK_EQUAL(active_tip, c1.m_chain.Tip());
                   +  - ]
     115   [ +  -  -  +  :           2 :     BOOST_CHECK_EQUAL(active_tip2, c2.m_chain.Tip());
                   +  - ]
     116                 :             : 
     117                 :             :     // Let scheduler events finish running to avoid accessing memory that is going to be unloaded
     118         [ +  - ]:           1 :     m_node.validation_signals->SyncWithValidationInterfaceQueue();
     119                 :           1 : }
     120                 :             : 
     121   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(chainstatemanager_delete_chainstate_no_mempool, ChainTestingSetup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     122                 :             : {
     123         [ -  + ]:           1 :     auto& manager{*Assert(m_node.chainman)};
     124   [ +  -  +  - ]:           3 :     auto& validated{WITH_LOCK(::cs_main, return manager.InitializeChainstate(/*mempool=*/nullptr))};
     125   [ +  -  +  -  :           3 :     auto& snapshot{WITH_LOCK(::cs_main, return manager.AddChainstate(std::make_unique<Chainstate>(nullptr, manager.m_blockman, manager, uint256::ONE)))};
                   +  - ]
     126         [ +  - ]:           3 :     WITH_LOCK(::cs_main, validated.SetTargetBlock(nullptr));
     127   [ +  -  +  - ]:           4 :     BOOST_CHECK(WITH_LOCK(::cs_main, return manager.DeleteChainstate(snapshot))); // Accept Kernel's null mempool
     128                 :           1 : }
     129                 :             : 
     130                 :             : //! Test rebalancing the caches associated with each chainstate.
     131   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(chainstatemanager_rebalance_caches, TestChain100Setup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     132                 :             : {
     133         [ +  - ]:           1 :     ChainstateManager& manager = *m_node.chainman;
     134                 :             : 
     135                 :           1 :     size_t max_cache = 10000;
     136                 :           1 :     manager.m_total_coinsdb_cache = max_cache;
     137                 :           1 :     manager.m_total_coinstip_cache = max_cache;
     138                 :             : 
     139                 :           1 :     std::vector<Chainstate*> chainstates;
     140                 :             : 
     141                 :             :     // Create a legacy (IBD) chainstate.
     142                 :             :     //
     143         [ +  - ]:           1 :     Chainstate& c1 = manager.ActiveChainstate();
     144         [ +  - ]:           1 :     chainstates.push_back(&c1);
     145                 :           1 :     {
     146         [ +  - ]:           1 :         LOCK(::cs_main);
     147         [ +  - ]:           1 :         c1.InitCoinsCache(8_MiB);
     148         [ +  - ]:           1 :         manager.MaybeRebalanceCaches();
     149                 :           0 :     }
     150                 :             : 
     151   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(c1.m_coinstip_cache_size_bytes, max_cache);
     152   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(c1.m_coinsdb_cache_size_bytes, max_cache);
     153                 :             : 
     154                 :             :     // Create a snapshot-based chainstate.
     155                 :             :     //
     156   [ +  -  +  -  :           4 :     CBlockIndex* snapshot_base{WITH_LOCK(manager.GetMutex(), return manager.ActiveChain()[manager.ActiveChain().Height() / 2])};
          -  +  +  -  +  
                      - ]
     157   [ +  -  +  -  :           3 :     Chainstate& c2{WITH_LOCK(::cs_main, return manager.AddChainstate(std::make_unique<Chainstate>(nullptr, manager.m_blockman, manager, *snapshot_base->phashBlock)))};
                   +  - ]
     158         [ +  - ]:           1 :     chainstates.push_back(&c2);
     159         [ +  - ]:           1 :     c2.InitCoinsDB(
     160                 :             :         /*cache_size_bytes=*/8_MiB, /*in_memory=*/true, /*should_wipe=*/false);
     161                 :             : 
     162                 :             :     // Reset IBD state so IsInitialBlockDownload() returns true and causes
     163                 :             :     // MaybeRebalanceCaches() to prioritize the snapshot chainstate, giving it
     164                 :             :     // more cache space than the snapshot chainstate. Calling ResetIbd() is
     165                 :             :     // necessary because m_cached_is_ibd is already latched to false before
     166                 :             :     // the test starts due to the test setup. After ResetIbd() is called,
     167                 :             :     // IsInitialBlockDownload() will return true because at this point the active
     168                 :             :     // chainstate has a null chain tip.
     169         [ +  - ]:           1 :     static_cast<TestChainstateManager&>(manager).ResetIbd();
     170                 :             : 
     171                 :           1 :     {
     172         [ +  - ]:           1 :         LOCK(::cs_main);
     173         [ +  - ]:           1 :         c2.InitCoinsCache(8_MiB);
     174         [ +  - ]:           1 :         manager.MaybeRebalanceCaches();
     175                 :           0 :     }
     176                 :             : 
     177   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(c1.m_coinstip_cache_size_bytes, size_t(max_cache * 0.05));
     178   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(c1.m_coinsdb_cache_size_bytes, size_t(max_cache * 0.05));
     179   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(c2.m_coinstip_cache_size_bytes, size_t(max_cache * 0.95));
     180   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(c2.m_coinsdb_cache_size_bytes, size_t(max_cache * 0.95));
     181                 :           1 : }
     182                 :             : 
     183   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(chainstatemanager_ibd_exit_after_loading_blocks, ChainTestingSetup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     184                 :             : {
     185                 :           1 :     CBlockIndex tip;
     186         [ -  + ]:           1 :     ChainstateManager& chainman{*Assert(m_node.chainman)};
     187                 :          33 :     auto apply{[&](bool cached_is_ibd, bool loading_blocks, bool tip_exists, bool enough_work, bool tip_recent) {
     188                 :          32 :         LOCK(::cs_main);
     189         [ +  - ]:          32 :         chainman.ResetChainstates();
     190         [ +  - ]:          32 :         chainman.InitializeChainstate(m_node.mempool.get());
     191                 :             : 
     192                 :          32 :         const auto recent_time{Now<NodeSeconds>() - chainman.m_options.max_tip_age};
     193                 :             : 
     194         [ +  + ]:          32 :         chainman.m_cached_is_ibd.store(cached_is_ibd, std::memory_order_relaxed);
     195         [ +  + ]:          32 :         chainman.m_blockman.m_importing = loading_blocks;
     196         [ +  + ]:          32 :         if (tip_exists) {
     197   [ +  +  +  -  :          32 :             tip.nChainWork = chainman.MinimumChainWork() - (enough_work ? 0 : 1);
                   +  + ]
     198   [ +  +  +  - ]:          16 :             tip.nTime = (recent_time - (tip_recent ? 0h : 100h)).time_since_epoch().count();
     199   [ +  -  +  - ]:          16 :             chainman.ActiveChain().SetTip(tip);
     200                 :             :         } else {
     201   [ +  -  -  +  :          16 :             assert(!chainman.ActiveChain().Tip());
                   -  - ]
     202                 :             :         }
     203         [ +  - ]:          32 :         chainman.UpdateIBDStatus();
     204                 :           1 :     }};
     205                 :             : 
     206         [ +  + ]:           3 :     for (const bool cached_is_ibd : {false, true}) {
     207         [ +  + ]:           6 :         for (const bool loading_blocks : {false, true}) {
     208         [ +  + ]:          12 :             for (const bool tip_exists : {false, true}) {
     209         [ +  + ]:          24 :                 for (const bool enough_work : {false, true}) {
     210         [ +  + ]:          48 :                     for (const bool tip_recent : {false, true}) {
     211                 :          32 :                         apply(cached_is_ibd, loading_blocks, tip_exists, enough_work, tip_recent);
     212   [ +  +  +  +  :          32 :                         const bool expected_ibd = cached_is_ibd && (loading_blocks || !tip_exists || !enough_work || !tip_recent);
                   +  + ]
     213         [ +  - ]:          32 :                         BOOST_CHECK_EQUAL(chainman.IsInitialBlockDownload(), expected_ibd);
     214                 :             :                     }
     215                 :             :                 }
     216                 :             :             }
     217                 :             :         }
     218                 :             :     }
     219                 :           1 : }
     220                 :             : 
     221                 :           8 : struct SnapshotTestSetup : TestChain100Setup {
     222                 :             :     // Run with coinsdb on the filesystem to support, e.g., moving invalidated
     223                 :             :     // chainstate dirs to "*_invalid".
     224                 :             :     //
     225                 :             :     // Note that this means the tests run considerably slower than in-memory DB
     226                 :             :     // tests, but we can't otherwise test this functionality since it relies on
     227                 :             :     // destructive filesystem operations.
     228                 :           4 :     SnapshotTestSetup() : TestChain100Setup{
     229                 :             :                               {},
     230                 :             :                               {
     231                 :             :                                   .coins_db_in_memory = false,
     232                 :             :                                   .block_tree_db_in_memory = false,
     233                 :             :                               },
     234         [ +  - ]:           4 :                           }
     235                 :             :     {
     236                 :           4 :     }
     237                 :             : 
     238                 :           4 :     std::tuple<Chainstate*, Chainstate*> SetupSnapshot()
     239                 :             :     {
     240         [ -  + ]:           4 :         ChainstateManager& chainman = *Assert(m_node.chainman);
     241                 :             : 
     242                 :           4 :         {
     243                 :           4 :             LOCK(::cs_main);
     244   [ +  -  +  -  :           8 :             BOOST_CHECK(!chainman.CurrentChainstate().m_from_snapshot_blockhash);
                   +  - ]
     245   [ +  -  +  -  :          12 :             BOOST_CHECK(!node::FindAssumeutxoChainstateDir(chainman.m_options.datadir));
          +  -  -  +  +  
                      - ]
     246                 :           0 :         }
     247                 :             : 
     248                 :           4 :         size_t initial_size;
     249                 :           4 :         size_t initial_total_coins{100};
     250                 :             : 
     251                 :             :         // Make some initial assertions about the contents of the chainstate.
     252                 :           4 :         {
     253                 :           4 :             LOCK(::cs_main);
     254   [ +  -  +  - ]:           4 :             CCoinsViewCache& ibd_coinscache = chainman.ActiveChainstate().CoinsTip();
     255         [ +  - ]:           4 :             initial_size = ibd_coinscache.GetCacheSize();
     256                 :           4 :             size_t total_coins{0};
     257                 :             : 
     258         [ +  + ]:         404 :             for (CTransactionRef& txn : m_coinbase_txns) {
     259         [ +  - ]:         400 :                 COutPoint op{txn->GetHash(), 0};
     260   [ +  -  +  -  :         800 :                 BOOST_CHECK(ibd_coinscache.HaveCoin(op));
                   +  - ]
     261                 :         400 :                 total_coins++;
     262                 :             :             }
     263                 :             : 
     264   [ +  -  +  - ]:           4 :             BOOST_CHECK_EQUAL(total_coins, initial_total_coins);
     265   [ +  -  +  -  :           4 :             BOOST_CHECK_EQUAL(initial_size, initial_total_coins);
                   +  - ]
     266                 :           0 :         }
     267                 :             : 
     268                 :           4 :         Chainstate& validation_chainstate = chainman.ActiveChainstate();
     269                 :             : 
     270                 :             :         // Snapshot should refuse to load at this height.
     271   [ +  -  +  - ]:           8 :         BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(this));
     272   [ +  -  +  - ]:           8 :         BOOST_CHECK(!chainman.ActiveChainstate().m_from_snapshot_blockhash);
     273                 :             : 
     274                 :             :         // Mine 10 more blocks, putting at us height 110 where a valid assumeutxo value can
     275                 :             :         // be found.
     276                 :           4 :         constexpr int snapshot_height = 110;
     277                 :           4 :         mineBlocks(10);
     278                 :           4 :         initial_size += 10;
     279                 :           4 :         initial_total_coins += 10;
     280                 :             : 
     281                 :             :         // Should not load malleated snapshots
     282   [ +  -  +  -  :          16 :         BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
                   +  - ]
     283                 :             :             this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
     284                 :             :                 // A UTXO is missing but count is correct
     285                 :             :                 metadata.m_coins_count -= 1;
     286                 :             : 
     287                 :             :                 Txid txid;
     288                 :             :                 auto_infile >> txid;
     289                 :             :                 // coins size
     290                 :             :                 (void)ReadCompactSize(auto_infile);
     291                 :             :                 // vout index
     292                 :             :                 (void)ReadCompactSize(auto_infile);
     293                 :             :                 Coin coin;
     294                 :             :                 auto_infile >> coin;
     295                 :             :         }));
     296                 :             : 
     297   [ +  -  +  -  :          12 :         BOOST_CHECK(!node::FindAssumeutxoChainstateDir(chainman.m_options.datadir));
                   -  + ]
     298                 :             : 
     299   [ -  +  +  -  :          12 :         BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
                   +  - ]
     300                 :             :             this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
     301                 :             :                 // Coins count is larger than coins in file
     302                 :             :                 metadata.m_coins_count += 1;
     303                 :             :         }));
     304   [ -  +  +  -  :          12 :         BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
                   +  - ]
     305                 :             :             this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
     306                 :             :                 // Coins count is smaller than coins in file
     307                 :             :                 metadata.m_coins_count -= 1;
     308                 :             :         }));
     309   [ -  +  +  -  :          12 :         BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
                   +  - ]
     310                 :             :             this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
     311                 :             :                 // Wrong hash
     312                 :             :                 metadata.m_base_blockhash = uint256::ZERO;
     313                 :             :         }));
     314   [ -  +  +  -  :          12 :         BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
                   +  - ]
     315                 :             :             this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
     316                 :             :                 // Wrong hash
     317                 :             :                 metadata.m_base_blockhash = uint256::ONE;
     318                 :             :         }));
     319                 :             : 
     320   [ +  -  +  - ]:           8 :         BOOST_REQUIRE(CreateAndActivateUTXOSnapshot(this));
     321   [ +  -  +  -  :          12 :         BOOST_CHECK(fs::exists(*node::FindAssumeutxoChainstateDir(chainman.m_options.datadir)));
             +  -  +  - ]
     322                 :             : 
     323                 :             :         // Ensure our active chain is the snapshot chainstate.
     324   [ +  -  +  - ]:          12 :         BOOST_CHECK(!chainman.ActiveChainstate().m_from_snapshot_blockhash->IsNull());
     325                 :             : 
     326                 :           4 :         Chainstate& snapshot_chainstate = chainman.ActiveChainstate();
     327                 :             : 
     328                 :           4 :         {
     329                 :           4 :             LOCK(::cs_main);
     330                 :             : 
     331   [ +  -  +  - ]:           4 :             fs::path found = *node::FindAssumeutxoChainstateDir(chainman.m_options.datadir);
     332                 :             : 
     333                 :             :             // Note: WriteSnapshotBaseBlockhash() is implicitly tested above.
     334   [ +  -  -  +  :           8 :             BOOST_CHECK_EQUAL(
          +  -  +  -  +  
                      - ]
     335                 :             :                 *node::ReadSnapshotBaseBlockhash(found),
     336                 :             :                 *Assert(chainman.CurrentChainstate().m_from_snapshot_blockhash));
     337         [ +  - ]:           4 :         }
     338                 :             : 
     339                 :           4 :         const auto& au_data = ::Params().AssumeutxoForHeight(snapshot_height);
     340   [ +  -  +  - ]:          12 :         const CBlockIndex* tip = WITH_LOCK(chainman.GetMutex(), return chainman.ActiveTip());
     341                 :             : 
     342         [ +  - ]:           4 :         BOOST_CHECK_EQUAL(tip->m_chain_tx_count, au_data->m_chain_tx_count);
     343                 :             : 
     344                 :             :         // To be checked against later when we try loading a subsequent snapshot.
     345   [ -  +  +  - ]:           8 :         uint256 loaded_snapshot_blockhash{*Assert(WITH_LOCK(chainman.GetMutex(), return chainman.CurrentChainstate().m_from_snapshot_blockhash))};
     346                 :             : 
     347                 :             :         // Make some assertions about the both chainstates. These checks ensure the
     348                 :             :         // legacy chainstate hasn't changed and that the newly created chainstate
     349                 :             :         // reflects the expected content.
     350                 :           4 :         {
     351                 :           4 :             LOCK(::cs_main);
     352                 :           4 :             int chains_tested{0};
     353                 :             : 
     354         [ +  + ]:          12 :             for (const auto& chainstate : chainman.m_chainstates) {
     355   [ +  -  +  -  :           8 :                 BOOST_TEST_MESSAGE("Checking coins in " << chainstate->ToString());
             +  -  +  - ]
     356         [ +  - ]:           8 :                 CCoinsViewCache& coinscache = chainstate->CoinsTip();
     357                 :             : 
     358                 :             :                 // Both caches will be empty initially.
     359   [ +  -  +  -  :           8 :                 BOOST_CHECK_EQUAL((unsigned int)0, coinscache.GetCacheSize());
                   +  - ]
     360                 :             : 
     361                 :           8 :                 size_t total_coins{0};
     362                 :             : 
     363         [ +  + ]:         888 :                 for (CTransactionRef& txn : m_coinbase_txns) {
     364         [ +  - ]:         880 :                     COutPoint op{txn->GetHash(), 0};
     365   [ +  -  +  -  :        1760 :                     BOOST_CHECK(coinscache.HaveCoin(op));
                   +  - ]
     366                 :         880 :                     total_coins++;
     367                 :             :                 }
     368                 :             : 
     369   [ +  -  +  -  :           8 :                 BOOST_CHECK_EQUAL(initial_size , coinscache.GetCacheSize());
                   +  - ]
     370   [ +  -  +  - ]:           8 :                 BOOST_CHECK_EQUAL(total_coins, initial_total_coins);
     371                 :           8 :                 chains_tested++;
     372                 :             :             }
     373                 :             : 
     374   [ +  -  +  -  :           4 :             BOOST_CHECK_EQUAL(chains_tested, 2);
                   +  - ]
     375                 :           0 :         }
     376                 :             : 
     377                 :             :         // Mine some new blocks on top of the activated snapshot chainstate.
     378                 :           4 :         constexpr size_t new_coins{100};
     379                 :           4 :         mineBlocks(new_coins);  // Defined in TestChain100Setup.
     380                 :             : 
     381                 :           4 :         {
     382                 :           4 :             LOCK(::cs_main);
     383                 :           4 :             size_t coins_in_active{0};
     384                 :           4 :             size_t coins_in_background{0};
     385                 :           4 :             size_t coins_missing_from_background{0};
     386                 :             : 
     387         [ +  + ]:          12 :             for (const auto& chainstate : chainman.m_chainstates) {
     388   [ +  -  +  -  :           8 :                 BOOST_TEST_MESSAGE("Checking coins in " << chainstate->ToString());
             +  -  +  - ]
     389         [ +  - ]:           8 :                 CCoinsViewCache& coinscache = chainstate->CoinsTip();
     390         [ +  - ]:           8 :                 bool is_background = chainstate.get() != &chainman.ActiveChainstate();
     391                 :             : 
     392         [ +  + ]:        1688 :                 for (CTransactionRef& txn : m_coinbase_txns) {
     393         [ +  - ]:        1680 :                     COutPoint op{txn->GetHash(), 0};
     394   [ +  -  +  + ]:        1680 :                     if (coinscache.HaveCoin(op)) {
     395         [ +  + ]:        1280 :                         (is_background ? coins_in_background : coins_in_active)++;
     396         [ +  - ]:         400 :                     } else if (is_background) {
     397                 :         400 :                         coins_missing_from_background++;
     398                 :             :                     }
     399                 :             :                 }
     400                 :             :             }
     401                 :             : 
     402   [ +  -  +  - ]:           4 :             BOOST_CHECK_EQUAL(coins_in_active, initial_total_coins + new_coins);
     403   [ +  -  +  - ]:           4 :             BOOST_CHECK_EQUAL(coins_in_background, initial_total_coins);
     404   [ +  -  +  -  :           4 :             BOOST_CHECK_EQUAL(coins_missing_from_background, new_coins);
                   +  - ]
     405                 :           0 :         }
     406                 :             : 
     407                 :             :         // Snapshot should refuse to load after one has already loaded.
     408   [ +  -  +  - ]:           8 :         BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(this));
     409                 :             : 
     410                 :             :         // Snapshot blockhash should be unchanged.
     411         [ +  - ]:           4 :         BOOST_CHECK_EQUAL(
     412                 :             :             *chainman.ActiveChainstate().m_from_snapshot_blockhash,
     413                 :             :             loaded_snapshot_blockhash);
     414                 :           4 :         return std::make_tuple(&validation_chainstate, &snapshot_chainstate);
     415                 :             :     }
     416                 :             : 
     417                 :             :     // Simulate a restart of the node by flushing all state to disk, clearing the
     418                 :             :     // existing ChainstateManager, and unloading the block index.
     419                 :             :     //
     420                 :             :     // @returns a reference to the "restarted" ChainstateManager
     421                 :           3 :     ChainstateManager& SimulateNodeRestart()
     422                 :             :     {
     423         [ -  + ]:           3 :         ChainstateManager& chainman = *Assert(m_node.chainman);
     424                 :             : 
     425         [ +  - ]:           3 :         BOOST_TEST_MESSAGE("Simulating node restart");
     426                 :           3 :         {
     427                 :           3 :             LOCK(chainman.GetMutex());
     428         [ +  + ]:           9 :             for (const auto& cs : chainman.m_chainstates) {
     429   [ +  +  +  - ]:          11 :                 if (cs->CanFlushToDisk()) cs->ForceFlushStateToDisk();
     430                 :             :             }
     431                 :           0 :         }
     432                 :           3 :         {
     433                 :             :             // Process all callbacks referring to the old manager before wiping it.
     434                 :           3 :             m_node.validation_signals->SyncWithValidationInterfaceQueue();
     435         [ +  - ]:           3 :             m_node.block_template_manager.reset();
     436                 :           3 :             LOCK(::cs_main);
     437         [ +  - ]:           3 :             chainman.ResetChainstates();
     438   [ +  -  -  +  :           3 :             BOOST_CHECK_EQUAL(chainman.m_chainstates.size(), 0);
                   +  - ]
     439   [ -  +  -  +  :           3 :             m_node.notifications = std::make_unique<KernelNotifications>(Assert(m_node.shutdown_request), m_node.exit_status, *Assert(m_node.warnings));
                   +  - ]
     440                 :           3 :             const ChainstateManager::Options chainman_opts{
     441         [ +  - ]:           3 :                 .chainparams = ::Params(),
     442                 :           3 :                 .datadir = chainman.m_options.datadir,
     443                 :           3 :                 .notifications = *m_node.notifications,
     444                 :           3 :                 .signals = m_node.validation_signals.get(),
     445   [ +  -  +  - ]:           3 :             };
     446                 :           3 :             const BlockManager::Options blockman_opts{
     447                 :           3 :                 .chainparams = chainman_opts.chainparams,
     448                 :             :                 .blocks_dir = m_args.GetBlocksDirPath(),
     449                 :           3 :                 .notifications = chainman_opts.notifications,
     450                 :             :                 .block_tree_db_params = DBParams{
     451   [ +  -  +  - ]:           9 :                     .path = chainman.m_options.datadir / "blocks" / "index",
     452                 :           3 :                     .cache_bytes = m_kernel_cache_sizes.block_tree_db,
     453                 :           3 :                     .memory_only = m_block_tree_db_in_memory,
     454                 :             :                 },
     455   [ +  -  +  - ]:           6 :             };
     456                 :             :             // For robustness, ensure the old manager is destroyed before creating a
     457                 :             :             // new one.
     458         [ +  - ]:           3 :             m_node.chainman.reset();
     459   [ -  +  +  - ]:           3 :             m_node.chainman = std::make_unique<ChainstateManager>(*Assert(m_node.shutdown_signal), chainman_opts, blockman_opts);
     460         [ +  - ]:           3 :             CreateBlockTemplateManager();
     461         [ +  - ]:           6 :         }
     462         [ -  + ]:           3 :         return *Assert(m_node.chainman);
     463                 :             :     }
     464                 :             : };
     465                 :             : 
     466                 :             : //! Test basic snapshot activation.
     467   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(chainstatemanager_activate_snapshot, SnapshotTestSetup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     468                 :             : {
     469                 :           1 :     this->SetupSnapshot();
     470                 :           1 : }
     471                 :             : 
     472                 :             : //! Test LoadBlockIndex behavior when multiple chainstates are in use.
     473                 :             : //!
     474                 :             : //! - First, verify that setBlockIndexCandidates is as expected when using a single,
     475                 :             : //!   fully-validating chainstate.
     476                 :             : //!
     477                 :             : //! - Then mark a region of the chain as missing data and introduce a second chainstate
     478                 :             : //!   that will tolerate assumed-valid blocks. Run LoadBlockIndex() and ensure that the first
     479                 :             : //!   chainstate only contains fully validated blocks and the other chainstate contains all blocks,
     480                 :             : //!   except those marked assume-valid, because those entries don't HAVE_DATA.
     481                 :             : //!
     482   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(chainstatemanager_loadblockindex, TestChain100Setup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     483                 :             : {
     484         [ -  + ]:           1 :     ChainstateManager& chainman = *Assert(m_node.chainman);
     485                 :           1 :     Chainstate& cs1 = chainman.ActiveChainstate();
     486                 :             : 
     487                 :           1 :     int num_indexes{0};
     488                 :             :     // Blocks in range [assumed_valid_start_idx, last_assumed_valid_idx) will be
     489                 :             :     // marked as assumed-valid and not having data.
     490                 :           1 :     const int expected_assumed_valid{20};
     491                 :           1 :     const int last_assumed_valid_idx{111};
     492                 :           1 :     const int assumed_valid_start_idx = last_assumed_valid_idx - expected_assumed_valid;
     493                 :             : 
     494                 :             :     // Mine to height 120, past the hardcoded regtest assumeutxo snapshot at
     495                 :             :     // height 110
     496                 :           1 :     mineBlocks(20);
     497                 :             : 
     498                 :           1 :     CBlockIndex* validated_tip{nullptr};
     499                 :           1 :     CBlockIndex* assumed_base{nullptr};
     500   [ +  -  -  +  :           4 :     CBlockIndex* assumed_tip{WITH_LOCK(chainman.GetMutex(), return chainman.ActiveChain().Tip())};
                   +  - ]
     501         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(assumed_tip->nHeight, 120);
     502                 :             : 
     503                 :           3 :     auto reload_all_block_indexes = [&]() {
     504                 :           2 :         LOCK(chainman.GetMutex());
     505                 :             :         // For completeness, we also reset the block sequence counters to
     506                 :             :         // ensure that no state which affects the ranking of tip-candidates is
     507                 :             :         // retained (even though this isn't strictly necessary).
     508                 :           2 :         chainman.ResetBlockSequenceCounters();
     509         [ +  + ]:           5 :         for (const auto& cs : chainman.m_chainstates) {
     510         [ +  - ]:           3 :             cs->ClearBlockIndexCandidates();
     511   [ +  -  +  - ]:           6 :             BOOST_CHECK(cs->setBlockIndexCandidates.empty());
     512                 :             :         }
     513         [ +  - ]:           2 :         chainman.LoadBlockIndex();
     514         [ +  + ]:           5 :         for (const auto& cs : chainman.m_chainstates) {
     515         [ +  - ]:           3 :             cs->PopulateBlockIndexCandidates();
     516                 :             :         }
     517                 :           3 :     };
     518                 :             : 
     519                 :             :     // Ensure that without any assumed-valid BlockIndex entries, only the current tip is
     520                 :             :     // considered as a candidate.
     521                 :           1 :     reload_all_block_indexes();
     522         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(cs1.setBlockIndexCandidates.size(), 1);
     523                 :             : 
     524                 :             :     // Reset some region of the chain's nStatus, removing the HAVE_DATA flag.
     525   [ -  +  +  + ]:         122 :     for (int i = 0; i <= cs1.m_chain.Height(); ++i) {
     526                 :         121 :         LOCK(::cs_main);
     527         [ +  - ]:         121 :         auto index = cs1.m_chain[i];
     528                 :             : 
     529                 :             :         // Blocks with heights in range [91, 110] are marked as missing data.
     530         [ +  + ]:         121 :         if (i < last_assumed_valid_idx && i >= assumed_valid_start_idx) {
     531                 :          20 :             index->nStatus = BlockStatus::BLOCK_VALID_TREE;
     532                 :          20 :             index->nTx = 0;
     533                 :          20 :             index->m_chain_tx_count = 0;
     534                 :             :         }
     535                 :             : 
     536                 :         121 :         ++num_indexes;
     537                 :             : 
     538                 :             :         // Note the last fully-validated block as the expected validated tip.
     539         [ +  + ]:         121 :         if (i == (assumed_valid_start_idx - 1)) {
     540                 :           1 :             validated_tip = index;
     541                 :             :         }
     542                 :             :         // Note the last assumed valid block as the snapshot base
     543         [ +  + ]:         121 :         if (i == last_assumed_valid_idx - 1) {
     544                 :           1 :             assumed_base = index;
     545                 :             :         }
     546                 :         121 :     }
     547                 :             : 
     548                 :             :     // Note: cs2's tip is not set when ActivateExistingSnapshot is called.
     549   [ +  -  +  -  :           3 :     Chainstate& cs2{WITH_LOCK(::cs_main, return chainman.AddChainstate(std::make_unique<Chainstate>(nullptr, chainman.m_blockman, chainman, *assumed_base->phashBlock)))};
                   +  - ]
     550                 :             : 
     551                 :             :     // Set tip of the fully validated chain to be the validated tip
     552                 :           1 :     cs1.m_chain.SetTip(*validated_tip);
     553                 :             : 
     554                 :             :     // Set tip of the assume-valid-based chain to the assume-valid block
     555                 :           1 :     cs2.m_chain.SetTip(*assumed_base);
     556                 :             : 
     557                 :             :     // Sanity check test variables.
     558         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(num_indexes, 121); // 121 total blocks, including genesis
     559         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(assumed_tip->nHeight, 120);  // original chain has height 120
     560         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(validated_tip->nHeight, 90); // current cs1 chain has height 90
     561         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(assumed_base->nHeight, 110); // current cs2 chain has height 110
     562                 :             : 
     563                 :             :     // Regenerate cs1.setBlockIndexCandidates and cs2.setBlockIndexCandidate and
     564                 :             :     // check contents below.
     565                 :           1 :     reload_all_block_indexes();
     566                 :             : 
     567                 :             :     // The fully validated chain should only have the current validated tip
     568                 :             :     // as a candidate (block 90). Specifically:
     569                 :             :     //
     570                 :             :     // - It does not have blocks 0-89 because they contain less work than the
     571                 :             :     //   chain tip.
     572                 :             :     //
     573                 :             :     // - It has block 90 because it has data and equal work to the chain tip,
     574                 :             :     //   (since it is the chain tip).
     575                 :             :     //
     576                 :             :     // - It does not have blocks 91-110 because they do not contain data.
     577                 :             :     //
     578                 :             :     // - It does not have any blocks after height 110 because cs1 is a background
     579                 :             :     //   chainstate, and only blocks that are ancestors of the snapshot block
     580                 :             :     //   are added as candidates for the background chainstate.
     581         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(cs1.setBlockIndexCandidates.size(), 1);
     582         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(cs1.setBlockIndexCandidates.count(validated_tip), 1);
     583                 :             : 
     584                 :             :     // The assumed-valid tolerant chain has the assumed valid base as a
     585                 :             :     // candidate, but otherwise has none of the assumed-valid (which do not
     586                 :             :     // HAVE_DATA) blocks as candidates.
     587                 :             :     //
     588                 :             :     // Specifically:
     589                 :             :     // - All blocks below height 110 are not candidates, because cs2 chain tip
     590                 :             :     //   has height 110 and they have less work than it does.
     591                 :             :     //
     592                 :             :     // - Block 110 is a candidate even though it does not have data, because it
     593                 :             :     //   is the snapshot block, which is assumed valid.
     594                 :             :     //
     595                 :             :     // - Blocks 111-120 are added because they have data.
     596                 :             : 
     597                 :             :     // Check that block 90 is absent
     598         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(validated_tip), 0);
     599                 :             :     // Check that block 109 is absent
     600         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(assumed_base->pprev), 0);
     601                 :             :     // Check that block 110 is present
     602         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(assumed_base), 1);
     603                 :             :     // Check that block 120 is present
     604         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(assumed_tip), 1);
     605                 :             :     // Check that 11 blocks total are present.
     606         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.size(), num_indexes - last_assumed_valid_idx + 1);
     607                 :           1 : }
     608                 :             : 
     609   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(loadblockindex_invalid_descendants, TestChain100Setup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     610                 :             : {
     611         [ -  + ]:           1 :     LOCK(Assert(m_node.chainman)->GetMutex());
     612                 :             :     // consider the chain of blocks grand_parent <- parent <- child
     613                 :             :     // intentionally mark:
     614                 :             :     //   - grand_parent: BLOCK_FAILED_VALID
     615                 :             :     //   - parent: BLOCK_FAILED_CHILD
     616                 :             :     //   - child: not invalid
     617                 :             :     // Test that when the block index is loaded, all blocks are marked as BLOCK_FAILED_VALID
     618   [ +  -  -  + ]:           1 :     auto* child{m_node.chainman->ActiveChain().Tip()};
     619                 :           1 :     auto* parent{child->pprev};
     620                 :           1 :     auto* grand_parent{parent->pprev};
     621                 :           1 :     grand_parent->nStatus = (grand_parent->nStatus | BLOCK_FAILED_VALID);
     622                 :           1 :     parent->nStatus = (parent->nStatus & ~BLOCK_FAILED_VALID) | BLOCK_FAILED_CHILD;
     623                 :           1 :     child->nStatus = (child->nStatus & ~BLOCK_FAILED_VALID);
     624                 :             : 
     625                 :             :     // Reload block index to recompute block status validity flags.
     626         [ +  - ]:           1 :     m_node.chainman->LoadBlockIndex();
     627                 :             : 
     628                 :             :     // check grand_parent, parent, child is marked as BLOCK_FAILED_VALID after reloading the block index
     629   [ +  -  +  -  :           2 :     BOOST_CHECK(grand_parent->nStatus & BLOCK_FAILED_VALID);
                   +  - ]
     630   [ +  -  +  -  :           2 :     BOOST_CHECK(parent->nStatus & BLOCK_FAILED_VALID);
                   +  - ]
     631   [ +  -  +  -  :           2 :     BOOST_CHECK(child->nStatus & BLOCK_FAILED_VALID);
                   +  - ]
     632                 :           1 : }
     633                 :             : 
     634                 :             : //! Verify that ReconsiderBlock clears failure flags for the target block, its ancestors, and descendants,
     635                 :             : //! but not for sibling forks that diverge from a shared ancestor.
     636   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(invalidate_block_and_reconsider_fork, TestChain100Setup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     637                 :             : {
     638         [ -  + ]:           1 :     ChainstateManager& chainman = *Assert(m_node.chainman);
     639                 :           1 :     Chainstate& chainstate = chainman.ActiveChainstate();
     640                 :             : 
     641                 :             :     // we have a chain of 100 blocks: genesis(0) <- ... <- block98 <- block99 <- block100
     642                 :           1 :     CBlockIndex* block98;
     643                 :           1 :     CBlockIndex* block99;
     644                 :           1 :     CBlockIndex* block100;
     645                 :           1 :     {
     646                 :           1 :         LOCK(chainman.GetMutex());
     647   [ +  -  -  + ]:           1 :         block98 = chainman.ActiveChain()[98];
     648   [ +  -  -  + ]:           1 :         block99 = chainman.ActiveChain()[99];
     649   [ +  -  -  +  :           2 :         block100 = chainman.ActiveChain()[100];
                   +  - ]
     650                 :           0 :     }
     651                 :             : 
     652                 :             :     // create the following block constellation:
     653                 :             :     // genesis(0) <- ... <- block98 <- block99  <- block100
     654                 :             :     //                              <- block99' <- block100'
     655                 :             :     // by temporarily invalidating block99. the chain tip now falls to block98,
     656                 :             :     // mine 2 new blocks on top of block 98 (block99' and block100') and then restore block99 and block 100.
     657         [ +  - ]:           1 :     BlockValidationState state;
     658   [ +  -  +  -  :           2 :     BOOST_REQUIRE(chainstate.InvalidateBlock(state, block99));
             +  -  +  - ]
     659   [ +  -  +  +  :           5 :     BOOST_REQUIRE(WITH_LOCK(cs_main, return chainman.ActiveChain().Tip()) == block98);
             +  -  +  - ]
     660   [ +  -  +  -  :           2 :     CScript coinbase_script = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
                   +  - ]
     661         [ +  + ]:           3 :     for (int i = 0; i < 2; ++i) {
     662         [ +  - ]:           4 :         CreateAndProcessBlock({}, coinbase_script);
     663                 :             :     }
     664                 :           1 :     const CBlockIndex* fork_block99;
     665                 :           1 :     const CBlockIndex* fork_block100;
     666                 :           1 :     {
     667         [ +  - ]:           1 :         LOCK(chainman.GetMutex());
     668   [ +  -  -  + ]:           1 :         fork_block99 = chainman.ActiveChain()[99];
     669   [ +  -  +  -  :           2 :         BOOST_REQUIRE(fork_block99->pprev == block98);
                   +  - ]
     670   [ +  -  -  + ]:           1 :         fork_block100 = chainman.ActiveChain()[100];
     671   [ +  -  +  -  :           2 :         BOOST_REQUIRE(fork_block100->pprev == fork_block99);
                   +  - ]
     672                 :           0 :     }
     673                 :             :     // Restore original block99 and block100
     674                 :           1 :     {
     675         [ +  - ]:           1 :         LOCK(chainman.GetMutex());
     676         [ +  - ]:           1 :         chainstate.ResetBlockFailureFlags(block99);
     677         [ +  - ]:           1 :         chainman.RecalculateBestHeader();
     678                 :           0 :     }
     679         [ +  - ]:           1 :     chainstate.ActivateBestChain(state);
     680   [ +  -  +  +  :           5 :     BOOST_REQUIRE(WITH_LOCK(cs_main, return chainman.ActiveChain().Tip()) == block100);
             +  -  +  - ]
     681                 :             : 
     682                 :           1 :     {
     683         [ +  - ]:           1 :         LOCK(chainman.GetMutex());
     684   [ +  -  +  -  :           2 :         BOOST_CHECK(!(block100->nStatus & BLOCK_FAILED_VALID));
                   +  - ]
     685   [ +  -  +  -  :           2 :         BOOST_CHECK(!(block99->nStatus & BLOCK_FAILED_VALID));
                   +  - ]
     686   [ +  -  +  -  :           2 :         BOOST_CHECK(!(fork_block100->nStatus & BLOCK_FAILED_VALID));
                   +  - ]
     687   [ +  -  +  -  :           2 :         BOOST_CHECK(!(fork_block99->nStatus & BLOCK_FAILED_VALID));
                   +  - ]
     688                 :           0 :     }
     689                 :             : 
     690                 :             :     // Invalidate block98
     691   [ +  -  +  -  :           2 :     BOOST_REQUIRE(chainstate.InvalidateBlock(state, block98));
             +  -  +  - ]
     692                 :             : 
     693                 :           1 :     {
     694         [ +  - ]:           1 :         LOCK(chainman.GetMutex());
     695                 :             :         // block98 and all descendants of block98 are marked BLOCK_FAILED_VALID
     696   [ +  -  +  -  :           2 :         BOOST_CHECK(block98->nStatus & BLOCK_FAILED_VALID);
                   +  - ]
     697   [ +  -  +  -  :           2 :         BOOST_CHECK(block99->nStatus & BLOCK_FAILED_VALID);
                   +  - ]
     698   [ +  -  +  -  :           2 :         BOOST_CHECK(block100->nStatus & BLOCK_FAILED_VALID);
                   +  - ]
     699   [ +  -  +  -  :           2 :         BOOST_CHECK(fork_block99->nStatus & BLOCK_FAILED_VALID);
                   +  - ]
     700   [ +  -  +  -  :           2 :         BOOST_CHECK(fork_block100->nStatus & BLOCK_FAILED_VALID);
                   +  - ]
     701                 :           0 :     }
     702                 :             : 
     703                 :             :     // Reconsider block99. ResetBlockFailureFlags clears BLOCK_FAILED_VALID from
     704                 :             :     // block99 and its ancestors (block98) and descendants (block100)
     705                 :             :     // but NOT from block99' and block100' (not a direct ancestor/descendant)
     706                 :           1 :     {
     707         [ +  - ]:           1 :         LOCK(chainman.GetMutex());
     708         [ +  - ]:           1 :         chainstate.ResetBlockFailureFlags(block99);
     709         [ +  - ]:           1 :         chainman.RecalculateBestHeader();
     710                 :           0 :     }
     711         [ +  - ]:           1 :     chainstate.ActivateBestChain(state);
     712                 :           1 :     {
     713         [ +  - ]:           1 :         LOCK(chainman.GetMutex());
     714   [ +  -  +  -  :           2 :         BOOST_CHECK(!(block98->nStatus & BLOCK_FAILED_VALID));
                   +  - ]
     715   [ +  -  +  -  :           2 :         BOOST_CHECK(!(block99->nStatus & BLOCK_FAILED_VALID));
                   +  - ]
     716   [ +  -  +  -  :           2 :         BOOST_CHECK(!(block100->nStatus & BLOCK_FAILED_VALID));
                   +  - ]
     717   [ +  -  +  -  :           2 :         BOOST_CHECK(fork_block99->nStatus & BLOCK_FAILED_VALID);
                   +  - ]
     718   [ +  -  +  -  :           2 :         BOOST_CHECK(fork_block100->nStatus & BLOCK_FAILED_VALID);
                   +  - ]
     719                 :           1 :     }
     720                 :           2 : }
     721                 :             : 
     722                 :             : //! Ensure that snapshot chainstate can be loaded when found on disk after a
     723                 :             : //! restart, and that new blocks can be connected to both chainstates.
     724   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(chainstatemanager_snapshot_init, SnapshotTestSetup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     725                 :             : {
     726         [ -  + ]:           1 :     ChainstateManager& chainman = *Assert(m_node.chainman);
     727                 :           1 :     Chainstate& bg_chainstate = chainman.ActiveChainstate();
     728                 :             : 
     729                 :           1 :     this->SetupSnapshot();
     730                 :             : 
     731         [ +  - ]:           1 :     fs::path snapshot_chainstate_dir = *node::FindAssumeutxoChainstateDir(chainman.m_options.datadir);
     732   [ +  -  +  -  :           2 :     BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
             +  -  +  - ]
     733   [ +  -  +  -  :           3 :     BOOST_CHECK_EQUAL(snapshot_chainstate_dir, gArgs.GetDataDirNet() / "chainstate_snapshot");
                   +  - ]
     734                 :             : 
     735   [ +  -  +  -  :           3 :     BOOST_CHECK(WITH_LOCK(::cs_main, return chainman.CurrentChainstate().m_from_snapshot_blockhash));
             +  -  +  - ]
     736   [ +  -  +  - ]:           3 :     const uint256 snapshot_tip_hash = WITH_LOCK(chainman.GetMutex(),
     737                 :             :         return chainman.ActiveTip()->GetBlockHash());
     738                 :             : 
     739   [ +  +  +  -  :           2 :     BOOST_CHECK_EQUAL(WITH_LOCK(chainman.GetMutex(), return chainman.m_chainstates.size()), 2);
                   +  - ]
     740                 :             : 
     741                 :             :     // "Rewind" the background chainstate so that its tip is not at the
     742                 :             :     // base block of the snapshot - this is so after simulating a node restart,
     743                 :             :     // it will initialize instead of attempting to complete validation.
     744                 :             :     //
     745                 :             :     // Note that this is not a realistic use of DisconnectTip().
     746         [ +  - ]:           1 :     DisconnectedBlockTransactions unused_pool{MAX_DISCONNECTED_TX_POOL_BYTES};
     747         [ +  - ]:           1 :     BlockValidationState unused_state;
     748                 :           1 :     {
     749   [ +  -  -  +  :           1 :         LOCK2(::cs_main, bg_chainstate.MempoolMutex());
                   +  - ]
     750   [ +  -  +  -  :           2 :         BOOST_CHECK(bg_chainstate.DisconnectTip(unused_state, &unused_pool));
             +  -  +  - ]
     751         [ +  - ]:           1 :         unused_pool.clear();  // to avoid queuedTx assertion errors on teardown
     752         [ +  - ]:           1 :     }
     753   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(bg_chainstate.m_chain.Height(), 109);
                   +  - ]
     754                 :             : 
     755                 :             :     // Test that simulating a shutdown (resetting ChainstateManager) and then performing
     756                 :             :     // chainstate reinitializing successfully reloads both chainstates.
     757         [ +  - ]:           1 :     ChainstateManager& chainman_restarted = this->SimulateNodeRestart();
     758                 :             : 
     759   [ +  -  +  -  :           1 :     BOOST_TEST_MESSAGE("Performing Load/Verify/Activate of chainstate");
                   +  - ]
     760                 :             : 
     761                 :             :     // This call reinitializes the chainstates.
     762         [ +  - ]:           1 :     this->LoadVerifyActivateChainstate();
     763                 :             : 
     764                 :           1 :     {
     765         [ +  - ]:           1 :         LOCK(chainman_restarted.GetMutex());
     766   [ +  -  -  +  :           1 :         BOOST_CHECK_EQUAL(chainman_restarted.m_chainstates.size(), 2);
                   +  - ]
     767                 :             :         // Background chainstate has height of 109 not 110 here due to a quirk
     768                 :             :         // of the LoadVerifyActivate only calling ActivateBestChain on one
     769                 :             :         // chainstate. The height would be 110 after a real restart, but it's
     770                 :             :         // fine for this test which is focused on the snapshot chainstate.
     771   [ +  -  -  +  :           1 :         BOOST_CHECK_EQUAL(chainman_restarted.m_chainstates[0]->m_chain.Height(), 109);
                   +  - ]
     772   [ +  -  -  +  :           1 :         BOOST_CHECK_EQUAL(chainman_restarted.m_chainstates[1]->m_chain.Height(), 210);
                   +  - ]
     773                 :             : 
     774   [ +  -  +  -  :           2 :         BOOST_CHECK(chainman_restarted.CurrentChainstate().m_from_snapshot_blockhash);
                   +  - ]
     775   [ +  -  +  -  :           2 :         BOOST_CHECK(chainman_restarted.CurrentChainstate().m_assumeutxo == Assumeutxo::UNVALIDATED);
                   +  - ]
     776                 :             : 
     777   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(chainman_restarted.ActiveTip()->GetBlockHash(), snapshot_tip_hash);
                   +  - ]
     778   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 210);
                   +  - ]
     779   [ +  -  -  +  :           1 :         BOOST_CHECK_EQUAL(chainman_restarted.HistoricalChainstate()->m_chain.Height(), 109);
             +  -  +  - ]
     780                 :           0 :     }
     781                 :             : 
     782   [ +  -  +  -  :           1 :     BOOST_TEST_MESSAGE(
                   +  - ]
     783                 :             :         "Ensure we can mine blocks on top of the initialized snapshot chainstate");
     784         [ +  - ]:           1 :     mineBlocks(10);
     785                 :           1 :     {
     786         [ +  - ]:           1 :         LOCK(chainman_restarted.GetMutex());
     787   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 220);
                   +  - ]
     788                 :             : 
     789                 :             :         // Background chainstate should be unaware of new blocks on the snapshot
     790                 :             :         // chainstate, but the block disconnected above is now reattached.
     791   [ +  -  -  +  :           1 :         BOOST_CHECK_EQUAL(chainman_restarted.m_chainstates.size(), 2);
                   +  - ]
     792   [ +  -  -  +  :           1 :         BOOST_CHECK_EQUAL(chainman_restarted.m_chainstates[0]->m_chain.Height(), 110);
                   +  - ]
     793   [ +  -  -  +  :           1 :         BOOST_CHECK_EQUAL(chainman_restarted.m_chainstates[1]->m_chain.Height(), 220);
                   +  - ]
     794   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(chainman_restarted.HistoricalChainstate(), nullptr);
                   +  - ]
     795                 :           1 :     }
     796                 :           2 : }
     797                 :             : 
     798   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(chainstatemanager_snapshot_completion, SnapshotTestSetup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     799                 :             : {
     800                 :           1 :     this->SetupSnapshot();
     801                 :             : 
     802         [ -  + ]:           1 :     ChainstateManager& chainman = *Assert(m_node.chainman);
     803                 :           1 :     Chainstate& active_cs = chainman.ActiveChainstate();
     804         [ +  + ]:           2 :     Chainstate& validated_cs{*Assert(WITH_LOCK(cs_main, return chainman.HistoricalChainstate()))};
     805                 :           1 :     auto tip_cache_before_complete = active_cs.m_coinstip_cache_size_bytes;
     806                 :           1 :     auto db_cache_before_complete = active_cs.m_coinsdb_cache_size_bytes;
     807                 :             : 
     808                 :           1 :     SnapshotCompletionResult res;
     809                 :           1 :     m_node.notifications->m_shutdown_on_fatal_error = false;
     810                 :             : 
     811         [ +  - ]:           1 :     fs::path snapshot_chainstate_dir = *node::FindAssumeutxoChainstateDir(chainman.m_options.datadir);
     812   [ +  -  +  -  :           2 :     BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
             +  -  +  - ]
     813   [ +  -  +  -  :           3 :     BOOST_CHECK_EQUAL(snapshot_chainstate_dir, gArgs.GetDataDirNet() / "chainstate_snapshot");
                   +  - ]
     814                 :             : 
     815   [ +  -  +  -  :           3 :     BOOST_CHECK(WITH_LOCK(::cs_main, return chainman.CurrentChainstate().m_from_snapshot_blockhash));
             +  -  +  - ]
     816   [ +  -  +  - ]:           3 :     const uint256 snapshot_tip_hash = WITH_LOCK(chainman.GetMutex(),
     817                 :             :         return chainman.ActiveTip()->GetBlockHash());
     818                 :             : 
     819   [ +  -  +  - ]:           3 :     res = WITH_LOCK(::cs_main, return chainman.MaybeValidateSnapshot(validated_cs, active_cs));
     820   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(res, SnapshotCompletionResult::SUCCESS);
     821                 :             : 
     822   [ +  -  +  -  :           3 :     BOOST_CHECK(WITH_LOCK(::cs_main, return chainman.CurrentChainstate().m_assumeutxo == Assumeutxo::VALIDATED));
             +  -  +  - ]
     823   [ +  -  +  -  :           3 :     BOOST_CHECK(WITH_LOCK(::cs_main, return chainman.CurrentChainstate().m_from_snapshot_blockhash));
             +  -  +  - ]
     824   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(WITH_LOCK(chainman.GetMutex(), return chainman.HistoricalChainstate()), nullptr);
                   +  - ]
     825                 :             : 
     826                 :             :     // Cache should have been rebalanced and reallocated to the "only" remaining
     827                 :             :     // chainstate.
     828   [ +  -  +  -  :           2 :     BOOST_CHECK(active_cs.m_coinstip_cache_size_bytes > tip_cache_before_complete);
                   +  - ]
     829   [ +  -  +  -  :           2 :     BOOST_CHECK(active_cs.m_coinsdb_cache_size_bytes > db_cache_before_complete);
                   +  - ]
     830                 :             : 
     831                 :             :     // Trying completion again should return false.
     832   [ +  -  +  - ]:           3 :     res = WITH_LOCK(::cs_main, return chainman.MaybeValidateSnapshot(validated_cs, active_cs));
     833   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(res, SnapshotCompletionResult::SKIPPED);
     834                 :             : 
     835                 :             :     // The invalid snapshot path should not have been used.
     836         [ +  - ]:           2 :     fs::path snapshot_invalid_dir = gArgs.GetDataDirNet() / "chainstate_snapshot_INVALID";
     837   [ +  -  +  -  :           2 :     BOOST_CHECK(!fs::exists(snapshot_invalid_dir));
             +  -  +  - ]
     838                 :             :     // chainstate_snapshot should still exist.
     839   [ +  -  +  -  :           2 :     BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
             +  -  +  - ]
     840                 :             : 
     841                 :             :     // Test that simulating a shutdown (resetting ChainstateManager) and then performing
     842                 :             :     // chainstate reinitializing successfully cleans up the background-validation
     843                 :             :     // chainstate data, and we end up with a single chainstate that is at tip.
     844         [ +  - ]:           1 :     ChainstateManager& chainman_restarted = this->SimulateNodeRestart();
     845                 :             : 
     846   [ +  -  +  -  :           1 :     BOOST_TEST_MESSAGE("Performing Load/Verify/Activate of chainstate");
                   +  - ]
     847                 :             : 
     848                 :             :     // This call reinitializes the chainstates, and should clean up the now unnecessary
     849                 :             :     // background-validation leveldb contents.
     850         [ +  - ]:           1 :     this->LoadVerifyActivateChainstate();
     851                 :             : 
     852   [ +  -  +  -  :           2 :     BOOST_CHECK(!fs::exists(snapshot_invalid_dir));
             +  -  +  - ]
     853                 :             :     // chainstate_snapshot should now *not* exist.
     854   [ +  -  +  -  :           2 :     BOOST_CHECK(!fs::exists(snapshot_chainstate_dir));
             +  -  +  - ]
     855                 :             : 
     856         [ +  - ]:           1 :     const Chainstate& active_cs2 = chainman_restarted.ActiveChainstate();
     857                 :             : 
     858                 :           1 :     {
     859         [ +  - ]:           1 :         LOCK(chainman_restarted.GetMutex());
     860   [ +  -  -  +  :           1 :         BOOST_CHECK_EQUAL(chainman_restarted.m_chainstates.size(), 1);
                   +  - ]
     861   [ +  -  +  -  :           2 :         BOOST_CHECK(!chainman_restarted.CurrentChainstate().m_from_snapshot_blockhash);
                   +  - ]
     862   [ +  -  +  -  :           2 :         BOOST_CHECK(active_cs2.m_coinstip_cache_size_bytes > tip_cache_before_complete);
                   +  - ]
     863   [ +  -  +  -  :           2 :         BOOST_CHECK(active_cs2.m_coinsdb_cache_size_bytes > db_cache_before_complete);
                   +  - ]
     864                 :             : 
     865   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(chainman_restarted.ActiveTip()->GetBlockHash(), snapshot_tip_hash);
                   +  - ]
     866   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 210);
             +  -  +  - ]
     867                 :           0 :     }
     868                 :             : 
     869   [ +  -  +  -  :           1 :     BOOST_TEST_MESSAGE(
                   +  - ]
     870                 :             :         "Ensure we can mine blocks on top of the \"new\" IBD chainstate");
     871         [ +  - ]:           1 :     mineBlocks(10);
     872                 :           1 :     {
     873         [ +  - ]:           1 :         LOCK(chainman_restarted.GetMutex());
     874   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 220);
             +  -  +  - ]
     875                 :           1 :     }
     876                 :           2 : }
     877                 :             : 
     878   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(chainstatemanager_snapshot_completion_hash_mismatch, SnapshotTestSetup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     879                 :             : {
     880                 :           1 :     auto chainstates = this->SetupSnapshot();
     881         [ -  + ]:           1 :     Chainstate& validation_chainstate = *std::get<0>(chainstates);
     882         [ -  + ]:           1 :     Chainstate& unvalidated_cs = *std::get<1>(chainstates);
     883         [ -  + ]:           1 :     ChainstateManager& chainman = *Assert(m_node.chainman);
     884                 :           1 :     SnapshotCompletionResult res;
     885                 :           1 :     m_node.notifications->m_shutdown_on_fatal_error = false;
     886                 :             : 
     887                 :             :     // Test tampering with the IBD UTXO set with an extra coin to ensure it causes
     888                 :             :     // snapshot completion to fail.
     889   [ +  -  +  - ]:           3 :     CCoinsViewCache& ibd_coins = WITH_LOCK(::cs_main,
     890                 :             :         return validation_chainstate.CoinsTip());
     891                 :           1 :     Coin badcoin;
     892                 :           1 :     badcoin.out.nValue = m_rng.rand32();
     893                 :           1 :     badcoin.nHeight = 1;
     894                 :           1 :     badcoin.out.scriptPubKey.assign(m_rng.randbits(6), 0);
     895         [ +  - ]:           1 :     Txid txid = Txid::FromUint256(m_rng.rand256());
     896         [ +  - ]:           1 :     ibd_coins.AddCoin(COutPoint(txid, 0), std::move(badcoin), false);
     897                 :             : 
     898         [ +  - ]:           2 :     fs::path snapshot_chainstate_dir = gArgs.GetDataDirNet() / "chainstate_snapshot";
     899   [ +  -  +  -  :           2 :     BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
             +  -  +  - ]
     900                 :             : 
     901                 :           1 :     {
     902   [ +  -  +  - ]:           2 :         ASSERT_DEBUG_LOG("failed to validate the -assumeutxo snapshot state");
     903   [ +  -  +  - ]:           3 :         res = WITH_LOCK(::cs_main, return chainman.MaybeValidateSnapshot(validation_chainstate, unvalidated_cs));
     904   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(res, SnapshotCompletionResult::HASH_MISMATCH);
     905                 :           1 :     }
     906                 :             : 
     907                 :           1 :     {
     908         [ +  - ]:           1 :         LOCK(chainman.GetMutex());
     909   [ +  -  -  +  :           1 :         BOOST_CHECK_EQUAL(chainman.m_chainstates.size(), 2);
                   +  - ]
     910   [ +  -  +  -  :           2 :         BOOST_CHECK(chainman.m_chainstates[0]->m_assumeutxo == Assumeutxo::VALIDATED);
                   +  - ]
     911   [ +  -  +  -  :           2 :         BOOST_CHECK(!chainman.m_chainstates[0]->SnapshotBase());
             +  -  +  - ]
     912   [ +  -  +  -  :           2 :         BOOST_CHECK(chainman.m_chainstates[1]->m_assumeutxo == Assumeutxo::INVALID);
                   +  - ]
     913   [ +  -  +  -  :           2 :         BOOST_CHECK(chainman.m_chainstates[1]->SnapshotBase());
             +  -  +  - ]
     914                 :           0 :     }
     915                 :             : 
     916         [ +  - ]:           2 :     fs::path snapshot_invalid_dir = gArgs.GetDataDirNet() / "chainstate_snapshot_INVALID";
     917   [ +  -  +  -  :           2 :     BOOST_CHECK(fs::exists(snapshot_invalid_dir));
             +  -  +  - ]
     918                 :             : 
     919                 :             :     // Test that simulating a shutdown (resetting ChainstateManager) and then performing
     920                 :             :     // chainstate reinitializing successfully loads only the fully-validated
     921                 :             :     // chainstate data, and we end up with a single chainstate that is at tip.
     922         [ +  - ]:           1 :     ChainstateManager& chainman_restarted = this->SimulateNodeRestart();
     923                 :             : 
     924   [ +  -  +  -  :           1 :     BOOST_TEST_MESSAGE("Performing Load/Verify/Activate of chainstate");
                   +  - ]
     925                 :             : 
     926                 :             :     // This call reinitializes the chainstates, and should clean up the now unnecessary
     927                 :             :     // background-validation leveldb contents.
     928         [ +  - ]:           1 :     this->LoadVerifyActivateChainstate();
     929                 :             : 
     930   [ +  -  +  -  :           2 :     BOOST_CHECK(fs::exists(snapshot_invalid_dir));
             +  -  +  - ]
     931   [ +  -  +  -  :           2 :     BOOST_CHECK(!fs::exists(snapshot_chainstate_dir));
             +  -  +  - ]
     932                 :             : 
     933                 :           1 :     {
     934         [ +  - ]:           1 :         LOCK(::cs_main);
     935   [ +  -  -  +  :           1 :         BOOST_CHECK_EQUAL(chainman_restarted.m_chainstates.size(), 1);
                   +  - ]
     936   [ +  -  +  -  :           2 :         BOOST_CHECK(!chainman_restarted.CurrentChainstate().m_from_snapshot_blockhash);
                   +  - ]
     937   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 210);
             +  -  +  - ]
     938                 :           0 :     }
     939                 :             : 
     940   [ +  -  +  -  :           1 :     BOOST_TEST_MESSAGE(
                   +  - ]
     941                 :             :         "Ensure we can mine blocks on top of the \"new\" IBD chainstate");
     942         [ +  - ]:           1 :     mineBlocks(10);
     943                 :           1 :     {
     944         [ +  - ]:           1 :         LOCK(::cs_main);
     945   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 220);
             +  -  +  - ]
     946                 :           1 :     }
     947                 :           2 : }
     948                 :             : 
     949                 :             : /** Helper function to parse args into args_man and return the result of applying them to opts */
     950                 :             : template <typename Options>
     951                 :          20 : util::Result<Options> SetOptsFromArgs(ArgsManager& args_man, Options opts,
     952                 :             :                                       const std::vector<const char*>& args)
     953                 :             : {
     954   [ +  -  -  + ]:          40 :     const auto argv{Cat({"ignore"}, args)};
     955         [ -  + ]:          20 :     std::string error{};
     956   [ -  +  +  -  :          20 :     if (!args_man.ParseParameters(argv.size(), argv.data(), error)) {
                   -  + ]
     957         [ #  # ]:           0 :         return util::Error{Untranslated("ParseParameters failed with error: " + error)};
     958                 :             :     }
     959         [ +  - ]:          20 :     const auto result{node::ApplyArgsManOptions(args_man, opts)};
     960         [ +  + ]:          30 :     if (!result) return util::Error{util::ErrorString(result)};
     961                 :          30 :     return opts;
     962   [ -  -  +  - ]:          45 : }
     963                 :             : 
     964   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(chainstatemanager_args, BasicTestingSetup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     965                 :             : {
     966                 :             :     //! Try to apply the provided args to a ChainstateManager::Options
     967                 :          21 :     auto get_opts = [&](const std::vector<const char*>& args) {
     968   [ +  +  +  - ]:          20 :         static kernel::Notifications notifications{};
     969                 :          20 :         static const ChainstateManager::Options options{
     970         [ +  - ]:           1 :             .chainparams = ::Params(),
     971                 :             :             .datadir = {},
     972   [ +  +  +  - ]:          21 :             .notifications = notifications};
     973         [ +  - ]:          40 :         return SetOptsFromArgs(*this->m_node.args, options, args);
     974                 :           1 :     };
     975                 :             :     //! Like get_opts, but requires the provided args to be valid and unwraps the result
     976                 :          16 :     auto get_valid_opts = [&](const std::vector<const char*>& args) {
     977                 :          15 :         const auto result{get_opts(args)};
     978   [ +  -  +  -  :          30 :         BOOST_REQUIRE_MESSAGE(result, util::ErrorString(result).original);
                   +  - ]
     979         [ +  - ]:          15 :         return *result;
     980                 :          16 :     };
     981                 :             : 
     982                 :             :     // test -assumevalid
     983   [ +  -  +  - ]:           3 :     BOOST_CHECK(!get_valid_opts({}).assumed_valid_block);
     984   [ +  -  +  - ]:           2 :     BOOST_CHECK_EQUAL(get_valid_opts({"-assumevalid="}).assumed_valid_block, uint256::ZERO);
     985   [ +  -  +  - ]:           2 :     BOOST_CHECK_EQUAL(get_valid_opts({"-assumevalid=0"}).assumed_valid_block, uint256::ZERO);
     986   [ +  -  +  - ]:           2 :     BOOST_CHECK_EQUAL(get_valid_opts({"-noassumevalid"}).assumed_valid_block, uint256::ZERO);
     987   [ +  -  +  - ]:           2 :     BOOST_CHECK_EQUAL(get_valid_opts({"-assumevalid=0x12"}).assumed_valid_block, uint256{0x12});
     988                 :             : 
     989                 :           1 :     std::string assume_valid{"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"};
     990   [ +  -  -  +  :           2 :     BOOST_CHECK_EQUAL(get_valid_opts({("-assumevalid=" + assume_valid).c_str()}).assumed_valid_block, uint256::FromHex(assume_valid));
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     991                 :             : 
     992   [ +  -  +  -  :           2 :     BOOST_CHECK(!get_opts({"-assumevalid=xyz"}));                                                               // invalid hex characters
          +  -  +  -  +  
                      - ]
     993   [ +  -  +  -  :           2 :     BOOST_CHECK(!get_opts({"-assumevalid=01234567890123456789012345678901234567890123456789012345678901234"})); // > 64 hex chars
          +  -  +  -  +  
                      - ]
     994                 :             : 
     995                 :             :     // test -minimumchainwork
     996   [ +  -  +  -  :           3 :     BOOST_CHECK(!get_valid_opts({}).minimum_chain_work);
             +  -  +  - ]
     997   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(get_valid_opts({"-minimumchainwork=0"}).minimum_chain_work, arith_uint256());
          +  -  +  -  +  
                      - ]
     998   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(get_valid_opts({"-nominimumchainwork"}).minimum_chain_work, arith_uint256());
          +  -  +  -  +  
                      - ]
     999   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(get_valid_opts({"-minimumchainwork=0x1234"}).minimum_chain_work, arith_uint256{0x1234});
          +  -  +  -  +  
                      - ]
    1000                 :             : 
    1001         [ +  - ]:           1 :     std::string minimum_chainwork{"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"};
    1002   [ +  -  -  +  :           3 :     BOOST_CHECK_EQUAL(get_valid_opts({("-minimumchainwork=" + minimum_chainwork).c_str()}).minimum_chain_work, UintToArith256(uint256::FromHex(minimum_chainwork).value()));
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    1003                 :             : 
    1004   [ +  -  +  -  :           2 :     BOOST_CHECK(!get_opts({"-minimumchainwork=xyz"}));                                                               // invalid hex characters
          +  -  +  -  +  
                      - ]
    1005   [ +  -  +  -  :           2 :     BOOST_CHECK(!get_opts({"-minimumchainwork=01234567890123456789012345678901234567890123456789012345678901234"})); // > 64 hex chars
          +  -  +  -  +  
                      - ]
    1006                 :             : 
    1007   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(get_valid_opts({}).prevoutfetch_threads_num, DEFAULT_PREVOUTFETCH_THREADS);
                   +  - ]
    1008   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(get_valid_opts({"-prevoutfetchthreads=0"}).prevoutfetch_threads_num, 0);
          +  -  +  -  +  
                      - ]
    1009   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(get_valid_opts({"-prevoutfetchthreads=3"}).prevoutfetch_threads_num, 3);
          +  -  +  -  +  
                      - ]
    1010   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(get_valid_opts({"-prevoutfetchthreads=100"}).prevoutfetch_threads_num, MAX_PREVOUTFETCH_THREADS);
          +  -  +  -  +  
                      - ]
    1011   [ +  -  +  -  :           2 :     BOOST_CHECK(!get_opts({"-prevoutfetchthreads=-1"}));
             +  -  +  - ]
    1012                 :           1 : }
    1013                 :             : 
    1014                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1