Skip to content

Commit

Permalink
more
Browse files Browse the repository at this point in the history
  • Loading branch information
altendky committed Dec 17, 2024
1 parent 3c4db40 commit a32f5dc
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 80 deletions.
7 changes: 5 additions & 2 deletions chia/_tests/cmds/test_cmd_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from chia.wallet.util.tx_config import CoinSelectionConfig, TXConfig


def check_click_parsing(cmd: ChiaCommand, *args: str, obj: Optional[Any] = None) -> None:
def check_click_parsing(cmd: ChiaCommand, *args: str, context: Optional[ChiaCliContext] = None) -> None:
@click.group()
def _cmd() -> None:
pass
Expand All @@ -58,8 +58,11 @@ def new_run(self: Any) -> None:
setattr(mock_type, "run", new_run)
chia_command(group=_cmd, name="_", short_help="", help="")(mock_type)

if context is None:
context = ChiaCliContext()

runner = CliRunner()
result = runner.invoke(_cmd, ["_", *args], catch_exceptions=False, obj=obj)
result = runner.invoke(_cmd, ["_", *args], catch_exceptions=False, obj=context.to_click())
assert result.output == ""


Expand Down
4 changes: 3 additions & 1 deletion chia/_tests/pools/test_pool_cli_parsing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from chia._tests.cmds.test_cmd_framework import check_click_parsing
from chia.cmds.cmd_classes import ChiaCliContext
from chia.cmds.cmd_helpers import NeedsWalletRPC
from chia.cmds.param_types import CliAddress
from chia.cmds.plotnft import (
Expand Down Expand Up @@ -37,7 +38,8 @@ def test_plotnft_command_default_parsing() -> None:
launcher_id.hex(),
"--address",
burn_address,
obj={"expected_prefix": "xch"}, # Needed for AddressParamType to work correctly without config
# Needed for AddressParamType to work correctly without config
context=ChiaCliContext(expected_prefix="xch"),
)

