Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
251block1.nVersion = 1;252CBlock block2;253block2.nVersion = 2;254CBlock block3;255block3.nVersion = 3;257// They are 80 bytes header + 1 byte 0x00 for vtx length258constexpr int TEST_BLOCK_SIZE{81};260// Blockstore is empty261LOCK(::cs_main);262BOOST_CHECK_EQUAL(blockman.CalculateCurrentUsage(), 0);264// Write the first block to a new location.265FlatFilePos pos1{blockman.WriteBlock(block1, /*nHeight=*/1)};267// Write second block268FlatFilePos pos2{blockman.WriteBlock(block2, /*nHeight=*/2)};270// Two blocks in the file271BOOST_CHECK_EQUAL(blockman.CalculateCurrentUsage(), (TEST_BLOCK_SIZE + STORAGE_HEADER_BYTES) * 2);273// First two blocks are written as expected274// Errors are expected because block data is junk, thrown AFTER successful read275CBlock read_block;276BOOST_CHECK_EQUAL(read_block.nVersion, 0);277{278ASSERT_DEBUG_LOG("Errors in block header");279BOOST_CHECK(!blockman.ReadBlock(read_block, pos1, {}));280BOOST_CHECK_EQUAL(read_block.nVersion, 1);281}282{283ASSERT_DEBUG_LOG("Errors in block header");284BOOST_CHECK(!blockman.ReadBlock(read_block, pos2, {}));285BOOST_CHECK_EQUAL(read_block.nVersion, 2);286}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 data291// to block 2 location.292CBlockFileInfo* block_data = blockman.GetBlockFileInfo(0);293BOOST_CHECK_EQUAL(block_data->nBlocks, 2);294blockman.UpdateBlockInfo(block3, /*nHeight=*/3, /*pos=*/pos2);295// Metadata is updated...296BOOST_CHECK_EQUAL(block_data->nBlocks, 3);297// ...but there are still only two blocks in the file298BOOST_CHECK_EQUAL(blockman.CalculateCurrentUsage(), (TEST_BLOCK_SIZE + STORAGE_HEADER_BYTES) * 2);300// Block 2 was not overwritten:301BOOST_CHECK(!blockman.ReadBlock(read_block, pos2, {}));302BOOST_CHECK_EQUAL(read_block.nVersion, 2);303}305BOOST_FIXTURE_TEST_CASE(prune_lock_update_and_delete, TestingSetup)306{307LOCK(::cs_main);308auto& chainman{*Assert(m_node.chainman)};309auto& blockman{chainman.m_blockman};311// Create a prune lock312blockman.UpdatePruneLock("test_lock", node::PruneLockInfo{.height_first = 100});314// Update it to a new height315blockman.UpdatePruneLock("test_lock", node::PruneLockInfo{.height_first = 200});317// Delete existing prune lock318BOOST_CHECK(blockman.DeletePruneLock("test_lock"));320// Verify deletion worked by trying to delete the same lock again321BOOST_CHECK(!blockman.DeletePruneLock("test_lock"));323// Deleting a non-existent lock returns false324BOOST_CHECK(!blockman.DeletePruneLock("nonexistent"));325}327BOOST_AUTO_TEST_SUITE_END()
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.