Skip to content

Commit 6b3676b

Browse files
committed
test: refactor: move read_xor_key/util_xor helpers to util module
1 parent 1a41e63 commit 6b3676b

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

test/functional/feature_reindex.py

Lines changed: 6 additions & 10 deletions
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

Lines changed: 13 additions & 0 deletions
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

0 commit comments

Comments
 (0)