Skip to content
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
20 changes: 12 additions & 8 deletions chia/_tests/cmds/cmd_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
from chia.wallet.util.tx_config import TXConfig
from chia.wallet.util.wallet_types import WalletType
from chia.wallet.wallet_request_types import (
CATAssetIDToName,
CATAssetIDToNameResponse,
CATGetName,
CATGetNameResponse,
GetSyncStatusResponse,
GetTransaction,
GetTransactionResponse,
Expand Down Expand Up @@ -146,9 +150,9 @@ async def get_transaction(self, request: GetTransaction) -> GetTransactionRespon
bytes32([2] * 32),
)

async def get_cat_name(self, wallet_id: int) -> str:
self.add_to_log("get_cat_name", (wallet_id,))
return "test" + str(wallet_id)
async def get_cat_name(self, request: CATGetName) -> CATGetNameResponse:
self.add_to_log("get_cat_name", (request.wallet_id,))
return CATGetNameResponse(request.wallet_id, "test" + str(request.wallet_id))

async def sign_message_by_address(self, request: SignMessageByAddress) -> SignMessageByAddressResponse:
self.add_to_log("sign_message_by_address", (request.address, request.message))
Expand Down Expand Up @@ -182,15 +186,15 @@ async def sign_message_by_id(self, request: SignMessageByID) -> SignMessageByIDR
signing_mode = SigningMode.CHIP_0002.value
return SignMessageByIDResponse(pubkey, signature, bytes32.zeros, signing_mode)

async def cat_asset_id_to_name(self, asset_id: bytes32) -> Optional[tuple[Optional[uint32], str]]:
async def cat_asset_id_to_name(self, request: CATAssetIDToName) -> CATAssetIDToNameResponse:
"""
if bytes32([1] * 32), return (uint32(2), "test1"), if bytes32([1] * 32), return (uint32(3), "test2")
"""
self.add_to_log("cat_asset_id_to_name", (asset_id,))
self.add_to_log("cat_asset_id_to_name", (request.asset_id,))
for i in range(256):
if asset_id == get_bytes32(i):
return uint32(i + 1), "test" + str(i)
return None
if request.asset_id == get_bytes32(i):
return CATAssetIDToNameResponse(uint32(i + 1), "test" + str(i))
return CATAssetIDToNameResponse(wallet_id=None, name=None)

async def get_nft_info(self, request: NFTGetInfo) -> NFTGetInfoResponse:
self.add_to_log("get_nft_info", (request.coin_id, request.latest))
Expand Down
36 changes: 19 additions & 17 deletions chia/_tests/cmds/wallet/test_notifications.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
from __future__ import annotations

from pathlib import Path
from typing import cast

from chia_rs.sized_bytes import bytes32
from chia_rs.sized_ints import uint32, uint64

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, FINGERPRINT_ARG, get_bytes32
from chia._tests.cmds.wallet.test_consts import FINGERPRINT, FINGERPRINT_ARG, STD_TX, STD_UTX, get_bytes32
from chia.util.bech32m import encode_puzzle_hash
from chia.wallet.conditions import ConditionValidTimes
from chia.wallet.conditions import Condition, ConditionValidTimes
from chia.wallet.notification_store import Notification
from chia.wallet.transaction_record import TransactionRecord
from chia.wallet.wallet_request_types import DeleteNotifications, GetNotifications, GetNotificationsResponse
from chia.wallet.util.tx_config import TXConfig
from chia.wallet.wallet_request_types import (
DeleteNotifications,
GetNotifications,
GetNotificationsResponse,
SendNotification,
SendNotificationResponse,
)

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

