Skip to content

Commit

Permalink
chore: Move CfgEnv from context-interface to context crate (#1910)
Browse files Browse the repository at this point in the history
* move CfgEnv

* crago fmt

* remove glitch in matrix

* fix docs path
  • Loading branch information
FredCoen authored Dec 14, 2024
1 parent 22435a4 commit 8e37679
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 195 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions bins/revme/src/cmd/statetest/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use indicatif::{ProgressBar, ProgressDrawTarget};
use inspector::{inspector_handler, inspectors::TracerEip3155, InspectorContext, InspectorMainEvm};
use revm::{
bytecode::Bytecode,
context::{block::BlockEnv, tx::TxEnv},
context::{block::BlockEnv, cfg::CfgEnv, tx::TxEnv},
context_interface::{
block::calc_excess_blob_gas,
result::{EVMError, ExecutionResult, HaltReason, InvalidTransaction},
Cfg, CfgEnv,
Cfg,
},
database_interface::EmptyDB,
handler::EthHandler,
Expand Down
24 changes: 16 additions & 8 deletions crates/context/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ bytecode.workspace = true

# misc
derive-where.workspace = true
cfg-if.workspace = true

# Optional
serde = { version = "1.0", default-features = false, features = [
Expand All @@ -44,13 +45,20 @@ serde = { version = "1.0", default-features = false, features = [
database.workspace = true

[features]
# Implementation-specific features
default = ["std"]
std = ["serde?/std"]
serde = [
"dep:serde",
"primitives/serde",
"specification/serde",
"state/serde",
"context-interface/serde",
std = []
dev = [
"memory_limit",
"optional_balance_check",
"optional_block_gas_limit",
"optional_eip3607",
"optional_gas_refund",
"optional_no_base_fee",
]
serde-json = ["serde"]
memory_limit = []
optional_balance_check = []
optional_block_gas_limit = []
optional_eip3607 = []
optional_gas_refund = []
optional_no_base_fee = []
18 changes: 0 additions & 18 deletions crates/context/interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ state.workspace = true
specification.workspace = true

# mics
cfg-if.workspace = true
auto_impl.workspace = true

# Optional
Expand All @@ -47,20 +46,3 @@ std = ["serde?/std"]
serde = ["dep:serde", "primitives/serde", "specification/serde", "state/serde"]
serde-json = ["serde"]

# Enable additional features for development
# They add functions to Cfg trait.
dev = [
"memory_limit",
"optional_balance_check",
"optional_block_gas_limit",
"optional_eip3607",
"optional_gas_refund",
"optional_no_base_fee",

]
memory_limit = []
optional_balance_check = []
optional_block_gas_limit = []
optional_eip3607 = []
optional_gas_refund = []
optional_no_base_fee = []
149 changes: 1 addition & 148 deletions crates/context/interface/src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use auto_impl::auto_impl;
use core::fmt::Debug;
use core::hash::Hash;
use primitives::{TxKind, U256};
use specification::{constants::MAX_CODE_SIZE, hardfork::SpecId};
use specification::hardfork::SpecId;

#[auto_impl(&, &mut, Box, Arc)]
pub trait Cfg {
Expand Down Expand Up @@ -39,153 +39,6 @@ pub enum AnalysisKind {
Analyse,
}

/// EVM configuration.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub struct CfgEnv<SPEC: Into<SpecId> = SpecId> {
/// Chain ID of the EVM, it will be compared to the transaction's Chain ID.
/// Chain ID is introduced EIP-155
pub chain_id: u64,
/// Specification for EVM represent the hardfork.
pub spec: SPEC,
/// If some it will effects EIP-170: Contract code size limit. Useful to increase this because of tests.
/// By default it is 0x6000 (~25kb).
pub limit_contract_code_size: Option<usize>,
/// Skips the nonce validation against the account's nonce.
pub disable_nonce_check: bool,
/// A hard memory limit in bytes beyond which [crate::result::OutOfGasError::Memory] cannot be resized.
///
/// In cases where the gas limit may be extraordinarily high, it is recommended to set this to
/// a sane value to prevent memory allocation panics. Defaults to `2^32 - 1` bytes per
/// EIP-1985.
#[cfg(feature = "memory_limit")]
pub memory_limit: u64,
/// Skip balance checks if true. Adds transaction cost to balance to ensure execution doesn't fail.
#[cfg(feature = "optional_balance_check")]
pub disable_balance_check: bool,
/// There are use cases where it's allowed to provide a gas limit that's higher than a block's gas limit. To that
/// end, you can disable the block gas limit validation.
/// By default, it is set to `false`.
#[cfg(feature = "optional_block_gas_limit")]
pub disable_block_gas_limit: bool,
/// EIP-3607 rejects transactions from senders with deployed code. In development, it can be desirable to simulate
/// calls from contracts, which this setting allows.
/// By default, it is set to `false`.
#[cfg(feature = "optional_eip3607")]
pub disable_eip3607: bool,
/// Disables all gas refunds. This is useful when using chains that have gas refunds disabled e.g. Avalanche.
/// Reasoning behind removing gas refunds can be found in EIP-3298.
/// By default, it is set to `false`.
#[cfg(feature = "optional_gas_refund")]
pub disable_gas_refund: bool,
/// Disables base fee checks for EIP-1559 transactions.
/// This is useful for testing method calls with zero gas price.
/// By default, it is set to `false`.
#[cfg(feature = "optional_no_base_fee")]
pub disable_base_fee: bool,
}

impl CfgEnv {
pub fn with_chain_id(mut self, chain_id: u64) -> Self {
self.chain_id = chain_id;
self
}
}

impl<SPEC: Into<SpecId> + Copy> Cfg for CfgEnv<SPEC> {
type Spec = SPEC;

fn chain_id(&self) -> u64 {
self.chain_id
}

fn spec(&self) -> Self::Spec {
self.spec
}

fn max_code_size(&self) -> usize {
self.limit_contract_code_size.unwrap_or(MAX_CODE_SIZE)
}

fn is_eip3607_disabled(&self) -> bool {
cfg_if::cfg_if! {
if #[cfg(feature = "optional_eip3607")] {
self.disable_eip3607
} else {
false
}
}
}

fn is_balance_check_disabled(&self) -> bool {
cfg_if::cfg_if! {
if #[cfg(feature = "optional_balance_check")] {
self.disable_balance_check
} else {
false
}
}
}

fn is_gas_refund_disabled(&self) -> bool {
cfg_if::cfg_if! {
if #[cfg(feature = "optional_gas_refund")] {
self.disable_gas_refund
} else {
false
}
}
}

fn is_block_gas_limit_disabled(&self) -> bool {
cfg_if::cfg_if! {
if #[cfg(feature = "optional_block_gas_limit")] {
self.disable_block_gas_limit
} else {
false
}
}
}

fn is_nonce_check_disabled(&self) -> bool {
self.disable_nonce_check
}

fn is_base_fee_check_disabled(&self) -> bool {
cfg_if::cfg_if! {
if #[cfg(feature = "optional_no_base_fee")] {
self.disable_base_fee
} else {
false
}
}
}
}

impl Default for CfgEnv {
fn default() -> Self {
Self {
chain_id: 1,
limit_contract_code_size: None,
spec: SpecId::PRAGUE,
disable_nonce_check: false,
#[cfg(feature = "memory_limit")]
memory_limit: (1 << 32) - 1,
#[cfg(feature = "optional_balance_check")]
disable_balance_check: false,
#[cfg(feature = "optional_block_gas_limit")]
disable_block_gas_limit: false,
#[cfg(feature = "optional_eip3607")]
disable_eip3607: false,
#[cfg(feature = "optional_gas_refund")]
disable_gas_refund: false,
#[cfg(feature = "optional_no_base_fee")]
disable_base_fee: false,
}
}
}

/// Transaction destination
pub type TransactTo = TxKind;

Expand Down
2 changes: 1 addition & 1 deletion crates/context/interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub mod result;
pub mod transaction;

pub use block::{Block, BlockGetter};
pub use cfg::{Cfg, CfgEnv, CfgGetter, CreateScheme, TransactTo};
pub use cfg::{Cfg, CfgGetter, CreateScheme, TransactTo};
pub use database_interface::{DBErrorMarker, Database, DatabaseGetter};
pub use errors::ErrorGetter;
pub use journaled_state::{JournalStateGetter, JournalStateGetterDBError, JournaledState};
Expand Down
Loading

0 comments on commit 8e37679

Please sign in to comment.