mirror of
https://github.com/Chia-Network/chia-blockchain.git
synced 2026-08-24 02:24:23 -05:00
use now allowed randbytes and other helpers (#19838)
This commit is contained in:
@@ -22,7 +22,7 @@ from chia.util.files import write_file_async
|
||||
|
||||
|
||||
def generate_random_ip(rand: random.Random) -> str:
|
||||
return str(IPv4Address(rand.getrandbits(32)))
|
||||
return str(IPv4Address(rand.randbytes(4)))
|
||||
|
||||
|
||||
def populate_address_manager(num_new: int = 500000, num_tried: int = 200000) -> AddressManager:
|
||||
|
||||
@@ -25,7 +25,6 @@ from chia_rs.sized_ints import uint8, uint32, uint64, uint128
|
||||
from benchmarks.utils import setup_db
|
||||
from chia._tests.util.benchmarks import (
|
||||
clvm_generator,
|
||||
rand_bytes,
|
||||
rand_class_group_element,
|
||||
rand_g1,
|
||||
rand_g2,
|
||||
@@ -110,7 +109,7 @@ async def run_add_block_benchmark(version: int) -> None:
|
||||
rand_hash() if not has_pool_pk else None,
|
||||
rand_g1(), # plot_public_key
|
||||
uint8(32),
|
||||
rand_bytes(8 * 32),
|
||||
random.randbytes(8 * 32),
|
||||
)
|
||||
|
||||
reward_chain_block = RewardChainBlock(
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import random
|
||||
import sys
|
||||
from dataclasses import dataclass
|
||||
from enum import Enum
|
||||
@@ -14,7 +15,7 @@ from chia_rs.sized_bytes import bytes32
|
||||
from chia_rs.sized_ints import uint8, uint64
|
||||
|
||||
from benchmarks.utils import EnumType, get_commit_hash
|
||||
from chia._tests.util.benchmarks import rand_bytes, rand_full_block, rand_hash
|
||||
from chia._tests.util.benchmarks import rand_full_block, rand_hash
|
||||
from chia.util.streamable import Streamable, streamable
|
||||
|
||||
# to run this benchmark:
|
||||
@@ -50,13 +51,13 @@ class BenchmarkClass(Streamable):
|
||||
|
||||
|
||||
def get_random_inner() -> BenchmarkInner:
|
||||
return BenchmarkInner(rand_bytes(20).hex())
|
||||
return BenchmarkInner(random.randbytes(20).hex())
|
||||
|
||||
|
||||
def get_random_middle() -> BenchmarkMiddle:
|
||||
a: uint64 = uint64(10)
|
||||
b: list[bytes32] = [rand_hash() for _ in range(a)]
|
||||
c: tuple[str, bool, uint8, list[bytes]] = ("benchmark", False, uint8(1), [rand_bytes(a) for _ in range(a)])
|
||||
c: tuple[str, bool, uint8, list[bytes]] = ("benchmark", False, uint8(1), [random.randbytes(a) for _ in range(a)])
|
||||
d: tuple[BenchmarkInner, BenchmarkInner] = (get_random_inner(), get_random_inner())
|
||||
e: BenchmarkInner = get_random_inner()
|
||||
return BenchmarkMiddle(a, b, c, d, e)
|
||||
|
||||
@@ -28,10 +28,7 @@ class TestStructStream(unittest.TestCase):
|
||||
|
||||
|
||||
def rand_hash(rng: random.Random) -> bytes32:
|
||||
ret = bytearray(32)
|
||||
for i in range(32):
|
||||
ret[i] = rng.getrandbits(8)
|
||||
return bytes32(ret)
|
||||
return bytes32.random(r=rng)
|
||||
|
||||
|
||||
def create_spends(num: int) -> tuple[list[CoinSpend], list[Coin]]:
|
||||
|
||||
@@ -158,13 +158,6 @@ def test_internal_hash(seeded_random: Random) -> None:
|
||||
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()
|
||||
@@ -176,13 +169,13 @@ def test_leaf_hash(seeded_random: Random) -> None:
|
||||
else:
|
||||
length = seeded_random.randrange(100)
|
||||
|
||||
key = get_random_bytes(length=length, r=seeded_random)
|
||||
key = seeded_random.randbytes(length)
|
||||
|
||||
if cycle in {1, 2}:
|
||||
length = 0
|
||||
else:
|
||||
length = seeded_random.randrange(100)
|
||||
value = get_random_bytes(length=length, r=seeded_random)
|
||||
value = seeded_random.randbytes(length)
|
||||
reference = definition(key=key, value=value)
|
||||
data.append((key, value, reference))
|
||||
|
||||
@@ -205,7 +198,7 @@ def test_key_hash(seeded_random: Random) -> None:
|
||||
length = 0
|
||||
else:
|
||||
length = seeded_random.randrange(100)
|
||||
key = get_random_bytes(length=length, r=seeded_random)
|
||||
key = seeded_random.randbytes(length)
|
||||
reference = definition(key=key)
|
||||
data.append((key, reference))
|
||||
|
||||
|
||||
@@ -1589,11 +1589,7 @@ async def test_benchmark_batch_insert_speed(
|
||||
r.seed("shadowlands", version=2)
|
||||
|
||||
changelist = [
|
||||
{
|
||||
"action": "insert",
|
||||
"key": x.to_bytes(32, byteorder="big", signed=False),
|
||||
"value": bytes(r.getrandbits(8) for _ in range(1200)),
|
||||
}
|
||||
{"action": "insert", "key": x.to_bytes(32, byteorder="big", signed=False), "value": r.randbytes(1200)}
|
||||
for x in range(case.pre + case.count)
|
||||
]
|
||||
|
||||
@@ -1637,7 +1633,7 @@ async def test_benchmark_batch_insert_speed_multiple_batches(
|
||||
{
|
||||
"action": "insert",
|
||||
"key": x.to_bytes(32, byteorder="big", signed=False),
|
||||
"value": bytes(r.getrandbits(8) for _ in range(10000)),
|
||||
"value": r.randbytes(10000),
|
||||
}
|
||||
for x in range(batch * case.count, (batch + 1) * case.count)
|
||||
]
|
||||
|
||||
@@ -369,16 +369,10 @@ async def test_count_uncompactified_blocks(bt: BlockTools, tmp_dir: Path, db_ver
|
||||
async def test_replace_proof(bt: BlockTools, tmp_dir: Path, db_version: int, use_cache: bool) -> None:
|
||||
blocks = bt.get_consecutive_blocks(10)
|
||||
|
||||
def rand_bytes(num: int) -> bytes:
|
||||
ret = bytearray(num)
|
||||
for i in range(num):
|
||||
ret[i] = random.getrandbits(8)
|
||||
return bytes(ret)
|
||||
|
||||
def rand_vdf_proof() -> VDFProof:
|
||||
return VDFProof(
|
||||
uint8(1), # witness_type
|
||||
rand_bytes(32), # witness
|
||||
random.randbytes(32), # witness
|
||||
bool(random.randint(0, 1)), # normalized_to_identity
|
||||
)
|
||||
|
||||
|
||||
@@ -2955,11 +2955,8 @@ def test_timeout(old: bool) -> None:
|
||||
|
||||
|
||||
def rand_hash() -> bytes32:
|
||||
rng = random.Random()
|
||||
ret = bytearray(32)
|
||||
for i in range(32):
|
||||
ret[i] = rng.getrandbits(8)
|
||||
return bytes32(ret)
|
||||
# TODO: does this need to be creating a new rng?
|
||||
return bytes32.random(r=random.Random())
|
||||
|
||||
|
||||
def item_cost(cost: int, fee_rate: float) -> MempoolItem:
|
||||
|
||||
@@ -20,13 +20,6 @@ from chia.simulator.block_tools import test_constants
|
||||
from chia.util.db_wrapper import DBWrapper2
|
||||
|
||||
|
||||
def rand_bytes(num) -> bytes:
|
||||
ret = bytearray(num)
|
||||
for i in range(num):
|
||||
ret[i] = random.getrandbits(8)
|
||||
return bytes(ret)
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
@pytest.mark.parametrize("with_hints", [True, False])
|
||||
@pytest.mark.skip("we no longer support DB v1")
|
||||
@@ -35,21 +28,21 @@ async def test_blocks(default_1000_blocks, with_hints: bool):
|
||||
|
||||
hints: list[tuple[bytes32, bytes]] = []
|
||||
for i in range(351):
|
||||
hints.append((bytes32(rand_bytes(32)), rand_bytes(20)))
|
||||
hints.append((bytes32.random(), random.randbytes(20)))
|
||||
|
||||
# the v1 schema allows duplicates in the hints table
|
||||
for i in range(10):
|
||||
coin_id = bytes32(rand_bytes(32))
|
||||
hint = rand_bytes(20)
|
||||
coin_id = bytes32.random()
|
||||
hint = random.randbytes(20)
|
||||
hints.append((coin_id, hint))
|
||||
hints.append((coin_id, hint))
|
||||
|
||||
for i in range(2000):
|
||||
hints.append((bytes32(rand_bytes(32)), rand_bytes(20)))
|
||||
hints.append((bytes32.random(), random.randbytes(20)))
|
||||
|
||||
for i in range(5):
|
||||
coin_id = bytes32(rand_bytes(32))
|
||||
hint = rand_bytes(20)
|
||||
coin_id = bytes32.random()
|
||||
hint = random.randbytes(20)
|
||||
hints.append((coin_id, hint))
|
||||
hints.append((coin_id, hint))
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import random
|
||||
import sqlite3
|
||||
from contextlib import closing
|
||||
from pathlib import Path
|
||||
@@ -25,10 +24,7 @@ from chia.util.db_wrapper import DBWrapper2
|
||||
|
||||
|
||||
def rand_hash() -> bytes32:
|
||||
ret = bytearray(32)
|
||||
for i in range(32):
|
||||
ret[i] = random.getrandbits(8)
|
||||
return bytes32(ret)
|
||||
return bytes32.random()
|
||||
|
||||
|
||||
def make_version(conn: sqlite3.Connection, version: int) -> None:
|
||||
|
||||
@@ -279,10 +279,7 @@ async def test_merkle_right_edge() -> None:
|
||||
|
||||
|
||||
def rand_hash(rng: random.Random) -> bytes32:
|
||||
ret = bytearray(32)
|
||||
for i in range(32):
|
||||
ret[i] = rng.getrandbits(8)
|
||||
return bytes32(ret)
|
||||
return bytes32.random(r=rng)
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
|
||||
@@ -40,29 +40,22 @@ def rewards(height: uint32) -> tuple[Coin, Coin]:
|
||||
return farmer_coin, pool_coin
|
||||
|
||||
|
||||
def rand_bytes(num: int) -> bytes:
|
||||
ret = bytearray(num)
|
||||
for i in range(num):
|
||||
ret[i] = random.getrandbits(8)
|
||||
return bytes(ret)
|
||||
|
||||
|
||||
def rand_hash() -> bytes32:
|
||||
return bytes32(rand_bytes(32))
|
||||
return bytes32.random()
|
||||
|
||||
|
||||
def rand_g1() -> G1Element:
|
||||
sk = AugSchemeMPL.key_gen(rand_bytes(96))
|
||||
sk = AugSchemeMPL.key_gen(random.randbytes(96))
|
||||
return sk.get_g1()
|
||||
|
||||
|
||||
def rand_g2() -> G2Element:
|
||||
sk = AugSchemeMPL.key_gen(rand_bytes(96))
|
||||
sk = AugSchemeMPL.key_gen(random.randbytes(96))
|
||||
return AugSchemeMPL.sign(sk, b"foobar")
|
||||
|
||||
|
||||
def rand_class_group_element() -> ClassgroupElement:
|
||||
return ClassgroupElement(bytes100(rand_bytes(100)))
|
||||
return ClassgroupElement(bytes100.random())
|
||||
|
||||
|
||||
def rand_vdf() -> VDFInfo:
|
||||
@@ -84,7 +77,7 @@ def rand_full_block() -> FullBlock:
|
||||
None,
|
||||
rand_g1(),
|
||||
uint8(0),
|
||||
rand_bytes(8 * 32),
|
||||
random.randbytes(8 * 32),
|
||||
)
|
||||
|
||||
reward_chain_block = RewardChainBlock(
|
||||
|
||||
@@ -26,7 +26,7 @@ from chia_rs import (
|
||||
from chia_rs.sized_bytes import bytes32
|
||||
from chia_rs.sized_ints import uint8, uint32, uint64, uint128
|
||||
|
||||
from chia._tests.util.benchmarks import rand_bytes, rand_g1, rand_g2, rand_hash, rand_vdf, rand_vdf_proof, rewards
|
||||
from chia._tests.util.benchmarks import rand_g1, rand_g2, rand_hash, rand_vdf, rand_vdf_proof, rewards
|
||||
from chia.consensus.generator_tools import get_block_header
|
||||
from chia.full_node.full_block_utils import (
|
||||
block_info_from_block,
|
||||
@@ -73,7 +73,7 @@ def get_proof_of_space() -> Generator[ProofOfSpace, None, None]:
|
||||
plot_hash,
|
||||
g1(), # plot_public_key
|
||||
uint8(32),
|
||||
rand_bytes(8 * 32),
|
||||
random.randbytes(8 * 32),
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user