Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1#!/usr/bin/env python32# Copyright (c) 2024-present The Bitcoin Core developers3# Distributed under the MIT software license, see the accompanying4# file COPYING or http://www.opensource.org/licenses/mit-license.php.5"""Test support for XORed block data and undo files (`-blocksxor` option)."""7from test_framework.test_framework import BitcoinTestFramework8from test_framework.test_node import (9ErrorMatch,10NULL_BLK_XOR_KEY,11)12from test_framework.util import (13assert_equal,14assert_greater_than,15util_xor,16)17from test_framework.wallet import MiniWallet20class BlocksXORTest(BitcoinTestFramework):21def set_test_params(self):22self.num_nodes = 123self.extra_args = [[24'-blocksxor=1',25'-fastprune=1', # use smaller block files26]]28def run_test(self):29self.log.info("Mine some blocks, to create multiple blk*.dat/rev*.dat files")30node = self.nodes[0]31wallet = MiniWallet(node)32for _ in range(5):33wallet.send_self_transfer(from_node=node, target_vsize=20000)34self.generate(wallet, 1)36block_files = list(node.blocks_path.glob('blk[0-9][0-9][0-9][0-9][0-9].dat'))37undo_files = list(node.blocks_path.glob('rev[0-9][0-9][0-9][0-9][0-9].dat'))38assert_equal(len(block_files), len(undo_files))39assert_greater_than(len(block_files), 1) # we want at least one full block file41self.log.info("Shut down node and un-XOR block/undo files manually")42self.stop_node(0)43xor_key = node.read_xor_key()44for data_file in sorted(block_files + undo_files):45self.log.debug(f"Rewriting file {data_file}...")46with open(data_file, 'rb+') as f:47xored_data = f.read()48f.seek(0)49f.write(util_xor(xored_data, xor_key, offset=0))51self.log.info("Check that restarting with 'blocksxor=0' fails if XOR key is present")52node.assert_start_raises_init_error(['-blocksxor=0'],53'The blocksdir XOR-key can not be disabled when a random key was already stored!',54match=ErrorMatch.PARTIAL_REGEX)56self.log.info("Delete XOR key, restart node with '-blocksxor=0', check blk*.dat/rev*.dat file integrity")57node.blocks_key_path.unlink()58self.start_node(0, extra_args=['-blocksxor=0'])59# checklevel=2 -> verify block validity + undo data60# nblocks=0 -> verify all blocks61node.verifychain(checklevel=2, nblocks=0)62self.log.info("Check that blocks XOR key is recreated")63assert_equal(node.read_xor_key(), NULL_BLK_XOR_KEY)66if __name__ == '__main__':67BlocksXORTest(__file__).main()
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.