check_click_parsing(
Expand Down
1 change: 1 addition & 0 deletions chia/_tests/pools/test_pool_cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,7 @@ async def test_plotnft_cli_change_payout(
# This tests what happens when using None for root_path
mocker.patch("chia.cmds.plotnft_funcs.DEFAULT_ROOT_PATH", root_path)
await ChangePayoutInstructionsPlotNFTCMD(
context=ChiaCliContext(root_path=wallet_environments.environments[0].node.root_path),
launcher_id=bytes32(32 * b"0"),
address=CliAddress(burn_ph, burn_address, AddressType.XCH),
).run()
Expand Down
4 changes: 4 additions & 0 deletions chia/cmds/cmd_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,14 @@ class ChiaCliContext:
root_path: pathlib.Path = DEFAULT_ROOT_PATH
expected_prefix: Optional[str] = None
rpc_port: Optional[int] = None
keys_fingerprint: Optional[int] = None
keys_filename: Optional[str] = None
expected_currency_prefix: Optional[str] = None

@classmethod
def from_click(cls, ctx: click.Context) -> ChiaCliContext:
if ctx.obj is None:
# TODO: should we set it up on the ctx here?
return cls()

existing = cast(Optional[ChiaCliContext], ctx.obj.get(cls.context_dict_key))
Expand Down
43 changes: 27 additions & 16 deletions chia/cmds/dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import click

from chia.cmds import options
from chia.cmds.cmd_classes import ChiaCliContext
from chia.cmds.cmds_util import CMDTXConfigLoader, tx_config_args, tx_out_cmd
from chia.cmds.param_types import AmountParamType, Bytes32ParamType, CliAmount, TransactionFeeParamType, Uint64ParamType
from chia.cmds.units import units
Expand Down Expand Up @@ -62,7 +63,11 @@ def dao_add_cmd(
) -> None:
from chia.cmds.dao_funcs import add_dao_wallet

asyncio.run(add_dao_wallet(ctx.obj["root_path"], wallet_rpc_port, fingerprint, name, treasury_id, filter_amount))
asyncio.run(
add_dao_wallet(
ChiaCliContext.from_click(ctx).root_path, wallet_rpc_port, fingerprint, name, treasury_id, filter_amount
)
)


# ----------------------------------------------------------------------------------------
Expand Down Expand Up @@ -184,7 +189,7 @@ def dao_create_cmd(

return asyncio.run(
create_dao_wallet(
ctx.obj["root_path"],
ChiaCliContext.from_click(ctx).root_path,
wallet_rpc_port,
fingerprint,
fee,
Expand Down Expand Up @@ -235,7 +240,7 @@ def dao_get_id_cmd(
) -> None:
from chia.cmds.dao_funcs import get_treasury_id

asyncio.run(get_treasury_id(ctx.obj["root_path"], wallet_rpc_port, fingerprint, wallet_id))
asyncio.run(get_treasury_id(ChiaCliContext.from_click(ctx).root_path, wallet_rpc_port, fingerprint, wallet_id))


@dao_cmd.command("add_funds", short_help="Send funds to a DAO treasury", no_args_is_help=True)
Expand Down Expand Up @@ -286,7 +291,7 @@ def dao_add_funds_cmd(

return asyncio.run(
add_funds_to_treasury(
ctx.obj["root_path"],
ChiaCliContext.from_click(ctx).root_path,
wallet_rpc_port,
fingerprint,
wallet_id,
Expand Down Expand Up @@ -325,7 +330,7 @@ def dao_get_balance_cmd(
) -> None:
from chia.cmds.dao_funcs import get_treasury_balance

asyncio.run(get_treasury_balance(ctx.obj["root_path"], wallet_rpc_port, fingerprint, wallet_id))
asyncio.run(get_treasury_balance(ChiaCliContext.from_click(ctx).root_path, wallet_rpc_port, fingerprint, wallet_id))


@dao_cmd.command("rules", short_help="Get the current rules governing the DAO", no_args_is_help=True)
Expand All @@ -347,7 +352,7 @@ def dao_rules_cmd(
) -> None:
from chia.cmds.dao_funcs import get_rules

asyncio.run(get_rules(ctx.obj["root_path"], wallet_rpc_port, fingerprint, wallet_id))
asyncio.run(get_rules(ChiaCliContext.from_click(ctx).root_path, wallet_rpc_port, fingerprint, wallet_id))


# ----------------------------------------------------------------------------------------
Expand Down Expand Up @@ -383,7 +388,11 @@ def dao_list_proposals_cmd(
if not include_closed:
include_closed = False

asyncio.run(list_proposals(ctx.obj["root_path"], wallet_rpc_port, fingerprint, wallet_id, include_closed))
asyncio.run(
list_proposals(
ChiaCliContext.from_click(ctx).root_path, wallet_rpc_port, fingerprint, wallet_id, include_closed
)
)


@dao_cmd.command("show_proposal", short_help="Show the details of a specific proposal", no_args_is_help=True)
Expand Down Expand Up @@ -413,7 +422,9 @@ def dao_show_proposal_cmd(
) -> None:
from chia.cmds.dao_funcs import show_proposal

asyncio.run(show_proposal(ctx.obj["root_path"], wallet_rpc_port, fingerprint, wallet_id, proposal_id))
asyncio.run(
show_proposal(ChiaCliContext.from_click(ctx).root_path, wallet_rpc_port, fingerprint, wallet_id, proposal_id)
)


# ----------------------------------------------------------------------------------------
Expand Down Expand Up @@ -477,7 +488,7 @@ def dao_vote_cmd(

return asyncio.run(
vote_on_proposal(
ctx.obj["root_path"],
ChiaCliContext.from_click(ctx).root_path,
wallet_rpc_port,
fingerprint,
wallet_id,
Expand Down Expand Up @@ -550,7 +561,7 @@ def dao_close_proposal_cmd(

return asyncio.run(
close_proposal(
ctx.obj["root_path"],
ChiaCliContext.from_click(ctx).root_path,
wallet_rpc_port,
fingerprint,
wallet_id,
Expand Down Expand Up @@ -614,7 +625,7 @@ def dao_lockup_coins_cmd(

return asyncio.run(
lockup_coins(
ctx.obj["root_path"],
ChiaCliContext.from_click(ctx).root_path,
wallet_rpc_port,
fingerprint,
wallet_id,
Expand Down Expand Up @@ -665,7 +676,7 @@ def dao_release_coins_cmd(

return asyncio.run(
release_coins(
ctx.obj["root_path"],
ChiaCliContext.from_click(ctx).root_path,
wallet_rpc_port,
fingerprint,
wallet_id,
Expand Down Expand Up @@ -715,7 +726,7 @@ def dao_exit_lockup_cmd(

return asyncio.run(
exit_lockup(
ctx.obj["root_path"],
ChiaCliContext.from_click(ctx).root_path,
wallet_rpc_port,
fingerprint,
wallet_id,
Expand Down Expand Up @@ -819,7 +830,7 @@ def dao_create_spend_proposal_cmd(

return asyncio.run(
create_spend_proposal(
ctx.obj["root_path"],
ChiaCliContext.from_click(ctx).root_path,
wallet_rpc_port,
fingerprint,
wallet_id,
Expand Down Expand Up @@ -931,7 +942,7 @@ def dao_create_update_proposal_cmd(

return asyncio.run(
create_update_proposal(
ctx.obj["root_path"],
ChiaCliContext.from_click(ctx).root_path,
wallet_rpc_port,
fingerprint,
wallet_id,
Expand Down Expand Up @@ -1014,7 +1025,7 @@ def dao_create_mint_proposal_cmd(

return asyncio.run(
create_mint_proposal(
ctx.obj["root_path"],
ChiaCliContext.from_click(ctx).root_path,
wallet_rpc_port,
fingerprint,
wallet_id,
Expand Down
14 changes: 12 additions & 2 deletions chia/cmds/farm.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import click

from chia.cmds.cmd_classes import ChiaCliContext


@click.group("farm", help="Manage your farm")
def farm_cmd() -> None:
Expand Down Expand Up @@ -61,7 +63,15 @@ def summary_cmd(

from chia.cmds.farm_funcs import summary

asyncio.run(summary(rpc_port, wallet_rpc_port, harvester_rpc_port, farmer_rpc_port, root_path=ctx.obj["root_path"]))
asyncio.run(
summary(
rpc_port,
wallet_rpc_port,
harvester_rpc_port,
farmer_rpc_port,
root_path=ChiaCliContext.from_click(ctx).root_path,
)
)


@farm_cmd.command("challenges", help="Show the latest challenges")
Expand All @@ -87,4 +97,4 @@ def challenges_cmd(ctx: click.Context, farmer_rpc_port: Optional[int], limit: in

from chia.cmds.farm_funcs import challenges

asyncio.run(challenges(ctx.obj["root_path"], farmer_rpc_port, limit))
asyncio.run(challenges(ChiaCliContext.from_click(ctx).root_path, farmer_rpc_port, limit))
22 changes: 13 additions & 9 deletions chia/cmds/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,9 @@ def verify_cmd(message: str, public_key: str, signature: str, as_bytes: bool, js
)
@click.pass_context
def derive_cmd(ctx: click.Context, fingerprint: Optional[int], filename: Optional[str]) -> None:
ctx.obj["fingerprint"] = fingerprint
ctx.obj["filename"] = filename
context = ChiaCliContext.from_click(ctx)
context.keys_fingerprint = fingerprint
context.keys_filename = filename


@derive_cmd.command("search", help="Search the keyring for one or more matching derived keys or wallet addresses")
Expand Down Expand Up @@ -338,8 +339,9 @@ def search_cmd(

from chia.cmds.keys_funcs import resolve_derivation_master_key, search_derive

fingerprint: Optional[int] = ctx.obj.get("fingerprint", None)
filename: Optional[str] = ctx.obj.get("filename", None)
context = ChiaCliContext.from_click(ctx)
fingerprint: Optional[int] = context.keys_fingerprint
filename: Optional[str] = context.keys_filename

# Specifying the master key is optional for the search command. If not specified, we'll search all keys.
resolved_sk = None
Expand All @@ -349,7 +351,7 @@ def search_cmd(
print("Could not resolve private key from fingerprint/mnemonic file")

found: bool = search_derive(
ChiaCliContext.from_click(ctx).root_path,
context.root_path,
fingerprint,
search_terms,
limit,
Expand Down Expand Up @@ -416,8 +418,9 @@ def wallet_address_cmd(
) -> None:
from chia.cmds.keys_funcs import derive_wallet_address

fingerprint: Optional[int] = ctx.obj.get("fingerprint", None)
filename: Optional[str] = ctx.obj.get("filename", None)
context = ChiaCliContext.from_click(ctx)
fingerprint = context.keys_fingerprint
filename = context.keys_filename

try:
fingerprint, sk = _resolve_fingerprint_and_sk(filename, fingerprint, non_observer_derivation)
Expand Down Expand Up @@ -501,8 +504,9 @@ def child_key_cmd(
if key_type is None and derive_from_hd_path is None:
ctx.fail("--type or --derive-from-hd-path is required")

fingerprint: Optional[int] = ctx.obj.get("fingerprint", None)
filename: Optional[str] = ctx.obj.get("filename", None)
context = ChiaCliContext.from_click(ctx)
fingerprint = context.keys_fingerprint
filename = context.keys_filename

try:
fingerprint, sk = _resolve_fingerprint_and_sk(filename, fingerprint, non_observer_derivation)
Expand Down
4 changes: 3 additions & 1 deletion chia/cmds/netspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import click

from chia.cmds.cmd_classes import ChiaCliContext


@click.command("netspace", help="Estimate total farmed space on the network")
@click.option(
Expand Down Expand Up @@ -45,4 +47,4 @@ def netspace_cmd(ctx: click.Context, rpc_port: Optional[int], delta_block_height

from chia.cmds.netspace_funcs import netstorge_async

asyncio.run(netstorge_async(ctx.obj["root_path"], rpc_port, delta_block_height, start))
asyncio.run(netstorge_async(ChiaCliContext.from_click(ctx).root_path, rpc_port, delta_block_height, start))
16 changes: 12 additions & 4 deletions chia/cmds/param_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,23 @@ def convert(self, value: Any, param: Optional[click.Parameter], ctx: Optional[cl
hrp, _b32data = bech32_decode(value)
if hrp in {"xch", "txch"}: # I hate having to load the config here
addr_type: AddressType = AddressType.XCH

# attempt to get cached prefix
expected_prefix = ChiaCliContext.from_click(ctx).expected_prefix if ctx else None
expected_prefix: Optional[str] = None
root_path = DEFAULT_ROOT_PATH

if ctx is not None:
context = ChiaCliContext.from_click(ctx)
root_path = context.root_path
expected_prefix = context.expected_prefix

if expected_prefix is None:
root_path = ChiaCliContext.from_click(ctx).root_path if ctx is not None else DEFAULT_ROOT_PATH
config = load_config(root_path, "config.yaml")
expected_prefix = selected_network_address_prefix(config)

if ctx is not None:
ctx.obj["expected_prefix"] = expected_prefix # cache prefix
if ctx is not None:
context.expected_prefix = expected_prefix # cache prefix

# now that we have the expected prefix, we can validate the address is for the right network
if hrp != expected_prefix:
self.fail(f"Unexpected Address Prefix: {hrp}, are you sure its for the right network?", param, ctx)
Expand Down
7 changes: 4 additions & 3 deletions chia/cmds/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import click
from aiohttp import ClientResponseError

from chia.cmds.cmd_classes import ChiaCliContext
from chia.util.config import load_config
from chia.util.ints import uint16

Expand Down Expand Up @@ -103,7 +104,7 @@ def rpc_cmd() -> None:
@click.argument("service", type=click.Choice(services))
@click.pass_context
def endpoints_cmd(ctx: click.Context, service: str) -> None:
root_path = ctx.obj["root_path"]
root_path = ChiaCliContext.from_click(ctx).root_path
config = load_config(root_path, "config.yaml")
try:
routes = get_routes(service, config, root_path=root_path)
Expand All @@ -119,7 +120,7 @@ def endpoints_cmd(ctx: click.Context, service: str) -> None:
def status_cmd(ctx: click.Context, json_output: bool) -> None:
import json

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

def print_row(c0: str, c1: str) -> None:
Expand Down Expand Up @@ -177,7 +178,7 @@ def rpc_client_cmd(
json_file: Optional[TextIO],
service: str = service,
) -> None:
root_path: Path = ctx.obj["root_path"]
root_path: Path = ChiaCliContext.from_click(ctx).root_path
config = load_config(root_path, "config.yaml")
if request is not None and json_file is not None:
sys.exit(
Expand Down
1 change: 1 addition & 0 deletions chia/cmds/sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
@click.pass_context
def sim_cmd(ctx: click.Context, rpc_port: Optional[int], root_path: str, simulator_name: str) -> None:
ctx.ensure_object(dict)
# TODO: maybe update instead of replacing?
ctx.obj.update(
ChiaCliContext(
root_path=Path(root_path) / simulator_name,
Expand Down
Loading

0 comments on commit a32f5dc

Please sign in to comment.