From 5641529b5e51e2cf1e5e5ae4a0183e53b1b01ac0 Mon Sep 17 00:00:00 2001 From: lightsing Date: Wed, 3 Jul 2024 16:01:09 +0800 Subject: [PATCH] fix pre 4844 update --- crates/precompile/src/hash.rs | 8 ++++- crates/precompile/src/lib.rs | 40 ++++++++++++++++++------ crates/primitives/src/specification.rs | 42 +++++++++++++++++++++++--- 3 files changed, 75 insertions(+), 15 deletions(-) diff --git a/crates/precompile/src/hash.rs b/crates/precompile/src/hash.rs index 1541e86a30..8a8dae0557 100644 --- a/crates/precompile/src/hash.rs +++ b/crates/precompile/src/hash.rs @@ -9,13 +9,19 @@ use revm_primitives::PrecompileError; pub const SHA256: PrecompileWithAddress = PrecompileWithAddress(crate::u64_to_address(2), Precompile::Standard(sha256_run)); +#[cfg(feature = "scroll")] +pub const SHA256_PRE_BERNOULLI: PrecompileWithAddress = PrecompileWithAddress( + crate::u64_to_address(2), + Precompile::Standard(|_input: &Bytes, _gas_limit: u64| Err(PrecompileError::NotImplemented)), +); + pub const RIPEMD160: PrecompileWithAddress = PrecompileWithAddress( crate::u64_to_address(3), Precompile::Standard(ripemd160_run), ); #[cfg(feature = "scroll")] -pub const RIPEMD160_BERNOULLI: PrecompileWithAddress = PrecompileWithAddress( +pub const RIPEMD160_PRE_BERNOULLI: PrecompileWithAddress = PrecompileWithAddress( crate::u64_to_address(3), Precompile::Standard(|_input: &Bytes, _gas_limit: u64| Err(PrecompileError::NotImplemented)), ); diff --git a/crates/precompile/src/lib.rs b/crates/precompile/src/lib.rs index 55e58154ec..a740f47e53 100644 --- a/crates/precompile/src/lib.rs +++ b/crates/precompile/src/lib.rs @@ -65,6 +65,8 @@ impl Precompiles { PrecompileSpecId::ISTANBUL => Self::istanbul(), PrecompileSpecId::BERLIN => Self::berlin(), #[cfg(feature = "scroll")] + PrecompileSpecId::PRE_BERNOULLI => Self::pre_bernoulli(), + #[cfg(feature = "scroll")] PrecompileSpecId::BERNOULLI => Self::bernoulli(), PrecompileSpecId::CANCUN => Self::cancun(), PrecompileSpecId::PRAGUE => Self::prague(), @@ -179,20 +181,33 @@ impl Precompiles { /// Returns precompiles for Scroll #[cfg(feature = "scroll")] - pub fn bernoulli() -> &'static Self { + pub fn pre_bernoulli() -> &'static Self { static INSTANCE: OnceBox = OnceBox::new(); INSTANCE.get_or_init(|| { let mut precompiles = Precompiles::default(); precompiles.extend([ - secp256k1::ECRECOVER, // 0x01 - hash::SHA256, // 0x02 - hash::RIPEMD160_BERNOULLI, // 0x03 - identity::FUN, // 0x04 - modexp::BERNOULLI, // 0x05 - bn128::add::ISTANBUL, // 0x06 - bn128::mul::ISTANBUL, // 0x07 - bn128::pair::BERNOULLI, // 0x08 - blake2::BERNOULLI, // 0x09 + secp256k1::ECRECOVER, // 0x01 + hash::SHA256_PRE_BERNOULLI, // 0x02 + hash::RIPEMD160_PRE_BERNOULLI, // 0x03 + identity::FUN, // 0x04 + modexp::BERNOULLI, // 0x05 + bn128::add::ISTANBUL, // 0x06 + bn128::mul::ISTANBUL, // 0x07 + bn128::pair::BERNOULLI, // 0x08 + blake2::BERNOULLI, // 0x09 + ]); + Box::new(precompiles) + }) + } + + /// Returns precompiles for Scroll + #[cfg(feature = "scroll")] + pub fn bernoulli() -> &'static Self { + static INSTANCE: OnceBox = OnceBox::new(); + INSTANCE.get_or_init(|| { + let mut precompiles = Self::pre_bernoulli().clone(); + precompiles.extend([ + hash::SHA256, // 0x02 ]); Box::new(precompiles) }) @@ -266,6 +281,7 @@ impl From for (Address, Precompile) { } } +#[allow(non_camel_case_types)] #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Ord, PartialOrd)] pub enum PrecompileSpecId { HOMESTEAD, @@ -273,6 +289,8 @@ pub enum PrecompileSpecId { ISTANBUL, BERLIN, #[cfg(feature = "scroll")] + PRE_BERNOULLI, + #[cfg(feature = "scroll")] BERNOULLI, CANCUN, PRAGUE, @@ -298,6 +316,8 @@ impl PrecompileSpecId { #[cfg(feature = "optimism")] ECOTONE => Self::CANCUN, #[cfg(feature = "scroll")] + PRE_BERNOULLI => Self::PRE_BERNOULLI, + #[cfg(feature = "scroll")] BERNOULLI | CURIE => Self::BERNOULLI, } } diff --git a/crates/primitives/src/specification.rs b/crates/primitives/src/specification.rs index 675163b2bf..5cfa02a7be 100644 --- a/crates/primitives/src/specification.rs +++ b/crates/primitives/src/specification.rs @@ -93,10 +93,20 @@ pub enum SpecId { GRAY_GLACIER = 14, MERGE = 15, SHANGHAI = 16, - BERNOULLI = 17, - CURIE = 18, - CANCUN = 19, - PRAGUE = 20, + /// The scroll network initially started with Shanghai with some features disabled. + PRE_BERNOULLI = 17, + /// Bernoulli update introduces: + /// - Enable `SHA-256` precompile. + /// - Use `EIP-4844` blobs for Data Availability (not part of layer2). + BERNOULLI = 18, + /// Curie update introduces: + /// - Support `EIP-1559` transactions. + /// - Support the `BASEFEE`, `MCOPY`, `TLOAD`, `TSTORE` opcodes. + /// 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, #[default] LATEST = u8::MAX, } @@ -148,7 +158,11 @@ impl From<&str> for SpecId { #[cfg(feature = "optimism")] "Ecotone" => SpecId::ECOTONE, #[cfg(feature = "scroll")] + "PreBernoulli" => SpecId::PRE_BERNOULLI, + #[cfg(feature = "scroll")] "Bernoulli" => SpecId::BERNOULLI, + #[cfg(feature = "scroll")] + "Curie" => SpecId::CURIE, _ => Self::LATEST, } } @@ -185,6 +199,8 @@ impl From for &'static str { #[cfg(feature = "optimism")] SpecId::ECOTONE => "Ecotone", #[cfg(feature = "scroll")] + SpecId::PRE_BERNOULLI => "PreBernoulli", + #[cfg(feature = "scroll")] SpecId::BERNOULLI => "Bernoulli", #[cfg(feature = "scroll")] SpecId::CURIE => "Curie", @@ -249,6 +265,8 @@ spec!(ECOTONE, EcotoneSpec); // Scroll Hardforks #[cfg(feature = "scroll")] +spec!(PRE_BERNOULLI, PreBernoulliSpec); +#[cfg(feature = "scroll")] spec!(BERNOULLI, BernoulliSpec); #[cfg(feature = "scroll")] spec!(CURIE, CurieSpec); @@ -337,6 +355,11 @@ macro_rules! spec_to_generic { $e } #[cfg(feature = "scroll")] + $crate::SpecId::PRE_BERNOULLI => { + use $crate::PreBernoulliSpec as SPEC; + $e + } + #[cfg(feature = "scroll")] $crate::SpecId::BERNOULLI => { use $crate::BernoulliSpec as SPEC; $e @@ -382,6 +405,8 @@ mod tests { #[cfg(feature = "optimism")] spec_to_generic!(CANYON, assert_eq!(SPEC::SPEC_ID, CANYON)); #[cfg(feature = "scroll")] + spec_to_generic!(PRE_BERNOULLI, assert_eq!(SPEC::SPEC_ID, PRE_BERNOULLI)); + #[cfg(feature = "scroll")] spec_to_generic!(BERNOULLI, assert_eq!(SPEC::SPEC_ID, BERNOULLI)); spec_to_generic!(CANCUN, assert_eq!(SPEC::SPEC_ID, CANCUN)); #[cfg(feature = "scroll")] @@ -488,6 +513,15 @@ mod optimism_tests { mod scroll_tests { use super::*; + #[test] + fn test_pre_bernoulli_post_merge_hardforks() { + assert!(PreBernoulliSpec::enabled(SpecId::MERGE)); + assert!(BernoulliSpec::enabled(SpecId::SHANGHAI)); + assert!(!BernoulliSpec::enabled(SpecId::BERNOULLI)); + assert!(!BernoulliSpec::enabled(SpecId::CANCUN)); + assert!(!BernoulliSpec::enabled(SpecId::LATEST)); + } + #[test] fn test_bernoulli_post_merge_hardforks() { assert!(BernoulliSpec::enabled(SpecId::MERGE));