Skip to content

Commit

Permalink
refactor: utils crate (#975)
Browse files Browse the repository at this point in the history
* refactor: utils crate

* move ethaddress trait
  • Loading branch information
enitrat authored Sep 26, 2024
1 parent 6787297 commit b587189
Show file tree
Hide file tree
Showing 44 changed files with 2,649 additions and 2,647 deletions.
2 changes: 1 addition & 1 deletion crates/contracts/src/cairo1_helpers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ pub mod embeddable_impls {
use evm::precompiles::EcAdd;
use evm::precompiles::EcMul;
use evm::precompiles::Sha256;
use utils::helpers::U256Trait;
use utils::traits::integer::U256Trait;


#[starknet::embeddable]
Expand Down
2 changes: 1 addition & 1 deletion crates/contracts/src/kakarot_core/eth_rpc.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use evm::backend::validation::validate_eth_tx;
use evm::model::{TransactionResult, Address};
use evm::{EVMTrait};
use utils::constants::POW_2_53;
use utils::eth_transaction::transaction::{TransactionTrait, Transaction};
use utils::eth_transaction::transaction::Transaction;

#[starknet::interface]
pub trait IEthRPC<T> {
Expand Down
12 changes: 6 additions & 6 deletions crates/contracts/tests/test_account_contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use contracts::test_utils::{
setup_contracts_for_testing, deploy_contract_account, fund_account_with_native_token, deploy_eoa
};
use contracts::{IAccountDispatcher, IAccountDispatcherTrait};
use core::starknet::EthAddress;
use core::starknet::account::{Call};
use core::starknet::{EthAddress, ContractAddress};
use evm::test_utils::{ca_address, native_token, eoa_address};
use openzeppelin::token::erc20::interface::{IERC20CamelDispatcher, IERC20CamelDispatcherTrait};
use evm::test_utils::{ca_address, eoa_address};
use openzeppelin::token::erc20::interface::IERC20CamelDispatcherTrait;
use snforge_std::{start_cheat_caller_address, stop_cheat_caller_address};

#[test]
Expand Down Expand Up @@ -101,7 +101,7 @@ fn test_ca_external_starknet_call_native_token() {
let (success, data) = contract_account.execute_starknet_call(call);
stop_cheat_caller_address(ca_address.starknet);

assert(success == true, 'execute_starknet_call failed');
assert(success, 'execute_starknet_call failed');
assert(data.len() == 2, 'wrong return data length');
let balance = native_token.balanceOf(ca_address.starknet);
assert((*data[0], *data[1]) == (balance.low.into(), balance.high.into()), 'wrong return data');
Expand All @@ -122,7 +122,7 @@ fn test_ca_external_starknet_call_kakarot_get_starknet_address() {
let (success, data) = contract_account.execute_starknet_call(call);
stop_cheat_caller_address(ca_address.starknet);

assert(success == true, 'execute_starknet_call failed');
assert(success, 'execute_starknet_call failed');
assert(data.len() == 1, 'wrong return data length');
assert(*data[0] == ca_address.starknet.try_into().unwrap(), 'wrong return data');
}
Expand All @@ -142,7 +142,7 @@ fn test_ca_external_starknet_call_cannot_call_kakarot_other_selector() {
let (success, data) = contract_account.execute_starknet_call(call);
stop_cheat_caller_address(ca_address.starknet);

assert(success == false, 'execute_starknet_call failed');
assert(!success, 'execute_starknet_call failed');
assert(data.len() == 19, 'wrong return data length');
assert(data == KAKAROT_REENTRANCY.span(), 'wrong return data');
}
2 changes: 1 addition & 1 deletion crates/contracts/tests/test_cairo1_helpers.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use contracts::cairo1_helpers::Cairo1Helpers;
use utils::helpers::BytesUsedTrait;
use utils::traits::integer::BytesUsedTrait;

#[test]
fn test_keccak() {
Expand Down
36 changes: 13 additions & 23 deletions crates/contracts/tests/test_execution_from_outside.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,29 @@ use contracts::kakarot_core::interface::{
};
use contracts::test_data::{counter_evm_bytecode, eip_2930_rlp_encoded_counter_inc_tx,};
use contracts::test_utils::{
setup_contracts_for_testing, deploy_eoa, deploy_contract_account,
fund_account_with_native_token, call_transaction
setup_contracts_for_testing, deploy_contract_account, fund_account_with_native_token,
call_transaction
};
use core::num::traits::Bounded;
use core::starknet::account::Call;
use core::starknet::secp256_trait::Signature;
use core::starknet::{
ContractAddress, ClassHash, VALIDATED, get_contract_address, contract_address_const, EthAddress,
get_tx_info, Event
};
use evm::test_utils::{
kakarot_address, other_evm_address, other_starknet_address, eoa_address, tx_gas_limit,
gas_price, VMBuilderTrait
};
use evm::test_utils::{ca_address, chain_id};
use openzeppelin::token::erc20::interface::{IERC20CamelDispatcher, IERC20CamelDispatcherTrait};
use core::starknet::{ContractAddress, contract_address_const, EthAddress, Event};
use evm::test_utils::chain_id;
use evm::test_utils::other_evm_address;
use openzeppelin::token::erc20::interface::IERC20CamelDispatcher;
use snforge_std::{
start_cheat_caller_address, stop_cheat_caller_address, start_cheat_signature,
stop_cheat_signature, start_cheat_chain_id, stop_cheat_chain_id, start_cheat_transaction_hash,
stop_cheat_transaction_hash, spy_events, EventSpyTrait, EventsFilterTrait, CheatSpan,
cheat_caller_address, stop_mock_call, stop_cheat_block_timestamp, start_cheat_block_timestamp,
start_cheat_chain_id_global, stop_cheat_chain_id_global, start_mock_call,
start_cheat_caller_address, stop_cheat_caller_address, start_cheat_transaction_hash, spy_events,
EventSpyTrait, CheatSpan, cheat_caller_address, stop_cheat_block_timestamp,
start_cheat_block_timestamp, start_cheat_chain_id_global, stop_cheat_chain_id_global,
start_cheat_caller_address_global, stop_cheat_caller_address_global
};

use snforge_utils::snforge_utils::{ContractEvents, ContractEventsTrait, EventsFilterBuilderTrait};
use utils::eth_transaction::transaction::{Transaction, TransactionTrait};
use snforge_utils::snforge_utils::EventsFilterBuilderTrait;
use utils::eth_transaction::transaction::Transaction;
use utils::eth_transaction::tx_type::TxType;
use utils::helpers::{U8SpanExTrait, u256_to_bytes_array};
use utils::helpers::u256_to_bytes_array;
use utils::serialization::{serialize_bytes, serialize_transaction_signature};
use utils::test_data::{
legacy_rlp_encoded_tx, eip_2930_encoded_tx, eip_1559_encoded_tx, legacy_rlp_encoded_deploy_tx
};
use utils::test_data::{legacy_rlp_encoded_tx, eip_2930_encoded_tx, eip_1559_encoded_tx};

fn transaction_signer() -> EthAddress {
0x6Bd85F39321B00c6d603474C5B2fddEB9c92A466.try_into().unwrap()
Expand Down
33 changes: 10 additions & 23 deletions crates/contracts/tests/test_kakarot_core.cairo
Original file line number Diff line number Diff line change
@@ -1,44 +1,31 @@
use contracts::UninitializedAccount;
use contracts::account_contract::{IAccountDispatcher, IAccountDispatcherTrait};
use contracts::kakarot_core::interface::{
IExtendedKakarotCoreDispatcher, IExtendedKakarotCoreDispatcherTrait
};
use contracts::kakarot_core::interface::IExtendedKakarotCoreDispatcherTrait;
use contracts::kakarot_core::{KakarotCore};
use contracts::test_contracts::test_upgradeable::{
MockContractUpgradeableV1, IMockContractUpgradeableDispatcher,
IMockContractUpgradeableDispatcherTrait
IMockContractUpgradeableDispatcher, IMockContractUpgradeableDispatcherTrait
};
use contracts::test_data::{deploy_counter_calldata, counter_evm_bytecode};
use contracts::{test_utils as contract_utils,};
use core::fmt::{Debug, Formatter, Error};
use core::num::traits::Zero;
use core::ops::SnapshotDeref;
use core::option::OptionTrait;
use core::starknet::storage::StoragePathEntry;
use core::starknet::{testing, contract_address_const, ContractAddress, EthAddress, ClassHash};
use core::starknet::{contract_address_const, ContractAddress, EthAddress, ClassHash};


use core::traits::TryInto;
use evm::model::{Address};
use evm::test_utils::{sequencer_evm_address, chain_id};
use evm::test_utils::chain_id;
use evm::test_utils;
use snforge_std::{
declare, DeclareResultTrait, start_cheat_caller_address, stop_cheat_caller_address,
start_cheat_signature, stop_cheat_signature, start_cheat_chain_id_global,
stop_cheat_chain_id_global, start_cheat_transaction_hash, stop_cheat_transaction_hash,
spy_events, Event, EventSpyTrait, test_address, cheat_caller_address, CheatSpan, store, load,
EventSpyAssertionsTrait, start_mock_call, stop_mock_call
};
use snforge_utils::snforge_utils::{
EventsFilterBuilderTrait, ContractEvents, ContractEventsTrait, store_evm
declare, DeclareResultTrait, start_cheat_caller_address, spy_events, EventSpyTrait,
cheat_caller_address, CheatSpan, store
};
use snforge_utils::snforge_utils::{EventsFilterBuilderTrait, ContractEventsTrait};
use starknet::storage::StorageTrait;
use utils::constants::EMPTY_KECCAK;
use utils::eth_transaction::common::{TxKind, TxKindTrait};
use utils::eth_transaction::legacy::TxLegacy;
use utils::eth_transaction::transaction::{Transaction, TransactionTrait};
use utils::helpers::U8SpanExTrait;
use utils::helpers::{EthAddressExTrait, u256_to_bytes_array};
use utils::eth_transaction::transaction::Transaction;
use utils::helpers::{u256_to_bytes_array};
use utils::traits::eth_address::EthAddressExTrait;

#[test]
fn test_kakarot_core_owner() {
Expand Down
8 changes: 2 additions & 6 deletions crates/contracts/tests/test_ownable.cairo
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
use contracts::components::ownable::{ownable_component};
use contracts::test_utils::constants::{ZERO, OWNER, OTHER};
use contracts::test_utils;
use core::num::traits::Zero;
use core::starknet::ContractAddress;
use core::starknet::testing;


use ownable_component::{InternalImpl, OwnableImpl};
use snforge_std::{
start_cheat_caller_address, stop_cheat_caller_address, spy_events, test_address, EventSpyTrait
};
use snforge_utils::snforge_utils::{EventsFilterBuilderTrait, ContractEvents, ContractEventsTrait};
use snforge_std::{start_cheat_caller_address, spy_events, test_address, EventSpyTrait};
use snforge_utils::snforge_utils::{EventsFilterBuilderTrait, ContractEventsTrait};


#[starknet::contract]
Expand Down
4 changes: 2 additions & 2 deletions crates/evm/src/backend/starknet_backend.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use core::starknet::storage::StoragePointerReadAccess;
use core::starknet::syscalls::{deploy_syscall};
use core::starknet::syscalls::{emit_event_syscall};
use core::starknet::{EthAddress, get_block_info, SyscallResultTrait};
use evm::errors::{ensure, EVMError, EOA_EXISTS};
use evm::errors::{ensure, EVMError};
use evm::model::{Address, AddressTrait, Environment, Account, AccountTrait};
use evm::model::{Transfer};
use evm::state::{State, StateTrait};
Expand Down Expand Up @@ -258,8 +258,8 @@ mod tests {
use snforge_std::{test_address, start_mock_call, get_class_hash};
use snforge_utils::snforge_utils::{assert_not_called, assert_called};
use super::commit_storage;
use utils::helpers::U8SpanExTrait;
use utils::helpers::compute_starknet_address;
use utils::traits::bytes::U8SpanExTrait;

// Helper function to create a test account
fn create_test_account(is_selfdestruct: bool, is_created: bool, id: felt252) -> Account {
Expand Down
1 change: 0 additions & 1 deletion crates/evm/src/backend/validation.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use core::starknet::{get_caller_address};
use evm::gas;
use openzeppelin::token::erc20::interface::{IERC20CamelDispatcher, IERC20CamelDispatcherTrait};
use starknet::storage::StorageTrait;
use utils::constants::POW_2_32;
use utils::eth_transaction::check_gas_fee;
use utils::eth_transaction::transaction::{Transaction, TransactionTrait};

Expand Down
2 changes: 1 addition & 1 deletion crates/evm/src/errors.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::fmt::{Debug, Formatter, Error, Display};
use utils::helpers::ToBytes;
use utils::traits::bytes::ToBytes;

// STACK

Expand Down
2 changes: 1 addition & 1 deletion crates/evm/src/gas.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ mod tests {
use utils::eth_transaction::eip2930::{AccessListItem, TxEip2930};
use utils::eth_transaction::legacy::TxLegacy;
use utils::eth_transaction::transaction::Transaction;
use utils::helpers::ToBytes;
use utils::traits::bytes::ToBytes;

#[test]
fn test_calculate_intrinsic_gas_cost() {
Expand Down
6 changes: 3 additions & 3 deletions crates/evm/src/instructions/environmental_information.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,9 @@ mod tests {
use evm::test_utils::{VMBuilderTrait, origin, callvalue, gas_price};
use snforge_std::test_address;
use utils::constants::EMPTY_KECCAK;
use utils::helpers::{
u256_to_bytes_array, ArrayExtTrait, compute_starknet_address, U8SpanExTrait
};
use utils::helpers::{u256_to_bytes_array, compute_starknet_address};
use utils::traits::array::ArrayExtTrait;
use utils::traits::bytes::{U8SpanExTrait};
use utils::traits::{EthAddressIntoU256};


Expand Down
2 changes: 1 addition & 1 deletion crates/evm/src/instructions/memory_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ mod tests {
native_token
};
use snforge_std::{test_address, start_mock_call};
use utils::helpers::U8SpanExTrait;
use utils::helpers::compute_starknet_address;
use utils::traits::bytes::U8SpanExTrait;

#[test]
fn test_pc_basic() {
Expand Down
3 changes: 1 addition & 2 deletions crates/evm/src/instructions/push_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use evm::errors::EVMError;
use evm::gas;
use evm::model::vm::{VM, VMTrait};
use evm::stack::StackTrait;
use utils::helpers::FromBytes;
use utils::helpers::U8SpanExTrait;
use utils::traits::bytes::{FromBytes, U8SpanExTrait};

/// Place i bytes items on stack.
#[inline(always)]
Expand Down
4 changes: 3 additions & 1 deletion crates/evm/src/instructions/sha3.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use evm::gas;
use evm::memory::MemoryTrait;
use evm::model::vm::{VM, VMTrait};
use evm::stack::StackTrait;
use utils::helpers::{ArrayExtTrait, U256Trait, ceil32};
use utils::helpers::ceil32;
use utils::traits::array::ArrayExtTrait;
use utils::traits::integer::U256Trait;

#[generate_trait]
pub impl Sha3Impl of Sha3Trait {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use evm::errors::EVMError;
use evm::gas;
use evm::model::vm::{VM, VMTrait};
use evm::stack::StackTrait;
use utils::helpers::BytesUsedTrait;
use utils::i256::i256;
use utils::math::{Exponentiation, WrappingExponentiation, u256_wide_add};
use utils::traits::integer::BytesUsedTrait;

#[generate_trait]
pub impl StopAndArithmeticOperations of StopAndArithmeticOperationsTrait {
Expand Down
2 changes: 1 addition & 1 deletion crates/evm/src/instructions/system_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -386,9 +386,9 @@ mod tests {
};
use snforge_std::{test_address, start_mock_call};
use utils::constants::EMPTY_KECCAK;
use utils::helpers::U8SpanExTrait;
use utils::helpers::compute_starknet_address;
use utils::helpers::load_word;
use utils::traits::bytes::U8SpanExTrait;
use utils::traits::{EthAddressIntoU256};


Expand Down
6 changes: 2 additions & 4 deletions crates/evm/src/interpreter.cairo
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use contracts::account_contract::{IAccountDispatcher, IAccountDispatcherTrait};
use contracts::kakarot_core::KakarotCore;
use contracts::kakarot_core::interface::IKakarotCore;
use core::num::traits::{Bounded, Zero};
use core::ops::SnapshotDeref;
use core::starknet::EthAddress;
use core::starknet::get_tx_info;
use core::starknet::storage::{StoragePointerReadAccess};
use evm::backend::starknet_backend;
use evm::create_helpers::CreateHelpers;
use evm::errors::{EVMError, EVMErrorTrait, CONTRACT_ACCOUNT_EXISTS};
use evm::errors::{EVMError, EVMErrorTrait};

use evm::instructions::{
ExchangeOperationsTrait, LoggingOperationsTrait, StopAndArithmeticOperationsTrait,
Expand All @@ -31,8 +29,8 @@ use utils::constants;
use utils::eth_transaction::common::TxKind;
use utils::eth_transaction::eip2930::{AccessListItem, AccessListItemTrait};
use utils::eth_transaction::transaction::{Transaction, TransactionTrait};
use utils::helpers::EthAddressExTrait;
use utils::set::{Set, SetTrait};
use utils::traits::eth_address::EthAddressExTrait;

#[generate_trait]
pub impl EVMImpl of EVMTrait {
Expand Down
7 changes: 5 additions & 2 deletions crates/evm/src/memory.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use utils::constants::{
POW_2_0, POW_2_8, POW_2_16, POW_2_24, POW_2_32, POW_2_40, POW_2_48, POW_2_56, POW_2_64,
POW_2_72, POW_2_80, POW_2_88, POW_2_96, POW_2_104, POW_2_112, POW_2_120, POW_256_16
};
use utils::{helpers, helpers::ArrayExtTrait, math::Bitshift};
use utils::traits::array::ArrayExtTrait;
use utils::{helpers, math::Bitshift};

// 2**17
const MEMORY_SEGMENT_SIZE: usize = 131072;
Expand Down Expand Up @@ -622,7 +623,9 @@ mod tests {
use core::num::traits::Bounded;
use evm::memory::{MemoryTrait, InternalMemoryTrait};
use utils::constants::{POW_2_8, POW_2_56, POW_2_64, POW_2_120};
use utils::{math::Exponentiation, math::WrappingExponentiation, helpers, helpers::SpanExtTrait};
use utils::{
math::Exponentiation, math::WrappingExponentiation, helpers, traits::array::SpanExtTrait
};


fn load_should_load_an_element_from_the_memory_with_offset_stored_with_store_n(
Expand Down
4 changes: 2 additions & 2 deletions crates/evm/src/model/account.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use core::starknet::{ContractAddress, EthAddress};
use evm::backend::starknet_backend::fetch_balance;
use evm::model::Address;
use utils::constants::EMPTY_KECCAK;
use utils::helpers::U8SpanExTrait;
use utils::traits::bytes::U8SpanExTrait;

#[derive(Drop)]
struct AccountBuilder {
Expand Down Expand Up @@ -278,7 +278,7 @@ mod tests {
mod test_has_code_or_nonce {
use evm::model::account::{Account, AccountTrait, Address};
use utils::constants::EMPTY_KECCAK;
use utils::helpers::U8SpanExTrait;
use utils::traits::bytes::U8SpanExTrait;

#[test]
fn test_should_return_false_when_empty() {
Expand Down
4 changes: 2 additions & 2 deletions crates/evm/src/precompiles/blake2f.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::starknet::EthAddress;
use evm::errors::{EVMError, ensure};
use evm::precompiles::Precompile;
use utils::crypto::blake2_compress::compress;
use utils::helpers::{FromBytes, ToBytes};
use utils::traits::bytes::{FromBytes, ToBytes};

const GF_ROUND: u64 = 1;
const INPUT_LENGTH: usize = 213;
Expand Down Expand Up @@ -97,7 +97,7 @@ mod tests {
};
use evm::test_utils::{VMBuilderTrait, native_token, setup_test_environment};
use snforge_std::start_mock_call;
use utils::helpers::FromBytes;
use utils::traits::bytes::FromBytes;

#[test]
fn test_blake2_precompile_fail_empty_input() {
Expand Down
Loading

0 comments on commit b587189

Please sign in to comment.