1
- use std:: collections:: VecDeque ;
2
-
3
1
use openmina_core:: { bug_condition, fuzz_maybe, fuzzed_maybe, Substate , SubstateAccess } ;
4
2
5
3
use crate :: {
@@ -10,7 +8,7 @@ use crate::{
10
8
} ;
11
9
12
10
use super :: {
13
- p2p_network_yamux_state:: { YamuxFrameKind , YamuxStreamState , MAX_WINDOW_SIZE } ,
11
+ p2p_network_yamux_state:: { YamuxFrameKind , YamuxStreamState } ,
14
12
P2pNetworkYamuxAction , P2pNetworkYamuxState , YamuxFlags , YamuxPing ,
15
13
} ;
16
14
@@ -286,26 +284,11 @@ impl P2pNetworkYamuxState {
286
284
. entry ( frame. stream_id )
287
285
. or_insert_with ( YamuxStreamState :: incoming) ;
288
286
289
- stream. window_theirs = stream. window_theirs . saturating_add ( difference) ;
290
-
291
- let mut pending_frames = VecDeque :: new ( ) ;
292
-
293
- if difference > 0 {
294
- let mut window = stream. window_theirs ;
295
- while let Some ( frame) = stream. pending . pop_front ( ) {
296
- let len = frame. len_as_u32 ( ) ;
297
- pending_frames. push_back ( frame) ;
298
- if let Some ( new_window) = window. checked_sub ( len) {
299
- window = new_window;
300
- } else {
301
- break ;
302
- }
303
- }
304
- }
287
+ let sendable_frames = stream. update_remote_window ( difference) ;
305
288
306
289
let dispatcher = state_context. into_dispatcher ( ) ;
307
290
308
- while let Some ( frame) = pending_frames . pop_front ( ) {
291
+ for frame in sendable_frames {
309
292
dispatcher. push ( P2pNetworkYamuxAction :: OutgoingFrame { addr, frame } ) ;
310
293
}
311
294
@@ -328,23 +311,15 @@ impl P2pNetworkYamuxState {
328
311
} ;
329
312
match & mut frame. inner {
330
313
YamuxFrameInner :: Data ( _) => {
331
- if let Some ( new_window) =
332
- stream. window_theirs . checked_sub ( frame. len_as_u32 ( ) )
333
- {
334
- // their window is big enough, decrease the size
335
- // and send the whole frame
336
- stream. window_theirs = new_window;
337
- } else {
338
- // their window is not big enough
339
- // split the frame to send as much as you can and put the rest in the queue
314
+ let frame_len = frame. len_as_u32 ( ) ;
315
+ if !stream. try_consume_window ( frame_len) {
316
+ // Window too small, split frame and queue remaining
340
317
if let Some ( remaining) = frame. split_at ( stream. window_theirs as usize ) {
341
318
stream. pending . push_front ( remaining) ;
342
319
}
343
-
344
- // the window will be zero after sending
345
320
stream. window_theirs = 0 ;
346
321
347
- // if size of pending that is above the limit, ignore the peer
322
+ // Check pending queue size limit
348
323
if stream. pending . iter ( ) . map ( YamuxFrame :: len) . sum :: < usize > ( )
349
324
> yamux_state. pending_outgoing_limit
350
325
{
@@ -356,10 +331,7 @@ impl P2pNetworkYamuxState {
356
331
}
357
332
}
358
333
YamuxFrameInner :: WindowUpdate { difference } => {
359
- stream. window_ours = stream. window_ours . saturating_add ( * difference) ;
360
- if stream. window_ours > stream. max_window_size {
361
- stream. max_window_size = stream. window_ours . min ( MAX_WINDOW_SIZE ) ;
362
- }
334
+ stream. update_local_window ( * difference) ;
363
335
}
364
336
_ => { }
365
337
}
0 commit comments