2023-01-05 17:04:28 -05:00
|
|
|
#!/usr/bin/env python3
|
2025-01-21 11:26:55 +01:00
|
|
|
# Copyright (c) 2022-present The Bitcoin Core developers
|
2023-01-05 17:04:28 -05:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2025-01-21 12:07:44 +01:00
|
|
|
"""Tests around pruning rev and blk files on startup."""
|
2023-01-05 17:04:28 -05:00
|
|
|
|
2023-12-08 17:30:19 +01:00
|
|
|
import platform
|
2023-01-05 17:04:28 -05:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
2025-01-21 12:07:44 +01:00
|
|
|
from test_framework.util import assert_equal
|
2023-01-05 17:04:28 -05:00
|
|
|
|
2025-01-21 11:26:55 +01:00
|
|
|
|
2023-01-05 17:04:28 -05:00
|
|
|
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)
|
|
|
|
|
|
|
|
def run_test(self):
|
2023-07-12 15:03:38 +02:00
|
|
|
blk0 = self.nodes[0].blocks_path / "blk00000.dat"
|
|
|
|
rev0 = self.nodes[0].blocks_path / "rev00000.dat"
|
|
|
|
blk1 = self.nodes[0].blocks_path / "blk00001.dat"
|
|
|
|
rev1 = self.nodes[0].blocks_path / "rev00001.dat"
|
2023-01-05 17:04:28 -05:00
|
|
|
self.mine_batches(800)
|
2025-01-21 11:26:55 +01:00
|
|
|
|
|
|
|
self.log.info("Open some files to check that this may delay deletion")
|
|
|
|
fd1 = open(blk0, "rb")
|
|
|
|
fd2 = open(rev1, "rb")
|
2023-01-05 17:04:28 -05:00
|
|
|
self.nodes[0].pruneblockchain(600)
|
|
|
|
|
|
|
|
# Windows systems will not remove files with an open fd
|
2023-12-08 17:30:19 +01:00
|
|
|
if platform.system() != 'Windows':
|
2025-01-21 11:26:55 +01:00
|
|
|
assert not blk0.exists()
|
|
|
|
assert not rev0.exists()
|
|
|
|
assert not blk1.exists()
|
|
|
|
assert not rev1.exists()
|
2023-01-05 17:04:28 -05:00
|
|
|
else:
|
2025-01-21 11:26:55 +01:00
|
|
|
assert blk0.exists()
|
|
|
|
assert not rev0.exists()
|
|
|
|
assert not blk1.exists()
|
|
|
|
assert rev1.exists()
|
2023-01-05 17:04:28 -05:00
|
|
|
|
2025-01-21 11:26:55 +01:00
|
|
|
self.log.info("Check that the files are removed on restart once the fds are closed")
|
2023-01-05 17:04:28 -05:00
|
|
|
fd1.close()
|
|
|
|
fd2.close()
|
|
|
|
self.restart_node(0)
|
2025-01-21 11:26:55 +01:00
|
|
|
assert not blk0.exists()
|
|
|
|
assert not rev1.exists()
|
|
|
|
|
2025-01-21 12:07:44 +01:00
|
|
|
self.log.info("Check that a reindex will wipe all files")
|
|
|
|
|
|
|
|
def ls_files():
|
|
|
|
ls = [
|
|
|
|
entry.name
|
|
|
|
for entry in self.nodes[0].blocks_path.iterdir()
|
|
|
|
if entry.is_file() and any(map(entry.name.startswith, ["rev", "blk"]))
|
|
|
|
]
|
|
|
|
return sorted(ls)
|
|
|
|
|
|
|
|
assert_equal(len(ls_files()), 4)
|
|
|
|
self.restart_node(0, extra_args=self.extra_args[0] + ["-reindex"])
|
|
|
|
assert_equal(self.nodes[0].getblockcount(), 0)
|
|
|
|
self.stop_node(0) # Stop node to flush the two newly created files
|
|
|
|
assert_equal(ls_files(), ["blk00000.dat", "rev00000.dat"])
|
|
|
|
|
2023-01-05 17:04:28 -05:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2024-07-16 22:05:14 +01:00
|
|
|
FeatureRemovePrunedFilesOnStartupTest(__file__).main()
|