@@ -3,26 +3,29 @@ use std::sync::atomic::{AtomicU8, Ordering};
33use std:: sync:: Arc ;
44use std:: time:: Duration ;
55
6+ use apollo_batcher_types:: batcher_types:: GetHeightResponse ;
7+ use apollo_batcher_types:: communication:: SharedBatcherClient ;
68use apollo_l1_provider_types:: SharedL1ProviderClient ;
79use apollo_state_sync_types:: communication:: SharedStateSyncClient ;
810use starknet_api:: block:: BlockNumber ;
911use starknet_api:: transaction:: TransactionHash ;
1012use tokio:: sync:: OnceCell ;
11- use tracing:: { debug, info} ;
13+ use tracing:: { debug, error , info} ;
1214
1315pub type LazyCatchUpHeight = Arc < OnceCell < BlockNumber > > ;
1416
1517/// Cache's commits to be applied later. This flow is only relevant while the node is starting up.
1618#[ derive( Clone ) ]
1719pub struct Bootstrapper {
18- /// The catch-up height for the bootstrapper is the sync height (unless overridden explicitly).
19- /// This value, due to infra constraints as of now, is only fetchable _after_ the provider is
20- /// running, and not during its initialization, hence we are forced to lazily fetch it at
21- /// runtime.
20+ /// The catch-up height for the bootstrapper is the batcher height (unless overridden
21+ /// explicitly). This value, due to infra constraints as of now, is only fetchable _after_
22+ /// the provider is running, and not during its initialization, hence we are forced to
23+ /// lazily fetch it at runtime.
2224 pub catch_up_height : LazyCatchUpHeight ,
2325 pub sync_retry_interval : Duration ,
2426 pub commit_block_backlog : Vec < CommitBlockBacklog > ,
2527 pub l1_provider_client : SharedL1ProviderClient ,
28+ pub batcher_client : SharedBatcherClient ,
2629 pub sync_client : SharedStateSyncClient ,
2730 // Keep track of sync task for health checks and logging status.
2831 pub sync_task_handle : SyncTaskHandle ,
@@ -36,6 +39,7 @@ impl Bootstrapper {
3639
3740 pub fn new (
3841 l1_provider_client : SharedL1ProviderClient ,
42+ batcher_client : SharedBatcherClient ,
3943 sync_client : SharedStateSyncClient ,
4044 sync_retry_interval : Duration ,
4145 catch_up_height : LazyCatchUpHeight ,
@@ -44,6 +48,7 @@ impl Bootstrapper {
4448 sync_retry_interval,
4549 commit_block_backlog : Default :: default ( ) ,
4650 l1_provider_client,
51+ batcher_client,
4752 sync_client,
4853 sync_task_handle : SyncTaskHandle :: NotStartedYet ,
4954 n_sync_health_check_failures : Default :: default ( ) ,
@@ -52,11 +57,12 @@ impl Bootstrapper {
5257 }
5358
5459 /// Check if the caller has caught up with the bootstrapper.
55- /// If catch_up_height is unset, the sync isn't even ready yet.
60+ /// If catch_up_height is unset, the batcher isn't even ready yet.
5661 pub fn is_caught_up ( & self , current_provider_height : BlockNumber ) -> bool {
57- let is_caught_up = self
58- . catch_up_height ( )
59- . is_some_and ( |catch_up_height| current_provider_height > catch_up_height) ;
62+ let is_caught_up = match self . catch_up_height ( ) {
63+ Some ( catch_up_height) => current_provider_height > catch_up_height,
64+ None => current_provider_height == BlockNumber ( 0 ) ,
65+ } ;
6066
6167 self . sync_task_health_check ( is_caught_up) ;
6268
@@ -80,14 +86,15 @@ impl Bootstrapper {
8086 }
8187
8288 /// Spawns async task that produces and sends commit block messages to the provider, according
83- /// to information from the sync client , until the provider is caught up.
89+ /// to information from the batcher and sync clients , until the provider is caught up.
8490 pub async fn start_l2_sync ( & mut self , current_provider_height : BlockNumber ) {
8591 // FIXME: spawning a task like this is evil.
8692 // However, we aren't using the task executor, so no choice :(
8793 // Once we start using a centralized threadpool, spawn through it instead of the
8894 // tokio runtime.
8995 let sync_task_handle = tokio:: spawn ( l2_sync_task (
9096 self . l1_provider_client . clone ( ) ,
97+ self . batcher_client . clone ( ) ,
9198 self . sync_client . clone ( ) ,
9299 current_provider_height,
93100 self . catch_up_height . clone ( ) ,
@@ -145,27 +152,34 @@ impl std::fmt::Debug for Bootstrapper {
145152 }
146153}
147154
155+ // TODO(noamsp): fix catch up height to use batcher height and not the latest block number in
156+ // storage.
148157async fn l2_sync_task (
149158 l1_provider_client : SharedL1ProviderClient ,
159+ batcher_client : SharedBatcherClient ,
150160 sync_client : SharedStateSyncClient ,
151161 mut current_height : BlockNumber ,
152162 catch_up_height : LazyCatchUpHeight ,
153163 retry_interval : Duration ,
154164) {
155- // Currently infra doesn't support starting up the provider only after sync is ready.
156- info ! ( "Try fetching sync height to initialize catch up point" ) ;
165+ info ! ( "Try fetching batcher height to initialize catch up point" ) ;
157166 while !catch_up_height. initialized ( ) {
158- let Some ( sync_height) = sync_client
159- . get_latest_block_number ( )
160- . await
161- . expect ( "network error handling not supported yet" )
167+ let Ok ( GetHeightResponse { height : batcher_height } ) = batcher_client. get_height ( ) . await
162168 else {
163- debug ! ( "Sync state not ready yet, trying again later " ) ;
169+ error ! ( "Batcher height request failed. Retrying... " ) ;
164170 tokio:: time:: sleep ( retry_interval) . await ;
165171 continue ;
166172 } ;
167- info ! ( "Catch up height set: {sync_height}" ) ;
168- catch_up_height. set ( sync_height) . expect ( "This is the only write-point, cannot fail" )
173+
174+ let Some ( batcher_latest_block_number) = batcher_height. prev ( ) else {
175+ info ! ( "Batcher height is 0, no need to catch up. exiting..." ) ;
176+ return ;
177+ } ;
178+
179+ info ! ( "Catch up height set: {batcher_latest_block_number}" ) ;
180+ catch_up_height
181+ . set ( batcher_latest_block_number)
182+ . expect ( "This is the only write-point, cannot fail" )
169183 }
170184 let catch_up_height = * catch_up_height. get ( ) . expect ( "Initialized above" ) ;
171185
0 commit comments