Skip to content

Commit 6474132

Browse files
committed
Merge bitcoin#30657: test: add functional test for XORed block/undo files (-blocksxor option)
faa1b9b test: add functional test for XORed block/undo files (`-blocksxor`) (Sebastian Falbesoner) 6b3676b test: refactor: move `read_xor_key`/`util_xor` helpers to util module (Sebastian Falbesoner) Pull request description: This PR adds a dedicated functional test for XORed block data/undo file support (bitcoind option `-blocksxor`, see PR bitcoin#28052). In order to verify that the XOR pattern has been applied, the {blk,rev}*.dat files are rewritten un-XORed manually by the test while the node is shut down; the node is then started again with `-blocksxor=0`, and both the data and undo files are verified via the `verifychain` RPC (with checklevel=2). Note that starting bitcoind with `-blocksxor=0` fails if a xor key is present already, which is also tested explicitly. Fixes bitcoin#30599. ACKs for top commit: glozow: ACK faa1b9b maflcko: ACK faa1b9b ismaelsadeeq: Tested ACK faa1b9b Tree-SHA512: e1df106f6b4e3ba67eca108e36d762f1b991673b881934b84cd36946496a09ce9c329c1363c36aa29409137ae4881e2d177e651359686511632ddf2870f7ca8e
2 parents 99ecb9a + faa1b9b commit 6474132

File tree

4 files changed

+85
-10
lines changed

4 files changed

+85
-10
lines changed

test/functional/feature_blocksxor.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2024 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+
import os
7+
8+
from test_framework.test_framework import BitcoinTestFramework
9+
from test_framework.test_node import ErrorMatch
10+
from test_framework.util import (
11+
assert_equal,
12+
assert_greater_than,
13+
read_xor_key,
14+
util_xor,
15+
)
16+
from test_framework.wallet import MiniWallet
17+
18+
19+
class BlocksXORTest(BitcoinTestFramework):
20+
def set_test_params(self):
21+
self.num_nodes = 1
22+
self.extra_args = [[
23+
'-blocksxor=1',
24+
'-fastprune=1', # use smaller block files
25+
'-datacarriersize=100000', # needed to pad transaction with MiniWallet
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_weight=80000)
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 = read_xor_key(node=node)
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+
os.remove(node.blocks_path / 'xor.dat')
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+
63+
64+
if __name__ == '__main__':
65+
BlocksXORTest(__file__).main()

test/functional/feature_reindex.py

+6-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212

1313
from test_framework.test_framework import BitcoinTestFramework
1414
from test_framework.messages import MAGIC_BYTES
15-
from test_framework.util import assert_equal
15+
from test_framework.util import (
16+
assert_equal,
17+
read_xor_key,
18+
util_xor,
19+
)
1620

1721

1822
class ReindexTest(BitcoinTestFramework):
@@ -39,15 +43,7 @@ def out_of_order(self):
3943
# we're generating them rather than getting them from peers), so to
4044
# test out-of-order handling, swap blocks 1 and 2 on disk.
4145
blk0 = self.nodes[0].blocks_path / "blk00000.dat"
42-
with open(self.nodes[0].blocks_path / "xor.dat", "rb") as xor_f:
43-
NUM_XOR_BYTES = 8 # From InitBlocksdirXorKey::xor_key.size()
44-
xor_dat = xor_f.read(NUM_XOR_BYTES)
45-
46-
def util_xor(data, key, *, offset):
47-
data = bytearray(data)
48-
for i in range(len(data)):
49-
data[i] ^= key[(i + offset) % len(key)]
50-
return bytes(data)
46+
xor_dat = read_xor_key(node=self.nodes[0])
5147

5248
with open(blk0, 'r+b') as bf:
5349
# Read at least the first few blocks (including genesis)

test/functional/test_framework/util.py

+13
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,13 @@ def sha256sum_file(filename):
311311
return h.digest()
312312

313313

314+
def util_xor(data, key, *, offset):
315+
data = bytearray(data)
316+
for i in range(len(data)):
317+
data[i] ^= key[(i + offset) % len(key)]
318+
return bytes(data)
319+
320+
314321
# RPC/P2P connection constants and functions
315322
############################################
316323

@@ -508,6 +515,12 @@ def check_node_connections(*, node, num_in, num_out):
508515
assert_equal(info["connections_out"], num_out)
509516

510517

518+
def read_xor_key(*, node):
519+
with open(node.blocks_path / "xor.dat", "rb") as xor_f:
520+
NUM_XOR_BYTES = 8 # From InitBlocksdirXorKey::xor_key.size()
521+
return xor_f.read(NUM_XOR_BYTES)
522+
523+
511524
# Transaction/Block functions
512525
#############################
513526

test/functional/test_runner.py

+1
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@
284284
'mempool_package_limits.py',
285285
'mempool_package_rbf.py',
286286
'feature_versionbits_warning.py',
287+
'feature_blocksxor.py',
287288
'rpc_preciousblock.py',
288289
'wallet_importprunedfunds.py --legacy-wallet',
289290
'wallet_importprunedfunds.py --descriptors',

0 commit comments

Comments
 (0)