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

fix: replace tuple in sstore return with struct #1115

Merged
merged 3 commits into from
Feb 21, 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
21 changes: 15 additions & 6 deletions crates/interpreter/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,7 @@ pub trait Host {
/// Set storage value of account address at index.
///
/// Returns (original, present, new, is_cold).
fn sstore(
&mut self,
address: Address,
index: U256,
value: U256,
) -> Option<(U256, U256, U256, bool)>;
fn sstore(&mut self, address: Address, index: U256, value: U256) -> Option<SStoreResult>;

/// Get the transient storage value of `address` at `index`.
fn tload(&mut self, address: Address, index: U256) -> U256;
Expand All @@ -56,3 +51,17 @@ pub trait Host {
/// Mark `address` to be deleted, with funds transferred to `target`.
fn selfdestruct(&mut self, address: Address, target: Address) -> Option<SelfDestructResult>;
}

/// Represents the result of an `sstore` operation.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SStoreResult {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if this is the right file to place it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The place is fine, would add more comments.

original: Value of the storage when it is first read
present: Current value of the storage
new_value: Values that that is set
is_cold: Is storage slot loaded from database

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated!

/// Value of the storage when it is first read
pub original_value: U256,
/// Current value of the storage
pub present_value: U256,
/// New value that is set
pub new_value: U256,
/// Is storage slot loaded from database
pub is_cold: bool,
}
16 changes: 8 additions & 8 deletions crates/interpreter/src/host/dummy.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::primitives::{hash_map::Entry, Bytecode, HashMap, U256};
use crate::{
primitives::{Address, Env, Log, B256, KECCAK_EMPTY},
Host, SelfDestructResult,
Host, SStoreResult, SelfDestructResult,
};
use std::vec::Vec;

Expand Down Expand Up @@ -80,12 +80,7 @@ impl Host for DummyHost {
}

#[inline]
fn sstore(
&mut self,
_address: Address,
index: U256,
value: U256,
) -> Option<(U256, U256, U256, bool)> {
fn sstore(&mut self, _address: Address, index: U256, value: U256) -> Option<SStoreResult> {
let (present, is_cold) = match self.storage.entry(index) {
Entry::Occupied(mut entry) => (entry.insert(value), false),
Entry::Vacant(entry) => {
Expand All @@ -94,7 +89,12 @@ impl Host for DummyHost {
}
};

Some((U256::ZERO, present, value, is_cold))
Some(SStoreResult {
original_value: U256::ZERO,
present_value: present,
new_value: value,
is_cold,
})
}

#[inline]
Expand Down
10 changes: 7 additions & 3 deletions crates/interpreter/src/instructions/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
interpreter::{Interpreter, InterpreterAction},
primitives::{Address, Bytes, Log, LogData, Spec, SpecId::*, B256, U256},
CallContext, CallInputs, CallScheme, CreateInputs, CreateScheme, Host, InstructionResult,
Transfer, MAX_INITCODE_SIZE,
SStoreResult, Transfer, MAX_INITCODE_SIZE,
};
use core::cmp::min;
use revm_primitives::BLOCK_HASH_HISTORY;
Expand Down Expand Up @@ -154,8 +154,12 @@ pub fn sstore<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H)
check_staticcall!(interpreter);

pop!(interpreter, index, value);
let Some((original, old, new, is_cold)) =
host.sstore(interpreter.contract.address, index, value)
let Some(SStoreResult {
original_value: original,
present_value: old,
new_value: new,
is_cold,
}) = host.sstore(interpreter.contract.address, index, value)
else {
interpreter.instruction_result = InstructionResult::FatalExternalError;
return;
Expand Down
2 changes: 1 addition & 1 deletion crates/interpreter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mod interpreter;
pub use call_outcome::CallOutcome;
pub use create_outcome::CreateOutcome;
pub use gas::Gas;
pub use host::{DummyHost, Host};
pub use host::{DummyHost, Host, SStoreResult};
pub use inner_models::*;
pub use instruction_result::*;
pub use instructions::{opcode, Instruction, OpCode, OPCODE_JUMPMAP};
Expand Down
8 changes: 2 additions & 6 deletions crates/revm/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
},
FrameOrResult, JournalCheckpoint, CALL_STACK_LIMIT,
};
use revm_interpreter::SStoreResult;
use std::boxed::Box;

/// Main Context structure that contains both EvmContext and External context.
Expand Down Expand Up @@ -261,12 +262,7 @@ impl<DB: Database> EvmContext<DB> {
/// Storage change of storage slot, before storing `sload` will be called for that slot.
#[inline]
#[must_use]
pub fn sstore(
&mut self,
address: Address,
index: U256,
value: U256,
) -> Option<(U256, U256, U256, bool)> {
pub fn sstore(&mut self, address: Address, index: U256, value: U256) -> Option<SStoreResult> {
self.journaled_state
.sstore(address, index, value, &mut self.db)
.map_err(|e| self.error = Some(e))
Expand Down
11 changes: 3 additions & 8 deletions crates/revm/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::{
db::{Database, DatabaseCommit, EmptyDB},
handler::Handler,
interpreter::{
opcode::InstructionTables, Host, Interpreter, InterpreterAction, SelfDestructResult,
SharedMemory,
opcode::InstructionTables, Host, Interpreter, InterpreterAction, SStoreResult,
SelfDestructResult, SharedMemory,
},
primitives::{
specification::SpecId, Address, BlockEnv, Bytecode, CfgEnv, EVMError, EVMResult, Env,
Expand Down Expand Up @@ -392,12 +392,7 @@ impl<EXT, DB: Database> Host for Evm<'_, EXT, DB> {
self.context.evm.sload(address, index)
}

fn sstore(
&mut self,
address: Address,
index: U256,
value: U256,
) -> Option<(U256, U256, U256, bool)> {
fn sstore(&mut self, address: Address, index: U256, value: U256) -> Option<SStoreResult> {
self.context.evm.sstore(address, index, value)
}

Expand Down
17 changes: 14 additions & 3 deletions crates/revm/src/journaled_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::primitives::{
};
use core::mem;
use revm_interpreter::primitives::SpecId;
use revm_interpreter::SStoreResult;
use std::vec::Vec;

/// JournalState is internal EVM state that is used to contain state and track changes to that state.
Expand Down Expand Up @@ -652,7 +653,7 @@ impl JournaledState {
key: U256,
new: U256,
db: &mut DB,
) -> Result<(U256, U256, U256, bool), DB::Error> {
) -> Result<SStoreResult, DB::Error> {
// assume that acc exists and load the slot.
let (present, is_cold) = self.sload(address, key, db)?;
let acc = self.state.get_mut(&address).unwrap();
Expand All @@ -662,7 +663,12 @@ impl JournaledState {

// new value is same as present, we don't need to do anything
if present == new {
return Ok((slot.previous_or_original_value, present, new, is_cold));
return Ok(SStoreResult {
original_value: slot.previous_or_original_value,
present_value: present,
new_value: new,
is_cold,
});
}

self.journal
Expand All @@ -675,7 +681,12 @@ impl JournaledState {
});
// insert value into present state.
slot.present_value = new;
Ok((slot.previous_or_original_value, present, new, is_cold))
Ok(SStoreResult {
original_value: slot.previous_or_original_value,
present_value: present,
new_value: new,
is_cold,
})
}

/// Read transient storage tied to the account.
Expand Down
Loading