diff --git a/crates/evm/src/backend/validation.cairo b/crates/evm/src/backend/validation.cairo index 93c129d28..3e4d3deb8 100644 --- a/crates/evm/src/backend/validation.cairo +++ b/crates/evm/src/backend/validation.cairo @@ -20,6 +20,8 @@ pub fn validate_eth_tx(kakarot_state: @KakarotCore::ContractState, tx: Transacti let kakarot_storage = kakarot_state.snapshot_deref().storage(); // Validate transaction + //TODO: add case for eip155 transactions + // Validate chain_id for post eip155 let tx_chain_id = tx.chain_id(); let kakarot_chain_id: u64 = get_tx_info() @@ -59,3 +61,116 @@ pub fn validate_eth_tx(kakarot_state: @KakarotCore::ContractState, tx: Transacti assert(tx_cost <= balance, 'Not enough ETH'); (effective_gas_price.unwrap(), intrinsic_gas) } + +#[cfg(test)] +mod tests { + use contracts::kakarot_core::KakarotCore; + use core::ops::SnapshotDeref; + + use core::starknet::storage::StorageTrait; + use snforge_std::cheatcodes::storage::store_felt252; + use snforge_std::{ + start_mock_call, test_address, start_cheat_chain_id_global, store, + start_cheat_caller_address, mock_call + }; + use super::validate_eth_tx; + use utils::constants::BLOCK_GAS_LIMIT; + use utils::eth_transaction::common::TxKind; + use utils::eth_transaction::eip1559::TxEip1559; + use utils::eth_transaction::transaction::{Transaction, TransactionTrait}; + + #[test] + fn test_validate_eth_tx_typical_case() { + // Setup the environment + let kakarot_state = KakarotCore::unsafe_new_contract_state(); + let kakarot_storage = kakarot_state.snapshot_deref().storage(); + let kakarot_address = test_address(); + let account_starknet_address = 'account_starknet_address'.try_into().unwrap(); + let native_token_address = 'native_token_address'.try_into().unwrap(); + + start_cheat_chain_id_global(1); + let base_fee_storage = kakarot_storage.Kakarot_base_fee.__base_address__; + let block_gas_limit_storage = kakarot_storage.Kakarot_block_gas_limit.__base_address__; + let native_token_storage_address = kakarot_storage + .Kakarot_native_token_address + .__base_address__; + store_felt252(kakarot_address, base_fee_storage, 1_000_000_000); // 1 Gwei + store_felt252(kakarot_address, block_gas_limit_storage, BLOCK_GAS_LIMIT.into()); + store_felt252(kakarot_address, native_token_storage_address, native_token_address.into()); + + // Create a valid Transaction object + let tx = Transaction::Eip1559( + TxEip1559 { + chain_id: 1, // Should match the chain_id in the environment + nonce: 0, + max_priority_fee_per_gas: 1_000_000_000, // 1 Gwei + max_fee_per_gas: 2_000_000_000, // 2 Gwei + gas_limit: 21000, // Standard gas limit for a simple transfer + to: TxKind::Call(0x1234567890123456789012345678901234567890.try_into().unwrap()), + value: 1000000000000000000_u256, // 1 ETH + input: array![].span(), + access_list: array![].span(), + } + ); + + // Mock the calls to the account contract and the native token contract + start_cheat_caller_address(kakarot_address, account_starknet_address); + start_mock_call(account_starknet_address, selector!("get_nonce"), 0); + start_mock_call(native_token_address, selector!("balanceOf"), 1000000000000000000_u256); + + // Test that the function succeeds and verify returned values + let (effective_gas_price, intrinsic_gas) = validate_eth_tx(@kakarot_state, tx); + + assert_eq!(effective_gas_price, 2_000_000_000); // max_fee_per_gas + assert_eq!(intrinsic_gas, 21000); // Standard intrinsic gas for a simple transfer + } + + #[test] + #[ignore] + fn test_validate_eth_tx_invalid_chain_id() { + panic!("unimplemented"); + } + + #[test] + #[ignore] + fn test_validate_eth_tx_invalid_nonce() { + panic!("unimplemented"); + } + + #[test] + #[ignore] + fn test_validate_eth_tx_gas_limit_exceeds_block_gas_limit() { + panic!("unimplemented"); + } + + #[test] + #[ignore] + fn test_validate_eth_tx_intrinsic_gas_exceeds_gas_limit() { + panic!("unimplemented"); + } + + #[test] + #[ignore] + fn test_validate_eth_tx_insufficient_balance() { + panic!("unimplemented"); + } + + #[test] + #[ignore] + fn test_validate_eth_tx_effective_gas_price_errors() { + panic!("unimplemented"); + } + + #[test] + #[ignore] + fn test_validate_eth_tx_max_gas_limit() { + panic!("unimplemented"); + } + + #[test] + #[ignore] + fn test_validate_eth_tx_pre_eip155() { + //TODO: implement pre-eip155 logic + panic!("unimplemented"); + } +}