Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Commit

Permalink
refactor: move unit tests (#800)
Browse files Browse the repository at this point in the history
* chore: move unit tests to src

* move evm tests to src

* add cfg test attr
  • Loading branch information
enitrat authored Jun 27, 2024
1 parent 57e4446 commit 86fb610
Show file tree
Hide file tree
Showing 80 changed files with 14,079 additions and 13,845 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
scarb 2.6.4
starknet-foundry 0.12.0
starknet-foundry 0.25.0
38 changes: 38 additions & 0 deletions crates/evm/src/backend/starknet_backend.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,41 @@ mod internals {
result
}
}

#[cfg(test)]
mod tests {
use contracts::account_contract::{IAccountDispatcher, IAccountDispatcherTrait};
use contracts::kakarot_core::KakarotCore;
use contracts::test_utils as contract_utils;
use contracts::test_utils::{setup_contracts_for_testing, fund_account_with_native_token};
use evm::backend::starknet_backend;
use evm::errors::EVMErrorTrait;
use evm::test_utils::{chain_id, evm_address, VMBuilderTrait};
use openzeppelin::token::erc20::interface::IERC20CamelDispatcherTrait;
use starknet::testing::{set_contract_address, set_chain_id};


#[test]
fn test_account_deploy() {
let (_, kakarot_core) = setup_contracts_for_testing();

let eoa_address = starknet_backend::deploy(evm_address())
.expect('deployment of EOA failed');

let event = contract_utils::pop_log::<
KakarotCore::AccountDeployed
>(kakarot_core.contract_address)
.unwrap();

assert(event.evm_address == evm_address(), 'wrong evm address');
assert(event.starknet_address.into() == eoa_address.starknet, 'wrong starknet address');

set_chain_id(chain_id().into());
let mut vm = VMBuilderTrait::new_with_presets().build();
let chain_id = vm.env.chain_id;

let eoa = IAccountDispatcher { contract_address: eoa_address.starknet };
let evm_address = eoa.get_evm_address();
assert!(eoa.is_initialized());
}
}
11 changes: 11 additions & 0 deletions crates/evm/src/create_helpers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,14 @@ impl CreateHelpersImpl of CreateHelpers {
Result::Ok(account)
}
}

#[cfg(test)]
mod tests {
use contracts::test_data::counter_evm_bytecode;
use evm::create_helpers::CreateHelpers;
use evm::test_utils::{VMBuilderTrait};
use starknet::EthAddress;
use utils::address::{compute_contract_address, compute_create2_contract_address};
//TODO: test create helpers

}
129 changes: 129 additions & 0 deletions crates/evm/src/gas.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,132 @@ fn calculate_intrinsic_gas_cost(tx: @EthereumTransaction) -> u128 {

TRANSACTION_BASE_COST + data_cost + create_cost + access_list_cost
}

#[cfg(test)]
mod tests {
use core::option::OptionTrait;

use evm::gas::{
calculate_intrinsic_gas_cost, calculate_memory_gas_cost, ACCESS_LIST_ADDRESS,
ACCESS_LIST_STORAGE_KEY
};
use evm::test_utils::evm_address;
use starknet::EthAddress;
use utils::eth_transaction::{
EthereumTransaction, LegacyTransaction, AccessListTransaction, EthereumTransactionTrait,
AccessListItem
};
use utils::helpers::{U256Trait, ToBytes};

#[test]
fn test_calculate_intrinsic_gas_cost() {
// RLP decoded value: (https://toolkit.abdk.consulting/ethereum#rlp,transaction)
// ["0xc9", "0x81", "0xf7", "0x81", "0x80", "0x81", "0x84", "0x00", "0x00", "0x12"]
// 16 16 16 16 16 16 16 4 4 16
// + 21000
// + 0
// ---------------------------
// = 21136
let rlp_encoded: u256 = 0xc981f781808184000012;

let calldata = rlp_encoded.to_be_bytes();
let destination: Option<EthAddress> = 'vitalik.eth'.try_into();

let tx: EthereumTransaction = EthereumTransaction::LegacyTransaction(
LegacyTransaction {
nonce: 0,
gas_price: 50,
gas_limit: 433926,
destination,
amount: 1,
calldata,
chain_id: 0x1
}
);

let expected_cost: u128 = 21136;
let out_cost: u128 = calculate_intrinsic_gas_cost(@tx);

assert_eq!(out_cost, expected_cost, "wrong cost");
}

#[test]
fn test_calculate_intrinsic_gas_cost_with_access_list() {
// RLP decoded value: (https://toolkit.abdk.consulting/ethereum#rlp,transaction)
// ["0xc9", "0x81", "0xf7", "0x81", "0x80", "0x81", "0x84", "0x00", "0x00", "0x12"]
// 16 16 16 16 16 16 16 4 4 16
// + 21000
// + 0
// ---------------------------
// = 21136
let rlp_encoded: u256 = 0xc981f781808184000012;

let calldata = rlp_encoded.to_be_bytes();
let destination: Option<EthAddress> = 'vitalik.eth'.try_into();

let access_list = array![
AccessListItem {
ethereum_address: evm_address(), storage_keys: array![1, 2, 3, 4, 5].span()
}
]
.span();

let tx: EthereumTransaction = EthereumTransaction::AccessListTransaction(
AccessListTransaction {
nonce: 0,
gas_price: 50,
gas_limit: 433926,
destination,
amount: 1,
calldata,
chain_id: 0x1,
access_list
}
);

let expected_cost: u128 = 21136 + ACCESS_LIST_ADDRESS + 5 * ACCESS_LIST_STORAGE_KEY;
let out_cost: u128 = calculate_intrinsic_gas_cost(@tx);

assert_eq!(out_cost, expected_cost, "wrong cost");
}


#[test]
fn test_calculate_intrinsic_gas_cost_without_destination() {
// RLP decoded value: (https://toolkit.abdk.consulting/ethereum#rlp,transaction)
// ["0xc9", "0x81", "0xf7", "0x81", "0x80", "0x81", "0x84", "0x00", "0x00", "0x12"]
// 16 16 16 16 16 16 16 4 4 16
// + 21000
// + (32000 + 2)
// ---------------------------
// = 53138
let rlp_encoded: u256 = 0xc981f781808184000012;

let calldata = rlp_encoded.to_be_bytes();

let tx: EthereumTransaction = EthereumTransaction::LegacyTransaction(
LegacyTransaction {
nonce: 0,
gas_price: 50,
gas_limit: 433926,
destination: Option::None(()),
amount: 1,
calldata,
chain_id: 0x1
}
);

let expected_cost: u128 = 53138;
let out_cost: u128 = calculate_intrinsic_gas_cost(@tx);

assert_eq!(out_cost, expected_cost, "wrong cost");
}

#[test]
fn test_calculate_memory_allocation_cost() {
let size_in_bytes: usize = 10018613;
let expected_cost: u128 = 192385220;
let out_cost: u128 = calculate_memory_gas_cost(size_in_bytes);
assert_eq!(out_cost, expected_cost, "wrong cost");
}
}
Loading

0 comments on commit 86fb610

Please sign in to comment.