-
Notifications
You must be signed in to change notification settings - Fork 2
tests: adds builder simulation test harness #219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dylanlott
wants to merge
8
commits into
main
Choose a base branch
from
dylan/sim-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
48bb9f3
tests: adds builder simulation test harness
dylanlott 62238f3
fmt
dylanlott 2cd365b
chore: update claude workflows to run fmt and clippy
dylanlott 0914ba3
fmt and clippy
dylanlott 3afbf74
fix: add state source type support
dylanlott 1f94311
refactor: pull integration tests back into tests/ and gate test_utils…
dylanlott 24b5361
docs: update docs to match testing changes
dylanlott 4f39f2d
fmt + clippy recrusion limits fix
dylanlott File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| //! Test utilities for block building and simulation. | ||
| //! This module provides builders for creating `BlockBuild` instances | ||
| //! for testing block simulation. | ||
|
|
||
| use super::{ | ||
| db::{TestDb, TestStateSource}, | ||
| env::{TestHostEnv, TestRollupEnv, TestSimEnvBuilder}, | ||
| }; | ||
| use signet_sim::{BlockBuild, BuiltBlock, SimCache}; | ||
| use std::time::Duration; | ||
| use tokio::time::Instant; | ||
| use trevm::revm::inspector::NoOpInspector; | ||
|
|
||
| /// Test block builder type using in-memory databases. | ||
| pub type TestBlockBuild = | ||
| BlockBuild<TestDb, TestDb, TestStateSource, TestStateSource, NoOpInspector, NoOpInspector>; | ||
|
|
||
| /// Builder for creating test `BlockBuild` instances. | ||
| /// Configures all the parameters needed for block simulation | ||
| /// and provides sensible defaults for testing scenarios. | ||
| #[derive(Debug)] | ||
| pub struct TestBlockBuildBuilder { | ||
| /// The test environment configuration for the block build. | ||
| env: TestBlockBuildEnv, | ||
| /// The simulation cache to use for the block build. | ||
| sim_cache: SimCache, | ||
| /// The duration from now until the block build should finish. | ||
| deadline_duration: Duration, | ||
| /// The concurrency limit for parallel simulation. | ||
| concurrency_limit: usize, | ||
| /// The maximum gas limit for the rollup block. | ||
| max_gas: u64, | ||
| /// The maximum gas limit for host transactions. | ||
| max_host_gas: u64, | ||
| } | ||
|
|
||
| /// Internal enum to manage the environment configuration for the block build. | ||
| #[derive(Debug)] | ||
| #[allow(clippy::large_enum_variant)] | ||
| enum TestBlockBuildEnv { | ||
| /// A builder that will create the environments when `into_block_build()` is called. | ||
| Builder(TestSimEnvBuilder), | ||
| /// A pair of already built environments to use directly. | ||
| Built { rollup: TestRollupEnv, host: TestHostEnv }, | ||
| } | ||
|
|
||
| impl Default for TestBlockBuildBuilder { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl TestBlockBuildBuilder { | ||
| /// Create a new test block build builder with test-focused defaults. | ||
| /// Default values: | ||
| /// - Deadline: 2 seconds | ||
| /// - Concurrency limit: 4 | ||
| /// - Max gas: 3,000,000,000 (3 billion) | ||
| /// - Max host gas: 24,000,000 | ||
| pub fn new() -> Self { | ||
| Self { | ||
| // Default to building fresh test environments unless the caller injects a pair. | ||
| env: TestBlockBuildEnv::Builder(TestSimEnvBuilder::new()), | ||
| sim_cache: SimCache::new(), | ||
| deadline_duration: Duration::from_secs(2), | ||
| concurrency_limit: 4, | ||
| max_gas: 3_000_000_000, | ||
| max_host_gas: 24_000_000, | ||
| } | ||
| } | ||
|
|
||
| /// Set the simulation environment builder. | ||
| /// The environments will be built from this builder when `into_block_build()` is called. | ||
| pub fn with_sim_env_builder(mut self, builder: TestSimEnvBuilder) -> Self { | ||
| self.env = TestBlockBuildEnv::Builder(builder); | ||
| self | ||
| } | ||
|
|
||
| /// Set both environments directly so the block build uses a consistent pair. | ||
| pub fn with_envs(mut self, rollup: TestRollupEnv, host: TestHostEnv) -> Self { | ||
| self.env = TestBlockBuildEnv::Built { rollup, host }; | ||
| self | ||
| } | ||
|
|
||
| /// Set the simulation cache. | ||
| pub fn with_cache(mut self, cache: SimCache) -> Self { | ||
| self.sim_cache = cache; | ||
| self | ||
| } | ||
|
|
||
| /// Set the deadline duration from now. | ||
| pub const fn with_deadline(mut self, duration: Duration) -> Self { | ||
| self.deadline_duration = duration; | ||
| self | ||
| } | ||
|
|
||
| /// Set the concurrency limit for parallel simulation. | ||
| pub const fn with_concurrency(mut self, limit: usize) -> Self { | ||
| self.concurrency_limit = limit; | ||
| self | ||
| } | ||
|
|
||
| /// Set the maximum gas limit for the rollup block. | ||
| pub const fn with_max_gas(mut self, gas: u64) -> Self { | ||
| self.max_gas = gas; | ||
| self | ||
| } | ||
|
|
||
| /// Set the maximum gas limit for host transactions. | ||
| pub const fn with_max_host_gas(mut self, gas: u64) -> Self { | ||
| self.max_host_gas = gas; | ||
| self | ||
| } | ||
|
|
||
| /// Build the test `BlockBuild` instance. | ||
| /// This creates a `BlockBuild` ready for simulation. | ||
| /// Call `.build().await` on the result to execute the simulation and get a `BuiltBlock`. | ||
| pub async fn into_block_build(self) -> BuiltBlock { | ||
| // Keep the async state sources aligned with whichever environment pair we use. | ||
| let (rollup_env, host_env, ru_source, host_source) = match self.env { | ||
| TestBlockBuildEnv::Builder(sim_env_builder) => sim_env_builder.build_with_sources(), | ||
| TestBlockBuildEnv::Built { rollup, host } => { | ||
| let ru_source = TestStateSource::from_inner_db(rollup.db().clone()); | ||
| let host_source = TestStateSource::from_inner_db(host.db().clone()); | ||
| (rollup, host, ru_source, host_source) | ||
| } | ||
| }; | ||
|
|
||
| // Convert the relative deadline into the absolute instant expected by `BlockBuild`. | ||
| let finish_by = Instant::now() + self.deadline_duration; | ||
|
|
||
| BlockBuild::new( | ||
| rollup_env, | ||
| host_env, | ||
| finish_by, | ||
| self.concurrency_limit, | ||
| self.sim_cache, | ||
| self.max_gas, | ||
| self.max_host_gas, | ||
| ru_source, | ||
| host_source, | ||
| ) | ||
| .build() | ||
| .await | ||
| } | ||
| } | ||
|
|
||
| /// Convenience function to quickly build a block with a cache and optional configuration. | ||
| /// This is useful for simple test cases where you just want to simulate | ||
| /// some transactions quickly. | ||
| pub async fn quick_build_block(cache: SimCache, deadline: Duration) -> BuiltBlock { | ||
| TestBlockBuildBuilder::new().with_cache(cache).with_deadline(deadline).into_block_build().await | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_block_build_builder_defaults() { | ||
| let builder = TestBlockBuildBuilder::new(); | ||
| assert_eq!(builder.deadline_duration, Duration::from_secs(2)); | ||
| assert_eq!(builder.concurrency_limit, 4); | ||
| assert_eq!(builder.max_gas, 3_000_000_000); | ||
| assert_eq!(builder.max_host_gas, 24_000_000); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_block_build_builder_custom_values() { | ||
| let builder = TestBlockBuildBuilder::new() | ||
| .with_deadline(Duration::from_secs(5)) | ||
| .with_concurrency(8) | ||
| .with_max_gas(1_000_000_000) | ||
| .with_max_host_gas(10_000_000); | ||
|
|
||
| assert_eq!(builder.deadline_duration, Duration::from_secs(5)); | ||
| assert_eq!(builder.concurrency_limit, 8); | ||
| assert_eq!(builder.max_gas, 1_000_000_000); | ||
| assert_eq!(builder.max_host_gas, 10_000_000); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.