Skip to content

Commit

Permalink
refactor: refactor child context to be child context return data
Browse files Browse the repository at this point in the history
  • Loading branch information
Eikix committed Oct 3, 2023
1 parent 31195a2 commit c60c610
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 11 deletions.
17 changes: 14 additions & 3 deletions crates/evm/src/context.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ impl DefaultBoxCallContext of Default<Box<CallContext>> {
}
}

impl DefaultOptionSpanU8 of Default<Option<Span<u8>>> {
fn default() -> Option<Span<u8>> {
Option::None
}
}


// *************************************************************************
// ExecutionContext
Expand All @@ -136,7 +142,7 @@ struct ExecutionContext {
create_addresses: Array<EthAddress>,
return_data: Array<u8>,
parent_context: Nullable<ExecutionContext>,
child_context: Nullable<ExecutionContext>,
child_context_return_data: Option<Span<u8>>,
}

impl DefaultBoxExecutionContext of Default<Box<ExecutionContext>> {
Expand All @@ -159,7 +165,7 @@ impl ExecutionContextImpl of ExecutionContextTrait {
starknet_address: ContractAddress,
call_context: CallContext,
parent_context: Nullable<ExecutionContext>,
child_context: Nullable<ExecutionContext>,
child_context_return_data: Option<Span<u8>>,
return_data: Array<u8>,
) -> ExecutionContext {
ExecutionContext {
Expand All @@ -184,7 +190,7 @@ impl ExecutionContextImpl of ExecutionContextTrait {
create_addresses: Default::default(),
return_data,
parent_context,
child_context,
child_context_return_data,
}
}

Expand Down Expand Up @@ -314,4 +320,9 @@ impl ExecutionContextImpl of ExecutionContextTrait {
fn pc(self: @ExecutionContext) -> u32 {
*self.program_counter
}

#[inline(always)]
fn child_context_return_data(self: @ExecutionContext) -> Option<Span<u8>> {
*self.child_context_return_data
}
}
10 changes: 10 additions & 0 deletions crates/evm/src/machine.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,14 @@ impl MachineCurrentContextImpl of MachineCurrentContextTrait {
current_execution_ctx.return_data = value;
self.current_context = BoxTrait::new(current_execution_ctx);
}

/// Getter for the return data of a child context, accessed from its parent context
/// Enabler for RETURNDATASIZE and RETURNDATACOPY opcodes
#[inline(always)]
fn child_context_return_data(ref self: Machine) -> Option<Span<u8>> {
let mut current_execution_ctx = self.current_context.unbox();
let child_context_return_data = current_execution_ctx.child_context_return_data();
self.current_context = BoxTrait::new(current_execution_ctx);
child_context_return_data
}
}
28 changes: 22 additions & 6 deletions crates/evm/src/tests/test_execution_context.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use core::nullable::{NullableTrait, null};
use debug::PrintTrait;
use evm::context::{CallContext, CallContextTrait, ExecutionContext, ExecutionContextTrait};
use evm::context::{
CallContext, CallContextTrait, ExecutionContext, ExecutionContextTrait, DefaultOptionSpanU8
};
use evm::memory::{Memory, MemoryTrait};
use evm::model::Event;
use evm::stack::{Stack, StackTrait};
Expand Down Expand Up @@ -64,7 +66,6 @@ fn test_execution_context_new() {
let read_only: bool = false;

let parent_context: Nullable<ExecutionContext> = null();
let child_context: Nullable<ExecutionContext> = null();

// When
let mut execution_context = ExecutionContextTrait::new(
Expand All @@ -73,7 +74,7 @@ fn test_execution_context_new() {
starknet_address,
call_context,
parent_context,
child_context,
Default::default(),
return_data
);

Expand Down Expand Up @@ -143,12 +144,27 @@ fn test_execution_context_read_code() {
#[available_gas(300000)]
#[ignore]
fn test_is_root() {
// TODO: finish this test once calling_contexts are implemented
// Given
let mut execution_context = setup_execution_context();

// When
let is_root = execution_context.is_root();
// Then
// assert(is_root == true, 'should not be a leaf');

// Then
assert(is_root, 'should not be a leaf');
}


#[test]
#[available_gas(300000)]
#[ignore]
fn test_child_context_return_data() {
// Given
let mut execution_context = setup_execution_context();

// When
let child_return_data = execution_context.child_context_return_data().unwrap();

// Then
assert(child_return_data == array![1, 2, 3].span(), 'should not be a leaf');
}
10 changes: 10 additions & 0 deletions crates/evm/src/tests/test_machine.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,13 @@ fn test_return_data() {
let return_data = machine.return_data();
assert(return_data.len() == 0, 'wrong length');
}


#[test]
#[available_gas(20000000)]
fn test_child_context_return_data() {
let mut machine: Machine = setup_machine();

let return_data = machine.child_context_return_data().unwrap();
assert(return_data == array![1, 2, 3].span(), 'wrong child return data');
}
7 changes: 5 additions & 2 deletions crates/evm/src/tests/test_utils.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use evm::context::{CallContext, CallContextTrait, ExecutionContext, ExecutionContextTrait,};
use evm::context::{
CallContext, CallContextTrait, ExecutionContext, ExecutionContextTrait, DefaultOptionSpanU8
};

use evm::machine::Machine;
use starknet::{contract_address_try_from_felt252, ContractAddress, EthAddress};
Expand Down Expand Up @@ -37,14 +39,15 @@ fn setup_execution_context() -> ExecutionContext {
let starknet_address: ContractAddress = starknet_address();
let evm_address: EthAddress = evm_address();
let return_data = Default::default();
let child_context_return_data = Option::Some(array![1, 2, 3].span());

ExecutionContextTrait::new(
context_id,
evm_address,
starknet_address,
call_context,
Default::default(),
Default::default(),
child_context_return_data,
return_data,
)
}
Expand Down

0 comments on commit c60c610

Please sign in to comment.