Skip to content

Commit

Permalink
fix(levm): transient storage should be reverted when callframe reverts (
Browse files Browse the repository at this point in the history
#1552)

**Motivation**

The transient storage was not being reverted when callframe reverts

**Description**

- Create a copy of the transient storage at the beginning of the
callframe
- Restore this copy if the callframe finishes with revert
  • Loading branch information
LeanSerra authored Dec 20, 2024
1 parent e6917e9 commit ef1f3a1
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions crates/vm/levm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
},
opcodes::Opcode,
precompiles::{execute_precompile, is_precompile},
AccountInfo,
AccountInfo, TransientStorage,
};
use bytes::Bytes;
use ethrex_core::{types::TxKind, Address, H256, U256};
Expand Down Expand Up @@ -259,11 +259,12 @@ impl VM {
&mut self,
current_call_frame: &mut CallFrame,
) -> Result<TransactionReport, VMError> {
// Backup of Database, Substate and Gas Refunds if sub-context is reverted
let (backup_db, backup_substate, backup_refunded_gas) = (
// Backup of Database, Substate, Gas Refunds and Transient Storage if sub-context is reverted
let (backup_db, backup_substate, backup_refunded_gas, backup_transient_storage) = (
self.cache.clone(),
self.accrued_substate.clone(),
self.env.refunded_gas,
self.env.transient_storage.clone(),
);

if is_precompile(&current_call_frame.code_address) {
Expand All @@ -290,7 +291,12 @@ impl VM {

self.call_frames.push(current_call_frame.clone());

self.restore_state(backup_db, backup_substate, backup_refunded_gas);
self.restore_state(
backup_db,
backup_substate,
backup_refunded_gas,
backup_transient_storage,
);

return Ok(TransactionReport {
result: TxResult::Revert(error),
Expand Down Expand Up @@ -459,7 +465,12 @@ impl VM {
Err(error) => {
// Revert if error
current_call_frame.gas_used = current_call_frame.gas_limit;
self.restore_state(backup_db, backup_substate, backup_refunded_gas);
self.restore_state(
backup_db,
backup_substate,
backup_refunded_gas,
backup_transient_storage,
);

return Ok(TransactionReport {
result: TxResult::Revert(error),
Expand Down Expand Up @@ -500,7 +511,12 @@ impl VM {
current_call_frame.gas_used.saturating_add(left_gas);
}

self.restore_state(backup_db, backup_substate, backup_refunded_gas);
self.restore_state(
backup_db,
backup_substate,
backup_refunded_gas,
backup_transient_storage,
);

return Ok(TransactionReport {
result: TxResult::Revert(error),
Expand All @@ -521,10 +537,12 @@ impl VM {
backup_cache: CacheDB,
backup_substate: Substate,
backup_refunded_gas: U256,
backup_transient_storage: TransientStorage,
) {
self.cache = backup_cache;
self.accrued_substate = backup_substate;
self.env.refunded_gas = backup_refunded_gas;
self.env.transient_storage = backup_transient_storage;
}

fn is_create(&self) -> bool {
Expand Down

0 comments on commit ef1f3a1

Please sign in to comment.