Skip to content

Commit 73b689d

Browse files
authored
feat: tweeks for v4.0 revm release (#1048)
* chore: docs and Eq added * chore: add auto_impl to GetInspectors * chore: add reset_handler * fix build * Modify env box * Impl GetInspector and Generic Inspector * Add Evn helpers with spec_id * helpers env with spec * Add derefs to CfgEnvWithSpecId * helper function * Remove cfg.optimism field * Support Optimism by default if optimism feature is enabled * Add optimism_default_handler * Add optimism enable field * Optimism in builder * into_env_with_spec_id * add db * Add box * fix doc * fix tests and move it to proper place * Rename EnvWithSpecId to EnvWithHandlerCfg * fix compile
1 parent 93c7ba0 commit 73b689d

File tree

19 files changed

+479
-219
lines changed

19 files changed

+479
-219
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bins/revme/src/cmd/statetest/runner.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ fn check_evm_execution<EXT>(
129129
"errorMsg": error.unwrap_or_default(),
130130
"evmResult": exec_result.as_ref().err().map(|e| e.to_string()).unwrap_or("Ok".to_string()),
131131
"postLogsHash": logs_root,
132-
"fork": evm.handler.spec_id(),
132+
"fork": evm.handler.cfg().spec_id,
133133
"test": test_name,
134134
"d": test.indexes.data,
135135
"g": test.indexes.gas,
@@ -240,7 +240,7 @@ pub fn execute_test_suite(
240240
cache_state.insert_account_with_storage(address, acc_info, info.storage);
241241
}
242242

243-
let mut env = Env::default();
243+
let mut env = Box::<Env>::default();
244244
// for mainnet
245245
env.cfg.chain_id = 1;
246246
// env.cfg.spec_id is set down the road

crates/interpreter/src/call_outcome.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use revm_primitives::Bytes;
1111
///
1212
/// * `result` - The result of the interpreter's execution, including output data and gas usage.
1313
/// * `memory_offset` - The range in memory where the output data is located.
14-
#[derive(Clone, Debug)]
14+
#[derive(Clone, Debug, PartialEq, Eq)]
1515
pub struct CallOutcome {
1616
pub result: InterpreterResult,
1717
pub memory_offset: Range<usize>,

crates/interpreter/src/create_outcome.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use revm_primitives::{Address, Bytes};
55
///
66
/// This struct holds the result of the operation along with an optional address.
77
/// It provides methods to determine the next action based on the result of the operation.
8-
#[derive(Debug, Clone)]
8+
#[derive(Debug, Clone, PartialEq, Eq)]
99
pub struct CreateOutcome {
1010
// The result of the interpreter operation.
1111
pub result: InterpreterResult,

crates/interpreter/src/interpreter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub struct Interpreter {
5454
}
5555

5656
/// The result of an interpreter operation.
57-
#[derive(Debug, Clone)]
57+
#[derive(Debug, Clone, PartialEq, Eq)]
5858
pub struct InterpreterResult {
5959
/// The result of the instruction execution.
6060
pub result: InstructionResult,

crates/primitives/src/env.rs

+4-88
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
pub mod handler_cfg;
2+
3+
pub use handler_cfg::{CfgEnvWithHandlerCfg, EnvWithHandlerCfg, HandlerCfg};
4+
15
use crate::{
26
alloc::vec::Vec, calc_blob_gasprice, Account, Address, Bytes, InvalidHeader,
37
InvalidTransaction, Spec, SpecId, B256, GAS_PER_BLOB, KECCAK_EMPTY, MAX_BLOB_NUMBER_PER_BLOCK,
@@ -79,21 +83,6 @@ impl Env {
7983
/// Return initial spend gas (Gas needed to execute transaction).
8084
#[inline]
8185
pub fn validate_tx<SPEC: Spec>(&self) -> Result<(), InvalidTransaction> {
82-
#[cfg(feature = "optimism")]
83-
if self.cfg.optimism {
84-
// Do not allow for a system transaction to be processed if Regolith is enabled.
85-
if self.tx.optimism.is_system_transaction.unwrap_or(false)
86-
&& SPEC::enabled(SpecId::REGOLITH)
87-
{
88-
return Err(InvalidTransaction::DepositSystemTxPostRegolith);
89-
}
90-
91-
// Do not perform any extra validation for deposit transactions, they are pre-verified on L1.
92-
if self.tx.optimism.source_hash.is_some() {
93-
return Ok(());
94-
}
95-
}
96-
9786
// BASEFEE tx check
9887
if SPEC::enabled(SpecId::LONDON) {
9988
if let Some(priority_fee) = self.tx.gas_priority_fee {
@@ -204,13 +193,6 @@ impl Env {
204193
return Err(InvalidTransaction::RejectCallerWithCode);
205194
}
206195

207-
// On Optimism, deposit transactions do not have verification on the nonce
208-
// nor the balance of the account.
209-
#[cfg(feature = "optimism")]
210-
if self.cfg.optimism && self.tx.optimism.source_hash.is_some() {
211-
return Ok(());
212-
}
213-
214196
// Check that the transaction's nonce is correct
215197
if let Some(tx) = self.tx.nonce {
216198
let state = account.info.nonce;
@@ -310,14 +292,6 @@ pub struct CfgEnv {
310292
/// By default, it is set to `false`.
311293
#[cfg(feature = "optional_beneficiary_reward")]
312294
pub disable_beneficiary_reward: bool,
313-
/// Enables Optimism's execution changes for deposit transactions and fee
314-
/// collection. Hot toggling the optimism field gives applications built
315-
/// on revm the ability to switch optimism execution on and off at runtime,
316-
/// allowing for features like multichain fork testing. Setting this field
317-
/// to false will disable all optimism execution changes regardless of
318-
/// compilation with the optimism feature flag.
319-
#[cfg(feature = "optimism")]
320-
pub optimism: bool,
321295
}
322296

323297
impl CfgEnv {
@@ -380,16 +354,6 @@ impl CfgEnv {
380354
pub fn is_beneficiary_reward_disabled(&self) -> bool {
381355
false
382356
}
383-
384-
#[cfg(feature = "optimism")]
385-
pub fn is_optimism(&self) -> bool {
386-
self.optimism
387-
}
388-
389-
#[cfg(not(feature = "optimism"))]
390-
pub fn is_optimism(&self) -> bool {
391-
false
392-
}
393357
}
394358

395359
impl Default for CfgEnv {
@@ -414,8 +378,6 @@ impl Default for CfgEnv {
414378
disable_base_fee: false,
415379
#[cfg(feature = "optional_beneficiary_reward")]
416380
disable_beneficiary_reward: false,
417-
#[cfg(feature = "optimism")]
418-
optimism: false,
419381
}
420382
}
421383
}
@@ -737,52 +699,6 @@ pub enum AnalysisKind {
737699
Analyse,
738700
}
739701

740-
#[cfg(test)]
741-
#[cfg(feature = "optimism")]
742-
mod op_tests {
743-
use super::*;
744-
745-
#[test]
746-
fn test_validate_sys_tx() {
747-
// Set the optimism flag to true and mark
748-
// the tx as a system transaction.
749-
let mut env = Env::default();
750-
env.cfg.optimism = true;
751-
env.tx.optimism.is_system_transaction = Some(true);
752-
assert_eq!(
753-
env.validate_tx::<crate::RegolithSpec>(),
754-
Err(InvalidTransaction::DepositSystemTxPostRegolith)
755-
);
756-
757-
// Pre-regolith system transactions should be allowed.
758-
assert!(env.validate_tx::<crate::BedrockSpec>().is_ok());
759-
}
760-
761-
#[test]
762-
fn test_validate_deposit_tx() {
763-
// Set the optimism flag and source hash.
764-
let mut env = Env::default();
765-
env.cfg.optimism = true;
766-
env.tx.optimism.source_hash = Some(B256::ZERO);
767-
assert!(env.validate_tx::<crate::RegolithSpec>().is_ok());
768-
}
769-
770-
#[test]
771-
fn test_validate_tx_against_state_deposit_tx() {
772-
// Set the optimism flag and source hash.
773-
774-
use crate::LatestSpec;
775-
let mut env = Env::default();
776-
env.cfg.optimism = true;
777-
env.tx.optimism.source_hash = Some(B256::ZERO);
778-
779-
// Nonce and balance checks should be skipped for deposit transactions.
780-
assert!(env
781-
.validate_tx_against_state::<LatestSpec>(&mut Account::default())
782-
.is_ok());
783-
}
784-
}
785-
786702
#[cfg(test)]
787703
mod tests {
788704
use super::*;
+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
use super::{BlockEnv, CfgEnv, Env, SpecId, TxEnv};
2+
use alloc::boxed::Box;
3+
use core::ops::{Deref, DerefMut};
4+
5+
/// Handler configuration fields. It is used to configure the handler.
6+
/// It contains specification id and the Optimism related field if
7+
/// optimism feature is enabled.
8+
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
9+
pub struct HandlerCfg {
10+
/// Specification identification.
11+
pub spec_id: SpecId,
12+
/// Optimism related field, it will append the Optimism handle register to the EVM.
13+
#[cfg(feature = "optimism")]
14+
pub is_optimism: bool,
15+
}
16+
17+
impl HandlerCfg {
18+
/// Creates new `HandlerCfg` instance.
19+
pub fn new(spec_id: SpecId) -> Self {
20+
Self {
21+
spec_id,
22+
#[cfg(feature = "optimism")]
23+
is_optimism: false,
24+
}
25+
}
26+
27+
/// Creates new `HandlerCfg` instance with the optimism feature.
28+
#[cfg(feature = "optimism")]
29+
pub fn new_with_optimism(spec_id: SpecId, is_optimism: bool) -> Self {
30+
Self {
31+
spec_id,
32+
is_optimism,
33+
}
34+
}
35+
36+
/// Returns true if the optimism feature is enabled and flag is set to true.
37+
pub fn is_optimism(&self) -> bool {
38+
cfg_if::cfg_if! {
39+
if #[cfg(feature = "optimism")] {
40+
self.is_optimism
41+
} else {
42+
false
43+
}
44+
}
45+
}
46+
}
47+
48+
/// Configuration environment with the chain spec id.
49+
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
50+
pub struct CfgEnvWithHandlerCfg {
51+
/// Configuration environment.
52+
pub cfg_env: CfgEnv,
53+
/// Handler configuration fields.
54+
pub handler_cfg: HandlerCfg,
55+
}
56+
57+
impl CfgEnvWithHandlerCfg {
58+
/// Returns new `CfgEnvWithHandlerCfg` instance.
59+
pub fn new(cfg_env: CfgEnv, spec_id: SpecId) -> Self {
60+
Self {
61+
cfg_env,
62+
handler_cfg: HandlerCfg {
63+
spec_id,
64+
#[cfg(feature = "optimism")]
65+
is_optimism: false,
66+
},
67+
}
68+
}
69+
70+
/// Enables the optimism feature.
71+
#[cfg(feature = "optimism")]
72+
pub fn enable_optimism(&mut self) {
73+
self.handler_cfg.is_optimism = true;
74+
}
75+
}
76+
77+
impl DerefMut for CfgEnvWithHandlerCfg {
78+
fn deref_mut(&mut self) -> &mut Self::Target {
79+
&mut self.cfg_env
80+
}
81+
}
82+
83+
impl Deref for CfgEnvWithHandlerCfg {
84+
type Target = CfgEnv;
85+
86+
fn deref(&self) -> &Self::Target {
87+
&self.cfg_env
88+
}
89+
}
90+
91+
/// Evm environment with the chain spec id.
92+
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
93+
pub struct EnvWithHandlerCfg {
94+
/// Evm enironment.
95+
pub env: Box<Env>,
96+
/// Handler configuration fields.
97+
pub handler_cfg: HandlerCfg,
98+
}
99+
100+
impl EnvWithHandlerCfg {
101+
/// Returns new `EnvWithHandlerCfg` instance.
102+
pub fn new(env: Box<Env>, spec_id: SpecId) -> Self {
103+
Self {
104+
env,
105+
handler_cfg: HandlerCfg {
106+
spec_id,
107+
#[cfg(feature = "optimism")]
108+
is_optimism: false,
109+
},
110+
}
111+
}
112+
113+
/// Takes `CfgEnvWithHandlerCfg` and returns new `EnvWithHandlerCfg` instance.
114+
pub fn new_with_cfg_env(cfg: CfgEnvWithHandlerCfg, block: BlockEnv, tx: TxEnv) -> Self {
115+
cfg_if::cfg_if! {
116+
if #[cfg(feature = "optimism")] {
117+
let mut new = Self::new(
118+
Box::new(Env {
119+
cfg: cfg.cfg_env,
120+
block,
121+
tx,
122+
}),
123+
cfg.handler_cfg.spec_id,
124+
);
125+
if cfg.handler_cfg.is_optimism {
126+
new.enable_optimism()
127+
}
128+
new
129+
} else {
130+
Self::new(
131+
Box::new(Env {
132+
cfg: cfg.cfg_env,
133+
block,
134+
tx,
135+
}),
136+
cfg.handler_cfg.spec_id,
137+
)
138+
}
139+
}
140+
}
141+
142+
/// Enables the optimism handle register.
143+
#[cfg(feature = "optimism")]
144+
pub fn enable_optimism(&mut self) {
145+
self.handler_cfg.is_optimism = true;
146+
}
147+
}
148+
149+
impl DerefMut for EnvWithHandlerCfg {
150+
fn deref_mut(&mut self) -> &mut Self::Target {
151+
&mut self.env
152+
}
153+
}
154+
155+
impl Deref for EnvWithHandlerCfg {
156+
type Target = Env;
157+
158+
fn deref(&self) -> &Self::Target {
159+
&self.env
160+
}
161+
}

0 commit comments

Comments
 (0)