LCOV - code coverage report
Current view: top level - src/test - validation_flush_tests.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 100.0 % 29 29
Test Date: 2025-08-25 05:11:47 Functions: 100.0 % 2 2
Branches: 52.7 % 150 79

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2019-present The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #include <sync.h>
       6                 :             : #include <test/util/coins.h>
       7                 :             : #include <test/util/random.h>
       8                 :             : #include <test/util/setup_common.h>
       9                 :             : #include <validation.h>
      10                 :             : 
      11                 :             : #include <boost/test/unit_test.hpp>
      12                 :             : 
      13                 :             : BOOST_FIXTURE_TEST_SUITE(validation_flush_tests, TestingSetup)
      14                 :             : 
      15                 :             : //! Verify that Chainstate::GetCoinsCacheSizeState() switches from OK→LARGE→CRITICAL
      16                 :             : //! at the expected utilization thresholds, first with *no* mempool head-room,
      17                 :             : //! then with additional mempool head-room.
      18   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      19                 :             : {
      20                 :           1 :     Chainstate& chainstate{m_node.chainman->ActiveChainstate()};
      21                 :             : 
      22                 :           1 :     LOCK(::cs_main);
      23         [ +  - ]:           1 :     CCoinsViewCache& view{chainstate.CoinsTip()};
      24                 :             : 
      25                 :             :     // Sanity: an empty cache should be ≲ 1 chunk (~ 256 KiB).
      26   [ +  -  +  -  :           1 :     BOOST_CHECK_LT(view.DynamicMemoryUsage() / (256 * 1024.0), 1.1);
                   +  - ]
      27                 :             : 
      28                 :           1 :     constexpr size_t MAX_COINS_BYTES{8_MiB};
      29                 :           1 :     constexpr size_t MAX_MEMPOOL_BYTES{4_MiB};
      30                 :           1 :     constexpr size_t MAX_ATTEMPTS{50'000};
      31                 :             : 
      32                 :             :     // Run the same growth-path twice: first with 0 head-room, then with extra head-room
      33         [ +  + ]:           3 :     for (size_t max_mempool_size_bytes : {size_t{0}, MAX_MEMPOOL_BYTES}) {
      34                 :           2 :         const int64_t full_cap{int64_t(MAX_COINS_BYTES + max_mempool_size_bytes)};
      35         [ +  - ]:           2 :         const int64_t large_cap{LargeCoinsCacheThreshold(full_cap)};
      36                 :             : 
      37                 :             :         // OK → LARGE
      38         [ +  - ]:           2 :         auto state{chainstate.GetCoinsCacheSizeState(MAX_COINS_BYTES, max_mempool_size_bytes)};
      39   [ +  -  +  -  :       47167 :         for (size_t i{0}; i < MAX_ATTEMPTS && int64_t(view.DynamicMemoryUsage()) <= large_cap; ++i) {
                   +  + ]
      40   [ +  -  +  - ]:       47165 :             BOOST_CHECK_EQUAL(state, CoinsCacheSizeState::OK);
      41         [ +  - ]:       47165 :             AddTestCoin(m_rng, view);
      42         [ +  - ]:       47165 :             state = chainstate.GetCoinsCacheSizeState(MAX_COINS_BYTES, max_mempool_size_bytes);
      43                 :             :         }
      44                 :             : 
      45                 :             :         // LARGE → CRITICAL
      46   [ +  -  +  -  :        9828 :         for (size_t i{0}; i < MAX_ATTEMPTS && int64_t(view.DynamicMemoryUsage()) <= full_cap; ++i) {
                   +  + ]
      47   [ +  -  +  - ]:        9826 :             BOOST_CHECK_EQUAL(state, CoinsCacheSizeState::LARGE);
      48         [ +  - ]:        9826 :             AddTestCoin(m_rng, view);
      49         [ +  - ]:        9826 :             state = chainstate.GetCoinsCacheSizeState(MAX_COINS_BYTES, max_mempool_size_bytes);
      50                 :             :         }
      51   [ +  -  +  - ]:           2 :         BOOST_CHECK_EQUAL(state, CoinsCacheSizeState::CRITICAL);
      52                 :             :     }
      53                 :             : 
      54                 :             :     // Default thresholds (no explicit limits) permit many more coins.
      55         [ +  + ]:        1001 :     for (int i{0}; i < 1'000; ++i) {
      56         [ +  - ]:        1000 :         AddTestCoin(m_rng, view);
      57   [ +  -  +  -  :        1000 :         BOOST_CHECK_EQUAL(chainstate.GetCoinsCacheSizeState(), CoinsCacheSizeState::OK);
                   +  - ]
      58                 :             :     }
      59                 :             : 
      60                 :             :     // CRITICAL → OK via Flush
      61   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(chainstate.GetCoinsCacheSizeState(MAX_COINS_BYTES, /*max_mempool_size_bytes=*/0), CoinsCacheSizeState::CRITICAL);
                   +  - ]
      62         [ +  - ]:           1 :     view.SetBestBlock(m_rng.rand256());
      63   [ +  -  +  -  :           2 :     BOOST_REQUIRE(view.Flush());
             +  -  +  - ]
      64   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(chainstate.GetCoinsCacheSizeState(MAX_COINS_BYTES, /*max_mempool_size_bytes=*/0), CoinsCacheSizeState::OK);
             +  -  +  - ]
      65                 :           1 : }
      66                 :             : 
      67                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1