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

feat: add code address in message #901

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions crates/contracts/src/kakarot_core/kakarot.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,12 @@ pub mod KakarotCore {
};

// Handle deploy/non-deploy transaction cases
let (to, is_deploy_tx, code, calldata) = match tx.destination() {
let (to, is_deploy_tx, code, code_address, calldata) = match tx.destination() {
Option::Some(to) => {
let target_starknet_address = self.compute_starknet_address(to);
let to = Address { evm: to, starknet: target_starknet_address };
let code = env.state.get_account(to.evm).code;
(to, false, code, tx.calldata())
(to, false, code, to, tx.calldata())
},
Option::None => {
// Deploy tx case.
Expand All @@ -319,7 +319,7 @@ pub mod KakarotCore {
let to = Address { evm: to_evm_address, starknet: to_starknet_address };
let code = tx.calldata();
let calldata = [].span();
(to, true, code, calldata)
(to, true, code, Zero::zero(), calldata)
},
};

Expand All @@ -346,6 +346,7 @@ pub mod KakarotCore {
gas_limit: gas_left,
data: calldata,
code,
code_address: code_address,
value: tx.value(),
should_transfer_value: true,
depth: 0,
Expand Down
5 changes: 3 additions & 2 deletions crates/evm/src/call_helpers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl CallHelpersImpl of CallHelpers {
self.memory.load_n(args_size, ref calldata, args_offset);

// We enter the standard flow
let code = self.env.state.get_account(code_address).code;
let code_account = self.env.state.get_account(code_address);
let read_only = is_staticcall || self.message.read_only;

let kakarot_core = KakarotCore::unsafe_new_contract_state();
Expand All @@ -87,7 +87,8 @@ impl CallHelpersImpl of CallHelpers {
target: to,
gas_limit: gas,
data: calldata.span(),
code,
code: code_account.code,
code_address: code_account.address,
value: value,
should_transfer_value: should_transfer_value,
depth: self.message().depth + 1,
Expand Down
2 changes: 2 additions & 0 deletions crates/evm/src/create_helpers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use contracts::kakarot_core::interface::IKakarotCore;
use core::cmp::min;
use core::keccak::cairo_keccak;
use core::num::traits::Bounded;
use core::num::traits::Zero;
use core::starknet::{EthAddress, get_tx_info};
use evm::errors::{
ensure, EVMError, CALL_GAS_GT_GAS_LIMIT, ACTIVE_MACHINE_STATE_IN_CALL_FINALIZATION
Expand Down Expand Up @@ -126,6 +127,7 @@ impl CreateHelpersImpl of CreateHelpers {
gas_limit: create_message_gas,
data: [].span(),
code: create_args.bytecode,
code_address: Zero::zero(),
value: create_args.value,
should_transfer_value: true,
depth: self.message().depth + 1,
Expand Down
20 changes: 10 additions & 10 deletions crates/evm/src/instructions/system_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ mod tests {
fn test_exec_call_code() {
// Given
// Set vm bytecode
// (call 0xffffff 0x100 0 0 0 0 1)
// (callcode 0xffffff 0x1234 0 0 0 0 1)
let bytecode = [
0x60,
0x01,
Expand All @@ -780,8 +780,8 @@ mod tests {
0x60,
0x00,
0x61,
0x01,
0x00,
0x12,
0x34,
0x62,
0xff,
0xff,
Expand All @@ -801,7 +801,7 @@ mod tests {
};
vm.env.state.set_account(eoa_account);

// Deploy bytecode at 0x100
// Deploy bytecode at 0x1234
// ret (+ 0x1 0x1)
let deployed_bytecode = [
0x60,
Expand All @@ -823,7 +823,7 @@ mod tests {
0x00,
0xf3
].span();
let eth_address: EthAddress = 0x100.try_into().unwrap();
let eth_address: EthAddress = 0x1234.try_into().unwrap();
let starknet_address = compute_starknet_address(
test_address(), eth_address, 0.try_into().unwrap()
);
Expand Down Expand Up @@ -855,7 +855,7 @@ mod tests {
// Given

// Set vm bytecode
// (call 0xffffff 0x100 0 0 0 0 1)
// (delegatecall 0xffffff 0x1234 0 0 0 0 1)
let bytecode = [
0x60,
0x01,
Expand All @@ -868,8 +868,8 @@ mod tests {
0x60,
0x00,
0x61,
0x01,
0x00,
0x12,
0x34,
0x62,
0xff,
0xff,
Expand All @@ -889,7 +889,7 @@ mod tests {
};
vm.env.state.set_account(eoa_account);

// Deploy bytecode at 0x100
// Deploy bytecode at 0x1234
// ret (+ 0x1 0x1)
let deployed_bytecode = [
0x60,
Expand All @@ -911,7 +911,7 @@ mod tests {
0x00,
0xf3
].span();
let eth_address: EthAddress = 0x100.try_into().unwrap();
let eth_address: EthAddress = 0x1234.try_into().unwrap();
let starknet_address = compute_starknet_address(
test_address(), eth_address, 0.try_into().unwrap()
);
Expand Down
2 changes: 1 addition & 1 deletion crates/evm/src/interpreter.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl EVMImpl of EVMTrait {

fn execute_code(ref vm: VM) -> ExecutionResult {
// Handle precompile logic
if vm.message.target.evm.is_precompile() {
if vm.message.code_address.evm.is_precompile() {
let result = Precompiles::exec_precompile(ref vm);

match result {
Expand Down
13 changes: 13 additions & 0 deletions crates/evm/src/model.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ struct Message {
gas_limit: u128,
data: Span<u8>,
code: Span<u8>,
code_address: Address,
value: u256,
should_transfer_value: bool,
depth: usize,
Expand Down Expand Up @@ -132,6 +133,18 @@ struct Address {
starknet: ContractAddress,
}

impl ZeroAddress of core::num::traits::Zero<Address> {
fn zero() -> Address {
Address { evm: Zero::zero(), starknet: Zero::zero(), }
}
fn is_zero(self: @Address) -> bool {
self.evm.is_zero() && self.starknet.is_zero()
}
fn is_non_zero(self: @Address) -> bool {
!self.is_zero()
}
}

#[generate_trait]
impl AddressImpl of AddressTrait {
fn is_deployed(self: @EthAddress) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion crates/evm/src/precompiles.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ trait Precompile {
#[generate_trait]
impl PrecompilesImpl of Precompiles {
fn exec_precompile(ref vm: VM) -> Result<(), EVMError> {
let precompile_address = vm.message.target.evm;
let precompile_address = vm.message.code_address.evm;
let input = vm.message().data;

let (gas, result) = if precompile_address.address == 0x100 {
Expand Down
2 changes: 2 additions & 0 deletions crates/evm/src/test_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ fn preset_message() -> Message {
test_address(), evm_address(), uninitialized_account()
)
};
let code_address = target;
let read_only = false;
let tx_gas_limit = tx_gas_limit();

Expand All @@ -238,6 +239,7 @@ fn preset_message() -> Message {
gas_limit: tx_gas_limit,
read_only,
code,
code_address,
should_transfer_value: true,
depth: 0,
accessed_addresses: Default::default(),
Expand Down
Loading