Skip to content

Commit

Permalink
dev: refactor machine preparataion into a util
Browse files Browse the repository at this point in the history
  • Loading branch information
Harsh Bajpai authored and Harsh Bajpai committed Nov 29, 2023
1 parent 41a8816 commit e705d2e
Showing 1 changed file with 108 additions and 86 deletions.
194 changes: 108 additions & 86 deletions crates/evm/src/tests/test_call_helpers.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use contracts::kakarot_core::interface::IExtendedKakarotCoreDispatcherTrait;
use contracts::kakarot_core::interface::{
IExtendedKakarotCoreDispatcher, IExtendedKakarotCoreDispatcherTrait
};

use contracts::tests::test_utils::setup_contracts_for_testing;
use core::box::BoxTrait;
Expand All @@ -7,21 +9,32 @@ use core::result::ResultTrait;
use core::traits::Destruct;
use core::traits::TryInto;


use evm::call_helpers::{MachineCallHelpers, CallType, CallArgs};
use evm::machine::Machine;
use evm::machine::MachineTrait;
use evm::model::{Address, account::{AccountBuilderTrait}};
use evm::stack::StackTrait;
use evm::state::StateTrait;
use evm::tests::test_utils::{MachineBuilderTestTrait, test_address, other_evm_address};
use starknet::testing::set_contract_address;
use starknet::{contract_address_const, EthAddress};

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

set_contract_address(kakarot_core.contract_address);
use starknet::{contract_address_const, EthAddress, ContractAddress};

struct TestSetupValues {
gas: u128,
address: EthAddress,
value: Option::<u256>,
args_offset: u256,
args_size: u256,
ret_offset: usize,
ret_size: usize,
caller_address: Address
}

