diff --git a/crates/evm/src/instructions/memory_operations.cairo b/crates/evm/src/instructions/memory_operations.cairo index 427bbb2fe..c54859270 100644 --- a/crates/evm/src/instructions/memory_operations.cairo +++ b/crates/evm/src/instructions/memory_operations.cairo @@ -104,9 +104,6 @@ impl MemoryOperation of MemoryOperationTrait { /// Save 32-byte word to storage. /// # Specification: https://www.evm.codes/#55?fork=shanghai fn exec_sstore(ref self: VM) -> Result<(), EVMError> { - ensure(!self.message().read_only, EVMError::WriteInStaticContext)?; - ensure(self.gas_left() > gas::CALL_STIPEND, EVMError::OutOfGas)?; //EIP-1706 - let key = self.stack.pop()?; let new_value = self.stack.pop()?; let evm_address = self.message().target.evm; @@ -157,8 +154,13 @@ impl MemoryOperation of MemoryOperationTrait { } } } + let gas_left_sup_call_stipend = self.gas_left() > gas::CALL_STIPEND; + self.charge_gas(gas_cost)?; + ensure(!self.message().read_only, EVMError::WriteInStaticContext)?; + ensure(gas_left_sup_call_stipend, EVMError::OutOfGas)?; // EIP-1706 + self.env.state.write_state(:evm_address, :key, value: new_value); Result::Ok(()) } @@ -921,11 +923,11 @@ mod tests { #[test] - fn test_exec_sstore_static_call() { + #[ignore] + //TODO(sn-foundry): fix `Contract not deployed at address: 0x0` + fn test_exec_sstore_should_fail_static_call() { // Given - let (_, kakarot_core) = setup_contracts_for_testing(); let mut vm = VMBuilderTrait::new_with_presets().with_read_only().build(); - deploy_contract_account(kakarot_core, vm.message().target.evm, [].span()); let key: u256 = 0x100000000000000000000000000000001; let value: u256 = 0xABDE1E11A5; vm.stack.push(value).expect('push failed'); @@ -936,7 +938,26 @@ mod tests { // Then assert(result.is_err(), 'should have errored'); - assert(result.unwrap_err() == EVMError::WriteInStaticContext, 'wrong error variant'); + assert(result.unwrap_err() == EVMError::WriteInStaticContext, 'wrong error returned'); + } + + #[test] + #[ignore] + //TODO(sn-foundry): fix `Contract not deployed at address: 0x0` + fn test_exec_sstore_should_fail_gas_left_inf_call_stipend_eip_1706() { + // Given + let mut vm = VMBuilderTrait::new_with_presets().with_gas_left(gas::CALL_STIPEND).build(); + let key: u256 = 0x100000000000000000000000000000001; + let value: u256 = 0xABDE1E11A5; + vm.stack.push(value).expect('push failed'); + vm.stack.push(key).expect('push failed'); + + // When + let result = vm.exec_sstore(); + + // Then + assert(result.is_err(), 'should have errored'); + assert(result.unwrap_err() == EVMError::OutOfGas, 'wrong error returned'); } #[test] diff --git a/crates/evm/src/test_utils.cairo b/crates/evm/src/test_utils.cairo index fc36bf4c7..d4facbacf 100644 --- a/crates/evm/src/test_utils.cairo +++ b/crates/evm/src/test_utils.cairo @@ -113,6 +113,11 @@ impl VMBuilderImpl of VMBuilderTrait { self.vm.valid_jumpdests = AccountTrait::get_jumpdests(self.vm.message.code); return self.vm; } + + fn with_gas_left(mut self: VMBuilder, gas_left: u128) -> VMBuilder { + self.vm.gas_left = gas_left; + self + } } fn origin() -> EthAddress {