Skip to content

Commit

Permalink
perf: Add static Optimism precompiles for Fjord & Granite (#1864)
Browse files Browse the repository at this point in the history
  • Loading branch information
hai-rise authored Nov 20, 2024
1 parent 900409f commit 9519b11
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 17 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

9 changes: 8 additions & 1 deletion crates/revm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions crates/revm/src/optimism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 6 additions & 16 deletions crates/revm/src/optimism/handler_register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -163,23 +163,13 @@ pub fn refund<SPEC: Spec, EXT, DB: Database>(
/// Load precompiles for Optimism chain.
#[inline]
pub fn load_precompiles<SPEC: Spec, EXT, DB: Database>() -> ContextPrecompiles<DB> {
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.
Expand Down
33 changes: 33 additions & 0 deletions crates/revm/src/optimism/precompile.rs
Original file line number Diff line number Diff line change
@@ -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<Precompiles> = 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<Precompiles> = 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)
})
}

0 comments on commit 9519b11

Please sign in to comment.