Expand All @@ -26,20 +31,17 @@ def test_notifications_send(capsys: object, get_test_cli_clients: tuple[TestRpcC
class NotificationsSendRpcClient(TestWalletRpcClient):
async def send_notification(
self,
target: bytes32,
msg: bytes,
amount: uint64,
fee: uint64 = uint64(0),
push: bool = True,
request: SendNotification,
tx_config: TXConfig,
extra_conditions: tuple[Condition, ...] = tuple(),
timelock_info: ConditionValidTimes = ConditionValidTimes(),
) -> TransactionRecord:
self.add_to_log("send_notification", (target, msg, amount, fee, push, timelock_info))

class FakeTransactionRecord:
def __init__(self, name: str) -> None:
self.name = name
) -> SendNotificationResponse:
self.add_to_log(
"send_notification",
(request.target, request.message, request.amount, request.fee, request.push, timelock_info),
)

return cast(TransactionRecord, FakeTransactionRecord(get_bytes32(2).hex()))
return SendNotificationResponse([STD_UTX], [STD_TX], tx=STD_TX)

inst_rpc_client = NotificationsSendRpcClient()
test_rpc_clients.wallet_rpc_client = inst_rpc_client
Expand Down
23 changes: 14 additions & 9 deletions chia/_tests/cmds/wallet/test_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
from chia.wallet.wallet_request_types import (
BalanceResponse,
CancelOfferResponse,
CATAssetIDToName,
CATAssetIDToNameResponse,
CATSetName,
CATSetNameResponse,
CATSpendResponse,
ClawbackPuzzleDecoratorOverride,
CreateOfferForIDsResponse,
Expand Down Expand Up @@ -683,8 +687,9 @@ async def create_wallet_for_existing_cat(self, asset_id: bytes) -> dict[str, int
self.add_to_log("create_wallet_for_existing_cat", (asset_id,))
return {"wallet_id": 3}

async def set_cat_name(self, wallet_id: int, name: str) -> None:
self.add_to_log("set_cat_name", (wallet_id, name))
async def set_cat_name(self, request: CATSetName) -> CATSetNameResponse:
self.add_to_log("set_cat_name", (request.wallet_id, request.name))
return CATSetNameResponse(wallet_id=request.wallet_id)

inst_rpc_client = AddTokenRpcClient()
test_rpc_clients.wallet_rpc_client = inst_rpc_client
Expand Down Expand Up @@ -1051,14 +1056,14 @@ async def take_offer(
),
)

async def cat_asset_id_to_name(self, asset_id: bytes32) -> Optional[tuple[Optional[uint32], str]]:
self.add_to_log("cat_asset_id_to_name", (asset_id,))
if asset_id == cat_offered_id:
return uint32(2), "offered cat"
elif asset_id == cat_requested_id:
return uint32(3), "requested cat"
async def cat_asset_id_to_name(self, request: CATAssetIDToName) -> CATAssetIDToNameResponse:
self.add_to_log("cat_asset_id_to_name", (request.asset_id,))
if request.asset_id == cat_offered_id:
return CATAssetIDToNameResponse(uint32(2), "offered cat")
elif request.asset_id == cat_requested_id:
return CATAssetIDToNameResponse(uint32(3), "requested cat")
else:
return None
return CATAssetIDToNameResponse(wallet_id=None, name=None)

inst_rpc_client = TakeOfferRpcClient()
test_rpc_clients.wallet_rpc_client = inst_rpc_client
Expand Down
7 changes: 4 additions & 3 deletions chia/_tests/core/cmds/test_wallet.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from __future__ import annotations

from typing import Any, Optional
from typing import Any

import pytest
from chia_rs.sized_bytes import bytes32
from chia_rs.sized_ints import uint32

from chia.cmds.wallet_funcs import print_offer_summary
from chia.wallet.wallet_request_types import CATAssetIDToName, CATAssetIDToNameResponse

TEST_DUCKSAUCE_ASSET_ID = "1000000000000000000000000000000000000000000000000000000000000001"
TEST_CRUNCHBERRIES_ASSET_ID = "1000000000000000000000000000000000000000000000000000000000000002"
Expand All @@ -19,8 +20,8 @@
}


async def cat_name_resolver(asset_id: bytes32) -> Optional[tuple[Optional[uint32], str]]:
return TEST_ASSET_ID_NAME_MAPPING.get(asset_id)
async def cat_name_resolver(request: CATAssetIDToName) -> CATAssetIDToNameResponse:
return CATAssetIDToNameResponse(*TEST_ASSET_ID_NAME_MAPPING.get(request.asset_id, (None, None)))


@pytest.mark.anyio
Expand Down
12 changes: 8 additions & 4 deletions chia/_tests/core/data_layer/test_data_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
from chia.wallet.util.tx_config import DEFAULT_TX_CONFIG
from chia.wallet.wallet import Wallet
from chia.wallet.wallet_node import WalletNode
from chia.wallet.wallet_request_types import DLLatestSingleton
from chia.wallet.wallet_request_types import CheckOfferValidity, DLLatestSingleton
from chia.wallet.wallet_rpc_api import WalletRpcApi
from chia.wallet.wallet_service import WalletService

Expand Down Expand Up @@ -222,7 +222,9 @@ async def check_singleton_confirmed(dl: DataLayer, store_id: bytes32) -> bool:

async def process_block_and_check_offer_validity(offer: TradingOffer, offer_setup: OfferSetup) -> bool:
await offer_setup.full_node_api.farm_blocks_to_puzzlehash(count=1, guarantee_transaction_blocks=True)
return (await offer_setup.maker.data_layer.wallet_rpc.check_offer_validity(offer=offer))[1]
return (
await offer_setup.maker.data_layer.wallet_rpc.check_offer_validity(CheckOfferValidity(offer=offer.to_bech32()))
).valid


async def run_cli_cmd(*args: str, root_path: Path) -> asyncio.subprocess.Process:
Expand Down Expand Up @@ -1825,9 +1827,11 @@ async def test_make_and_cancel_offer(offer_setup: OfferSetup, reference: MakeAnd
for _ in range(10):
if not (
await offer_setup.maker.data_layer.wallet_rpc.check_offer_validity(
offer=TradingOffer.from_bytes(hexstr_to_bytes(maker_response["offer"]["offer"])),
CheckOfferValidity(
offer=TradingOffer.from_bytes(hexstr_to_bytes(maker_response["offer"]["offer"])).to_bech32()
),
)
)[1]:
).valid:
break
await offer_setup.full_node_api.farm_blocks_to_puzzlehash(count=1, guarantee_transaction_blocks=True)
await asyncio.sleep(0.5)
Expand Down
93 changes: 53 additions & 40 deletions chia/_tests/wallet/rpc/test_wallet_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,12 @@
from chia.wallet.wallet_protocol import WalletProtocol
from chia.wallet.wallet_request_types import (
AddKey,
CATAssetIDToName,
CATGetAssetID,
CATGetName,
CATSetName,
CheckDeleteKey,
CheckOfferValidity,
ClawbackPuzzleDecoratorOverride,
CombineCoins,
DefaultCAT,
Expand Down Expand Up @@ -144,6 +149,7 @@
PushTX,
RoyaltyAsset,
SelectCoins,
SendNotification,
SendTransaction,
SetWalletResyncOnStartup,
SpendClawbackCoins,
Expand Down Expand Up @@ -1202,25 +1208,23 @@ async def test_cat_endpoints(wallet_environments: WalletTestFramework, wallet_ty
env_0.wallet_states[uint32(env_0.wallet_aliases["cat0"])].balance.to_json_dict().items()
<= (await env_0.rpc_client.get_wallet_balance(GetWalletBalance(cat_0_id))).wallet_balance.to_json_dict().items()
)
asset_id = await env_0.rpc_client.get_cat_asset_id(cat_0_id)
assert (await env_0.rpc_client.get_cat_name(cat_0_id)) == wallet_type.default_wallet_name_for_unknown_cat(
asset_id.hex()
)
await env_0.rpc_client.set_cat_name(cat_0_id, "My cat")
assert (await env_0.rpc_client.get_cat_name(cat_0_id)) == "My cat"
result = await env_0.rpc_client.cat_asset_id_to_name(asset_id)
assert result is not None
wid, name = result
assert wid == cat_0_id
assert name == "My cat"
result = await env_0.rpc_client.cat_asset_id_to_name(bytes32.zeros)
assert result is None
asset_id = (await env_0.rpc_client.get_cat_asset_id(CATGetAssetID(cat_0_id))).asset_id
assert (
await env_0.rpc_client.get_cat_name(CATGetName(cat_0_id))
).name == wallet_type.default_wallet_name_for_unknown_cat(asset_id.hex())
await env_0.rpc_client.set_cat_name(CATSetName(cat_0_id, "My cat"))
assert (await env_0.rpc_client.get_cat_name(CATGetName(cat_0_id))).name == "My cat"
asset_to_name_response = await env_0.rpc_client.cat_asset_id_to_name(CATAssetIDToName(asset_id))
assert asset_to_name_response.wallet_id == cat_0_id
assert asset_to_name_response.name == "My cat"
asset_to_name_response = await env_0.rpc_client.cat_asset_id_to_name(CATAssetIDToName(bytes32.zeros))
assert asset_to_name_response.name is None
verified_asset_id = next(iter(DEFAULT_CATS.items()))[1]["asset_id"]
result = await env_0.rpc_client.cat_asset_id_to_name(bytes32.from_hexstr(verified_asset_id))
assert result is not None
should_be_none, name = result
assert should_be_none is None
assert name == next(iter(DEFAULT_CATS.items()))[1]["name"]
asset_to_name_response = await env_0.rpc_client.cat_asset_id_to_name(
CATAssetIDToName(bytes32.from_hexstr(verified_asset_id))
)
assert asset_to_name_response.wallet_id is None
assert asset_to_name_response.name == next(iter(DEFAULT_CATS.items()))[1]["name"]

# Creates a second wallet with the same CAT
res = await env_1.rpc_client.create_wallet_for_existing_cat(asset_id)
Expand Down Expand Up @@ -1394,8 +1398,8 @@ async def test_cat_endpoints(wallet_environments: WalletTestFramework, wallet_ty
await env_0.wallet_state_manager.interested_store.add_unacknowledged_token(
asset_id, "Unknown", uint32(10000), bytes32(b"\00" * 32)
)
cats = await env_0.rpc_client.get_stray_cats()
assert len(cats) == 1
stray_cats_response = await env_0.rpc_client.get_stray_cats()
assert len(stray_cats_response.stray_cats) == 1

# Test CAT coin selection
select_coins_response = await env_0.rpc_client.select_coins(
Expand Down Expand Up @@ -1563,8 +1567,9 @@ async def test_offer_endpoints(wallet_environments: WalletTestFramework, wallet_
}
assert advanced_summary == summary

id, _valid = await env_1.rpc_client.check_offer_validity(offer)
assert id == offer.name()
offer_validity_response = await env_1.rpc_client.check_offer_validity(CheckOfferValidity(offer.to_bech32()))
assert offer_validity_response.id == offer.name()
assert offer_validity_response.valid

all_offers = await env_1.rpc_client.get_all_offers(file_contents=True)
assert len(all_offers) == 1
Expand Down Expand Up @@ -2621,21 +2626,25 @@ async def test_notification_rpcs(wallet_rpc_environment: WalletRpcTestEnvironmen
env.wallet_2.node.config["enable_notifications"] = True
env.wallet_2.node.config["required_notification_amount"] = 100000000000
async with wallet_2.wallet_state_manager.new_action_scope(DEFAULT_TX_CONFIG, push=True) as action_scope:
tx = await client.send_notification(
await action_scope.get_puzzle_hash(wallet_2.wallet_state_manager),
b"hello",
uint64(100000000000),
fee=uint64(100000000000),
response = await client.send_notification(
SendNotification(
target=(await action_scope.get_puzzle_hash(wallet_2.wallet_state_manager)),
message=b"hello",
amount=uint64(100000000000),
fee=uint64(100000000000),
push=True,
),
tx_config=DEFAULT_TX_CONFIG,
)

assert tx.spend_bundle is not None
assert response.tx.spend_bundle is not None
await time_out_assert(
5,
full_node_api.full_node.mempool_manager.get_spendbundle,
tx.spend_bundle,
tx.spend_bundle.name(),
response.tx.spend_bundle,
response.tx.spend_bundle.name(),
)
await farm_transaction(full_node_api, wallet_node, tx.spend_bundle)
await farm_transaction(full_node_api, wallet_node, response.tx.spend_bundle)
await time_out_assert(20, env.wallet_2.wallet.get_confirmed_balance, uint64(100000000000))

notification = (await client_2.get_notifications(GetNotifications())).notifications[0]
Expand All @@ -2648,21 +2657,25 @@ async def test_notification_rpcs(wallet_rpc_environment: WalletRpcTestEnvironmen
assert [] == (await client_2.get_notifications(GetNotifications([notification.id]))).notifications

async with wallet_2.wallet_state_manager.new_action_scope(DEFAULT_TX_CONFIG, push=True) as action_scope:
tx = await client.send_notification(
await action_scope.get_puzzle_hash(wallet_2.wallet_state_manager),
b"hello",
uint64(100000000000),
fee=uint64(100000000000),
response = await client.send_notification(
SendNotification(
target=(await action_scope.get_puzzle_hash(wallet_2.wallet_state_manager)),
message=b"hello",
amount=uint64(100000000000),
fee=uint64(100000000000),
push=True,
),
tx_config=DEFAULT_TX_CONFIG,
)

assert tx.spend_bundle is not None
assert response.tx.spend_bundle is not None
await time_out_assert(
5,
full_node_api.full_node.mempool_manager.get_spendbundle,
tx.spend_bundle,
tx.spend_bundle.name(),
response.tx.spend_bundle,
response.tx.spend_bundle.name(),
)
await farm_transaction(full_node_api, wallet_node, tx.spend_bundle)
await farm_transaction(full_node_api, wallet_node, response.tx.spend_bundle)
await time_out_assert(20, env.wallet_2.wallet.get_confirmed_balance, uint64(200000000000))

notification = (await client_2.get_notifications(GetNotifications())).notifications[0]
Expand Down
Loading
Loading