Skip to content
Open
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
37 changes: 19 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ url = "2.5.4"
[dev-dependencies]
alloy-hardforks = "0.4.0"
alloy-chains = "0.2"
signet-bundle = "0.16.0-rc.11"

# comment / uncomment for local dev
# [patch.crates-io]
Expand Down
3 changes: 2 additions & 1 deletion src/tasks/cache/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ impl CacheTask {
basefee = sim_env.basefee;
info!(
basefee,
block_env_number = sim_env.number.to::<u64>(), block_env_timestamp = sim_env.timestamp.to::<u64>(),
block_env_number = sim_env.number.to::<u64>(),
block_env_timestamp = sim_env.timestamp.to::<u64>(),
"rollup block env changed, clearing cache"
);
cache.clean(
Expand Down
20 changes: 9 additions & 11 deletions src/test_utils/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,13 @@ impl TestBlockBuildBuilder {
/// This creates a `BlockBuild` ready for simulation.
/// Call `.build().await` on the result to execute the simulation and get a `BuiltBlock`.
pub fn build(self) -> TestBlockBuild {
let sim_env_builder = self.sim_env_builder.unwrap_or_default();

let (rollup_env, host_env, ru_source, host_source) = match (self.rollup_env, self.host_env)
{
(Some(rollup), Some(host)) => {
let (ru_source, host_source) = sim_env_builder.build_state_sources();
(rollup, host, ru_source, host_source)
}
_ => sim_env_builder.build_with_sources(),
let builder = self.sim_env_builder.unwrap_or_default();
let ru_state_source = TestStateSource::new(builder.rollup_db());
let host_state_source = TestStateSource::new(builder.host_db());

let (rollup_env, host_env) = match (self.rollup_env, self.host_env) {
(Some(rollup), Some(host)) => (rollup, host),
_ => builder.build(),
};

let finish_by = Instant::now() + self.deadline_duration;
Expand All @@ -132,8 +130,8 @@ impl TestBlockBuildBuilder {
self.sim_cache,
self.max_gas,
self.max_host_gas,
ru_source,
host_source,
ru_state_source,
host_state_source,
)
}
}
Expand Down
30 changes: 15 additions & 15 deletions src/test_utils/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
//! for testing block simulation without requiring network access.

use alloy::primitives::{Address, B256, U256};
use signet_sim::AcctInfo;
use signet_sim::{AcctInfo, StateSource};
use trevm::revm::{
database::{CacheDB, EmptyDB},
database_interface::DatabaseRef,
primitives::KECCAK_EMPTY,
state::AccountInfo,
};

Expand All @@ -17,32 +16,33 @@ use trevm::revm::{
/// with `RollupEnv` and `HostEnv` for offline simulation testing.
pub type TestDb = CacheDB<EmptyDB>;

/// A [`StateSource`] backed by a [`TestDb`] for offline testing.
///
/// This wraps an in-memory database and implements [`signet_sim::StateSource`]
/// so it can be used as the async state source parameter in [`BlockBuild::new`].
///
/// [`StateSource`]: signet_sim::StateSource
/// [`BlockBuild::new`]: signet_sim::BlockBuild::new
/// A [`StateSource`] for testing backed by an in-memory [`TestDb`].
/// Returns actual account info (nonce, balance) from the database,
/// which is required for preflight validity checks during simulation.
#[derive(Debug, Clone)]
pub struct TestStateSource {
db: TestDb,
}

impl TestStateSource {
/// Create a new [`TestStateSource`] from a [`TestDb`].
/// Create a new `TestStateSource` backed by the given database.
pub const fn new(db: TestDb) -> Self {
Self { db }
}
}

impl signet_sim::StateSource for TestStateSource {
type Error = <TestDb as DatabaseRef>::Error;
impl StateSource for TestStateSource {
type Error = std::convert::Infallible;

async fn account_details(&self, address: &Address) -> Result<AcctInfo, Self::Error> {
let info = self.db.basic_ref(*address)?.unwrap_or_default();
let has_code = info.code_hash() != KECCAK_EMPTY;
Ok(AcctInfo { nonce: info.nonce, balance: info.balance, has_code })
match self.db.basic_ref(*address) {
Ok(Some(info)) => Ok(AcctInfo {
nonce: info.nonce,
balance: info.balance,
has_code: info.code_hash != trevm::revm::primitives::KECCAK_EMPTY,
}),
_ => Ok(AcctInfo { nonce: 0, balance: U256::ZERO, has_code: false }),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to future-proof in case we change the error type:

Suggested change
_ => Ok(AcctInfo { nonce: 0, balance: U256::ZERO, has_code: false }),
Ok(None) => Ok(AcctInfo { nonce: 0, balance: U256::ZERO, has_code: false }),
Err(_) => unreachable!("error type is infallible"),

}
}
}

Expand Down
23 changes: 9 additions & 14 deletions src/test_utils/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! This module provides builders for creating `RollupEnv` and `HostEnv`
//! instances with in-memory databases for offline testing.

use super::db::{TestDb, TestDbBuilder, TestStateSource};
use super::db::{TestDb, TestDbBuilder};
use crate::tasks::block::cfg::SignetCfgEnv;
use alloy::primitives::{Address, B256, U256};
use signet_constants::SignetSystemConstants;
Expand Down Expand Up @@ -117,9 +117,14 @@ impl TestSimEnvBuilder {
HostEnv::new(self.host_db.clone(), self.constants.clone(), &cfg, &self.host_block_env)
}

/// Build [`TestStateSource`] instances from the current databases.
pub fn build_state_sources(&self) -> (TestStateSource, TestStateSource) {
(TestStateSource::new(self.rollup_db.clone()), TestStateSource::new(self.host_db.clone()))
/// Get a clone of the rollup database.
pub fn rollup_db(&self) -> TestDb {
self.rollup_db.clone()
}

/// Get a clone of the host database.
pub fn host_db(&self) -> TestDb {
self.host_db.clone()
}

/// Build both environments as a tuple.
Expand All @@ -128,16 +133,6 @@ impl TestSimEnvBuilder {
let host = self.build_host_env();
(rollup, host)
}

/// Build environments and state sources together.
pub fn build_with_sources(
&self,
) -> (TestRollupEnv, TestHostEnv, TestStateSource, TestStateSource) {
let rollup = self.build_rollup_env();
let host = self.build_host_env();
let (ru_source, host_source) = self.build_state_sources();
(rollup, host, ru_source, host_source)
}
}

#[cfg(test)]
Expand Down
Loading