LCOV - code coverage report
Current view: top level - src/test - interfaces_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 100.0 % 121 121
Test Date: 2024-11-04 04:45:35 Functions: 100.0 % 13 13
Branches: 50.2 % 914 459

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2020-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 <interfaces/chain.h>
       8                 :             : #include <test/util/setup_common.h>
       9                 :             : #include <script/solver.h>
      10                 :             : #include <validation.h>
      11                 :             : 
      12                 :             : #include <boost/test/unit_test.hpp>
      13                 :             : 
      14                 :             : using interfaces::FoundBlock;
      15                 :             : 
      16                 :             : BOOST_FIXTURE_TEST_SUITE(interfaces_tests, TestChain100Setup)
      17                 :             : 
      18   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(findBlock)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
      19                 :             : {
      20                 :           1 :     LOCK(Assert(m_node.chainman)->GetMutex());
      21                 :           1 :     auto& chain = m_node.chain;
      22   [ +  -  +  - ]:           1 :     const CChain& active = Assert(m_node.chainman)->ActiveChain();
      23                 :             : 
      24                 :           1 :     uint256 hash;
      25   [ +  -  +  -  :           3 :     BOOST_CHECK(chain->findBlock(active[10]->GetBlockHash(), FoundBlock().hash(hash)));
          +  -  +  -  +  
                      - ]
      26   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(hash, active[10]->GetBlockHash());
                   +  - ]
      27                 :             : 
      28                 :           1 :     int height = -1;
      29   [ +  -  +  -  :           3 :     BOOST_CHECK(chain->findBlock(active[20]->GetBlockHash(), FoundBlock().height(height)));
          +  -  +  -  +  
                      - ]
      30   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(height, active[20]->nHeight);
                   +  - ]
      31                 :             : 
      32                 :           1 :     CBlock data;
      33   [ +  -  +  -  :           3 :     BOOST_CHECK(chain->findBlock(active[30]->GetBlockHash(), FoundBlock().data(data)));
          +  -  +  -  +  
                      - ]
      34   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(data.GetHash(), active[30]->GetBlockHash());
                   +  - ]
      35                 :             : 
      36                 :           1 :     int64_t time = -1;
      37   [ +  -  +  -  :           3 :     BOOST_CHECK(chain->findBlock(active[40]->GetBlockHash(), FoundBlock().time(time)));
          +  -  +  -  +  
                      - ]
      38   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(time, active[40]->GetBlockTime());
                   +  - ]
      39                 :             : 
      40                 :           1 :     int64_t max_time = -1;
      41   [ +  -  +  -  :           3 :     BOOST_CHECK(chain->findBlock(active[50]->GetBlockHash(), FoundBlock().maxTime(max_time)));
          +  -  +  -  +  
                      - ]
      42   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(max_time, active[50]->GetBlockTimeMax());
                   +  - ]
      43                 :             : 
      44                 :           1 :     int64_t mtp_time = -1;
      45   [ +  -  +  -  :           3 :     BOOST_CHECK(chain->findBlock(active[60]->GetBlockHash(), FoundBlock().mtpTime(mtp_time)));
          +  -  +  -  +  
                      - ]
      46   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(mtp_time, active[60]->GetMedianTimePast());
                   +  - ]
      47                 :             : 
      48                 :           1 :     bool cur_active{false}, next_active{false};
      49                 :           1 :     uint256 next_hash;
      50   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(active.Height(), 100);
      51   [ +  -  +  -  :           3 :     BOOST_CHECK(chain->findBlock(active[99]->GetBlockHash(), FoundBlock().inActiveChain(cur_active).nextBlock(FoundBlock().inActiveChain(next_active).hash(next_hash))));
          +  -  +  -  +  
                      - ]
      52   [ +  -  +  -  :           2 :     BOOST_CHECK(cur_active);
                   +  - ]
      53   [ +  -  +  -  :           2 :     BOOST_CHECK(next_active);
                   +  - ]
      54   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(next_hash, active[100]->GetBlockHash());
                   +  - ]
      55                 :           1 :     cur_active = next_active = false;
      56   [ +  -  +  -  :           3 :     BOOST_CHECK(chain->findBlock(active[100]->GetBlockHash(), FoundBlock().inActiveChain(cur_active).nextBlock(FoundBlock().inActiveChain(next_active))));
          +  -  +  -  +  
                      - ]
      57   [ +  -  +  -  :           2 :     BOOST_CHECK(cur_active);
                   +  - ]
      58   [ +  -  +  -  :           2 :     BOOST_CHECK(!next_active);
                   +  - ]
      59                 :             : 
      60   [ +  -  +  -  :           2 :     BOOST_CHECK(!chain->findBlock({}, FoundBlock()));
                   +  - ]
      61         [ +  - ]:           2 : }
      62                 :             : 
      63   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(findFirstBlockWithTimeAndHeight)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
      64                 :             : {
      65                 :           1 :     LOCK(Assert(m_node.chainman)->GetMutex());
      66                 :           1 :     auto& chain = m_node.chain;
      67   [ +  -  +  - ]:           1 :     const CChain& active = Assert(m_node.chainman)->ActiveChain();
      68                 :           1 :     uint256 hash;
      69                 :           1 :     int height;
      70   [ +  -  +  -  :           2 :     BOOST_CHECK(chain->findFirstBlockWithTimeAndHeight(/* min_time= */ 0, /* min_height= */ 5, FoundBlock().hash(hash).height(height)));
             +  -  +  - ]
      71   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(hash, active[5]->GetBlockHash());
                   +  - ]
      72   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(height, 5);
      73   [ +  -  +  -  :           3 :     BOOST_CHECK(!chain->findFirstBlockWithTimeAndHeight(/* min_time= */ active.Tip()->GetBlockTimeMax() + 1, /* min_height= */ 0));
          +  -  +  -  +  
                      - ]
      74                 :           1 : }
      75                 :             : 
      76   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(findAncestorByHeight)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
      77                 :             : {
      78                 :           1 :     LOCK(Assert(m_node.chainman)->GetMutex());
      79                 :           1 :     auto& chain = m_node.chain;
      80   [ +  -  +  - ]:           1 :     const CChain& active = Assert(m_node.chainman)->ActiveChain();
      81                 :           1 :     uint256 hash;
      82   [ +  -  +  -  :           3 :     BOOST_CHECK(chain->findAncestorByHeight(active[20]->GetBlockHash(), 10, FoundBlock().hash(hash)));
          +  -  +  -  +  
                      - ]
      83   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(hash, active[10]->GetBlockHash());
             +  -  +  - ]
      84   [ +  -  +  -  :           3 :     BOOST_CHECK(!chain->findAncestorByHeight(active[10]->GetBlockHash(), 20));
          +  -  +  -  +  
                      - ]
      85                 :           1 : }
      86                 :             : 
      87   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(findAncestorByHash)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
      88                 :             : {
      89                 :           1 :     LOCK(Assert(m_node.chainman)->GetMutex());
      90                 :           1 :     auto& chain = m_node.chain;
      91   [ +  -  +  - ]:           1 :     const CChain& active = Assert(m_node.chainman)->ActiveChain();
      92                 :           1 :     int height = -1;
      93   [ +  -  +  -  :           4 :     BOOST_CHECK(chain->findAncestorByHash(active[20]->GetBlockHash(), active[10]->GetBlockHash(), FoundBlock().height(height)));
          +  -  +  -  +  
                -  +  - ]
      94   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(height, 10);
      95   [ +  -  +  -  :           4 :     BOOST_CHECK(!chain->findAncestorByHash(active[10]->GetBlockHash(), active[20]->GetBlockHash()));
          +  -  +  -  +  
                -  +  - ]
      96                 :           1 : }
      97                 :             : 
      98   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(findCommonAncestor)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
      99                 :             : {
     100                 :           1 :     auto& chain = m_node.chain;
     101   [ +  -  +  -  :           3 :     const CChain& active{*WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return &Assert(m_node.chainman)->ActiveChain())};
                   +  - ]
     102         [ +  - ]:           1 :     auto* orig_tip = active.Tip();
     103         [ +  + ]:          11 :     for (int i = 0; i < 10; ++i) {
     104         [ +  - ]:          10 :         BlockValidationState state;
     105   [ +  -  +  -  :          20 :         m_node.chainman->ActiveChainstate().InvalidateBlock(state, active.Tip());
                   +  - ]
     106                 :          10 :     }
     107         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(active.Height(), orig_tip->nHeight - 10);
     108                 :           1 :     coinbaseKey.MakeNewKey(true);
     109         [ +  + ]:          21 :     for (int i = 0; i < 20; ++i) {
     110         [ +  - ]:          40 :         CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
     111                 :             :     }
     112         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(active.Height(), orig_tip->nHeight + 10);
     113                 :           1 :     uint256 fork_hash;
     114                 :           1 :     int fork_height;
     115                 :           1 :     int orig_height;
     116   [ +  -  +  -  :           3 :     BOOST_CHECK(chain->findCommonAncestor(orig_tip->GetBlockHash(), active.Tip()->GetBlockHash(), FoundBlock().height(fork_height).hash(fork_hash), FoundBlock().height(orig_height)));
                   +  - ]
     117         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(orig_height, orig_tip->nHeight);
     118         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(fork_height, orig_tip->nHeight - 10);
     119   [ +  -  +  - ]:           2 :     BOOST_CHECK_EQUAL(fork_hash, active[fork_height]->GetBlockHash());
     120                 :             : 
     121                 :           1 :     uint256 active_hash, orig_hash;
     122   [ +  -  +  -  :           3 :     BOOST_CHECK(!chain->findCommonAncestor(active.Tip()->GetBlockHash(), {}, {}, FoundBlock().hash(active_hash), {}));
                   +  - ]
     123   [ +  -  +  - ]:           2 :     BOOST_CHECK(!chain->findCommonAncestor({}, orig_tip->GetBlockHash(), {}, {}, FoundBlock().hash(orig_hash)));
     124   [ +  -  +  - ]:           2 :     BOOST_CHECK_EQUAL(active_hash, active.Tip()->GetBlockHash());
     125         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(orig_hash, orig_tip->GetBlockHash());
     126                 :           1 : }
     127                 :             : 
     128   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(hasBlocks)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     129                 :             : {
     130                 :           1 :     LOCK(::cs_main);
     131                 :           1 :     auto& chain = m_node.chain;
     132   [ +  -  +  - ]:           1 :     const CChain& active = Assert(m_node.chainman)->ActiveChain();
     133                 :             : 
     134                 :             :     // Test ranges
     135   [ +  -  +  -  :           3 :     BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 10, 90));
          +  -  +  -  +  
                      - ]
     136   [ +  -  +  -  :           3 :     BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 10, {}));
          +  -  +  -  +  
                      - ]
     137   [ +  -  +  -  :           3 :     BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 0, 90));
          +  -  +  -  +  
                      - ]
     138   [ +  -  +  -  :           3 :     BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 0, {}));
          +  -  +  -  +  
                      - ]
     139   [ +  -  +  -  :           3 :     BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), -1000, 1000));
          +  -  +  -  +  
                      - ]
     140         [ +  - ]:           1 :     active[5]->nStatus &= ~BLOCK_HAVE_DATA;
     141   [ +  -  +  -  :           3 :     BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 10, 90));
          +  -  +  -  +  
                      - ]
     142   [ +  -  +  -  :           3 :     BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 10, {}));
          +  -  +  -  +  
                      - ]
     143   [ +  -  +  -  :           3 :     BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 0, 90));
          +  -  +  -  +  
                      - ]
     144   [ +  -  +  -  :           3 :     BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 0, {}));
          +  -  +  -  +  
                      - ]
     145   [ +  -  +  -  :           3 :     BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), -1000, 1000));
          +  -  +  -  +  
                      - ]
     146         [ +  - ]:           1 :     active[95]->nStatus &= ~BLOCK_HAVE_DATA;
     147   [ +  -  +  -  :           3 :     BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 10, 90));
          +  -  +  -  +  
                      - ]
     148   [ +  -  +  -  :           3 :     BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 10, {}));
          +  -  +  -  +  
                      - ]
     149   [ +  -  +  -  :           3 :     BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 0, 90));
          +  -  +  -  +  
                      - ]
     150   [ +  -  +  -  :           3 :     BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 0, {}));
          +  -  +  -  +  
                      - ]
     151   [ +  -  +  -  :           3 :     BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), -1000, 1000));
          +  -  +  -  +  
                      - ]
     152         [ +  - ]:           1 :     active[50]->nStatus &= ~BLOCK_HAVE_DATA;
     153   [ +  -  +  -  :           3 :     BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 10, 90));
          +  -  +  -  +  
                      - ]
     154   [ +  -  +  -  :           3 :     BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 10, {}));
          +  -  +  -  +  
                      - ]
     155   [ +  -  +  -  :           3 :     BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 0, 90));
          +  -  +  -  +  
                      - ]
     156   [ +  -  +  -  :           3 :     BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 0, {}));
          +  -  +  -  +  
                      - ]
     157   [ +  -  +  -  :           3 :     BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), -1000, 1000));
          +  -  +  -  +  
                      - ]
     158                 :             : 
     159                 :             :     // Test edge cases
     160   [ +  -  +  -  :           3 :     BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 6, 49));
          +  -  +  -  +  
                      - ]
     161   [ +  -  +  -  :           3 :     BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 5, 49));
          +  -  +  -  +  
                      - ]
     162   [ +  -  +  -  :           3 :     BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 6, 50));
          +  -  +  -  +  
                      - ]
     163                 :           1 : }
     164                 :             : 
     165                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1