Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "trevm"
version = "0.23.2"
version = "0.23.4"
rust-version = "1.83.0"
edition = "2021"
authors = ["init4"]
Expand Down Expand Up @@ -76,7 +76,7 @@ default = [
"revm/secp256k1",
]

alloy-db = ["dep:tokio"]
alloy-db = ["dep:tokio", "alloy/providers"]

call = ["optional_eip3607", "optional_no_base_fee"]

Expand All @@ -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"]

Expand Down
40 changes: 34 additions & 6 deletions src/builder.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -19,13 +19,14 @@ pub struct TrevmBuilder<Db, Insp> {
pub(crate) db: Option<Db>,
pub(crate) insp: Insp,
pub(crate) spec: SpecId,
pub(crate) precompiles: Option<&'static Precompiles>,
}

impl TrevmBuilder<InMemoryDB, NoOpInspector> {
/// 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 }
}
}

Expand All @@ -35,12 +36,17 @@ impl<Db, Insp> TrevmBuilder<Db, Insp> {
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<OInsp>(self, insp: OInsp) -> TrevmBuilder<Db, OInsp> {
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.
Expand All @@ -49,6 +55,22 @@ impl<Db, Insp> TrevmBuilder<Db, Insp> {
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<EvmNeedsCfg<Db, Insp>, TrevmBuilderError>
where
Expand All @@ -57,7 +79,13 @@ impl<Db, Insp> TrevmBuilder<Db, Insp> {
{
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))
}
}