Skip to content

Commit 24742ef

Browse files
committed
wip: figuring out database trait bounds
1 parent b85d409 commit 24742ef

File tree

3 files changed

+51
-32
lines changed

3 files changed

+51
-32
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ signet-bundle = { git = "https://github.com/init4tech/signet-sdk", branch = "mai
2828
signet-types = { git = "https://github.com/init4tech/signet-sdk", branch = "main" }
2929
signet-zenith = { git = "https://github.com/init4tech/signet-sdk", branch = "main" }
3030

31-
trevm = { version = "0.20.4", features = [ "concurrent-db" ]}
31+
trevm = { version = "0.20.4", features = [ "concurrent-db", "test-utils" ]}
3232

3333
alloy = { version = "0.12.6", features = [
3434
"full",

src/tasks/simulator.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ use eyre::Result;
55
use std::sync::Arc;
66
use tokio::{select, sync::mpsc::UnboundedReceiver, task::JoinSet};
77
use trevm::{
8-
db::sync::{ConcurrentState, ConcurrentStateInfo}, helpers::Ctx, revm::{
8+
db::{cow::CacheOnWrite, sync::{ConcurrentState, ConcurrentStateInfo}}, helpers::Ctx, revm::{
99
context::{
1010
result::{EVMError, ExecutionResult, ResultAndState}, CfgEnv
11-
}, inspector::inspectors::GasInspector, primitives::address, state::Account, Database, DatabaseCommit, DatabaseRef, Inspector
12-
}, BlockDriver, Cfg, DbConnect, EvmFactory, NoopBlock, Trevm, TrevmBuilder, TrevmBuilderError, Tx
11+
}, primitives::address, state::Account, Database, DatabaseCommit, DatabaseRef, Inspector
12+
}, BlockDriver, Cfg, DbConnect, EvmFactory, NoopBlock, TrevmBuilder, TrevmBuilderError, Tx
1313
};
1414

1515
/// Tracks the EVM state, score, and result of an EVM execution.
@@ -25,7 +25,7 @@ pub struct Best<T, S: PartialOrd + Ord = U256> {
2525
pub score: S,
2626
}
2727

28-
/// Binds a database and an extension together.
28+
/// Binds a database and an inspector together for simulation.
2929
#[derive(Debug, Clone)]
3030
pub struct SimulatorFactory<Db, Insp> {
3131
/// The database state the execution is carried out on.
@@ -40,10 +40,15 @@ type SimResult<Db> = Result<Option<(Best<TxEnvelope>, ConcurrentState<Arc<Concur
4040

4141
impl<Db, Insp> SimulatorFactory<Db, Insp>
4242
where
43-
Insp: Inspector<Ctx<ConcurrentState<Db>>> + Send + Sync + Clone + 'static,
43+
Insp: Inspector<Ctx<ConcurrentState<Db>>>
44+
+ Inspector<Ctx<ConcurrentState<Arc<ConcurrentState<Db>>>>>
45+
+ Send
46+
+ Sync
47+
+ Clone
48+
+ 'static,
4449
Db: Database + DatabaseRef + DatabaseCommit + Send + Sync + Clone + 'static,
4550
{
46-
/// Creates a new Simulator factory out of the database and extension.
51+
/// Creates a new Simulator factory from the provided database and inspector.
4752
pub const fn new(db: Db, inspector: Insp) -> Self {
4853
Self { db, inspector }
4954
}

tests/simulator_test.rs

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
1-
use alloy::consensus::{SignableTransaction as _, TxEip1559, TxEnvelope};
2-
use alloy::network::Ethereum;
3-
use alloy::primitives::U256;
4-
use alloy::signers::local::PrivateKeySigner;
5-
use alloy::signers::SignerSync as _;
6-
use alloy::transports::http::{Client, Http};
7-
use builder::config::WalletlessProvider;
8-
use builder::tasks::simulator::SimulatorFactory;
9-
use trevm::revm::database::{AlloyDB, CacheDB};
10-
use trevm::revm::primitives::{address, TxKind};
1+
use alloy::{
2+
consensus::{SignableTransaction as _, TxEip1559, TxEnvelope},
3+
eips::BlockId,
4+
network::Ethereum,
5+
primitives::U256,
6+
providers::{Provider, ProviderBuilder},
7+
signers::SignerSync as _,
8+
signers::local::PrivateKeySigner,
9+
};
10+
use builder::{config::WalletlessProvider, tasks::simulator::SimulatorFactory};
1111
use std::sync::Arc;
12-
use tokio::sync::mpsc;
13-
use tokio::time::{Duration, Instant};
14-
use trevm::revm::primitives::{Account, ExecutionResult, ResultAndState};
15-
use alloy::eips::BlockId;
16-
use alloy::providers::{Provider, ProviderBuilder};
12+
use tokio::{
13+
sync::mpsc,
14+
time::{Duration, Instant},
15+
};
16+
use trevm::{
17+
db::{
18+
cow::CacheOnWrite,
19+
sync::{ConcurrentState, ConcurrentStateInfo},
20+
},
21+
revm::{
22+
context::result::{ExecutionResult, ResultAndState},
23+
database::{CacheDB, Database, DatabaseCommit, DatabaseRef, AlloyDB, WrapDatabaseAsync},
24+
primitives::{TxKind, address},
25+
inspector::NoOpInspector,
26+
state::Account,
27+
},
28+
};
29+
30+
// Define a type alias for the database used with SimulatorFactory.
31+
type Db = WrapDatabaseAsync<AlloyDB<Ethereum, WalletlessProvider>>;
1732

1833
#[tokio::test(flavor = "multi_thread")]
1934
async fn test_spawn() {
@@ -24,7 +39,7 @@ async fn test_spawn() {
2439

2540
// Create a new anvil instance
2641
let anvil =
27-
alloy::node_bindings::Anvil::new().block_time(1).chain_id(17003).try_spawn().unwrap();
42+
alloy::node_bindings::Anvil::new().block_time(1).chain_id(14174).try_spawn().unwrap();
2843

2944
// Create a test wallet from the anvil keys
3045
let keys = anvil.keys();
@@ -35,20 +50,19 @@ async fn test_spawn() {
3550
let latest = root_provider.get_block_number().await.unwrap();
3651

3752
// Create an alloyDB from the provider at the latest height
38-
let alloy_db: AlloyDB<Http<Client>, Ethereum, Arc<WalletlessProvider>> =
39-
AlloyDB::new(Arc::new(root_provider.clone()), BlockId::Number(latest.into())).unwrap();
40-
let db = CacheDB::new(Arc::new(alloy_db));
41-
42-
// Define trevm extension, if any
43-
let ext = ();
53+
let alloy_db: AlloyDB<Ethereum, WalletlessProvider> =
54+
AlloyDB::new(root_provider.clone(), BlockId::from(latest));
4455

56+
let wrapped_db = WrapDatabaseAsync::new(alloy_db).unwrap();
57+
let concurrent_db = ConcurrentState::new(wrapped_db, ConcurrentStateInfo::default());
58+
4559
// Define the evaluator function
4660
let evaluator = Arc::new(test_evaluator);
4761

4862
// Create a simulation factory with the provided DB
49-
let sim_factory = SimulatorFactory::new(db, ext);
50-
let handle =
51-
sim_factory.spawn::<TxEnvelope, _>(tx_receiver, evaluator, deadline);
63+
let sim_factory = SimulatorFactory::new(concurrent_db, NoOpInspector);
64+
65+
let handle = sim_factory.spawn(tx_receiver, evaluator, deadline);
5266

5367
// Send some transactions
5468
for count in 0..2 {

0 commit comments

Comments
 (0)