Skip to content

Commit

Permalink
less DEFAULT_ROOT_PATH for rpc commands (#18984)
Browse files Browse the repository at this point in the history
  • Loading branch information
altendky authored Dec 4, 2024
1 parent 65dc231 commit e03042c
Showing 1 changed file with 38 additions and 19 deletions.
57 changes: 38 additions & 19 deletions chia/cmds/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,38 @@
import asyncio
import json
import sys
from pathlib import Path
from typing import Any, Optional, TextIO

import click
from aiohttp import ClientResponseError

from chia.util.config import load_config
from chia.util.default_root import DEFAULT_ROOT_PATH
from chia.util.ints import uint16

services: list[str] = ["crawler", "daemon", "farmer", "full_node", "harvester", "timelord", "wallet", "data_layer"]


async def call_endpoint(
service: str, endpoint: str, request: dict[str, Any], config: dict[str, Any], quiet: bool = False
service: str,
endpoint: str,
request: dict[str, Any],
config: dict[str, Any],
root_path: Path,
quiet: bool = False,
) -> dict[str, Any]:
if service == "daemon":
return await call_daemon_command(endpoint, request, config, quiet)
return await call_daemon_command(endpoint, request, config, root_path=root_path, quiet=quiet)

return await call_rpc_service_endpoint(service, endpoint, request, config)
return await call_rpc_service_endpoint(service, endpoint, request, config, root_path=root_path)


async def call_rpc_service_endpoint(
service: str, endpoint: str, request: dict[str, Any], config: dict[str, Any]
service: str,
endpoint: str,
request: dict[str, Any],
config: dict[str, Any],
root_path: Path,
) -> dict[str, Any]:
from chia.rpc.rpc_client import RpcClient

Expand All @@ -37,7 +46,7 @@ async def call_rpc_service_endpoint(
port = uint16(config[service]["rpc_port"])

try:
client = await RpcClient.create(config["self_hostname"], port, DEFAULT_ROOT_PATH, config)
client = await RpcClient.create(config["self_hostname"], port, root_path, config)
except Exception as e:
raise Exception(f"Failed to create RPC client {service}: {e}")
result: dict[str, Any]
Expand All @@ -56,11 +65,11 @@ async def call_rpc_service_endpoint(


async def call_daemon_command(
command: str, request: dict[str, Any], config: dict[str, Any], quiet: bool = False
command: str, request: dict[str, Any], config: dict[str, Any], root_path: Path, quiet: bool = False
) -> dict[str, Any]:
from chia.daemon.client import connect_to_daemon_and_validate

daemon = await connect_to_daemon_and_validate(DEFAULT_ROOT_PATH, config, quiet=quiet)
daemon = await connect_to_daemon_and_validate(root_path, config, quiet=quiet)

if daemon is None:
raise Exception("Failed to connect to chia daemon")
Expand All @@ -81,8 +90,8 @@ def print_result(json_dict: dict[str, Any]) -> None:
print(json.dumps(json_dict, indent=2, sort_keys=True))


def get_routes(service: str, config: dict[str, Any], quiet: bool = False) -> dict[str, Any]:
return asyncio.run(call_endpoint(service, "get_routes", {}, config, quiet))
def get_routes(service: str, config: dict[str, Any], root_path: Path, quiet: bool = False) -> dict[str, Any]:
return asyncio.run(call_endpoint(service, "get_routes", {}, config, root_path=root_path, quiet=quiet))


@click.group("rpc", help="RPC Client")
Expand All @@ -92,10 +101,12 @@ def rpc_cmd() -> None:

@rpc_cmd.command("endpoints", help="Print all endpoints of a service")
@click.argument("service", type=click.Choice(services))
def endpoints_cmd(service: str) -> None:
config = load_config(DEFAULT_ROOT_PATH, "config.yaml")
@click.pass_context
def endpoints_cmd(ctx: click.Context, service: str) -> None:
root_path = ctx.obj["root_path"]
config = load_config(root_path, "config.yaml")
try:
routes = get_routes(service, config)
routes = get_routes(service, config, root_path=root_path)
for route in routes["routes"]:
print(route.lstrip("/"))
except Exception as e:
Expand All @@ -104,10 +115,12 @@ def endpoints_cmd(service: str) -> None:

@rpc_cmd.command("status", help="Print the status of all available RPC services")
@click.option("--json-output", "json_output", is_flag=True, help="Output status as JSON")
def status_cmd(json_output: bool) -> None:
@click.pass_context
def status_cmd(ctx: click.Context, json_output: bool) -> None:
import json

config = load_config(DEFAULT_ROOT_PATH, "config.yaml")
root_path = ctx.obj["root_path"]
config = load_config(root_path, "config.yaml")

def print_row(c0: str, c1: str) -> None:
print(f"│ {c0:<12}{c1:<9} │")
Expand All @@ -116,7 +129,7 @@ def print_row(c0: str, c1: str) -> None:
for service in services:
status = "ACTIVE"
try:
if not get_routes(service, config, quiet=True)["success"]:
if not get_routes(service, config, root_path=root_path, quiet=True)["success"]:
raise Exception()
except Exception:
status = "INACTIVE"
Expand Down Expand Up @@ -156,10 +169,16 @@ def create_commands() -> None:
type=click.File("r"),
default=None,
)
@click.pass_context
def rpc_client_cmd(
endpoint: str, request: Optional[str], json_file: Optional[TextIO], service: str = service
ctx: click.Context,
endpoint: str,
request: Optional[str],
json_file: Optional[TextIO],
service: str = service,
) -> None:
config = load_config(DEFAULT_ROOT_PATH, "config.yaml")
root_path: Path = ctx.obj["root_path"]
config = load_config(root_path, "config.yaml")
if request is not None and json_file is not None:
sys.exit(
"Can only use one request source: REQUEST argument OR -j/--json-file option. See the help with -h"
Expand All @@ -180,7 +199,7 @@ def rpc_client_cmd(
try:
if endpoint[0] == "/":
endpoint = endpoint[1:]
print_result(asyncio.run(call_endpoint(service, endpoint, request_json, config)))
print_result(asyncio.run(call_endpoint(service, endpoint, request_json, config, root_path=root_path)))
except Exception as e:
sys.exit(str(e))

Expand Down

0 comments on commit e03042c

Please sign in to comment.