forked from bitpay/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reindex, log, test: fixes bitcoin#21379
This fixes a blk file size calculation made during reindex that results in increased blk file malformity. The fix is to avoid double counting the size of the serialization header during reindex. This adds a unit test to reproduce the bug before the fix and to ensure that it does not recur. These changes include a log message change also so as to not be as alarming. This is a common and recoverable data corruption. These messages can now be filtered by the debug log reindex category.
- Loading branch information
1 parent
59ac8ba
commit e8b90d4
Showing
5 changed files
with
70 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright (c) 2022 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include <chainparams.h> | ||
#include <node/blockstorage.h> | ||
#include <node/context.h> | ||
#include <validation.h> | ||
|
||
#include <boost/test/unit_test.hpp> | ||
#include <test/util/setup_common.h> | ||
|
||
using node::BlockManager; | ||
using node::BLOCK_SERIALIZATION_HEADER_SIZE; | ||
|
||
// use BasicTestingSetup here for the data directory configuration, setup, and cleanup | ||
BOOST_FIXTURE_TEST_SUITE(blockmanager_tests, BasicTestingSetup) | ||
|
||
BOOST_AUTO_TEST_CASE(blockmanager_find_block_pos) | ||
{ | ||
const auto params {CreateChainParams(ArgsManager{}, CBaseChainParams::MAIN)}; | ||
BlockManager blockman {}; | ||
CChain chain {}; | ||
// simulate adding a genesis block normally | ||
FlatFilePos pos{blockman.SaveBlockToDisk(params->GenesisBlock(), 0, chain, *params, nullptr)}; | ||
BOOST_CHECK_EQUAL(pos.nPos, BLOCK_SERIALIZATION_HEADER_SIZE); | ||
// simulate what happens during reindex | ||
// simulate a well-formed genesis block being found at offset 8 in the blk00000.dat file | ||
// the block is found at offset 8 because there is an 8 byte serialization header | ||
// consisting of 4 magic bytes + 4 length bytes before each block in a well-formed blk file. | ||
pos = blockman.SaveBlockToDisk(params->GenesisBlock(), 0, chain, *params, &pos); | ||
BOOST_CHECK_EQUAL(pos.nPos, BLOCK_SERIALIZATION_HEADER_SIZE); | ||
// now simulate what happens after reindex for the first new block processed | ||
// the actual block contents don't matter, just that it's a block. | ||
// verify that the write position is at offset 0x12d. | ||
// this is a check to make sure that https://github.com/bitcoin/bitcoin/issues/21379 does not recur | ||
// 8 bytes (for serialization header) + 285 (for serialized genesis block) = 293 | ||
// add another 8 bytes for the second block's serialization header and we get 293 + 8 = 301 | ||
pos = blockman.SaveBlockToDisk(params->GenesisBlock(), 1, chain, *params, nullptr); | ||
BOOST_CHECK_EQUAL(pos.nPos, BLOCK_SERIALIZATION_HEADER_SIZE + ::GetSerializeSize(params->GenesisBlock(), CLIENT_VERSION) + BLOCK_SERIALIZATION_HEADER_SIZE); | ||
} | ||
|
||
BOOST_AUTO_TEST_SUITE_END() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters