Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CHIA-1165] Add --valid-at/--expires-at to all CLI transaction endpoints (mostly hidden) #18501

Merged
merged 6 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion chia/_tests/cmds/cmd_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,9 @@ async def send_transaction_multi(
coins: Optional[List[Coin]] = None,
fee: uint64 = uint64(0),
push: bool = True,
timelock_info: ConditionValidTimes = ConditionValidTimes(),
) -> SendTransactionMultiResponse:
self.add_to_log("send_transaction_multi", (wallet_id, additions, tx_config, coins, fee, push))
self.add_to_log("send_transaction_multi", (wallet_id, additions, tx_config, coins, fee, push, timelock_info))
name = bytes32([2] * 32)
return SendTransactionMultiResponse(
[STD_UTX],
Expand Down
38 changes: 29 additions & 9 deletions chia/_tests/cmds/test_timelock_args.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
from __future__ import annotations

from typing import Optional

import click
from click.testing import CliRunner

from chia.cmds.cmds_util import timelock_args
from chia.wallet.conditions import ConditionValidTimes


def test_timelock_args() -> None:
@click.command()
@timelock_args
def test_cmd(
valid_at: Optional[int],
expires_at: Optional[int],
) -> None:
print(valid_at)
print(expires_at)
@timelock_args(enable=True)
def test_cmd(condition_valid_times: ConditionValidTimes) -> None:
print(condition_valid_times.min_time)
print(condition_valid_times.max_time)

runner = CliRunner()

Expand Down Expand Up @@ -53,3 +49,27 @@ def test_cmd(
)

assert "None\nNone\n" == result.output

# Test the hidden help
@click.command()
@timelock_args(enable=False)
def test_cmd_disabled(condition_valid_times: ConditionValidTimes) -> None:
print(condition_valid_times.min_time)
print(condition_valid_times.max_time)

result = runner.invoke(
test_cmd_disabled,
[],
catch_exceptions=False,
)

assert "None\nNone\n" == result.output

result = runner.invoke(
test_cmd_disabled,
["--help"],
catch_exceptions=False,
)

assert "--valid-at" not in result.output
assert "--expires-at" not in result.output
20 changes: 16 additions & 4 deletions chia/_tests/cmds/wallet/test_coins.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
from chia._tests.cmds.wallet.test_consts import FINGERPRINT, FINGERPRINT_ARG, STD_TX, STD_UTX, get_bytes32
from chia.rpc.wallet_request_types import SplitCoins, SplitCoinsResponse
from chia.util.ints import uint16, uint32, uint64
from chia.wallet.conditions import ConditionValidTimes
from chia.wallet.util.tx_config import DEFAULT_TX_CONFIG, CoinSelectionConfig, TXConfig

test_condition_valid_times: ConditionValidTimes = ConditionValidTimes(min_time=uint64(100), max_time=uint64(150))

# Coin Commands


Expand Down Expand Up @@ -82,6 +85,10 @@ async def select_coins(
"0.2",
"--exclude-amount",
"0.3",
"--valid-at",
"100",
"--expires-at",
"150",
]
# these are various things that should be in the output
assert_list = [
Expand Down Expand Up @@ -138,6 +145,7 @@ async def select_coins(
],
1000000000,
True,
test_condition_valid_times,
),
(
1,
Expand All @@ -156,6 +164,7 @@ async def select_coins(
],
1000000000,
True,
test_condition_valid_times,
),
],
}
Expand All @@ -168,11 +177,9 @@ def test_coins_split(capsys: object, get_test_cli_clients: Tuple[TestRpcClients,
# set RPC Client
class CoinsSplitRpcClient(TestWalletRpcClient):
async def split_coins(
self,
args: SplitCoins,
tx_config: TXConfig,
self, args: SplitCoins, tx_config: TXConfig, timelock_info: ConditionValidTimes
) -> SplitCoinsResponse:
self.add_to_log("split_coins", (args, tx_config))
self.add_to_log("split_coins", (args, tx_config, timelock_info))
return SplitCoinsResponse([STD_UTX], [STD_TX])

inst_rpc_client = CoinsSplitRpcClient() # pylint: disable=no-value-for-parameter
Expand All @@ -188,6 +195,10 @@ async def split_coins(
"-n10",
"-a0.0000001",
f"-t{target_coin_id.hex()}",
"--valid-at",
"100",
"--expires-at",
"150",
]
# these are various things that should be in the output
assert_list = [
Expand All @@ -209,6 +220,7 @@ async def split_coins(
push=True,
),
DEFAULT_TX_CONFIG,
test_condition_valid_times,
)
],
}
Expand Down
10 changes: 9 additions & 1 deletion chia/_tests/cmds/wallet/test_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.util.bech32m import encode_puzzle_hash
from chia.util.ints import uint8, uint32, uint64
from chia.wallet.conditions import parse_timelock_info
from chia.wallet.conditions import ConditionValidTimes, parse_timelock_info
from chia.wallet.transaction_record import TransactionRecord
from chia.wallet.util.transaction_type import TransactionType
from chia.wallet.util.tx_config import TXConfig
Expand All @@ -49,6 +49,7 @@ async def create_new_dao_wallet(
fee: uint64 = uint64(0),
fee_for_cat: uint64 = uint64(0),
push: bool = True,
timelock_info: ConditionValidTimes = ConditionValidTimes(),
) -> CreateNewDAOWalletResponse:
if not treasury_id:
treasury_id = bytes32(token_bytes(32))
Expand Down Expand Up @@ -142,6 +143,7 @@ async def dao_add_funds_to_treasury(
fee: uint64 = uint64(0),
reuse_puzhash: Optional[bool] = None,
push: bool = True,
timelock_info: ConditionValidTimes = ConditionValidTimes(),
) -> DAOAddFundsToTreasuryResponse:
return DAOAddFundsToTreasuryResponse([STD_UTX], [STD_TX], STD_TX.name, STD_TX)

