From f64ea09757c2f9b8de19a182919a5a79e1ebec48 Mon Sep 17 00:00:00 2001 From: Hai Nguyen Date: Tue, 19 Nov 2024 18:51:15 +0700 Subject: [PATCH] perf: Add static Optimism precompiles for Fjord & Granite --- Cargo.lock | 1 + crates/revm/Cargo.toml | 9 +++++- crates/revm/src/optimism.rs | 1 + crates/revm/src/optimism/handler_register.rs | 22 ++++--------- crates/revm/src/optimism/precompile.rs | 33 ++++++++++++++++++++ 5 files changed, 49 insertions(+), 17 deletions(-) create mode 100644 crates/revm/src/optimism/precompile.rs diff --git a/Cargo.lock b/Cargo.lock index 9d3cb3e65c..ff22884fad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3199,6 +3199,7 @@ dependencies = [ "ethers-core", "ethers-providers", "indicatif", + "once_cell", "reqwest 0.12.7", "revm-interpreter", "revm-precompile", diff --git a/crates/revm/Cargo.toml b/crates/revm/Cargo.toml index 2ab2440d61..22b0409a61 100644 --- a/crates/revm/Cargo.toml +++ b/crates/revm/Cargo.toml @@ -32,6 +32,9 @@ cfg-if = "1.0" dyn-clone = "1.0" # Optional +once_cell = { version = "1.19", default-features = false, features = [ + "alloc", +], optional = true } serde = { version = "1.0", default-features = false, features = [ "derive", "rc", @@ -83,7 +86,11 @@ portable = ["revm-precompile/portable", "revm-interpreter/portable"] test-utils = [] -optimism = ["revm-interpreter/optimism", "revm-precompile/optimism"] +optimism = [ + "revm-interpreter/optimism", + "revm-precompile/optimism", + "dep:once_cell", +] # Optimism default handler enabled Optimism handler register by default in EvmBuilder. optimism-default-handler = [ "optimism", diff --git a/crates/revm/src/optimism.rs b/crates/revm/src/optimism.rs index 05256ca290..61e24cb842 100644 --- a/crates/revm/src/optimism.rs +++ b/crates/revm/src/optimism.rs @@ -4,6 +4,7 @@ mod bn128; mod fast_lz; mod handler_register; mod l1block; +mod precompile; pub use handler_register::{ deduct_caller, end, last_frame_return, load_accounts, load_precompiles, diff --git a/crates/revm/src/optimism/handler_register.rs b/crates/revm/src/optimism/handler_register.rs index f9141e0407..43fcca8f60 100644 --- a/crates/revm/src/optimism/handler_register.rs +++ b/crates/revm/src/optimism/handler_register.rs @@ -15,7 +15,7 @@ use crate::{ Context, ContextPrecompiles, FrameResult, }; use core::ops::Mul; -use revm_precompile::{secp256r1, PrecompileSpecId}; +use revm_precompile::PrecompileSpecId; use std::string::ToString; use std::sync::Arc; @@ -163,23 +163,13 @@ pub fn refund( /// Load precompiles for Optimism chain. #[inline] pub fn load_precompiles() -> ContextPrecompiles { - let mut precompiles = ContextPrecompiles::new(PrecompileSpecId::from_spec_id(SPEC::SPEC_ID)); - - if SPEC::enabled(SpecId::FJORD) { - precompiles.extend([ - // EIP-7212: secp256r1 P256verify - secp256r1::P256VERIFY, - ]) - } - if SPEC::enabled(SpecId::GRANITE) { - precompiles.extend([ - // Restrict bn256Pairing input size - optimism::bn128::pair::GRANITE, - ]) + ContextPrecompiles::from_static_precompiles(optimism::precompile::granite()) + } else if SPEC::enabled(SpecId::FJORD) { + ContextPrecompiles::from_static_precompiles(optimism::precompile::fjord()) + } else { + ContextPrecompiles::new(PrecompileSpecId::from_spec_id(SPEC::SPEC_ID)) } - - precompiles } /// Load account (make them warm) and l1 data from database. diff --git a/crates/revm/src/optimism/precompile.rs b/crates/revm/src/optimism/precompile.rs new file mode 100644 index 0000000000..3d79ef6531 --- /dev/null +++ b/crates/revm/src/optimism/precompile.rs @@ -0,0 +1,33 @@ +use once_cell::race::OnceBox; +use revm_precompile::{secp256r1, Precompiles}; +use std::boxed::Box; + +/// Returns precompiles for Fjord spec. +pub(crate) fn fjord() -> &'static Precompiles { + static INSTANCE: OnceBox = OnceBox::new(); + INSTANCE.get_or_init(|| { + let mut precompiles = Precompiles::cancun().clone(); + + precompiles.extend([ + // EIP-7212: secp256r1 P256verify + secp256r1::P256VERIFY, + ]); + + Box::new(precompiles) + }) +} + +/// Returns precompiles for Granite spec. +pub(crate) fn granite() -> &'static Precompiles { + static INSTANCE: OnceBox = OnceBox::new(); + INSTANCE.get_or_init(|| { + let mut precompiles = fjord().clone(); + + precompiles.extend([ + // Restrict bn256Pairing input size + crate::optimism::bn128::pair::GRANITE, + ]); + + Box::new(precompiles) + }) +}