11#!/usr/bin/env python3
2- # Copyright (c) 2022 The Bitcoin Core developers
2+ # Copyright (c) 2022-present The Bitcoin Core developers
33# Distributed under the MIT software license, see the accompanying
44# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5- """Test removing undeleted pruned blk files on startup."""
5+ """Tests around pruning rev and blk files on startup."""
66
77import platform
8- import os
98from test_framework .test_framework import BitcoinTestFramework
9+ from test_framework .util import assert_equal
10+
1011
1112class FeatureRemovePrunedFilesOnStartupTest (BitcoinTestFramework ):
1213 def set_test_params (self ):
@@ -18,38 +19,54 @@ def mine_batches(self, blocks):
1819 for _ in range (n ):
1920 self .generate (self .nodes [0 ], 250 )
2021 self .generate (self .nodes [0 ], blocks % 250 )
21- self .sync_blocks ()
2222
2323 def run_test (self ):
2424 blk0 = self .nodes [0 ].blocks_path / "blk00000.dat"
2525 rev0 = self .nodes [0 ].blocks_path / "rev00000.dat"
2626 blk1 = self .nodes [0 ].blocks_path / "blk00001.dat"
2727 rev1 = self .nodes [0 ].blocks_path / "rev00001.dat"
2828 self .mine_batches (800 )
29- fo1 = os . open ( blk0 , os . O_RDONLY )
30- fo2 = os . open ( rev1 , os . O_RDONLY )
31- fd1 = os . fdopen ( fo1 )
32- fd2 = os . fdopen ( fo2 )
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" )
3333 self .nodes [0 ].pruneblockchain (600 )
3434
3535 # Windows systems will not remove files with an open fd
3636 if platform .system () != 'Windows' :
37- assert not os . path . exists (blk0 )
38- assert not os . path . exists (rev0 )
39- assert not os . path . exists (blk1 )
40- assert not os . path . exists (rev1 )
37+ assert not blk0 . exists ()
38+ assert not rev0 . exists ()
39+ assert not blk1 . exists ()
40+ assert not rev1 . exists ()
4141 else :
42- assert os . path . exists (blk0 )
43- assert not os . path . exists (rev0 )
44- assert not os . path . exists (blk1 )
45- assert os . path . exists (rev1 )
42+ assert blk0 . exists ()
43+ assert not rev0 . exists ()
44+ assert not blk1 . exists ()
45+ assert rev1 . exists ()
4646
47- # Check that the files are removed on restart once the fds are closed
47+ self . log . info ( " Check that the files are removed on restart once the fds are closed" )
4848 fd1 .close ()
4949 fd2 .close ()
5050 self .restart_node (0 )
51- assert not os .path .exists (blk0 )
52- assert not os .path .exists (rev1 )
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+
5370
5471if __name__ == '__main__' :
5572 FeatureRemovePrunedFilesOnStartupTest (__file__ ).main ()
0 commit comments