Expand Down Expand Up @@ -281,6 +283,7 @@ async def dao_vote_on_proposal(
is_yes_vote: bool,
fee: uint64 = uint64(0),
push: bool = True,
timelock_info: ConditionValidTimes = ConditionValidTimes(),
) -> DAOVoteOnProposalResponse:
return DAOVoteOnProposalResponse([STD_UTX], [STD_TX], STD_TX.name, STD_TX)

Expand All @@ -293,6 +296,7 @@ async def dao_close_proposal(
self_destruct: bool = False,
reuse_puzhash: Optional[bool] = None,
push: bool = True,
timelock_info: ConditionValidTimes = ConditionValidTimes(),
) -> DAOCloseProposalResponse:
return DAOCloseProposalResponse([STD_UTX], [STD_TX], STD_TX.name, STD_TX)

Expand All @@ -311,6 +315,7 @@ async def dao_create_proposal(
fee: uint64 = uint64(0),
reuse_puzhash: Optional[bool] = None,
push: bool = True,
timelock_info: ConditionValidTimes = ConditionValidTimes(),
) -> DAOCreateProposalResponse:
return DAOCreateProposalResponse([STD_UTX], [STD_TX], bytes32([0] * 32), STD_TX.name, STD_TX)

Expand Down Expand Up @@ -495,6 +500,7 @@ async def dao_send_to_lockup(
fee: uint64 = uint64(0),
reuse_puzhash: Optional[bool] = None,
push: bool = True,
timelock_info: ConditionValidTimes = ConditionValidTimes(),
) -> DAOSendToLockupResponse:
return DAOSendToLockupResponse([STD_UTX], [STD_TX], STD_TX.name, [STD_TX])

Expand All @@ -505,6 +511,7 @@ async def dao_free_coins_from_finished_proposals(
fee: uint64 = uint64(0),
reuse_puzhash: Optional[bool] = None,
push: bool = True,
timelock_info: ConditionValidTimes = ConditionValidTimes(),
) -> DAOFreeCoinsFromFinishedProposalsResponse:
return DAOFreeCoinsFromFinishedProposalsResponse([STD_UTX], [STD_TX], STD_TX.name, STD_TX)

