@@ -13,7 +13,7 @@ use libp2p::swarm::{
13
13
use libp2p:: swarm:: { FromSwarm , SubstreamProtocol , THandlerInEvent } ;
14
14
use libp2p:: PeerId ;
15
15
use parking_lot:: Mutex ;
16
- use rate_limiter:: { RPCRateLimiter as RateLimiter , RateLimitedErr } ;
16
+ use rate_limiter:: RPCRateLimiter as RateLimiter ;
17
17
use slog:: { crit, debug, o} ;
18
18
use std:: marker:: PhantomData ;
19
19
use std:: sync:: Arc ;
@@ -25,6 +25,7 @@ pub(crate) use handler::{HandlerErr, HandlerEvent};
25
25
pub ( crate ) use methods:: { MetaData , MetaDataV1 , MetaDataV2 , Ping , RPCCodedResponse , RPCResponse } ;
26
26
pub ( crate ) use protocol:: InboundRequest ;
27
27
28
+ use crate :: rpc:: rate_limiter:: InboundRequestSizeLimiter ;
28
29
pub use handler:: SubstreamId ;
29
30
pub use methods:: {
30
31
BlocksByRangeRequest , BlocksByRootRequest , GoodbyeReason , LightClientBootstrapRequest ,
@@ -124,6 +125,8 @@ pub struct RPC<Id: ReqId, E: EthSpec> {
124
125
response_limiter : Option < Arc < Mutex < RateLimiter > > > ,
125
126
/// Rate limiter for our own requests.
126
127
self_limiter : Option < SelfRateLimiter < Id , E > > ,
128
+ /// Limiter for our inbound requests, which checks the request size.
129
+ inbound_request_size_limiter : Option < InboundRequestSizeLimiter > ,
127
130
/// Queue of events to be processed.
128
131
events : Vec < BehaviourAction < Id , E > > ,
129
132
fork_context : Arc < ForkContext > ,
@@ -152,14 +155,19 @@ impl<Id: ReqId, E: EthSpec> RPC<Id, E> {
152
155
// });
153
156
let inbound_limiter = None ; // TODO
154
157
155
- let response_limiter = inbound_rate_limiter_config. map ( |config| {
158
+ let response_limiter = inbound_rate_limiter_config. clone ( ) . map ( |config| {
156
159
debug ! ( log, "Using response rate limiting params" ; "config" => ?config) ;
157
160
Arc :: new ( Mutex :: new (
158
161
RateLimiter :: new_with_config ( config. 0 )
159
162
. expect ( "Inbound limiter configuration parameters are valid" ) ,
160
163
) )
161
164
} ) ;
162
165
166
+ let inbound_request_size_limiter = inbound_rate_limiter_config. map ( |config| {
167
+ InboundRequestSizeLimiter :: new_with_config ( config. 0 )
168
+ . expect ( "Inbound limiter configuration parameters are valid" )
169
+ } ) ;
170
+
163
171
let self_limiter = outbound_rate_limiter_config. map ( |config| {
164
172
SelfRateLimiter :: new ( config, log. clone ( ) ) . expect ( "Configuration parameters are valid" )
165
173
} ) ;
@@ -168,6 +176,7 @@ impl<Id: ReqId, E: EthSpec> RPC<Id, E> {
168
176
limiter : inbound_limiter,
169
177
response_limiter,
170
178
self_limiter,
179
+ inbound_request_size_limiter,
171
180
events : Vec :: new ( ) ,
172
181
fork_context,
173
182
enable_light_client_server,
@@ -315,57 +324,42 @@ where
315
324
) {
316
325
match event {
317
326
HandlerEvent :: Ok ( RPCReceived :: Request ( ref id, ref req) ) => {
318
- if let Some ( limiter) = self . limiter . as_mut ( ) {
319
- // check if the request is conformant to the quota
320
- match limiter. allows ( & peer_id, req) {
321
- Ok ( ( ) ) => {
322
- // send the event to the user
323
- self . events . push ( ToSwarm :: GenerateEvent ( RPCMessage {
324
- peer_id,
325
- conn_id,
326
- event,
327
- } ) )
328
- }
329
- Err ( RateLimitedErr :: TooLarge ) => {
330
- // we set the batch sizes, so this is a coding/config err for most protocols
331
- let protocol = req. versioned_protocol ( ) . protocol ( ) ;
332
- if matches ! (
333
- protocol,
334
- Protocol :: BlocksByRange
335
- | Protocol :: BlobsByRange
336
- | Protocol :: BlocksByRoot
337
- | Protocol :: BlobsByRoot
338
- ) {
339
- debug ! ( self . log, "Request too large to process" ; "request" => %req, "protocol" => %protocol) ;
340
- } else {
341
- // Other protocols shouldn't be sending large messages, we should flag the peer kind
342
- crit ! ( self . log, "Request size too large to ever be processed" ; "protocol" => %protocol) ;
343
- }
344
- // send an error code to the peer.
345
- // the handler upon receiving the error code will send it back to the behaviour
346
- self . send_response (
347
- peer_id,
348
- ( conn_id, * id) ,
349
- RPCCodedResponse :: Error (
350
- RPCResponseErrorCode :: RateLimited ,
351
- "Rate limited. Request too large" . into ( ) ,
352
- ) ,
353
- ) ;
354
- }
355
- Err ( RateLimitedErr :: TooSoon ( wait_time) ) => {
356
- debug ! ( self . log, "Request exceeds the rate limit" ;
357
- "request" => %req, "peer_id" => %peer_id, "wait_time_ms" => wait_time. as_millis( ) ) ;
358
- // send an error code to the peer.
359
- // the handler upon receiving the error code will send it back to the behaviour
360
- self . send_response (
361
- peer_id,
362
- ( conn_id, * id) ,
363
- RPCCodedResponse :: Error (
364
- RPCResponseErrorCode :: RateLimited ,
365
- format ! ( "Wait {:?}" , wait_time) . into ( ) ,
366
- ) ,
367
- ) ;
327
+ // TODO: Send error response if there is ongoing request with the same protocol.
328
+
329
+ if let Some ( limiter) = self . inbound_request_size_limiter . as_ref ( ) {
330
+ // Check if the request is conformant to the quota
331
+ if limiter. allows ( req) {
332
+ // Send the event to the user
333
+ self . events . push ( ToSwarm :: GenerateEvent ( RPCMessage {
334
+ peer_id,
335
+ conn_id,
336
+ event,
337
+ } ) )
338
+ } else {
339
+ // We set the batch sizes, so this is a coding/config err for most protocols
340
+ let protocol = req. versioned_protocol ( ) . protocol ( ) ;
341
+ if matches ! (
342
+ protocol,
343
+ Protocol :: BlocksByRange
344
+ | Protocol :: BlobsByRange
345
+ | Protocol :: BlocksByRoot
346
+ | Protocol :: BlobsByRoot
347
+ ) {
348
+ debug ! ( self . log, "Request too large to process" ; "request" => %req, "protocol" => %protocol) ;
349
+ } else {
350
+ // Other protocols shouldn't be sending large messages, we should flag the peer kind
351
+ crit ! ( self . log, "Request size too large to ever be processed" ; "protocol" => %protocol) ;
368
352
}
353
+ // Send an error code to the peer.
354
+ // The handler upon receiving the error code will send it back to the behaviour
355
+ self . send_response (
356
+ peer_id,
357
+ ( conn_id, * id) ,
358
+ RPCCodedResponse :: Error (
359
+ RPCResponseErrorCode :: RateLimited ,
360
+ "Rate limited. Request too large" . into ( ) ,
361
+ ) ,
362
+ ) ;
369
363
}
370
364
} else {
371
365
// No rate limiting, send the event to the user
0 commit comments