1
1
from __future__ import annotations
2
2
3
3
from dataclasses import dataclass
4
- from typing import Any
4
+ from typing import Any , cast
5
5
6
6
import pytest
7
7
from chia_rs .sized_ints import uint32
19
19
from chia .server .server import ChiaServer
20
20
from chia .server .ws_connection import WSChiaConnection
21
21
from chia .simulator .block_tools import BlockTools
22
+ from chia .simulator .full_node_simulator import FullNodeSimulator
22
23
from chia .types .peer_info import PeerInfo
23
24
24
25
rl_v2 = [Capability .BASE , Capability .BLOCK_HEADERS , Capability .RATE_LIMITS_V2 ]
@@ -39,7 +40,7 @@ def advance(self, duration: float) -> None:
39
40
40
41
41
42
@pytest .mark .anyio
42
- async def test_get_rate_limits_to_use ():
43
+ async def test_get_rate_limits_to_use () -> None :
43
44
assert get_rate_limits_to_use (rl_v2 , rl_v2 ) != get_rate_limits_to_use (rl_v2 , rl_v1 )
44
45
assert get_rate_limits_to_use (rl_v1 , rl_v1 ) == get_rate_limits_to_use (rl_v2 , rl_v1 )
45
46
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():
57
58
@boolean_datacases (name = "incoming" , true = "incoming" , false = "outgoing" )
58
59
@boolean_datacases (name = "tx_msg" , true = "tx" , false = "non-tx" )
59
60
@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 :
61
62
# this test uses a single message type, and alters the rate limit settings
62
63
# for it to hit the different cases
63
64
@@ -96,7 +97,7 @@ async def test_limits_v2(incoming: bool, tx_msg: bool, limit_size: bool, monkeyp
96
97
else :
97
98
limits .update ({"rate_limits_other" : rate_limit , "rate_limits_tx" : {}})
98
99
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 ]:
100
101
return limits
101
102
102
103
import chia .server .rate_limits
@@ -141,7 +142,7 @@ def mock_get_limits(*args, **kwargs) -> dict[str, Any]:
141
142
142
143
143
144
@pytest .mark .anyio
144
- async def test_large_message ():
145
+ async def test_large_message () -> None :
145
146
# Large tx
146
147
small_tx_message = make_msg (ProtocolMessageTypes .respond_transaction , bytes ([1 ] * 500 * 1024 ))
147
148
large_tx_message = make_msg (ProtocolMessageTypes .new_transaction , bytes ([1 ] * 3 * 1024 * 1024 ))
@@ -162,7 +163,7 @@ async def test_large_message():
162
163
163
164
164
165
@pytest .mark .anyio
165
- async def test_too_much_data ():
166
+ async def test_too_much_data () -> None :
166
167
# Too much data
167
168
r = RateLimiter (incoming = True , get_time = lambda : 0 )
168
169
tx_message = make_msg (ProtocolMessageTypes .respond_transaction , bytes ([1 ] * 500 * 1024 ))
@@ -190,7 +191,7 @@ async def test_too_much_data():
190
191
191
192
192
193
@pytest .mark .anyio
193
- async def test_non_tx_aggregate_limits ():
194
+ async def test_non_tx_aggregate_limits () -> None :
194
195
# Frequency limits
195
196
r = RateLimiter (incoming = True , get_time = lambda : 0 )
196
197
message_1 = make_msg (ProtocolMessageTypes .coin_state_update , bytes ([1 ] * 32 ))
@@ -227,7 +228,7 @@ async def test_non_tx_aggregate_limits():
227
228
228
229
229
230
@pytest .mark .anyio
230
- async def test_periodic_reset ():
231
+ async def test_periodic_reset () -> None :
231
232
timer = SimClock ()
232
233
r = RateLimiter (True , 5 , get_time = timer .monotonic )
233
234
tx_message = make_msg (ProtocolMessageTypes .respond_transaction , bytes ([1 ] * 500 * 1024 ))
@@ -261,7 +262,7 @@ async def test_periodic_reset():
261
262
262
263
263
264
@pytest .mark .anyio
264
- async def test_percentage_limits ():
265
+ async def test_percentage_limits () -> None :
265
266
r = RateLimiter (True , 60 , 40 , get_time = lambda : 0 )
266
267
new_peak_message = make_msg (ProtocolMessageTypes .new_peak , bytes ([1 ] * 40 ))
267
268
for i in range (50 ):
@@ -321,7 +322,7 @@ async def test_percentage_limits():
321
322
322
323
323
324
@pytest .mark .anyio
324
- async def test_too_many_outgoing_messages ():
325
+ async def test_too_many_outgoing_messages () -> None :
325
326
# Too many messages
326
327
r = RateLimiter (incoming = False , get_time = lambda : 0 )
327
328
new_peers_message = make_msg (ProtocolMessageTypes .respond_peers , bytes ([1 ]))
@@ -345,7 +346,7 @@ async def test_too_many_outgoing_messages():
345
346
346
347
347
348
@pytest .mark .anyio
348
- async def test_too_many_incoming_messages ():
349
+ async def test_too_many_incoming_messages () -> None :
349
350
# Too many messages
350
351
r = RateLimiter (incoming = True , get_time = lambda : 0 )
351
352
new_peers_message = make_msg (ProtocolMessageTypes .respond_peers , bytes ([1 ]))
@@ -406,7 +407,9 @@ async def test_too_many_incoming_messages():
406
407
)
407
408
@pytest .mark .anyio
408
409
@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 :
410
413
node_a = node_with_params
411
414
node_b = node_with_params_b
412
415
@@ -442,27 +445,31 @@ async def test_different_versions(node_with_params, node_with_params_b, self_hos
442
445
443
446
444
447
@pytest .mark .anyio
445
- async def test_compose ():
448
+ async def test_compose () -> None :
446
449
rl_1 = rl_numbers [1 ]
447
450
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
457
464
458
465
comps = compose_rate_limits (rl_1 , rl_2 )
459
466
# v2 limits are used if present
460
467
assert ProtocolMessageTypes .respond_children not in comps ["rate_limits_other" ]
461
468
assert ProtocolMessageTypes .respond_children in comps ["rate_limits_tx" ]
462
469
463
470
# 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
466
473
467
474
468
475
@pytest .mark .anyio
@@ -475,7 +482,7 @@ async def test_compose():
475
482
(ProtocolMessageTypes .reject_block , 90 ),
476
483
],
477
484
)
478
- async def test_unlimited (msg_type : ProtocolMessageTypes , size : int ):
485
+ async def test_unlimited (msg_type : ProtocolMessageTypes , size : int ) -> None :
479
486
r = RateLimiter (incoming = False , get_time = lambda : 0 )
480
487
481
488
message = make_msg (msg_type , bytes ([1 ] * size ))
@@ -532,8 +539,12 @@ async def test_unlimited(msg_type: ProtocolMessageTypes, size: int):
532
539
indirect = True ,
533
540
)
534
541
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 :
537
548
node_a = node_with_params
538
549
node_b = node_with_params_b
539
550
0 commit comments