Skip to content

Commit 6bcf099

Browse files
committed
test: Unit tests for block index filter.
1 parent b5e8200 commit 6bcf099

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ BITCOIN_TESTS =\
9292
test/blockchain_tests.cpp \
9393
test/blockencodings_tests.cpp \
9494
test/blockfilter_tests.cpp \
95+
test/blockfilter_index_tests.cpp \
9596
test/bloom_tests.cpp \
9697
test/bswap_tests.cpp \
9798
test/checkqueue_tests.cpp \

src/test/blockfilter_index_tests.cpp

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
// Copyright (c) 2017-2018 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 <blockfilter.h>
6+
#include <chainparams.h>
7+
#include <index/blockfilterindex.h>
8+
#include <test/test_bitcoin.h>
9+
#include <script/standard.h>
10+
#include <validation.h>
11+
12+
#include <boost/test/unit_test.hpp>
13+
14+
BOOST_AUTO_TEST_SUITE(blockfilter_index_tests)
15+
16+
static bool ComputeFilter(BlockFilterType filter_type, const CBlockIndex* block_index,
17+
BlockFilter& filter)
18+
{
19+
CBlock block;
20+
if (!ReadBlockFromDisk(block, block_index->GetBlockPos(), Params().GetConsensus())) {
21+
return false;
22+
}
23+
24+
CBlockUndo block_undo;
25+
if (block_index->nHeight > 0 && !UndoReadFromDisk(block_undo, block_index)) {
26+
return false;
27+
}
28+
29+
filter = BlockFilter(filter_type, block, block_undo);
30+
return true;
31+
}
32+
33+
static bool CheckFilterLookups(BlockFilterIndex& filter_index, const CBlockIndex* block_index,
34+
uint256& last_header)
35+
{
36+
BlockFilter expected_filter;
37+
if (!ComputeFilter(filter_index.GetFilterType(), block_index, expected_filter)) {
38+
BOOST_ERROR("ComputeFilter failed on block " << block_index->nHeight);
39+
return false;
40+
}
41+
42+
BlockFilter filter;
43+
uint256 filter_header;
44+
std::vector<BlockFilter> filters;
45+
std::vector<uint256> filter_hashes;
46+
47+
BOOST_CHECK(filter_index.LookupFilter(block_index, filter));
48+
BOOST_CHECK(filter_index.LookupFilterHeader(block_index, filter_header));
49+
BOOST_CHECK(filter_index.LookupFilterRange(block_index->nHeight, block_index, filters));
50+
BOOST_CHECK(filter_index.LookupFilterHashRange(block_index->nHeight, block_index,
51+
filter_hashes));
52+
53+
BOOST_CHECK_EQUAL(filters.size(), 1);
54+
BOOST_CHECK_EQUAL(filter_hashes.size(), 1);
55+
56+
BOOST_CHECK_EQUAL(filter.GetHash(), expected_filter.GetHash());
57+
BOOST_CHECK_EQUAL(filter_header, expected_filter.ComputeHeader(last_header));
58+
BOOST_CHECK_EQUAL(filters[0].GetHash(), expected_filter.GetHash());
59+
BOOST_CHECK_EQUAL(filter_hashes[0], expected_filter.GetHash());
60+
61+
filters.clear();
62+
filter_hashes.clear();
63+
last_header = filter_header;
64+
return true;
65+
}
66+
67+
BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync, TestChain100Setup)
68+
{
69+
BlockFilterIndex filter_index(BlockFilterType::BASIC, 1 << 20, true);
70+
71+
uint256 last_header;
72+
73+
// Filter should not be found in the index before it is started.
74+
{
75+
LOCK(cs_main);
76+
77+
BlockFilter filter;
78+
uint256 filter_header;
79+
std::vector<BlockFilter> filters;
80+
std::vector<uint256> filter_hashes;
81+
82+
for (const CBlockIndex* block_index = chainActive.Genesis();
83+
block_index != nullptr;
84+
block_index = chainActive.Next(block_index)) {
85+
BOOST_CHECK(!filter_index.LookupFilter(block_index, filter));
86+
BOOST_CHECK(!filter_index.LookupFilterHeader(block_index, filter_header));
87+
BOOST_CHECK(!filter_index.LookupFilterRange(block_index->nHeight, block_index, filters));
88+
BOOST_CHECK(!filter_index.LookupFilterHashRange(block_index->nHeight, block_index,
89+
filter_hashes));
90+
}
91+
}
92+
93+
// BlockUntilSyncedToCurrentChain should return false before index is started.
94+
BOOST_CHECK(!filter_index.BlockUntilSyncedToCurrentChain());
95+
96+
filter_index.Start();
97+
98+
// Allow filter index to catch up with the block index.
99+
constexpr int64_t timeout_ms = 10 * 1000;
100+
int64_t time_start = GetTimeMillis();
101+
while (!filter_index.BlockUntilSyncedToCurrentChain()) {
102+
BOOST_REQUIRE(time_start + timeout_ms > GetTimeMillis());
103+
MilliSleep(100);
104+
}
105+
106+
// Check that filter index has all blocks that were in the chain before it started.
107+
{
108+
LOCK(cs_main);
109+
const CBlockIndex* block_index;
110+
for (block_index = chainActive.Genesis();
111+
block_index != nullptr;
112+
block_index = chainActive.Next(block_index)) {
113+
CheckFilterLookups(filter_index, block_index, last_header);
114+
}
115+
}
116+
117+
// Check that new blocks get indexed.
118+
for (int i = 0; i < 10; i++) {
119+
CScript coinbase_script_pub_key = GetScriptForDestination(coinbaseKey.GetPubKey().GetID());
120+
std::vector<CMutableTransaction> no_txns;
121+
const CBlock& block = CreateAndProcessBlock(no_txns, coinbase_script_pub_key);
122+
const CBlockIndex* block_index;
123+
{
124+
LOCK(cs_main);
125+
block_index = LookupBlockIndex(block.GetHash());
126+
}
127+
128+
BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
129+
CheckFilterLookups(filter_index, block_index, last_header);
130+
}
131+
132+
// Test lookups for a range of filters/hashes.
133+
std::vector<BlockFilter> filters;
134+
std::vector<uint256> filter_hashes;
135+
136+
const CBlockIndex* block_index = chainActive.Tip();
137+
BOOST_CHECK(filter_index.LookupFilterRange(0, block_index, filters));
138+
BOOST_CHECK(filter_index.LookupFilterHashRange(0, block_index, filter_hashes));
139+
140+
BOOST_CHECK_EQUAL(filters.size(), chainActive.Height() + 1);
141+
BOOST_CHECK_EQUAL(filter_hashes.size(), chainActive.Height() + 1);
142+
143+
filters.clear();
144+
filter_hashes.clear();
145+
146+
filter_index.Interrupt();
147+
filter_index.Stop();
148+
}
149+
150+
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)