Skip to content

Commit 167751b

Browse files
committed
Annotate test_rate_limits.py.
1 parent bec867e commit 167751b

File tree

2 files changed

+38
-27
lines changed

2 files changed

+38
-27
lines changed

chia/_tests/core/server/test_rate_limits.py

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import asyncio
4+
from typing import cast
45

56
import pytest
67
from chia_rs.sized_ints import uint32
@@ -11,12 +12,13 @@
1112
from chia.protocols.outbound_message import make_msg
1213
from chia.protocols.protocol_message_types import ProtocolMessageTypes
1314
from chia.protocols.shared_protocol import Capability
14-
from chia.server.rate_limit_numbers import compose_rate_limits, get_rate_limits_to_use
15+
from chia.server.rate_limit_numbers import RLSettings, compose_rate_limits, get_rate_limits_to_use
1516
from chia.server.rate_limit_numbers import rate_limits as rl_numbers
1617
from chia.server.rate_limits import RateLimiter
1718
from chia.server.server import ChiaServer
1819
from chia.server.ws_connection import WSChiaConnection
1920
from chia.simulator.block_tools import BlockTools
21+
from chia.simulator.full_node_simulator import FullNodeSimulator
2022
from chia.types.peer_info import PeerInfo
2123

2224
rl_v2 = [Capability.BASE, Capability.BLOCK_HEADERS, Capability.RATE_LIMITS_V2]
@@ -27,13 +29,13 @@
2729

2830
class TestRateLimits:
2931
@pytest.mark.anyio
30-
async def test_get_rate_limits_to_use(self):
32+
async def test_get_rate_limits_to_use(self) -> None:
3133
assert get_rate_limits_to_use(rl_v2, rl_v2) != get_rate_limits_to_use(rl_v2, rl_v1)
3234
assert get_rate_limits_to_use(rl_v1, rl_v1) == get_rate_limits_to_use(rl_v2, rl_v1)
3335
assert get_rate_limits_to_use(rl_v1, rl_v1) == get_rate_limits_to_use(rl_v1, rl_v2)
3436

3537
@pytest.mark.anyio
36-
async def test_too_many_messages(self):
38+
async def test_too_many_messages(self) -> None:
3739
# Too many messages
3840
r = RateLimiter(incoming=True)
3941
new_tx_message = make_msg(ProtocolMessageTypes.new_transaction, bytes([1] * 40))
@@ -61,7 +63,7 @@ async def test_too_many_messages(self):
6163
assert saw_disconnect
6264

6365
@pytest.mark.anyio
64-
async def test_large_message(self):
66+
async def test_large_message(self) -> None:
6567
# Large tx
6668
small_tx_message = make_msg(ProtocolMessageTypes.respond_transaction, bytes([1] * 500 * 1024))
6769
large_tx_message = make_msg(ProtocolMessageTypes.new_transaction, bytes([1] * 3 * 1024 * 1024))
@@ -81,7 +83,7 @@ async def test_large_message(self):
8183
assert r.process_msg_and_check(large_blocks_message, rl_v2, rl_v2) is not None
8284

8385
@pytest.mark.anyio
84-
async def test_too_much_data(self):
86+
async def test_too_much_data(self) -> None:
8587
# Too much data
8688
r = RateLimiter(incoming=True)
8789
tx_message = make_msg(ProtocolMessageTypes.respond_transaction, bytes([1] * 500 * 1024))
@@ -108,7 +110,7 @@ async def test_too_much_data(self):
108110
assert saw_disconnect
109111

110112
@pytest.mark.anyio
111-
async def test_non_tx_aggregate_limits(self):
113+
async def test_non_tx_aggregate_limits(self) -> None:
112114
# Frequency limits
113115
r = RateLimiter(incoming=True)
114116
message_1 = make_msg(ProtocolMessageTypes.coin_state_update, bytes([1] * 32))
@@ -144,7 +146,7 @@ async def test_non_tx_aggregate_limits(self):
144146
assert saw_disconnect
145147

146148
@pytest.mark.anyio
147-
async def test_periodic_reset(self):
149+
async def test_periodic_reset(self) -> None:
148150
r = RateLimiter(True, 5)
149151
tx_message = make_msg(ProtocolMessageTypes.respond_transaction, bytes([1] * 500 * 1024))
150152
for i in range(10):
@@ -176,7 +178,7 @@ async def test_periodic_reset(self):
176178
assert r.process_msg_and_check(new_tx_message, rl_v2, rl_v2) is None
177179

178180
@pytest.mark.anyio
179-
async def test_percentage_limits(self):
181+
async def test_percentage_limits(self) -> None:
180182
r = RateLimiter(True, 60, 40)
181183
new_peak_message = make_msg(ProtocolMessageTypes.new_peak, bytes([1] * 40))
182184
for i in range(50):
@@ -235,7 +237,7 @@ async def test_percentage_limits(self):
235237
assert saw_disconnect
236238

237239
@pytest.mark.anyio
238-
async def test_too_many_outgoing_messages(self):
240+
async def test_too_many_outgoing_messages(self) -> None:
239241
# Too many messages
240242
r = RateLimiter(incoming=False)
241243
new_peers_message = make_msg(ProtocolMessageTypes.respond_peers, bytes([1]))
@@ -258,7 +260,7 @@ async def test_too_many_outgoing_messages(self):
258260
assert r.process_msg_and_check(new_signatures_message, rl_v2, rl_v2) is None
259261

