From a1f26aee2f8abd8accbb930a3af5457a497a5514 Mon Sep 17 00:00:00 2001 From: chengwenxi <22697326+chengwenxi@users.noreply.github.com> Date: Fri, 21 Feb 2025 09:59:04 +0800 Subject: [PATCH 1/4] apply morph203 changes --- crates/precompile/src/lib.rs | 22 ++++++++++++++++++++++ crates/primitives/src/specification.rs | 13 +++++++++++++ 2 files changed, 35 insertions(+) diff --git a/crates/precompile/src/lib.rs b/crates/precompile/src/lib.rs index 5fb2eb9f55..3e1bb9c0f9 100644 --- a/crates/precompile/src/lib.rs +++ b/crates/precompile/src/lib.rs @@ -64,6 +64,8 @@ impl Precompiles { PrecompileSpecId::PRE_BERNOULLI => Self::pre_bernoulli(), #[cfg(feature = "morph")] PrecompileSpecId::BERNOULLI => Self::bernoulli(), + #[cfg(feature = "morph")] + PrecompileSpecId::MORPH203 => Self::morph203(), PrecompileSpecId::CANCUN => Self::cancun(), PrecompileSpecId::PRAGUE => Self::prague(), PrecompileSpecId::LATEST => Self::latest(), @@ -217,6 +219,22 @@ impl Precompiles { }) } + /// Returns precompiles for Morph + #[cfg(feature = "morph")] + pub fn morph203() -> &'static Self { + static INSTANCE: OnceBox = OnceBox::new(); + INSTANCE.get_or_init(|| { + let mut precompiles = Self::bernoulli().clone(); + precompiles.extend([ + hash::RIPEMD160, // 0x03 + modexp::BERLIN, // 0x05 + bn128::pair::ISTANBUL, // 0x08 + blake2::FUN, // 0x09 + ]); + Box::new(precompiles) + }) + } + /// Returns the precompiles for the latest spec. pub fn latest() -> &'static Self { Self::prague() @@ -318,6 +336,8 @@ pub enum PrecompileSpecId { PRE_BERNOULLI, #[cfg(feature = "morph")] BERNOULLI, + #[cfg(feature = "morph")] + MORPH203, CANCUN, PRAGUE, LATEST, @@ -345,6 +365,8 @@ impl PrecompileSpecId { PRE_BERNOULLI => Self::PRE_BERNOULLI, #[cfg(feature = "morph")] BERNOULLI | CURIE => Self::BERNOULLI, + #[cfg(feature = "morph")] + MORPH203 => Self::MORPH203, } } } diff --git a/crates/primitives/src/specification.rs b/crates/primitives/src/specification.rs index 8c3389dbe7..724c6ff863 100644 --- a/crates/primitives/src/specification.rs +++ b/crates/primitives/src/specification.rs @@ -113,6 +113,7 @@ pub enum SpecId { CANCUN = 20, PRAGUE = 21, PRAGUE_EOF = 22, + MORPH203 = 23, // revert Precompiles: RIPEMD-160, point evaluation, modexp, ecPairing #[default] LATEST = u8::MAX, } @@ -174,6 +175,8 @@ impl From<&str> for SpecId { "Bernoulli" => SpecId::BERNOULLI, #[cfg(feature = "morph")] "Curie" => SpecId::CURIE, + #[cfg(feature = "morph")] + "Morph203" => SpecId::MORPH203, _ => Self::LATEST, } } @@ -220,6 +223,8 @@ impl From for &'static str { SpecId::BERNOULLI => "Bernoulli", #[cfg(feature = "morph")] SpecId::CURIE => "Curie", + #[cfg(feature = "morph")] + SpecId::MORPH203 => "Morph203", SpecId::LATEST => "Latest", } } @@ -290,6 +295,8 @@ spec!(PRE_BERNOULLI, PreBernoulliSpec); #[cfg(feature = "morph")] spec!(BERNOULLI, BernoulliSpec); #[cfg(feature = "morph")] +spec!(MORPH203, Morph203Spec); +#[cfg(feature = "morph")] spec!(CURIE, CurieSpec); #[cfg(not(any(feature = "optimism", feature = "morph")))] @@ -534,6 +541,10 @@ macro_rules! spec_to_generic { use $crate::BernoulliSpec as SPEC; $e } + $crate::SpecId::MORPH203 => { + use $crate::Morph203Spec as SPEC; + $e + } $crate::SpecId::CURIE => { use $crate::CurieSpec as SPEC; $e @@ -578,6 +589,8 @@ mod tests { #[cfg(feature = "morph")] spec_to_generic!(BERNOULLI, assert_eq!(SPEC::SPEC_ID, BERNOULLI)); #[cfg(feature = "morph")] + spec_to_generic!(MORPH203, assert_eq!(SPEC::SPEC_ID, MORPH203)); + #[cfg(feature = "morph")] spec_to_generic!(CURIE, assert_eq!(SPEC::SPEC_ID, CURIE)); spec_to_generic!(CANCUN, assert_eq!(SPEC::SPEC_ID, CANCUN)); #[cfg(feature = "optimism")] From 7049dfecf50b7962f4578286b14ab69d78d711bb Mon Sep 17 00:00:00 2001 From: chengwenxi <22697326+chengwenxi@users.noreply.github.com> Date: Fri, 21 Feb 2025 11:01:03 +0800 Subject: [PATCH 2/4] fix test --- crates/precompile/src/lib.rs | 2 +- .../revm/src/context/context_precompiles.rs | 2 +- crates/revm/src/handler/mainnet/validation.rs | 32 +++++++++++-------- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/crates/precompile/src/lib.rs b/crates/precompile/src/lib.rs index 3e1bb9c0f9..7de20e04b8 100644 --- a/crates/precompile/src/lib.rs +++ b/crates/precompile/src/lib.rs @@ -41,7 +41,7 @@ use once_cell::race::OnceBox; use std::{boxed::Box, vec::Vec}; pub fn calc_linear_cost_u32(len: usize, base: u64, word: u64) -> u64 { - (len as u64 + 32 - 1) / 32 * word + base + (len as u64).div_ceil(32) * word + base } #[derive(Clone, Default, Debug)] diff --git a/crates/revm/src/context/context_precompiles.rs b/crates/revm/src/context/context_precompiles.rs index f674093535..f3fa881a14 100644 --- a/crates/revm/src/context/context_precompiles.rs +++ b/crates/revm/src/context/context_precompiles.rs @@ -95,7 +95,7 @@ impl ContextPrecompiles { /// Returns precompiles addresses. #[inline] - pub fn addresses<'a>(&'a self) -> Box + 'a> { + pub fn addresses<'a>(&'a self) -> Box + 'a> { match self.inner { PrecompilesCow::StaticRef(inner) => Box::new(inner.addresses()), PrecompilesCow::Owned(ref inner) => Box::new(inner.keys()), diff --git a/crates/revm/src/handler/mainnet/validation.rs b/crates/revm/src/handler/mainnet/validation.rs index 32f95a3049..8aa4c86c23 100644 --- a/crates/revm/src/handler/mainnet/validation.rs +++ b/crates/revm/src/handler/mainnet/validation.rs @@ -49,7 +49,8 @@ pub fn validate_initial_tx_gas( .map(|l| l.len() as u64) .unwrap_or_default(); - let mut initial_gas_spend = gas::validate_initial_tx_gas( + #[cfg(not(feature = "morph"))] + let initial_gas_spend = gas::validate_initial_tx_gas( SPEC::SPEC_ID, input, is_create, @@ -57,20 +58,25 @@ pub fn validate_initial_tx_gas( authorization_list_num, ); + #[cfg(feature = "morph")] + let initial_gas_spend = { + let gas = gas::validate_initial_tx_gas( + SPEC::SPEC_ID, + input, + is_create, + access_list, + authorization_list_num, + ); + if gas > env.tx.gas_limit && env.tx.morph.is_l1_msg { + env.tx.gas_limit + } else { + gas + } + }; + // Additional check to see if limit is big enough to cover initial gas. if initial_gas_spend > env.tx.gas_limit { - cfg_if::cfg_if! { - if #[cfg(not(feature = "morph"))] { - return Err(InvalidTransaction::CallGasCostMoreThanGasLimit.into()); - } else { - // reset initial gas spend for l1 message to ensure execution doesn't fail - if env.tx.morph.is_l1_msg { - initial_gas_spend = env.tx.gas_limit - } else { - return Err(InvalidTransaction::CallGasCostMoreThanGasLimit.into()); - } - } - } + return Err(InvalidTransaction::CallGasCostMoreThanGasLimit.into()); } Ok(initial_gas_spend) } From 9a32436062ed108f7b47dea29f1e13c3e17ef650 Mon Sep 17 00:00:00 2001 From: chengwenxi <22697326+chengwenxi@users.noreply.github.com> Date: Fri, 21 Feb 2025 11:16:58 +0800 Subject: [PATCH 3/4] fix clippy --- crates/interpreter/src/opcode/tables.rs | 2 +- crates/revm/src/context/inner_evm_context.rs | 3 --- crates/revm/src/handler/handle_types/execution.rs | 2 +- crates/revm/src/handler/handle_types/post_execution.rs | 2 +- crates/revm/src/handler/handle_types/pre_execution.rs | 2 +- crates/revm/src/handler/handle_types/validation.rs | 2 +- 6 files changed, 5 insertions(+), 8 deletions(-) diff --git a/crates/interpreter/src/opcode/tables.rs b/crates/interpreter/src/opcode/tables.rs index 48a1b6cca8..5a9d64192f 100644 --- a/crates/interpreter/src/opcode/tables.rs +++ b/crates/interpreter/src/opcode/tables.rs @@ -27,7 +27,7 @@ pub enum InstructionTables<'a, H: ?Sized> { Boxed(BoxedInstructionTable<'a, H>), } -impl<'a, H: Host + ?Sized> InstructionTables<'a, H> { +impl InstructionTables<'_, H> { /// Creates a plain instruction table for the given spec. See [`make_instruction_table`]. #[inline] pub const fn new_plain() -> Self { diff --git a/crates/revm/src/context/inner_evm_context.rs b/crates/revm/src/context/inner_evm_context.rs index 4a4f94c594..ab86a9ee1f 100644 --- a/crates/revm/src/context/inner_evm_context.rs +++ b/crates/revm/src/context/inner_evm_context.rs @@ -227,9 +227,6 @@ impl InnerEvmContext { .map(|acc| (acc.info.code_size, acc.is_cold)) } - /// Get code hash of address. - /// - /// Get code hash of address. /// /// In case of EOF account it will return `EOF_MAGIC_HASH` diff --git a/crates/revm/src/handler/handle_types/execution.rs b/crates/revm/src/handler/handle_types/execution.rs index 27d31eb118..d8d86e5190 100644 --- a/crates/revm/src/handler/handle_types/execution.rs +++ b/crates/revm/src/handler/handle_types/execution.rs @@ -162,7 +162,7 @@ impl<'a, EXT: 'a, DB: Database + 'a> ExecutionHandler<'a, EXT, DB> { } } -impl<'a, EXT, DB: Database> ExecutionHandler<'a, EXT, DB> { +impl ExecutionHandler<'_, EXT, DB> { /// Executes single frame. #[inline] pub fn execute_frame( diff --git a/crates/revm/src/handler/handle_types/post_execution.rs b/crates/revm/src/handler/handle_types/post_execution.rs index b6899bbfba..564f9b165b 100644 --- a/crates/revm/src/handler/handle_types/post_execution.rs +++ b/crates/revm/src/handler/handle_types/post_execution.rs @@ -74,7 +74,7 @@ impl<'a, EXT: 'a, DB: Database + 'a> PostExecutionHandler<'a, EXT, DB> { } } -impl<'a, EXT, DB: Database> PostExecutionHandler<'a, EXT, DB> { +impl PostExecutionHandler<'_, EXT, DB> { /// Calculate final refund pub fn refund(&self, context: &mut Context, gas: &mut Gas, eip7702_refund: i64) { (self.refund)(context, gas, eip7702_refund) diff --git a/crates/revm/src/handler/handle_types/pre_execution.rs b/crates/revm/src/handler/handle_types/pre_execution.rs index 2645b03a19..3a12ab83f6 100644 --- a/crates/revm/src/handler/handle_types/pre_execution.rs +++ b/crates/revm/src/handler/handle_types/pre_execution.rs @@ -45,7 +45,7 @@ impl<'a, EXT: 'a, DB: Database + 'a> PreExecutionHandler<'a, EXT, DB> { } } -impl<'a, EXT, DB: Database> PreExecutionHandler<'a, EXT, DB> { +impl PreExecutionHandler<'_, EXT, DB> { /// Deduct caller to its limit. pub fn deduct_caller(&self, context: &mut Context) -> Result<(), EVMError> { (self.deduct_caller)(context) diff --git a/crates/revm/src/handler/handle_types/validation.rs b/crates/revm/src/handler/handle_types/validation.rs index 8aa55c28c2..30fbc2d776 100644 --- a/crates/revm/src/handler/handle_types/validation.rs +++ b/crates/revm/src/handler/handle_types/validation.rs @@ -39,7 +39,7 @@ impl<'a, EXT: 'a, DB: Database + 'a> ValidationHandler<'a, EXT, DB> { } } -impl<'a, EXT, DB: Database> ValidationHandler<'a, EXT, DB> { +impl ValidationHandler<'_, EXT, DB> { /// Validate env. pub fn env(&self, env: &Env) -> Result<(), EVMError> { (self.env)(env) From 568e946ee59793dc46c79318c118866b235ff940 Mon Sep 17 00:00:00 2001 From: chengwenxi <22697326+chengwenxi@users.noreply.github.com> Date: Wed, 19 Mar 2025 10:56:30 +0800 Subject: [PATCH 4/4] update spec id --- crates/primitives/src/specification.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/primitives/src/specification.rs b/crates/primitives/src/specification.rs index 724c6ff863..17dc5c09ed 100644 --- a/crates/primitives/src/specification.rs +++ b/crates/primitives/src/specification.rs @@ -110,10 +110,10 @@ pub enum SpecId { /// Although the Curie update include new opcodes in Cancun, the most important change /// `EIP-4844` is not included. So we sort it before Cancun. CURIE = 19, - CANCUN = 20, - PRAGUE = 21, - PRAGUE_EOF = 22, - MORPH203 = 23, // revert Precompiles: RIPEMD-160, point evaluation, modexp, ecPairing + MORPH203 = 20, // revert Precompiles: RIPEMD-160, point evaluation, modexp, ecPairing + CANCUN = 21, + PRAGUE = 22, + PRAGUE_EOF = 23, #[default] LATEST = u8::MAX, }