Skip to content

Commit d50f0ce

Browse files
committed
Merge bitcoin#30669: test: XORed blocks test follow up
e1d5dd7 test: check xor.dat recreated when missing (tdb3) d161096 test: add null block xor key (tdb3) 1ad999b refactor: lift NUM_XOR_BYTES (tdb3) d839958 refactor: move read_xor_key() to TestNode (tdb3) d43948c refactor: use unlink rather than os.remove (tdb3) c8176f7 test: add blocks_key_path (tdb3) Pull request description: Builds on PR bitcoin#30657. Refactors `read_xor_key()` from `util.py` to `test_node.py` (comment bitcoin#30657 (comment)) Adds a check that `xor.dat` is created when missing (comment bitcoin#30657 (comment)) Help states: ``` -blocksxor Whether an XOR-key applies to blocksdir *.dat files. The created XOR-key will be zeros for an existing blocksdir or when `-blocksxor=0` is set, and random for a freshly initialized blocksdir. (default: 1) ``` ACKs for top commit: maflcko: ACK e1d5dd7 achow101: ACK e1d5dd7 theStack: re-ACK e1d5dd7 brunoerg: reACK e1d5dd7 Tree-SHA512: 325912ef646ec88e0a58e1ece263a2b04cbc06497e8fe5fcd603e509e80c6bcf84b09dd52dfac60e23013f07fc2b2f6db851ed0598649c3593f452c4a1424bd9
2 parents 6d54633 + e1d5dd7 commit d50f0ce

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed

test/functional/feature_blocksxor.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test support for XORed block data and undo files (`-blocksxor` option)."""
6-
import os
76

87
from test_framework.test_framework import BitcoinTestFramework
9-
from test_framework.test_node import ErrorMatch
8+
from test_framework.test_node import (
9+
ErrorMatch,
10+
NULL_BLK_XOR_KEY,
11+
)
1012
from test_framework.util import (
1113
assert_equal,
1214
assert_greater_than,
13-
read_xor_key,
1415
util_xor,
1516
)
1617
from test_framework.wallet import MiniWallet
@@ -40,7 +41,7 @@ def run_test(self):
4041

4142
self.log.info("Shut down node and un-XOR block/undo files manually")
4243
self.stop_node(0)
43-
xor_key = read_xor_key(node=node)
44+
xor_key = node.read_xor_key()
4445
for data_file in sorted(block_files + undo_files):
4546
self.log.debug(f"Rewriting file {data_file}...")
4647
with open(data_file, 'rb+') as f:
@@ -54,11 +55,13 @@ def run_test(self):
5455
match=ErrorMatch.PARTIAL_REGEX)
5556

5657
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+
node.blocks_key_path.unlink()
5859
self.start_node(0, extra_args=['-blocksxor=0'])
5960
# checklevel=2 -> verify block validity + undo data
6061
# nblocks=0 -> verify all blocks
6162
node.verifychain(checklevel=2, nblocks=0)
63+
self.log.info("Check that blocks XOR key is recreated")
64+
assert_equal(node.read_xor_key(), NULL_BLK_XOR_KEY)
6265

6366

6467
if __name__ == '__main__':

test/functional/feature_reindex.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from test_framework.messages import MAGIC_BYTES
1515
from test_framework.util import (
1616
assert_equal,
17-
read_xor_key,
1817
util_xor,
1918
)
2019

@@ -43,7 +42,7 @@ def out_of_order(self):
4342
# we're generating them rather than getting them from peers), so to
4443
# test out-of-order handling, swap blocks 1 and 2 on disk.
4544
blk0 = self.nodes[0].blocks_path / "blk00000.dat"
46-
xor_dat = read_xor_key(node=self.nodes[0])
45+
xor_dat = self.nodes[0].read_xor_key()
4746

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

test/functional/test_framework/test_node.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343
)
4444

4545
BITCOIND_PROC_WAIT_TIMEOUT = 60
46+
# The size of the blocks xor key
47+
# from InitBlocksdirXorKey::xor_key.size()
48+
NUM_XOR_BYTES = 8
49+
# The null blocks key (all 0s)
50+
NULL_BLK_XOR_KEY = bytes([0] * NUM_XOR_BYTES)
4651

4752

4853
class FailedToStartError(Exception):
@@ -465,6 +470,14 @@ def debug_log_path(self) -> Path:
465470
def blocks_path(self) -> Path:
466471
return self.chain_path / "blocks"
467472

473+
@property
474+
def blocks_key_path(self) -> Path:
475+
return self.blocks_path / "xor.dat"
476+
477+
def read_xor_key(self) -> bytes:
478+
with open(self.blocks_key_path, "rb") as xor_f:
479+
return xor_f.read(NUM_XOR_BYTES)
480+
468481
@property
469482
def wallets_path(self) -> Path:
470483
return self.chain_path / "wallets"

test/functional/test_framework/util.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -515,12 +515,6 @@ def check_node_connections(*, node, num_in, num_out):
515515
assert_equal(info["connections_out"], num_out)
516516

517517

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-
524518
# Transaction/Block functions
525519
#############################
526520

0 commit comments

Comments
 (0)