From b353a398fef5d04e0da45de8310e322aaddd6cef Mon Sep 17 00:00:00 2001 From: Kyle Altendorf Date: Thu, 3 Oct 2024 16:32:02 -0400 Subject: [PATCH] stop asking for zero random bits of data (#18668) --- .../core/data_layer/test_data_layer_util.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/chia/_tests/core/data_layer/test_data_layer_util.py b/chia/_tests/core/data_layer/test_data_layer_util.py index 5d3835fb5eb2..8e43dfec9faf 100644 --- a/chia/_tests/core/data_layer/test_data_layer_util.py +++ b/chia/_tests/core/data_layer/test_data_layer_util.py @@ -159,6 +159,13 @@ def definition(left_hash: bytes32, right_hash: bytes32) -> bytes32: assert definition(left_hash=left_hash, right_hash=right_hash) == reference +def get_random_bytes(length: int, r: Random) -> bytes: + if length == 0: + return b"" + + return r.getrandbits(length * 8).to_bytes(length, "big") + + def test_leaf_hash(seeded_random: Random) -> None: def definition(key: bytes, value: bytes) -> bytes32: return SerializedProgram.to((key, value)).get_tree_hash() @@ -169,12 +176,14 @@ def definition(key: bytes, value: bytes) -> bytes32: length = 0 else: length = seeded_random.randrange(100) - key = seeded_random.getrandbits(length * 8).to_bytes(length, "big") + + key = get_random_bytes(length=length, r=seeded_random) + if cycle in (1, 2): length = 0 else: length = seeded_random.randrange(100) - value = seeded_random.getrandbits(length * 8).to_bytes(length, "big") + value = get_random_bytes(length=length, r=seeded_random) reference = definition(key=key, value=value) data.append((key, value, reference)) @@ -197,7 +206,7 @@ def definition(key: bytes) -> bytes32: length = 0 else: length = seeded_random.randrange(100) - key = seeded_random.getrandbits(length * 8).to_bytes(length, "big") + key = get_random_bytes(length=length, r=seeded_random) reference = definition(key=key) data.append((key, reference))