Skip to content

Commit

Permalink
resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmadkaouk committed Jun 25, 2024
1 parent 924e068 commit c4bad3c
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 108 deletions.
7 changes: 1 addition & 6 deletions interpreter/src/eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use crate::{
machine::Machine,
opcode::Opcode,
runtime::{GasState, RuntimeBackend, RuntimeEnvironment, RuntimeState},
fork::Fork,
};

pub fn eval_pass<S, H, Tr>(
Expand Down Expand Up @@ -725,11 +724,7 @@ pub fn eval_suicide<S: AsRef<RuntimeState>, H: RuntimeEnvironment + RuntimeBacke
_opcode: Opcode,
_position: usize,
) -> Control<Tr> {
if machine.state.as_ref().fork >= Fork::CANCUN {
self::system::suicide_eip_6780(machine, handle)
} else {
self::system::suicide(machine, handle)
}
self::system::suicide(machine, handle)
}

pub fn eval_chainid<S: AsRef<RuntimeState>, H: RuntimeEnvironment + RuntimeBackend, Tr>(
Expand Down
38 changes: 1 addition & 37 deletions interpreter/src/eval/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,43 +362,7 @@ pub fn suicide<S: AsRef<RuntimeState>, H: RuntimeEnvironment + RuntimeBackend, T
value: balance,
})?;

handler.mark_delete(address);
handler.reset_balance(address);

Ok(((), ()))
}) {
Ok(()) => Control::Exit(ExitSucceed::Suicided.into()),
Err(e) => Control::Exit(Err(e)),
}
}

