LCOV - code coverage report
Current view: top level - src/test - coinstatsindex_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 90.3 % 31 28
Test Date: 2026-10-11 07:59:32 Functions: 100.0 % 2 2
Branches: 50.0 % 170 85

             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 <index/coinstatsindex.h>
       7                 :             : #include <interfaces/chain.h>
       8                 :             : #include <kernel/coinstats.h>
       9                 :             : #include <key.h>
      10                 :             : #include <primitives/block.h>
      11                 :             : #include <primitives/transaction.h>
      12                 :             : #include <script/script.h>
      13                 :             : #include <sync.h>
      14                 :             : #include <test/util/setup_common.h>
      15                 :             : #include <test/util/validation.h>
      16                 :             : #include <util/check.h>
      17                 :             : #include <validation.h>
      18                 :             : 
      19                 :             : #include <boost/test/unit_test.hpp>
      20                 :             : 
      21                 :             : #include <memory>
      22                 :             : #include <optional>
      23                 :             : #include <span>
      24                 :             : #include <vector>
      25                 :             : 
      26                 :             : BOOST_AUTO_TEST_SUITE(coinstatsindex_tests)
      27                 :             : 
      28   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      29                 :             : {
      30         [ +  - ]:           1 :     CoinStatsIndex coin_stats_index{interfaces::MakeChain(m_node), 1_MiB, true};
      31   [ +  -  +  -  :           2 :     BOOST_REQUIRE(coin_stats_index.Init());
             +  -  +  - ]
      32                 :             : 
      33                 :           1 :     const CBlockIndex* block_index;
      34                 :           1 :     {
      35         [ +  - ]:           1 :         LOCK(cs_main);
      36   [ +  -  -  +  :           2 :         block_index = m_node.chainman->ActiveChain().Tip();
                   +  - ]
      37                 :           0 :     }
      38                 :             : 
      39                 :             :     // CoinStatsIndex should not be found before it is started.
      40   [ +  -  +  -  :           2 :     BOOST_CHECK(!coin_stats_index.LookUpStats(*block_index));
             +  -  +  - ]
      41                 :             : 
      42                 :             :     // BlockUntilSyncedToCurrentChain should return false before CoinStatsIndex
      43                 :             :     // is started.
      44   [ +  -  +  -  :           2 :     BOOST_CHECK(!coin_stats_index.BlockUntilSyncedToCurrentChain());
             +  -  +  - ]
      45                 :             : 
      46         [ +  - ]:           1 :     coin_stats_index.Sync();
      47                 :             : 
      48                 :             :     // Check that CoinStatsIndex works for genesis block.
      49                 :           1 :     const CBlockIndex* genesis_block_index;
      50                 :           1 :     {
      51         [ +  - ]:           1 :         LOCK(cs_main);
      52   [ +  -  -  +  :           2 :         genesis_block_index = m_node.chainman->ActiveChain().Genesis();
                   +  - ]
      53                 :           0 :     }
      54   [ +  -  +  -  :           2 :     BOOST_CHECK(coin_stats_index.LookUpStats(*genesis_block_index));
             +  -  +  - ]
      55                 :             : 
      56                 :             :     // Check that CoinStatsIndex updates with new blocks.
      57   [ +  -  +  -  :           2 :     BOOST_CHECK(coin_stats_index.LookUpStats(*block_index));
             +  -  +  - ]
      58                 :             : 
      59   [ +  -  +  -  :           2 :     const CScript script_pub_key{CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG};
                   +  - ]
      60                 :           1 :     std::vector<CMutableTransaction> noTxns;
      61         [ +  - ]:           1 :     CreateAndProcessBlock(noTxns, script_pub_key);
      62                 :             : 
      63                 :             :     // Let the CoinStatsIndex to catch up again.
      64   [ +  -  +  -  :           2 :     BOOST_CHECK(coin_stats_index.BlockUntilSyncedToCurrentChain());
             +  -  +  - ]
      65                 :             : 
      66                 :           1 :     const CBlockIndex* new_block_index;
      67                 :           1 :     {
      68         [ +  - ]:           1 :         LOCK(cs_main);
      69   [ +  -  -  +  :           2 :         new_block_index = m_node.chainman->ActiveChain().Tip();
                   +  - ]
      70                 :           0 :     }
      71   [ +  -  +  -  :           2 :     BOOST_CHECK(coin_stats_index.LookUpStats(*new_block_index));
             +  -  +  - ]
      72                 :             : 
      73   [ +  -  +  -  :           2 :     BOOST_CHECK(block_index != new_block_index);
                   +  - ]
      74                 :             : 
      75         [ +  - ]:           1 :     StopIndex(coin_stats_index, m_node);
      76                 :           1 : }
      77                 :             : 
      78                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1