Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feat/send-transaction' into feat…
Browse files Browse the repository at this point in the history
…/send-transaction
  • Loading branch information
greged93 committed Nov 21, 2023
2 parents 49b48cd + e70b3f0 commit a098117
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 67 deletions.
87 changes: 33 additions & 54 deletions crates/contracts/src/kakarot_core/kakarot.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ mod KakarotCore {
use evm::model::{ExecutionResult, Address, AddressTrait};
use evm::state::StateTrait;
use starknet::{
EthAddress, ContractAddress, ClassHash, get_tx_info, get_contract_address,
get_caller_address, deploy_syscall
EthAddress, ContractAddress, ClassHash, get_tx_info, get_contract_address, deploy_syscall,
get_caller_address
};
use super::{INVOKE_ETH_CALL_FORBIDDEN};
use super::{StoredAccountType};
Expand Down Expand Up @@ -255,17 +255,7 @@ mod KakarotCore {

let from = Address { evm: from, starknet: self.compute_starknet_address(from) };

let to = match to {
Option::Some(to) => {
let target_starknet_address = self.compute_starknet_address(to);
Option::Some(Address { evm: to, starknet: target_starknet_address })
},
Option::None => Option::None
};

let result = KakarotInternal::handle_execute(
:from, :to, :gas_limit, :gas_price, :value, :data
);
let result = self.handle_call(:from, :to, :gas_limit, :gas_price, :value, :data);
match result {
Result::Ok(result) => result.return_data,
// TODO: Return the error message as Bytes in the response
Expand Down Expand Up @@ -298,17 +288,7 @@ mod KakarotCore {
.expect('Fetching EOA failed');
assert(caller_account_type == AccountType::EOA, 'Caller is not an EOA');

let to = match to {
Option::Some(to) => {
let target_starknet_address = self.compute_starknet_address(to);
Option::Some(Address { evm: to, starknet: target_starknet_address })
},
Option::None => Option::None
};

let mut result = KakarotInternal::handle_execute(
:from, :to, :gas_limit, :gas_price, :value, :data
);
let mut result = self.handle_call(:from, :to, :gas_limit, :gas_price, :value, :data);
match result {
Result::Ok(result) => {
let mut state = result.state;
Expand Down Expand Up @@ -360,14 +340,32 @@ mod KakarotCore {
}
}

mod KakarotInternal {
use evm::errors::EVMError;
use evm::execution::execute;
use evm::model::{ExecutionResult, Address, AccountTrait};
#[generate_trait]
impl KakarotCoreInternalImpl of KakarotCoreInternal {
fn is_view(self: @ContractState) -> bool {
let tx_info = get_tx_info().unbox();

// If the account that originated the transaction is not zero, this means we
// are in an invoke transaction instead of a call; therefore, `eth_call` is being wrongly called
// For invoke transactions, `eth_send_transaction` must be used
if !tx_info.account_contract_address.is_zero() {
return false;
}
true
}

fn handle_execute(
/// Maps an EVM address to a Starknet address
/// Triggerred when deployment of an EOA or CA is successful
fn set_address_registry(
ref self: ContractState, evm_address: EthAddress, account: StoredAccountType
) {
self.address_registry.write(evm_address, account);
}

fn handle_call(
self: @ContractState,
from: Address,
to: Option<Address>,
to: Option<EthAddress>,
gas_limit: u128,
gas_price: u128,
value: u256,
Expand All @@ -376,7 +374,11 @@ mod KakarotCore {
match to {
//TODO we can optimize this by doing this one step later, when we load the account from the state. This way we can avoid loading the account bytecode twice.
Option::Some(to) => {
let bytecode = AccountTrait::fetch_or_create(to.evm)?.code;
let bytecode = AccountTrait::fetch_or_create(to)?.code;

let target_starknet_address = self.compute_starknet_address(to);
let to = Address { evm: to, starknet: target_starknet_address };

let execution_result = execute(
from,
to,
Expand All @@ -399,27 +401,4 @@ mod KakarotCore {
}
}
}

#[generate_trait]
impl KakarotCoreInternalImpl of KakarotCoreInternal {
fn is_view(self: @ContractState) -> bool {
let tx_info = get_tx_info().unbox();

// If the account that originated the transaction is not zero, this means we
// are in an invoke transaction instead of a call; therefore, `eth_call` is being wrongly called
// For invoke transactions, `eth_send_transaction` must be used
if !tx_info.account_contract_address.is_zero() {
return false;
}
true
}

/// Maps an EVM address to a Starknet address
/// Triggerred when deployment of an EOA or CA is successful
fn set_address_registry(
ref self: ContractState, evm_address: EthAddress, account: StoredAccountType
) {
self.address_registry.write(evm_address, account);
}
}
}
25 changes: 12 additions & 13 deletions crates/contracts/src/tests/test_kakarot_core.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use contracts::kakarot_core::interface::IExtendedKakarotCoreDispatcherTrait;
use contracts::kakarot_core::interface::IKakarotCore;
use contracts::kakarot_core::kakarot::StoredAccountType;
use contracts::kakarot_core::{
interface::IExtendedKakarotCoreDispatcherImpl, KakarotCore,
KakarotCore::{KakarotCoreInternal, KakarotInternal},
interface::IExtendedKakarotCoreDispatcherImpl, KakarotCore, KakarotCore::{KakarotCoreInternal},
};
use contracts::tests::test_data::counter_evm_bytecode;
use contracts::tests::test_upgradeable::{
Expand Down Expand Up @@ -337,24 +336,24 @@ fn test_handle_execute() {
)
.unwrap();

let to = test_utils::other_evm_address();
let to = Option::Some(Address { evm: to, starknet: kakarot_core.compute_starknet_address(to) });
let to = Option::Some(test_utils::other_evm_address());
let gas_limit = test_utils::gas_limit();
let gas_price = test_utils::gas_price();
let value = 0;
// selector: function get()
let data = array![0x6d, 0x4c, 0xe6, 0x3c].span();

// When

let result = KakarotInternal::handle_execute(
from: Address { evm: evm_address, starknet: eoa },
:to,
:gas_limit,
:gas_price,
:value,
:data
)
let mut kakarot_core = KakarotCore::unsafe_new_contract_state();
let result = kakarot_core
.handle_call(
from: Address { evm: evm_address, starknet: eoa },
:to,
:gas_limit,
:gas_price,
:value,
:data
)
.expect('handle_call failed');
let return_data = result.return_data;

Expand Down

0 comments on commit a098117

Please sign in to comment.