pub fn suicide_eip_6780<S: AsRef<RuntimeState>, H: RuntimeEnvironment + RuntimeBackend, Tr>(
machine: &mut Machine<S>,
handler: &mut H,
) -> Control<Tr> {
let address = machine.state.as_ref().context.address;

match machine.stack.perform_pop1_push0(|target| {
let balance = handler.balance(address);
let target = (*target).into();

// Cancun EIP-6780 only allow contract deletion within the same transaction that created it
if handler.created(address) {
handler.transfer(Transfer {
source: address,
target,
value: balance,
})?;

handler.mark_delete(address);
handler.reset_balance(address);
} else if address != target {
handler.transfer(Transfer {
source: address,
target,
value: balance,
})?;
}
handler.mark_delete_reset(address);
Ok(((), ()))
}) {
Ok(()) => Control::Exit(ExitSucceed::Suicided.into()),
Expand Down
11 changes: 0 additions & 11 deletions interpreter/src/fork.rs

This file was deleted.

1 change: 0 additions & 1 deletion interpreter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ pub mod machine;
pub mod opcode;
pub mod runtime;
pub mod utils;
pub mod fork;

pub use self::interpreter::{EtableInterpreter, Interpreter, RunInterpreter, StepInterpreter};
10 changes: 4 additions & 6 deletions interpreter/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use alloc::{rc::Rc, vec::Vec};
use primitive_types::{H160, H256, U256};
use sha3::{Digest, Keccak256};

use crate::{error::ExitError, fork::Fork};
use crate::error::ExitError;

/// Gas state.
pub trait GasState {
Expand All @@ -19,8 +19,6 @@ pub struct RuntimeState {
pub transaction_context: Rc<TransactionContext>,
/// Return data buffer.
pub retbuf: Vec<u8>,
/// EVM Fork.
pub fork: Fork,
}

impl AsRef<Self> for RuntimeState {
Expand Down Expand Up @@ -155,9 +153,9 @@ pub trait RuntimeBackend: RuntimeBaseBackend {
) -> Result<(), ExitError>;
/// Create a log owned by address with given topics and data.
fn log(&mut self, log: Log) -> Result<(), ExitError>;
/// Mark an address to be deleted.
fn mark_delete(&mut self, address: H160);
// Mark an address that is being created in the transaction.
/// Mark an address to be deleted and its balance to be reset.
fn mark_delete_reset(&mut self, address: H160);
// Mark an address as created in the current transaction.
fn mark_create(&mut self, address: H160);
/// Fully delete storages of an account.
fn reset_storage(&mut self, address: H160);
Expand Down
6 changes: 2 additions & 4 deletions interpreter/tests/usability.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use evm_interpreter::fork::Fork;
use std::rc::Rc;
use evm_interpreter::{
error::{CallCreateTrap, Capture, ExitError, ExitSucceed},
etable::{Control, Etable},
Expand All @@ -12,6 +10,7 @@ use evm_interpreter::{
EtableInterpreter, RunInterpreter,
};
use primitive_types::{H160, H256, U256};
use std::rc::Rc;

const CODE1: &str = "60e060020a6000350480632839e92814601e57806361047ff414603457005b602a6004356024356047565b8060005260206000f35b603d6004356099565b8060005260206000f35b600082600014605457605e565b8160010190506093565b81600014606957607b565b60756001840360016047565b90506093565b609060018403608c85600186036047565b6047565b90505b92915050565b6000816000148060a95750816001145b60b05760b7565b81905060cf565b60c1600283036099565b60cb600184036099565b0190505b91905056";
const DATA1: &str = "2839e92800000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000001";
Expand Down Expand Up @@ -162,7 +161,7 @@ impl RuntimeBackend for UnimplementedHandler {
fn log(&mut self, _log: Log) -> Result<(), ExitError> {
unimplemented!()
}
fn mark_delete(&mut self, _address: H160) {
fn mark_delete_reset(&mut self, _address: H160) {
unimplemented!()
}

Expand Down Expand Up @@ -220,7 +219,6 @@ fn etable_runtime() {
}
.into(),
retbuf: Vec::new(),
fork: Fork::FRONTIER,
},
);
let mut vm = EtableInterpreter::new(machine, &RUNTIME_ETABLE);
Expand Down
4 changes: 2 additions & 2 deletions jsontests/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ pub fn run_test(
state,
};

let mut run_backend = OverlayedBackend::new(&base_backend, initial_accessed.clone());
let mut step_backend = OverlayedBackend::new(&base_backend, initial_accessed.clone());
let mut run_backend = OverlayedBackend::new(&base_backend, initial_accessed.clone(), &config);
let mut step_backend = OverlayedBackend::new(&base_backend, initial_accessed.clone(), &config);

// Run
let run_result = evm::transact(args.clone(), Some(4), &mut run_backend, &invoker);
Expand Down
34 changes: 24 additions & 10 deletions src/backend/overlayed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use evm_interpreter::{
};
use primitive_types::{H160, H256, U256};

use crate::{backend::TransactionalBackend, MergeStrategy};
use crate::{backend::TransactionalBackend, standard::Config, MergeStrategy};

#[derive(Clone, Debug)]
pub struct OverlayedChangeSet {
Expand All @@ -25,18 +25,24 @@ pub struct OverlayedChangeSet {
pub deletes: BTreeSet<H160>,
}

pub struct OverlayedBackend<B> {
pub struct OverlayedBackend<'config, B> {
backend: B,
substate: Box<Substate>,
accessed: BTreeSet<(H160, Option<H256>)>,
config: &'config Config,
}

impl<B> OverlayedBackend<B> {
pub fn new(backend: B, accessed: BTreeSet<(H160, Option<H256>)>) -> Self {
impl<'config, B> OverlayedBackend<'config, B> {
pub fn new(
backend: B,
accessed: BTreeSet<(H160, Option<H256>)>,
config: &'config Config,
) -> Self {
Self {
backend,
substate: Box::new(Substate::new()),
accessed,
config,
}
}

Expand All @@ -57,7 +63,7 @@ impl<B> OverlayedBackend<B> {
}
}

impl<B: RuntimeEnvironment> RuntimeEnvironment for OverlayedBackend<B> {
impl<B: RuntimeEnvironment> RuntimeEnvironment for OverlayedBackend<'_, B> {
fn block_hash(&self, number: U256) -> H256 {
self.backend.block_hash(number)
}
Expand Down Expand Up @@ -95,7 +101,7 @@ impl<B: RuntimeEnvironment> RuntimeEnvironment for OverlayedBackend<B> {
}
}

impl<B: RuntimeBaseBackend> RuntimeBaseBackend for OverlayedBackend<B> {
impl<B: RuntimeBaseBackend> RuntimeBaseBackend for OverlayedBackend<'_, B> {
fn balance(&self, address: H160) -> U256 {
if let Some(balance) = self.substate.known_balance(address) {
balance
Expand Down Expand Up @@ -145,7 +151,7 @@ impl<B: RuntimeBaseBackend> RuntimeBaseBackend for OverlayedBackend<B> {
}
}

impl<B: RuntimeBaseBackend> RuntimeBackend for OverlayedBackend<B> {
impl<B: RuntimeBaseBackend> RuntimeBackend for OverlayedBackend<'_, B> {
fn original_storage(&self, address: H160, index: H256) -> H256 {
self.backend.storage(address, index)
}
Expand Down Expand Up @@ -188,8 +194,16 @@ impl<B: RuntimeBaseBackend> RuntimeBackend for OverlayedBackend<B> {
Ok(())
}

fn mark_delete(&mut self, address: H160) {
self.substate.deletes.insert(address);
fn mark_delete_reset(&mut self, address: H160) {
if self.config.suicide_only_in_same_tx {
if self.created(address) {
self.substate.deletes.insert(address);
self.substate.storage_resets.insert(address);
}
} else {
self.substate.deletes.insert(address);
self.substate.storage_resets.insert(address);
}
}

fn mark_create(&mut self, address: H160) {
Expand Down Expand Up @@ -241,7 +255,7 @@ impl<B: RuntimeBaseBackend> RuntimeBackend for OverlayedBackend<B> {
}
}

impl<B: RuntimeBaseBackend> TransactionalBackend for OverlayedBackend<B> {
impl<'config, B: RuntimeBaseBackend> TransactionalBackend for OverlayedBackend<'config, B> {
fn push_substate(&mut self) {
let mut parent = Box::new(Substate::new());
mem::swap(&mut parent, &mut self.substate);
Expand Down
25 changes: 12 additions & 13 deletions src/standard/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use evm_interpreter::fork::{Fork, Fork::*};

/// Runtime configuration.
#[derive(Clone, Debug)]
pub struct Config {
Expand Down Expand Up @@ -105,7 +103,8 @@ pub struct Config {
pub eip_5656_enabled: bool,
/// Uses EIP-1559 (Base fee is burned when this flag is enabled) [EIP-1559](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1559.md)
pub eip_1559_enabled: bool,
pub fork: Fork,
/// Selfdestruct deletet contract only if called in the same tx as creation [EIP-6780](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-6780.md)
pub suicide_only_in_same_tx: bool,
}

impl Config {
Expand Down Expand Up @@ -162,7 +161,7 @@ impl Config {
eip_1153_enabled: false,
eip_5656_enabled: false,
eip_1559_enabled: false,
fork: FRONTIER,
suicide_only_in_same_tx: false,
}
}

Expand Down Expand Up @@ -219,7 +218,7 @@ impl Config {
eip_1153_enabled: false,
eip_5656_enabled: false,
eip_1559_enabled: false,
fork: ISTANBUL,
suicide_only_in_same_tx: false,
}
}

Expand Down Expand Up @@ -262,7 +261,7 @@ impl Config {
eip_1153_enabled,
eip_5656_enabled,
eip_1559_enabled,
fork,
suicide_only_in_same_tx,
} = inputs;

// See https://eips.ethereum.org/EIPS/eip-2929
Expand Down Expand Up @@ -328,7 +327,7 @@ impl Config {
eip_1153_enabled,
eip_5656_enabled,
eip_1559_enabled,
fork,
suicide_only_in_same_tx,
}
}
}
Expand All @@ -351,7 +350,7 @@ struct DerivedConfigInputs {
eip_1153_enabled: bool,
eip_5656_enabled: bool,
eip_1559_enabled: bool,
fork: Fork,
suicide_only_in_same_tx: bool,
}

impl DerivedConfigInputs {
Expand All @@ -369,7 +368,7 @@ impl DerivedConfigInputs {
eip_1153_enabled: false,
eip_5656_enabled: false,
eip_1559_enabled: false,
fork: BERLIN,
suicide_only_in_same_tx: false,
}
}

Expand All @@ -387,7 +386,7 @@ impl DerivedConfigInputs {
eip_1153_enabled: false,
eip_5656_enabled: false,
eip_1559_enabled: true,
fork: LONDON,
suicide_only_in_same_tx: false,
}
}

Expand All @@ -405,7 +404,7 @@ impl DerivedConfigInputs {
eip_1153_enabled: false,
eip_5656_enabled: false,
eip_1559_enabled: true,
fork: MERGE,
suicide_only_in_same_tx: false,
}
}

Expand All @@ -424,7 +423,7 @@ impl DerivedConfigInputs {
eip_1153_enabled: false,
eip_5656_enabled: false,
eip_1559_enabled: true,
fork: SHANGHAI,
suicide_only_in_same_tx: false,
}
}
const fn cancun() -> Self {
Expand All @@ -442,7 +441,7 @@ impl DerivedConfigInputs {
eip_1153_enabled: false,
eip_5656_enabled: false,
eip_1559_enabled: true,
fork: CANCUN,
suicide_only_in_same_tx: true,
}
}
}
3 changes: 0 additions & 3 deletions src/standard/invoker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ where
context,
transaction_context: Rc::new(transaction_context),
retbuf: Vec::new(),
fork: self.config.fork,
};

let work = || -> Result<(TransactInvoke, _), ExitError> {
Expand Down Expand Up @@ -493,7 +492,6 @@ where
context: call_trap_data.context.clone(),
transaction_context,
retbuf: Vec::new(),
fork: self.config.fork,
},
gas_limit,
is_static,
Expand Down Expand Up @@ -528,7 +526,6 @@ where
},
transaction_context,
retbuf: Vec::new(),
fork: self.config.fork,
},
gas_limit,
is_static,
Expand Down
Loading

0 comments on commit c4bad3c

Please sign in to comment.