Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add missing test EIP-1706 #875

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -893,11 +895,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() {
obatirou marked this conversation as resolved.
Show resolved Hide resolved
// 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 @@ -908,7 +910,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
Loading