Skip to content

Commit

Permalink
dev: test effective gas price (#945)
Browse files Browse the repository at this point in the history
* dev: test effective gas price

* fix test
  • Loading branch information
enitrat committed Sep 16, 2024
1 parent b1f7f8c commit 1aaf113
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 221 deletions.
2 changes: 1 addition & 1 deletion crates/contracts/tests/test_execution_from_outside.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ fn test_execute_from_outside_multicall_not_supported() {

#[test]
#[should_panic(expected: 'EOA: invalid signature')]
fn test_execute_from_outside_inPLACEHOLDER_SIGNATURE() {
fn test_execute_from_outside_invalid_signature() {
let (kakarot_core, contract_account, _) = set_up();

let caller = contract_address_const::<SNIP9_CALLER>();
Expand Down
4 changes: 2 additions & 2 deletions crates/evm/src/backend/validation.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ pub fn validate_eth_tx(kakarot_state: @KakarotCore::ContractState, tx: Transacti

// Validate gas
let gas_limit = tx.gas_limit();
assert(gas_limit <= kakarot_state.get_block_gas_limit(), 'Tx gas > Block gas');
assert(gas_limit <= kakarot_storage.Kakarot_block_gas_limit.read(), 'Tx gas > Block gas');
let block_base_fee = kakarot_storage.Kakarot_base_fee.read();
let effective_gas_price = get_effective_gas_price(
Option::Some(tx.max_fee_per_gas()), tx.max_priority_fee_per_gas(), block_base_fee.into()
tx.max_fee_per_gas(), tx.max_priority_fee_per_gas(), block_base_fee.into()
);
assert!(effective_gas_price.is_ok(), "{:?}", effective_gas_price.unwrap_err());
// Intrinsic Gas
Expand Down
95 changes: 66 additions & 29 deletions crates/utils/src/eth_transaction.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pub mod eip2930;
pub mod legacy;
pub mod transaction;
pub mod tx_type;
pub mod validation;
use core::cmp::min;
use core::num::traits::{CheckedAdd, Zero};
use core::option::OptionTrait;
Expand All @@ -24,35 +23,73 @@ pub struct TransactionMetadata {
/// Get the effective gas price of a transaction as specfified in EIP-1559 with relevant
/// checks.
pub fn get_effective_gas_price(
max_fee_per_gas: Option<u128>, max_priority_fee_per_gas: Option<u128>, block_base_fee: u128,
max_fee_per_gas: u128, max_priority_fee_per_gas: Option<u128>, block_base_fee: u128,
) -> Result<u128, EthTransactionError> {
match max_fee_per_gas {
Option::Some(max_fee) => {
let max_priority_fee_per_gas = max_priority_fee_per_gas.unwrap_or(0);

// only enforce the fee cap if provided input is not zero
if !(max_fee.is_zero() && max_priority_fee_per_gas.is_zero())
&& max_fee < block_base_fee {
// `base_fee_per_gas` is greater than the `max_fee_per_gas`
return Result::Err(EthTransactionError::FeeCapTooLow);
}
if max_fee < max_priority_fee_per_gas {
// `max_priority_fee_per_gas` is greater than the `max_fee_per_gas`
return Result::Err(EthTransactionError::TipAboveFeeCap);
}
Result::Ok(
min(
max_fee,
block_base_fee
.checked_add(max_priority_fee_per_gas)
.ok_or(EthTransactionError::TipVeryHigh)?,
)
)
},
Option::None => Result::Ok(
let max_priority_fee_per_gas = max_priority_fee_per_gas.unwrap_or(0);

if max_fee_per_gas < block_base_fee {
// `base_fee_per_gas` is greater than the `max_fee_per_gas`
return Result::Err(EthTransactionError::FeeCapTooLow);
}
if max_fee_per_gas < max_priority_fee_per_gas {
// `max_priority_fee_per_gas` is greater than the `max_fee_per_gas`
return Result::Err(EthTransactionError::TipAboveFeeCap);
}
Result::Ok(
min(
max_fee_per_gas,
block_base_fee
.checked_add(max_priority_fee_per_gas.unwrap_or(0))
.ok_or(EthTransactionError::TipVeryHigh)?
),
.checked_add(max_priority_fee_per_gas)
.ok_or(EthTransactionError::TipVeryHigh)?,
)
)
}

#[cfg(test)]
mod tests {
use core::num::traits::Bounded;
use super::get_effective_gas_price;
use utils::errors::EthTransactionError;

#[test]
fn test_max_fee_superior_block_fee_should_return_effective_gas_price() {
let result = get_effective_gas_price(100, Option::Some(10), 50);
assert_eq!(result, Result::Ok(60));
}

#[test]
fn test_max_fee_equal_block_fee_plus_priority_fee_should_return_max_fee() {
let result = get_effective_gas_price(100, Option::Some(50), 50);
assert_eq!(result, Result::Ok(100));
}

#[test]
fn test_max_fee_inferior_block_fee_should_err() {
let result = get_effective_gas_price(40, Option::Some(10), 50);
assert_eq!(result, Result::Err(EthTransactionError::FeeCapTooLow));
}

#[test]
fn test_max_fee_inferior_priority_fee_should_err() {
let result = get_effective_gas_price(100, Option::Some(110), 50);
assert_eq!(result, Result::Err(EthTransactionError::TipAboveFeeCap));
}

#[test]
fn test_block_fee_plus_priority_fee_overflow_should_err() {
let result = get_effective_gas_price(Bounded::MAX, Option::Some(1), Bounded::MAX);
assert_eq!(result, Result::Err(EthTransactionError::TipVeryHigh));
}

#[test]
fn test_priority_fee_none_should_use_zero() {
let result = get_effective_gas_price(100, Option::None, 50);
assert_eq!(result, Result::Ok(50));
}

#[test]
fn test_max_fee_equal_block_fee_less_than_total_should_return_max_fee() {
let result = get_effective_gas_price(50, Option::Some(10), 50);
assert_eq!(result, Result::Ok(50));
}
}
189 changes: 0 additions & 189 deletions crates/utils/src/eth_transaction/validation.cairo

This file was deleted.

0 comments on commit 1aaf113

Please sign in to comment.