Skip to content

Commit de1dc6b

Browse files
committed
Merge bitcoin/bitcoin#33515: Improve LastCommonAncestor performance + add tests
3635d62 chain: make use of pskip in LastCommonAncestor (optimization) (Pieter Wuille) 2e09d66 tests: add unit tests for CBlockIndex::GetAncestor and LastCommonAncestor (Pieter Wuille) Pull request description: In theory, the `LastCommonAncestor` function in chain.cpp can take $\mathcal{O}(n)$ time, walking over the entire chain, if the forking point is very early, which could take ~milliseconds. I expect this to be very rare in normal occurrences, but it seems nontrivial to reason about worst cases as it's accessible from several places in net_processing. This PR modifies the algorithm to make use of the `CBlockIndex::pskip` skip pointers to find the forking point in sublinear time (a simulation shows that for heights up to $34 \cdot 4^k - 2$ and $k \geq 8$, no more than $k^2 + 10k + 13$ steps are ever needed), in a way that should be nearly free - at worst the same number of memory accesses should be made, with a tiny increase in computation. As it appears we didn't really have tests for this function, unit tests are added for that function as well as `CBlockIndex::GetAncestor()`. This is inspired by bitcoin/bitcoin#32180 (comment) ACKs for top commit: optout21: ACK 3635d62 achow101: ACK 3635d62 vasild: ACK 3635d62 mzumsande: Code Review ACK 3635d62 furszy: ACK 3635d62 stratospher: ACK 3635d62. Tree-SHA512: f9b7dea1e34c1cc1ec1da3fb9e90c4acbf4aaf0f04768844f538201efa6b11eeeefc97b720509e78c21878977192e2c4031fd8974151667e2e756247002b8164
2 parents 919e6d0 + 3635d62 commit de1dc6b

File tree

3 files changed

+100
-5
lines changed

3 files changed

+100
-5
lines changed

src/chain.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <chain.h>
77
#include <tinyformat.h>
8+
#include <util/check.h>
89
#include <util/time.h>
910

1011
std::string CBlockFileInfo::ToString() const
@@ -158,18 +159,26 @@ int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& fr
158159
/** Find the last common ancestor two blocks have.
159160
* Both pa and pb must be non-nullptr. */
160161
const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex* pb) {
162+
// First rewind to the last common height (the forking point cannot be past one of the two).
161163
if (pa->nHeight > pb->nHeight) {
162164
pa = pa->GetAncestor(pb->nHeight);
163165
} else if (pb->nHeight > pa->nHeight) {
164166
pb = pb->GetAncestor(pa->nHeight);
165167
}
166-
167-
while (pa != pb && pa && pb) {
168+
while (pa != pb) {
169+
// Jump back until pa and pb have a common "skip" ancestor.
170+
while (pa->pskip != pb->pskip) {
171+
// This logic relies on the property that equal-height blocks have equal-height skip
172+
// pointers.
173+
Assume(pa->nHeight == pb->nHeight);
174+
Assume(pa->pskip->nHeight == pb->pskip->nHeight);
175+
pa = pa->pskip;
176+
pb = pb->pskip;
177+
}
178+
// At this point, pa and pb are different, but have equal pskip. The forking point lies in
179+
// between pa/pb on the one end, and pa->pskip/pb->pskip on the other end.
168180
pa = pa->pprev;
169181
pb = pb->pprev;
170182
}
171-
172-
// Eventually all chain branches meet at the genesis block.
173-
assert(pa == pb);
174183
return pa;
175184
}

src/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ add_executable(test_bitcoin
2626
bloom_tests.cpp
2727
bswap_tests.cpp
2828
caches_tests.cpp
29+
chain_tests.cpp
2930
chainstate_write_tests.cpp
3031
checkqueue_tests.cpp
3132
cluster_linearize_tests.cpp

src/test/chain_tests.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright (c) 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.
4+
5+
#include <boost/test/unit_test.hpp>
6+
7+
#include <chain.h>
8+
#include <test/util/setup_common.h>
9+
10+
#include <memory>
11+
12+
BOOST_FIXTURE_TEST_SUITE(chain_tests, BasicTestingSetup)
13+
14+
namespace {
15+
16+
const CBlockIndex* NaiveGetAncestor(const CBlockIndex* a, int height)
17+
{
18+
while (a->nHeight > height) {
19+
a = a->pprev;
20+
}
21+
BOOST_REQUIRE_EQUAL(a->nHeight, height);
22+
return a;
23+
}
24+
25+
const CBlockIndex* NaiveLastCommonAncestor(const CBlockIndex* a, const CBlockIndex* b)
26+
{
27+
while (a->nHeight > b->nHeight) {
28+
a = a->pprev;
29+
}
30+
while (b->nHeight > a->nHeight) {
31+
b = b->pprev;
32+
}
33+
while (a != b) {
34+
BOOST_REQUIRE_EQUAL(a->nHeight, b->nHeight);
35+
a = a->pprev;
36+
b = b->pprev;
37+
}
38+
BOOST_REQUIRE_EQUAL(a, b);
39+
return a;
40+
}
41+
42+
} // namespace
43+
44+
BOOST_AUTO_TEST_CASE(chain_test)
45+
{
46+
FastRandomContext ctx;
47+
std::vector<std::unique_ptr<CBlockIndex>> block_index;
48+
// Run 10 iterations of the whole test.
49+
for (int i = 0; i < 10; ++i) {
50+
block_index.clear();
51+
// Create genesis block.
52+
auto genesis = std::make_unique<CBlockIndex>();
53+
genesis->nHeight = 0;
54+
block_index.push_back(std::move(genesis));
55+
// Create 10000 more blocks.
56+
for (int b = 0; b < 10000; ++b) {
57+
auto new_index = std::make_unique<CBlockIndex>();
58+
// 95% of blocks build on top of the last block; the others fork off randomly.
59+
if (ctx.randrange(20) != 0) {
60+
new_index->pprev = block_index.back().get();
61+
} else {
62+
new_index->pprev = block_index[ctx.randrange(block_index.size())].get();
63+
}
64+
new_index->nHeight = new_index->pprev->nHeight + 1;
65+
new_index->BuildSkip();
66+
block_index.push_back(std::move(new_index));
67+
}
68+
// Run 10000 random GetAncestor queries.
69+
for (int q = 0; q < 10000; ++q) {
70+
const CBlockIndex* block = block_index[ctx.randrange(block_index.size())].get();
71+
unsigned height = ctx.randrange<unsigned>(block->nHeight + 1);
72+
const CBlockIndex* result = block->GetAncestor(height);
73+
BOOST_CHECK(result == NaiveGetAncestor(block, height));
74+
}
75+
// Run 10000 random LastCommonAncestor queries.
76+
for (int q = 0; q < 10000; ++q) {
77+
const CBlockIndex* block1 = block_index[ctx.randrange(block_index.size())].get();
78+
const CBlockIndex* block2 = block_index[ctx.randrange(block_index.size())].get();
79+
const CBlockIndex* result = LastCommonAncestor(block1, block2);
80+
BOOST_CHECK(result == NaiveLastCommonAncestor(block1, block2));
81+
}
82+
}
83+
}
84+
85+
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)