Branch data Line data Source code
1 : : // Copyright (c) 2022-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 <chain.h>
6 : : #include <chainparams.h>
7 : : #include <clientversion.h>
8 : : #include <node/blockstorage.h>
9 : : #include <node/context.h>
10 : : #include <node/kernel_notifications.h>
11 : : #include <script/solver.h>
12 : : #include <primitives/block.h>
13 : : #include <util/chaintype.h>
14 : : #include <validation.h>
15 : :
16 : : #include <boost/test/unit_test.hpp>
17 : : #include <test/util/common.h>
18 : : #include <test/util/logging.h>
19 : : #include <test/util/setup_common.h>
20 : :
21 : : using kernel::CBlockFileInfo;
22 : : using node::STORAGE_HEADER_BYTES;
23 : : using node::BlockManager;
24 : : using node::KernelNotifications;
25 : : using node::MAX_BLOCKFILE_SIZE;
26 : :
27 : : // use BasicTestingSetup here for the data directory configuration, setup, and cleanup
28 : : BOOST_FIXTURE_TEST_SUITE(blockmanager_tests, BasicTestingSetup)
29 : :
30 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(blockmanager_find_block_pos)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
31 : : {
32 [ + - ]: 1 : const auto params {CreateChainParams(ArgsManager{}, ChainType::MAIN)};
33 [ - + - + ]: 1 : KernelNotifications notifications{Assert(m_node.shutdown_request), m_node.exit_status, *Assert(m_node.warnings)};
34 [ + - ]: 1 : const BlockManager::Options blockman_opts{
35 : 1 : .chainparams = *params,
36 : : .blocks_dir = m_args.GetBlocksDirPath(),
37 : : .notifications = notifications,
38 : : .block_tree_db_params = DBParams{
39 [ + - ]: 2 : .path = m_args.GetDataDirNet() / "blocks" / "index",
40 : : .cache_bytes = 0,
41 : : },
42 [ + - + - ]: 2 : };
43 [ - + + - : 1 : BlockManager blockman{*Assert(m_node.shutdown_signal), blockman_opts};
+ - ]
44 : : // simulate adding a genesis block normally
45 [ + - + - : 1 : BOOST_CHECK_EQUAL(blockman.WriteBlock(params->GenesisBlock(), 0).nPos, STORAGE_HEADER_BYTES);
+ - ]
46 : : // simulate what happens during reindex
47 : : // simulate a well-formed genesis block being found at offset 8 in the blk00000.dat file
48 : : // the block is found at offset 8 because there is an 8 byte serialization header
49 : : // consisting of 4 magic bytes + 4 length bytes before each block in a well-formed blk file.
50 [ + - ]: 1 : const FlatFilePos pos{0, STORAGE_HEADER_BYTES};
51 [ + - ]: 1 : blockman.UpdateBlockInfo(params->GenesisBlock(), 0, pos);
52 : : // now simulate what happens after reindex for the first new block processed
53 : : // the actual block contents don't matter, just that it's a block.
54 : : // verify that the write position is at offset 0x12d.
55 : : // this is a check to make sure that https://github.com/bitcoin/bitcoin/issues/21379 does not recur
56 : : // 8 bytes (for serialization header) + 285 (for serialized genesis block) = 293
57 : : // add another 8 bytes for the second block's serialization header and we get 293 + 8 = 301
58 [ + - ]: 1 : FlatFilePos actual{blockman.WriteBlock(params->GenesisBlock(), 1)};
59 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(actual.nPos, STORAGE_HEADER_BYTES + ::GetSerializeSize(TX_WITH_WITNESS(params->GenesisBlock())) + STORAGE_HEADER_BYTES);
60 : 1 : }
61 : :
62 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(blockmanager_scan_unlink_already_pruned_files, TestChain100Setup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
63 : : {
64 : : // Cap last block file size, and mine new block in a new block file.
65 [ - + ]: 1 : auto& chainman{*Assert(m_node.chainman)};
66 : 1 : auto& blockman{chainman.m_blockman};
67 [ + - - + : 4 : const CBlockIndex* old_tip{WITH_LOCK(chainman.GetMutex(), return chainman.ActiveChain().Tip())};
+ - ]
68 [ + - + - ]: 3 : WITH_LOCK(chainman.GetMutex(), blockman.GetBlockFileInfo(old_tip->GetBlockPos().nFile)->nSize = MAX_BLOCKFILE_SIZE);
69 [ + - ]: 2 : CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
70 : :
71 : : // Prune the older block file, but don't unlink it
72 : 1 : int file_number;
73 : 1 : {
74 : 1 : LOCK(chainman.GetMutex());
75 : 1 : file_number = old_tip->GetBlockPos().nFile;
76 [ + - ]: 1 : blockman.PruneOneBlockFile(file_number);
77 : 0 : }
78 : :
79 : 1 : const FlatFilePos pos(file_number, 0);
80 : :
81 : : // Check that the file is not unlinked after ScanAndUnlinkAlreadyPrunedFiles
82 : : // if m_have_pruned is not yet set
83 [ + - ]: 3 : WITH_LOCK(chainman.GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles());
84 [ + - + - ]: 2 : BOOST_CHECK(!blockman.OpenBlockFile(pos, true).IsNull());
85 : :
86 : : // Check that the file is unlinked after ScanAndUnlinkAlreadyPrunedFiles
87 : : // once m_have_pruned is set
88 : 1 : blockman.m_have_pruned = true;
89 [ + - ]: 3 : WITH_LOCK(chainman.GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles());
90 [ + - + - ]: 2 : BOOST_CHECK(blockman.OpenBlockFile(pos, true).IsNull());
91 : :
92 : : // Check that calling with already pruned files doesn't cause an error
93 [ + - ]: 3 : WITH_LOCK(chainman.GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles());
94 : :
95 : : // Check that the new tip file has not been removed
96 [ + - - + : 4 : const CBlockIndex* new_tip{WITH_LOCK(chainman.GetMutex(), return chainman.ActiveChain().Tip())};
+ - ]
97 [ + - ]: 1 : BOOST_CHECK_NE(old_tip, new_tip);
98 [ + - ]: 2 : const int new_file_number{WITH_LOCK(chainman.GetMutex(), return new_tip->GetBlockPos().nFile)};
99 : 1 : const FlatFilePos new_pos(new_file_number, 0);
100 [ + - + - ]: 2 : BOOST_CHECK(!blockman.OpenBlockFile(new_pos, true).IsNull());
101 : 1 : }
102 : :
103 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(blockmanager_block_data_availability, TestChain100Setup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
104 : : {
105 : : // The goal of the function is to return the first not pruned block in the range [upper_block, lower_block].
106 : 1 : LOCK(::cs_main);
107 : 1 : auto& chainman = m_node.chainman;
108 [ + - ]: 1 : auto& blockman = chainman->m_blockman;
109 [ + - ]: 1 : const CBlockIndex& tip = *chainman->ActiveTip();
110 : :
111 : : // Function to prune all blocks from 'last_pruned_block' down to the genesis block
112 : 2 : const auto& func_prune_blocks = [&](CBlockIndex* last_pruned_block)
113 : : {
114 : 1 : LOCK(::cs_main);
115 : 1 : CBlockIndex* it = last_pruned_block;
116 [ + + + - ]: 53 : while (it != nullptr && it->nStatus & BLOCK_HAVE_DATA) {
117 : 51 : it->nStatus &= ~BLOCK_HAVE_DATA;
118 : 51 : it = it->pprev;
119 : : }
120 : 1 : };
121 : :
122 : : // 1) Return genesis block when all blocks are available
123 [ + - + - : 2 : BOOST_CHECK_EQUAL(&blockman.GetFirstBlock(tip, BLOCK_HAVE_DATA), chainman->ActiveChain()[0]);
- + + - +
- ]
124 [ + - + - : 3 : BOOST_CHECK(blockman.CheckBlockDataAvailability(tip, *chainman->ActiveChain()[0]));
- + + - +
- + - ]
125 : :
126 : : // 2) Check lower_block when all blocks are available
127 [ + - + - ]: 1 : CBlockIndex* lower_block = chainman->ActiveChain()[tip.nHeight / 2];
128 [ + - + - : 2 : BOOST_CHECK(blockman.CheckBlockDataAvailability(tip, *lower_block));
+ - + - ]
129 : :
130 : : // Ensure we don't fail due to the expected absence of undo data in the genesis block
131 [ + - - + ]: 1 : CBlockIndex* upper_block = chainman->ActiveChain()[2];
132 [ + - - + ]: 1 : CBlockIndex* genesis = chainman->ActiveChain()[0];
133 [ + - + - : 2 : BOOST_CHECK(blockman.CheckBlockDataAvailability(*upper_block, *genesis, BlockStatus{BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO}));
+ - + - ]
134 : : // Ensure we detect absence of undo data in the first block
135 [ + - - + ]: 1 : chainman->ActiveChain()[1]->nStatus &= ~BLOCK_HAVE_UNDO;
136 [ + - + - : 2 : BOOST_CHECK(!blockman.CheckBlockDataAvailability(tip, *genesis, BlockStatus{BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO}));
+ - + - ]
137 : :
138 : : // Prune half of the blocks
139 : 1 : int height_to_prune = tip.nHeight / 2;
140 [ + - + - ]: 1 : CBlockIndex* first_available_block = chainman->ActiveChain()[height_to_prune + 1];
141 : 1 : CBlockIndex* last_pruned_block = first_available_block->pprev;
142 [ + - ]: 1 : func_prune_blocks(last_pruned_block);
143 : :
144 : : // 3) The last block not pruned is in-between upper-block and the genesis block
145 [ + - + - : 1 : BOOST_CHECK_EQUAL(&blockman.GetFirstBlock(tip, BLOCK_HAVE_DATA), first_available_block);
+ - ]
146 [ + - + - : 2 : BOOST_CHECK(blockman.CheckBlockDataAvailability(tip, *first_available_block));
+ - + - ]
147 [ + - + - : 2 : BOOST_CHECK(!blockman.CheckBlockDataAvailability(tip, *last_pruned_block));
+ - + - ]
148 : :
149 : : // Simulate that the first available block is missing undo data and
150 : : // detect this by using a status mask.
151 : 1 : first_available_block->nStatus &= ~BLOCK_HAVE_UNDO;
152 [ + - + - : 2 : BOOST_CHECK(!blockman.CheckBlockDataAvailability(tip, *first_available_block, BlockStatus{BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO}));
+ - + - ]
153 [ + - + - : 2 : BOOST_CHECK(blockman.CheckBlockDataAvailability(tip, *first_available_block, BlockStatus{BLOCK_HAVE_DATA}));
+ - + - ]
154 : 1 : }
155 : :
156 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(blockmanager_block_data_part, TestChain100Setup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
157 : : {
158 : 1 : LOCK(::cs_main);
159 : 1 : auto& chainman{m_node.chainman};
160 [ + - ]: 1 : auto& blockman{chainman->m_blockman};
161 [ + - ]: 1 : const CBlockIndex& tip{*chainman->ActiveTip()};
162 : 1 : const FlatFilePos tip_block_pos{tip.GetBlockPos()};
163 : :
164 [ + - ]: 1 : auto block{blockman.ReadRawBlock(tip_block_pos)};
165 [ + - + - : 2 : BOOST_REQUIRE(block);
+ - ]
166 [ + - - + : 1 : BOOST_REQUIRE_GE(block->size(), 200);
+ - ]
167 : :
168 : 8 : const auto expect_part{[&](size_t offset, size_t size) {
169 : 7 : auto res{blockman.ReadRawBlock(tip_block_pos, std::pair{offset, size})};
170 [ + - + - : 14 : BOOST_CHECK(res);
+ - ]
171 [ + - ]: 7 : const auto& part{res.value()};
172 [ + - + - : 14 : BOOST_CHECK_EQUAL_COLLECTIONS(part.begin(), part.end(), block->begin() + offset, block->begin() + offset + size);
+ - ]
173 : 8 : }};
174 : :
175 [ + - ]: 1 : expect_part(0, 20);
176 [ - + + - ]: 1 : expect_part(0, block->size() - 1);
177 [ - + + - ]: 1 : expect_part(0, block->size() - 10);
178 [ - + + - ]: 1 : expect_part(0, block->size());
179 [ - + + - ]: 1 : expect_part(1, block->size() - 1);
180 [ + - ]: 1 : expect_part(10, 20);
181 [ - + + - ]: 1 : expect_part(block->size() - 1, 1);
182 [ + - ]: 2 : }
183 : :
184 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(blockmanager_block_data_part_error, TestChain100Setup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
185 : : {
186 : 1 : LOCK(::cs_main);
187 : 1 : auto& chainman{m_node.chainman};
188 [ + - ]: 1 : auto& blockman{chainman->m_blockman};
189 [ + - ]: 1 : const CBlockIndex& tip{*chainman->ActiveTip()};
190 : 1 : const FlatFilePos tip_block_pos{tip.GetBlockPos()};
191 : :
192 [ + - ]: 1 : auto block{blockman.ReadRawBlock(tip_block_pos)};
193 [ + - + - : 2 : BOOST_REQUIRE(block);
+ - ]
194 [ + - - + : 1 : BOOST_REQUIRE_GE(block->size(), 200);
+ - ]
195 : :
196 : 15 : const auto expect_part_error{[&](size_t offset, size_t size) {
197 : 14 : auto res{blockman.ReadRawBlock(tip_block_pos, std::pair{offset, size})};
198 [ + - + - : 28 : BOOST_CHECK(!res);
+ - ]
199 [ + - + - ]: 14 : BOOST_CHECK_EQUAL(res.error(), node::ReadRawError::BadPartRange);
200 : 15 : }};
201 : :
202 [ + - ]: 1 : expect_part_error(0, 0);
203 [ - + + - ]: 1 : expect_part_error(0, block->size() + 1);
204 [ + - ]: 1 : expect_part_error(0, std::numeric_limits<size_t>::max());
205 [ - + + - ]: 1 : expect_part_error(1, block->size());
206 [ - + + - ]: 1 : expect_part_error(2, block->size() - 1);
207 [ - + + - ]: 1 : expect_part_error(block->size() - 1, 2);
208 [ - + + - ]: 1 : expect_part_error(block->size() - 2, 3);
209 [ - + + - ]: 1 : expect_part_error(block->size() + 1, 0);
210 [ - + + - ]: 1 : expect_part_error(block->size() + 1, 1);
211 [ - + + - ]: 1 : expect_part_error(block->size() + 2, 2);
212 [ - + + - ]: 1 : expect_part_error(block->size(), 0);
213 [ - + + - ]: 1 : expect_part_error(block->size(), 1);
214 [ + - ]: 1 : expect_part_error(std::numeric_limits<size_t>::max(), 1);
215 [ + - ]: 1 : expect_part_error(std::numeric_limits<size_t>::max(), std::numeric_limits<size_t>::max());
216 [ + - ]: 2 : }
217 : :
218 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(blockmanager_readblock_hash_mismatch, TestingSetup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
219 : : {
220 : 1 : CBlockIndex index;
221 : 1 : {
222 : 1 : LOCK(cs_main);
223 [ + - ]: 1 : const auto tip{m_node.chainman->ActiveTip()};
224 : 1 : index.nStatus = tip->nStatus;
225 : 1 : index.nDataPos = tip->nDataPos;
226 [ + - ]: 1 : index.phashBlock = &uint256::ONE; // mismatched block hash
227 : 0 : }
228 : :
229 [ + - + - ]: 2 : ASSERT_DEBUG_LOG("GetHash() doesn't match index");
230 : 1 : CBlock block;
231 [ + - + - : 2 : BOOST_CHECK(!m_node.chainman->m_blockman.ReadBlock(block, index));
+ - ]
232 : 1 : }
233 : :
234 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(blockmanager_flush_block_file)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
235 : : {
236 [ - + - + ]: 1 : KernelNotifications notifications{Assert(m_node.shutdown_request), m_node.exit_status, *Assert(m_node.warnings)};
237 : 1 : node::BlockManager::Options blockman_opts{
238 [ + - ]: 1 : .chainparams = Params(),
239 : : .blocks_dir = m_args.GetBlocksDirPath(),
240 : : .notifications = notifications,
241 : : .block_tree_db_params = DBParams{
242 [ + - ]: 2 : .path = m_args.GetDataDirNet() / "blocks" / "index",
243 : : .cache_bytes = 0,
244 : : },
245 [ + - + - ]: 2 : };
246 [ - + + - : 1 : BlockManager blockman{*Assert(m_node.shutdown_signal), blockman_opts};
+ - ]
247 : :
248 : : // Test blocks with no transactions, not even a coinbase
249 : 1 : CBlock block1;
250 : 1 : block1.nVersion = 1;
251 : 1 : CBlock block2;
252 : 1 : block2.nVersion = 2;
253 : 1 : CBlock block3;
254 : 1 : block3.nVersion = 3;
255 : :
256 : : // They are 80 bytes header + 1 byte 0x00 for vtx length
257 : 1 : constexpr int TEST_BLOCK_SIZE{81};
258 : :
259 : : // Blockstore is empty
260 [ + - + - : 1 : BOOST_CHECK_EQUAL(blockman.CalculateCurrentUsage(), 0);
+ - ]
261 : :
262 : : // Write the first block to a new location.
263 [ + - ]: 1 : FlatFilePos pos1{blockman.WriteBlock(block1, /*nHeight=*/1)};
264 : :
265 : : // Write second block
266 [ + - ]: 1 : FlatFilePos pos2{blockman.WriteBlock(block2, /*nHeight=*/2)};
267 : :
268 : : // Two blocks in the file
269 [ + - + - : 1 : BOOST_CHECK_EQUAL(blockman.CalculateCurrentUsage(), (TEST_BLOCK_SIZE + STORAGE_HEADER_BYTES) * 2);
+ - ]
270 : :
271 : : // First two blocks are written as expected
272 : : // Errors are expected because block data is junk, thrown AFTER successful read
273 : 1 : CBlock read_block;
274 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(read_block.nVersion, 0);
275 : 1 : {
276 [ + - + - ]: 2 : ASSERT_DEBUG_LOG("Errors in block header");
277 [ + - + - : 2 : BOOST_CHECK(!blockman.ReadBlock(read_block, pos1, {}));
+ - + - ]
278 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(read_block.nVersion, 1);
279 : 1 : }
280 : 1 : {
281 [ + - + - ]: 2 : ASSERT_DEBUG_LOG("Errors in block header");
282 [ + - + - : 2 : BOOST_CHECK(!blockman.ReadBlock(read_block, pos2, {}));
+ - + - ]
283 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(read_block.nVersion, 2);
284 : 1 : }
285 : :
286 : : // During reindex, the flat file block storage will not be written to.
287 : : // UpdateBlockInfo will, however, update the blockfile metadata.
288 : : // Verify this behavior by attempting (and failing) to write block 3 data
289 : : // to block 2 location.
290 [ + - ]: 1 : CBlockFileInfo* block_data = blockman.GetBlockFileInfo(0);
291 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(block_data->nBlocks, 2);
292 [ + - ]: 1 : blockman.UpdateBlockInfo(block3, /*nHeight=*/3, /*pos=*/pos2);
293 : : // Metadata is updated...
294 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(block_data->nBlocks, 3);
295 : : // ...but there are still only two blocks in the file
296 [ + - + - : 1 : BOOST_CHECK_EQUAL(blockman.CalculateCurrentUsage(), (TEST_BLOCK_SIZE + STORAGE_HEADER_BYTES) * 2);
+ - ]
297 : :
298 : : // Block 2 was not overwritten:
299 [ + - + - : 2 : BOOST_CHECK(!blockman.ReadBlock(read_block, pos2, {}));
+ - + - ]
300 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(read_block.nVersion, 2);
301 : 1 : }
302 : :
303 : : BOOST_AUTO_TEST_SUITE_END()
|