feature_blocksxor.py raw

   1  #!/usr/bin/env python3
   2  # Copyright (c) 2024-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  """Test support for XORed block data and undo files (`-blocksxor` option)."""
   6  
   7  from test_framework.test_framework import BitcoinTestFramework
   8  from test_framework.test_node import (
   9      ErrorMatch,
  10      NULL_BLK_XOR_KEY,
  11  )
  12  from test_framework.util import (
  13      assert_equal,
  14      assert_greater_than,
  15      util_xor,
  16  )
  17  from test_framework.wallet import MiniWallet
  18  
  19  
  20  class BlocksXORTest(BitcoinTestFramework):
  21      def set_test_params(self):
  22          self.num_nodes = 1
  23          self.extra_args = [[
  24              '-blocksxor=1',
  25              '-fastprune=1',             # use smaller block files
  26          ]]
  27  
  28      def run_test(self):
  29          self.log.info("Mine some blocks, to create multiple blk*.dat/rev*.dat files")
  30          node = self.nodes[0]
  31          wallet = MiniWallet(node)
  32          for _ in range(5):
  33              wallet.send_self_transfer(from_node=node, target_vsize=20000)
  34              self.generate(wallet, 1)
  35  
  36          block_files = list(node.blocks_path.glob('blk[0-9][0-9][0-9][0-9][0-9].dat'))
  37          undo_files  = list(node.blocks_path.glob('rev[0-9][0-9][0-9][0-9][0-9].dat'))
  38          assert_equal(len(block_files), len(undo_files))
  39          assert_greater_than(len(block_files), 1)  # we want at least one full block file
  40  
  41          self.log.info("Shut down node and un-XOR block/undo files manually")
  42          self.stop_node(0)
  43          xor_key = node.read_xor_key()
  44          for data_file in sorted(block_files + undo_files):
  45              self.log.debug(f"Rewriting file {data_file}...")
  46              with open(data_file, 'rb+') as f:
  47                  xored_data = f.read()
  48                  f.seek(0)
  49                  f.write(util_xor(xored_data, xor_key, offset=0))
  50  
  51          self.log.info("Check that restarting with 'blocksxor=0' fails if XOR key is present")
  52          node.assert_start_raises_init_error(['-blocksxor=0'],
  53              'The blocksdir XOR-key can not be disabled when a random key was already stored!',
  54              match=ErrorMatch.PARTIAL_REGEX)
  55  
  56          self.log.info("Delete XOR key, restart node with '-blocksxor=0', check blk*.dat/rev*.dat file integrity")
  57          node.blocks_key_path.unlink()
  58          self.start_node(0, extra_args=['-blocksxor=0'])
  59          # checklevel=2 -> verify block validity + undo data
  60          # nblocks=0    -> verify all blocks
  61          node.verifychain(checklevel=2, nblocks=0)
  62          self.log.info("Check that blocks XOR key is recreated")
  63          assert_equal(node.read_xor_key(), NULL_BLK_XOR_KEY)
  64  
  65  
  66  if __name__ == '__main__':
  67      BlocksXORTest(__file__).main()
  68