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

          Line data    Source code
       1             : // Copyright (c) 2014-2019 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 <chain.h>
       6             : #include <test/util/setup_common.h>
       7             : 
       8             : #include <vector>
       9             : 
      10             : #include <boost/test/unit_test.hpp>
      11             : 
      12             : #define SKIPLIST_LENGTH 300000
      13             : 
      14          89 : BOOST_FIXTURE_TEST_SUITE(skiplist_tests, BasicTestingSetup)
      15             : 
      16          95 : BOOST_AUTO_TEST_CASE(skiplist_test)
      17             : {
      18           1 :     std::vector<CBlockIndex> vIndex(SKIPLIST_LENGTH);
      19             : 
      20      300001 :     for (int i=0; i<SKIPLIST_LENGTH; i++) {
      21      300000 :         vIndex[i].nHeight = i;
      22      300000 :         vIndex[i].pprev = (i == 0) ? nullptr : &vIndex[i - 1];
      23      300000 :         vIndex[i].BuildSkip();
      24             :     }
      25             : 
      26      300001 :     for (int i=0; i<SKIPLIST_LENGTH; i++) {
      27      300000 :         if (i > 0) {
      28      299999 :             BOOST_CHECK(vIndex[i].pskip == &vIndex[vIndex[i].pskip->nHeight]);
      29      299999 :             BOOST_CHECK(vIndex[i].pskip->nHeight < i);
      30             :         } else {
      31           1 :             BOOST_CHECK(vIndex[i].pskip == nullptr);
      32             :         }
      33             :     }
      34             : 
      35        1001 :     for (int i=0; i < 1000; i++) {
      36        1000 :         int from = InsecureRandRange(SKIPLIST_LENGTH - 1);
      37        1000 :         int to = InsecureRandRange(from + 1);
      38             : 
      39        1000 :         BOOST_CHECK(vIndex[SKIPLIST_LENGTH - 1].GetAncestor(from) == &vIndex[from]);
      40        1000 :         BOOST_CHECK(vIndex[from].GetAncestor(to) == &vIndex[to]);
      41        1000 :         BOOST_CHECK(vIndex[from].GetAncestor(0) == vIndex.data());
      42             :     }
      43           1 : }
      44             : 
      45          95 : BOOST_AUTO_TEST_CASE(getlocator_test)
      46             : {
      47             :     // Build a main chain 100000 blocks long.
      48           1 :     std::vector<uint256> vHashMain(100000);
      49           1 :     std::vector<CBlockIndex> vBlocksMain(100000);
      50      100001 :     for (unsigned int i=0; i<vBlocksMain.size(); i++) {
      51      100000 :         vHashMain[i] = ArithToUint256(i); // Set the hash equal to the height, so we can quickly check the distances.
      52      100000 :         vBlocksMain[i].nHeight = i;
      53      100000 :         vBlocksMain[i].pprev = i ? &vBlocksMain[i - 1] : nullptr;
      54      100000 :         vBlocksMain[i].phashBlock = &vHashMain[i];
      55      100000 :         vBlocksMain[i].BuildSkip();
      56      100000 :         BOOST_CHECK_EQUAL((int)UintToArith256(vBlocksMain[i].GetBlockHash()).GetLow64(), vBlocksMain[i].nHeight);
      57      100000 :         BOOST_CHECK(vBlocksMain[i].pprev == nullptr || vBlocksMain[i].nHeight == vBlocksMain[i].pprev->nHeight + 1);
      58             :     }
      59             : 
      60             :     // Build a branch that splits off at block 49999, 50000 blocks long.
      61           1 :     std::vector<uint256> vHashSide(50000);
      62           1 :     std::vector<CBlockIndex> vBlocksSide(50000);
      63       50001 :     for (unsigned int i=0; i<vBlocksSide.size(); i++) {
      64       50000 :         vHashSide[i] = ArithToUint256(i + 50000 + (arith_uint256(1) << 128)); // Add 1<<128 to the hashes, so GetLow64() still returns the height.
      65       50000 :         vBlocksSide[i].nHeight = i + 50000;
      66       50000 :         vBlocksSide[i].pprev = i ? &vBlocksSide[i - 1] : (vBlocksMain.data()+49999);
      67       50000 :         vBlocksSide[i].phashBlock = &vHashSide[i];
      68       50000 :         vBlocksSide[i].BuildSkip();
      69       50000 :         BOOST_CHECK_EQUAL((int)UintToArith256(vBlocksSide[i].GetBlockHash()).GetLow64(), vBlocksSide[i].nHeight);
      70       50000 :         BOOST_CHECK(vBlocksSide[i].pprev == nullptr || vBlocksSide[i].nHeight == vBlocksSide[i].pprev->nHeight + 1);
      71             :     }
      72             : 
      73             :     // Build a CChain for the main branch.
      74           1 :     CChain chain;
      75           1 :     chain.SetTip(&vBlocksMain.back());
      76             : 
      77             :     // Test 100 random starting points for locators.
      78         101 :     for (int n=0; n<100; n++) {
      79         100 :         int r = InsecureRandRange(150000);
      80         100 :         CBlockIndex* tip = (r < 100000) ? &vBlocksMain[r] : &vBlocksSide[r - 100000];
      81         100 :         CBlockLocator locator = chain.GetLocator(tip);
      82             : 
      83             :         // The first result must be the block itself, the last one must be genesis.
      84         100 :         BOOST_CHECK(locator.vHave.front() == tip->GetBlockHash());
      85         100 :         BOOST_CHECK(locator.vHave.back() == vBlocksMain[0].GetBlockHash());
      86             : 
      87             :         // Entries 1 through 11 (inclusive) go back one step each.
      88        1200 :         for (unsigned int i = 1; i < 12 && i < locator.vHave.size() - 1; i++) {
      89        1100 :             BOOST_CHECK_EQUAL(UintToArith256(locator.vHave[i]).GetLow64(), tip->nHeight - i);
      90             :         }
      91             : 
      92             :         // The further ones (excluding the last one) go back with exponential steps.
      93         100 :         unsigned int dist = 2;
      94        1496 :         for (unsigned int i = 12; i < locator.vHave.size() - 1; i++) {
      95        1396 :             BOOST_CHECK_EQUAL(UintToArith256(locator.vHave[i - 1]).GetLow64() - UintToArith256(locator.vHave[i]).GetLow64(), dist);
      96        1396 :             dist *= 2;
      97             :         }
      98         100 :     }
      99           1 : }
     100             : 
     101          95 : BOOST_AUTO_TEST_CASE(findearliestatleast_test)
     102             : {
     103           1 :     std::vector<uint256> vHashMain(100000);
     104           1 :     std::vector<CBlockIndex> vBlocksMain(100000);
     105      100001 :     for (unsigned int i=0; i<vBlocksMain.size(); i++) {
     106      100000 :         vHashMain[i] = ArithToUint256(i); // Set the hash equal to the height
     107      100000 :         vBlocksMain[i].nHeight = i;
     108      100000 :         vBlocksMain[i].pprev = i ? &vBlocksMain[i - 1] : nullptr;
     109      100000 :         vBlocksMain[i].phashBlock = &vHashMain[i];
     110      100000 :         vBlocksMain[i].BuildSkip();
     111      100000 :         if (i < 10) {
     112          10 :             vBlocksMain[i].nTime = i;
     113          10 :             vBlocksMain[i].nTimeMax = i;
     114          10 :         } else {
     115             :             // randomly choose something in the range [MTP, MTP*2]
     116       99990 :             int64_t medianTimePast = vBlocksMain[i].GetMedianTimePast();
     117       99990 :             int r = InsecureRandRange(medianTimePast);
     118       99990 :             vBlocksMain[i].nTime = r + medianTimePast;
     119       99990 :             vBlocksMain[i].nTimeMax = std::max(vBlocksMain[i].nTime, vBlocksMain[i-1].nTimeMax);
     120           0 :         }
     121             :     }
     122             :     // Check that we set nTimeMax up correctly.
     123           1 :     unsigned int curTimeMax = 0;
     124      100001 :     for (unsigned int i=0; i<vBlocksMain.size(); ++i) {
     125      100000 :         curTimeMax = std::max(curTimeMax, vBlocksMain[i].nTime);
     126      100000 :         BOOST_CHECK(curTimeMax == vBlocksMain[i].nTimeMax);
     127             :     }
     128             : 
     129             :     // Build a CChain for the main branch.
     130           1 :     CChain chain;
     131           1 :     chain.SetTip(&vBlocksMain.back());
     132             : 
     133             :     // Verify that FindEarliestAtLeast is correct.
     134       10001 :     for (unsigned int i=0; i<10000; ++i) {
     135             :         // Pick a random element in vBlocksMain.
     136       10000 :         int r = InsecureRandRange(vBlocksMain.size());
     137       10000 :         int64_t test_time = vBlocksMain[r].nTime;
     138       10000 :         CBlockIndex* ret = chain.FindEarliestAtLeast(test_time, 0);
     139       10000 :         BOOST_CHECK(ret->nTimeMax >= test_time);
     140       10000 :         BOOST_CHECK((ret->pprev==nullptr) || ret->pprev->nTimeMax < test_time);
     141       10000 :         BOOST_CHECK(vBlocksMain[r].GetAncestor(ret->nHeight) == ret);
     142             :     }
     143           1 : }
     144             : 
     145          95 : BOOST_AUTO_TEST_CASE(findearliestatleast_edge_test)
     146             : {
     147           1 :     std::list<CBlockIndex> blocks;
     148          10 :     for (const unsigned int timeMax : {100, 100, 100, 200, 200, 200, 300, 300, 300}) {
     149           9 :         CBlockIndex* prev = blocks.empty() ? nullptr : &blocks.back();
     150           9 :         blocks.emplace_back();
     151           9 :         blocks.back().nHeight = prev ? prev->nHeight + 1 : 0;
     152           9 :         blocks.back().pprev = prev;
     153           9 :         blocks.back().BuildSkip();
     154           9 :         blocks.back().nTimeMax = timeMax;
     155             :     }
     156             : 
     157           1 :     CChain chain;
     158           1 :     chain.SetTip(&blocks.back());
     159             : 
     160           1 :     BOOST_CHECK_EQUAL(chain.FindEarliestAtLeast(50, 0)->nHeight, 0);
     161           1 :     BOOST_CHECK_EQUAL(chain.FindEarliestAtLeast(100, 0)->nHeight, 0);
     162           1 :     BOOST_CHECK_EQUAL(chain.FindEarliestAtLeast(150, 0)->nHeight, 3);
     163           1 :     BOOST_CHECK_EQUAL(chain.FindEarliestAtLeast(200, 0)->nHeight, 3);
     164           1 :     BOOST_CHECK_EQUAL(chain.FindEarliestAtLeast(250, 0)->nHeight, 6);
     165           1 :     BOOST_CHECK_EQUAL(chain.FindEarliestAtLeast(300, 0)->nHeight, 6);
     166           1 :     BOOST_CHECK(!chain.FindEarliestAtLeast(350, 0));
     167             : 
     168           1 :     BOOST_CHECK_EQUAL(chain.FindEarliestAtLeast(0, 0)->nHeight, 0);
     169           1 :     BOOST_CHECK_EQUAL(chain.FindEarliestAtLeast(-1, 0)->nHeight, 0);
     170             : 
     171           1 :     BOOST_CHECK_EQUAL(chain.FindEarliestAtLeast(std::numeric_limits<int64_t>::min(), 0)->nHeight, 0);
     172           1 :     BOOST_CHECK_EQUAL(chain.FindEarliestAtLeast(-int64_t(std::numeric_limits<unsigned int>::max()) - 1, 0)->nHeight, 0);
     173           1 :     BOOST_CHECK(!chain.FindEarliestAtLeast(std::numeric_limits<int64_t>::max(), 0));
     174           1 :     BOOST_CHECK(!chain.FindEarliestAtLeast(std::numeric_limits<unsigned int>::max(), 0));
     175           1 :     BOOST_CHECK(!chain.FindEarliestAtLeast(int64_t(std::numeric_limits<unsigned int>::max()) + 1, 0));
     176             : 
     177           1 :     BOOST_CHECK_EQUAL(chain.FindEarliestAtLeast(0, -1)->nHeight, 0);
     178           1 :     BOOST_CHECK_EQUAL(chain.FindEarliestAtLeast(0, 0)->nHeight, 0);
     179           1 :     BOOST_CHECK_EQUAL(chain.FindEarliestAtLeast(0, 3)->nHeight, 3);
     180           1 :     BOOST_CHECK_EQUAL(chain.FindEarliestAtLeast(0, 8)->nHeight, 8);
     181           1 :     BOOST_CHECK(!chain.FindEarliestAtLeast(0, 9));
     182             : 
     183           1 :     CBlockIndex* ret1 = chain.FindEarliestAtLeast(100, 2);
     184           1 :     BOOST_CHECK(ret1->nTimeMax >= 100 && ret1->nHeight == 2);
     185           1 :     BOOST_CHECK(!chain.FindEarliestAtLeast(300, 9));
     186           1 :     CBlockIndex* ret2 = chain.FindEarliestAtLeast(200, 4);
     187           1 :     BOOST_CHECK(ret2->nTimeMax >= 200 && ret2->nHeight == 4);
     188           1 : }
     189             : 
     190          89 : BOOST_AUTO_TEST_SUITE_END()

Generated by: LCOV version 1.15