Skip to content

Commit

Permalink
[fix] pre 4844 update (#9)
Browse files Browse the repository at this point in the history
* fix pre 4844 update

* fix clippy

* add test

* fmt

* fix test
  • Loading branch information
lightsing authored Jul 3, 2024
1 parent 30613ed commit 92d811b
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 20 deletions.
8 changes: 7 additions & 1 deletion crates/precompile/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
);
Expand Down
40 changes: 30 additions & 10 deletions crates/precompile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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<Precompiles> = 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<Precompiles> = OnceBox::new();
INSTANCE.get_or_init(|| {
let mut precompiles = Self::pre_bernoulli().clone();
precompiles.extend([
hash::SHA256, // 0x02
]);
Box::new(precompiles)
})
Expand Down Expand Up @@ -266,13 +281,16 @@ impl From<PrecompileWithAddress> for (Address, Precompile) {
}
}

#[allow(non_camel_case_types)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub enum PrecompileSpecId {
HOMESTEAD,
BYZANTIUM,
ISTANBUL,
BERLIN,
#[cfg(feature = "scroll")]
PRE_BERNOULLI,
#[cfg(feature = "scroll")]
BERNOULLI,
CANCUN,
PRAGUE,
Expand All @@ -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,
}
}
Expand Down
55 changes: 51 additions & 4 deletions crates/primitives/src/specification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -185,6 +199,8 @@ impl From<SpecId> 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",
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -488,11 +513,33 @@ mod optimism_tests {
mod scroll_tests {
use super::*;

#[test]
fn test_pre_bernoulli_post_merge_hardforks() {
assert!(PreBernoulliSpec::enabled(SpecId::MERGE));
assert!(PreBernoulliSpec::enabled(SpecId::SHANGHAI));
assert!(!PreBernoulliSpec::enabled(SpecId::BERNOULLI));
assert!(!PreBernoulliSpec::enabled(SpecId::CURIE));
assert!(!PreBernoulliSpec::enabled(SpecId::CANCUN));
assert!(!PreBernoulliSpec::enabled(SpecId::LATEST));
}

#[test]
fn test_bernoulli_post_merge_hardforks() {
assert!(BernoulliSpec::enabled(SpecId::MERGE));
assert!(BernoulliSpec::enabled(SpecId::SHANGHAI));
assert!(BernoulliSpec::enabled(SpecId::PRE_BERNOULLI));
assert!(!BernoulliSpec::enabled(SpecId::CURIE));
assert!(!BernoulliSpec::enabled(SpecId::CANCUN));
assert!(!BernoulliSpec::enabled(SpecId::LATEST));
}

#[test]
fn test_curie_post_merge_hardforks() {
assert!(CurieSpec::enabled(SpecId::MERGE));
assert!(CurieSpec::enabled(SpecId::SHANGHAI));
assert!(CurieSpec::enabled(SpecId::PRE_BERNOULLI));
assert!(CurieSpec::enabled(SpecId::BERNOULLI));
assert!(!CurieSpec::enabled(SpecId::CANCUN));
assert!(!CurieSpec::enabled(SpecId::LATEST));
}
}
9 changes: 4 additions & 5 deletions crates/revm/src/scroll/l1block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl L1BlockInfo {
}
}

fn calculate_tx_l1_cost_bernoulli(&self, input: &[u8], spec_id: SpecId) -> U256 {
fn calculate_tx_l1_cost_pre_bernoulli(&self, input: &[u8], spec_id: SpecId) -> U256 {
let tx_l1_gas = self.data_gas(input, spec_id);
tx_l1_gas
.saturating_mul(self.l1_base_fee)
Expand All @@ -113,11 +113,10 @@ impl L1BlockInfo {

/// Calculate the gas cost of a transaction based on L1 block data posted on L2.
pub fn calculate_tx_l1_cost(&self, input: &[u8], spec_id: SpecId) -> U256 {
let l1fee = if !spec_id.is_enabled_in(SpecId::CURIE) {
self.calculate_tx_l1_cost_bernoulli(input, spec_id)
if !spec_id.is_enabled_in(SpecId::CURIE) {
self.calculate_tx_l1_cost_pre_bernoulli(input, spec_id)
} else {
self.calculate_tx_l1_cost_curie(input, spec_id)
};
l1fee
}
}
}

0 comments on commit 92d811b

Please sign in to comment.