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

Code before:
from typing import Any, Optional

import aiofiles
import click
from chia_rs.sized_bytes import bytes32

from chia.cmds.cmds_util import get_any_service_client
from chia.full_node.full_node_rpc_client import FullNodeRpcClient
from chia.util.default_root import resolve_root_path
from chia.util.path import path_from_root

DEFAULT_REQUESTS_PER_BATCH: int = 25000


def get_height_to_hash_filename(root_path: Path, config: dict[str, Any]) -> Path:
    """
    Utility function to get the path to the height-to-hash database file.
    """
    db_path_replaced: Path = root_path / config["full_node"]["database_path"]
    db_directory: Path = path_from_root(root_path, db_path_replaced).parent
    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)

Review comment (performance):
calling this `block_cache` is a bit misleading I think. In the node we refer to it as height-to-hash

Code after:

import aiofiles
import click
from chia_rs.sized_bytes import bytes32

from chia.cmds.cmds_util import get_any_service_client
from chia.full_node.full_node_rpc_client import FullNodeRpcClient
from chia.util.default_root import resolve_root_path
from chia.util.path import path_from_root
from chia.util.task_referencer import create_referenced_task

DEFAULT_PIPELINE_DEPTH: int = 10


def get_height_to_hash_filename(root_path: Path, config: dict[str, Any]) -> Path:
    """
    Utility function to get the path to the height-to-hash database file.
    """
    db_path_replaced: Path = root_path / config["full_node"]["database_path"]
    db_directory: Path = path_from_root(root_path, db_path_replaced).parent
    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,
)