@@ -18,9 +18,11 @@ use aws_sdk_kms::config::BehaviorVersion;
1818use reth_chainspec:: EthChainSpec ;
1919use reth_network:: NetworkProtocols ;
2020use reth_network_api:: FullNetwork ;
21- use reth_node_builder:: rpc:: RethRpcServerHandles ;
21+ use reth_node_builder:: { rpc:: RethRpcServerHandles , NodeConfig as RethNodeConfig } ;
2222use reth_node_core:: primitives:: BlockHeader ;
23- use reth_scroll_chainspec:: { ChainConfig , ScrollChainConfig , SCROLL_FEE_VAULT_ADDRESS } ;
23+ use reth_scroll_chainspec:: {
24+ ChainConfig , ScrollChainConfig , ScrollChainSpec , SCROLL_FEE_VAULT_ADDRESS ,
25+ } ;
2426use reth_scroll_node:: ScrollNetworkPrimitives ;
2527use rollup_node_chain_orchestrator:: ChainOrchestrator ;
2628use rollup_node_manager:: {
@@ -79,6 +81,9 @@ pub struct ScrollRollupNodeConfig {
7981 /// The gas price oracle args
8082 #[ command( flatten) ]
8183 pub gas_price_oracle_args : GasPriceOracleArgs ,
84+ /// The database connection (not parsed via CLI but hydrated after validation).
85+ #[ arg( skip) ]
86+ pub database : Option < Arc < Database > > ,
8287}
8388
8489impl ScrollRollupNodeConfig {
@@ -112,6 +117,26 @@ impl ScrollRollupNodeConfig {
112117
113118 Ok ( ( ) )
114119 }
120+
121+ /// Hydrate the config by initializing the database connection.
122+ pub async fn hydrate (
123+ & mut self ,
124+ node_config : RethNodeConfig < ScrollChainSpec > ,
125+ ) -> eyre:: Result < ( ) > {
126+ // Instantiate the database
127+ let db_path = node_config. datadir ( ) . db ( ) ;
128+ let database_path = if let Some ( database_path) = & self . database_args . path {
129+ database_path. to_string_lossy ( ) . to_string ( )
130+ } else {
131+ // append the path using strings as using `join(...)` overwrites "sqlite://"
132+ // if the path is absolute.
133+ let path = db_path. join ( "scroll.db?mode=rwc" ) ;
134+ "sqlite://" . to_string ( ) + & * path. to_string_lossy ( )
135+ } ;
136+ let db = Database :: new ( & database_path) . await ?;
137+ self . database = Some ( Arc :: new ( db) ) ;
138+ Ok ( ( ) )
139+ }
115140}
116141
117142impl ScrollRollupNodeConfig {
@@ -175,17 +200,8 @@ impl ScrollRollupNodeConfig {
175200 . expect ( "failed to create payload provider" ) ;
176201 let l2_provider = Arc :: new ( l2_provider) ;
177202
178- // Instantiate the database
179- let db_path = ctx. datadir ;
180- let database_path = if let Some ( database_path) = self . database_args . path {
181- database_path. to_string_lossy ( ) . to_string ( )
182- } else {
183- // append the path using strings as using `join(...)` overwrites "sqlite://"
184- // if the path is absolute.
185- let path = db_path. join ( "scroll.db?mode=rwc" ) ;
186- "sqlite://" . to_string ( ) + & * path. to_string_lossy ( )
187- } ;
188- let db = Database :: new ( & database_path) . await ?;
203+ // Fetch the database from the hydrated config.
204+ let db = self . database . clone ( ) . expect ( "should hydrate config before build" ) ;
189205
190206 // Run the database migrations
191207 if let Some ( named) = chain_spec. chain ( ) . named ( ) {
@@ -215,9 +231,6 @@ impl ScrollRollupNodeConfig {
215231 tracing:: info!( target: "scroll::node::args" , ?genesis_hash, "Overwriting genesis hash for custom chain" ) ;
216232 }
217233
218- // Wrap the database in an Arc
219- let db = Arc :: new ( db) ;
220-
221234 let chain_spec_fcs = || {
222235 ForkchoiceState :: head_from_chain_spec ( chain_spec. clone ( ) )
223236 . expect ( "failed to derive forkchoice state from chain spec" )
@@ -787,6 +800,7 @@ mod tests {
787800 algorithm : ConsensusAlgorithm :: SystemContract ,
788801 authorized_signer : None ,
789802 } ,
803+ database : None ,
790804 } ;
791805
792806 let result = config. validate ( ) ;
@@ -817,6 +831,7 @@ mod tests {
817831 algorithm : ConsensusAlgorithm :: SystemContract ,
818832 authorized_signer : None ,
819833 } ,
834+ database : None ,
820835 } ;
821836
822837 let result = config. validate ( ) ;
@@ -842,6 +857,7 @@ mod tests {
842857 network_args : NetworkArgs :: default ( ) ,
843858 gas_price_oracle_args : GasPriceOracleArgs :: default ( ) ,
844859 consensus_args : ConsensusArgs :: noop ( ) ,
860+ database : None ,
845861 } ;
846862
847863 assert ! ( config. validate( ) . is_ok( ) ) ;
@@ -865,6 +881,7 @@ mod tests {
865881 network_args : NetworkArgs :: default ( ) ,
866882 gas_price_oracle_args : GasPriceOracleArgs :: default ( ) ,
867883 consensus_args : ConsensusArgs :: noop ( ) ,
884+ database : None ,
868885 } ;
869886
870887 assert ! ( config. validate( ) . is_ok( ) ) ;
@@ -884,6 +901,7 @@ mod tests {
884901 network_args : NetworkArgs :: default ( ) ,
885902 gas_price_oracle_args : GasPriceOracleArgs :: default ( ) ,
886903 consensus_args : ConsensusArgs :: noop ( ) ,
904+ database : None ,
887905 } ;
888906
889907 assert ! ( config. validate( ) . is_ok( ) ) ;
0 commit comments