Skip to content

Commit f4b00be

Browse files
committed
Add CChain::GetLocator() unit test
1 parent 3c85d2e commit f4b00be

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

src/test/skiplist_tests.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,61 @@ BOOST_AUTO_TEST_CASE(skiplist_test)
4141
}
4242
}
4343

44+
BOOST_AUTO_TEST_CASE(getlocator_test)
45+
{
46+
// Build a main chain 100000 blocks long.
47+
std::vector<uint256> vHashMain(100000);
48+
std::vector<CBlockIndex> vBlocksMain(100000);
49+
for (unsigned int i=0; i<vBlocksMain.size(); i++) {
50+
vHashMain[i] = i; // Set the hash equal to the height, so we can quickly check the distances.
51+
vBlocksMain[i].nHeight = i;
52+
vBlocksMain[i].pprev = i ? &vBlocksMain[i - 1] : NULL;
53+
vBlocksMain[i].phashBlock = &vHashMain[i];
54+
vBlocksMain[i].BuildSkip();
55+
BOOST_CHECK_EQUAL((int)vBlocksMain[i].GetBlockHash().GetLow64(), vBlocksMain[i].nHeight);
56+
BOOST_CHECK(vBlocksMain[i].pprev == NULL || vBlocksMain[i].nHeight == vBlocksMain[i].pprev->nHeight + 1);
57+
}
58+
59+
// Build a branch that splits off at block 49999, 50000 blocks long.
60+
std::vector<uint256> vHashSide(50000);
61+
std::vector<CBlockIndex> vBlocksSide(50000);
62+
for (unsigned int i=0; i<vBlocksSide.size(); i++) {
63+
vHashSide[i] = i + 50000 + (uint256(1) << 128); // Add 1<<128 to the hashes, so GetLow64() still returns the height.
64+
vBlocksSide[i].nHeight = i + 50000;
65+
vBlocksSide[i].pprev = i ? &vBlocksSide[i - 1] : &vBlocksMain[49999];
66+
vBlocksSide[i].phashBlock = &vHashSide[i];
67+
vBlocksSide[i].BuildSkip();
68+
BOOST_CHECK_EQUAL((int)vBlocksSide[i].GetBlockHash().GetLow64(), vBlocksSide[i].nHeight);
69+
BOOST_CHECK(vBlocksSide[i].pprev == NULL || vBlocksSide[i].nHeight == vBlocksSide[i].pprev->nHeight + 1);
70+
}
71+
72+
// Build a CChain for the main branch.
73+
CChain chain;
74+
chain.SetTip(&vBlocksMain.back());
75+
76+
// Test 100 random starting points for locators.
77+
for (int n=0; n<100; n++) {
78+
int r = insecure_rand() % 150000;
79+
CBlockIndex* tip = (r < 100000) ? &vBlocksMain[r] : &vBlocksSide[r - 100000];
80+
CBlockLocator locator = chain.GetLocator(tip);
81+
82+
// The first result must be the block itself, the last one must be genesis.
83+
BOOST_CHECK(locator.vHave.front() == tip->GetBlockHash());
84+
BOOST_CHECK(locator.vHave.back() == vBlocksMain[0].GetBlockHash());
85+
86+
// Entries 1 through 11 (inclusive) go back one step each.
87+
for (unsigned int i = 1; i < 12 && i < locator.vHave.size() - 1; i++) {
88+
BOOST_CHECK_EQUAL(locator.vHave[i].GetLow64(), tip->nHeight - i);
89+
}
90+
91+
// The further ones (excluding the last one) go back with exponential steps.
92+
unsigned int dist = 2;
93+
for (unsigned int i = 12; i < locator.vHave.size() - 1; i++) {
94+
BOOST_CHECK_EQUAL(locator.vHave[i - 1].GetLow64() - locator.vHave[i].GetLow64(), dist);
95+
dist *= 2;
96+
}
97+
}
98+
}
99+
44100
BOOST_AUTO_TEST_SUITE_END()
45101

0 commit comments

Comments
 (0)