File tree Expand file tree Collapse file tree 2 files changed +26
-8
lines changed
crates/fuel-gas-price-algorithm/src Expand file tree Collapse file tree 2 files changed +26
-8
lines changed Original file line number Diff line number Diff line change @@ -18,3 +18,24 @@ pub(crate) fn cumulative_percentage_change(
1818 // `f64` over `u64::MAX` are cast to `u64::MAX`
1919 approx. ceil ( ) as u64
2020}
21+
22+ pub ( crate ) fn safe_signed_abs ( n : i128 ) -> i128 {
23+ let n = if n == i128:: MIN {
24+ n. saturating_add ( 1 )
25+ } else {
26+ n
27+ } ;
28+ debug_assert ! ( n != i128 :: MIN ) ;
29+ n. abs ( )
30+ }
31+
32+ #[ cfg( test) ]
33+ mod tests {
34+ use crate :: utils:: safe_signed_abs;
35+
36+ #[ test]
37+ fn safe_signed_abs_does_not_overflow_on_min_value ( ) {
38+ let abs = safe_signed_abs ( i128:: MIN ) ;
39+ assert_eq ! ( abs, i128 :: MAX ) ;
40+ }
41+ }
Original file line number Diff line number Diff line change @@ -4,7 +4,10 @@ use std::{
44 ops:: Div ,
55} ;
66
7- use crate :: utils:: cumulative_percentage_change;
7+ use crate :: utils:: {
8+ cumulative_percentage_change,
9+ safe_signed_abs,
10+ } ;
811
912#[ cfg( test) ]
1013mod tests;
@@ -325,19 +328,13 @@ impl AlgorithmUpdaterV1 {
325328
326329 fn da_change ( & self , p : i128 , d : i128 ) -> i128 {
327330 let pd_change = p. saturating_add ( d) ;
328- let pd_change = if pd_change == i128:: MIN {
329- pd_change + 1
330- } else {
331- pd_change
332- } ;
333331 let upcast_percent = self . max_da_gas_price_change_percent . into ( ) ;
334332 let max_change = self
335333 . new_scaled_da_gas_price
336334 . saturating_mul ( upcast_percent)
337335 . saturating_div ( 100 )
338336 . into ( ) ;
339- debug_assert ! ( pd_change != i128 :: MIN ) ;
340- let clamped_change = pd_change. abs ( ) . min ( max_change) ;
337+ let clamped_change = safe_signed_abs ( pd_change) . min ( max_change) ;
341338 pd_change. signum ( ) . saturating_mul ( clamped_change)
342339 }
343340
You can’t perform that action at this time.
0 commit comments