1
1
#!/usr/bin/env python3
2
- # Copyright (c) 2022 The Bitcoin Core developers
2
+ # Copyright (c) 2022-present The Bitcoin Core developers
3
3
# Distributed under the MIT software license, see the accompanying
4
4
# 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."""
6
6
7
7
import platform
8
- import os
9
8
from test_framework .test_framework import BitcoinTestFramework
9
+ from test_framework .util import assert_equal
10
+
10
11
11
12
class FeatureRemovePrunedFilesOnStartupTest (BitcoinTestFramework ):
12
13
def set_test_params (self ):
@@ -18,38 +19,54 @@ def mine_batches(self, blocks):
18
19
for _ in range (n ):
19
20
self .generate (self .nodes [0 ], 250 )
20
21
self .generate (self .nodes [0 ], blocks % 250 )
21
- self .sync_blocks ()
22
22
23
23
def run_test (self ):
24
24
blk0 = self .nodes [0 ].blocks_path / "blk00000.dat"
25
25
rev0 = self .nodes [0 ].blocks_path / "rev00000.dat"
26
26
blk1 = self .nodes [0 ].blocks_path / "blk00001.dat"
27
27
rev1 = self .nodes [0 ].blocks_path / "rev00001.dat"
28
28
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" )
33
33
self .nodes [0 ].pruneblockchain (600 )
34
34
35
35
# Windows systems will not remove files with an open fd
36
36
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 ()
41
41
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 ()
46
46
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" )
48
48
fd1 .close ()
49
49
fd2 .close ()
50
50
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
+
53
70
54
71
if __name__ == '__main__' :
55
72
FeatureRemovePrunedFilesOnStartupTest (__file__ ).main ()
0 commit comments