Branch data Line data Source code
1 : : // Copyright (c) 2017-present 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 <addresstype.h>
6 : : #include <blockfilter.h>
7 : : #include <chain.h>
8 : : #include <index/blockfilterindex.h>
9 : : #include <interfaces/chain.h>
10 : : #include <key.h>
11 : : #include <node/blockstorage.h>
12 : : #include <primitives/block.h>
13 : : #include <script/script.h>
14 : : #include <sync.h>
15 : : #include <test/util/blockfilter.h>
16 : : #include <test/util/common.h>
17 : : #include <test/util/mining.h>
18 : : #include <test/util/setup_common.h>
19 : : #include <uint256.h>
20 : : #include <util/check.h>
21 : : #include <validation.h>
22 : :
23 : : #include <boost/test/unit_test.hpp>
24 : :
25 : : #include <compare>
26 : : #include <cstddef>
27 : : #include <cstdint>
28 : : #include <functional>
29 : : #include <memory>
30 : : #include <span>
31 : : #include <string>
32 : : #include <utility>
33 : : #include <vector>
34 : :
35 : : using node::BlockManager;
36 : :
37 : : BOOST_AUTO_TEST_SUITE(blockfilter_index_tests)
38 : :
39 : 114 : static bool CheckFilterLookups(BlockFilterIndex& filter_index, const CBlockIndex* block_index,
40 : : uint256& last_header, const BlockManager& blockman)
41 : : {
42 : 114 : BlockFilter expected_filter;
43 [ + - - + ]: 114 : if (!ComputeFilter(filter_index.GetFilterType(), *block_index, expected_filter, blockman)) {
44 [ # # # # ]: 0 : BOOST_ERROR("ComputeFilter failed on block " << block_index->nHeight);
45 : 0 : return false;
46 : : }
47 : :
48 [ + - ]: 114 : BlockFilter filter;
49 : 114 : uint256 filter_header;
50 : 114 : std::vector<BlockFilter> filters;
51 : 114 : std::vector<uint256> filter_hashes;
52 : :
53 [ + - + - : 228 : BOOST_CHECK(filter_index.LookupFilter(block_index, filter));
+ - + - ]
54 [ + - + - : 228 : BOOST_CHECK(filter_index.LookupFilterHeader(block_index, filter_header));
+ - + - ]
55 [ + - + - : 228 : BOOST_CHECK(filter_index.LookupFilterRange(block_index->nHeight, block_index, filters));
+ - + - ]
56 [ + - + - : 228 : BOOST_CHECK(filter_index.LookupFilterHashRange(block_index->nHeight, block_index,
+ - + - ]
57 : : filter_hashes));
58 : :
59 [ + - - + : 114 : BOOST_CHECK_EQUAL(filters.size(), 1U);
+ - ]
60 [ + - - + : 114 : BOOST_CHECK_EQUAL(filter_hashes.size(), 1U);
+ - ]
61 : :
62 [ + - + - : 114 : BOOST_CHECK_EQUAL(filter.GetHash(), expected_filter.GetHash());
+ - + - ]
63 [ + - + - : 114 : BOOST_CHECK_EQUAL(filter_header, expected_filter.ComputeHeader(last_header));
+ - ]
64 [ + - + - : 114 : BOOST_CHECK_EQUAL(filters[0].GetHash(), expected_filter.GetHash());
+ - + - ]
65 [ + - + - : 114 : BOOST_CHECK_EQUAL(filter_hashes[0], expected_filter.GetHash());
+ - ]
66 : :
67 : 114 : filters.clear();
68 [ + - ]: 114 : filter_hashes.clear();
69 : 114 : last_header = filter_header;
70 : 114 : return true;
71 : 228 : }
72 : :
73 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync, TestChain100Setup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
74 : : {
75 [ + - ]: 1 : BlockFilterIndex filter_index(interfaces::MakeChain(m_node), BlockFilterType::BASIC, 1_MiB, true);
76 [ + - + - : 2 : BOOST_REQUIRE(filter_index.Init());
+ - + - ]
77 : :
78 : 1 : uint256 last_header;
79 : :
80 : : // Filter should not be found in the index before it is started.
81 : 1 : {
82 [ + - ]: 1 : LOCK(cs_main);
83 : :
84 [ + - ]: 1 : BlockFilter filter;
85 : 1 : uint256 filter_header;
86 : 1 : std::vector<BlockFilter> filters;
87 : 1 : std::vector<uint256> filter_hashes;
88 : :
89 [ + - - + ]: 1 : for (const CBlockIndex* block_index = m_node.chainman->ActiveChain().Genesis();
90 [ + + ]: 102 : block_index != nullptr;
91 [ + - ]: 101 : block_index = m_node.chainman->ActiveChain().Next(*block_index)) {
92 [ + - + - : 202 : BOOST_CHECK(!filter_index.LookupFilter(block_index, filter));
+ - + - ]
93 [ + - + - : 202 : BOOST_CHECK(!filter_index.LookupFilterHeader(block_index, filter_header));
+ - + - ]
94 [ + - + - : 202 : BOOST_CHECK(!filter_index.LookupFilterRange(block_index->nHeight, block_index, filters));
+ - + - ]
95 [ + - + - : 202 : BOOST_CHECK(!filter_index.LookupFilterHashRange(block_index->nHeight, block_index,
+ - + - ]
96 : : filter_hashes));
97 : : }
98 [ + - ]: 1 : }
99 : :
100 : : // BlockUntilSyncedToCurrentChain should return false before index is started.
101 [ + - + - : 2 : BOOST_CHECK(!filter_index.BlockUntilSyncedToCurrentChain());
+ - + - ]
102 : :
103 [ + - ]: 1 : filter_index.Sync();
104 : :
105 : : // Check that filter index has all blocks that were in the chain before it started.
106 : 1 : {
107 [ + - ]: 1 : LOCK(cs_main);
108 : 1 : const CBlockIndex* block_index;
109 [ + - - + ]: 1 : for (block_index = m_node.chainman->ActiveChain().Genesis();
110 [ + + ]: 102 : block_index != nullptr;
111 [ + - ]: 101 : block_index = m_node.chainman->ActiveChain().Next(*block_index)) {
112 [ + - ]: 101 : CheckFilterLookups(filter_index, block_index, last_header, m_node.chainman->m_blockman);
113 : : }
114 : 0 : }
115 : :
116 : : // Create two forks.
117 : 1 : const CBlockIndex* tip;
118 : 1 : {
119 [ + - ]: 1 : LOCK(cs_main);
120 [ + - - + : 2 : tip = m_node.chainman->ActiveChain().Tip();
+ - ]
121 : 0 : }
122 : 1 : CKey coinbase_key_A = GenerateRandomKey();
123 : 1 : CKey coinbase_key_B = GenerateRandomKey();
124 [ + - + - : 1 : CScript coinbase_script_pub_key_A = GetScriptForDestination(PKHash(coinbase_key_A.GetPubKey()));
+ - ]
125 [ + - + - : 1 : CScript coinbase_script_pub_key_B = GetScriptForDestination(PKHash(coinbase_key_B.GetPubKey()));
+ - ]
126 : 1 : std::vector<std::shared_ptr<CBlock>> chainA, chainB;
127 [ + - + - : 2 : BOOST_REQUIRE(BuildChain(m_node, tip, coinbase_script_pub_key_A, 10, chainA));
+ - + - ]
128 [ + - + - : 2 : BOOST_REQUIRE(BuildChain(m_node, tip, coinbase_script_pub_key_B, 10, chainB));
+ - ]
129 : :
130 : : // Check that new blocks on chain A get indexed.
131 : 1 : uint256 chainA_last_header = last_header;
132 [ + + ]: 3 : for (size_t i = 0; i < 2; i++) {
133 [ + - ]: 2 : const auto& block = chainA[i];
134 [ + - - + : 8 : BOOST_REQUIRE(Assert(m_node.chainman)->ProcessNewBlock(block, true, true, nullptr));
+ - + - +
- + - ]
135 : : }
136 [ + + ]: 3 : for (size_t i = 0; i < 2; i++) {
137 [ + - ]: 2 : const auto& block = chainA[i];
138 : 2 : const CBlockIndex* block_index;
139 : 2 : {
140 [ + - ]: 2 : LOCK(cs_main);
141 [ + - + - : 2 : block_index = m_node.chainman->m_blockman.LookupBlockIndex(block->GetHash());
+ - ]
142 : 0 : }
143 : :
144 [ + - + - : 4 : BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
+ - + - ]
145 [ + - ]: 2 : CheckFilterLookups(filter_index, block_index, chainA_last_header, m_node.chainman->m_blockman);
146 : : }
147 : :
148 : : // Reorg to chain B.
149 : 1 : uint256 chainB_last_header = last_header;
150 [ + + ]: 4 : for (size_t i = 0; i < 3; i++) {
151 [ + - ]: 3 : const auto& block = chainB[i];
152 [ + - - + : 12 : BOOST_REQUIRE(Assert(m_node.chainman)->ProcessNewBlock(block, true, true, nullptr));
+ - + - +
- + - ]
153 : : }
154 [ + + ]: 4 : for (size_t i = 0; i < 3; i++) {
155 [ + - ]: 3 : const auto& block = chainB[i];
156 : 3 : const CBlockIndex* block_index;
157 : 3 : {
158 [ + - ]: 3 : LOCK(cs_main);
159 [ + - + - : 3 : block_index = m_node.chainman->m_blockman.LookupBlockIndex(block->GetHash());
+ - ]
160 : 0 : }
161 : :
162 [ + - + - : 6 : BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
+ - + - ]
163 [ + - ]: 3 : CheckFilterLookups(filter_index, block_index, chainB_last_header, m_node.chainman->m_blockman);
164 : : }
165 : :
166 : : // Check that filters for stale blocks on A can be retrieved.
167 : 1 : chainA_last_header = last_header;
168 [ + + ]: 3 : for (size_t i = 0; i < 2; i++) {
169 [ + - ]: 2 : const auto& block = chainA[i];
170 : 2 : const CBlockIndex* block_index;
171 : 2 : {
172 [ + - ]: 2 : LOCK(cs_main);
173 [ + - + - : 2 : block_index = m_node.chainman->m_blockman.LookupBlockIndex(block->GetHash());
+ - ]
174 : 0 : }
175 : :
176 [ + - + - : 4 : BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
+ - + - ]
177 [ + - ]: 2 : CheckFilterLookups(filter_index, block_index, chainA_last_header, m_node.chainman->m_blockman);
178 : : }
179 : :
180 : : // Reorg back to chain A.
181 [ + + ]: 3 : for (size_t i = 2; i < 4; i++) {
182 [ + - ]: 2 : const auto& block = chainA[i];
183 [ + - - + : 8 : BOOST_REQUIRE(Assert(m_node.chainman)->ProcessNewBlock(block, true, true, nullptr));
+ - + - +
- + - ]
184 : : }
185 : :
186 : : // Check that chain A and B blocks can be retrieved.
187 : 1 : chainA_last_header = last_header;
188 : 1 : chainB_last_header = last_header;
189 [ + + ]: 4 : for (size_t i = 0; i < 3; i++) {
190 : 3 : const CBlockIndex* block_index;
191 : :
192 : 3 : {
193 [ + - ]: 3 : LOCK(cs_main);
194 [ + - + - : 3 : block_index = m_node.chainman->m_blockman.LookupBlockIndex(chainA[i]->GetHash());
+ - ]
195 : 0 : }
196 [ + - + - : 6 : BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
+ - + - ]
197 [ + - ]: 3 : CheckFilterLookups(filter_index, block_index, chainA_last_header, m_node.chainman->m_blockman);
198 : :
199 : 3 : {
200 [ + - ]: 3 : LOCK(cs_main);
201 [ + - + - : 3 : block_index = m_node.chainman->m_blockman.LookupBlockIndex(chainB[i]->GetHash());
+ - ]
202 : 0 : }
203 [ + - + - : 6 : BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
+ - + - ]
204 [ + - ]: 3 : CheckFilterLookups(filter_index, block_index, chainB_last_header, m_node.chainman->m_blockman);
205 : : }
206 : :
207 : : // Test lookups for a range of filters/hashes.
208 : 1 : std::vector<BlockFilter> filters;
209 : 1 : std::vector<uint256> filter_hashes;
210 : :
211 : 1 : {
212 [ + - ]: 1 : LOCK(cs_main);
213 [ + - - + : 2 : tip = m_node.chainman->ActiveChain().Tip();
+ - ]
214 : 0 : }
215 [ + - + - : 2 : BOOST_CHECK(filter_index.LookupFilterRange(0, tip, filters));
+ - + - ]
216 [ + - + - : 2 : BOOST_CHECK(filter_index.LookupFilterHashRange(0, tip, filter_hashes));
+ - - + ]
217 : :
218 [ - + ]: 1 : assert(tip->nHeight >= 0);
219 [ + - - + : 1 : BOOST_CHECK_EQUAL(filters.size(), tip->nHeight + 1U);
+ - ]
220 [ + - - + : 1 : BOOST_CHECK_EQUAL(filter_hashes.size(), tip->nHeight + 1U);
+ - ]
221 : :
222 : 1 : filters.clear();
223 [ + - ]: 1 : filter_hashes.clear();
224 : :
225 [ + - ]: 1 : filter_index.Interrupt();
226 [ + - ]: 1 : filter_index.Stop();
227 : 1 : }
228 : :
229 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(blockfilter_index_init_destroy, BasicTestingSetup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
230 : : {
231 : 1 : BlockFilterIndex* filter_index;
232 : :
233 : 1 : filter_index = GetBlockFilterIndex(BlockFilterType::BASIC);
234 [ + - ]: 2 : BOOST_CHECK(filter_index == nullptr);
235 : :
236 [ + - + - ]: 3 : BOOST_CHECK(InitBlockFilterIndex([&]{ return interfaces::MakeChain(m_node); }, BlockFilterType::BASIC, 1_MiB, true, false));
237 : :
238 : 1 : filter_index = GetBlockFilterIndex(BlockFilterType::BASIC);
239 [ + - ]: 2 : BOOST_CHECK(filter_index != nullptr);
240 [ + - ]: 2 : BOOST_CHECK(filter_index->GetFilterType() == BlockFilterType::BASIC);
241 : :
242 : : // Initialize returns false if index already exists.
243 [ + - + - : 3 : BOOST_CHECK(!InitBlockFilterIndex([&]{ return interfaces::MakeChain(m_node); }, BlockFilterType::BASIC, 1_MiB, true, false));
+ - ]
244 : :
245 : 1 : int iter_count = 0;
246 [ + - ]: 2 : ForEachBlockFilterIndex([&iter_count](BlockFilterIndex& _index) { iter_count++; });
247 [ + - ]: 1 : BOOST_CHECK_EQUAL(iter_count, 1);
248 : :
249 [ + - + - ]: 2 : BOOST_CHECK(DestroyBlockFilterIndex(BlockFilterType::BASIC));
250 : :
251 : : // Destroy returns false because index was already destroyed.
252 [ + - + - ]: 2 : BOOST_CHECK(!DestroyBlockFilterIndex(BlockFilterType::BASIC));
253 : :
254 : 1 : filter_index = GetBlockFilterIndex(BlockFilterType::BASIC);
255 [ + - ]: 2 : BOOST_CHECK(filter_index == nullptr);
256 : :
257 : : // Reinitialize index.
258 [ + - + - ]: 3 : BOOST_CHECK(InitBlockFilterIndex([&]{ return interfaces::MakeChain(m_node); }, BlockFilterType::BASIC, 1_MiB, true, false));
259 : :
260 : 1 : DestroyAllBlockFilterIndexes();
261 : :
262 : 1 : filter_index = GetBlockFilterIndex(BlockFilterType::BASIC);
263 [ + - ]: 2 : BOOST_CHECK(filter_index == nullptr);
264 : 1 : }
265 : :
266 : : BOOST_AUTO_TEST_SUITE_END()
|