Skip to content

Commit c4bad3c

Browse files
committed
resolve comments
1 parent 924e068 commit c4bad3c

File tree

12 files changed

+73
-108
lines changed

12 files changed

+73
-108
lines changed

interpreter/src/eval/mod.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use crate::{
1515
machine::Machine,
1616
opcode::Opcode,
1717
runtime::{GasState, RuntimeBackend, RuntimeEnvironment, RuntimeState},
18-
fork::Fork,
1918
};
2019

2120
pub fn eval_pass<S, H, Tr>(
@@ -725,11 +724,7 @@ pub fn eval_suicide<S: AsRef<RuntimeState>, H: RuntimeEnvironment + RuntimeBacke
725724
_opcode: Opcode,
726725
_position: usize,
727726
) -> Control<Tr> {
728-
if machine.state.as_ref().fork >= Fork::CANCUN {
729-
self::system::suicide_eip_6780(machine, handle)
730-
} else {
731-
self::system::suicide(machine, handle)
732-
}
727+
self::system::suicide(machine, handle)
733728
}
734729

735730
pub fn eval_chainid<S: AsRef<RuntimeState>, H: RuntimeEnvironment + RuntimeBackend, Tr>(

interpreter/src/eval/system.rs

+1-37
Original file line numberDiff line numberDiff line change
@@ -362,43 +362,7 @@ pub fn suicide<S: AsRef<RuntimeState>, H: RuntimeEnvironment + RuntimeBackend, T
362362
value: balance,
363363
})?;
364364

