Skip to content

Commit

Permalink
less DEFAULT_ROOT_PATH for get_any_service_client() (#19049)
Browse files Browse the repository at this point in the history
  • Loading branch information
altendky authored Dec 17, 2024
1 parent 5c08ab9 commit 9c60a48
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 34 deletions.
4 changes: 2 additions & 2 deletions chia/_tests/cmds/cmd_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,8 @@ def create_service_and_wallet_client_generators(test_rpc_clients: TestRpcClients
@asynccontextmanager
async def test_get_any_service_client(
client_type: type[_T_RpcClient],
root_path: Path,
rpc_port: Optional[int] = None,
root_path: Optional[Path] = None,
consume_errors: bool = True,
use_ssl: bool = True,
) -> AsyncIterator[tuple[_T_RpcClient, dict[str, Any]]]:
Expand Down Expand Up @@ -413,7 +413,7 @@ async def test_get_wallet_client(
wallet_rpc_port: Optional[int] = None,
fingerprint: Optional[int] = None,
) -> AsyncIterator[tuple[WalletRpcClient, int, dict[str, Any]]]:
async with test_get_any_service_client(WalletRpcClient, wallet_rpc_port, root_path) as (wallet_client, config):
async with test_get_any_service_client(WalletRpcClient, root_path, wallet_rpc_port) as (wallet_client, config):
wallet_client.fingerprint = fingerprint # type: ignore
assert fingerprint is not None
yield wallet_client, fingerprint, config
Expand Down
4 changes: 2 additions & 2 deletions chia/_tests/farmer_harvester/test_farmer_harvester.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ def farmer_is_started(farmer: Farmer) -> bool:


async def get_harvester_config(harvester_rpc_port: Optional[int], root_path: Path) -> dict[str, Any]:
async with get_any_service_client(HarvesterRpcClient, harvester_rpc_port, root_path) as (harvester_client, _):
async with get_any_service_client(HarvesterRpcClient, root_path, harvester_rpc_port) as (harvester_client, _):
return await harvester_client.get_harvester_config()


async def update_harvester_config(harvester_rpc_port: Optional[int], root_path: Path, config: dict[str, Any]) -> bool:
async with get_any_service_client(HarvesterRpcClient, harvester_rpc_port, root_path) as (harvester_client, _):
async with get_any_service_client(HarvesterRpcClient, root_path, harvester_rpc_port) as (harvester_client, _):
return await harvester_client.update_harvester_config(config)


Expand Down
8 changes: 2 additions & 6 deletions chia/cmds/cmds_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.types.mempool_submission_status import MempoolSubmissionStatus
from chia.util.config import load_config
from chia.util.default_root import DEFAULT_ROOT_PATH
from chia.util.errors import CliRpcConnectionError, InvalidPathError
from chia.util.ints import uint16, uint32, uint64
from chia.util.keychain import KeyData
Expand Down Expand Up @@ -95,8 +94,8 @@ async def validate_client_connection(
@asynccontextmanager
async def get_any_service_client(
client_type: type[_T_RpcClient],
root_path: Path,
rpc_port: Optional[int] = None,
root_path: Optional[Path] = None,
consume_errors: bool = True,
use_ssl: bool = True,
) -> AsyncIterator[tuple[_T_RpcClient, dict[str, Any]]]:
Expand All @@ -106,9 +105,6 @@ async def get_any_service_client(
the RpcClient.
"""

if root_path is None:
root_path = DEFAULT_ROOT_PATH

node_type = node_config_section_names.get(client_type)
if node_type is None:
# Click already checks this, so this should never happen
Expand Down Expand Up @@ -253,7 +249,7 @@ async def get_wallet_client(
fingerprint: Optional[int] = None,
consume_errors: bool = True,
) -> AsyncIterator[tuple[WalletRpcClient, int, dict[str, Any]]]:
async with get_any_service_client(WalletRpcClient, wallet_rpc_port, root_path, consume_errors) as (
async with get_any_service_client(WalletRpcClient, root_path, wallet_rpc_port, consume_errors) as (
wallet_client,
config,
):
Expand Down
3 changes: 3 additions & 0 deletions chia/cmds/data_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
from chia.rpc.data_layer_rpc_client import DataLayerRpcClient
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.util.byte_types import hexstr_to_bytes
from chia.util.default_root import resolve_root_path
from chia.util.ints import uint64


@contextlib.asynccontextmanager
async def get_client(
rpc_port: Optional[int], fingerprint: Optional[int] = None, root_path: Optional[Path] = None
) -> AsyncIterator[tuple[DataLayerRpcClient, dict[str, Any]]]:
root_path = resolve_root_path(override=root_path)

async with get_any_service_client(
client_type=DataLayerRpcClient,
rpc_port=rpc_port,
Expand Down
5 changes: 3 additions & 2 deletions chia/cmds/farm.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ def summary_cmd(
default=20,
show_default=True,
)
def challenges_cmd(farmer_rpc_port: Optional[int], limit: int) -> None:
@click.pass_context
def challenges_cmd(ctx: click.Context, farmer_rpc_port: Optional[int], limit: int) -> None:
import asyncio

from chia.cmds.farm_funcs import challenges

asyncio.run(challenges(farmer_rpc_port, limit))
asyncio.run(challenges(ctx.obj["root_path"], farmer_rpc_port, limit))
16 changes: 8 additions & 8 deletions chia/cmds/farm_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@


async def get_harvesters_summary(farmer_rpc_port: Optional[int], root_path: Path) -> Optional[dict[str, Any]]:
async with get_any_service_client(FarmerRpcClient, farmer_rpc_port, root_path) as (farmer_client, _):
async with get_any_service_client(FarmerRpcClient, root_path, farmer_rpc_port) as (farmer_client, _):
return await farmer_client.get_harvesters_summary()


async def get_blockchain_state(rpc_port: Optional[int], root_path: Path) -> Optional[dict[str, Any]]:
async with get_any_service_client(FullNodeRpcClient, rpc_port, root_path) as (client, _):
async with get_any_service_client(FullNodeRpcClient, root_path, rpc_port) as (client, _):
return await client.get_blockchain_state()


async def get_average_block_time(rpc_port: Optional[int], root_path: Path) -> float:
async with get_any_service_client(FullNodeRpcClient, rpc_port, root_path) as (client, _):
async with get_any_service_client(FullNodeRpcClient, root_path, rpc_port) as (client, _):
blocks_to_compare = 500
blockchain_state = await client.get_blockchain_state()
curr: Optional[BlockRecord] = blockchain_state["peak"]
Expand All @@ -49,17 +49,17 @@ async def get_average_block_time(rpc_port: Optional[int], root_path: Path) -> fl


async def get_wallets_stats(wallet_rpc_port: Optional[int], root_path: Path) -> Optional[dict[str, Any]]:
async with get_any_service_client(WalletRpcClient, wallet_rpc_port, root_path) as (wallet_client, _):
async with get_any_service_client(WalletRpcClient, root_path, wallet_rpc_port) as (wallet_client, _):
return await wallet_client.get_farmed_amount()


async def get_challenges(farmer_rpc_port: Optional[int]) -> Optional[list[dict[str, Any]]]:
async with get_any_service_client(FarmerRpcClient, farmer_rpc_port) as (farmer_client, _):
async def get_challenges(root_path: Path, farmer_rpc_port: Optional[int]) -> Optional[list[dict[str, Any]]]:
async with get_any_service_client(FarmerRpcClient, root_path, farmer_rpc_port) as (farmer_client, _):
return await farmer_client.get_signage_points()


async def challenges(farmer_rpc_port: Optional[int], limit: int) -> None:
signage_points = await get_challenges(farmer_rpc_port)
async def challenges(root_path: Path, farmer_rpc_port: Optional[int], limit: int) -> None:
signage_points = await get_challenges(root_path, farmer_rpc_port)
if signage_points is None:
return None

Expand Down
5 changes: 3 additions & 2 deletions chia/cmds/netspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@
type=str,
default="",
)
def netspace_cmd(rpc_port: Optional[int], delta_block_height: str, start: str) -> None:
@click.pass_context
def netspace_cmd(ctx: click.Context, rpc_port: Optional[int], delta_block_height: str, start: str) -> None:
"""
Calculates the estimated space on the network given two block header hashes.
"""
import asyncio

from chia.cmds.netspace_funcs import netstorge_async

asyncio.run(netstorge_async(rpc_port, delta_block_height, start))
asyncio.run(netstorge_async(ctx.obj["root_path"], rpc_port, delta_block_height, start))
5 changes: 3 additions & 2 deletions chia/cmds/netspace_funcs.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
from __future__ import annotations

from pathlib import Path
from typing import Optional

from chia.cmds.cmds_util import format_bytes, get_any_service_client
from chia.rpc.full_node_rpc_client import FullNodeRpcClient
from chia.types.blockchain_format.sized_bytes import bytes32


async def netstorge_async(rpc_port: Optional[int], delta_block_height: str, start: str) -> None:
async def netstorge_async(root_path: Path, rpc_port: Optional[int], delta_block_height: str, start: str) -> None:
"""
Calculates the estimated space on the network given two block header hashes.
"""
async with get_any_service_client(FullNodeRpcClient, rpc_port) as (client, _):
async with get_any_service_client(FullNodeRpcClient, root_path, rpc_port) as (client, _):
if delta_block_height:
if start == "":
blockchain_state = await client.get_blockchain_state()
Expand Down
2 changes: 1 addition & 1 deletion chia/cmds/peer_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ async def peer_async(
remove_connection: str,
) -> None:
client_type = NODE_TYPES[node_type]
async with get_any_service_client(client_type, rpc_port, root_path) as (rpc_client, config):
async with get_any_service_client(client_type, root_path, rpc_port) as (rpc_client, config):
# Check or edit node connections
if show_connections:
trusted_peers: dict[str, Any] = config[node_type].get("trusted_peers", {})
Expand Down
4 changes: 2 additions & 2 deletions chia/cmds/plotnft.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async def run(self) -> None:
async with self.rpc_info.wallet_rpc() as wallet_info:
await show(
wallet_info=wallet_info,
root_path=self.context.get("root_path"),
root_path=self.context["root_path"],
wallet_id_passed_in=self.id,
)

Expand All @@ -62,7 +62,7 @@ class GetLoginLinkCMD:
async def run(self) -> None:
from chia.cmds.plotnft_funcs import get_login_link

await get_login_link(self.launcher_id, root_path=self.context.get("root_path"))
await get_login_link(self.launcher_id, root_path=self.context["root_path"])


# Functions with this mark in this file are not being ported to @tx_out_cmd due to lack of observer key support
Expand Down
4 changes: 2 additions & 2 deletions chia/cmds/plotnft_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ async def pprint_all_pool_wallet_state(

async def show(
wallet_info: WalletClientInfo,
root_path: Optional[Path],
root_path: Path,
wallet_id_passed_in: Optional[int],
) -> None:
summaries_response = await wallet_info.client.get_wallets()
Expand Down Expand Up @@ -239,7 +239,7 @@ async def show(
await pprint_all_pool_wallet_state(wallet_info.client, summaries_response, address_prefix, pool_state_dict)


async def get_login_link(launcher_id: bytes32, root_path: Optional[Path]) -> None:
async def get_login_link(launcher_id: bytes32, root_path: Path) -> None:
async with get_any_service_client(FarmerRpcClient, root_path=root_path) as (farmer_client, _):
login_link: Optional[str] = await farmer_client.get_pool_login_link(launcher_id)
if login_link is None:
Expand Down
2 changes: 1 addition & 1 deletion chia/cmds/show_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ async def show_async(
) -> None:
from chia.cmds.cmds_util import get_any_service_client

async with get_any_service_client(FullNodeRpcClient, rpc_port, root_path) as (node_client, config):
async with get_any_service_client(FullNodeRpcClient, root_path, rpc_port) as (node_client, config):
# Check State
if print_state:
if await print_blockchain_state(node_client, config) is True:
Expand Down
8 changes: 4 additions & 4 deletions chia/cmds/sim_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ async def print_status(
from chia.cmds.show_funcs import print_blockchain_state
from chia.cmds.units import units

async with get_any_service_client(SimulatorFullNodeRpcClient, rpc_port, root_path) as (node_client, config):
async with get_any_service_client(SimulatorFullNodeRpcClient, root_path, rpc_port) as (node_client, config):
# Display keychain info
if show_key:
if fingerprint is None:
Expand Down Expand Up @@ -451,7 +451,7 @@ async def revert_block_height(
"""
This function allows users to easily revert the chain to a previous state or perform a reorg.
"""
async with get_any_service_client(SimulatorFullNodeRpcClient, rpc_port, root_path) as (node_client, _):
async with get_any_service_client(SimulatorFullNodeRpcClient, root_path, rpc_port) as (node_client, _):
if use_revert_blocks:
if num_new_blocks != 1:
print(f"Ignoring num_new_blocks: {num_new_blocks}, because we are not performing a reorg.")
Expand Down Expand Up @@ -479,7 +479,7 @@ async def farm_blocks(
"""
This function is used to generate new blocks.
"""
async with get_any_service_client(SimulatorFullNodeRpcClient, rpc_port, root_path) as (node_client, config):
async with get_any_service_client(SimulatorFullNodeRpcClient, root_path, rpc_port) as (node_client, config):
if target_address == "":
target_address = config["simulator"]["farming_address"]
if target_address is None:
Expand All @@ -500,7 +500,7 @@ async def set_auto_farm(rpc_port: Optional[int], root_path: Path, set_autofarm:
"""
This function can be used to enable or disable Auto Farming.
"""
async with get_any_service_client(SimulatorFullNodeRpcClient, rpc_port, root_path) as (node_client, _):
async with get_any_service_client(SimulatorFullNodeRpcClient, root_path, rpc_port) as (node_client, _):
current = await node_client.get_auto_farming()
if current == set_autofarm:
print(f"Auto farming is already {'on' if set_autofarm else 'off'}")
Expand Down

0 comments on commit 9c60a48

Please sign in to comment.