2014-06-29 15:26:58 +02:00
|
|
|
// Copyright (c) 2014 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2014-06-29 15:26:58 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include "main.h"
|
2014-06-26 14:41:53 +02:00
|
|
|
#include "random.h"
|
2014-06-29 15:26:58 +02:00
|
|
|
#include "util.h"
|
2015-03-12 09:34:42 +01:00
|
|
|
#include "test/test_bitcoin.h"
|
2014-06-29 15:26:58 +02:00
|
|
|
|
2014-06-26 14:41:53 +02:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
2014-06-29 15:26:58 +02:00
|
|
|
|
|
|
|
#define SKIPLIST_LENGTH 300000
|
|
|
|
|
2015-03-12 09:34:42 +01:00
|
|
|
BOOST_FIXTURE_TEST_SUITE(skiplist_tests, BasicTestingSetup)
|
2014-06-29 15:26:58 +02:00
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(skiplist_test)
|
|
|
|
{
|
|
|
|
std::vector<CBlockIndex> vIndex(SKIPLIST_LENGTH);
|
|
|
|
|
|
|
|
for (int i=0; i<SKIPLIST_LENGTH; i++) {
|
|
|
|
vIndex[i].nHeight = i;
|
|
|
|
vIndex[i].pprev = (i == 0) ? NULL : &vIndex[i - 1];
|
|
|
|
vIndex[i].BuildSkip();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i=0; i<SKIPLIST_LENGTH; i++) {
|
|
|
|
if (i > 0) {
|
|
|
|
BOOST_CHECK(vIndex[i].pskip == &vIndex[vIndex[i].pskip->nHeight]);
|
|
|
|
BOOST_CHECK(vIndex[i].pskip->nHeight < 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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-06 14:40:31 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(getlocator_test)
|
|
|
|
{
|
|
|
|
// Build a main chain 100000 blocks long.
|
|
|
|
std::vector<uint256> vHashMain(100000);
|
|
|
|
std::vector<CBlockIndex> vBlocksMain(100000);
|
|
|
|
for (unsigned int i=0; i<vBlocksMain.size(); i++) {
|
2014-12-16 15:43:03 +01:00
|
|
|
vHashMain[i] = ArithToUint256(i); // Set the hash equal to the height, so we can quickly check the distances.
|
2014-07-06 14:40:31 +02:00
|
|
|
vBlocksMain[i].nHeight = i;
|
|
|
|
vBlocksMain[i].pprev = i ? &vBlocksMain[i - 1] : NULL;
|
|
|
|
vBlocksMain[i].phashBlock = &vHashMain[i];
|
|
|
|
vBlocksMain[i].BuildSkip();
|
2014-12-16 15:43:03 +01:00
|
|
|
BOOST_CHECK_EQUAL((int)UintToArith256(vBlocksMain[i].GetBlockHash()).GetLow64(), vBlocksMain[i].nHeight);
|
2014-07-06 14:40:31 +02:00
|
|
|
BOOST_CHECK(vBlocksMain[i].pprev == NULL || vBlocksMain[i].nHeight == vBlocksMain[i].pprev->nHeight + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build a branch that splits off at block 49999, 50000 blocks long.
|
|
|
|
std::vector<uint256> vHashSide(50000);
|
|
|
|
std::vector<CBlockIndex> vBlocksSide(50000);
|
|
|
|
for (unsigned int i=0; i<vBlocksSide.size(); i++) {
|
2014-12-16 15:43:03 +01:00
|
|
|
vHashSide[i] = ArithToUint256(i + 50000 + (arith_uint256(1) << 128)); // Add 1<<128 to the hashes, so GetLow64() still returns the height.
|
2014-07-06 14:40:31 +02:00
|
|
|
vBlocksSide[i].nHeight = i + 50000;
|
|
|
|
vBlocksSide[i].pprev = i ? &vBlocksSide[i - 1] : &vBlocksMain[49999];
|
|
|
|
vBlocksSide[i].phashBlock = &vHashSide[i];
|
|
|
|
vBlocksSide[i].BuildSkip();
|
2014-12-16 15:43:03 +01:00
|
|
|
BOOST_CHECK_EQUAL((int)UintToArith256(vBlocksSide[i].GetBlockHash()).GetLow64(), vBlocksSide[i].nHeight);
|
2014-07-06 14:40:31 +02:00
|
|
|
BOOST_CHECK(vBlocksSide[i].pprev == NULL || vBlocksSide[i].nHeight == vBlocksSide[i].pprev->nHeight + 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++) {
|
2014-12-16 15:43:03 +01:00
|
|
|
BOOST_CHECK_EQUAL(UintToArith256(locator.vHave[i]).GetLow64(), tip->nHeight - i);
|
2014-07-06 14:40:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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++) {
|
2014-12-16 15:43:03 +01:00
|
|
|
BOOST_CHECK_EQUAL(UintToArith256(locator.vHave[i - 1]).GetLow64() - UintToArith256(locator.vHave[i]).GetLow64(), dist);
|
2014-07-06 14:40:31 +02:00
|
|
|
dist *= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-29 15:26:58 +02:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|