Repository: Chia-Network/chia-blockchain
File: tools/validate_rpcs.py (Python)

Code before:
    selected_network: str = config["full_node"]["selected_network"]
    suffix = "" if (selected_network is None or selected_network == "mainnet") else f"-{selected_network}"
    return db_directory / f"height-to-hash{suffix}"


async def get_block_cache_bytearray(root_path: Path, config: dict[str, Any], peak: int) -> bytearray:
    """
    Load the height-to-hash database file into a bytearray.
    """
    height_to_hash = bytearray()  # Init as bytearray to prep file loading
    height_to_hash_filename: Path = get_height_to_hash_filename(root_path, config)
    # Load the height-to-hash file
    async with aiofiles.open(height_to_hash_filename, "rb") as f:
        height_to_hash = bytearray(await f.read())
    # allocate memory for height to hash map
    # this may also truncate it, if the file on disk had an invalid size
    new_size = (peak + 1) * 32
    size = len(height_to_hash)
    if size > new_size:
        del height_to_hash[new_size:]
    else:
        height_to_hash += bytearray([0] * (new_size - size))
    return height_to_hash


def get_block_header_from_height(height: int, height_to_hash: bytearray) -> bytes32:
    """
    Get the block header hash from the height-to-hash database.
    """
    idx = height * 32
    assert idx + 32 <= len(height_to_hash)
    return bytes32(height_to_hash[idx : idx + 32])


@click.command(help="Test RPC endpoints using chain", no_args_is_help=True)
@click.option(
    "--root-path",
    default=resolve_root_path(override=None),
    help="Config file root",
    type=click.Path(),
    show_default=True,
)
@click.option(
    "-p",
    "--rpc-port",
    help=(
        "Set the port where the Full Node is hosting the RPC interface. See the rpc_port under full_node in config.yaml"
    ),
    type=int,
    default=None,
)

Review comment (suggestion):
```suggestion
def get_block_hash_from_height(height: int, height_to_hash: bytearray) -> bytes32:
```

Code after:
    selected_network: str = config["full_node"]["selected_network"]
    suffix = "" if (selected_network is None or selected_network == "mainnet") else f"-{selected_network}"
    return db_directory / f"height-to-hash{suffix}"


async def get_height_to_hash_bytes(root_path: Path, config: dict[str, Any]) -> bytes:
    """
    Load the height-to-hash database file into a bytearray.
    """
    height_to_hash_filename: Path = get_height_to_hash_filename(root_path, config)
    async with aiofiles.open(height_to_hash_filename, "rb") as f:
        return await f.read()


def get_block_hash_from_height(height: int, height_to_hash: bytes) -> bytes32:
    """
    Get the block header hash from the height-to-hash database.
    """
    idx = height * 32
    assert idx + 32 <= len(height_to_hash)
    return bytes32(height_to_hash[idx : idx + 32])


@click.command(help="Test RPC endpoints using chain", no_args_is_help=True)
@click.option(
    "--root-path",
    default=resolve_root_path(override=None),
    help="Config file root",
    type=click.Path(),
    show_default=True,
)
@click.option(
    "-p",
    "--rpc-port",
    help=(
        "Set the port where the Full Node is hosting the RPC interface. See the rpc_port under full_node in config.yaml"
    ),
    type=int,
    default=None,
)
@click.option(
    "-sc",
    "--spends_with_conditions",
    help="Test get_block_spends_with_conditions",
    is_flag=True,
    type=bool,
    default=False,
)
@click.option(
    "-sp",
    "--block_spends",