@@ -16,28 +16,25 @@ struct ExpiringSum {
1616
1717impl ExpiringSum {
1818 fn get_sum ( & mut self , duration : & Duration ) -> u128 {
19- let now = Instant :: now ( ) ;
20- while let Some ( & ( timestamp, value) ) = self . entries . front ( ) {
21- if now. duration_since ( timestamp) >= * duration {
22- self . entries . pop_front ( ) ;
23- self . sum -= value;
24- } else {
25- break ;
26- }
27- }
19+ self . cleanup ( duration) ;
2820 self . sum
2921 }
3022
3123 fn get_count ( & mut self , duration : & Duration ) -> usize {
24+ self . cleanup ( duration) ;
25+ self . entries . len ( )
26+ }
27+
28+ fn cleanup ( & mut self , duration : & Duration ) {
3229 let now = Instant :: now ( ) ;
33- while let Some ( & ( timestamp, _ ) ) = self . entries . front ( ) {
30+ while let Some ( & ( timestamp, value ) ) = self . entries . front ( ) {
3431 if now. duration_since ( timestamp) >= * duration {
3532 self . entries . pop_front ( ) ;
33+ self . sum -= value;
3634 } else {
3735 break ;
3836 }
3937 }
40- self . entries . len ( )
4138 }
4239}
4340
@@ -219,13 +216,13 @@ impl SenderFeeTracker {
219216 pub fn start_rav_request ( & mut self , allocation_id : Address ) {
220217 let current_fee = self . id_to_fee . entry ( allocation_id) . or_default ( ) ;
221218 self . ids_requesting . insert ( allocation_id) ;
222- self . fees_requesting += * current_fee;
219+ self . fees_requesting += current_fee. fee ;
223220 }
224221
225222 /// Should be called before `update`
226223 pub fn finish_rav_request ( & mut self , allocation_id : Address ) {
227224 let current_fee = self . id_to_fee . entry ( allocation_id) . or_default ( ) ;
228- self . fees_requesting -= * current_fee;
225+ self . fees_requesting -= current_fee. fee ;
229226 self . ids_requesting . remove ( & allocation_id) ;
230227 }
231228
@@ -442,4 +439,84 @@ mod tests {
442439 assert_eq ! ( tracker. get_total_fee( ) , 60 ) ;
443440 assert_eq ! ( tracker. get_total_fee_outside_buffer( ) , 60 ) ;
444441 }
442+
443+ #[ test]
444+ fn check_counter_and_fee_outside_buffer_unordered ( ) {
445+ let allocation_id_0 = address ! ( "abababababababababababababababababababab" ) ;
446+ let allocation_id_2 = address ! ( "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd" ) ;
447+
448+ const BUFFER_WINDOW : Duration = Duration :: from_millis ( 20 ) ;
449+ let mut tracker = SenderFeeTracker :: new ( BUFFER_WINDOW ) ;
450+ assert_eq ! ( tracker. get_total_fee_outside_buffer( ) , 0 ) ;
451+ assert_eq ! (
452+ tracker. get_total_counter_outside_buffer_for_allocation( & allocation_id_0) ,
453+ 0
454+ ) ;
455+
456+ tracker. add ( allocation_id_0, 10 ) ;
457+ assert_eq ! ( tracker. get_total_fee_outside_buffer( ) , 0 ) ;
458+ assert_eq ! (
459+ tracker. get_total_counter_outside_buffer_for_allocation( & allocation_id_0) ,
460+ 0
461+ ) ;
462+
463+ sleep ( BUFFER_WINDOW ) ;
464+
465+ assert_eq ! ( tracker. get_total_fee_outside_buffer( ) , 10 ) ;
466+ assert_eq ! (
467+ tracker. get_total_counter_outside_buffer_for_allocation( & allocation_id_0) ,
468+ 1
469+ ) ;
470+
471+ tracker. add ( allocation_id_2, 20 ) ;
472+ assert_eq ! (
473+ tracker. get_total_counter_outside_buffer_for_allocation( & allocation_id_2) ,
474+ 0
475+ ) ;
476+ assert_eq ! ( tracker. get_total_fee_outside_buffer( ) , 10 ) ;
477+
478+ sleep ( BUFFER_WINDOW ) ;
479+
480+ tracker. block_allocation_id ( allocation_id_2) ;
481+ assert_eq ! (
482+ tracker. get_total_counter_outside_buffer_for_allocation( & allocation_id_2) ,
483+ 1
484+ ) ;
485+ assert_eq ! ( tracker. get_total_fee_outside_buffer( ) , 30 ) ;
486+ }
487+
488+ #[ test]
489+ fn check_get_count_updates_sum ( ) {
490+ let allocation_id_0 = address ! ( "abababababababababababababababababababab" ) ;
491+
492+ const BUFFER_WINDOW : Duration = Duration :: from_millis ( 20 ) ;
493+ let mut tracker = SenderFeeTracker :: new ( BUFFER_WINDOW ) ;
494+
495+ tracker. add ( allocation_id_0, 10 ) ;
496+ let expiring_sum = tracker
497+ . buffer_window_fee
498+ . get_mut ( & allocation_id_0)
499+ . expect ( "there should be something here" ) ;
500+ assert_eq ! ( expiring_sum. get_sum( & BUFFER_WINDOW ) , 10 ) ;
501+ assert_eq ! ( expiring_sum. get_count( & BUFFER_WINDOW ) , 1 ) ;
502+
503+ sleep ( BUFFER_WINDOW ) ;
504+
505+ assert_eq ! ( expiring_sum. get_sum( & BUFFER_WINDOW ) , 0 ) ;
506+ assert_eq ! ( expiring_sum. get_count( & BUFFER_WINDOW ) , 0 ) ;
507+
508+ tracker. add ( allocation_id_0, 10 ) ;
509+ let expiring_sum = tracker
510+ . buffer_window_fee
511+ . get_mut ( & allocation_id_0)
512+ . expect ( "there should be something here" ) ;
513+
514+ assert_eq ! ( expiring_sum. get_count( & BUFFER_WINDOW ) , 1 ) ;
515+ assert_eq ! ( expiring_sum. get_sum( & BUFFER_WINDOW ) , 10 ) ;
516+
517+ sleep ( BUFFER_WINDOW ) ;
518+
519+ assert_eq ! ( expiring_sum. get_count( & BUFFER_WINDOW ) , 0 ) ;
520+ assert_eq ! ( expiring_sum. get_sum( & BUFFER_WINDOW ) , 0 ) ;
521+ }
445522}
0 commit comments