Skip to content

Commit

Permalink
dupl app_event + setup_readonly
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentash committed Oct 3, 2023
1 parent 2d7917c commit bffc21e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 13 deletions.
5 changes: 5 additions & 0 deletions crates/evm/src/context.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,9 @@ impl ExecutionContextImpl of ExecutionContextTrait {
fn pc(self: @ExecutionContext) -> u32 {
*self.program_counter
}

#[inline(always)]
fn append_event(ref self: ExecutionContext, event: Event) {
self.events.append(event);
}
}
11 changes: 1 addition & 10 deletions crates/evm/src/machine.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -189,19 +189,10 @@ impl MachineCurrentContextImpl of MachineCurrentContextTrait {
current_call_ctx.read_only()
}

#[inline(always)]
fn set_read_only(ref self: Machine, value: bool) {
let mut current_call_ctx = self.call_context();
let mut current_execution_ctx = self.current_context.unbox();
current_call_ctx.read_only = value;
current_execution_ctx.call_context = BoxTrait::new(current_call_ctx);
self.current_context = BoxTrait::new(current_execution_ctx);
}

#[inline(always)]
fn append_event(ref self: Machine, event: Event) {
let mut current_execution_ctx = self.current_context.unbox();
current_execution_ctx.events.append(event);
current_execution_ctx.append_event(event);
self.current_context = BoxTrait::new(current_execution_ctx);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use evm::instructions::LoggingOperationsTrait;
use evm::machine::{Machine, MachineCurrentContextTrait};
use evm::memory::MemoryTrait;
use evm::stack::StackTrait;
use evm::tests::test_utils::setup_machine;
use evm::tests::test_utils::{setup_machine, setup_machine_with_read_only};
use integer::BoundedInt;
use utils::helpers::u256_to_bytes_array;

Expand Down Expand Up @@ -180,8 +180,7 @@ fn test_exec_log4() {
#[available_gas(20000000)]
fn test_exec_log1_read_only_context() {
// Given
let mut machine = setup_machine();
machine.set_read_only(true);
let mut machine = setup_machine_with_read_only();

machine.memory.store(BoundedInt::<u256>::max(), 0);

Expand Down
40 changes: 40 additions & 0 deletions crates/evm/src/tests/test_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,43 @@ fn setup_machine_with_calldata(calldata: Span<u8>) -> Machine {
storage_journal: Default::default(),
}
}

fn setup_machine_with_read_only() -> Machine {
Machine {
current_context: BoxTrait::new(setup_execution_context_with_read_only()),
ctx_count: 1,
stack: Default::default(),
memory: Default::default(),
storage_journal: Default::default(),
}
}

fn setup_execution_context_with_read_only() -> ExecutionContext {
let context_id = 0;
let call_context = setup_call_context_with_read_only();
let starknet_address: ContractAddress = starknet_address();
let evm_address: EthAddress = evm_address();
let return_data = Default::default();

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

fn setup_call_context_with_read_only() -> CallContext {
let bytecode: Span<u8> = array![1, 2, 3].span();
let calldata: Span<u8> = array![4, 5, 6].span();
let value: u256 = callvalue();
let address = evm_address();
let read_only = true;
let gas_price = 0xaaaaaa;
let gas_limit = 0xffffff;

CallContextTrait::new(address, bytecode, calldata, value, read_only, gas_limit, gas_price)
}

0 comments on commit bffc21e

Please sign in to comment.