Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 26 additions & 39 deletions Cargo.lock

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

8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exclude = []
# https://doc.rust-lang.org/edition-guide/rust-2021/default-cargo-resolver.html
resolver = "2"
[workspace.package]
version = "1.4.0"
version = "1.5.0"
edition = "2021"
rust-version = "1.86"
license = "MIT OR Apache-2.0"
Expand Down Expand Up @@ -177,6 +177,12 @@ tracing-subscriber = { version = "0.3", default-features = false }
triehash = { version = "0.8", default-features = false }
walkdir = { version = "2.5", default-features = false }

[patch.crates-io]
op-revm = { git = "https://github.com/megaeth-labs/revm.git", rev = "975fba4c85ea37e4e3b58d7cef60be8ace5cc22f" }
revm = { git = "https://github.com/megaeth-labs/revm.git", rev = "975fba4c85ea37e4e3b58d7cef60be8ace5cc22f" }
revm-database-interface = { git = "https://github.com/megaeth-labs/revm.git", rev = "975fba4c85ea37e4e3b58d7cef60be8ace5cc22f" }
revm-database = { git = "https://github.com/megaeth-labs/revm.git", rev = "975fba4c85ea37e4e3b58d7cef60be8ace5cc22f" }

# Speed up compilation time for dev builds by reducing emitted debug info.
# NOTE: Debuggers may provide less useful information with this setting.
# Uncomment this section if you're using a debugger.
Expand Down
23 changes: 12 additions & 11 deletions bin/mega-evme/src/common/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use mega_evm::{
context::{block::BlockEnv, cfg::CfgEnv},
primitives::eip4844,
},
MegaContext, MegaSpecId, TestExternalEnvs,
BucketId, MegaContext, MegaSpecId, TestExternalEnvs,
};
use tracing::{debug, trace};

Expand Down Expand Up @@ -127,18 +127,19 @@ pub struct ExtEnvArgs {
}