260262
@pytest.mark.anyio
261-
async def test_too_many_incoming_messages(self):
263+
async def test_too_many_incoming_messages(self) -> None:
262264
# Too many messages
263265
r = RateLimiter(incoming=True)
264266
new_peers_message = make_msg(ProtocolMessageTypes.respond_peers, bytes([1]))
@@ -318,7 +320,9 @@ async def test_too_many_incoming_messages(self):
318320
)
319321
@pytest.mark.anyio
320322
@pytest.mark.limit_consensus_modes(reason="save time")
321-
async def test_different_versions(self, node_with_params, node_with_params_b, self_hostname):
323+
async def test_different_versions(
324+
self, node_with_params: FullNodeSimulator, node_with_params_b: FullNodeSimulator, self_hostname: str
325+
) -> None:
322326
node_a = node_with_params
323327
node_b = node_with_params_b
324328

@@ -353,27 +357,31 @@ async def test_different_versions(self, node_with_params, node_with_params_b, se
353357
assert len(set(test_different_versions_results)) >= 2
354358

355359
@pytest.mark.anyio
356-
async def test_compose(self):
360+
async def test_compose(self) -> None:
357361
rl_1 = rl_numbers[1]
358362
rl_2 = rl_numbers[2]
359-
assert ProtocolMessageTypes.respond_children in rl_1["rate_limits_other"]
360-
assert ProtocolMessageTypes.respond_children not in rl_1["rate_limits_tx"]
361-
assert ProtocolMessageTypes.respond_children not in rl_2["rate_limits_other"]
362-
assert ProtocolMessageTypes.respond_children in rl_2["rate_limits_tx"]
363-
364-
assert ProtocolMessageTypes.request_block in rl_1["rate_limits_other"]
365-
assert ProtocolMessageTypes.request_block not in rl_1["rate_limits_tx"]
366-
assert ProtocolMessageTypes.request_block not in rl_2["rate_limits_other"]
367-
assert ProtocolMessageTypes.request_block not in rl_2["rate_limits_tx"]
363+
rl_1_rate_limits_other = cast(dict[ProtocolMessageTypes, RLSettings], rl_1["rate_limits_other"])
364+
rl_2_rate_limits_other = cast(dict[ProtocolMessageTypes, RLSettings], rl_2["rate_limits_other"])
365+
rl_1_rate_limits_tx = cast(dict[ProtocolMessageTypes, RLSettings], rl_1["rate_limits_tx"])
366+
rl_2_rate_limits_tx = cast(dict[ProtocolMessageTypes, RLSettings], rl_2["rate_limits_tx"])
367+
assert ProtocolMessageTypes.respond_children in rl_1_rate_limits_other
368+
assert ProtocolMessageTypes.respond_children not in rl_1_rate_limits_tx
369+
assert ProtocolMessageTypes.respond_children not in rl_2_rate_limits_other
370+
assert ProtocolMessageTypes.respond_children in rl_2_rate_limits_tx
371+
372+
assert ProtocolMessageTypes.request_block in rl_1_rate_limits_other
373+
assert ProtocolMessageTypes.request_block not in rl_1_rate_limits_tx
374+
assert ProtocolMessageTypes.request_block not in rl_2_rate_limits_other
375+
assert ProtocolMessageTypes.request_block not in rl_2_rate_limits_tx
368376

369377
comps = compose_rate_limits(rl_1, rl_2)
370378
# v2 limits are used if present
371379
assert ProtocolMessageTypes.respond_children not in comps["rate_limits_other"]
372380
assert ProtocolMessageTypes.respond_children in comps["rate_limits_tx"]
373381

374382
# Otherwise, fall back to v1
375-
assert ProtocolMessageTypes.request_block in rl_1["rate_limits_other"]
376-
assert ProtocolMessageTypes.request_block not in rl_1["rate_limits_tx"]
383+
assert ProtocolMessageTypes.request_block in rl_1_rate_limits_other
384+
assert ProtocolMessageTypes.request_block not in rl_1_rate_limits_tx
377385

378386

379387
@pytest.mark.anyio
@@ -386,7 +394,7 @@ async def test_compose(self):
386394
(ProtocolMessageTypes.reject_block, 90),
387395
],
388396
)
389-
async def test_unlimited(msg_type: ProtocolMessageTypes, size: int):
397+
async def test_unlimited(msg_type: ProtocolMessageTypes, size: int) -> None:
390398
r = RateLimiter(incoming=False)
391399

392400
message = make_msg(msg_type, bytes([1] * size))
@@ -443,8 +451,12 @@ async def test_unlimited(msg_type: ProtocolMessageTypes, size: int):
443451
indirect=True,
444452
)
445453
async def test_unsolicited_responses(
446-
node_with_params, node_with_params_b, self_hostname: str, msg_type: ProtocolMessageTypes, bt: BlockTools
447-
):
454+
node_with_params: FullNodeSimulator,
455+
node_with_params_b: FullNodeSimulator,
456+
self_hostname: str,
457+
msg_type: ProtocolMessageTypes,
458+
bt: BlockTools,
459+
) -> None:
448460
node_a = node_with_params
449461
node_b = node_with_params_b
450462

mypy-exclusions.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ chia._tests.core.full_node.test_address_manager
5858
chia._tests.core.full_node.test_node_load
5959
chia._tests.core.full_node.test_performance
6060
chia._tests.core.full_node.test_transactions
61-
chia._tests.core.server.test_rate_limits
6261
chia._tests.core.ssl.test_ssl
6362
chia._tests.core.test_crawler_rpc
6463
chia._tests.core.test_daemon_rpc

0 commit comments

Comments
 (0)