Skip to content

Commit f9cee0b

Browse files
committed
review comments
1 parent 3e88de7 commit f9cee0b

File tree

4 files changed

+44
-15
lines changed

4 files changed

+44
-15
lines changed

chia/_tests/core/server/test_rate_limits.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ async def test_limits_v2(incoming: bool, tx_msg: bool, limit_size: bool, monkeyp
6868
limits: dict[ProtocolMessageTypes, Union[RLSettings, Unlimited]]
6969

7070
if limit_size:
71-
agg_limit = RLSettings(False, count * 2, 1024, count * len(message_data))
71+
agg_limit = RLSettings(False, count * 2, len(message_data), count * len(message_data))
7272
else:
73-
agg_limit = RLSettings(False, count, 1024, count * 2 * len(message_data))
73+
agg_limit = RLSettings(False, count, len(message_data), count * 2 * len(message_data))
7474

7575
if limit_size:
76-
limits = {msg_type: RLSettings(not tx_msg, count * 2, 1024, count * len(message_data))}
76+
limits = {msg_type: RLSettings(not tx_msg, count * 2, len(message_data), count * len(message_data))}
7777
else:
78-
limits = {msg_type: RLSettings(not tx_msg, count, 1024, count * 2 * len(message_data))}
78+
limits = {msg_type: RLSettings(not tx_msg, count, len(message_data), count * 2 * len(message_data))}
7979

8080
def mock_get_limits(
8181
our_capabilities: list[Capability], peer_capabilities: list[Capability]

chia/_tests/util/test_network_protocol_test.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import inspect
55
from typing import Any, cast
66

7+
import pytest
8+
79
from chia.protocols import (
810
farmer_protocol,
911
full_node_protocol,
@@ -264,3 +266,19 @@ def test_missing_messages() -> None:
264266
assert types_in_module(shared_protocol) == shared_msgs, (
265267
f"message types were added or removed from shared_protocol. {STANDARD_ADVICE}"
266268
)
269+
270+
271+
@pytest.mark.parametrize("version", [1, 2])
272+
def test_rate_limits_complete(version: int) -> None:
273+
from chia.protocols.protocol_message_types import ProtocolMessageTypes
274+
from chia.server.rate_limit_numbers import rate_limits
275+
276+
if version == 1:
277+
composed = rate_limits[1]
278+
elif version == 2:
279+
composed = {
280+
**rate_limits[1],
281+
**rate_limits[2],
282+
}
283+
284+
assert set(composed.keys()) == set(ProtocolMessageTypes)

chia/server/rate_limit_numbers.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# All of these rate limits scale with the number of transactions so the aggregate amounts are higher
22
from __future__ import annotations
33

4-
import copy
54
import dataclasses
65
from typing import Optional, Union
76

@@ -33,7 +32,13 @@ class Unlimited:
3332
max_size: int # Max size of each request
3433

3534

36-
aggregate_limit = RLSettings(False, 1000, 100 * 1024 * 1024 * 1024, 100 * 1024 * 1024 * 1024)
35+
# for the aggregate limit, not all fields of RLSettings are used. Only "frequency" and "max_total_size"
36+
aggregate_limit = RLSettings(
37+
aggregate_limit=False,
38+
frequency=1000,
39+
max_size=0,
40+
max_total_size=100 * 1024 * 1024,
41+
)
3742

3843

3944
def get_rate_limits_to_use(
@@ -46,8 +51,10 @@ def get_rate_limits_to_use(
4651
# Use V2 rate limits
4752
if 2 in compose_rate_limits_cache:
4853
return compose_rate_limits_cache[2], aggregate_limit
49-
composed = copy.copy(rate_limits[1])
50-
composed.update(rate_limits[2])
54+
composed = {
55+
**rate_limits[1],
56+
**rate_limits[2],
57+
}
5158
compose_rate_limits_cache[2] = composed
5259
return composed, aggregate_limit
5360
else:
@@ -111,10 +118,14 @@ def get_rate_limits_to_use(
111118
ProtocolMessageTypes.request_puzzle_solution: RLSettings(True, 1000, 100),
112119
ProtocolMessageTypes.respond_puzzle_solution: RLSettings(True, 1000, 1024 * 1024),
113120
ProtocolMessageTypes.reject_puzzle_solution: RLSettings(True, 1000, 100),
121+
ProtocolMessageTypes.none_response: RLSettings(False, 500, 100),
114122
ProtocolMessageTypes.new_peak_wallet: RLSettings(True, 200, 300),
115123
ProtocolMessageTypes.request_block_header: RLSettings(True, 500, 100),
116124
ProtocolMessageTypes.respond_block_header: RLSettings(True, 500, 500 * 1024),
117125
ProtocolMessageTypes.reject_header_request: RLSettings(True, 500, 100),
126+
ProtocolMessageTypes.request_block_headers: RLSettings(False, 5000, 100),
127+
ProtocolMessageTypes.reject_block_headers: RLSettings(False, 1000, 100),
128+
ProtocolMessageTypes.respond_block_headers: RLSettings(False, 5000, 2 * 1024 * 1024),
118129
ProtocolMessageTypes.request_removals: RLSettings(True, 500, 50 * 1024, 10 * 1024 * 1024),
119130
ProtocolMessageTypes.respond_removals: RLSettings(True, 500, 1024 * 1024, 10 * 1024 * 1024),
120131
ProtocolMessageTypes.reject_removals_request: RLSettings(True, 500, 100),
@@ -160,6 +171,12 @@ def get_rate_limits_to_use(
160171
ProtocolMessageTypes.respond_ses_hashes: RLSettings(True, 2000, 1 * 1024 * 1024),
161172
ProtocolMessageTypes.request_children: RLSettings(True, 2000, 1024 * 1024),
162173
ProtocolMessageTypes.respond_children: RLSettings(True, 2000, 1 * 1024 * 1024),
174+
ProtocolMessageTypes.error: RLSettings(False, 50000, 100),
175+
ProtocolMessageTypes.request_fee_estimates: RLSettings(True, 10, 100),
176+
ProtocolMessageTypes.respond_fee_estimates: RLSettings(True, 10, 100),
177+
ProtocolMessageTypes.solve: RLSettings(False, 120, 1024),
178+
ProtocolMessageTypes.solution_response: RLSettings(False, 120, 1024),
179+
ProtocolMessageTypes.partial_proofs: RLSettings(False, 120, 3 * 1024),
163180
},
164181
2: {
165182
ProtocolMessageTypes.request_block_header: RLSettings(False, 500, 100),
@@ -173,18 +190,13 @@ def get_rate_limits_to_use(
173190
ProtocolMessageTypes.reject_additions_request: RLSettings(False, 500, 100),
174191
ProtocolMessageTypes.reject_header_blocks: RLSettings(False, 1000, 100),
175192
ProtocolMessageTypes.respond_header_blocks: RLSettings(False, 5000, 2 * 1024 * 1024),
176-
ProtocolMessageTypes.request_block_headers: RLSettings(False, 5000, 100),
177-
ProtocolMessageTypes.reject_block_headers: RLSettings(False, 1000, 100),
178-
ProtocolMessageTypes.respond_block_headers: RLSettings(False, 5000, 2 * 1024 * 1024),
179193
ProtocolMessageTypes.request_ses_hashes: RLSettings(False, 2000, 1 * 1024 * 1024),
180194
ProtocolMessageTypes.respond_ses_hashes: RLSettings(False, 2000, 1 * 1024 * 1024),
181195
ProtocolMessageTypes.request_children: RLSettings(False, 2000, 1024 * 1024),
182196
ProtocolMessageTypes.respond_children: RLSettings(False, 2000, 1 * 1024 * 1024),
183197
ProtocolMessageTypes.request_puzzle_solution: RLSettings(False, 5000, 100),
184198
ProtocolMessageTypes.respond_puzzle_solution: RLSettings(False, 5000, 1024 * 1024),
185199
ProtocolMessageTypes.reject_puzzle_solution: RLSettings(False, 5000, 100),
186-
ProtocolMessageTypes.none_response: RLSettings(False, 500, 100),
187-
ProtocolMessageTypes.error: RLSettings(False, 50000, 100),
188200
# These will have a lower cap since they don't scale with high TPS (NON_TX_FREQ)
189201
ProtocolMessageTypes.request_header_blocks: RLSettings(True, 5000, 100),
190202
},

chia/server/rate_limits.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ def process_msg_and_check(
8484
rate_limits, agg_limit = get_rate_limits_to_use(our_capabilities, peer_capabilities)
8585

8686
try:
87-
limits: Union[RLSettings, Unlimited]
88-
limits = rate_limits[message_type]
87+
limits: Union[RLSettings, Unlimited] = rate_limits[message_type]
8988
if isinstance(limits, RLSettings) and limits.aggregate_limit:
9089
non_tx_freq = agg_limit.frequency
9190
assert agg_limit.max_total_size

0 commit comments

Comments
 (0)