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

Convert wallet RPC client to deserialized types #17393

Merged
merged 16 commits into from
Apr 8, 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
46 changes: 27 additions & 19 deletions chia/_tests/cmds/cmd_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import chia.cmds.wallet_funcs
from chia._tests.cmds.testing_classes import create_test_block_record
from chia._tests.cmds.wallet.test_consts import STD_TX, STD_UTX
from chia.cmds.chia import cli as chia_cli
from chia.cmds.cmds_util import _T_RpcClient, node_config_section_names
from chia.consensus.block_record import BlockRecord
Expand All @@ -18,6 +19,7 @@
from chia.rpc.farmer_rpc_client import FarmerRpcClient
from chia.rpc.full_node_rpc_client import FullNodeRpcClient
from chia.rpc.rpc_client import RpcClient
from chia.rpc.wallet_request_types import SendTransactionMultiResponse
from chia.rpc.wallet_rpc_client import WalletRpcClient
from chia.simulator.simulator_full_node_rpc_client import SimulatorFullNodeRpcClient
from chia.types.blockchain_format.sized_bytes import bytes32
Expand Down Expand Up @@ -252,26 +254,32 @@ async def send_transaction_multi(
tx_config: TXConfig,
coins: Optional[List[Coin]] = None,
fee: uint64 = uint64(0),
) -> TransactionRecord:
) -> SendTransactionMultiResponse:
self.add_to_log("send_transaction_multi", (wallet_id, additions, tx_config, coins, fee))
return TransactionRecord(
confirmed_at_height=uint32(1),
created_at_time=uint64(1234),
to_puzzle_hash=bytes32([1] * 32),
amount=uint64(12345678),
fee_amount=uint64(1234567),
confirmed=False,
sent=uint32(0),
spend_bundle=SpendBundle([], G2Element()),
additions=[Coin(bytes32([1] * 32), bytes32([2] * 32), uint64(12345678))],
removals=[Coin(bytes32([2] * 32), bytes32([4] * 32), uint64(12345678))],
wallet_id=uint32(1),
sent_to=[("aaaaa", uint8(1), None)],
trade_id=None,
type=uint32(TransactionType.OUTGOING_TX.value),
name=bytes32([2] * 32),
memos=[(bytes32([3] * 32), [bytes([4] * 32)])],
valid_times=ConditionValidTimes(),
name = bytes32([2] * 32)
return SendTransactionMultiResponse(
[STD_UTX],
[STD_TX],
TransactionRecord(
confirmed_at_height=uint32(1),
created_at_time=uint64(1234),
to_puzzle_hash=bytes32([1] * 32),
amount=uint64(12345678),
fee_amount=uint64(1234567),
confirmed=False,
sent=uint32(0),
spend_bundle=SpendBundle([], G2Element()),
additions=[Coin(bytes32([1] * 32), bytes32([2] * 32), uint64(12345678))],
removals=[Coin(bytes32([2] * 32), bytes32([4] * 32), uint64(12345678))],
wallet_id=uint32(1),
sent_to=[("aaaaa", uint8(1), None)],
trade_id=None,
type=uint32(TransactionType.OUTGOING_TX.value),
name=name,
memos=[(bytes32([3] * 32), [bytes([4] * 32)])],
valid_times=ConditionValidTimes(),
),
name,
)


Expand Down
8 changes: 6 additions & 2 deletions chia/_tests/cmds/wallet/test_consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

from chia.types.blockchain_format.sized_bytes import bytes32
from chia.types.spend_bundle import SpendBundle
from chia.util.ints import uint8, uint32, uint64
from chia.util.ints import uint32, uint64
from chia.wallet.conditions import ConditionValidTimes
from chia.wallet.signer_protocol import KeyHints, SigningInstructions, TransactionInfo, UnsignedTransaction
from chia.wallet.transaction_record import TransactionRecord
from chia.wallet.util.transaction_type import TransactionType

Expand Down Expand Up @@ -34,10 +35,13 @@ def get_bytes32(bytes_index: int) -> bytes32:
additions=[Coin(get_bytes32(1), get_bytes32(2), uint64(12345678))],
removals=[Coin(get_bytes32(2), get_bytes32(4), uint64(12345678))],
wallet_id=uint32(1),
sent_to=[("aaaaa", uint8(1), None)],
sent_to=[],
trade_id=None,
type=uint32(TransactionType.OUTGOING_TX.value),
name=get_bytes32(2),
memos=[(get_bytes32(3), [bytes([4] * 32)])],
valid_times=ConditionValidTimes(),
)


