LCOV - code coverage report
Current view: top level - src/test - interfaces_tests.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 120 120 100.0 %
Date: 2020-09-26 01:30:44 Functions: 51 51 100.0 %

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

Generated by: LCOV version 1.15