Skip to content

Commit

Permalink
Name wallet protocol subscription messages consistently (#19132)
Browse files Browse the repository at this point in the history
* Name wallet protocol subscription messages consistently

* fix changelog

* fix name in full_node_api

* missed a spot
  • Loading branch information
Rigidity authored Jan 14, 2025
1 parent e562cd7 commit 8f484e6
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 57 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ macOS 11 (Big Sur) is deprecated. This release (2.4.0) will be the last release
- Only subscribe to inner wallet puzzle hashes
- Rpc: Fix and test `WalletRpcApi.get_coin_records_by_names`
- Full_node: `uint32.MAXIMUM_EXCLUSIVE` -> `uint32.MAXIMUM`
- Full_node: Don't send duplicates in `register_interest_in_puzzle_hash`
- Full_node: Don't send duplicates in `register_for_ph_updates`
- Wallet: Deduplicate coin states from peers
- Build: include `puzzles` packages (#15508)
- Handle VC syncing exceptions better
Expand Down
60 changes: 30 additions & 30 deletions chia/_tests/wallet/simple_sync/test_simple_sync_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ async def test_subscribe_for_ph(simulator_and_wallet: OldSimulatorsAndWallets, s
junk_ph = bytes32(32 * b"\a")
fake_wallet_peer = fn_server.all_connections[peer_id]
msg = wallet_protocol.RegisterForPhUpdates([zero_ph], uint32(0))
msg_response = await full_node_api.register_interest_in_puzzle_hash(msg, fake_wallet_peer)
msg_response = await full_node_api.register_for_ph_updates(msg, fake_wallet_peer)

assert msg_response.type == ProtocolMessageTypes.respond_to_ph_update.value
assert msg_response.type == ProtocolMessageTypes.respond_to_ph_updates.value
data_response = RespondToCoinUpdates.from_bytes(msg_response.data)
assert data_response.coin_states == []

Expand All @@ -68,8 +68,8 @@ async def test_subscribe_for_ph(simulator_and_wallet: OldSimulatorsAndWallets, s
await full_node_api.farm_new_transaction_block(FarmNewBlockProtocol(zero_ph))

msg = wallet_protocol.RegisterForPhUpdates([zero_ph], uint32(0))
msg_response = await full_node_api.register_interest_in_puzzle_hash(msg, fake_wallet_peer)
assert msg_response.type == ProtocolMessageTypes.respond_to_ph_update.value
msg_response = await full_node_api.register_for_ph_updates(msg, fake_wallet_peer)
assert msg_response.type == ProtocolMessageTypes.respond_to_ph_updates.value
data_response = RespondToCoinUpdates.from_bytes(msg_response.data)
# we have already subscribed to this puzzle hash, it will be ignored
# we still receive the updates (see below)
Expand Down Expand Up @@ -101,7 +101,7 @@ async def test_subscribe_for_ph(simulator_and_wallet: OldSimulatorsAndWallets, s
# Test subscribing to more coins
one_ph = bytes32(32 * b"\1")
msg = wallet_protocol.RegisterForPhUpdates([one_ph], uint32(0))
msg_response = await full_node_api.register_interest_in_puzzle_hash(msg, fake_wallet_peer)
msg_response = await full_node_api.register_for_ph_updates(msg, fake_wallet_peer)
peak = full_node_api.full_node.blockchain.get_peak()

for i in range(num_blocks):
Expand Down Expand Up @@ -163,8 +163,8 @@ async def test_subscribe_for_ph(simulator_and_wallet: OldSimulatorsAndWallets, s
assert funds == fn_amount

msg_1 = wallet_protocol.RegisterForPhUpdates([puzzle_hash], uint32(0))
msg_response_1 = await full_node_api.register_interest_in_puzzle_hash(msg_1, fake_wallet_peer)
assert msg_response_1.type == ProtocolMessageTypes.respond_to_ph_update.value
msg_response_1 = await full_node_api.register_for_ph_updates(msg_1, fake_wallet_peer)
assert msg_response_1.type == ProtocolMessageTypes.respond_to_ph_updates.value
data_response_1 = RespondToCoinUpdates.from_bytes(msg_response_1.data)
assert len(data_response_1.coin_states) == 2 * num_blocks # 2 per height farmer / pool reward

Expand Down Expand Up @@ -244,9 +244,9 @@ async def test_subscribe_for_coin_id(simulator_and_wallet: OldSimulatorsAndWalle
coin_to_spend = my_coins[0].coin

msg = wallet_protocol.RegisterForCoinUpdates([coin_to_spend.name()], uint32(0))
msg_response = await full_node_api.register_interest_in_coin(msg, fake_wallet_peer)
msg_response = await full_node_api.register_for_coin_updates(msg, fake_wallet_peer)
assert msg_response is not None
assert msg_response.type == ProtocolMessageTypes.respond_to_coin_update.value
assert msg_response.type == ProtocolMessageTypes.respond_to_coin_updates.value
data_response = RespondToCoinUpdates.from_bytes(msg_response.data)
assert data_response.coin_states[0].coin == coin_to_spend

Expand Down Expand Up @@ -286,9 +286,9 @@ async def test_subscribe_for_coin_id(simulator_and_wallet: OldSimulatorsAndWalle
assert added_target is not None

msg = wallet_protocol.RegisterForCoinUpdates([added_target.name()], uint32(0))
msg_response = await full_node_api.register_interest_in_coin(msg, fake_wallet_peer)
msg_response = await full_node_api.register_for_coin_updates(msg, fake_wallet_peer)
assert msg_response is not None
assert msg_response.type == ProtocolMessageTypes.respond_to_coin_update.value
assert msg_response.type == ProtocolMessageTypes.respond_to_coin_updates.value
data_response = RespondToCoinUpdates.from_bytes(msg_response.data)
assert len(data_response.coin_states) == 0

Expand Down Expand Up @@ -338,7 +338,7 @@ async def test_subscribe_for_ph_reorg(simulator_and_wallet: OldSimulatorsAndWall
await full_node_api.farm_new_transaction_block(FarmNewBlockProtocol(zero_ph))

msg = wallet_protocol.RegisterForPhUpdates([puzzle_hash], uint32(0))
msg_response = await full_node_api.register_interest_in_puzzle_hash(msg, fake_wallet_peer)
msg_response = await full_node_api.register_for_ph_updates(msg, fake_wallet_peer)
assert msg_response is not None
await full_node_api.farm_new_transaction_block(FarmNewBlockProtocol(puzzle_hash))

Expand Down Expand Up @@ -425,7 +425,7 @@ async def test_subscribe_for_coin_id_reorg(simulator_and_wallet: OldSimulatorsAn

for coin_rec in coin_records:
msg = wallet_protocol.RegisterForCoinUpdates([coin_rec.name], uint32(0))
msg_response = await full_node_api.register_interest_in_coin(msg, fake_wallet_peer)
msg_response = await full_node_api.register_for_coin_updates(msg, fake_wallet_peer)
assert msg_response is not None

fork_height = uint32(expected_height - num_blocks - 5)
Expand Down Expand Up @@ -481,8 +481,8 @@ async def test_subscribe_for_hint(simulator_and_wallet: OldSimulatorsAndWallets,

fake_wallet_peer = fn_server.all_connections[peer_id]
msg = wallet_protocol.RegisterForPhUpdates([hint], uint32(0))
msg_response = await full_node_api.register_interest_in_puzzle_hash(msg, fake_wallet_peer)
assert msg_response.type == ProtocolMessageTypes.respond_to_ph_update.value
msg_response = await full_node_api.register_for_ph_updates(msg, fake_wallet_peer)
assert msg_response.type == ProtocolMessageTypes.respond_to_ph_updates.value
data_response = RespondToCoinUpdates.from_bytes(msg_response.data)
assert len(data_response.coin_states) == 0

Expand Down Expand Up @@ -512,8 +512,8 @@ async def test_subscribe_for_hint(simulator_and_wallet: OldSimulatorsAndWallets,
assert notified_state.items[0].coin == Coin(coin_spent.name(), hint_puzzle_hash, amount)

msg = wallet_protocol.RegisterForPhUpdates([hint], uint32(0))
msg_response = await full_node_api.register_interest_in_puzzle_hash(msg, fake_wallet_peer)
assert msg_response.type == ProtocolMessageTypes.respond_to_ph_update.value
msg_response = await full_node_api.register_for_ph_updates(msg, fake_wallet_peer)
assert msg_response.type == ProtocolMessageTypes.respond_to_ph_updates.value
response = RespondToCoinUpdates.from_bytes(msg_response.data)
# we have already subscribed to this puzzle hash. The full node will
# ignore the duplicate
Expand Down Expand Up @@ -548,8 +548,8 @@ async def test_subscribe_for_puzzle_hash_coin_hint_duplicates(
await full_node_api.process_spend_bundles(bundles=[tx])
# Query the coin states and make sure it doesn't contain duplicated entries
msg = wallet_protocol.RegisterForPhUpdates([ph], uint32(0))
msg_response = await full_node_api.register_interest_in_puzzle_hash(msg, wallet_connection)
assert msg_response.type == ProtocolMessageTypes.respond_to_ph_update.value
msg_response = await full_node_api.register_for_ph_updates(msg, wallet_connection)
assert msg_response.type == ProtocolMessageTypes.respond_to_ph_updates.value
response = RespondToCoinUpdates.from_bytes(msg_response.data)
assert len(response.coin_states) > 0
assert len(set(response.coin_states)) == len(response.coin_states)
Expand Down Expand Up @@ -586,10 +586,10 @@ async def test_subscribe_for_hint_long_sync(
fake_wallet_peer = fn_server.all_connections[peer_id]
fake_wallet_peer_1 = fn_server_1.all_connections[peer_id_1]
msg = wallet_protocol.RegisterForPhUpdates([hint], uint32(0))
msg_response = await full_node_api.register_interest_in_puzzle_hash(msg, fake_wallet_peer)
await full_node_api_1.register_interest_in_puzzle_hash(msg, fake_wallet_peer_1)
msg_response = await full_node_api.register_for_ph_updates(msg, fake_wallet_peer)
await full_node_api_1.register_for_ph_updates(msg, fake_wallet_peer_1)

assert msg_response.type == ProtocolMessageTypes.respond_to_ph_update.value
assert msg_response.type == ProtocolMessageTypes.respond_to_ph_updates.value
data_response = RespondToCoinUpdates.from_bytes(msg_response.data)
assert len(data_response.coin_states) == 0

Expand Down Expand Up @@ -655,8 +655,8 @@ async def test_ph_subscribe_limits(simulator_and_wallet: OldSimulatorsAndWallets
full_node_api.full_node.config["max_subscribe_items"] = 2
assert full_node_api.is_trusted(con) is False
msg = wallet_protocol.RegisterForPhUpdates(phs, uint32(0))
msg_response = await full_node_api.register_interest_in_puzzle_hash(msg, con)
assert msg_response.type == ProtocolMessageTypes.respond_to_ph_update.value
msg_response = await full_node_api.register_for_ph_updates(msg, con)
assert msg_response.type == ProtocolMessageTypes.respond_to_ph_updates.value
s = full_node_api.full_node.subscriptions
assert s.puzzle_subscription_count() == 2
assert s.has_puzzle_subscription(phs[0])
Expand All @@ -666,8 +666,8 @@ async def test_ph_subscribe_limits(simulator_and_wallet: OldSimulatorsAndWallets
full_node_api.full_node.config["trusted_max_subscribe_items"] = 4
full_node_api.full_node.config["trusted_peers"] = {server_2.node_id.hex(): server_2.node_id.hex()}
assert full_node_api.is_trusted(con) is True
msg_response = await full_node_api.register_interest_in_puzzle_hash(msg, con)
assert msg_response.type == ProtocolMessageTypes.respond_to_ph_update.value
msg_response = await full_node_api.register_for_ph_updates(msg, con)
assert msg_response.type == ProtocolMessageTypes.respond_to_ph_updates.value
assert s.puzzle_subscription_count() == 4
assert s.has_puzzle_subscription(phs[0])
assert s.has_puzzle_subscription(phs[1])
Expand Down Expand Up @@ -696,8 +696,8 @@ async def test_coin_subscribe_limits(simulator_and_wallet: OldSimulatorsAndWalle
full_node_api.full_node.config["max_subscribe_items"] = 2
assert full_node_api.is_trusted(con) is False
msg = wallet_protocol.RegisterForCoinUpdates(coins, uint32(0))
msg_response = await full_node_api.register_interest_in_coin(msg, con)
assert msg_response.type == ProtocolMessageTypes.respond_to_coin_update.value
msg_response = await full_node_api.register_for_coin_updates(msg, con)
assert msg_response.type == ProtocolMessageTypes.respond_to_coin_updates.value
s = full_node_api.full_node.subscriptions
assert s.coin_subscription_count() == 2
assert s.has_coin_subscription(coins[0])
Expand All @@ -707,8 +707,8 @@ async def test_coin_subscribe_limits(simulator_and_wallet: OldSimulatorsAndWalle
full_node_api.full_node.config["trusted_max_subscribe_items"] = 4
full_node_api.full_node.config["trusted_peers"] = {server_2.node_id.hex(): server_2.node_id.hex()}
assert full_node_api.is_trusted(con) is True
msg_response = await full_node_api.register_interest_in_coin(msg, con)
assert msg_response.type == ProtocolMessageTypes.respond_to_coin_update.value
msg_response = await full_node_api.register_for_coin_updates(msg, con)
assert msg_response.type == ProtocolMessageTypes.respond_to_coin_updates.value
assert s.coin_subscription_count() == 4
assert s.has_coin_subscription(coins[0])
assert s.has_coin_subscription(coins[1])
Expand Down
4 changes: 2 additions & 2 deletions chia/_tests/wallet/sync/test_wallet_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -1573,7 +1573,7 @@ async def test_long_sync_untrusted_break(

sync_canceled = False

async def register_interest_in_puzzle_hash(
async def register_for_ph_updates(
self: object,
request: wallet_protocol.RegisterForPhUpdates,
peer: WSChiaConnection,
Expand Down Expand Up @@ -1604,7 +1604,7 @@ def only_trusted_peer() -> bool:

await add_blocks_in_batches(default_1000_blocks[:400], untrusted_full_node_api.full_node)

with patch_request_handler(api=untrusted_full_node_api, handler=register_interest_in_puzzle_hash):
with patch_request_handler(api=untrusted_full_node_api, handler=register_for_ph_updates):
# Connect to the untrusted peer and wait until the long sync started
await wallet_server.start_client(PeerInfo(self_hostname, untrusted_full_node_server.get_port()), None)
await time_out_assert(30, wallet_syncing)
Expand Down
6 changes: 3 additions & 3 deletions chia/_tests/wallet/test_wallet_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,11 +688,11 @@ async def test_wallet_node_bad_coin_state_ignore(

await wallet_server.start_client(PeerInfo(self_hostname, full_node_api.server.get_port()), None)

async def register_interest_in_coin(
async def register_for_coin_updates(
self: Self, request: wallet_protocol.RegisterForCoinUpdates, *, test: bool = False
) -> Optional[Message]:
return make_msg(
ProtocolMessageTypes.respond_to_coin_update,
ProtocolMessageTypes.respond_to_coin_updates,
wallet_protocol.RespondToCoinUpdates(
[], uint32(0), [CoinState(Coin(bytes32.zeros, bytes32.zeros, uint64(0)), uint32(0), uint32(0))]
),
Expand All @@ -704,7 +704,7 @@ async def validate_received_state_from_peer(*args: Any) -> bool:

assert full_node_api.full_node._server is not None
with patch_request_handler(
api=full_node_api.full_node._server.get_connections()[0].api, handler=register_interest_in_coin
api=full_node_api.full_node._server.get_connections()[0].api, handler=register_for_coin_updates
):
monkeypatch.setattr(
wallet_node,
Expand Down
8 changes: 4 additions & 4 deletions chia/full_node/full_node_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1614,7 +1614,7 @@ async def respond_compact_vdf(self, request: full_node_protocol.RespondCompactVD
return None

@metadata.request(peer_required=True)
async def register_interest_in_puzzle_hash(
async def register_for_ph_updates(
self, request: wallet_protocol.RegisterForPhUpdates, peer: WSChiaConnection
) -> Message:
trusted = self.is_trusted(peer)
Expand Down Expand Up @@ -1673,11 +1673,11 @@ async def register_interest_in_puzzle_hash(
)

response = wallet_protocol.RespondToPhUpdates(request.puzzle_hashes, request.min_height, list(states))
msg = make_msg(ProtocolMessageTypes.respond_to_ph_update, response)
msg = make_msg(ProtocolMessageTypes.respond_to_ph_updates, response)
return msg

@metadata.request(peer_required=True)
async def register_interest_in_coin(
async def register_for_coin_updates(
self, request: wallet_protocol.RegisterForCoinUpdates, peer: WSChiaConnection
) -> Message:
max_items = self.max_subscribe_response_items(peer)
Expand All @@ -1693,7 +1693,7 @@ async def register_interest_in_coin(
)

response = wallet_protocol.RespondToCoinUpdates(request.coin_ids, request.min_height, states)
msg = make_msg(ProtocolMessageTypes.respond_to_coin_update, response)
msg = make_msg(ProtocolMessageTypes.respond_to_coin_updates, response)
return msg

@metadata.request()
Expand Down
8 changes: 4 additions & 4 deletions chia/protocols/protocol_message_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ class ProtocolMessageTypes(Enum):

# More wallet protocol
coin_state_update = 69
register_interest_in_puzzle_hash = 70
respond_to_ph_update = 71
register_interest_in_coin = 72
respond_to_coin_update = 73
register_for_ph_updates = 70
respond_to_ph_updates = 71
register_for_coin_updates = 72
respond_to_coin_updates = 73
request_children = 74
respond_children = 75
request_ses_hashes = 76
Expand Down
4 changes: 2 additions & 2 deletions chia/protocols/protocol_state_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
pmt.request_compact_vdf: [pmt.respond_compact_vdf],
pmt.request_peers: [pmt.respond_peers],
pmt.request_header_blocks: [pmt.respond_header_blocks, pmt.reject_header_blocks, pmt.reject_block_headers],
pmt.register_interest_in_puzzle_hash: [pmt.respond_to_ph_update],
pmt.register_interest_in_coin: [pmt.respond_to_coin_update],
pmt.register_for_ph_updates: [pmt.respond_to_ph_updates],
pmt.register_for_coin_updates: [pmt.respond_to_coin_updates],
pmt.request_children: [pmt.respond_children],
pmt.request_ses_hashes: [pmt.respond_ses_hashes],
pmt.request_block_headers: [pmt.respond_block_headers, pmt.reject_block_headers, pmt.reject_header_blocks],
Expand Down
8 changes: 4 additions & 4 deletions chia/server/rate_limit_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ def compose_rate_limits(old_rate_limits: dict[str, Any], new_rate_limits: dict[s
ProtocolMessageTypes.plot_sync_done: RLSettings(1000, 100 * 1024 * 1024),
ProtocolMessageTypes.plot_sync_response: RLSettings(3000, 100 * 1024 * 1024),
ProtocolMessageTypes.coin_state_update: RLSettings(1000, 100 * 1024 * 1024),
ProtocolMessageTypes.register_interest_in_puzzle_hash: RLSettings(1000, 100 * 1024 * 1024),
ProtocolMessageTypes.respond_to_ph_update: RLSettings(1000, 100 * 1024 * 1024),
ProtocolMessageTypes.register_interest_in_coin: RLSettings(1000, 100 * 1024 * 1024),
ProtocolMessageTypes.respond_to_coin_update: RLSettings(1000, 100 * 1024 * 1024),
ProtocolMessageTypes.register_for_ph_updates: RLSettings(1000, 100 * 1024 * 1024),
ProtocolMessageTypes.respond_to_ph_updates: RLSettings(1000, 100 * 1024 * 1024),
ProtocolMessageTypes.register_for_coin_updates: RLSettings(1000, 100 * 1024 * 1024),
ProtocolMessageTypes.respond_to_coin_updates: RLSettings(1000, 100 * 1024 * 1024),
ProtocolMessageTypes.request_remove_puzzle_subscriptions: RLSettings(1000, 100 * 1024 * 1024),
ProtocolMessageTypes.respond_remove_puzzle_subscriptions: RLSettings(1000, 100 * 1024 * 1024),
ProtocolMessageTypes.request_remove_coin_subscriptions: RLSettings(1000, 100 * 1024 * 1024),
Expand Down
8 changes: 4 additions & 4 deletions chia/wallet/util/wallet_sync_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ async def subscribe_to_phs(
"""
msg = RegisterForPhUpdates(puzzle_hashes, uint32(max(min_height, uint32(0))))
all_coins_state: Optional[RespondToPhUpdates] = await peer.call_api(
FullNodeAPI.register_interest_in_puzzle_hash, msg, timeout=300
FullNodeAPI.register_for_ph_updates, msg, timeout=300
)
if all_coins_state is None:
raise ValueError(f"None response from peer {peer.peer_info.host} for register_interest_in_puzzle_hash")
raise ValueError(f"None response from peer {peer.peer_info.host} for register_for_ph_updates")
return all_coins_state.coin_states


Expand All @@ -73,11 +73,11 @@ async def subscribe_to_coin_updates(
"""
msg = RegisterForCoinUpdates(coin_names, uint32(max(0, min_height)))
all_coins_state: Optional[RespondToCoinUpdates] = await peer.call_api(
FullNodeAPI.register_interest_in_coin, msg, timeout=300
FullNodeAPI.register_for_coin_updates, msg, timeout=300
)

if all_coins_state is None:
raise ValueError(f"None response from peer {peer.peer_info.host} for register_interest_in_coin")
raise ValueError(f"None response from peer {peer.peer_info.host} for register_for_coin_updates")
return all_coins_state.coin_states


Expand Down
2 changes: 1 addition & 1 deletion chia/wallet/wallet_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -1683,7 +1683,7 @@ async def get_coin_state(
self, coin_names: list[bytes32], peer: WSChiaConnection, fork_height: Optional[uint32] = None
) -> list[CoinState]:
msg = RegisterForCoinUpdates(coin_names, uint32(0))
coin_state: Optional[RespondToCoinUpdates] = await peer.call_api(FullNodeAPI.register_interest_in_coin, msg)
coin_state: Optional[RespondToCoinUpdates] = await peer.call_api(FullNodeAPI.register_for_coin_updates, msg)
if coin_state is None or not isinstance(coin_state, RespondToCoinUpdates):
raise PeerRequestException(f"Was not able to get states for {coin_names}")

Expand Down
Loading

0 comments on commit 8f484e6

Please sign in to comment.