Skip to content

Commit

Permalink
test: add missing test EIP-1706 (#875)
Browse files Browse the repository at this point in the history
* test: add missing test EIP-1706

* refactor: checks after charge gas
  • Loading branch information
obatirou authored Aug 28, 2024
1 parent 89760c5 commit 5191cc8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
35 changes: 28 additions & 7 deletions crates/evm/src/instructions/memory_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(())
}
Expand Down Expand Up @@ -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');
Expand All @@ -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]
Expand Down
5 changes: 5 additions & 0 deletions crates/evm/src/test_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 5191cc8

Please sign in to comment.