aboutsummaryrefslogtreecommitdiff
path: root/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 /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 'test')
-rwxr-xr-xtest/functional/feature_remove_pruned_files_on_startup.py54
-rwxr-xr-xtest/functional/test_runner.py1
2 files changed, 55 insertions, 0 deletions
diff --git a/test/functional/feature_remove_pruned_files_on_startup.py b/test/functional/feature_remove_pruned_files_on_startup.py
new file mode 100755
index 0000000000..ca0e5ace9f
--- /dev/null
+++ b/test/functional/feature_remove_pruned_files_on_startup.py
@@ -0,0 +1,54 @@
+#!/usr/bin/env python3
+# 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.
+"""Test removing undeleted pruned blk files on startup."""
+
+import os
+from test_framework.test_framework import BitcoinTestFramework
+
+class FeatureRemovePrunedFilesOnStartupTest(BitcoinTestFramework):
+ def set_test_params(self):
+ self.num_nodes = 1
+ self.extra_args = [["-fastprune", "-prune=1"]]
+
+ def mine_batches(self, blocks):
+ n = blocks // 250
+ for _ in range(n):
+ self.generate(self.nodes[0], 250)
+ self.generate(self.nodes[0], blocks % 250)
+ self.sync_blocks()
+
+ def run_test(self):
+ blk0 = os.path.join(self.nodes[0].datadir, self.nodes[0].chain, 'blocks', 'blk00000.dat')
+ rev0 = os.path.join(self.nodes[0].datadir, self.nodes[0].chain, 'blocks', 'rev00000.dat')
+ blk1 = os.path.join(self.nodes[0].datadir, self.nodes[0].chain, 'blocks', 'blk00001.dat')
+ rev1 = os.path.join(self.nodes[0].datadir, self.nodes[0].chain, 'blocks', 'rev00001.dat')
+ self.mine_batches(800)
+ fo1 = os.open(blk0, os.O_RDONLY)
+ fo2 = os.open(rev1, os.O_RDONLY)
+ fd1 = os.fdopen(fo1)
+ fd2 = os.fdopen(fo2)
+ self.nodes[0].pruneblockchain(600)
+
+ # Windows systems will not remove files with an open fd
+ if os.name != 'nt':
+ assert not os.path.exists(blk0)
+ assert not os.path.exists(rev0)
+ assert not os.path.exists(blk1)
+ assert not os.path.exists(rev1)
+ else:
+ assert os.path.exists(blk0)
+ assert not os.path.exists(rev0)
+ assert not os.path.exists(blk1)
+ assert os.path.exists(rev1)
+
+ # Check that the files are removed on restart once the fds are closed
+ fd1.close()
+ fd2.close()
+ self.restart_node(0)
+ assert not os.path.exists(blk0)
+ assert not os.path.exists(rev1)
+
+if __name__ == '__main__':
+ FeatureRemovePrunedFilesOnStartupTest().main()
diff --git a/test/functional/test_runner.py b/test/functional/test_runner.py
index 26ebce039b..1d560706c2 100755
--- a/test/functional/test_runner.py
+++ b/test/functional/test_runner.py
@@ -342,6 +342,7 @@ BASE_SCRIPTS = [
'p2p_permissions.py',
'feature_blocksdir.py',
'wallet_startup.py',
+ 'feature_remove_pruned_files_on_startup.py',
'p2p_i2p_ports.py',
'p2p_i2p_sessions.py',
'feature_config_args.py',