LCOV - code coverage report
Current view: top level - src/test - coinstatsindex_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 95.1 % 61 58
Test Date: 2026-06-07 07:36:48 Functions: 100.0 % 4 4
Branches: 50.0 % 324 162

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2020-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 <chain.h>
       6                 :             : #include <chainparams.h>
       7                 :             : #include <coins.h>
       8                 :             : #include <consensus/validation.h>
       9                 :             : #include <index/coinstatsindex.h>
      10                 :             : #include <interfaces/chain.h>
      11                 :             : #include <kernel/coinstats.h>
      12                 :             : #include <kernel/types.h>
      13                 :             : #include <key.h>
      14                 :             : #include <primitives/block.h>
      15                 :             : #include <primitives/transaction.h>
      16                 :             : #include <script/script.h>
      17                 :             : #include <sync.h>
      18                 :             : #include <test/util/setup_common.h>
      19                 :             : #include <test/util/validation.h>
      20                 :             : #include <util/check.h>
      21                 :             : #include <validation.h>
      22                 :             : 
      23                 :             : #include <boost/test/unit_test.hpp>
      24                 :             : 
      25                 :             : #include <memory>
      26                 :             : #include <optional>
      27                 :             : #include <span>
      28                 :             : #include <vector>
      29                 :             : 
      30                 :             : using kernel::ChainstateRole;
      31                 :             : 
      32                 :             : BOOST_AUTO_TEST_SUITE(coinstatsindex_tests)
      33                 :             : 
      34   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      35                 :             : {
      36         [ +  - ]:           1 :     CoinStatsIndex coin_stats_index{interfaces::MakeChain(m_node), 1_MiB, true};
      37   [ +  -  +  -  :           2 :     BOOST_REQUIRE(coin_stats_index.Init());
             +  -  +  - ]
      38                 :             : 
      39                 :           1 :     const CBlockIndex* block_index;
      40                 :           1 :     {
      41         [ +  - ]:           1 :         LOCK(cs_main);
      42   [ +  -  -  +  :           2 :         block_index = m_node.chainman->ActiveChain().Tip();
                   +  - ]
      43                 :           0 :     }
      44                 :             : 
      45                 :             :     // CoinStatsIndex should not be found before it is started.
      46   [ +  -  +  -  :           2 :     BOOST_CHECK(!coin_stats_index.LookUpStats(*block_index));
             +  -  +  - ]
      47                 :             : 
      48                 :             :     // BlockUntilSyncedToCurrentChain should return false before CoinStatsIndex
      49                 :             :     // is started.
      50   [ +  -  +  -  :           2 :     BOOST_CHECK(!coin_stats_index.BlockUntilSyncedToCurrentChain());
             +  -  +  - ]
      51                 :             : 
      52         [ +  - ]:           1 :     coin_stats_index.Sync();
      53                 :             : 
      54                 :             :     // Check that CoinStatsIndex works for genesis block.
      55                 :           1 :     const CBlockIndex* genesis_block_index;
      56                 :           1 :     {
      57         [ +  - ]:           1 :         LOCK(cs_main);
      58   [ +  -  -  +  :           2 :         genesis_block_index = m_node.chainman->ActiveChain().Genesis();
                   +  - ]
      59                 :           0 :     }
      60   [ +  -  +  -  :           2 :     BOOST_CHECK(coin_stats_index.LookUpStats(*genesis_block_index));
             +  -  +  - ]
      61                 :             : 
      62                 :             :     // Check that CoinStatsIndex updates with new blocks.
      63   [ +  -  +  -  :           2 :     BOOST_CHECK(coin_stats_index.LookUpStats(*block_index));
             +  -  +  - ]
      64                 :             : 
      65   [ +  -  +  -  :           2 :     const CScript script_pub_key{CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG};
                   +  - ]
      66                 :           1 :     std::vector<CMutableTransaction> noTxns;
      67         [ +  - ]:           1 :     CreateAndProcessBlock(noTxns, script_pub_key);
      68                 :             : 
      69                 :             :     // Let the CoinStatsIndex to catch up again.
      70   [ +  -  +  -  :           2 :     BOOST_CHECK(coin_stats_index.BlockUntilSyncedToCurrentChain());
             +  -  +  - ]
      71                 :             : 
      72                 :           1 :     const CBlockIndex* new_block_index;
      73                 :           1 :     {
      74         [ +  - ]:           1 :         LOCK(cs_main);
      75   [ +  -  -  +  :           2 :         new_block_index = m_node.chainman->ActiveChain().Tip();
                   +  - ]
      76                 :           0 :     }
      77   [ +  -  +  -  :           2 :     BOOST_CHECK(coin_stats_index.LookUpStats(*new_block_index));
             +  -  +  - ]
      78                 :             : 
      79   [ +  -  +  -  :           2 :     BOOST_CHECK(block_index != new_block_index);
                   +  - ]
      80                 :             : 
      81                 :             :     // Shutdown sequence (c.f. Shutdown() in init.cpp)
      82         [ +  - ]:           1 :     coin_stats_index.Stop();
      83                 :           1 : }
      84                 :             : 
      85                 :             : // Test shutdown between BlockConnected and ChainStateFlushed notifications,
      86                 :             : // make sure index is not corrupted and is able to reload.
      87   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(coinstatsindex_unclean_shutdown, TestChain100Setup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      88                 :             : {
      89   [ -  +  -  + ]:           2 :     Chainstate& chainstate = Assert(m_node.chainman)->ActiveChainstate();
      90                 :           1 :     const CChainParams& params = Params();
      91                 :           1 :     {
      92         [ +  - ]:           1 :         CoinStatsIndex index{interfaces::MakeChain(m_node), 1_MiB};
      93   [ +  -  +  -  :           2 :         BOOST_REQUIRE(index.Init());
             +  -  +  - ]
      94         [ +  - ]:           1 :         index.Sync();
      95                 :           1 :         std::shared_ptr<const CBlock> new_block;
      96                 :           1 :         CBlockIndex* new_block_index = nullptr;
      97                 :           1 :         {
      98   [ +  -  +  -  :           2 :             const CScript script_pub_key{CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG};
                   +  - ]
      99         [ +  - ]:           1 :             const CBlock block = this->CreateBlock({}, script_pub_key);
     100                 :             : 
     101   [ +  -  -  + ]:           1 :             new_block = std::make_shared<CBlock>(block);
     102                 :             : 
     103         [ +  - ]:           1 :             LOCK(cs_main);
     104         [ +  - ]:           1 :             BlockValidationState state;
     105   [ +  -  +  -  :           2 :             BOOST_CHECK(CheckBlock(block, state, params.GetConsensus()));
             +  -  +  - ]
     106   [ +  -  +  -  :           2 :             BOOST_CHECK(m_node.chainman->AcceptBlock(new_block, state, &new_block_index, true, nullptr, nullptr, true));
             +  -  +  - ]
     107   [ +  -  +  - ]:           1 :             CCoinsViewCache view(&chainstate.CoinsTip());
     108   [ +  -  +  -  :           2 :             BOOST_CHECK(chainstate.ConnectBlock(block, state, new_block_index, view));
                   +  - ]
     109         [ +  - ]:           3 :         }
     110                 :             :         // Send block connected notification, then stop the index without
     111                 :             :         // sending a chainstate flushed notification. Prior to #24138, this
     112                 :             :         // would cause the index to be corrupted and fail to reload.
     113         [ +  - ]:           1 :         ValidationInterfaceTest::BlockConnected(ChainstateRole{}, index, new_block, new_block_index);
     114         [ +  - ]:           1 :         index.Stop();
     115                 :           1 :     }
     116                 :             : 
     117                 :           1 :     {
     118         [ +  - ]:           1 :         CoinStatsIndex index{interfaces::MakeChain(m_node), 1_MiB};
     119   [ +  -  +  -  :           2 :         BOOST_REQUIRE(index.Init());
             +  -  +  - ]
     120                 :             :         // Make sure the index can be loaded.
     121   [ +  -  +  -  :           2 :         BOOST_REQUIRE(index.StartBackgroundSync());
             +  -  +  - ]
     122         [ +  - ]:           1 :         index.Stop();
     123                 :           1 :     }
     124                 :           1 : }
     125                 :             : 
     126                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1