fn prep_machine_prepare_call_test(
call_type: CallType, kakarot_core_address: ContractAddress
) -> (Machine, TestSetupValues) {
let kakarot_core = IExtendedKakarotCoreDispatcher { contract_address: kakarot_core_address };
let caller_address = EthAddress { address: 0xabde2 };
let caller_address = Address {
evm: caller_address, starknet: kakarot_core.compute_starknet_address(caller_address)
Expand All @@ -33,7 +46,6 @@ fn test_prepare_call_type_call() {

let gas: u128 = 1000;
let address = other_evm_address();
let value = 100;
let args_offset = 5;
let args_size = 1;
let ret_offset: usize = 15;
Expand All @@ -43,18 +55,60 @@ fn test_prepare_call_type_call() {
machine.stack.push(ret_offset.into()).unwrap();
machine.stack.push(args_size).unwrap();
machine.stack.push(args_offset).unwrap();
machine.stack.push(value).unwrap();

let value = match call_type {
CallType::Call => {
let value = 100;
machine.stack.push(value).unwrap();
Option::Some(value)
},
CallType::DelegateCall => { Option::None },
CallType::CallCode => {
let value = 100;
machine.stack.push(value).unwrap();
Option::Some(value)
},
CallType::StaticCall => { Option::None },
};

machine.stack.push(address.address.into()).unwrap();
machine.stack.push(gas.into()).unwrap();

(
machine,
TestSetupValues {
gas, address, value, args_offset, args_size, ret_offset, ret_size, caller_address
}
)
}

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

let (
mut machine,
TestSetupValues{gas,
address,
value,
args_offset: _,
args_size: _,
ret_offset,
ret_size,
caller_address: _ }
) =
prep_machine_prepare_call_test(
CallType::Call, kakarot_core.contract_address
);

let expected_call_args = CallArgs {
caller: test_address(),
code_address: Address {
evm: address, starknet: kakarot_core.compute_starknet_address(address)
},
to: Address { evm: address, starknet: kakarot_core.compute_starknet_address(address) },
gas,
value,
value: value.unwrap(),
calldata: array![0x0].span(),
ret_offset,
ret_size,
Expand All @@ -70,30 +124,20 @@ fn test_prepare_call_type_call() {
fn test_prepare_call_type_delegate_call() {
let (_, kakarot_core) = setup_contracts_for_testing();

let caller_address = EthAddress { address: 0xabde2 };
let caller_address = Address {
evm: caller_address, starknet: kakarot_core.compute_starknet_address(caller_address)
};

set_contract_address(kakarot_core.contract_address);

let mut machine = MachineBuilderTestTrait::new_with_presets()
.with_caller(caller_address)
.build();

let gas: u128 = 1000;
let address = other_evm_address();
let args_offset = 5;
let args_size = 1;
let ret_offset: usize = 15;
let ret_size: usize = 20;

machine.stack.push(ret_size.into()).unwrap();
machine.stack.push(ret_offset.into()).unwrap();
machine.stack.push(args_size).unwrap();
machine.stack.push(args_offset).unwrap();
machine.stack.push(address.address.into()).unwrap();
machine.stack.push(gas.into()).unwrap();
let (
mut machine,
TestSetupValues{gas,
address,
value: _,
args_offset: _,
args_size: _,
ret_offset,
ret_size,
caller_address }
) =
prep_machine_prepare_call_test(
CallType::DelegateCall, kakarot_core.contract_address
);

let expected_call_args = CallArgs {
caller: caller_address,
Expand Down Expand Up @@ -121,32 +165,20 @@ fn test_prepare_call_type_delegate_call() {
fn test_prepare_call_type_call_code() {
let (_, kakarot_core) = setup_contracts_for_testing();

let caller_address = EthAddress { address: 0xabde2 };
let caller_address = Address {
evm: caller_address, starknet: kakarot_core.compute_starknet_address(caller_address)
};

set_contract_address(kakarot_core.contract_address);

let mut machine = MachineBuilderTestTrait::new_with_presets()
.with_caller(caller_address)
.build();

let gas: u128 = 1000;
let address = other_evm_address();
let value = 100;
let args_offset = 5;
let args_size = 1;
let ret_offset: usize = 15;
let ret_size: usize = 20;

machine.stack.push(ret_size.into()).unwrap();
machine.stack.push(ret_offset.into()).unwrap();
machine.stack.push(args_size).unwrap();
machine.stack.push(args_offset).unwrap();
machine.stack.push(value).unwrap();
machine.stack.push(address.address.into()).unwrap();
machine.stack.push(gas.into()).unwrap();
let (
mut machine,
TestSetupValues{gas,
address,
value,
args_offset: _,
args_size: _,
ret_offset,
ret_size,
caller_address: _ }
) =
prep_machine_prepare_call_test(
CallType::CallCode, kakarot_core.contract_address
);

let expected_call_args = CallArgs {
caller: test_address(),
Expand All @@ -158,7 +190,7 @@ fn test_prepare_call_type_call_code() {
starknet: kakarot_core.compute_starknet_address(test_address().evm)
},
gas,
value: 100,
value: value.unwrap(),
calldata: array![0x0].span(),
ret_offset,
ret_size,
Expand All @@ -174,30 +206,20 @@ fn test_prepare_call_type_call_code() {
fn test_prepare_call_type_static_call() {
let (_, kakarot_core) = setup_contracts_for_testing();

let caller_address = EthAddress { address: 0xabde2 };
let caller_address = Address {
evm: caller_address, starknet: kakarot_core.compute_starknet_address(caller_address)
};

set_contract_address(kakarot_core.contract_address);

let mut machine = MachineBuilderTestTrait::new_with_presets()
.with_caller(caller_address)
.build();

let gas: u128 = 1000;
let address = other_evm_address();
let args_offset = 5;
let args_size = 1;
let ret_offset: usize = 15;
let ret_size: usize = 20;

machine.stack.push(ret_size.into()).unwrap();
machine.stack.push(ret_offset.into()).unwrap();
machine.stack.push(args_size).unwrap();
machine.stack.push(args_offset).unwrap();
machine.stack.push(address.address.into()).unwrap();
machine.stack.push(gas.into()).unwrap();
let (
mut machine,
TestSetupValues{gas,
address,
value: _,
args_offset: _,
args_size: _,
ret_offset,
ret_size,
caller_address: _ }
) =
prep_machine_prepare_call_test(
CallType::StaticCall, kakarot_core.contract_address
);

let expected_call_args = CallArgs {
caller: test_address(),
Expand Down

0 comments on commit e705d2e

Please sign in to comment.