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