Merge bitcoin/bitcoin#30669: test: XORed blocks test follow up

e1d5dd732d test: check xor.dat recreated when missing (tdb3)
d1610962bf test: add null block xor key (tdb3)
1ad999b9da refactor: lift NUM_XOR_BYTES (tdb3)
d8399584dd refactor: move read_xor_key() to TestNode (tdb3)
d43948c3ef refactor: use unlink rather than os.remove (tdb3)
c8176f758b test: add blocks_key_path (tdb3)

Pull request description:

  Builds on PR #30657.

  Refactors `read_xor_key()` from `util.py` to `test_node.py` (comment https://github.com/bitcoin/bitcoin/pull/30657#discussion_r1723358327)

  Adds a check that `xor.dat` is created when missing (comment https://github.com/bitcoin/bitcoin/pull/30657#discussion_r1717724161)

  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 e1d5dd732d
  achow101:
    ACK e1d5dd732d
  theStack:
    re-ACK e1d5dd732d
  brunoerg:
    reACK e1d5dd732d

Tree-SHA512: 325912ef646ec88e0a58e1ece263a2b04cbc06497e8fe5fcd603e509e80c6bcf84b09dd52dfac60e23013f07fc2b2f6db851ed0598649c3593f452c4a1424bd9
This commit is contained in:
Ava Chow 2024-08-26 14:30:07 -04:00
commit d50f0ce248
No known key found for this signature in database
GPG key ID: 17565732E08E5E41
4 changed files with 22 additions and 13 deletions

View file

@ -3,14 +3,15 @@
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test support for XORed block data and undo files (`-blocksxor` option)."""
import os
from test_framework.test_framework import BitcoinTestFramework
from test_framework.test_node import ErrorMatch
from test_framework.test_node import (
ErrorMatch,
NULL_BLK_XOR_KEY,
)
from test_framework.util import (
assert_equal,
assert_greater_than,
read_xor_key,
util_xor,
)
from test_framework.wallet import MiniWallet
@ -40,7 +41,7 @@ class BlocksXORTest(BitcoinTestFramework):
self.log.info("Shut down node and un-XOR block/undo files manually")
self.stop_node(0)
xor_key = read_xor_key(node=node)
xor_key = node.read_xor_key()
for data_file in sorted(block_files + undo_files):
self.log.debug(f"Rewriting file {data_file}...")
with open(data_file, 'rb+') as f:
@ -54,11 +55,13 @@ class BlocksXORTest(BitcoinTestFramework):
match=ErrorMatch.PARTIAL_REGEX)
self.log.info("Delete XOR key, restart node with '-blocksxor=0', check blk*.dat/rev*.dat file integrity")
os.remove(node.blocks_path / 'xor.dat')
node.blocks_key_path.unlink()
self.start_node(0, extra_args=['-blocksxor=0'])
# checklevel=2 -> verify block validity + undo data
# nblocks=0 -> verify all blocks
node.verifychain(checklevel=2, nblocks=0)
self.log.info("Check that blocks XOR key is recreated")
assert_equal(node.read_xor_key(), NULL_BLK_XOR_KEY)
if __name__ == '__main__':

View file

@ -14,7 +14,6 @@ from test_framework.test_framework import BitcoinTestFramework
from test_framework.messages import MAGIC_BYTES
from test_framework.util import (
assert_equal,
read_xor_key,
util_xor,
)
@ -43,7 +42,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"
xor_dat = read_xor_key(node=self.nodes[0])
xor_dat = self.nodes[0].read_xor_key()
with open(blk0, 'r+b') as bf:
# Read at least the first few blocks (including genesis)

View file

@ -43,6 +43,11 @@ from .util import (
)
BITCOIND_PROC_WAIT_TIMEOUT = 60
# The size of the blocks xor key
# from InitBlocksdirXorKey::xor_key.size()
NUM_XOR_BYTES = 8
# The null blocks key (all 0s)
NULL_BLK_XOR_KEY = bytes([0] * NUM_XOR_BYTES)
class FailedToStartError(Exception):
@ -465,6 +470,14 @@ class TestNode():
def blocks_path(self) -> Path:
return self.chain_path / "blocks"
@property
def blocks_key_path(self) -> Path:
return self.blocks_path / "xor.dat"
def read_xor_key(self) -> bytes:
with open(self.blocks_key_path, "rb") as xor_f:
return xor_f.read(NUM_XOR_BYTES)
@property
def wallets_path(self) -> Path:
return self.chain_path / "wallets"

View file

@ -515,12 +515,6 @@ 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
#############################