aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorAndrew Chow <github@achow101.com>2023-02-28 09:54:06 -0500
committerAndrew Chow <github@achow101.com>2023-02-28 09:54:10 -0500
commitbb136aaf2c3bb862bf126751b0aeae62d695eaf7 (patch)
treece93b06c9982ca43248617187879aa56150a7fc8 /src/test
parent519ec2650e7a4dd2850ff74cba403cf446a44a5a (diff)
parent3141eab9c669488a2e7fef5f60d356ac92294922 (diff)
Merge bitcoin/bitcoin#26533: prune: scan and unlink already pruned block files on startup
3141eab9c669488a2e7fef5f60d356ac92294922 test: add functional test for ScanAndUnlinkAlreadyPrunedFiles (Andrew Toth) e252909e561e47d75cb3a892657662a139f6532c test: add unit test for ScanAndUnlinkAlreadyPrunedFiles (Andrew Toth) 77557dda4a123515d0fa2a545ee21d7c43a66988 prune: scan and unlink already pruned block files on startup (Andrew Toth) Pull request description: There are a few cases where we can mark a block and undo file as pruned in our block index, but not actually remove the files from disk. 1. If we call `FindFilesToPrune` or `FindFilesToPruneManual` and crash before `UnlinkPrunedFiles`. 2. If on Windows there is an open file handle to the file somewhere else when calling `fs::remove` in `UnlinkPrunedFiles` (https://en.cppreference.com/w/cpp/filesystem/remove, https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-deletefilew#remarks). This could be from another process, or if we are calling `ReadBlockFromDisk`/`ReadRawBlockFromDisk` without having a lock on `cs_main` (which has been allowed since https://github.com/bitcoin/bitcoin/commit/ccd8ef65f93ed82a87cee634660bed3ac17d9eb5). This PR mitigates this by scanning all pruned block files on startup after `LoadBlockIndexDB` and unlinking them again. ACKs for top commit: achow101: ACK 3141eab9c669488a2e7fef5f60d356ac92294922 pablomartin4btc: re-ACK with added functional test 3141eab9c669488a2e7fef5f60d356ac92294922. furszy: Code review ACK 3141eab9 theStack: Code-review ACK 3141eab9c669488a2e7fef5f60d356ac92294922 Tree-SHA512: 6c73bc57838ad1b7e5d441af3c4d6bf4c61c4382e2b86485e57fbb74a61240710c0ceeceb8b4834e610ecfa3175c6955c81ea4b2285fee11ca6383f472979d8d
Diffstat (limited to 'src/test')
-rw-r--r--src/test/blockmanager_tests.cpp43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/test/blockmanager_tests.cpp b/src/test/blockmanager_tests.cpp
index dd7c32cc46..d5825d0d7e 100644
--- a/src/test/blockmanager_tests.cpp
+++ b/src/test/blockmanager_tests.cpp
@@ -12,6 +12,8 @@
using node::BlockManager;
using node::BLOCK_SERIALIZATION_HEADER_SIZE;
+using node::MAX_BLOCKFILE_SIZE;
+using node::OpenBlockFile;
// use BasicTestingSetup here for the data directory configuration, setup, and cleanup
BOOST_FIXTURE_TEST_SUITE(blockmanager_tests, BasicTestingSetup)
@@ -39,4 +41,45 @@ BOOST_AUTO_TEST_CASE(blockmanager_find_block_pos)
BOOST_CHECK_EQUAL(actual.nPos, BLOCK_SERIALIZATION_HEADER_SIZE + ::GetSerializeSize(params->GenesisBlock(), CLIENT_VERSION) + BLOCK_SERIALIZATION_HEADER_SIZE);
}
+BOOST_FIXTURE_TEST_CASE(blockmanager_scan_unlink_already_pruned_files, TestChain100Setup)
+{
+ // Cap last block file size, and mine new block in a new block file.
+ const auto& chainman = Assert(m_node.chainman);
+ auto& blockman = chainman->m_blockman;
+ const CBlockIndex* old_tip{WITH_LOCK(chainman->GetMutex(), return chainman->ActiveChain().Tip())};
+ WITH_LOCK(chainman->GetMutex(), blockman.GetBlockFileInfo(old_tip->GetBlockPos().nFile)->nSize = MAX_BLOCKFILE_SIZE);
+ CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
+
+ // Prune the older block file, but don't unlink it
+ int file_number;
+ {
+ LOCK(chainman->GetMutex());
+ file_number = old_tip->GetBlockPos().nFile;
+ blockman.PruneOneBlockFile(file_number);
+ }
+
+ const FlatFilePos pos(file_number, 0);
+
+ // Check that the file is not unlinked after ScanAndUnlinkAlreadyPrunedFiles
+ // if m_have_pruned is not yet set
+ WITH_LOCK(chainman->GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles());
+ BOOST_CHECK(!AutoFile(OpenBlockFile(pos, true)).IsNull());
+
+ // Check that the file is unlinked after ScanAndUnlinkAlreadyPrunedFiles
+ // once m_have_pruned is set
+ blockman.m_have_pruned = true;
+ WITH_LOCK(chainman->GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles());
+ BOOST_CHECK(AutoFile(OpenBlockFile(pos, true)).IsNull());
+
+ // Check that calling with already pruned files doesn't cause an error
+ WITH_LOCK(chainman->GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles());
+
+ // Check that the new tip file has not been removed
+ const CBlockIndex* new_tip{WITH_LOCK(chainman->GetMutex(), return chainman->ActiveChain().Tip())};
+ BOOST_CHECK_NE(old_tip, new_tip);
+ const int new_file_number{WITH_LOCK(chainman->GetMutex(), return new_tip->GetBlockPos().nFile)};
+ const FlatFilePos new_pos(new_file_number, 0);
+ BOOST_CHECK(!AutoFile(OpenBlockFile(new_pos, true)).IsNull());
+}
+
BOOST_AUTO_TEST_SUITE_END()