22//! actor that handles the simulation of a stream of bundles and transactions
33//! and turns them into valid Pecorino blocks for network submission.
44use crate :: {
5- config:: { BuilderConfig , RuProvider } ,
5+ config:: { BuilderConfig , HostProvider , RuProvider } ,
66 tasks:: env:: SimEnv ,
77} ;
8- use alloy:: { eips:: BlockId , network:: Ethereum } ;
8+ use alloy:: { eips:: BlockId , network:: Ethereum , primitives :: BlockNumber } ;
99use init4_bin_base:: {
1010 deps:: metrics:: { counter, histogram} ,
1111 utils:: calc:: SlotCalculator ,
1212} ;
13- use signet_sim:: { BlockBuild , BuiltBlock , SimCache } ;
13+ use signet_sim:: { BlockBuild , BuiltBlock , HostEnv , RollupEnv , SimCache } ;
1414use signet_types:: constants:: SignetSystemConstants ;
1515use std:: time:: { Duration , Instant } ;
1616use tokio:: {
@@ -27,7 +27,8 @@ use trevm::revm::{
2727 inspector:: NoOpInspector ,
2828} ;
2929
30- type AlloyDatabaseProvider = WrapDatabaseAsync < AlloyDB < Ethereum , RuProvider > > ;
30+ type HostAlloyDatabaseProvider = WrapDatabaseAsync < AlloyDB < Ethereum , HostProvider > > ;
31+ type RollupAlloyDatabaseProvider = WrapDatabaseAsync < AlloyDB < Ethereum , RuProvider > > ;
3132
3233/// `Simulator` is responsible for periodically building blocks and submitting them for
3334/// signing and inclusion in the blockchain. It wraps a rollup provider and a slot
@@ -36,6 +37,8 @@ type AlloyDatabaseProvider = WrapDatabaseAsync<AlloyDB<Ethereum, RuProvider>>;
3637pub struct Simulator {
3738 /// Configuration for the builder.
3839 pub config : BuilderConfig ,
40+ /// Host Provider to interact with the host chain.
41+ pub host_provider : HostProvider ,
3942 /// A provider that cannot sign transactions, used for interacting with the rollup.
4043 pub ru_provider : RuProvider ,
4144 /// The block configuration environment on which to simulate
@@ -88,10 +91,11 @@ impl Simulator {
8891 /// A new `Simulator` instance.
8992 pub fn new (
9093 config : & BuilderConfig ,
94+ host_provider : HostProvider ,
9195 ru_provider : RuProvider ,
9296 sim_env : watch:: Receiver < Option < SimEnv > > ,
9397 ) -> Self {
94- Self { config : config. clone ( ) , ru_provider, sim_env }
98+ Self { config : config. clone ( ) , host_provider , ru_provider, sim_env }
9599 }
96100
97101 /// Get the slot calculator.
@@ -129,17 +133,33 @@ impl Simulator {
129133 let concurrency_limit = self . config . concurrency_limit ( ) ;
130134
131135 // NB: Build AlloyDB from the previous block number's state, since block_env maps to the in-progress block
132- let db = self . create_db ( block_env. number . to :: < u64 > ( ) - 1 ) . unwrap ( ) ;
136+ let latest_block_number = BlockNumber :: from ( block_env. number . to :: < u64 > ( ) - 1 ) ;
133137
134- let block_build: BlockBuild < _ , NoOpInspector > = BlockBuild :: new (
135- db,
138+ let max_host_gas = 45_000_000u64 ;
139+
140+ let host_db = self . create_host_db ( latest_block_number) . await ;
141+ let host_env = HostEnv :: < _ , NoOpInspector > :: new (
142+ host_db,
143+ constants. clone ( ) ,
144+ & self . config . cfg_env ( ) ,
145+ & block_env,
146+ ) ;
147+ let rollup_db = self . create_rollup_db ( latest_block_number) ;
148+ let rollup_env = RollupEnv :: < _ , NoOpInspector > :: new (
149+ rollup_db,
136150 constants,
137- self . config . cfg_env ( ) ,
138- block_env,
151+ & self . config . cfg_env ( ) ,
152+ & block_env,
153+ ) ;
154+
155+ let block_build = BlockBuild :: new (
156+ rollup_env,
157+ host_env,
139158 finish_by,
140159 concurrency_limit,
141160 sim_items,
142161 self . config . rollup_block_gas_limit ,
162+ max_host_gas,
143163 ) ;
144164
145165 let built_block = block_build. build ( ) . in_current_span ( ) . await ;
@@ -149,7 +169,7 @@ impl Simulator {
149169 "block simulation completed" ,
150170 ) ;
151171 counter ! ( "signet.builder.built_blocks" ) . increment ( 1 ) ;
152- histogram ! ( "signet.builder.built_blocks.tx_count" ) . record ( built_block. tx_count ( ) as f64 ) ;
172+ histogram ! ( "signet.builder.built_blocks.tx_count" ) . record ( built_block. tx_count ( ) as u32 ) ;
153173
154174 Ok ( built_block)
155175 }
@@ -251,12 +271,23 @@ impl Simulator {
251271 deadline. max ( Instant :: now ( ) )
252272 }
253273
274+ /// Creates an `AlloyDB` instnace from the host provider.
275+ async fn create_host_db ( & self , latest_block_number : u64 ) -> HostAlloyDatabaseProvider {
276+ let alloy_db = AlloyDB :: new ( self . host_provider . clone ( ) , BlockId :: from ( latest_block_number) ) ;
277+
278+ // Wrap the AlloyDB instance in a WrapDatabaseAsync and return it.
279+ // This is safe to unwrap because the main function sets the proper runtime settings.
280+ //
281+ // See: https://docs.rs/tokio/latest/tokio/attr.main.html
282+ WrapDatabaseAsync :: new ( alloy_db) . unwrap ( )
283+ }
284+
254285 /// Creates an `AlloyDB` instance from the rollup provider.
255286 ///
256287 /// # Returns
257288 ///
258289 /// An `Option` containing the wrapped database or `None` if an error occurs.
259- fn create_db ( & self , latest_block_number : u64 ) -> Option < AlloyDatabaseProvider > {
290+ fn create_rollup_db ( & self , latest_block_number : u64 ) -> RollupAlloyDatabaseProvider {
260291 // Make an AlloyDB instance from the rollup provider with that latest block number
261292 let alloy_db: AlloyDB < Ethereum , RuProvider > =
262293 AlloyDB :: new ( self . ru_provider . clone ( ) , BlockId :: from ( latest_block_number) ) ;
@@ -265,7 +296,6 @@ impl Simulator {
265296 // This is safe to unwrap because the main function sets the proper runtime settings.
266297 //
267298 // See: https://docs.rs/tokio/latest/tokio/attr.main.html
268- let wrapped_db: AlloyDatabaseProvider = WrapDatabaseAsync :: new ( alloy_db) . unwrap ( ) ;
269- Some ( wrapped_db)
299+ WrapDatabaseAsync :: new ( alloy_db) . unwrap ( )
270300 }
271301}
0 commit comments