Expand All @@ -516,6 +523,7 @@ async def dao_exit_lockup(
fee: uint64 = uint64(0),
reuse_puzhash: Optional[bool] = None,
push: bool = True,
timelock_info: ConditionValidTimes = ConditionValidTimes(),
) -> DAOExitLockupResponse:
return DAOExitLockupResponse([STD_UTX], [STD_TX], STD_TX.name, STD_TX)

Expand Down
73 changes: 62 additions & 11 deletions chia/_tests/cmds/wallet/test_did.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
from chia.types.signing_mode import SigningMode
from chia.util.bech32m import encode_puzzle_hash
from chia.util.config import load_config
from chia.util.ints import uint32
from chia.wallet.conditions import Condition, CreateCoinAnnouncement, CreatePuzzleAnnouncement
from chia.util.ints import uint32, uint64
from chia.wallet.conditions import Condition, ConditionValidTimes, CreateCoinAnnouncement, CreatePuzzleAnnouncement
from chia.wallet.util.tx_config import DEFAULT_TX_CONFIG, TXConfig
from chia.wallet.wallet_spend_bundle import WalletSpendBundle

test_condition_valid_times: ConditionValidTimes = ConditionValidTimes(min_time=uint64(100), max_time=uint64(150))

# DID Commands


Expand All @@ -34,23 +36,40 @@ async def create_new_did_wallet(
backup_ids: Optional[List[str]] = None,
required_num: int = 0,
push: bool = True,
timelock_info: ConditionValidTimes = ConditionValidTimes(),
) -> Dict[str, Union[str, int]]:
if backup_ids is None:
backup_ids = []
self.add_to_log("create_new_did_wallet", (amount, tx_config, fee, name, backup_ids, required_num, push))
self.add_to_log(
"create_new_did_wallet", (amount, tx_config, fee, name, backup_ids, required_num, push, timelock_info)
)
return {"wallet_id": 3, "my_did": "did:chia:testdid123456"}

inst_rpc_client = DidCreateRpcClient() # pylint: disable=no-value-for-parameter
test_rpc_clients.wallet_rpc_client = inst_rpc_client
command_args = ["wallet", "did", "create", FINGERPRINT_ARG, "-ntest", "-a3", "-m0.1"]
command_args = [
"wallet",
"did",
"create",
FINGERPRINT_ARG,
"-ntest",
"-a3",
"-m0.1",
"--valid-at",
"100",
"--expires-at",
"150",
]
# these are various things that should be in the output
assert_list = [
"Successfully created a DID wallet with name test and id 3 on key 123456",
"Successfully created a DID did:chia:testdid123456 in the newly created DID wallet",
]
run_cli_command_and_assert(capsys, root_dir, command_args, assert_list)
expected_calls: logType = {
"create_new_did_wallet": [(3, DEFAULT_TX_CONFIG, 100000000000, "test", [], 0, True)],
"create_new_did_wallet": [
(3, DEFAULT_TX_CONFIG, 100000000000, "test", [], 0, True, test_condition_valid_times)
],
}
test_rpc_clients.wallet_rpc_client.check_log(expected_calls)

Expand Down Expand Up @@ -183,8 +202,9 @@ async def update_did_metadata(
metadata: Dict[str, object],
tx_config: TXConfig,
push: bool = True,
timelock_info: ConditionValidTimes = ConditionValidTimes(),
) -> DIDUpdateMetadataResponse:
self.add_to_log("update_did_metadata", (wallet_id, metadata, tx_config, push))
self.add_to_log("update_did_metadata", (wallet_id, metadata, tx_config, push, timelock_info))
return DIDUpdateMetadataResponse([STD_UTX], [STD_TX], WalletSpendBundle([], G2Element()), uint32(wallet_id))

