|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2022 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 | +"""Test removing undeleted pruned blk files on startup.""" |
| 6 | + |
| 7 | +import os |
| 8 | +from test_framework.test_framework import BitcoinTestFramework |
| 9 | + |
| 10 | +class FeatureRemovePrunedFilesOnStartupTest(BitcoinTestFramework): |
| 11 | + def set_test_params(self): |
| 12 | + self.num_nodes = 1 |
| 13 | + self.extra_args = [["-fastprune", "-prune=1"]] |
| 14 | + |
| 15 | + def mine_batches(self, blocks): |
| 16 | + n = blocks // 250 |
| 17 | + for _ in range(n): |
| 18 | + self.generate(self.nodes[0], 250) |
| 19 | + self.generate(self.nodes[0], blocks % 250) |
| 20 | + self.sync_blocks() |
| 21 | + |
| 22 | + def run_test(self): |
| 23 | + blk0 = os.path.join(self.nodes[0].datadir, self.nodes[0].chain, 'blocks', 'blk00000.dat') |
| 24 | + rev0 = os.path.join(self.nodes[0].datadir, self.nodes[0].chain, 'blocks', 'rev00000.dat') |
| 25 | + blk1 = os.path.join(self.nodes[0].datadir, self.nodes[0].chain, 'blocks', 'blk00001.dat') |
| 26 | + rev1 = os.path.join(self.nodes[0].datadir, self.nodes[0].chain, 'blocks', 'rev00001.dat') |
| 27 | + self.mine_batches(800) |
| 28 | + fo1 = os.open(blk0, os.O_RDONLY) |
| 29 | + fo2 = os.open(rev1, os.O_RDONLY) |
| 30 | + fd1 = os.fdopen(fo1) |
| 31 | + fd2 = os.fdopen(fo2) |
| 32 | + self.nodes[0].pruneblockchain(600) |
| 33 | + |
| 34 | + # Windows systems will not remove files with an open fd |
| 35 | + if os.name != 'nt': |
| 36 | + assert not os.path.exists(blk0) |
| 37 | + assert not os.path.exists(rev0) |
| 38 | + assert not os.path.exists(blk1) |
| 39 | + assert not os.path.exists(rev1) |
| 40 | + else: |
| 41 | + assert os.path.exists(blk0) |
| 42 | + assert not os.path.exists(rev0) |
| 43 | + assert not os.path.exists(blk1) |
| 44 | + assert os.path.exists(rev1) |
| 45 | + |
| 46 | + # Check that the files are removed on restart once the fds are closed |
| 47 | + fd1.close() |
| 48 | + fd2.close() |
| 49 | + self.restart_node(0) |
| 50 | + assert not os.path.exists(blk0) |
| 51 | + assert not os.path.exists(rev1) |
| 52 | + |
| 53 | +if __name__ == '__main__': |
| 54 | + FeatureRemovePrunedFilesOnStartupTest().main() |
0 commit comments