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

feat: execute #430

Merged
merged 5 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
23 changes: 23 additions & 0 deletions crates/contracts/src/kakarot_core/kakarot.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ mod KakarotCore {
use core::pedersen::{HashState, PedersenTrait};
use core::starknet::SyscallResultTrait;
use core::zeroable::Zeroable;
use evm::context::Status;
use evm::errors::EVMError;
use evm::execution::execute;
use starknet::{
EthAddress, ContractAddress, ClassHash, get_tx_info, get_contract_address, deploy_syscall
};
Expand Down Expand Up @@ -267,6 +269,27 @@ mod KakarotCore {
}
Result::Ok(())
}


/// Deploys a contract account for a particular EVM address
fn deploy_contract_account(
enitrat marked this conversation as resolved.
Show resolved Hide resolved
ref self: ContractState, evm_address: EthAddress, value: u256, bytecode: Span<u8>
) -> bool {
let (ret_status, ret_data) = execute(
enitrat marked this conversation as resolved.
Show resolved Hide resolved
:evm_address,
Eikix marked this conversation as resolved.
Show resolved Hide resolved
:bytecode,
calldata: array![].span(),
:value,
gas_price: 0,
gas_limit: 0,
);
//TODO gas params
if ret_status != Status::Reverted {
//TODO store_bytecode
return true;
}
return false;
}
}
}

