Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
blockmanager_tests.cpp
Go to the documentation of this file.
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>
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>
20
26
27// use BasicTestingSetup here for the data directory configuration, setup, and cleanup
29
31{
32 const auto params {CreateChainParams(ArgsManager{}, ChainType::MAIN)};
33 KernelNotifications notifications{Assert(m_node.shutdown_request), m_node.exit_status, *Assert(m_node.warnings)};
35 .chainparams = *params,
36 .blocks_dir = m_args.GetBlocksDirPath(),
37 .notifications = notifications,
38 .block_tree_db_params = DBParams{
39 .path = m_args.GetDataDirNet() / "blocks" / "index",
40 .cache_bytes = 0,
41 },
42 };
44 // simulate adding a genesis block normally
46 BOOST_CHECK_EQUAL(blockman.WriteBlock(params->GenesisBlock(), 0).nPos, STORAGE_HEADER_BYTES);
47 // simulate what happens during reindex
48 // simulate a well-formed genesis block being found at offset 8 in the blk00000.dat file
49 // the block is found at offset 8 because there is an 8 byte serialization header
50 // consisting of 4 magic bytes + 4 length bytes before each block in a well-formed blk file.
51 const FlatFilePos pos{0, STORAGE_HEADER_BYTES};
52 blockman.UpdateBlockInfo(params->GenesisBlock(), 0, pos);
53 // now simulate what happens after reindex for the first new block processed
54 // the actual block contents don't matter, just that it's a block.
55 // verify that the write position is at offset 0x12d.
56 // this is a check to make sure that https://github.com/bitcoin/bitcoin/issues/21379 does not recur
57 // 8 bytes (for serialization header) + 285 (for serialized genesis block) = 293
58 // add another 8 bytes for the second block's serialization header and we get 293 + 8 = 301
59 FlatFilePos actual{blockman.WriteBlock(params->GenesisBlock(), 1)};
60 BOOST_CHECK_EQUAL(actual.nPos, STORAGE_HEADER_BYTES + ::GetSerializeSize(TX_WITH_WITNESS(params->GenesisBlock())) + STORAGE_HEADER_BYTES);
61}
62
64{
65 // Cap last block file size, and mine new block in a new block file.
66 auto& chainman{*Assert(m_node.chainman)};
67 auto& blockman{chainman.m_blockman};
68 const CBlockIndex* old_tip{WITH_LOCK(chainman.GetMutex(), return chainman.ActiveChain().Tip())};
69 WITH_LOCK(chainman.GetMutex(), blockman.GetBlockFileInfo(old_tip->GetBlockPos().nFile)->nSize = MAX_BLOCKFILE_SIZE);
70 CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
71
72 // Prune the older block file, but don't unlink it
73 int file_number;
74 {
75 LOCK(chainman.GetMutex());
76 file_number = old_tip->GetBlockPos().nFile;
77 blockman.PruneOneBlockFile(file_number);
78 }
79
80 const FlatFilePos pos(file_number, 0);
81
82 // Check that the file is not unlinked after ScanAndUnlinkAlreadyPrunedFiles
83 // if m_have_pruned is not yet set
84 WITH_LOCK(chainman.GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles());
85 BOOST_CHECK(!blockman.OpenBlockFile(pos, true).IsNull());
86
87 // Check that the file is unlinked after ScanAndUnlinkAlreadyPrunedFiles
88 // once m_have_pruned is set
89 blockman.m_have_pruned = true;
90 WITH_LOCK(chainman.GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles());
91 BOOST_CHECK(blockman.OpenBlockFile(pos, true).IsNull());
92
93 // Check that calling with already pruned files doesn't cause an error
94 WITH_LOCK(chainman.GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles());
95
96 // Check that the new tip file has not been removed
97 const CBlockIndex* new_tip{WITH_LOCK(chainman.GetMutex(), return chainman.ActiveChain().Tip())};
99 const int new_file_number{WITH_LOCK(chainman.GetMutex(), return new_tip->GetBlockPos().nFile)};
101 BOOST_CHECK(!blockman.OpenBlockFile(new_pos, true).IsNull());
102}
103
105{
106 // The goal of the function is to return the first not pruned block in the range [upper_block, lower_block].
108 auto& chainman = m_node.chainman;
109 auto& blockman = chainman->m_blockman;
110 const CBlockIndex& tip = *chainman->ActiveTip();
111
112 // Function to prune all blocks from 'last_pruned_block' down to the genesis block
114 {
117 while (it != nullptr && it->nStatus & BLOCK_HAVE_DATA) {
118 it->nStatus &= ~BLOCK_HAVE_DATA;
119 it = it->pprev;
120 }
121 };
122
123 // 1) Return genesis block when all blocks are available
124 BOOST_CHECK_EQUAL(&blockman.GetFirstBlock(tip, BLOCK_HAVE_DATA), chainman->ActiveChain()[0]);
125 BOOST_CHECK(blockman.CheckBlockDataAvailability(tip, *chainman->ActiveChain()[0]));
126
127 // 2) Check lower_block when all blocks are available
128 CBlockIndex* lower_block = chainman->ActiveChain()[tip.nHeight / 2];
129 BOOST_CHECK(blockman.CheckBlockDataAvailability(tip, *lower_block));
130
131 // Ensure we don't fail due to the expected absence of undo data in the genesis block
132 CBlockIndex* upper_block = chainman->ActiveChain()[2];
133 CBlockIndex* genesis = chainman->ActiveChain()[0];
134 BOOST_CHECK(blockman.CheckBlockDataAvailability(*upper_block, *genesis, BlockStatus{BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO}));
135 // Ensure we detect absence of undo data in the first block
136 chainman->ActiveChain()[1]->nStatus &= ~BLOCK_HAVE_UNDO;
137 BOOST_CHECK(!blockman.CheckBlockDataAvailability(tip, *genesis, BlockStatus{BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO}));
138
139 // Prune half of the blocks
140 int height_to_prune = tip.nHeight / 2;
141 CBlockIndex* first_available_block = chainman->ActiveChain()[height_to_prune + 1];
144
145 // 3) The last block not pruned is in-between upper-block and the genesis block
147 BOOST_CHECK(blockman.CheckBlockDataAvailability(tip, *first_available_block));
148 BOOST_CHECK(!blockman.CheckBlockDataAvailability(tip, *last_pruned_block));
149
150 // Simulate that the first available block is missing undo data and
151 // detect this by using a status mask.
153 BOOST_CHECK(!blockman.CheckBlockDataAvailability(tip, *first_available_block, BlockStatus{BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO}));
154 BOOST_CHECK(blockman.CheckBlockDataAvailability(tip, *first_available_block, BlockStatus{BLOCK_HAVE_DATA}));
155}
156
158{
160 auto& chainman{m_node.chainman};
161 auto& blockman{chainman->m_blockman};
162 const CBlockIndex& tip{*chainman->ActiveTip()};
163 const FlatFilePos tip_block_pos{tip.GetBlockPos()};
164
165 auto block{blockman.ReadRawBlock(tip_block_pos)};
166 BOOST_REQUIRE(block);
167 BOOST_REQUIRE_GE(block->size(), 200);
168
169 const auto expect_part{[&](size_t offset, size_t size) {
170 auto res{blockman.ReadRawBlock(tip_block_pos, std::pair{offset, size})};
172 const auto& part{res.value()};
173 BOOST_CHECK_EQUAL_COLLECTIONS(part.begin(), part.end(), block->begin() + offset, block->begin() + offset + size);
174 }};
175
176 expect_part(0, 20);
177 expect_part(0, block->size() - 1);
178 expect_part(0, block->size() - 10);
179 expect_part(0, block->size());
180 expect_part(1, block->size() - 1);
181 expect_part(10, 20);
182 expect_part(block->size() - 1, 1);
183}
184
186{
188 auto& chainman{m_node.chainman};
189 auto& blockman{chainman->m_blockman};
190 const CBlockIndex& tip{*chainman->ActiveTip()};
191 const FlatFilePos tip_block_pos{tip.GetBlockPos()};
192
193 auto block{blockman.ReadRawBlock(tip_block_pos)};
194 BOOST_REQUIRE(block);
195 BOOST_REQUIRE_GE(block->size(), 200);
196
197 const auto expect_part_error{[&](size_t offset, size_t size) {
198 auto res{blockman.ReadRawBlock(tip_block_pos, std::pair{offset, size})};
201 }};
202
203 expect_part_error(0, 0);
204 expect_part_error(0, block->size() + 1);
205 expect_part_error(0, std::numeric_limits<size_t>::max());
206 expect_part_error(1, block->size());
207 expect_part_error(2, block->size() - 1);
208 expect_part_error(block->size() - 1, 2);
209 expect_part_error(block->size() - 2, 3);
210 expect_part_error(block->size() + 1, 0);
211 expect_part_error(block->size() + 1, 1);
212 expect_part_error(block->size() + 2, 2);
213 expect_part_error(block->size(), 0);
214 expect_part_error(block->size(), 1);
215 expect_part_error(std::numeric_limits<size_t>::max(), 1);
216 expect_part_error(std::numeric_limits<size_t>::max(), std::numeric_limits<size_t>::max());
217}
218
220{
221 CBlockIndex index;
222 {
223 LOCK(cs_main);
224 const auto tip{m_node.chainman->ActiveTip()};
225 index.nStatus = tip->nStatus;
226 index.nDataPos = tip->nDataPos;
227 index.phashBlock = &uint256::ONE; // mismatched block hash
228 }
229
230 ASSERT_DEBUG_LOG("GetHash() doesn't match index");
231 CBlock block;
232 BOOST_CHECK(!m_node.chainman->m_blockman.ReadBlock(block, index));
233}
234
236{
237 KernelNotifications notifications{Assert(m_node.shutdown_request), m_node.exit_status, *Assert(m_node.warnings)};
239 .chainparams = Params(),
240 .blocks_dir = m_args.GetBlocksDirPath(),
241 .notifications = notifications,
242 .block_tree_db_params = DBParams{
243 .path = m_args.GetDataDirNet() / "blocks" / "index",
244 .cache_bytes = 0,
245 },
246 };
248
249 // Test blocks with no transactions, not even a coinbase
251 block1.nVersion = 1;
253 block2.nVersion = 2;
255 block3.nVersion = 3;
256
257 // They are 80 bytes header + 1 byte 0x00 for vtx length
258 constexpr int TEST_BLOCK_SIZE{81};
259
260 // Blockstore is empty
262 BOOST_CHECK_EQUAL(blockman.CalculateCurrentUsage(), 0);
263
264 // Write the first block to a new location.
265 FlatFilePos pos1{blockman.WriteBlock(block1, /*nHeight=*/1)};
266
267 // Write second block
268 FlatFilePos pos2{blockman.WriteBlock(block2, /*nHeight=*/2)};
269
270 // Two blocks in the file
271 BOOST_CHECK_EQUAL(blockman.CalculateCurrentUsage(), (TEST_BLOCK_SIZE + STORAGE_HEADER_BYTES) * 2);
272
273 // First two blocks are written as expected
274 // Errors are expected because block data is junk, thrown AFTER successful read
276 BOOST_CHECK_EQUAL(read_block.nVersion, 0);
277 {
278 ASSERT_DEBUG_LOG("Errors in block header");
279 BOOST_CHECK(!blockman.ReadBlock(read_block, pos1, {}));
280 BOOST_CHECK_EQUAL(read_block.nVersion, 1);
281 }
282 {
283 ASSERT_DEBUG_LOG("Errors in block header");
284 BOOST_CHECK(!blockman.ReadBlock(read_block, pos2, {}));
285 BOOST_CHECK_EQUAL(read_block.nVersion, 2);
286 }
287
288 // During reindex, the flat file block storage will not be written to.
289 // UpdateBlockInfo will, however, update the blockfile metadata.
290 // Verify this behavior by attempting (and failing) to write block 3 data
291 // to block 2 location.
292 CBlockFileInfo* block_data = blockman.GetBlockFileInfo(0);
293 BOOST_CHECK_EQUAL(block_data->nBlocks, 2);
294 blockman.UpdateBlockInfo(block3, /*nHeight=*/3, /*pos=*/pos2);
295 // Metadata is updated...
296 BOOST_CHECK_EQUAL(block_data->nBlocks, 3);
297 // ...but there are still only two blocks in the file
298 BOOST_CHECK_EQUAL(blockman.CalculateCurrentUsage(), (TEST_BLOCK_SIZE + STORAGE_HEADER_BYTES) * 2);
299
300 // Block 2 was not overwritten:
301 BOOST_CHECK(!blockman.ReadBlock(read_block, pos2, {}));
302 BOOST_CHECK_EQUAL(read_block.nVersion, 2);
303}
304
306{
308 auto& chainman{*Assert(m_node.chainman)};
309 auto& blockman{chainman.m_blockman};
310
311 // Create a prune lock
312 blockman.UpdatePruneLock("test_lock", node::PruneLockInfo{.height_first = 100});
313
314 // Update it to a new height
315 blockman.UpdatePruneLock("test_lock", node::PruneLockInfo{.height_first = 200});
316
317 // Delete existing prune lock
318 BOOST_CHECK(blockman.DeletePruneLock("test_lock"));
319
320 // Verify deletion worked by trying to delete the same lock again
321 BOOST_CHECK(!blockman.DeletePruneLock("test_lock"));
322
323 // Deleting a non-existent lock returns false
324 BOOST_CHECK(!blockman.DeletePruneLock("nonexistent"));
325}
326
node::NodeContext m_node
BOOST_AUTO_TEST_CASE(blockmanager_find_block_pos)
BOOST_FIXTURE_TEST_CASE(blockmanager_scan_unlink_already_pruned_files, TestChain100Setup)
BlockStatus
Definition chain.h:42
@ BLOCK_HAVE_DATA
full block available in blk*.dat
Definition chain.h:75
std::unique_ptr< const CChainParams > CreateChainParams(const ArgsManager &args, const ChainType chain)
Creates and returns a std::unique_ptr<CChainParams> of the chosen chain.
const CChainParams & Params()
Return the currently selected parameters.
#define Assert(val)
Identity function.
Definition check.h:119
int32_t nVersion
Definition block.h:30
Definition block.h:74
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition chain.h:94
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition chain.h:100
const uint256 * phashBlock
pointer to the hash of the block, if any. Memory is owned by this CBlockIndex
Definition chain.h:97
Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-w...
kernel::BlockManagerOpts Options
static const uint256 ONE
Definition uint256.h:205
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition cs_main.cpp:8
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
constexpr uint32_t STORAGE_HEADER_BYTES
Size of header written by WriteBlock before a serialized CBlock (8 bytes)
constexpr unsigned int MAX_BLOCKFILE_SIZE
The maximum size of a blk?????.dat file (since 0.8)
#define BOOST_CHECK_EQUAL(v1, v2)
Definition object.cpp:17
#define BOOST_CHECK(expr)
Definition object.cpp:16
constexpr TransactionSerParams TX_WITH_WITNESS
uint64_t GetSerializeSize(const T &t)
Definition serialize.h:1157
CScript GetScriptForRawPubKey(const CPubKey &pubKey)
Generate a P2PK script for the given pubkey.
Definition solver.cpp:213
Basic testing setup.
Application-specific storage settings.
Definition dbwrapper.h:43
fs::path path
Location in the filesystem where leveldb data will be stored.
Definition dbwrapper.h:45
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Testing setup that configures a complete environment.
An options struct for BlockManager, more ergonomically referred to as BlockManager::Options due to th...
const CChainParams & chainparams
std::unique_ptr< ChainstateManager > chainman
Definition context.h:74
std::unique_ptr< node::Warnings > warnings
Manages all the node warnings.
Definition context.h:92
std::function< bool()> shutdown_request
Function to request a shutdown.
Definition context.h:64
util::SignalInterrupt * shutdown_signal
Interrupt object used to track whether node shutdown was requested.
Definition context.h:66
std::atomic< int > exit_status
Definition context.h:90
int height_first
Height of earliest block that should be kept and not pruned.
#define LOCK(cs)
Definition sync.h:268
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition sync.h:299
#define ASSERT_DEBUG_LOG(message)
Definition logging.h:42
BOOST_CHECK_NE(OneL.ToString(), ArrayToString(ZeroArray, 32))
BOOST_CHECK_EQUAL_COLLECTIONS(R1L.begin(), R1L.end(), R1Array, R1Array+uint256::size())
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82