// Copyright (c) 2014 The Bitcoin Core developers // Distributed under the GPLv3 software license, see the accompanying // file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html #include "main.h" #include "random.h" #include "util.h" #include "test/test_bitcoin.h" #include #include #define SKIPLIST_LENGTH 300000 BOOST_FIXTURE_TEST_SUITE(skiplist_tests, BasicTestingSetup) BOOST_AUTO_TEST_CASE(skiplist_test) { std::vector vIndex(SKIPLIST_LENGTH); for (int i=0; i 0) { BOOST_CHECK(vIndex[i].pskip == &vIndex[vIndex[i].pskip->GetHeight()]); BOOST_CHECK(vIndex[i].pskip->GetHeight() < i); } else { BOOST_CHECK(vIndex[i].pskip == NULL); } } for (int i=0; i < 1000; i++) { int from = insecure_rand() % (SKIPLIST_LENGTH - 1); int to = insecure_rand() % (from + 1); BOOST_CHECK(vIndex[SKIPLIST_LENGTH - 1].GetAncestor(from) == &vIndex[from]); BOOST_CHECK(vIndex[from].GetAncestor(to) == &vIndex[to]); BOOST_CHECK(vIndex[from].GetAncestor(0) == &vIndex[0]); } } BOOST_AUTO_TEST_CASE(getlocator_test) { // Build a main chain 100000 blocks long. std::vector vHashMain(100000); std::vector vBlocksMain(100000); for (unsigned int i=0; iGetHeight() + 1); } // Build a branch that splits off at block 49999, 50000 blocks long. std::vector vHashSide(50000); std::vector vBlocksSide(50000); for (unsigned int i=0; iGetHeight() + 1); } // Build a CChain for the main branch. CChain chain; chain.SetTip(&vBlocksMain.back()); // Test 100 random starting points for locators. for (int n=0; n<100; n++) { int r = insecure_rand() % 150000; CBlockIndex* tip = (r < 100000) ? &vBlocksMain[r] : &vBlocksSide[r - 100000]; CBlockLocator locator = chain.GetLocator(tip); // The first result must be the block itself, the last one must be genesis. BOOST_CHECK(locator.vHave.front() == tip->GetBlockHash()); BOOST_CHECK(locator.vHave.back() == vBlocksMain[0].GetBlockHash()); // Entries 1 through 11 (inclusive) go back one step each. for (unsigned int i = 1; i < 12 && i < locator.vHave.size() - 1; i++) { BOOST_CHECK_EQUAL(UintToArith256(locator.vHave[i]).GetLow64(), tip->GetHeight() - i); } // The further ones (excluding the last one) go back with exponential steps. unsigned int dist = 2; for (unsigned int i = 12; i < locator.vHave.size() - 1; i++) { BOOST_CHECK_EQUAL(UintToArith256(locator.vHave[i - 1]).GetLow64() - UintToArith256(locator.vHave[i]).GetLow64(), dist); dist *= 2; } } } BOOST_AUTO_TEST_SUITE_END()