1
1
from __future__ import annotations
2
2
3
3
import asyncio
4
+ from typing import cast
4
5
5
6
import pytest
6
7
from chia_rs .sized_ints import uint32
11
12
from chia .protocols .outbound_message import make_msg
12
13
from chia .protocols .protocol_message_types import ProtocolMessageTypes
13
14
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
15
16
from chia .server .rate_limit_numbers import rate_limits as rl_numbers
16
17
from chia .server .rate_limits import RateLimiter
17
18
from chia .server .server import ChiaServer
18
19
from chia .server .ws_connection import WSChiaConnection
19
20
from chia .simulator .block_tools import BlockTools
21
+ from chia .simulator .full_node_simulator import FullNodeSimulator
20
22
from chia .types .peer_info import PeerInfo
21
23
22
24
rl_v2 = [Capability .BASE , Capability .BLOCK_HEADERS , Capability .RATE_LIMITS_V2 ]
27
29
28
30
class TestRateLimits :
29
31
@pytest .mark .anyio
30
- async def test_get_rate_limits_to_use (self ):
32
+ async def test_get_rate_limits_to_use (self ) -> None :
31
33
assert get_rate_limits_to_use (rl_v2 , rl_v2 ) != get_rate_limits_to_use (rl_v2 , rl_v1 )
32
34
assert get_rate_limits_to_use (rl_v1 , rl_v1 ) == get_rate_limits_to_use (rl_v2 , rl_v1 )
33
35
assert get_rate_limits_to_use (rl_v1 , rl_v1 ) == get_rate_limits_to_use (rl_v1 , rl_v2 )
34
36
35
37
@pytest .mark .anyio
36
- async def test_too_many_messages (self ):
38
+ async def test_too_many_messages (self ) -> None :
37
39
# Too many messages
38
40
r = RateLimiter (incoming = True )
39
41
new_tx_message = make_msg (ProtocolMessageTypes .new_transaction , bytes ([1 ] * 40 ))
@@ -61,7 +63,7 @@ async def test_too_many_messages(self):
61
63
assert saw_disconnect
62
64
63
65
@pytest .mark .anyio
64
- async def test_large_message (self ):
66
+ async def test_large_message (self ) -> None :
65
67
# Large tx
66
68
small_tx_message = make_msg (ProtocolMessageTypes .respond_transaction , bytes ([1 ] * 500 * 1024 ))
67
69
large_tx_message = make_msg (ProtocolMessageTypes .new_transaction , bytes ([1 ] * 3 * 1024 * 1024 ))
@@ -81,7 +83,7 @@ async def test_large_message(self):
81
83
assert r .process_msg_and_check (large_blocks_message , rl_v2 , rl_v2 ) is not None
82
84
83
85
@pytest .mark .anyio
84
- async def test_too_much_data (self ):
86
+ async def test_too_much_data (self ) -> None :
85
87
# Too much data
86
88
r = RateLimiter (incoming = True )
87
89
tx_message = make_msg (ProtocolMessageTypes .respond_transaction , bytes ([1 ] * 500 * 1024 ))
@@ -108,7 +110,7 @@ async def test_too_much_data(self):
108
110
assert saw_disconnect
109
111
110
112
@pytest .mark .anyio
111
- async def test_non_tx_aggregate_limits (self ):
113
+ async def test_non_tx_aggregate_limits (self ) -> None :
112
114
# Frequency limits
113
115
r = RateLimiter (incoming = True )
114
116
message_1 = make_msg (ProtocolMessageTypes .coin_state_update , bytes ([1 ] * 32 ))
@@ -144,7 +146,7 @@ async def test_non_tx_aggregate_limits(self):
144
146
assert saw_disconnect
145
147
146
148
@pytest .mark .anyio
147
- async def test_periodic_reset (self ):
149
+ async def test_periodic_reset (self ) -> None :
148
150
r = RateLimiter (True , 5 )
149
151
tx_message = make_msg (ProtocolMessageTypes .respond_transaction , bytes ([1 ] * 500 * 1024 ))
150
152
for i in range (10 ):
@@ -176,7 +178,7 @@ async def test_periodic_reset(self):
176
178
assert r .process_msg_and_check (new_tx_message , rl_v2 , rl_v2 ) is None
177
179
178
180
@pytest .mark .anyio
179
- async def test_percentage_limits (self ):
181
+ async def test_percentage_limits (self ) -> None :
180
182
r = RateLimiter (True , 60 , 40 )
181
183
new_peak_message = make_msg (ProtocolMessageTypes .new_peak , bytes ([1 ] * 40 ))
182
184
for i in range (50 ):
@@ -235,7 +237,7 @@ async def test_percentage_limits(self):
235
237
assert saw_disconnect
236
238
237
239
@pytest .mark .anyio
238
- async def test_too_many_outgoing_messages (self ):
240
+ async def test_too_many_outgoing_messages (self ) -> None :
239
241
# Too many messages
240
242
r = RateLimiter (incoming = False )
241
243
new_peers_message = make_msg (ProtocolMessageTypes .respond_peers , bytes ([1 ]))
@@ -258,7 +260,7 @@ async def test_too_many_outgoing_messages(self):
258
260
assert r .process_msg_and_check (new_signatures_message , rl_v2 , rl_v2 ) is None
259
261
260
262
@pytest .mark .anyio
261
- async def test_too_many_incoming_messages (self ):
263
+ async def test_too_many_incoming_messages (self ) -> None :
262
264
# Too many messages
263
265
r = RateLimiter (incoming = True )
264
266
new_peers_message = make_msg (ProtocolMessageTypes .respond_peers , bytes ([1 ]))
@@ -318,7 +320,9 @@ async def test_too_many_incoming_messages(self):
318
320
)
319
321
@pytest .mark .anyio
320
322
@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 :
322
326
node_a = node_with_params
323
327
node_b = node_with_params_b
324
328
@@ -353,27 +357,31 @@ async def test_different_versions(self, node_with_params, node_with_params_b, se
353
357
assert len (set (test_different_versions_results )) >= 2
354
358
355
359
@pytest .mark .anyio
356
- async def test_compose (self ):
360
+ async def test_compose (self ) -> None :
357
361
rl_1 = rl_numbers [1 ]
358
362
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
368
376
369
377
comps = compose_rate_limits (rl_1 , rl_2 )
370
378
# v2 limits are used if present
371
379
assert ProtocolMessageTypes .respond_children not in comps ["rate_limits_other" ]
372
380
assert ProtocolMessageTypes .respond_children in comps ["rate_limits_tx" ]
373
381
374
382
# 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
377
385
378
386
379
387
@pytest .mark .anyio
@@ -386,7 +394,7 @@ async def test_compose(self):
386
394
(ProtocolMessageTypes .reject_block , 90 ),
387
395
],
388
396
)
389
- async def test_unlimited (msg_type : ProtocolMessageTypes , size : int ):
397
+ async def test_unlimited (msg_type : ProtocolMessageTypes , size : int ) -> None :
390
398
r = RateLimiter (incoming = False )
391
399
392
400
message = make_msg (msg_type , bytes ([1 ] * size ))
@@ -443,8 +451,12 @@ async def test_unlimited(msg_type: ProtocolMessageTypes, size: int):
443
451
indirect = True ,
444
452
)
445
453
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 :
448
460
node_a = node_with_params
449
461
node_b = node_with_params_b
450
462
0 commit comments