1 // Copyright (c) 2014 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.
11 #include <boost/test/unit_test.hpp>
13 #define SKIPLIST_LENGTH 300000
15 BOOST_AUTO_TEST_SUITE(skiplist_tests)
17 BOOST_AUTO_TEST_CASE(skiplist_test)
19 std::vector<CBlockIndex> vIndex(SKIPLIST_LENGTH);
21 for (int i=0; i<SKIPLIST_LENGTH; i++) {
22 vIndex[i].nHeight = i;
23 vIndex[i].pprev = (i == 0) ? NULL : &vIndex[i - 1];
24 vIndex[i].BuildSkip();
27 for (int i=0; i<SKIPLIST_LENGTH; i++) {
29 BOOST_CHECK(vIndex[i].pskip == &vIndex[vIndex[i].pskip->nHeight]);
30 BOOST_CHECK(vIndex[i].pskip->nHeight < i);
32 BOOST_CHECK(vIndex[i].pskip == NULL);
36 for (int i=0; i < 1000; i++) {
37 int from = insecure_rand() % (SKIPLIST_LENGTH - 1);
38 int to = insecure_rand() % (from + 1);
40 BOOST_CHECK(vIndex[SKIPLIST_LENGTH - 1].GetAncestor(from) == &vIndex[from]);
41 BOOST_CHECK(vIndex[from].GetAncestor(to) == &vIndex[to]);
42 BOOST_CHECK(vIndex[from].GetAncestor(0) == &vIndex[0]);
46 BOOST_AUTO_TEST_CASE(getlocator_test)
48 // Build a main chain 100000 blocks long.
49 std::vector<uint256> vHashMain(100000);
50 std::vector<CBlockIndex> vBlocksMain(100000);
51 for (unsigned int i=0; i<vBlocksMain.size(); i++) {
52 vHashMain[i] = i; // Set the hash equal to the height, so we can quickly check the distances.
53 vBlocksMain[i].nHeight = i;
54 vBlocksMain[i].pprev = i ? &vBlocksMain[i - 1] : NULL;
55 vBlocksMain[i].phashBlock = &vHashMain[i];
56 vBlocksMain[i].BuildSkip();
57 BOOST_CHECK_EQUAL((int)vBlocksMain[i].GetBlockHash().GetLow64(), vBlocksMain[i].nHeight);
58 BOOST_CHECK(vBlocksMain[i].pprev == NULL || vBlocksMain[i].nHeight == vBlocksMain[i].pprev->nHeight + 1);
61 // Build a branch that splits off at block 49999, 50000 blocks long.
62 std::vector<uint256> vHashSide(50000);
63 std::vector<CBlockIndex> vBlocksSide(50000);
64 for (unsigned int i=0; i<vBlocksSide.size(); i++) {
65 vHashSide[i] = i + 50000 + (uint256(1) << 128); // Add 1<<128 to the hashes, so GetLow64() still returns the height.
66 vBlocksSide[i].nHeight = i + 50000;
67 vBlocksSide[i].pprev = i ? &vBlocksSide[i - 1] : &vBlocksMain[49999];
68 vBlocksSide[i].phashBlock = &vHashSide[i];
69 vBlocksSide[i].BuildSkip();
70 BOOST_CHECK_EQUAL((int)vBlocksSide[i].GetBlockHash().GetLow64(), vBlocksSide[i].nHeight);
71 BOOST_CHECK(vBlocksSide[i].pprev == NULL || vBlocksSide[i].nHeight == vBlocksSide[i].pprev->nHeight + 1);
74 // Build a CChain for the main branch.
76 chain.SetTip(&vBlocksMain.back());
78 // Test 100 random starting points for locators.
79 for (int n=0; n<100; n++) {
80 int r = insecure_rand() % 150000;
81 CBlockIndex* tip = (r < 100000) ? &vBlocksMain[r] : &vBlocksSide[r - 100000];
82 CBlockLocator locator = chain.GetLocator(tip);
84 // The first result must be the block itself, the last one must be genesis.
85 BOOST_CHECK(locator.vHave.front() == tip->GetBlockHash());
86 BOOST_CHECK(locator.vHave.back() == vBlocksMain[0].GetBlockHash());
88 // Entries 1 through 11 (inclusive) go back one step each.
89 for (unsigned int i = 1; i < 12 && i < locator.vHave.size() - 1; i++) {
90 BOOST_CHECK_EQUAL(locator.vHave[i].GetLow64(), tip->nHeight - i);
93 // The further ones (excluding the last one) go back with exponential steps.
94 unsigned int dist = 2;
95 for (unsigned int i = 12; i < locator.vHave.size() - 1; i++) {
96 BOOST_CHECK_EQUAL(locator.vHave[i - 1].GetLow64() - locator.vHave[i].GetLow64(), dist);
102 BOOST_AUTO_TEST_SUITE_END()