LCOV - code coverage report
Current view: top level - src/test - validation_chainstatemanager_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 96.8 % 434 420
Test Date: 2024-08-28 04:44:32 Functions: 100.0 % 49 49
Branches: 50.8 % 1720 874

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

Generated by: LCOV version 2.0-1