/ test / functional / feature_remove_pruned_files_on_startup.py
feature_remove_pruned_files_on_startup.py
 1  #!/usr/bin/env python3
 2  # Copyright (c) 2022-present The Bitcoin Core developers
 3  # Distributed under the MIT software license, see the accompanying
 4  # file COPYING or http://www.opensource.org/licenses/mit-license.php.
 5  """Tests around pruning rev and blk files on startup."""
 6  
 7  import platform
 8  from test_framework.test_framework import BitcoinTestFramework
 9  from test_framework.util import assert_equal
10  
11  
12  class FeatureRemovePrunedFilesOnStartupTest(BitcoinTestFramework):
13      def set_test_params(self):
14          self.num_nodes = 1
15          self.extra_args = [["-fastprune", "-prune=1"]]
16  
17      def mine_batches(self, blocks):
18          n = blocks // 250
19          for _ in range(n):
20              self.generate(self.nodes[0], 250)
21          self.generate(self.nodes[0], blocks % 250)
22  
23      def run_test(self):
24          blk0 = self.nodes[0].blocks_path / "blk00000.dat"
25          rev0 = self.nodes[0].blocks_path / "rev00000.dat"
26          blk1 = self.nodes[0].blocks_path / "blk00001.dat"
27          rev1 = self.nodes[0].blocks_path / "rev00001.dat"
28          self.mine_batches(800)
29  
30          self.log.info("Open some files to check that this may delay deletion")
31          fd1 = open(blk0, "rb")
32          fd2 = open(rev1, "rb")
33          self.nodes[0].pruneblockchain(600)
34  
35          # Windows systems will not remove files with an open fd
36          if platform.system() != 'Windows':
37              assert not blk0.exists()
38              assert not rev0.exists()
39              assert not blk1.exists()
40              assert not rev1.exists()
41          else:
42              assert blk0.exists()
43              assert not rev0.exists()
44              assert not blk1.exists()
45              assert rev1.exists()
46  
47          self.log.info("Check that the files are removed on restart once the fds are closed")
48          fd1.close()
49          fd2.close()
50          self.restart_node(0)
51          assert not blk0.exists()
52          assert not rev1.exists()
53  
54          self.log.info("Check that a reindex will wipe all files")
55  
56          def ls_files():
57              ls = [
58                  entry.name
59                  for entry in self.nodes[0].blocks_path.iterdir()
60                  if entry.is_file() and any(map(entry.name.startswith, ["rev", "blk"]))
61              ]
62              return sorted(ls)
63  
64          assert_equal(len(ls_files()), 4)
65          self.restart_node(0, extra_args=self.extra_args[0] + ["-reindex"])
66          assert_equal(self.nodes[0].getblockcount(), 0)
67          self.stop_node(0)  # Stop node to flush the two newly created files
68          assert_equal(ls_files(), ["blk00000.dat", "rev00000.dat"])
69  
70  
71  if __name__ == '__main__':
72      FeatureRemovePrunedFilesOnStartupTest(__file__).main()