Skip to content

Commit 91e5f4f

Browse files
authored
feat: add precompiles to builder (#107)
* feat: add precompiles to builder * doc: more doc * chore: version
1 parent a086074 commit 91e5f4f

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "trevm"
3-
version = "0.23.2"
3+
version = "0.23.4"
44
rust-version = "1.83.0"
55
edition = "2021"
66
authors = ["init4"]
@@ -76,7 +76,7 @@ default = [
7676
"revm/secp256k1",
7777
]
7878

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

8181
call = ["optional_eip3607", "optional_no_base_fee"]
8282

@@ -87,6 +87,7 @@ estimate_gas = ["optional_eip3607", "optional_no_base_fee", "dep:tracing"]
8787
test-utils = ["revm/std", "revm/serde-json", "revm/alloydb"]
8888

8989
secp256k1 = ["revm/secp256k1"]
90+
secp256r1 = ["revm/secp256r1"]
9091
c-kzg = ["revm/c-kzg"]
9192
blst = ["revm/blst"]
9293

src/builder.rs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{evm::Trevm, helpers::Ctx, states::EvmNeedsCfg};
22
use revm::{
3-
database::in_memory_db::InMemoryDB, inspector::NoOpInspector, primitives::hardfork::SpecId,
4-
Database, Inspector, MainBuilder,
3+
database::in_memory_db::InMemoryDB, handler::EthPrecompiles, inspector::NoOpInspector,
4+
precompile::Precompiles, primitives::hardfork::SpecId, Database, Inspector, MainBuilder,
55
};
66

77
/// Error that can occur when building a Trevm instance.
@@ -19,13 +19,14 @@ pub struct TrevmBuilder<Db, Insp> {
1919
pub(crate) db: Option<Db>,
2020
pub(crate) insp: Insp,
2121
pub(crate) spec: SpecId,
22+
pub(crate) precompiles: Option<&'static Precompiles>,
2223
}
2324

2425
impl TrevmBuilder<InMemoryDB, NoOpInspector> {
2526
/// Create a new builder with the default database and inspector.
2627
#[allow(clippy::new_without_default)] // default would make bad devex :(
2728
pub const fn new() -> Self {
28-
Self { db: None, insp: NoOpInspector, spec: SpecId::PRAGUE }
29+
Self { db: None, insp: NoOpInspector, spec: SpecId::PRAGUE, precompiles: None }
2930
}
3031
}
3132

@@ -35,12 +36,17 @@ impl<Db, Insp> TrevmBuilder<Db, Insp> {
3536
where
3637
Db: Database,
3738
{
38-
TrevmBuilder { db: Some(db), insp: self.insp, spec: self.spec }
39+
TrevmBuilder {
40+
db: Some(db),
41+
insp: self.insp,
42+
spec: self.spec,
43+
precompiles: self.precompiles,
44+
}
3945
}
4046

4147
/// Set the inspector for the EVM.
4248
pub fn with_insp<OInsp>(self, insp: OInsp) -> TrevmBuilder<Db, OInsp> {
43-
TrevmBuilder { db: self.db, insp, spec: self.spec }
49+
TrevmBuilder { db: self.db, insp, spec: self.spec, precompiles: self.precompiles }
4450
}
4551

4652
/// Set the spec id for the EVM.
@@ -49,6 +55,22 @@ impl<Db, Insp> TrevmBuilder<Db, Insp> {
4955
self
5056
}
5157

58+
/// Set the precompiles for the EVM.
59+
///
60+
/// The precompiles must be a static reference to a precompiles instance.
61+
/// If not using a built-in [`Precompiles`], it is generally recommended to
62+
/// use a `OnceLock` to create this borrow.
63+
pub const fn with_precompiles(mut self, precompiles: &'static Precompiles) -> Self {
64+
self.precompiles = Some(precompiles);
65+
self
66+
}
67+
68+
/// Set the precompiles for the EVM from the current spec id.
69+
pub fn with_precompiles_from_spec(mut self) -> Self {
70+
self.precompiles = Some(Precompiles::new(self.spec.into()));
71+
self
72+
}
73+
5274
/// Build the Trevm instance.
5375
pub fn build_trevm(self) -> Result<EvmNeedsCfg<Db, Insp>, TrevmBuilderError>
5476
where
@@ -57,7 +79,13 @@ impl<Db, Insp> TrevmBuilder<Db, Insp> {
5779
{
5880
let db = self.db.ok_or(TrevmBuilderError::DatabaseNotSet)?;
5981
let ctx = Ctx::new(db, self.spec);
60-
let evm = ctx.build_mainnet_with_inspector(self.insp);
82+
83+
let mut evm = ctx.build_mainnet_with_inspector(self.insp);
84+
85+
if let Some(precompiles) = self.precompiles {
86+
evm.precompiles = EthPrecompiles { precompiles, spec: self.spec };
87+
}
88+
6189
Ok(Trevm::from(evm))
6290
}
6391
}

0 commit comments

Comments
 (0)