LCOV - code coverage report
Current view: top level - src/test - validation_chainstatemanager_tests.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 96.2 % 548 527
Test Date: 2026-04-27 06:58:15 Functions: 100.0 % 66 66
Branches: 51.6 % 2278 1176

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

Generated by: LCOV version 2.0-1