|
| 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 <chainparams.h> |
| 6 | +#include <node/blockstorage.h> |
| 7 | +#include <node/context.h> |
| 8 | +#include <validation.h> |
| 9 | + |
| 10 | +#include <boost/test/unit_test.hpp> |
| 11 | +#include <test/util/setup_common.h> |
| 12 | + |
| 13 | +// use BasicTestingSetup here for the data directory configuration, setup, and cleanup |
| 14 | +BOOST_FIXTURE_TEST_SUITE(blockmanager_tests, BasicTestingSetup) |
| 15 | + |
| 16 | +BOOST_AUTO_TEST_CASE(blockmanager_find_block_pos) |
| 17 | +{ |
| 18 | + const auto params {CreateChainParams(ArgsManager{}, CBaseChainParams::MAIN)}; |
| 19 | + BlockManager blockman {}; |
| 20 | + CChain chain {}; |
| 21 | + // simulate adding a genesis block normally |
| 22 | + BOOST_CHECK_EQUAL(blockman.SaveBlockToDisk(params->GenesisBlock(), 0, chain, *params, nullptr).nPos, BLOCK_SERIALIZATION_HEADER_SIZE); |
| 23 | + // simulate what happens during reindex |
| 24 | + // simulate a well-formed genesis block being found at offset 8 in the blk00000.dat file |
| 25 | + // the block is found at offset 8 because there is an 8 byte serialization header |
| 26 | + // consisting of 4 magic bytes + 4 length bytes before each block in a well-formed blk file. |
| 27 | + FlatFilePos pos{0, BLOCK_SERIALIZATION_HEADER_SIZE}; |
| 28 | + BOOST_CHECK_EQUAL(blockman.SaveBlockToDisk(params->GenesisBlock(), 0, chain, *params, &pos).nPos, BLOCK_SERIALIZATION_HEADER_SIZE); |
| 29 | + // now simulate what happens after reindex for the first new block processed |
| 30 | + // the actual block contents don't matter, just that it's a block. |
| 31 | + // verify that the write position is at offset 0x12d. |
| 32 | + // this is a check to make sure that https://github.com/bitcoin/bitcoin/issues/21379 does not recur |
| 33 | + // 8 bytes (for serialization header) + 285 (for serialized genesis block) = 293 |
| 34 | + // add another 8 bytes for the second block's serialization header and we get 293 + 8 = 301 |
| 35 | + FlatFilePos actual{blockman.SaveBlockToDisk(params->GenesisBlock(), 1, chain, *params, nullptr)}; |
| 36 | + BOOST_CHECK_EQUAL(actual.nPos, BLOCK_SERIALIZATION_HEADER_SIZE + ::GetSerializeSize(params->GenesisBlock(), CLIENT_VERSION) + BLOCK_SERIALIZATION_HEADER_SIZE); |
| 37 | +} |
| 38 | + |
| 39 | +BOOST_AUTO_TEST_SUITE_END() |
0 commit comments