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 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
2 changes: 1 addition & 1 deletion .trunk/trunk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# To learn more about the format of this file, see https://docs.trunk.io/reference/trunk-yaml
version: 0.1
cli:
version: 1.17.0
version: 1.17.1
plugins:
sources:
- id: trunk
Expand Down
2 changes: 2 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
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
61 changes: 52 additions & 9 deletions crates/evm/src/execution.cairo
Original file line number Diff line number Diff line change
@@ -1,16 +1,59 @@
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 evm::model::ExecutionResult;
use starknet::{EthAddress, ContractAddress};

/// Creates an instance of the EVM to execute the given bytecode.
///
/// # 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
/// * ExecutionResult struct, containing:
/// * The execution status
/// * The return data of the execution.
/// * The destroyed contracts
/// * The created contracts
/// * The events emitted
fn execute(
evm_address: EthAddress,
bytecode: Span<u8>,
calldata: Span<u8>,
value: u256,
gas_price: u64,
gas_limit: u64,
) -> ExecutionResult {
// Create a new root execution context.
let call_ctx = CallContextTrait::new(
caller: evm_address, :bytecode, :calldata, :value, read_only: false, :gas_limit, :gas_price
);
let ctx = ExecutionContextTrait::new(
id: 0,
:evm_address,
:call_ctx,
parent_ctx: Default::default(),
return_data: Default::default().span()
);

/// Execute EVM bytecode.
fn execute() {
/// TODO: implement the execute function.
// Create new execution context.
let mut machine: Machine = Default::default();
// Initiate the Machine with the root context
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);
ExecutionResult {
status: machine.status(),
Eikix marked this conversation as resolved.
Show resolved Hide resolved
return_data: machine.return_data(),
destroyed_contracts: machine.destroyed_contracts(),
create_addresses: machine.create_addresses(),
events: machine.events()
}
}

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
10 changes: 10 additions & 0 deletions crates/evm/src/model.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
use evm::execution::Status;
use starknet::EthAddress;
#[derive(Drop)]
struct Event {
keys: Array<u256>,
data: Array<u8>,
}

struct ExecutionResult {
status: Status,
return_data: Span<u8>,
create_addresses: Span<EthAddress>,
destroyed_contracts: Span<EthAddress>,
events: Span<Event>
}
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