STD_UTX = UnsignedTransaction(TransactionInfo([]), SigningInstructions(KeyHints([], []), []))
66 changes: 40 additions & 26 deletions chia/_tests/cmds/wallet/test_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,17 @@
from typing_extensions import override

from chia._tests.cmds.cmd_test_utils import TestRpcClients, TestWalletRpcClient, run_cli_command_and_assert
from chia._tests.cmds.wallet.test_consts import FINGERPRINT_ARG
from chia._tests.cmds.wallet.test_consts import FINGERPRINT_ARG, STD_TX, STD_UTX
from chia.rpc.wallet_request_types import (
CreateNewDAOWalletResponse,
DAOAddFundsToTreasuryResponse,
DAOCloseProposalResponse,
DAOCreateProposalResponse,
DAOExitLockupResponse,
DAOFreeCoinsFromFinishedProposalsResponse,
DAOSendToLockupResponse,
DAOVoteOnProposalResponse,
)
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
Expand Down Expand Up @@ -38,17 +48,21 @@ async def create_new_dao_wallet(
name: Optional[str] = None,
fee: uint64 = uint64(0),
fee_for_cat: uint64 = uint64(0),
) -> Dict[str, Union[str, int, bytes32]]:
) -> CreateNewDAOWalletResponse:
if not treasury_id:
treasury_id = bytes32(token_bytes(32))
return {
"success": True,
"type": "DAO",
"wallet_id": 2,
"treasury_id": treasury_id,
"cat_wallet_id": 3,
"dao_cat_wallet_id": 4,
}
return CreateNewDAOWalletResponse.from_json_dict(
{
"success": True,
"transactions": [STD_TX.to_json_dict()],
"unsigned_transactions": [STD_UTX.to_json_dict()],
"type": WalletType.DAO,
"wallet_id": 2,
"treasury_id": treasury_id,
"cat_wallet_id": 3,
"dao_cat_wallet_id": 4,
}
)

inst_rpc_client = DAOCreateRpcClient() # pylint: disable=no-value-for-parameter
test_rpc_clients.wallet_rpc_client = inst_rpc_client
Expand Down Expand Up @@ -126,8 +140,8 @@ async def dao_add_funds_to_treasury(
tx_config: TXConfig,
fee: uint64 = uint64(0),
reuse_puzhash: Optional[bool] = None,
) -> Dict[str, Union[str, bool]]:
return {"success": True, "tx_id": bytes32(b"1" * 32).hex()}
) -> DAOAddFundsToTreasuryResponse:
return DAOAddFundsToTreasuryResponse([STD_UTX], [STD_TX], STD_TX.name, STD_TX)

async def dao_get_rules(
self,
Expand Down Expand Up @@ -264,8 +278,8 @@ async def dao_vote_on_proposal(
tx_config: TXConfig,
is_yes_vote: bool,
fee: uint64 = uint64(0),
) -> Dict[str, Union[str, bool]]:
return {"success": True, "tx_id": bytes32(b"1" * 32).hex()}
) -> DAOVoteOnProposalResponse:
return DAOVoteOnProposalResponse([STD_UTX], [STD_TX], STD_TX.name, STD_TX)

async def dao_close_proposal(
self,
Expand All @@ -275,8 +289,8 @@ async def dao_close_proposal(
fee: uint64 = uint64(0),
self_destruct: bool = False,
reuse_puzhash: Optional[bool] = None,
) -> Dict[str, Union[str, bool]]:
return {"success": True, "tx_id": bytes32(b"1" * 32).hex()}
) -> DAOCloseProposalResponse:
return DAOCloseProposalResponse([STD_UTX], [STD_TX], STD_TX.name, STD_TX)

