Skip to content

Commit 9d71f28

Browse files
authored
[CHIA-3603] Port send_notifications to @marshal (#19987)
* Port `spend_clawback_coins` to `@marshal` * fix tests * fix test again * how do I keep missing these? * Port `select_coins` to `@marshal` * fix tests * fix test * Port `get_spendable_coins` * Port `get_coin_records_by_names` * Port `send_notifications` to `@marshal` * Fixing the thing that is always broken * Remove accidental import
1 parent f945e8c commit 9d71f28

File tree

6 files changed

+95
-68
lines changed

6 files changed

+95
-68
lines changed

chia/_tests/cmds/wallet/test_notifications.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
from __future__ import annotations
22

33
from pathlib import Path
4-
from typing import cast
54

65
from chia_rs.sized_bytes import bytes32
76
from chia_rs.sized_ints import uint32, uint64
87

98
from chia._tests.cmds.cmd_test_utils import TestRpcClients, TestWalletRpcClient, logType, run_cli_command_and_assert
10-
from chia._tests.cmds.wallet.test_consts import FINGERPRINT, FINGERPRINT_ARG, get_bytes32
9+
from chia._tests.cmds.wallet.test_consts import FINGERPRINT, FINGERPRINT_ARG, STD_TX, STD_UTX, get_bytes32
1110
from chia.util.bech32m import encode_puzzle_hash
12-
from chia.wallet.conditions import ConditionValidTimes
11+
from chia.wallet.conditions import Condition, ConditionValidTimes
1312
from chia.wallet.notification_store import Notification
14-
from chia.wallet.transaction_record import TransactionRecord
15-
from chia.wallet.wallet_request_types import DeleteNotifications, GetNotifications, GetNotificationsResponse
13+
from chia.wallet.util.tx_config import TXConfig
14+
from chia.wallet.wallet_request_types import (
15+
DeleteNotifications,
16+
GetNotifications,
17+
GetNotificationsResponse,
18+
SendNotification,
19+
SendNotificationResponse,
20+
)
1621

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

@@ -26,20 +31,17 @@ def test_notifications_send(capsys: object, get_test_cli_clients: tuple[TestRpcC
2631
class NotificationsSendRpcClient(TestWalletRpcClient):
2732
async def send_notification(
2833
self,
29-
target: bytes32,
30-
msg: bytes,
31-
amount: uint64,
32-
fee: uint64 = uint64(0),
33-
push: bool = True,
34+
request: SendNotification,
35+
tx_config: TXConfig,
36+
extra_conditions: tuple[Condition, ...] = tuple(),
3437
timelock_info: ConditionValidTimes = ConditionValidTimes(),
35-
) -> TransactionRecord:
36-
self.add_to_log("send_notification", (target, msg, amount, fee, push, timelock_info))
37-
38-
class FakeTransactionRecord:
39-
def __init__(self, name: str) -> None:
40-
self.name = name
38+
) -> SendNotificationResponse:
39+
self.add_to_log(
40+
"send_notification",
41+
(request.target, request.message, request.amount, request.fee, request.push, timelock_info),
42+
)
4143

42-
return cast(TransactionRecord, FakeTransactionRecord(get_bytes32(2).hex()))
44+
return SendNotificationResponse([STD_UTX], [STD_TX], tx=STD_TX)
4345

4446
inst_rpc_client = NotificationsSendRpcClient()
4547
test_rpc_clients.wallet_rpc_client = inst_rpc_client

chia/_tests/wallet/rpc/test_wallet_rpc.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
PushTX,
145145
RoyaltyAsset,
146146
SelectCoins,
147+
SendNotification,
147148
SendTransaction,
148149
SetWalletResyncOnStartup,
149150
SpendClawbackCoins,
@@ -2621,21 +2622,25 @@ async def test_notification_rpcs(wallet_rpc_environment: WalletRpcTestEnvironmen
26212622
env.wallet_2.node.config["enable_notifications"] = True
26222623
env.wallet_2.node.config["required_notification_amount"] = 100000000000
26232624
async with wallet_2.wallet_state_manager.new_action_scope(DEFAULT_TX_CONFIG, push=True) as action_scope:
2624-
tx = await client.send_notification(
2625-
await action_scope.get_puzzle_hash(wallet_2.wallet_state_manager),
2626-
b"hello",
2627-
uint64(100000000000),
2628-
fee=uint64(100000000000),
2625+
response = await client.send_notification(
2626+
SendNotification(
2627+
target=(await action_scope.get_puzzle_hash(wallet_2.wallet_state_manager)),
2628+
message=b"hello",
2629+
amount=uint64(100000000000),
2630+
fee=uint64(100000000000),
2631+
push=True,
2632+
),
2633+
tx_config=DEFAULT_TX_CONFIG,
26292634
)
26302635

2631-
assert tx.spend_bundle is not None
2636+
assert response.tx.spend_bundle is not None
26322637
await time_out_assert(
26332638
5,
26342639
full_node_api.full_node.mempool_manager.get_spendbundle,
2635-
tx.spend_bundle,
2636-
tx.spend_bundle.name(),
2640+
response.tx.spend_bundle,
2641+
response.tx.spend_bundle.name(),
26372642
)
2638-
await farm_transaction(full_node_api, wallet_node, tx.spend_bundle)
2643+
await farm_transaction(full_node_api, wallet_node, response.tx.spend_bundle)
26392644
await time_out_assert(20, env.wallet_2.wallet.get_confirmed_balance, uint64(100000000000))
26402645

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

26502655
async with wallet_2.wallet_state_manager.new_action_scope(DEFAULT_TX_CONFIG, push=True) as action_scope:
2651-
tx = await client.send_notification(
2652-
await action_scope.get_puzzle_hash(wallet_2.wallet_state_manager),
2653-
b"hello",
2654-
uint64(100000000000),
2655-
fee=uint64(100000000000),
2656+
response = await client.send_notification(
2657+
SendNotification(
2658+
target=(await action_scope.get_puzzle_hash(wallet_2.wallet_state_manager)),
2659+
message=b"hello",
2660+
amount=uint64(100000000000),
2661+
fee=uint64(100000000000),
2662+
push=True,
2663+
),
2664+
tx_config=DEFAULT_TX_CONFIG,
26562665
)
26572666

2658-
assert tx.spend_bundle is not None
2667+
assert response.tx.spend_bundle is not None
26592668
await time_out_assert(
26602669
5,
26612670
full_node_api.full_node.mempool_manager.get_spendbundle,
2662-
tx.spend_bundle,
2663-
tx.spend_bundle.name(),
2671+
response.tx.spend_bundle,
2672+
response.tx.spend_bundle.name(),
26642673
)
2665-
await farm_transaction(full_node_api, wallet_node, tx.spend_bundle)
2674+
await farm_transaction(full_node_api, wallet_node, response.tx.spend_bundle)
26662675
await time_out_assert(20, env.wallet_2.wallet.get_confirmed_balance, uint64(200000000000))
26672676

26682677
notification = (await client_2.get_notifications(GetNotifications())).notifications[0]

chia/cmds/wallet_funcs.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
NFTSetNFTDID,
7575
NFTTransferNFT,
7676
RoyaltyAsset,
77+
SendNotification,
7778
SendTransaction,
7879
SendTransactionResponse,
7980
SignMessageByAddress,
@@ -1576,19 +1577,25 @@ async def send_notification(
15761577
async with get_wallet_client(root_path, wallet_rpc_port, fp) as (wallet_client, fingerprint, _):
15771578
amount: uint64 = cli_amount.convert_amount(units["chia"])
15781579

1579-
tx = await wallet_client.send_notification(
1580-
address.puzzle_hash,
1581-
message,
1582-
amount,
1583-
fee,
1584-
push=push,
1580+
response = await wallet_client.send_notification(
1581+
SendNotification(
1582+
address.puzzle_hash,
1583+
message,
1584+
amount,
1585+
fee=fee,
1586+
push=push,
1587+
),
1588+
tx_config=DEFAULT_TX_CONFIG,
15851589
timelock_info=condition_valid_times,
15861590
)
15871591

15881592
if push:
15891593
print("Notification sent successfully.")
1590-
print(f"To get status, use command: chia wallet get_transaction -f {fingerprint} -tx 0x{tx.name}")
1591-
return [tx]
1594+
print(
1595+
"To get status, use command: chia wallet get_transaction"
1596+
f" -f {fingerprint} -tx 0x{response.transactions[0].name}"
1597+
)
1598+
return response.transactions
15921599

15931600

15941601
async def get_notifications(

chia/wallet/wallet_request_types.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,20 @@ class SpendClawbackCoinsResponse(TransactionEndpointResponse):
12341234
transaction_ids: list[bytes32]
12351235

12361236

1237+
@streamable
1238+
@dataclass(frozen=True)
1239+
class SendNotification(TransactionEndpointRequest):
1240+
target: bytes32 = field(default_factory=default_raise)
1241+
message: bytes = field(default_factory=default_raise)
1242+
amount: uint64 = uint64(0)
1243+
1244+
1245+
@streamable
1246+
@dataclass(frozen=True)
1247+
class SendNotificationResponse(TransactionEndpointResponse):
1248+
tx: TransactionRecord
1249+
1250+
12371251
@streamable
12381252
@dataclass(frozen=True)
12391253
class PushTransactions(TransactionEndpointRequest):

chia/wallet/wallet_rpc_api.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@
248248
PWStatusResponse,
249249
SelectCoins,
250250
SelectCoinsResponse,
251+
SendNotification,
252+
SendNotificationResponse,
251253
SendTransaction,
252254
SendTransactionResponse,
253255
SetWalletResyncOnStartup,
@@ -1937,22 +1939,24 @@ async def delete_notifications(self, request: DeleteNotifications) -> Empty:
19371939
return Empty()
19381940

19391941
@tx_endpoint(push=True)
1942+
@marshal
19401943
async def send_notification(
19411944
self,
1942-
request: dict[str, Any],
1945+
request: SendNotification,
19431946
action_scope: WalletActionScope,
19441947
extra_conditions: tuple[Condition, ...] = tuple(),
1945-
) -> EndpointResult:
1948+
) -> SendNotificationResponse:
19461949
await self.service.wallet_state_manager.notification_manager.send_new_notification(
1947-
bytes32.from_hexstr(request["target"]),
1948-
bytes.fromhex(request["message"]),
1949-
uint64(request["amount"]),
1950+
request.target,
1951+
request.message,
1952+
request.amount,
19501953
action_scope,
1951-
request.get("fee", uint64(0)),
1954+
request.fee,
19521955
extra_conditions=extra_conditions,
19531956
)
19541957

1955-
return {"tx": None, "transactions": None} # tx_endpoint wrapper will take care of this
1958+
# tx_endpoint will take care of these default values
1959+
return SendNotificationResponse([], [], tx=REPLACEABLE_TRANSACTION_RECORD)
19561960

19571961
@marshal
19581962
async def verify_signature(self, request: VerifySignature) -> VerifySignatureResponse:

chia/wallet/wallet_rpc_client.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@
162162
PWStatusResponse,
163163
SelectCoins,
164164
SelectCoinsResponse,
165+
SendNotification,
166+
SendNotificationResponse,
165167
SendTransaction,
166168
SendTransactionMultiResponse,
167169
SendTransactionResponse,
@@ -1088,27 +1090,16 @@ async def delete_notifications(self, request: DeleteNotifications) -> None:
10881090

10891091
async def send_notification(
10901092
self,
1091-
target: bytes32,
1092-
msg: bytes,
1093-
amount: uint64,
1094-
fee: uint64 = uint64(0),
1093+
request: SendNotification,
1094+
tx_config: TXConfig,
10951095
extra_conditions: tuple[Condition, ...] = tuple(),
10961096
timelock_info: ConditionValidTimes = ConditionValidTimes(),
1097-
push: bool = True,
1098-
) -> TransactionRecord:
1099-
response = await self.fetch(
1100-
"send_notification",
1101-
{
1102-
"target": target.hex(),
1103-
"message": msg.hex(),
1104-
"amount": amount,
1105-
"fee": fee,
1106-
"extra_conditions": conditions_to_json_dicts(extra_conditions),
1107-
"push": push,
1108-
**timelock_info.to_json_dict(),
1109-
},
1097+
) -> SendNotificationResponse:
1098+
return SendNotificationResponse.from_json_dict(
1099+
await self.fetch(
1100+
"send_notification", request.json_serialize_for_transport(tx_config, extra_conditions, timelock_info)
1101+
)
11101102
)
1111-
return TransactionRecord.from_json_dict(response["tx"])
11121103

11131104
async def sign_message_by_address(self, request: SignMessageByAddress) -> SignMessageByAddressResponse:
11141105
return SignMessageByAddressResponse.from_json_dict(

0 commit comments

Comments
 (0)