Skip to content

Commit cd792f5

Browse files
authored
CHIA-3708 Annotate test_rate_limits.py (#20005)
Annotate test_rate_limits.py.
1 parent a6cde00 commit cd792f5

File tree

2 files changed

+38
-28
lines changed

2 files changed

+38
-28
lines changed

chia/_tests/core/server/test_rate_limits.py

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

33
from dataclasses import dataclass
4-
from typing import Any
4+
from typing import Any, cast
55

66
import pytest
77
from chia_rs.sized_ints import uint32
@@ -19,6 +19,7 @@
1919
from chia.server.server import ChiaServer
2020
from chia.server.ws_connection import WSChiaConnection
2121
from chia.simulator.block_tools import BlockTools
22+
from chia.simulator.full_node_simulator import FullNodeSimulator
2223
from chia.types.peer_info import PeerInfo
2324

2425
rl_v2 = [Capability.BASE, Capability.BLOCK_HEADERS, Capability.RATE_LIMITS_V2]
@@ -39,7 +40,7 @@ def advance(self, duration: float) -> None:
3940

4041

4142
@pytest.mark.anyio
42-
async def test_get_rate_limits_to_use():
43+
async def test_get_rate_limits_to_use() -> None:
4344
assert get_rate_limits_to_use(rl_v2, rl_v2) != get_rate_limits_to_use(rl_v2, rl_v1)
4445
assert get_rate_limits_to_use(rl_v1, rl_v1) == get_rate_limits_to_use(rl_v2, rl_v1)
4546
assert get_rate_limits_to_use(rl_v1, rl_v1) == get_rate_limits_to_use(rl_v1, rl_v2)
@@ -57,7 +58,7 @@ async def test_get_rate_limits_to_use():
5758
@boolean_datacases(name="incoming", true="incoming", false="outgoing")
5859
@boolean_datacases(name="tx_msg", true="tx", false="non-tx")
5960
@boolean_datacases(name="limit_size", true="size-limit", false="count-limit")
60-
async def test_limits_v2(incoming: bool, tx_msg: bool, limit_size: bool, monkeypatch: pytest.MonkeyPatch):
61+
async def test_limits_v2(incoming: bool, tx_msg: bool, limit_size: bool, monkeypatch: pytest.MonkeyPatch) -> None:
6162
# this test uses a single message type, and alters the rate limit settings
6263
# for it to hit the different cases
6364

@@ -96,7 +97,7 @@ async def test_limits_v2(incoming: bool, tx_msg: bool, limit_size: bool, monkeyp
9697
else:
9798
limits.update({"rate_limits_other": rate_limit, "rate_limits_tx": {}})
9899

99-
def mock_get_limits(*args, **kwargs) -> dict[str, Any]:
100+
def mock_get_limits(our_capabilities: list[Capability], peer_capabilities: list[Capability]) -> dict[str, Any]:
100101
return limits
101102

102103
import chia.server.rate_limits
@@ -141,7 +142,7 @@ def mock_get_limits(*args, **kwargs) -> dict[str, Any]:
141142

142143

143144
@pytest.mark.anyio
144-
async def test_large_message():
145+
async def test_large_message() -> None:
145146
# Large tx
146147
small_tx_message = make_msg(ProtocolMessageTypes.respond_transaction, bytes([1] * 500 * 1024))
147148
large_tx_message = make_msg(ProtocolMessageTypes.new_transaction, bytes([1] * 3 * 1024 * 1024))
@@ -162,7 +163,7 @@ async def test_large_message():
162163

163164

164165
@pytest.mark.anyio
165-
async def test_too_much_data():
166+
async def test_too_much_data() -> None:
166167
# Too much data
167168
r = RateLimiter(incoming=True, get_time=lambda: 0)
168169
tx_message = make_msg(ProtocolMessageTypes.respond_transaction, bytes([1] * 500 * 1024))
@@ -190,7 +191,7 @@ async def test_too_much_data():
190191

191192

192193
@pytest.mark.anyio
193-
async def test_non_tx_aggregate_limits():
194+
async def test_non_tx_aggregate_limits() -> None:
194195
# Frequency limits
195196
r = RateLimiter(incoming=True, get_time=lambda: 0)
196197
message_1 = make_msg(ProtocolMessageTypes.coin_state_update, bytes([1] * 32))
@@ -227,7 +228,7 @@ async def test_non_tx_aggregate_limits():
227228

228229

229230
@pytest.mark.anyio
230-
async def test_periodic_reset():
231+
async def test_periodic_reset() -> None:
231232
timer = SimClock()
232233
r = RateLimiter(True, 5, get_time=timer.monotonic)
233234
tx_message = make_msg(ProtocolMessageTypes.respond_transaction, bytes([1] * 500 * 1024))
@@ -261,7 +262,7 @@ async def test_periodic_reset():
261262

262263

263264
@pytest.mark.anyio
264-
async def test_percentage_limits():
265+
async def test_percentage_limits() -> None:
265266
r = RateLimiter(True, 60, 40, get_time=lambda: 0)
266267
new_peak_message = make_msg(ProtocolMessageTypes.new_peak, bytes([1] * 40))
267268
for i in range(50):
@@ -321,7 +322,7 @@ async def test_percentage_limits():
321322

322323

323324
@pytest.mark.anyio
324-
async def test_too_many_outgoing_messages():
325+
async def test_too_many_outgoing_messages() -> None:
325326
# Too many messages
326327
r = RateLimiter(incoming=False, get_time=lambda: 0)
327328
new_peers_message = make_msg(ProtocolMessageTypes.respond_peers, bytes([1]))
@@ -345,7 +346,7 @@ async def test_too_many_outgoing_messages():
345346

346347

347348
@pytest.mark.anyio
348-
async def test_too_many_incoming_messages():
349+
async def test_too_many_incoming_messages() -> None:
349350
# Too many messages
350351
r = RateLimiter(incoming=True, get_time=lambda: 0)
351352
new_peers_message = make_msg(ProtocolMessageTypes.respond_peers, bytes([1]))
@@ -406,7 +407,9 @@ async def test_too_many_incoming_messages():
406407
)
407408
@pytest.mark.anyio
408409
@pytest.mark.limit_consensus_modes(reason="save time")
409-
async def test_different_versions(node_with_params, node_with_params_b, self_hostname):
410+
async def test_different_versions(
411+
node_with_params: FullNodeSimulator, node_with_params_b: FullNodeSimulator, self_hostname: str
412+
) -> None:
410413
node_a = node_with_params
411414
node_b = node_with_params_b
412415

@@ -442,27 +445,31 @@ async def test_different_versions(node_with_params, node_with_params_b, self_hos
442445

443446

444447
@pytest.mark.anyio
445-
async def test_compose():
448+
async def test_compose() -> None:
446449
rl_1 = rl_numbers[1]
447450
rl_2 = rl_numbers[2]
448-
assert ProtocolMessageTypes.respond_children in rl_1["rate_limits_other"]
449-
assert ProtocolMessageTypes.respond_children not in rl_1["rate_limits_tx"]
450-
assert ProtocolMessageTypes.respond_children not in rl_2["rate_limits_other"]
451-
assert ProtocolMessageTypes.respond_children in rl_2["rate_limits_tx"]
452-
453-
assert ProtocolMessageTypes.request_block in rl_1["rate_limits_other"]
454-
assert ProtocolMessageTypes.request_block not in rl_1["rate_limits_tx"]
455-
assert ProtocolMessageTypes.request_block not in rl_2["rate_limits_other"]
456-
assert ProtocolMessageTypes.request_block not in rl_2["rate_limits_tx"]
451+
rl_1_rate_limits_other = cast(dict[ProtocolMessageTypes, RLSettings], rl_1["rate_limits_other"])
452+
rl_2_rate_limits_other = cast(dict[ProtocolMessageTypes, RLSettings], rl_2["rate_limits_other"])
453+
rl_1_rate_limits_tx = cast(dict[ProtocolMessageTypes, RLSettings], rl_1["rate_limits_tx"])
454+
rl_2_rate_limits_tx = cast(dict[ProtocolMessageTypes, RLSettings], rl_2["rate_limits_tx"])
455+
assert ProtocolMessageTypes.respond_children in rl_1_rate_limits_other
456+
assert ProtocolMessageTypes.respond_children not in rl_1_rate_limits_tx
457+
assert ProtocolMessageTypes.respond_children not in rl_2_rate_limits_other
458+
assert ProtocolMessageTypes.respond_children in rl_2_rate_limits_tx
459+
460+
assert ProtocolMessageTypes.request_block in rl_1_rate_limits_other
461+
assert ProtocolMessageTypes.request_block not in rl_1_rate_limits_tx
462+
assert ProtocolMessageTypes.request_block not in rl_2_rate_limits_other
463+
assert ProtocolMessageTypes.request_block not in rl_2_rate_limits_tx
457464

458465
comps = compose_rate_limits(rl_1, rl_2)
459466
# v2 limits are used if present
460467
assert ProtocolMessageTypes.respond_children not in comps["rate_limits_other"]
461468
assert ProtocolMessageTypes.respond_children in comps["rate_limits_tx"]
462469

463470
# Otherwise, fall back to v1
464-
assert ProtocolMessageTypes.request_block in rl_1["rate_limits_other"]
465-
assert ProtocolMessageTypes.request_block not in rl_1["rate_limits_tx"]
471+
assert ProtocolMessageTypes.request_block in rl_1_rate_limits_other
472+
assert ProtocolMessageTypes.request_block not in rl_1_rate_limits_tx
466473

467474

468475
@pytest.mark.anyio
@@ -475,7 +482,7 @@ async def test_compose():
475482
(ProtocolMessageTypes.reject_block, 90),
476483
],
477484
)
478-
async def test_unlimited(msg_type: ProtocolMessageTypes, size: int):
485+
async def test_unlimited(msg_type: ProtocolMessageTypes, size: int) -> None:
479486
r = RateLimiter(incoming=False, get_time=lambda: 0)
480487

481488
message = make_msg(msg_type, bytes([1] * size))
@@ -532,8 +539,12 @@ async def test_unlimited(msg_type: ProtocolMessageTypes, size: int):
532539
indirect=True,
533540
)
534541
async def test_unsolicited_responses(
535-
node_with_params, node_with_params_b, self_hostname: str, msg_type: ProtocolMessageTypes, bt: BlockTools
536-
):
542+
node_with_params: FullNodeSimulator,
543+
node_with_params_b: FullNodeSimulator,
544+
self_hostname: str,
545+
msg_type: ProtocolMessageTypes,
546+
bt: BlockTools,
547+
) -> None:
537548
node_a = node_with_params
538549
node_b = node_with_params_b
539550

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)