async def dao_create_proposal(
self,
Expand All @@ -292,8 +306,8 @@ async def dao_create_proposal(
new_dao_rules: Optional[Dict[str, uint64]] = None,
fee: uint64 = uint64(0),
reuse_puzhash: Optional[bool] = None,
) -> Dict[str, Union[str, bool]]:
return {"success": True, "proposal_id": "0xCAFEF00D"}
) -> DAOCreateProposalResponse:
return DAOCreateProposalResponse([STD_UTX], [STD_TX], bytes32([0] * 32), STD_TX.name, STD_TX)

async def get_wallets(self, wallet_type: Optional[WalletType] = None) -> List[Dict[str, Union[str, int]]]:
return [{"id": 1, "type": 0}, {"id": 2, "type": 14}]
Expand Down Expand Up @@ -405,7 +419,7 @@ async def get_transaction(self, transaction_id: bytes32) -> TransactionRecord:
"-m 0.1",
"--reuse",
]
proposal_asserts = ["Successfully created proposal", "Proposal ID: 0xCAFEF00D"]
proposal_asserts = ["Successfully created proposal", f"Proposal ID: {bytes32([0] * 32).hex()}"]
run_cli_command_and_assert(capsys, root_dir, spend_args, proposal_asserts)

bad_spend_args = [
Expand All @@ -423,7 +437,7 @@ async def get_transaction(self, transaction_id: bytes32) -> TransactionRecord:
"-m 0.1",
"--reuse",
]
proposal_asserts = ["Successfully created proposal", "Proposal ID: 0xCAFEF00D"]
proposal_asserts = ["Successfully created proposal", f"Proposal ID: {bytes32([0] * 32).hex()}"]
with pytest.raises(ValueError) as e_info:
run_cli_command_and_assert(capsys, root_dir, bad_spend_args, proposal_asserts)
assert e_info.value.args[0] == "Must include a json specification or an address / amount pair."
Expand Down Expand Up @@ -475,17 +489,17 @@ async def dao_send_to_lockup(
tx_config: TXConfig,
fee: uint64 = uint64(0),
reuse_puzhash: Optional[bool] = None,
) -> Dict[str, Union[str, int]]:
return {"success": True, "tx_id": bytes32(b"x" * 32).hex()}
) -> DAOSendToLockupResponse:
return DAOSendToLockupResponse([STD_UTX], [STD_TX], STD_TX.name, [STD_TX])

async def dao_free_coins_from_finished_proposals(
self,
wallet_id: int,
tx_config: TXConfig,
fee: uint64 = uint64(0),
reuse_puzhash: Optional[bool] = None,
) -> Dict[str, Union[str, int]]:
return {"success": True, "tx_id": bytes32(b"x" * 32).hex()}
) -> DAOFreeCoinsFromFinishedProposalsResponse:
return DAOFreeCoinsFromFinishedProposalsResponse([STD_UTX], [STD_TX], STD_TX.name, STD_TX)

async def dao_exit_lockup(
self,
Expand All @@ -494,8 +508,8 @@ async def dao_exit_lockup(
coins: Optional[List[Dict[str, Union[str, int]]]] = None,
fee: uint64 = uint64(0),
reuse_puzhash: Optional[bool] = None,
) -> Dict[str, Union[str, int]]:
return {"success": True, "tx_id": bytes32(b"x" * 32).hex()}
) -> DAOExitLockupResponse:
return DAOExitLockupResponse([STD_UTX], [STD_TX], STD_TX.name, STD_TX)

@override
async def get_transaction(self, transaction_id: bytes32) -> TransactionRecord:
Expand Down
37 changes: 27 additions & 10 deletions chia/_tests/cmds/wallet/test_did.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
from pathlib import Path
from typing import Dict, List, Optional, Tuple, Union

from chia_rs import G2Element

