/ test / functional / feature_reindex.py
feature_reindex.py
 1  #!/usr/bin/env python3
 2  # Copyright (c) 2014-2021 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 running bitcoind with -reindex and -reindex-chainstate options.
 6  
 7  - Start a single node and generate 3 blocks.
 8  - Stop the node and restart it with -reindex. Verify that the node has reindexed up to block 3.
 9  - Stop the node and restart it with -reindex-chainstate. Verify that the node has reindexed up to block 3.
10  - Verify that out-of-order blocks are correctly processed, see LoadExternalBlockFile()
11  """
12  
13  from test_framework.test_framework import BitcoinTestFramework
14  from test_framework.messages import MAGIC_BYTES
15  from test_framework.util import assert_equal
16  
17  
18  class ReindexTest(BitcoinTestFramework):
19      def set_test_params(self):
20          self.setup_clean_chain = True
21          self.num_nodes = 1
22  
23      def reindex(self, justchainstate=False):
24          self.generatetoaddress(self.nodes[0], 3, self.nodes[0].get_deterministic_priv_key().address)
25          blockcount = self.nodes[0].getblockcount()
26          self.stop_nodes()
27          extra_args = [["-reindex-chainstate" if justchainstate else "-reindex"]]
28          self.start_nodes(extra_args)
29          assert_equal(self.nodes[0].getblockcount(), blockcount)  # start_node is blocking on reindex
30          self.log.info("Success")
31  
32      # Check that blocks can be processed out of order
33      def out_of_order(self):
34          # The previous test created 12 blocks
35          assert_equal(self.nodes[0].getblockcount(), 12)
36          self.stop_nodes()
37  
38          # In this test environment, blocks will always be in order (since
39          # we're generating them rather than getting them from peers), so to
40          # test out-of-order handling, swap blocks 1 and 2 on disk.
41          blk0 = self.nodes[0].blocks_path / "blk00000.dat"
42          with open(blk0, 'r+b') as bf:
43              # Read at least the first few blocks (including genesis)
44              b = bf.read(2000)
45  
46              # Find the offsets of blocks 2, 3, and 4 (the first 3 blocks beyond genesis)
47              # by searching for the regtest marker bytes (see pchMessageStart).
48              def find_block(b, start):
49                  return b.find(MAGIC_BYTES["regtest"], start)+4
50  
51              genesis_start = find_block(b, 0)
52              assert_equal(genesis_start, 4)
53              b2_start = find_block(b, genesis_start)
54              b3_start = find_block(b, b2_start)
55              b4_start = find_block(b, b3_start)
56  
57              # Blocks 2 and 3 should be the same size.
58              assert_equal(b3_start-b2_start, b4_start-b3_start)
59  
60              # Swap the second and third blocks (don't disturb the genesis block).
61              bf.seek(b2_start)
62              bf.write(b[b3_start:b4_start])
63              bf.write(b[b2_start:b3_start])
64  
65          # The reindexing code should detect and accommodate out of order blocks.
66          with self.nodes[0].assert_debug_log([
67              'LoadExternalBlockFile: Out of order block',
68              'LoadExternalBlockFile: Processing out of order child',
69          ]):
70              extra_args = [["-reindex"]]
71              self.start_nodes(extra_args)
72  
73          # All blocks should be accepted and processed.
74          assert_equal(self.nodes[0].getblockcount(), 12)
75  
76      def run_test(self):
77          self.reindex(False)
78          self.reindex(True)
79          self.reindex(False)
80          self.reindex(True)
81  
82          self.out_of_order()
83  
84  
85  if __name__ == '__main__':
86      ReindexTest().main()