diff --git a/crates/evm/src/machine.cairo b/crates/evm/src/machine.cairo index bccfbc1c3..b1a1014c7 100644 --- a/crates/evm/src/machine.cairo +++ b/crates/evm/src/machine.cairo @@ -67,7 +67,7 @@ impl MachineCurrentContextImpl of MachineCurrentContextTrait { /// to divide a unique Stack/Memory simulated by a dict into /// multiple sub-structures relative to a single context. #[inline(always)] - fn set_active_execution_ctx(ref self: Machine, ctx: ExecutionContext) { + fn set_current_context(ref self: Machine, ctx: ExecutionContext) { self.memory.set_active_segment(ctx.id); self.stack.set_active_segment(ctx.id); self.current_context = BoxTrait::new(ctx); diff --git a/crates/evm/src/tests/test_machine.cairo b/crates/evm/src/tests/test_machine.cairo index 89e14e9f3..50841f94b 100644 --- a/crates/evm/src/tests/test_machine.cairo +++ b/crates/evm/src/tests/test_machine.cairo @@ -1,7 +1,8 @@ use evm::context::CallContextTrait; use evm::machine::{Machine, MachineCurrentContextTrait}; use evm::tests::test_utils::{ - evm_address, setup_machine_with_bytecode, setup_machine, starknet_address + evm_address, setup_machine_with_bytecode, setup_machine, starknet_address, + setup_execution_context }; @@ -16,6 +17,28 @@ fn test_machine_default() { assert(!machine.stopped(), 'ctx should not be stopped'); } +#[test] +#[available_gas(20000000)] +fn test_set_current_context() { + let mut machine: Machine = Default::default(); + + let first_ctx = machine.current_context.unbox(); + assert(first_ctx.id == 0, 'wrong first id'); + // We need to re-box the context into the machine, otherwise we have a "Variable Moved" error. + machine.current_context = BoxTrait::new(first_ctx); + assert(machine.stack.active_segment == 0, 'wrong initial stack segment'); + assert(machine.memory.active_segment == 0, 'wrong initial memory segment'); + + // Create another context with id=1 + let mut second_ctx = setup_execution_context(); + second_ctx.id = 1; + + machine.set_current_context(second_ctx); + assert(machine.stack.active_segment == 1, 'wrong updated stack segment'); + assert(machine.memory.active_segment == 1, 'wrong updated stack segment'); + assert(machine.current_context.unbox().id == 1, 'wrong updated id'); +} + #[test] #[available_gas(20000000)] fn test_set_pc() {