Skip to content

Commit 5e1b7a2

Browse files
committed
Merge pull request #4470
f4b00be Add CChain::GetLocator() unit test (Pieter Wuille) 3c85d2e Fix CChain::GetLocator (Pieter Wuille)
2 parents bc06e8f + f4b00be commit 5e1b7a2

File tree

2 files changed

+62
-8
lines changed

2 files changed

+62
-8
lines changed

src/main.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -423,15 +423,13 @@ CBlockLocator CChain::GetLocator(const CBlockIndex *pindex) const {
423423
break;
424424
// Exponentially larger steps back, plus the genesis block.
425425
int nHeight = std::max(pindex->nHeight - nStep, 0);
426-
// Jump back quickly to the same height as the chain.
427-
if (pindex->nHeight > nHeight)
428-
pindex = pindex->GetAncestor(nHeight);
429-
// In case pindex is not in this chain, iterate pindex->pprev to find blocks.
430-
while (!Contains(pindex))
431-
pindex = pindex->pprev;
432-
// If pindex is in this chain, use direct height-based access.
433-
if (pindex->nHeight > nHeight)
426+
if (Contains(pindex)) {
427+
// Use O(1) CChain index if possible.
434428
pindex = (*this)[nHeight];
429+
} else {
430+
// Otherwise, use O(log n) skiplist.
431+
pindex = pindex->GetAncestor(nHeight);
432+
}
435433
if (vHave.size() > 10)
436434
nStep *= 2;
437435
}

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)