Skip to content

Commit 070c4f7

Browse files
committed
fix: add state source type support
1 parent 0788c93 commit 070c4f7

4 files changed

Lines changed: 62 additions & 9 deletions

File tree

src/test_utils/block.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! for testing block simulation.
44
55
use super::{
6-
db::TestDb,
6+
db::{TestDb, TestStateSource},
77
env::{TestHostEnv, TestRollupEnv, TestSimEnvBuilder},
88
};
99
use signet_sim::{BlockBuild, BuiltBlock, SimCache};
@@ -12,7 +12,8 @@ use tokio::time::Instant;
1212
use trevm::revm::inspector::NoOpInspector;
1313

1414
/// Test block builder type using in-memory databases.
15-
pub type TestBlockBuild = BlockBuild<TestDb, TestDb, NoOpInspector, NoOpInspector>;
15+
pub type TestBlockBuild =
16+
BlockBuild<TestDb, TestDb, TestStateSource, TestStateSource, NoOpInspector, NoOpInspector>;
1617

1718
/// Builder for creating test `BlockBuild` instances.
1819
/// Configures all the parameters needed for block simulation
@@ -110,12 +111,15 @@ impl TestBlockBuildBuilder {
110111
/// This creates a `BlockBuild` ready for simulation.
111112
/// Call `.build().await` on the result to execute the simulation and get a `BuiltBlock`.
112113
pub fn build(self) -> TestBlockBuild {
113-
let (rollup_env, host_env) = match (self.rollup_env, self.host_env) {
114-
(Some(rollup), Some(host)) => (rollup, host),
115-
_ => {
116-
let builder = self.sim_env_builder.unwrap_or_default();
117-
builder.build()
114+
let sim_env_builder = self.sim_env_builder.unwrap_or_default();
115+
116+
let (rollup_env, host_env, ru_source, host_source) = match (self.rollup_env, self.host_env)
117+
{
118+
(Some(rollup), Some(host)) => {
119+
let (ru_source, host_source) = sim_env_builder.build_state_sources();
120+
(rollup, host, ru_source, host_source)
118121
}
122+
_ => sim_env_builder.build_with_sources(),
119123
};
120124

121125
let finish_by = Instant::now() + self.deadline_duration;
@@ -128,6 +132,8 @@ impl TestBlockBuildBuilder {
128132
self.sim_cache,
129133
self.max_gas,
130134
self.max_host_gas,
135+
ru_source,
136+
host_source,
131137
)
132138
}
133139
}

src/test_utils/db.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
//! for testing block simulation without requiring network access.
44
55
use alloy::primitives::{Address, B256, U256};
6+
use signet_sim::AcctInfo;
67
use trevm::revm::{
78
database::{CacheDB, EmptyDB},
9+
database_interface::DatabaseRef,
10+
primitives::KECCAK_EMPTY,
811
state::AccountInfo,
912
};
1013

@@ -14,6 +17,35 @@ use trevm::revm::{
1417
/// with `RollupEnv` and `HostEnv` for offline simulation testing.
1518
pub type TestDb = CacheDB<EmptyDB>;
1619

20+
/// A [`StateSource`] backed by a [`TestDb`] for offline testing.
21+
///
22+
/// This wraps an in-memory database and implements [`signet_sim::StateSource`]
23+
/// so it can be used as the async state source parameter in [`BlockBuild::new`].
24+
///
25+
/// [`StateSource`]: signet_sim::StateSource
26+
/// [`BlockBuild::new`]: signet_sim::BlockBuild::new
27+
#[derive(Debug, Clone)]
28+
pub struct TestStateSource {
29+
db: TestDb,
30+
}
31+
32+
impl TestStateSource {
33+
/// Create a new [`TestStateSource`] from a [`TestDb`].
34+
pub const fn new(db: TestDb) -> Self {
35+
Self { db }
36+
}
37+
}
38+
39+
impl signet_sim::StateSource for TestStateSource {
40+
type Error = <TestDb as DatabaseRef>::Error;
41+
42+
async fn account_details(&self, address: &Address) -> Result<AcctInfo, Self::Error> {
43+
let info = self.db.basic_ref(*address)?.unwrap_or_default();
44+
let has_code = info.code_hash() != KECCAK_EMPTY;
45+
Ok(AcctInfo { nonce: info.nonce, balance: info.balance, has_code })
46+
}
47+
}
48+
1749
/// Builder for creating pre-populated test databases.
1850
/// Use this builder to set up blockchain state (accounts, contracts, storage)
1951
/// before running simulations.

src/test_utils/env.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! This module provides builders for creating `RollupEnv` and `HostEnv`
33
//! instances with in-memory databases for offline testing.
44
5-
use super::db::{TestDb, TestDbBuilder};
5+
use super::db::{TestDb, TestDbBuilder, TestStateSource};
66
use crate::tasks::block::cfg::SignetCfgEnv;
77
use alloy::primitives::{Address, B256, U256};
88
use signet_constants::SignetSystemConstants;
@@ -117,12 +117,27 @@ impl TestSimEnvBuilder {
117117
HostEnv::new(self.host_db.clone(), self.constants.clone(), &cfg, &self.host_block_env)
118118
}
119119

120+
/// Build [`TestStateSource`] instances from the current databases.
121+
pub fn build_state_sources(&self) -> (TestStateSource, TestStateSource) {
122+
(TestStateSource::new(self.rollup_db.clone()), TestStateSource::new(self.host_db.clone()))
123+
}
124+
120125
/// Build both environments as a tuple.
121126
pub fn build(self) -> (TestRollupEnv, TestHostEnv) {
122127
let rollup = self.build_rollup_env();
123128
let host = self.build_host_env();
124129
(rollup, host)
125130
}
131+
132+
/// Build environments and state sources together.
133+
pub fn build_with_sources(
134+
&self,
135+
) -> (TestRollupEnv, TestHostEnv, TestStateSource, TestStateSource) {
136+
let rollup = self.build_rollup_env();
137+
let host = self.build_host_env();
138+
let (ru_source, host_source) = self.build_state_sources();
139+
(rollup, host, ru_source, host_source)
140+
}
126141
}
127142

128143
#[cfg(test)]

src/test_utils/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ mod tx;
1111

1212
// Re-export test harness components
1313
pub use block::{TestBlockBuild, TestBlockBuildBuilder, quick_build_block};
14-
pub use db::{TestDb, TestDbBuilder};
14+
pub use db::{TestDb, TestDbBuilder, TestStateSource};
1515
pub use env::{TestHostEnv, TestRollupEnv, TestSimEnvBuilder};
1616
pub use scenarios::{
1717
DEFAULT_BALANCE, DEFAULT_BASEFEE, basic_scenario, custom_funded_scenario, funded_test_db,

0 commit comments

Comments
 (0)