LCOV - code coverage report
Current view: top level - src/test - interfaces_tests.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 118 118 100.0 %
Date: 2022-04-21 14:51:19 Functions: 6 6 100.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 4 4 100.0 %

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

Generated by: LCOV version 0-eol-96201-ge66f56f4af6a