Skip to content

Commit 11f6ab9

Browse files
prestwichEvalir
andauthored
Dep: update revm to 20 (#94)
* wip * wip * wip * fixing * chore: fix doctests * fix: doclinks * lint: clippy * chore: depend on stable * nit: * docs: slight cleanup * chore: update example * chore: fix examples * nit: remove note * chore: clean up syscalls * docs: slight cleanup * Update src/builder.rs Co-authored-by: evalir <[email protected]> --------- Co-authored-by: evalir <[email protected]>
1 parent b58a5b4 commit 11f6ab9

38 files changed

+942
-1049
lines changed

Cargo.toml

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "trevm"
3-
version = "0.19.12"
3+
version = "0.20.0"
44
rust-version = "1.83.0"
55
edition = "2021"
66
authors = ["init4"]
@@ -27,21 +27,23 @@ option-if-let-else = "warn"
2727
redundant-clone = "warn"
2828

2929
[dependencies]
30-
alloy = { version = "=0.11.1", default-features = false, features = ["consensus", "rpc-types-mev", "eips", "k256", "std", "rlp", "sol-types"] }
30+
alloy = { version = "0.12.6", default-features = false, features = ["consensus", "rpc-types-mev", "eips", "k256", "std", "rlp", "sol-types"] }
3131

32-
revm = { version = "19.5.0", default-features = false, features = ["std"] }
32+
revm = { version = "20.0.0", default-features = false }
3333

3434
dashmap = { version = "6.1.0", optional = true }
3535
tracing = { version = "0.1.41", optional = true}
3636
thiserror = "2.0.11"
3737

3838
[dev-dependencies]
39-
revm = { version = "19.5.0", features = [
40-
"test-utils",
39+
revm = { version = "20.0.0", features = [
4140
"serde-json",
4241
"std",
4342
"alloydb",
4443
] }
44+
trevm = { path = ".", features = ["test-utils"] }
45+
46+
alloy = { version = "*", features = ["providers"]}
4547

4648
# misc
4749
eyre = "0.6"
@@ -67,7 +69,7 @@ concurrent-db = ["dep:dashmap"]
6769

6870
estimate_gas = ["optional_eip3607", "optional_no_base_fee", "dep:tracing"]
6971

70-
test-utils = ["revm/test-utils", "revm/std", "revm/serde-json", "revm/alloydb"]
72+
test-utils = ["revm/std", "revm/serde-json", "revm/alloydb"]
7173

7274
secp256k1 = ["revm/secp256k1"]
7375
c-kzg = ["revm/c-kzg"]
@@ -80,23 +82,17 @@ dev = [
8082
"optional_balance_check",
8183
"optional_block_gas_limit",
8284
"optional_eip3607",
83-
"optional_gas_refund",
8485
"optional_no_base_fee",
85-
"optional_beneficiary_reward",
8686
]
8787

8888
memory_limit = ["revm/memory_limit"]
8989
optional_balance_check = ["revm/optional_balance_check"]
90-
optional_beneficiary_reward = ["revm/optional_beneficiary_reward"]
9190
optional_block_gas_limit = ["revm/optional_block_gas_limit"]
9291
optional_eip3607 = ["revm/optional_eip3607"]
93-
optional_gas_refund = ["revm/optional_gas_refund"]
9492
optional_no_base_fee = ["revm/optional_no_base_fee"]
9593
full_env_cfg = [
9694
"optional_balance_check",
9795
"optional_block_gas_limit",
9896
"optional_eip3607",
99-
"optional_gas_refund",
10097
"optional_no_base_fee",
101-
"optional_beneficiary_reward",
10298
]

examples/basic_transact.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
//! Simple TREVM example that demonstrates how to execute a transaction on a contract.
22
//! It simply loads the contract bytecode and executes a transaction.
33
4+
use revm::context::TransactTo;
45
use trevm::{
56
revm::{
6-
inspector_handle_register,
7-
inspectors::TracerEip3155,
8-
primitives::{hex, AccountInfo, Address, Bytecode, TransactTo, U256},
9-
EvmBuilder, InMemoryDB,
7+
bytecode::Bytecode,
8+
database::InMemoryDB,
9+
inspector::inspectors::TracerEip3155,
10+
primitives::{hex, Address, U256},
11+
state::AccountInfo,
1012
},
1113
trevm_aliases, NoopBlock, NoopCfg, TrevmBuilder, Tx,
1214
};
@@ -27,18 +29,18 @@ const CALLER_ADDR: Address = Address::with_last_byte(1);
2729
struct SampleTx;
2830

2931
impl Tx for SampleTx {
30-
fn fill_tx_env(&self, tx_env: &mut revm::primitives::TxEnv) {
32+
fn fill_tx_env(&self, tx_env: &mut revm::context::TxEnv) {
3133
tx_env.caller = CALLER_ADDR;
32-
tx_env.transact_to = TransactTo::Call(CONTRACT_ADDR);
34+
tx_env.kind = TransactTo::Call(CONTRACT_ADDR);
3335
tx_env.data = hex::decode(PROGRAM_INPUT).unwrap().into();
3436
}
3537
}
3638

3739
// Produce aliases for the Trevm type
3840
trevm_aliases!(TracerEip3155, InMemoryDB);
3941

40-
fn main() {
41-
let mut db = revm::InMemoryDB::default();
42+
fn main() -> Result<(), Box<dyn std::error::Error>> {
43+
let mut db = revm::database::InMemoryDB::default();
4244

4345
let bytecode = Bytecode::new_raw(hex::decode(CONTRACT_BYTECODE).unwrap().into());
4446
let acc_info = AccountInfo::new(U256::ZERO, 1, bytecode.hash_slow(), bytecode);
@@ -47,18 +49,19 @@ fn main() {
4749
db.insert_contract(&mut acc_info.clone());
4850
db.insert_account_info(CONTRACT_ADDR, acc_info);
4951

50-
let evm = EvmBuilder::default()
52+
let insp = TracerEip3155::new(Box::new(std::io::stdout()));
53+
54+
let trevm = TrevmBuilder::new()
5155
.with_db(db)
52-
.with_external_context(TracerEip3155::new(Box::new(std::io::stdout())))
53-
.append_handler_register(inspector_handle_register)
54-
.build_trevm()
56+
.with_insp(insp)
57+
.build_trevm()?
5558
.fill_cfg(&NoopCfg)
5659
.fill_block(&NoopBlock);
5760

58-
let account = evm.read_account_ref(CONTRACT_ADDR).unwrap();
61+
let account = trevm.read_account_ref(CONTRACT_ADDR).unwrap();
5962
println!("account: {account:?}");
6063

61-
let evm = evm.fill_tx(&SampleTx).run();
64+
let evm = trevm.fill_tx(&SampleTx).run();
6265

6366
match evm {
6467
Ok(res) => {
@@ -69,4 +72,6 @@ fn main() {
6972
println!("Execution error: {e:?}");
7073
}
7174
};
75+
76+
Ok(())
7277
}

examples/fork_ref_transact.rs.bak renamed to examples/fork_ref_transact.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
44
//! This example is currently disabled while waiting for revm @ 14.0.4
55
6-
use alloy::{eips::BlockId, providers::ProviderBuilder};
7-
use alloy_primitives::{address, Address, TxKind, U256};
8-
use alloy_sol_types::{sol, SolCall};
6+
use alloy::{
7+
eips::BlockId,
8+
primitives::{address, Address, TxKind, U256},
9+
providers::ProviderBuilder,
10+
sol,
11+
sol_types::SolCall,
12+
};
13+
use revm::{context::TxEnv, database::WrapDatabaseAsync};
914
use trevm::{
10-
revm::{
11-
db::{AlloyDB, CacheDB},
12-
Evm,
13-
},
15+
revm::database::{AlloyDB, CacheDB},
1416
NoopBlock, NoopCfg, TrevmBuilder, Tx,
1517
};
1618

@@ -22,10 +24,10 @@ sol! {
2224
struct GetReservesFiller;
2325

2426
impl Tx for GetReservesFiller {
25-
fn fill_tx_env(&self, tx_env: &mut revm::primitives::TxEnv) {
27+
fn fill_tx_env(&self, tx_env: &mut TxEnv) {
2628
tx_env.caller = Address::with_last_byte(0);
2729
// ETH/USDT pair on Uniswap V2
28-
tx_env.transact_to = TxKind::Call(POOL_ADDRESS);
30+
tx_env.kind = TxKind::Call(POOL_ADDRESS);
2931
// calldata formed via alloy's abi encoder
3032
tx_env.data = getReservesCall::new(()).abi_encode().into();
3133
// transaction value in wei
@@ -55,15 +57,15 @@ async fn main() -> eyre::Result<()> {
5557
// =========================================================== //
5658

5759
// initialize new AlloyDB
58-
let alloydb = AlloyDB::new(client, BlockId::default()).unwrap();
60+
let alloydb = WrapDatabaseAsync::new(AlloyDB::new(client, BlockId::default())).unwrap();
5961

6062
// initialise empty in-memory-db
6163
let cache_db = CacheDB::new(alloydb);
6264

6365
// initialise an empty (default) EVM
64-
let evm = Evm::builder()
66+
let evm = TrevmBuilder::new()
6567
.with_db(cache_db)
66-
.build_trevm()
68+
.build_trevm()?
6769
.fill_cfg(&NoopCfg)
6870
.fill_block(&NoopBlock)
6971
.fill_tx(&GetReservesFiller)

src/builder.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use crate::{evm::Trevm, helpers::Ctx, states::EvmNeedsCfg};
2+
use revm::{
3+
database::in_memory_db::InMemoryDB, primitives::hardfork::SpecId, Database, MainBuilder,
4+
};
5+
6+
/// Error that can occur when building a Trevm instance.
7+
#[derive(Debug, Clone, thiserror::Error)]
8+
#[non_exhaustive]
9+
pub enum TrevmBuilderError {
10+
/// Database not set.
11+
#[error("Database not set")]
12+
DatabaseNotSet,
13+
}
14+
15+
/// A builder for [`Trevm`] that allows configuring the EVM.
16+
#[derive(Debug, Clone)]
17+
pub struct TrevmBuilder<Db, Insp> {
18+
pub(crate) db: Option<Db>,
19+
pub(crate) insp: Insp,
20+
pub(crate) spec: SpecId,
21+
}
22+
23+
impl TrevmBuilder<InMemoryDB, ()> {
24+
/// Create a new builder with the default database and inspector.
25+
#[allow(clippy::new_without_default)] // default would make bad devex :(
26+
pub const fn new() -> Self {
27+
Self { db: None, insp: (), spec: SpecId::PRAGUE }
28+
}
29+
}
30+
31+
impl<Db, Insp> TrevmBuilder<Db, Insp> {
32+
/// Set the database for the EVM.
33+
pub fn with_db<Odb>(self, db: Odb) -> TrevmBuilder<Odb, Insp>
34+
where
35+
Db: Database,
36+
{
37+
TrevmBuilder { db: Some(db), insp: self.insp, spec: self.spec }
38+
}
39+
40+
/// Set the inspector for the EVM.
41+
pub fn with_insp<OInsp>(self, insp: OInsp) -> TrevmBuilder<Db, OInsp> {
42+
TrevmBuilder { db: self.db, insp, spec: self.spec }
43+
}
44+
45+
/// Set the spec id for the EVM.
46+
pub const fn with_spec_id(mut self, spec: SpecId) -> Self {
47+
self.spec = spec;
48+
self
49+
}
50+
51+
/// Build the Trevm instance.
52+
pub fn build_trevm(self) -> Result<EvmNeedsCfg<Db, Insp>, TrevmBuilderError>
53+
where
54+
Db: Database,
55+
{
56+
let db = self.db.ok_or(TrevmBuilderError::DatabaseNotSet)?;
57+
let ctx = Ctx::new(db, self.spec);
58+
let evm = ctx.build_mainnet_with_inspector(self.insp);
59+
Ok(Trevm::from(evm))
60+
}
61+
}

src/connect.rs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
};
44
use core::convert::Infallible;
55
use revm::{
6-
primitives::{EVMError, ResultAndState},
6+
context::result::{EVMError, ResultAndState},
77
Database,
88
};
99
use std::format;
@@ -18,18 +18,18 @@ use std::format;
1818
/// connector. E.g. the connector may contain some `Db` and the resulting Db may
1919
/// contain `&Db`. This allows for (e.g.) shared caches between DBs on multiple
2020
/// threads.
21-
pub trait DbConnect<'a>: Sync {
21+
pub trait DbConnect: Sync {
2222
/// The database type returned when connecting.
2323
type Database: Database;
2424

2525
/// The error type returned when connecting to the database.
2626
type Error: core::error::Error;
2727

2828
/// Connect to the database.
29-
fn connect(&'a self) -> Result<Self::Database, Self::Error>;
29+
fn connect(&self) -> Result<Self::Database, Self::Error>;
3030
}
3131

32-
impl<Db> DbConnect<'_> for Db
32+
impl<Db> DbConnect for Db
3333
where
3434
Db: Database + Clone + Sync,
3535
{
@@ -44,28 +44,28 @@ where
4444

4545
/// Trait for types that can create EVM instances.
4646
///
47-
/// Factories should contain configuration information like chain `EXT` types,
48-
/// and database connections. They are intended to enable parallel instantiation
47+
/// Factories should contain configuration information like `Insp` types, and
48+
/// database connections. They are intended to enable parallel instantiation
4949
/// of multiple EVMs in multiple threads sharing some configuration or backing
5050
/// store.
5151
///
5252
/// The lifetime on this trait allows the resulting EVM to borrow from the
5353
/// connector. E.g. the connector may contain some `Db` and the resulting EVM
5454
/// may contain `&Db`. This allows for (e.g.) shared caches between EVMs on
5555
/// multiple threads.
56-
pub trait EvmFactory<'a>: DbConnect<'a> {
57-
/// The `Ext` type used in the resulting EVM.
58-
type Ext: Sync;
56+
pub trait EvmFactory: DbConnect {
57+
/// The `Insp` type used in the resulting EVM.
58+
type Insp: Sync;
5959

6060
/// Create a new EVM instance with the given database connection and
6161
/// extension.
62-
fn create(&'a self) -> Result<EvmNeedsCfg<'a, Self::Ext, Self::Database>, Self::Error>;
62+
fn create(&self) -> Result<EvmNeedsCfg<Self::Database, Self::Insp>, Self::Error>;
6363

6464
/// Create a new EVM instance and parameterize it with a [`Cfg`].
6565
fn create_with_cfg<C>(
66-
&'a self,
66+
&self,
6767
cfg: &C,
68-
) -> Result<EvmNeedsBlock<'a, Self::Ext, Self::Database>, Self::Error>
68+
) -> Result<EvmNeedsBlock<Self::Database, Self::Insp>, Self::Error>
6969
where
7070
C: Cfg,
7171
{
@@ -75,10 +75,10 @@ pub trait EvmFactory<'a>: DbConnect<'a> {
7575
/// Create a new EVM instance and parameterize it with a [`Cfg`] and a
7676
/// [`Block`].
7777
fn create_with_block<C, B>(
78-
&'a self,
78+
&self,
7979
cfg: &C,
8080
block: &B,
81-
) -> Result<EvmNeedsTx<'a, Self::Ext, Self::Database>, Self::Error>
81+
) -> Result<EvmNeedsTx<Self::Database, Self::Insp>, Self::Error>
8282
where
8383
C: Cfg,
8484
B: Block,
@@ -89,11 +89,11 @@ pub trait EvmFactory<'a>: DbConnect<'a> {
8989
/// Create a new EVM instance, and parameterize it with a [`Cfg`], a
9090
/// [`Block`], and a [`Tx`], yielding an [`EvmReady`].
9191
fn create_with_tx<C, B, T>(
92-
&'a self,
92+
&self,
9393
cfg: &C,
9494
block: &B,
9595
tx: &T,
96-
) -> Result<EvmReady<'a, Self::Ext, Self::Database>, Self::Error>
96+
) -> Result<EvmReady<Self::Database, Self::Insp>, Self::Error>
9797
where
9898
C: Cfg,
9999
B: Block,
@@ -107,15 +107,12 @@ pub trait EvmFactory<'a>: DbConnect<'a> {
107107
/// [`EvmTransacted`] or [`EvmErrored`].
108108
#[allow(clippy::type_complexity)]
109109
fn transact<C, B, T>(
110-
&'a self,
110+
&self,
111111
cfg: &C,
112112
block: &B,
113113
tx: &T,
114114
) -> Result<
115-
Result<
116-
EvmTransacted<'a, Self::Ext, Self::Database>,
117-
EvmErrored<'a, Self::Ext, Self::Database>,
118-
>,
115+
Result<EvmTransacted<Self::Database, Self::Insp>, EvmErrored<Self::Database, Self::Insp>>,
119116
Self::Error,
120117
>
121118
where
@@ -130,7 +127,7 @@ pub trait EvmFactory<'a>: DbConnect<'a> {
130127
/// Run a transaction, take the [`ResultAndState`], and discard the Evm.
131128
/// This is a high-level shortcut function.
132129
fn run<C, B, T>(
133-
&'a self,
130+
&self,
134131
cfg: &C,
135132
block: &B,
136133
tx: &T,

src/db/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// Concurrent version of [`revm::db::State`]
1+
/// Concurrent version of [`revm::database::State`]
22
#[cfg(feature = "concurrent-db")]
33
pub mod sync;
44

0 commit comments

Comments
 (0)