From 3047c7e2024081212514b0bd3304ca37986fe732 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Chabowski?= Date: Tue, 24 Sep 2024 16:50:35 +0200 Subject: [PATCH] Test overflow fix via `update_l2_block_data()` --- .../src/v1/tests/algorithm_v1_tests.rs | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/crates/fuel-gas-price-algorithm/src/v1/tests/algorithm_v1_tests.rs b/crates/fuel-gas-price-algorithm/src/v1/tests/algorithm_v1_tests.rs index ed8aaa332e2..37c7ead8006 100644 --- a/crates/fuel-gas-price-algorithm/src/v1/tests/algorithm_v1_tests.rs +++ b/crates/fuel-gas-price-algorithm/src/v1/tests/algorithm_v1_tests.rs @@ -150,27 +150,37 @@ fn worst_case__same_block_gives_the_same_value_as_calculate() { #[test] fn da_change_should_not_panic() { - let updater = AlgorithmUpdaterV1 { + const HEIGHT: u32 = 0; + + // The following values are chosen to make the `p.saturating_add(d)` result in + // i128::MIN, which would cause a panic if the calculation is not using `saturating_abs()`. + const LAST_PROFIT: i128 = i128::MIN / 2; + const TOTAL_DA_REWARDS_EXCESS: i128 = i128::MAX / 2; + const P: i64 = 1; + const D: i64 = 1; + + let mut updater = AlgorithmUpdaterV1 { new_scaled_exec_price: 0, min_exec_gas_price: 0, exec_gas_price_change_percent: 0, - l2_block_height: 0, + l2_block_height: HEIGHT, l2_block_fullness_threshold_percent: ClampedPercentage::new(0), new_scaled_da_gas_price: 0, gas_price_factor: NonZeroU64::new(1).unwrap(), min_da_gas_price: 0, max_da_gas_price_change_percent: 0, - total_da_rewards_excess: 0, + total_da_rewards_excess: TOTAL_DA_REWARDS_EXCESS as u128, da_recorded_block_height: 0, latest_known_total_da_cost_excess: 0, projected_total_da_cost: 0, - da_p_component: 0, - da_d_component: 0, - last_profit: 0, + da_p_component: P, + da_d_component: D, + last_profit: LAST_PROFIT, second_to_last_profit: 0, latest_da_cost_per_byte: 0, unrecorded_blocks: vec![], }; - updater.da_change(i128::MIN / 2, i128::MIN / 2); + let _ = + updater.update_l2_block_data(HEIGHT + 1, 0, NonZeroU64::new(1).unwrap(), 0, 0); }