From b299c8e62be870979578f1a57c4184b55277c0a6 Mon Sep 17 00:00:00 2001 From: Mathieu <60658558+enitrat@users.noreply.github.com> Date: Mon, 16 Sep 2024 10:47:58 +0200 Subject: [PATCH] dev: write test template for validate_eth_tx (#946) * dev: write test template for validate_eth_tx * separate setup logic * fmt --- crates/evm/src/backend/validation.cairo | 126 ++++++++++++++++++++++++ 1 file changed, 126 insertions(+) diff --git a/crates/evm/src/backend/validation.cairo b/crates/evm/src/backend/validation.cairo index 495dc35ee..0c7a4ab3d 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,127 @@ 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::num::traits::Bounded; + 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}; + + fn set_up() -> KakarotCore::ContractState { + // Define the addresses used in the tests, whose calls will be mocked + 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(); + + // Set up the environment + 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()); + + // 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"), Bounded::::MAX + ); // Min to pay for gas + value + + kakarot_state + } + + #[test] + fn test_validate_eth_tx_typical_case() { + // Setup the environment + let kakarot_state = set_up(); + + // Create a transaction object for the test + 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(), + } + ); + + // Test that the function performs validation and assert expected results + 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"); + } +}