LCOV - code coverage report
Current view: top level - src/test/util - validation.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 95.4 % 65 62
Test Date: 2026-08-13 06:14:20 Functions: 90.0 % 10 9
Branches: 57.1 % 42 24

             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 <test/util/validation.h>
       6                 :             : 
       7                 :             : #include <coins.h>
       8                 :             : #include <consensus/consensus.h>
       9                 :             : #include <node/blockstorage.h>
      10                 :             : #include <node/mining_types.h>
      11                 :             : #include <test/util/mining.h>
      12                 :             : #include <test/util/script.h>
      13                 :             : #include <test/util/setup_common.h>
      14                 :             : #include <test/util/time.h>
      15                 :             : #include <test/util/txmempool.h>
      16                 :             : #include <txmempool.h>
      17                 :             : #include <util/check.h>
      18                 :             : #include <util/time.h>
      19                 :             : #include <validation.h>
      20                 :             : #include <validationinterface.h>
      21                 :             : 
      22                 :             : #include <memory>
      23                 :             : #include <utility>
      24                 :             : #include <vector>
      25                 :             : 
      26                 :             : using kernel::ChainstateRole;
      27                 :             : 
      28                 :         430 : void TestBlockManager::CleanupForFuzzing()
      29                 :             : {
      30                 :         430 :     m_dirty_blockindex.clear();
      31                 :         430 :     m_dirty_fileinfo.clear();
      32                 :         430 :     m_blockfile_info.resize(1);
      33                 :         430 : }
      34                 :             : 
      35                 :       11863 : void TestChainstateManager::DisableNextWrite()
      36                 :             : {
      37                 :       11863 :     struct TestChainstate : public Chainstate {
      38                 :       11863 :         void ResetNextWrite() { m_next_write = NodeClock::time_point::max() - 1s; }
      39                 :             :     };
      40                 :       11863 :     LOCK(::cs_main);
      41         [ +  + ]:       23726 :     for (const auto& cs : m_chainstates) {
      42                 :       11863 :         static_cast<TestChainstate&>(*cs).ResetNextWrite();
      43                 :             :     }
      44                 :       11863 : }
      45                 :             : 
      46                 :       13673 : void TestChainstateManager::ResetIbd()
      47                 :             : {
      48                 :       13673 :     m_cached_is_ibd = true;
      49         [ -  + ]:       13673 :     assert(IsInitialBlockDownload());
      50                 :       13673 : }
      51                 :             : 
      52                 :        6445 : void TestChainstateManager::JumpOutOfIbd()
      53                 :             : {
      54         [ -  + ]:        6445 :     Assert(IsInitialBlockDownload());
      55                 :        6445 :     m_cached_is_ibd = false;
      56         [ -  + ]:        6445 :     Assert(!IsInitialBlockDownload());
      57                 :        6445 : }
      58                 :             : 
      59                 :           0 : void ValidationInterfaceTest::BlockConnected(
      60                 :             :     const ChainstateRole& role,
      61                 :             :     CValidationInterface& obj,
      62                 :             :     const std::shared_ptr<const CBlock>& block,
      63                 :             :     const CBlockIndex* pindex)
      64                 :             : {
      65                 :           0 :     obj.BlockConnected(role, block, pindex);
      66                 :           0 : }
      67                 :        5844 : void TestChainstateManager::InvalidBlockFound(CBlockIndex* pindex, const BlockValidationState& state)
      68                 :             : {
      69                 :        5844 :     struct TestChainstate : public Chainstate {
      70                 :        5844 :         void CallInvalidBlockFound(CBlockIndex* pindex, const BlockValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
      71                 :             :         {
      72                 :        5844 :             InvalidBlockFound(pindex, state);
      73                 :             :         }
      74                 :             :     };
      75                 :             : 
      76                 :        5844 :     static_cast<TestChainstate*>(&ActiveChainstate())->CallInvalidBlockFound(pindex, state);
      77                 :        5844 : }
      78                 :             : 
      79                 :        1164 : void TestChainstateManager::InvalidChainFound(CBlockIndex* pindexNew)
      80                 :             : {
      81                 :        1164 :     struct TestChainstate : public Chainstate {
      82                 :        1164 :         void CallInvalidChainFound(CBlockIndex* pindexNew) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
      83                 :             :         {
      84                 :        1164 :             InvalidChainFound(pindexNew);
      85                 :             :         }
      86                 :             :     };
      87                 :             : 
      88                 :        1164 :     static_cast<TestChainstate*>(&ActiveChainstate())->CallInvalidChainFound(pindexNew);
      89                 :        1164 : }
      90                 :             : 
      91                 :        5476 : CBlockIndex* TestChainstateManager::FindMostWorkChain()
      92                 :             : {
      93                 :        5476 :     struct TestChainstate : public Chainstate {
      94                 :        5476 :         CBlockIndex* CallFindMostWorkChain() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
      95                 :             :         {
      96                 :        5476 :             return FindMostWorkChain();
      97                 :             :         }
      98                 :             :     };
      99                 :             : 
     100                 :        5476 :     return static_cast<TestChainstate*>(&ActiveChainstate())->CallFindMostWorkChain();
     101                 :             : }
     102                 :             : 
     103                 :         430 : void TestChainstateManager::ResetBestInvalid()
     104                 :             : {
     105                 :         430 :     m_best_invalid = nullptr;
     106                 :         430 : }
     107                 :             : 
     108                 :        1405 : std::vector<std::pair<COutPoint, CAmount>> ResetChainmanAndMempool(TestingSetup& setup)
     109                 :             : {
     110                 :        1405 :     GetFakeNodeClock().set(setup.m_node.chainman->GetParams().GenesisBlock().Time());
     111                 :             : 
     112         [ +  - ]:        1405 :     bilingual_str error{};
     113         [ +  - ]:        1405 :     setup.m_node.mempool.reset();
     114   [ +  -  +  - ]:        1405 :     setup.m_node.mempool = std::make_unique<CTxMemPool>(MemPoolOptionsForTest(setup.m_node), error);
     115         [ -  + ]:        1405 :     Assert(error.empty());
     116                 :             : 
     117         [ +  - ]:        1405 :     setup.m_node.chainman.reset();
     118         [ +  - ]:        1405 :     setup.m_make_chainman();
     119         [ +  - ]:        1405 :     setup.LoadVerifyActivateChainstate();
     120                 :             : 
     121         [ +  - ]:        1405 :     node::BlockCreateOptions options;
     122                 :        1405 :     options.coinbase_output_script = P2WSH_OP_TRUE;
     123                 :             : 
     124                 :        1405 :     std::vector<std::pair<COutPoint, CAmount>> mature_coinbase;
     125         [ +  + ]:      282405 :     for (int i = 0; i < 2 * COINBASE_MATURITY; ++i) {
     126         [ +  - ]:      281000 :         COutPoint prevout{MineBlock(setup.m_node, options)};
     127         [ +  + ]:      281000 :         if (i < COINBASE_MATURITY) {
     128         [ +  - ]:      140500 :             LOCK(cs_main);
     129   [ +  -  +  -  :      140500 :             CAmount subsidy{setup.m_node.chainman->ActiveChainstate().CoinsTip().GetCoin(prevout)->out.nValue};
                   +  - ]
     130         [ +  - ]:      140500 :             mature_coinbase.emplace_back(prevout, subsidy);
     131                 :      140500 :         }
     132                 :             :     }
     133                 :        1405 :     return mature_coinbase;
     134                 :        2810 : }
        

Generated by: LCOV version 2.5.0-full