from chia._tests.cmds.cmd_test_utils import TestRpcClients, TestWalletRpcClient, logType, run_cli_command_and_assert
from chia._tests.cmds.wallet.test_consts import FINGERPRINT_ARG, get_bytes32
from chia._tests.cmds.wallet.test_consts import FINGERPRINT_ARG, STD_TX, STD_UTX, get_bytes32
from chia.rpc.wallet_request_types import DIDMessageSpendResponse, DIDTransferDIDResponse, DIDUpdateMetadataResponse
from chia.types.blockchain_format.sized_bytes import bytes48
from chia.types.signing_mode import SigningMode
from chia.types.spend_bundle import SpendBundle
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.wallet.util.tx_config import DEFAULT_TX_CONFIG, TXConfig

Expand Down Expand Up @@ -174,9 +180,9 @@ async def update_did_metadata(
wallet_id: int,
metadata: Dict[str, object],
tx_config: TXConfig,
) -> Dict[str, object]:
) -> DIDUpdateMetadataResponse:
self.add_to_log("update_did_metadata", (wallet_id, metadata, tx_config))
return {"wallet_id": wallet_id, "spend_bundle": "spend bundle here"}
return DIDUpdateMetadataResponse([STD_UTX], [STD_TX], SpendBundle([], G2Element()), uint32(wallet_id))

inst_rpc_client = DidUpdateMetadataRpcClient() # pylint: disable=no-value-for-parameter
test_rpc_clients.wallet_rpc_client = inst_rpc_client
Expand All @@ -193,7 +199,8 @@ async def update_did_metadata(
"--reuse",
]
# these are various things that should be in the output
assert_list = [f"Successfully updated DID wallet ID: {w_id}, Spend Bundle: spend bundle here"]
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))],
Expand Down Expand Up @@ -246,9 +253,9 @@ def test_did_message_spend(capsys: object, get_test_cli_clients: Tuple[TestRpcCl
class DidMessageSpendRpcClient(TestWalletRpcClient):
async def did_message_spend(
self, wallet_id: int, tx_config: TXConfig, extra_conditions: Tuple[Condition, ...]
) -> Dict[str, object]:
) -> DIDMessageSpendResponse:
self.add_to_log("did_message_spend", (wallet_id, tx_config, extra_conditions))
return {"spend_bundle": "spend bundle here"}
return DIDMessageSpendResponse([STD_UTX], [STD_TX], SpendBundle([], G2Element()))

inst_rpc_client = DidMessageSpendRpcClient() # pylint: disable=no-value-for-parameter
test_rpc_clients.wallet_rpc_client = inst_rpc_client
Expand All @@ -267,7 +274,8 @@ async def did_message_spend(
",".join([announcement.hex() for announcement in puz_announcements]),
]
# these are various things that should be in the output
assert_list = ["Message Spend Bundle: spend bundle here"]
assert STD_TX.spend_bundle is not None
assert_list = [f"Message Spend Bundle: {STD_TX.spend_bundle.to_json_dict()}"]
run_cli_command_and_assert(capsys, root_dir, command_args, assert_list)
expected_calls: logType = {
"did_message_spend": [
Expand Down Expand Up @@ -296,9 +304,14 @@ async def did_transfer_did(
fee: int,
with_recovery: bool,
tx_config: TXConfig,
) -> Dict[str, object]:
) -> DIDTransferDIDResponse:
self.add_to_log("did_transfer_did", (wallet_id, address, fee, with_recovery, tx_config))
return {"transaction_id": get_bytes32(2).hex(), "transaction": "transaction here"}
return DIDTransferDIDResponse(
[STD_UTX],
[STD_TX],
STD_TX,
STD_TX.name,
)

inst_rpc_client = DidTransferRpcClient() # pylint: disable=no-value-for-parameter
test_rpc_clients.wallet_rpc_client = inst_rpc_client
Expand All @@ -316,10 +329,14 @@ async def did_transfer_did(
t_address,
]
# these are various things that should be in the output
config = load_config(
root_dir,
"config.yaml",
)
assert_list = [
f"Successfully transferred DID to {t_address}",
f"Transaction ID: {get_bytes32(2).hex()}",
"Transaction: transaction here",
f"Transaction: {STD_TX.to_json_dict_convenience(config)}",
]
run_cli_command_and_assert(capsys, root_dir, command_args, assert_list)
expected_calls: logType = {
Expand Down
Loading
Loading