inst_rpc_client = DidUpdateMetadataRpcClient() # pylint: disable=no-value-for-parameter
Expand All @@ -200,13 +220,19 @@ async def update_did_metadata(
"--metadata",
json_mdata,
"--reuse",
"--valid-at",
"100",
"--expires-at",
"150",
]
# these are various things that should be in the output
assert STD_TX.spend_bundle is not None
assert_list = [f"Successfully updated DID wallet ID: {w_id}, Spend Bundle: {STD_TX.spend_bundle.to_json_dict()}"]
run_cli_command_and_assert(capsys, root_dir, command_args, assert_list)
expected_calls: logType = {
"update_did_metadata": [(w_id, {"test": True}, DEFAULT_TX_CONFIG.override(reuse_puzhash=True), True)],
"update_did_metadata": [
(w_id, {"test": True}, DEFAULT_TX_CONFIG.override(reuse_puzhash=True), True, test_condition_valid_times)
],
}
test_rpc_clients.wallet_rpc_client.check_log(expected_calls)

Expand Down Expand Up @@ -255,9 +281,14 @@ def test_did_message_spend(capsys: object, get_test_cli_clients: Tuple[TestRpcCl
# set RPC Client
class DidMessageSpendRpcClient(TestWalletRpcClient):
async def did_message_spend(
self, wallet_id: int, tx_config: TXConfig, extra_conditions: Tuple[Condition, ...], push: bool
self,
wallet_id: int,
tx_config: TXConfig,
extra_conditions: Tuple[Condition, ...],
push: bool,
timelock_info: ConditionValidTimes = ConditionValidTimes(),
) -> DIDMessageSpendResponse:
self.add_to_log("did_message_spend", (wallet_id, tx_config, extra_conditions, True))
self.add_to_log("did_message_spend", (wallet_id, tx_config, extra_conditions, push, timelock_info))
return DIDMessageSpendResponse([STD_UTX], [STD_TX], WalletSpendBundle([], G2Element()))

inst_rpc_client = DidMessageSpendRpcClient() # pylint: disable=no-value-for-parameter
Expand All @@ -275,6 +306,10 @@ async def did_message_spend(
",".join([announcement.hex() for announcement in c_announcements]),
"--puzzle_announcements",
",".join([announcement.hex() for announcement in puz_announcements]),
"--valid-at",
"100",
"--expires-at",
"150",
]
# these are various things that should be in the output
assert STD_TX.spend_bundle is not None
Expand All @@ -290,6 +325,7 @@ async def did_message_spend(
*(CreatePuzzleAnnouncement(ann) for ann in puz_announcements),
),
True,
test_condition_valid_times,
)
],
}
Expand All @@ -309,8 +345,11 @@ async def did_transfer_did(
with_recovery: bool,
tx_config: TXConfig,
push: bool,
timelock_info: ConditionValidTimes = ConditionValidTimes(),
) -> DIDTransferDIDResponse:
self.add_to_log("did_transfer_did", (wallet_id, address, fee, with_recovery, tx_config, push))
self.add_to_log(
"did_transfer_did", (wallet_id, address, fee, with_recovery, tx_config, push, timelock_info)
)
return DIDTransferDIDResponse(
[STD_UTX],
[STD_TX],
Expand All @@ -332,6 +371,10 @@ async def did_transfer_did(
"--reuse",
"--target-address",
t_address,
"--valid-at",
"100",
"--expires-at",
"150",
]
# these are various things that should be in the output
config = load_config(
Expand All @@ -346,7 +389,15 @@ async def did_transfer_did(
run_cli_command_and_assert(capsys, root_dir, command_args, assert_list)
expected_calls: logType = {
"did_transfer_did": [
(w_id, t_address, 500000000000, True, DEFAULT_TX_CONFIG.override(reuse_puzhash=True), True)
(
w_id,
t_address,
500000000000,
True,
DEFAULT_TX_CONFIG.override(reuse_puzhash=True),
True,
test_condition_valid_times,
)
],
}
test_rpc_clients.wallet_rpc_client.check_log(expected_calls)
Loading
Loading