365-
handler.mark_delete(address);
366-
handler.reset_balance(address);
367-
368-
Ok(((), ()))
369-
}) {
370-
Ok(()) => Control::Exit(ExitSucceed::Suicided.into()),
371-
Err(e) => Control::Exit(Err(e)),
372-
}
373-
}
374-
375-
pub fn suicide_eip_6780<S: AsRef<RuntimeState>, H: RuntimeEnvironment + RuntimeBackend, Tr>(
376-
machine: &mut Machine<S>,
377-
handler: &mut H,
378-
) -> Control<Tr> {
379-
let address = machine.state.as_ref().context.address;
380-
381-
match machine.stack.perform_pop1_push0(|target| {
382-
let balance = handler.balance(address);
383-
let target = (*target).into();
384-
385-
// Cancun EIP-6780 only allow contract deletion within the same transaction that created it
386-
if handler.created(address) {
387-
handler.transfer(Transfer {
388-
source: address,
389-
target,
390-
value: balance,
391-
})?;
392-
393-
handler.mark_delete(address);
394-
handler.reset_balance(address);
395-
} else if address != target {
396-
handler.transfer(Transfer {
397-
source: address,
398-
target,
399-
value: balance,
400-
})?;
401-
}
365+
handler.mark_delete_reset(address);
402366
Ok(((), ()))
403367
}) {
404368
Ok(()) => Control::Exit(ExitSucceed::Suicided.into()),

interpreter/src/fork.rs

-11
This file was deleted.

interpreter/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,5 @@ pub mod machine;
1414
pub mod opcode;
1515
pub mod runtime;
1616
pub mod utils;
17-
pub mod fork;
1817

1918
pub use self::interpreter::{EtableInterpreter, Interpreter, RunInterpreter, StepInterpreter};

interpreter/src/runtime.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use alloc::{rc::Rc, vec::Vec};
33
use primitive_types::{H160, H256, U256};
44
use sha3::{Digest, Keccak256};
55

6-
use crate::{error::ExitError, fork::Fork};
6+
use crate::error::ExitError;
77

88
/// Gas state.
99
pub trait GasState {
@@ -19,8 +19,6 @@ pub struct RuntimeState {
1919
pub transaction_context: Rc<TransactionContext>,
2020
/// Return data buffer.
2121
pub retbuf: Vec<u8>,
22-
/// EVM Fork.
23-
pub fork: Fork,
2422
}
2523

2624
impl AsRef<Self> for RuntimeState {
@@ -155,9 +153,9 @@ pub trait RuntimeBackend: RuntimeBaseBackend {
155153
) -> Result<(), ExitError>;
156154
/// Create a log owned by address with given topics and data.
157155
fn log(&mut self, log: Log) -> Result<(), ExitError>;
158-
/// Mark an address to be deleted.
159-
fn mark_delete(&mut self, address: H160);
160-
// Mark an address that is being created in the transaction.
156+
/// Mark an address to be deleted and its balance to be reset.
157+
fn mark_delete_reset(&mut self, address: H160);
158+
// Mark an address as created in the current transaction.
161159
fn mark_create(&mut self, address: H160);
162160
/// Fully delete storages of an account.
163161
fn reset_storage(&mut self, address: H160);

interpreter/tests/usability.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use evm_interpreter::fork::Fork;
2-
use std::rc::Rc;
31
use evm_interpreter::{
42
error::{CallCreateTrap, Capture, ExitError, ExitSucceed},
53
etable::{Control, Etable},
@@ -12,6 +10,7 @@ use evm_interpreter::{
1210
EtableInterpreter, RunInterpreter,
1311
};
1412
use primitive_types::{H160, H256, U256};
13+
use std::rc::Rc;
1514

1615
const CODE1: &str = "60e060020a6000350480632839e92814601e57806361047ff414603457005b602a6004356024356047565b8060005260206000f35b603d6004356099565b8060005260206000f35b600082600014605457605e565b8160010190506093565b81600014606957607b565b60756001840360016047565b90506093565b609060018403608c85600186036047565b6047565b90505b92915050565b6000816000148060a95750816001145b60b05760b7565b81905060cf565b60c1600283036099565b60cb600184036099565b0190505b91905056";
1716
const DATA1: &str = "2839e92800000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000001";
@@ -162,7 +161,7 @@ impl RuntimeBackend for UnimplementedHandler {
162161
fn log(&mut self, _log: Log) -> Result<(), ExitError> {
163162
unimplemented!()
164163
}
165-
fn mark_delete(&mut self, _address: H160) {
164+
fn mark_delete_reset(&mut self, _address: H160) {
166165
unimplemented!()
167166
}
168167

@@ -220,7 +219,6 @@ fn etable_runtime() {
220219
}
221220
.into(),
222221
retbuf: Vec::new(),
223-
fork: Fork::FRONTIER,
224222
},
225223
);
226224
let mut vm = EtableInterpreter::new(machine, &RUNTIME_ETABLE);

jsontests/src/run.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ pub fn run_test(
184184
state,
185185
};
186186

187-
let mut run_backend = OverlayedBackend::new(&base_backend, initial_accessed.clone());
188-
let mut step_backend = OverlayedBackend::new(&base_backend, initial_accessed.clone());
187+
let mut run_backend = OverlayedBackend::new(&base_backend, initial_accessed.clone(), &config);
188+
let mut step_backend = OverlayedBackend::new(&base_backend, initial_accessed.clone(), &config);
189189

190190
// Run
191191
let run_result = evm::transact(args.clone(), Some(4), &mut run_backend, &invoker);

src/backend/overlayed.rs

+24-10
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use evm_interpreter::{
1111
};
1212
use primitive_types::{H160, H256, U256};
1313

14-
use crate::{backend::TransactionalBackend, MergeStrategy};
14+
use crate::{backend::TransactionalBackend, standard::Config, MergeStrategy};
1515

1616
#[derive(Clone, Debug)]
1717
pub struct OverlayedChangeSet {
@@ -25,18 +25,24 @@ pub struct OverlayedChangeSet {
2525
pub deletes: BTreeSet<H160>,
2626
}
2727

28-
pub struct OverlayedBackend<B> {
28+
pub struct OverlayedBackend<'config, B> {
2929
backend: B,
3030
substate: Box<Substate>,
3131
accessed: BTreeSet<(H160, Option<H256>)>,
32+
config: &'config Config,
3233
}
3334

34-
impl<B> OverlayedBackend<B> {
35-
pub fn new(backend: B, accessed: BTreeSet<(H160, Option<H256>)>) -> Self {
35+
impl<'config, B> OverlayedBackend<'config, B> {
36+
pub fn new(
37+
backend: B,
38+
accessed: BTreeSet<(H160, Option<H256>)>,
39+
config: &'config Config,
40+
) -> Self {
3641
Self {
3742
backend,
3843
substate: Box::new(Substate::new()),
3944
accessed,
45+
config,
4046
}
4147
}
4248

@@ -57,7 +63,7 @@ impl<B> OverlayedBackend<B> {
5763
}
5864
}
5965

60-
impl<B: RuntimeEnvironment> RuntimeEnvironment for OverlayedBackend<B> {
66+
impl<B: RuntimeEnvironment> RuntimeEnvironment for OverlayedBackend<'_, B> {
6167
fn block_hash(&self, number: U256) -> H256 {
6268
self.backend.block_hash(number)
6369
}
@@ -95,7 +101,7 @@ impl<B: RuntimeEnvironment> RuntimeEnvironment for OverlayedBackend<B> {
95101
}
96102
}
97103

98-
impl<B: RuntimeBaseBackend> RuntimeBaseBackend for OverlayedBackend<B> {
104+
impl<B: RuntimeBaseBackend> RuntimeBaseBackend for OverlayedBackend<'_, B> {
99105
fn balance(&self, address: H160) -> U256 {
100106
if let Some(balance) = self.substate.known_balance(address) {
101107
balance
@@ -145,7 +151,7 @@ impl<B: RuntimeBaseBackend> RuntimeBaseBackend for OverlayedBackend<B> {
145151
}
146152
}
147153

148-
impl<B: RuntimeBaseBackend> RuntimeBackend for OverlayedBackend<B> {
154+
impl<B: RuntimeBaseBackend> RuntimeBackend for OverlayedBackend<'_, B> {
149155
fn original_storage(&self, address: H160, index: H256) -> H256 {
150156
self.backend.storage(address, index)
151157
}
@@ -188,8 +194,16 @@ impl<B: RuntimeBaseBackend> RuntimeBackend for OverlayedBackend<B> {
188194
Ok(())
189195
}
190196

191-
fn mark_delete(&mut self, address: H160) {
192-
self.substate.deletes.insert(address);
197+
fn mark_delete_reset(&mut self, address: H160) {
198+
if self.config.suicide_only_in_same_tx {
199+
if self.created(address) {
200+
self.substate.deletes.insert(address);
201+
self.substate.storage_resets.insert(address);
202+
}
203+
} else {
204+
self.substate.deletes.insert(address);
205+
self.substate.storage_resets.insert(address);
206+
}
193207
}
194208

195209
fn mark_create(&mut self, address: H160) {
@@ -241,7 +255,7 @@ impl<B: RuntimeBaseBackend> RuntimeBackend for OverlayedBackend<B> {
241255
}
242256
}
243257

244-
impl<B: RuntimeBaseBackend> TransactionalBackend for OverlayedBackend<B> {
258+
impl<'config, B: RuntimeBaseBackend> TransactionalBackend for OverlayedBackend<'config, B> {
245259
fn push_substate(&mut self) {
246260
let mut parent = Box::new(Substate::new());
247261
mem::swap(&mut parent, &mut self.substate);

src/standard/config.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use evm_interpreter::fork::{Fork, Fork::*};
2-
31
/// Runtime configuration.
42
#[derive(Clone, Debug)]
53
pub struct Config {
@@ -105,7 +103,8 @@ pub struct Config {
105103
pub eip_5656_enabled: bool,
106104
/// 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)
107105
pub eip_1559_enabled: bool,
108-
pub fork: Fork,
106+
/// 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)
107+
pub suicide_only_in_same_tx: bool,
109108
}
110109

111110
impl Config {
@@ -162,7 +161,7 @@ impl Config {
162161
eip_1153_enabled: false,
163162
eip_5656_enabled: false,
164163
eip_1559_enabled: false,
165-
fork: FRONTIER,
164+
suicide_only_in_same_tx: false,
166165
}
167166
}
168167

@@ -219,7 +218,7 @@ impl Config {
219218
eip_1153_enabled: false,
220219
eip_5656_enabled: false,
221220
eip_1559_enabled: false,
222-
fork: ISTANBUL,
221+
suicide_only_in_same_tx: false,
223222
}
224223
}
225224

@@ -262,7 +261,7 @@ impl Config {
262261
eip_1153_enabled,
263262
eip_5656_enabled,
264263
eip_1559_enabled,
265-
fork,
264+
suicide_only_in_same_tx,
266265
} = inputs;
267266

268267
// See https://eips.ethereum.org/EIPS/eip-2929
@@ -328,7 +327,7 @@ impl Config {
328327
eip_1153_enabled,
329328
eip_5656_enabled,
330329
eip_1559_enabled,
331-
fork,
330+
suicide_only_in_same_tx,
332331
}
333332
}
334333
}
@@ -351,7 +350,7 @@ struct DerivedConfigInputs {
351350
eip_1153_enabled: bool,
352351
eip_5656_enabled: bool,
353352
eip_1559_enabled: bool,
354-
fork: Fork,
353+
suicide_only_in_same_tx: bool,
355354
}
356355

357356
impl DerivedConfigInputs {
@@ -369,7 +368,7 @@ impl DerivedConfigInputs {
369368
eip_1153_enabled: false,
370369
eip_5656_enabled: false,
371370
eip_1559_enabled: false,
372-
fork: BERLIN,
371+
suicide_only_in_same_tx: false,
373372
}
374373
}
375374

@@ -387,7 +386,7 @@ impl DerivedConfigInputs {
387386
eip_1153_enabled: false,
388387
eip_5656_enabled: false,
389388
eip_1559_enabled: true,
390-
fork: LONDON,
389+
suicide_only_in_same_tx: false,
391390
}
392391
}
393392

@@ -405,7 +404,7 @@ impl DerivedConfigInputs {
405404
eip_1153_enabled: false,
406405
eip_5656_enabled: false,
407406
eip_1559_enabled: true,
408-
fork: MERGE,
407+
suicide_only_in_same_tx: false,
409408
}
410409
}
411410

@@ -424,7 +423,7 @@ impl DerivedConfigInputs {
424423
eip_1153_enabled: false,
425424
eip_5656_enabled: false,
426425
eip_1559_enabled: true,
427-
fork: SHANGHAI,
426+
suicide_only_in_same_tx: false,
428427
}
429428
}
430429
const fn cancun() -> Self {
@@ -442,7 +441,7 @@ impl DerivedConfigInputs {
442441
eip_1153_enabled: false,
443442
eip_5656_enabled: false,
444443
eip_1559_enabled: true,
445-
fork: CANCUN,
444+
suicide_only_in_same_tx: true,
446445
}
447446
}
448447
}

src/standard/invoker/mod.rs

-3
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,6 @@ where
270270
context,
271271
transaction_context: Rc::new(transaction_context),
272272
retbuf: Vec::new(),
273-
fork: self.config.fork,
274273
};
275274

276275
let work = || -> Result<(TransactInvoke, _), ExitError> {
@@ -493,7 +492,6 @@ where
493492
context: call_trap_data.context.clone(),
494493
transaction_context,
495494
retbuf: Vec::new(),
496-
fork: self.config.fork,
497495
},
498496
gas_limit,
499497
is_static,
@@ -528,7 +526,6 @@ where
528526
},
529527
transaction_context,
530528
retbuf: Vec::new(),
531-
fork: self.config.fork,
532529
},
533530
gas_limit,
534531
is_static,

0 commit comments

Comments
 (0)