|  | 
|  | 1 | +use alloy_primitives::U256; | 
|  | 2 | + | 
|  | 3 | +/// Ported from op-geth/ethapi/api.go | 
|  | 4 | +/// Check if the transaction fee is reasonable (under the cap). | 
|  | 5 | +/// | 
|  | 6 | +/// # Arguments | 
|  | 7 | +/// | 
|  | 8 | +/// * `gas_price` - The gas price in wei as U256. | 
|  | 9 | +/// * `gas` - The gas amount as U256. | 
|  | 10 | +/// * `cap_in_wei` - The fee cap in wei as U256. | 
|  | 11 | +/// | 
|  | 12 | +/// # Returns | 
|  | 13 | +/// | 
|  | 14 | +/// * `Ok(())` if the fee is under the cap, or if there is no cap. | 
|  | 15 | +/// * `Err(String)` with an error message if the fee exceeds the cap. | 
|  | 16 | +pub fn check_tx_fee(gas_price: U256, gas: U256, cap_in_wei: U256) -> Result<(), String> { | 
|  | 17 | +    // Short circuit if there is no cap for transaction fee at all. | 
|  | 18 | +    if cap_in_wei.is_zero() { | 
|  | 19 | +        return Ok(()); | 
|  | 20 | +    } | 
|  | 21 | + | 
|  | 22 | +    let fee_wei = gas_price | 
|  | 23 | +        .checked_mul(gas) | 
|  | 24 | +        .ok_or_else(|| "fee calculation overflow".to_string())?; | 
|  | 25 | + | 
|  | 26 | +    if fee_wei > cap_in_wei { | 
|  | 27 | +        let gwei = U256::from(1_000_000_000u64); // 1 Gwei = 10^9 Wei | 
|  | 28 | +        let fee_gwei = fee_wei / gwei; | 
|  | 29 | +        let cap_gwei = cap_in_wei / gwei; | 
|  | 30 | +        Err(format!( | 
|  | 31 | +            "tx fee ({} gwei) exceeds the configured cap ({} gwei)", | 
|  | 32 | +            fee_gwei, cap_gwei | 
|  | 33 | +        )) | 
|  | 34 | +    } else { | 
|  | 35 | +        Ok(()) | 
|  | 36 | +    } | 
|  | 37 | +} | 
|  | 38 | + | 
|  | 39 | +#[cfg(test)] | 
|  | 40 | +mod tests { | 
|  | 41 | +    use super::*; | 
|  | 42 | + | 
|  | 43 | +    const GWEI: U256 = U256::from_limbs([1_000_000_000, 0, 0, 0]); | 
|  | 44 | +    const ETHER: U256 = U256::from_limbs([1_000_000_000_000_000_000, 0, 0, 0]); | 
|  | 45 | + | 
|  | 46 | +    #[test] | 
|  | 47 | +    fn test_no_cap() { | 
|  | 48 | +        assert!(check_tx_fee( | 
|  | 49 | +            U256::from(1_000_000_000u64), | 
|  | 50 | +            U256::from(21000u64), | 
|  | 51 | +            U256::ZERO | 
|  | 52 | +        ) | 
|  | 53 | +        .is_ok()); | 
|  | 54 | +    } | 
|  | 55 | + | 
|  | 56 | +    #[test] | 
|  | 57 | +    fn test_under_cap() { | 
|  | 58 | +        assert!(check_tx_fee(U256::from(20_000_000_000u64), U256::from(21000u64), ETHER).is_ok()); | 
|  | 59 | +    } | 
|  | 60 | + | 
|  | 61 | +    #[test] | 
|  | 62 | +    fn test_exact_cap() { | 
|  | 63 | +        let gas_price = U256::from(47619047619u64); // 47.619047619 Gwei | 
|  | 64 | +        let gas = U256::from(21000u64); | 
|  | 65 | +        let cap = gas_price * gas; | 
|  | 66 | +        assert!(check_tx_fee(gas_price, gas, cap).is_ok()); | 
|  | 67 | +    } | 
|  | 68 | + | 
|  | 69 | +    #[test] | 
|  | 70 | +    fn test_over_cap() { | 
|  | 71 | +        let gas_price = U256::from(130u64); | 
|  | 72 | +        let gas = U256::from(20_123_000_123u64); | 
|  | 73 | +        let cap = GWEI * U256::from(1000u64); // 1000 Gwei | 
|  | 74 | +        let result = check_tx_fee(gas_price, gas, cap); | 
|  | 75 | +        assert!(result.is_err()); | 
|  | 76 | +        assert!(result | 
|  | 77 | +            .unwrap_err() | 
|  | 78 | +            .contains("tx fee (2615 gwei) exceeds the configured cap (1000 gwei)")); | 
|  | 79 | +    } | 
|  | 80 | + | 
|  | 81 | +    #[test] | 
|  | 82 | +    fn test_large_values() { | 
|  | 83 | +        let gas_price = U256::MAX; | 
|  | 84 | +        let gas = U256::from(u64::MAX); | 
|  | 85 | +        let cap = U256::MAX; | 
|  | 86 | +        let result = check_tx_fee(gas_price, gas, cap); | 
|  | 87 | +        assert!(result.is_err()); // This will error due to overflow in fee calculation | 
|  | 88 | +    } | 
|  | 89 | + | 
|  | 90 | +    #[test] | 
|  | 91 | +    fn test_small_values() { | 
|  | 92 | +        assert!(check_tx_fee(U256::from(1u64), U256::from(1u64), U256::from(2u64)).is_ok()); | 
|  | 93 | +    } | 
|  | 94 | + | 
|  | 95 | +    #[test] | 
|  | 96 | +    fn test_zero_gas_price() { | 
|  | 97 | +        assert!(check_tx_fee(U256::ZERO, U256::from(21000u64), ETHER).is_ok()); | 
|  | 98 | +    } | 
|  | 99 | + | 
|  | 100 | +    #[test] | 
|  | 101 | +    fn test_zero_gas() { | 
|  | 102 | +        assert!(check_tx_fee(U256::from(20_000_000_000u64), U256::ZERO, ETHER).is_ok()); | 
|  | 103 | +    } | 
|  | 104 | + | 
|  | 105 | +    #[test] | 
|  | 106 | +    fn test_precision() { | 
|  | 107 | +        let gas_price = U256::from(20_000_000_000u64); // 20 Gwei | 
|  | 108 | +        let gas = U256::from(21_000u64); | 
|  | 109 | +        let fee = gas_price * gas; | 
|  | 110 | +        let cap_slightly_over = fee + U256::from(1u64); | 
|  | 111 | +        let cap_slightly_under = fee - U256::from(1u64); | 
|  | 112 | + | 
|  | 113 | +        assert!(check_tx_fee(gas_price, gas, cap_slightly_over).is_ok()); | 
|  | 114 | +        assert!(check_tx_fee(gas_price, gas, cap_slightly_under).is_err()); | 
|  | 115 | +    } | 
|  | 116 | + | 
|  | 117 | +    #[test] | 
|  | 118 | +    fn test_very_small_cap() { | 
|  | 119 | +        assert!(check_tx_fee(U256::from(1u64), U256::from(1u64), U256::from(1u64)).is_ok()); | 
|  | 120 | +    } | 
|  | 121 | + | 
|  | 122 | +    #[test] | 
|  | 123 | +    fn test_very_large_cap() { | 
|  | 124 | +        // This test now uses large but not maximum values to avoid overflow | 
|  | 125 | +        let gas_price = U256::from(u64::MAX); | 
|  | 126 | +        let gas = U256::from(u64::MAX); | 
|  | 127 | +        let cap = U256::MAX; | 
|  | 128 | +        assert!(check_tx_fee(gas_price, gas, cap).is_ok()); // Will not fail | 
|  | 129 | +    } | 
|  | 130 | + | 
|  | 131 | +    #[test] | 
|  | 132 | +    fn test_error_message_formatting() { | 
|  | 133 | +        let gas_price = U256::from(100u64); | 
|  | 134 | +        let gas = U256::from(1_600_000_000u64); | 
|  | 135 | +        let cap = GWEI * U256::from(150u64); // 150 Gwei | 
|  | 136 | +        let result = check_tx_fee(gas_price, gas, cap); | 
|  | 137 | +        assert!(result.is_err()); | 
|  | 138 | +        assert!(result | 
|  | 139 | +            .unwrap_err() | 
|  | 140 | +            .contains("tx fee (160 gwei) exceeds the configured cap (150 gwei)")); | 
|  | 141 | +    } | 
|  | 142 | + | 
|  | 143 | +    #[test] | 
|  | 144 | +    fn test_fee_calculation_overflow() { | 
|  | 145 | +        let gas_price = U256::MAX; | 
|  | 146 | +        let gas = U256::from(2u64); | 
|  | 147 | +        let cap = U256::MAX; | 
|  | 148 | +        let result = check_tx_fee(gas_price, gas, cap); | 
|  | 149 | +        assert!(result.is_err()); | 
|  | 150 | +        assert_eq!(result.unwrap_err(), "fee calculation overflow"); | 
|  | 151 | +    } | 
|  | 152 | +} | 
0 commit comments