From 6b3676be3e5b6e407876031791172f441b359295 Mon Sep 17 00:00:00 2001 From: Sebastian Falbesoner Date: Wed, 14 Aug 2024 16:43:46 +0200 Subject: [PATCH] test: refactor: move `read_xor_key`/`util_xor` helpers to util module --- test/functional/feature_reindex.py | 16 ++++++---------- test/functional/test_framework/util.py | 13 +++++++++++++ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/test/functional/feature_reindex.py b/test/functional/feature_reindex.py index 504b3dfff44..2961a2d3564 100755 --- a/test/functional/feature_reindex.py +++ b/test/functional/feature_reindex.py @@ -12,7 +12,11 @@ from test_framework.test_framework import BitcoinTestFramework from test_framework.messages import MAGIC_BYTES -from test_framework.util import assert_equal +from test_framework.util import ( + assert_equal, + read_xor_key, + util_xor, +) class ReindexTest(BitcoinTestFramework): @@ -39,15 +43,7 @@ class ReindexTest(BitcoinTestFramework): # we're generating them rather than getting them from peers), so to # test out-of-order handling, swap blocks 1 and 2 on disk. blk0 = self.nodes[0].blocks_path / "blk00000.dat" - with open(self.nodes[0].blocks_path / "xor.dat", "rb") as xor_f: - NUM_XOR_BYTES = 8 # From InitBlocksdirXorKey::xor_key.size() - xor_dat = xor_f.read(NUM_XOR_BYTES) - - def util_xor(data, key, *, offset): - data = bytearray(data) - for i in range(len(data)): - data[i] ^= key[(i + offset) % len(key)] - return bytes(data) + xor_dat = read_xor_key(node=self.nodes[0]) with open(blk0, 'r+b') as bf: # Read at least the first few blocks (including genesis) diff --git a/test/functional/test_framework/util.py b/test/functional/test_framework/util.py index de756691e05..c3bc861cf6e 100644 --- a/test/functional/test_framework/util.py +++ b/test/functional/test_framework/util.py @@ -311,6 +311,13 @@ def sha256sum_file(filename): return h.digest() +def util_xor(data, key, *, offset): + data = bytearray(data) + for i in range(len(data)): + data[i] ^= key[(i + offset) % len(key)] + return bytes(data) + + # RPC/P2P connection constants and functions ############################################ @@ -508,6 +515,12 @@ def check_node_connections(*, node, num_in, num_out): assert_equal(info["connections_out"], num_out) +def read_xor_key(*, node): + with open(node.blocks_path / "xor.dat", "rb") as xor_f: + NUM_XOR_BYTES = 8 # From InitBlocksdirXorKey::xor_key.size() + return xor_f.read(NUM_XOR_BYTES) + + # Transaction/Block functions #############################