From 3c85d2ec37c3725e407649e487fbeb2a36606d74 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Sun, 6 Jul 2014 13:37:32 +0200 Subject: [PATCH 1/2] Fix CChain::GetLocator --- src/main.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index b2773953..3de60ee3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -423,15 +423,13 @@ CBlockLocator CChain::GetLocator(const CBlockIndex *pindex) const { break; // Exponentially larger steps back, plus the genesis block. int nHeight = std::max(pindex->nHeight - nStep, 0); - // Jump back quickly to the same height as the chain. - if (pindex->nHeight > nHeight) - pindex = pindex->GetAncestor(nHeight); - // In case pindex is not in this chain, iterate pindex->pprev to find blocks. - while (!Contains(pindex)) - pindex = pindex->pprev; - // If pindex is in this chain, use direct height-based access. - if (pindex->nHeight > nHeight) + if (Contains(pindex)) { + // Use O(1) CChain index if possible. pindex = (*this)[nHeight]; + } else { + // Otherwise, use O(log n) skiplist. + pindex = pindex->GetAncestor(nHeight); + } if (vHave.size() > 10) nStep *= 2; } From f4b00beae52f22fc6958855a85e1012387c3c33e Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Sun, 6 Jul 2014 14:40:31 +0200 Subject: [PATCH 2/2] Add CChain::GetLocator() unit test --- src/test/skiplist_tests.cpp | 56 +++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/test/skiplist_tests.cpp b/src/test/skiplist_tests.cpp index ea301685..11762c6e 100644 --- a/src/test/skiplist_tests.cpp +++ b/src/test/skiplist_tests.cpp @@ -41,5 +41,61 @@ BOOST_AUTO_TEST_CASE(skiplist_test) } } +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; inHeight + 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; inHeight + 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(locator.vHave[i].GetLow64(), tip->nHeight - 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(locator.vHave[i - 1].GetLow64() - locator.vHave[i].GetLow64(), dist); + dist *= 2; + } + } +} + BOOST_AUTO_TEST_SUITE_END()