Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate eth tx test #986

Merged
merged 3 commits into from
Sep 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 153 additions & 25 deletions crates/evm/src/backend/validation.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ 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 = kakarot_state.eth_chain_id();
Expand Down Expand Up @@ -64,7 +62,9 @@ mod tests {
use core::num::traits::Bounded;
use core::ops::SnapshotDeref;

use core::starknet::ContractAddress;
use core::starknet::storage::StorageTrait;
use evm::gas;
use snforge_std::cheatcodes::storage::store_felt252;
use snforge_std::{
start_mock_call, test_address, start_cheat_chain_id_global, store,
Expand All @@ -74,9 +74,10 @@ mod tests {
use utils::constants::BLOCK_GAS_LIMIT;
use utils::eth_transaction::common::TxKind;
use utils::eth_transaction::eip1559::TxEip1559;
use utils::eth_transaction::legacy::TxLegacy;
use utils::eth_transaction::transaction::Transaction;

fn set_up() -> KakarotCore::ContractState {
fn set_up() -> (KakarotCore::ContractState, ContractAddress) {
// 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();
Expand All @@ -102,13 +103,13 @@ mod tests {
native_token_address, selector!("balanceOf"), Bounded::<u256>::MAX
); // Min to pay for gas + value

kakarot_state
(kakarot_state, native_token_address)
}

#[test]
fn test_validate_eth_tx_typical_case() {
// Setup the environment
let kakarot_state = set_up();
let (kakarot_state, _) = set_up();

// Create a transaction object for the test
let tx = Transaction::Eip1559(
Expand All @@ -132,51 +133,178 @@ mod tests {
}

#[test]
#[ignore]
#[should_panic(expected: ('Invalid chain id',))]
fn test_validate_eth_tx_invalid_chain_id() {
panic!("unimplemented");
// Setup the environment
let (kakarot_state, _) = set_up();

// Create a transaction object for the test
let tx = Transaction::Eip1559(
TxEip1559 {
chain_id: 600, // wrong chain_id
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
validate_eth_tx(@kakarot_state, tx);
}

#[test]
#[ignore]
#[should_panic(expected: ('Invalid nonce',))]
fn test_validate_eth_tx_invalid_nonce() {
panic!("unimplemented");
// 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: 600, //Invalid nonce
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
validate_eth_tx(@kakarot_state, tx);
}

#[test]
#[ignore]
#[should_panic(expected: ('Tx gas > Block gas',))]
fn test_validate_eth_tx_gas_limit_exceeds_block_gas_limit() {
panic!("unimplemented");
// 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: BLOCK_GAS_LIMIT + 1, // Gas limit greater than block gas limit
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
validate_eth_tx(@kakarot_state, tx);
}

#[test]
#[ignore]
#[should_panic(expected: ('Intrinsic gas > gas limit',))]
fn test_validate_eth_tx_intrinsic_gas_exceeds_gas_limit() {
panic!("unimplemented");
// Setup the environment
let (kakarot_state, _) = set_up();

// Create a transaction object for the test
let mut 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: 0,
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
validate_eth_tx(@kakarot_state, tx);
}

#[test]
#[ignore]
#[should_panic(expected: ('Not enough ETH',))]
fn test_validate_eth_tx_insufficient_balance() {
panic!("unimplemented");
}
// Setup the environment
let (kakarot_state, native_token_address) = set_up();

#[test]
#[ignore]
fn test_validate_eth_tx_effective_gas_price_errors() {
panic!("unimplemented");
// 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(),
}
);

start_mock_call(native_token_address, selector!("balanceOf"), Bounded::<u256>::MIN);

// Test that the function performs validation and assert expected results
validate_eth_tx(@kakarot_state, tx);
}

#[test]
#[ignore]
fn test_validate_eth_tx_max_gas_limit() {
panic!("unimplemented");
// 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: BLOCK_GAS_LIMIT, // Gas limit = Max block gas limit
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 intrinsic_gas = validate_eth_tx(@kakarot_state, tx);
assert_eq!(intrinsic_gas, 21000); // Standard intrinsic gas for a simple transfer
}

#[test]
#[ignore]
fn test_validate_eth_tx_pre_eip155() {
//TODO: implement pre-eip155 logic
panic!("unimplemented");
// Setup the environment
let (kakarot_state, _) = set_up();

// Create a transaction object for the test
let tx = Transaction::Legacy(
TxLegacy {
chain_id: Option::None,
nonce: 0,
gas_price: 120000000000000000000000000,
gas_limit: 21000,
to: TxKind::Call(0x1234567890123456789012345678901234567890.try_into().unwrap()),
value: 1000000000000000000_u256, // Standard gas limit for a simple transfer
input: array![].span(),
}
);

// Test that the function performs validation and assert expected results
let intrinsic_gas = validate_eth_tx(@kakarot_state, tx);

assert_eq!(intrinsic_gas, 21000); // Standard intrinsic gas for a simple transfer
}
}
Loading