impl ExtEnvArgs {
/// Creates [`TestExternalEnvs`].
pub fn create_external_envs(&self) -> Result<TestExternalEnvs> {
let mut external_envs = TestExternalEnvs::new();
/// Creates [`TestExternalEnvs`] and returns bucket capacity configuration.
pub fn create_external_envs(&self) -> Result<(TestExternalEnvs, Vec<(BucketId, u64)>)> {
let external_envs = TestExternalEnvs::new();

// Parse and configure bucket capacities
// Parse bucket capacities
let mut bucket_capacities = Vec::new();
for bucket_capacity_str in &self.bucket_capacity {
let (bucket_id, capacity) = parse_bucket_capacity(bucket_capacity_str)?;
external_envs = external_envs.with_bucket_capacity(bucket_id, capacity);
bucket_capacities.push((bucket_id, capacity));
}
debug!(external_envs = ?external_envs, "Evm TestExternalEnvs created");
debug!(external_envs = ?external_envs, bucket_capacities = ?bucket_capacities, "Evm TestExternalEnvs created");

Ok(external_envs)
Ok((external_envs, bucket_capacities))
}
}

Expand Down Expand Up @@ -174,8 +175,8 @@ impl EnvArgs {
self.block.create_block_env()
}

/// Creates [`TestExternalEnvs`].
pub fn create_external_envs(&self) -> Result<TestExternalEnvs> {
/// Creates [`TestExternalEnvs`] and returns bucket capacity configuration.
pub fn create_external_envs(&self) -> Result<(TestExternalEnvs, Vec<(BucketId, u64)>)> {
self.ext.create_external_envs()
}

Expand All @@ -186,7 +187,7 @@ impl EnvArgs {
) -> Result<MegaContext<DB, TestExternalEnvs>> {
let cfg = self.create_cfg_env()?;
let block = self.create_block_env()?;
let external_envs = self.create_external_envs()?;
let (external_envs, _bucket_capacities) = self.create_external_envs()?;

Ok(MegaContext::new(db, cfg.spec)
.with_cfg(cfg)
Expand Down
2 changes: 1 addition & 1 deletion bin/mega-evme/src/replay/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ impl Cmd {
let transaction_index = preceding_transactions.len() as u64;
debug!(transaction_index, "Setting up block executor");

let external_env_factory = self.ext_args.create_external_envs()?;
let (external_env_factory, _bucket_capacities) = self.ext_args.create_external_envs()?;
let evm_factory = MegaEvmFactory::new().with_external_env_factory(external_env_factory);
let block_executor_factory = MegaBlockExecutorFactory::new(
&hardforks,
Expand Down
12 changes: 12 additions & 0 deletions crates/mega-evm/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
//!
//! It groups the constants for different EVM specs as sub-modules.

// SALT bucket constants

/// Number of bits to represent the minimum bucket size (8 bits = 256 slots).
pub const MIN_BUCKET_SIZE_BITS: usize = 8;

/// Minimum capacity of a SALT bucket in number of slots (256).
///
/// Buckets hold accounts or storage slots and can grow beyond this size. The gas cost
/// multiplier is calculated as `capacity / MIN_BUCKET_SIZE`, so a bucket at minimum
/// capacity has a 1x multiplier.
pub const MIN_BUCKET_SIZE: usize = 1 << MIN_BUCKET_SIZE_BITS;

/// Constants for the `EQUIVALENCE` spec.
pub mod equivalence {
use revm::interpreter::gas;
Expand Down
21 changes: 6 additions & 15 deletions crates/mega-evm/src/evm/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub struct MegaContext<DB: Database, ExtEnvs: ExternalEnvTypes> {
pub additional_limit: Rc<RefCell<AdditionalLimit>>,

/// Calculator for dynamic gas costs during transaction execution.
pub dynamic_storage_gas_cost: Rc<RefCell<DynamicGasCost<ExtEnvs::SaltEnv>>>,
pub dynamic_storage_gas_cost: Rc<RefCell<DynamicGasCost>>,

/// The oracle environment.
pub oracle_env: Rc<RefCell<ExtEnvs::OracleEnv>>,
Expand Down Expand Up @@ -109,7 +109,6 @@ impl<DB: Database> MegaContext<DB, EmptyExternalEnv> {
additional_limit: Rc::new(RefCell::new(AdditionalLimit::new(spec, tx_limits))),
dynamic_storage_gas_cost: Rc::new(RefCell::new(DynamicGasCost::new(
spec,
EmptyExternalEnv,
inner.block.number.to::<u64>().saturating_sub(1),
))),
oracle_env: Rc::new(RefCell::new(EmptyExternalEnv)),
Expand Down Expand Up @@ -169,7 +168,6 @@ impl<DB: Database, ExtEnvTypes: ExternalEnvTypes> MegaContext<DB, ExtEnvTypes> {
additional_limit: Rc::new(RefCell::new(AdditionalLimit::new(spec, tx_limits))),
dynamic_storage_gas_cost: Rc::new(RefCell::new(DynamicGasCost::new(
spec,
external_envs.salt_env,
inner.block.number.to::<u64>() - 1,
))),
oracle_env: Rc::new(RefCell::new(external_envs.oracle_env)),
Expand Down Expand Up @@ -274,10 +272,9 @@ impl<DB: Database, ExtEnvTypes: ExternalEnvTypes> MegaContext<DB, ExtEnvTypes> {

/// Sets the external environments for the EVM.
///
/// This method updates the external environments used for gas cost calculations,
/// including the salt environment and oracle environment. When setting new
/// external environments, the dynamic gas cost calculator and oracle environment
/// are reinitialized with the new configurations.
/// This method updates the external environments used for oracle environment.
/// When setting new external environments, the oracle environment is reinitialized
/// with the new configuration.
///
/// # Arguments
///
Expand All @@ -290,18 +287,12 @@ impl<DB: Database, ExtEnvTypes: ExternalEnvTypes> MegaContext<DB, ExtEnvTypes> {
self,
external_envs: ExternalEnvs<NewExtEnvTypes>,
) -> MegaContext<DB, NewExtEnvTypes> {
let parent_block_number = self.inner.block.number.to::<u64>().saturating_sub(1);
let spec = self.spec;
MegaContext {
inner: self.inner,
spec,
spec: self.spec,
disable_beneficiary: self.disable_beneficiary,
additional_limit: self.additional_limit,
dynamic_storage_gas_cost: Rc::new(RefCell::new(DynamicGasCost::new(
spec,
external_envs.salt_env,
parent_block_number,
))),
dynamic_storage_gas_cost: self.dynamic_storage_gas_cost,
oracle_env: Rc::new(RefCell::new(external_envs.oracle_env)),
volatile_data_tracker: self.volatile_data_tracker,
disable_sandbox: self.disable_sandbox,
Expand Down
3 changes: 1 addition & 2 deletions crates/mega-evm/src/evm/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,9 @@ impl<ExtEnvFactory: ExternalEnvFactory + Clone> alloy_evm::EvmFactory
evm_env: EvmEnv<Self::Spec>,
) -> Self::Evm<DB, revm::inspector::NoOpInspector> {
let spec_id = *evm_env.spec_id();
let block_number = evm_env.block_env.number.to();
let runtime_limits = EvmTxRuntimeLimits::from_spec(spec_id);
let ctx = MegaContext::new(db, spec_id)
.with_external_envs(self.external_env_factory.external_envs(block_number))
.with_external_envs(self.external_env_factory.external_envs())
.with_tx(MegaTransaction::default())
.with_block(evm_env.block_env)
.with_cfg(evm_env.cfg_env)
Expand Down
Loading