diff --git a/Cargo.toml b/Cargo.toml index 53730ad..dd34d99 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "trevm" -version = "0.23.2" +version = "0.23.4" rust-version = "1.83.0" edition = "2021" authors = ["init4"] @@ -76,7 +76,7 @@ default = [ "revm/secp256k1", ] -alloy-db = ["dep:tokio"] +alloy-db = ["dep:tokio", "alloy/providers"] call = ["optional_eip3607", "optional_no_base_fee"] @@ -87,6 +87,7 @@ estimate_gas = ["optional_eip3607", "optional_no_base_fee", "dep:tracing"] test-utils = ["revm/std", "revm/serde-json", "revm/alloydb"] secp256k1 = ["revm/secp256k1"] +secp256r1 = ["revm/secp256r1"] c-kzg = ["revm/c-kzg"] blst = ["revm/blst"] diff --git a/src/builder.rs b/src/builder.rs index 7ad880b..4138898 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -1,7 +1,7 @@ use crate::{evm::Trevm, helpers::Ctx, states::EvmNeedsCfg}; use revm::{ - database::in_memory_db::InMemoryDB, inspector::NoOpInspector, primitives::hardfork::SpecId, - Database, Inspector, MainBuilder, + database::in_memory_db::InMemoryDB, handler::EthPrecompiles, inspector::NoOpInspector, + precompile::Precompiles, primitives::hardfork::SpecId, Database, Inspector, MainBuilder, }; /// Error that can occur when building a Trevm instance. @@ -19,13 +19,14 @@ pub struct TrevmBuilder { pub(crate) db: Option, pub(crate) insp: Insp, pub(crate) spec: SpecId, + pub(crate) precompiles: Option<&'static Precompiles>, } impl TrevmBuilder { /// Create a new builder with the default database and inspector. #[allow(clippy::new_without_default)] // default would make bad devex :( pub const fn new() -> Self { - Self { db: None, insp: NoOpInspector, spec: SpecId::PRAGUE } + Self { db: None, insp: NoOpInspector, spec: SpecId::PRAGUE, precompiles: None } } } @@ -35,12 +36,17 @@ impl TrevmBuilder { where Db: Database, { - TrevmBuilder { db: Some(db), insp: self.insp, spec: self.spec } + TrevmBuilder { + db: Some(db), + insp: self.insp, + spec: self.spec, + precompiles: self.precompiles, + } } /// Set the inspector for the EVM. pub fn with_insp(self, insp: OInsp) -> TrevmBuilder { - TrevmBuilder { db: self.db, insp, spec: self.spec } + TrevmBuilder { db: self.db, insp, spec: self.spec, precompiles: self.precompiles } } /// Set the spec id for the EVM. @@ -49,6 +55,22 @@ impl TrevmBuilder { self } + /// Set the precompiles for the EVM. + /// + /// The precompiles must be a static reference to a precompiles instance. + /// If not using a built-in [`Precompiles`], it is generally recommended to + /// use a `OnceLock` to create this borrow. + pub const fn with_precompiles(mut self, precompiles: &'static Precompiles) -> Self { + self.precompiles = Some(precompiles); + self + } + + /// Set the precompiles for the EVM from the current spec id. + pub fn with_precompiles_from_spec(mut self) -> Self { + self.precompiles = Some(Precompiles::new(self.spec.into())); + self + } + /// Build the Trevm instance. pub fn build_trevm(self) -> Result, TrevmBuilderError> where @@ -57,7 +79,13 @@ impl TrevmBuilder { { let db = self.db.ok_or(TrevmBuilderError::DatabaseNotSet)?; let ctx = Ctx::new(db, self.spec); - let evm = ctx.build_mainnet_with_inspector(self.insp); + + let mut evm = ctx.build_mainnet_with_inspector(self.insp); + + if let Some(precompiles) = self.precompiles { + evm.precompiles = EthPrecompiles { precompiles, spec: self.spec }; + } + Ok(Trevm::from(evm)) } }