9 changes: 0 additions & 9 deletions crates/evm/src/context.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ impl DefaultOptionSpanU8 of Default<Option<Span<u8>>> {
struct ExecutionContext {
id: usize,
evm_address: EthAddress,
starknet_address: ContractAddress,
program_counter: u32,
status: Status,
call_ctx: Box<CallContext>,
Expand Down Expand Up @@ -162,15 +161,13 @@ impl ExecutionContextImpl of ExecutionContextTrait {
fn new(
id: usize,
evm_address: EthAddress,
starknet_address: ContractAddress,
call_ctx: CallContext,
parent_ctx: Nullable<ExecutionContext>,
return_data: Span<u8>,
) -> ExecutionContext {
ExecutionContext {
id,
evm_address,
starknet_address,
program_counter: Default::default(),
status: Status::Active,
call_ctx: BoxTrait::new(call_ctx),
Expand Down Expand Up @@ -258,12 +255,6 @@ impl ExecutionContextImpl of ExecutionContextTrait {
*self.evm_address
}

#[inline(always)]
fn starknet_address(self: @ExecutionContext) -> ContractAddress {
*self.starknet_address
}


// *************************************************************************
// ExecutionContext methods
// *************************************************************************
Expand Down
43 changes: 35 additions & 8 deletions crates/evm/src/execution.cairo
Original file line number Diff line number Diff line change
@@ -1,16 +1,43 @@
use evm::context::{CallContext, ExecutionContext};
use evm::context::{CallContext, CallContextTrait, ExecutionContext, ExecutionContextTrait, Status};
use evm::interpreter::EVMInterpreterTrait;
use evm::machine::Machine;
use evm::machine::{Machine, MachineCurrentContextTrait};
use starknet::{EthAddress, ContractAddress};


/// Execute EVM bytecode.
fn execute() {
/// TODO: implement the execute function.
/// Runs the given bytecode with the given calldata and parameters.
///
/// # Arguments
///
/// * `evm_contract_address` - The EVM address of the called contract. Set to 0
/// if there is no notion of deployed contract in the bytecode.
/// * `bytecode` - The bytecode to run.
/// * `calldata` - The calldata of the execution.
/// * `value` - The value of the execution.
/// * `gas_limit` - The gas limit of the execution.
/// * `gas_price` - The gas price for the execution.
/// # Returns
/// * The final Status of the execution.
/// * The return data of the execution.
fn execute(
evm_address: EthAddress,
bytecode: Span<u8>,
calldata: Span<u8>,
value: u256,
gas_price: u64,
gas_limit: u64,
) -> (Status, Span<u8>) {
let call_ctx = CallContextTrait::new(
caller: evm_address, :bytecode, :calldata, :value, read_only: false, :gas_limit, :gas_price
);
// Create new execution context.
let mut machine: Machine = Default::default();
let ctx = ExecutionContextTrait::new(
id: 0, :evm_address, :call_ctx, parent_ctx: Default::default(), return_data: array![].span()
enitrat marked this conversation as resolved.
Show resolved Hide resolved
);
let mut machine: Machine = MachineCurrentContextTrait::new(ctx);

let mut interpreter = EVMInterpreterTrait::new();
// Execute the transaction.
interpreter.run(ref machine)
// Execute the bytecode.
interpreter.run(ref machine);
(machine.status(), machine.return_data())
Eikix marked this conversation as resolved.
Show resolved Hide resolved
}

19 changes: 11 additions & 8 deletions crates/evm/src/machine.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ impl DefaultMachine of Default<Machine> {

#[generate_trait]
impl MachineCurrentContextImpl of MachineCurrentContextTrait {
#[inline(always)]
fn new(ctx: ExecutionContext) -> Machine {
Machine {
current_ctx: BoxTrait::new(ctx),
ctx_count: 1,
stack: Default::default(),
memory: Default::default(),
storage_journal: Default::default(),
}
}

/// Sets the current execution context being executed by the machine.
/// This is an implementation-specific concept that is used
/// to divide a unique Stack/Memory simulated by a dict into
Expand Down Expand Up @@ -167,14 +178,6 @@ impl MachineCurrentContextImpl of MachineCurrentContextTrait {
evm_address
}

#[inline(always)]
fn starknet_address(ref self: Machine) -> ContractAddress {
let current_execution_ctx = self.current_ctx.unbox();
let starknet_address = current_execution_ctx.starknet_address();
self.current_ctx = BoxTrait::new(current_execution_ctx);
starknet_address
}

#[inline(always)]
fn caller(ref self: Machine) -> EthAddress {
let current_call_ctx = self.call_ctx();
Expand Down
4 changes: 1 addition & 3 deletions crates/evm/src/tests/test_execution_context.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ fn test_execution_context_new() {
let stopped: bool = false;
let return_data: Array<u8> = ArrayTrait::new();

let starknet_address: ContractAddress = 0.try_into().unwrap();
let evm_address: EthAddress = 0.try_into().unwrap();
let destroyed_contracts: Array<EthAddress> = Default::default();
let events: Array<Event> = Default::default();
Expand All @@ -72,7 +71,7 @@ fn test_execution_context_new() {

// When
let mut execution_context = ExecutionContextTrait::new(
context_id, evm_address, starknet_address, call_ctx, parent_ctx, return_data.span()
context_id, evm_address, call_ctx, parent_ctx, return_data.span()
);

// Then
Expand All @@ -81,7 +80,6 @@ fn test_execution_context_new() {
assert(execution_context.program_counter == program_counter, 'wrong program_counter');
assert(execution_context.stopped() == stopped, 'wrong stopped');
assert(execution_context.return_data() == Default::default().span(), 'wrong return_data');
assert(execution_context.starknet_address() == starknet_address, 'wrong starknet_address');
assert(execution_context.evm_address() == evm_address, 'wrong evm_address');
assert(
execution_context.destroyed_contracts() == destroyed_contracts.span(),
Expand Down
4 changes: 0 additions & 4 deletions crates/evm/src/tests/test_machine.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,6 @@ fn test_addresses() {

let evm_address = machine.evm_address();
assert(evm_address == evm_address_expected, 'wrong evm address');

let starknet_address = machine.starknet_address();
let starknet_address_expected = starknet_address();
assert(starknet_address == starknet_address_expected, 'wrong starknet address');
}

#[test]
Expand Down
15 changes: 3 additions & 12 deletions crates/evm/src/tests/test_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,10 @@ fn setup_call_context() -> CallContext {
fn setup_execution_context() -> ExecutionContext {
let context_id = 0;
let call_ctx = setup_call_context();
let starknet_address: ContractAddress = starknet_address();
let evm_address: EthAddress = evm_address();
let return_data = array![1, 2, 3].span();

ExecutionContextTrait::new(
context_id, evm_address, starknet_address, call_ctx, Default::default(), return_data,
)
ExecutionContextTrait::new(context_id, evm_address, call_ctx, Default::default(), return_data,)
}

fn setup_nested_execution_context() -> ExecutionContext {
Expand Down Expand Up @@ -112,13 +109,10 @@ fn setup_call_context_with_bytecode(bytecode: Span<u8>) -> CallContext {
fn setup_execution_context_with_bytecode(bytecode: Span<u8>) -> ExecutionContext {
let context_id = 0;
let call_ctx = setup_call_context_with_bytecode(bytecode);
let starknet_address: ContractAddress = starknet_address();
let evm_address: EthAddress = evm_address();
let return_data = Default::default().span();

ExecutionContextTrait::new(
context_id, evm_address, starknet_address, call_ctx, Default::default(), return_data,
)
ExecutionContextTrait::new(context_id, evm_address, call_ctx, Default::default(), return_data,)
}


Expand All @@ -136,13 +130,10 @@ fn setup_call_context_with_calldata(calldata: Span<u8>) -> CallContext {
fn setup_execution_context_with_calldata(calldata: Span<u8>) -> ExecutionContext {
let context_id = 0;
let call_ctx = setup_call_context_with_calldata(calldata);
let starknet_address: ContractAddress = starknet_address();
let evm_address: EthAddress = evm_address();
let return_data = Default::default().span();

ExecutionContextTrait::new(
context_id, evm_address, starknet_address, call_ctx, Default::default(), return_data,
)
ExecutionContextTrait::new(context_id, evm_address, call_ctx, Default::default(), return_data,)
}

impl CallContextPartialEq of PartialEq<CallContext> {
Expand Down