@@ -12,7 +12,7 @@ use async_trait::async_trait;
1212use papyrus_base_layer:: { BaseLayerContract , L1BlockHeader , L1BlockNumber } ;
1313use starknet_api:: block:: GasPrice ;
1414use thiserror:: Error ;
15- use tracing:: { error, info, trace} ;
15+ use tracing:: { error, info, trace, warn } ;
1616
1717use crate :: metrics:: {
1818 register_scraper_metrics,
@@ -63,6 +63,32 @@ impl<B: BaseLayerContract + Send + Sync + Debug> L1GasPriceScraper<B> {
6363 Self { config, l1_gas_price_provider, base_layer, last_l1_header : None }
6464 }
6565
66+ pub async fn get_first_block_number ( & mut self ) -> L1GasPriceScraperResult < L1BlockNumber , B > {
67+ match self . config . starting_block {
68+ Some ( block) => Ok ( block) ,
69+ None => {
70+ loop {
71+ let latest = self . latest_l1_block_number ( ) . await ;
72+ let Ok ( latest) = latest else {
73+ warn ! ( "Failed to get the latest L1 block number at startup: {latest:?}" ) ;
74+ L1_GAS_PRICE_SCRAPER_BASELAYER_ERROR_COUNT . increment ( 1 ) ;
75+ tokio:: time:: sleep ( self . config . polling_interval ) . await ;
76+ continue ;
77+ } ;
78+ // If no starting block is provided, the default is to start from
79+ // startup_num_blocks_multiplier * number_of_blocks_for_mean before the tip of
80+ // L1. Note that for new chains this subtraction may be
81+ // negative, hence the use of saturating_sub.
82+ let latest = latest. saturating_sub (
83+ self . config . number_of_blocks_for_mean
84+ * self . config . startup_num_blocks_multiplier ,
85+ ) ;
86+ break Ok ( latest) ;
87+ }
88+ }
89+ }
90+ }
91+
6692 /// Run the scraper, starting from the given L1 `block_number`, indefinitely.
6793 pub async fn run ( & mut self , mut block_number : L1BlockNumber ) -> L1GasPriceScraperResult < ( ) , B > {
6894 self . l1_gas_price_provider
@@ -174,24 +200,15 @@ where
174200 async fn start ( & mut self ) {
175201 info ! ( "Starting component {}." , type_name:: <Self >( ) ) ;
176202 register_scraper_metrics ( ) ;
177- let start_from = match self . config . starting_block {
178- Some ( block) => block,
179- None => {
180- let latest = self
181- . latest_l1_block_number ( )
182- . await
183- . expect ( "Failed to get the latest L1 block number at startup" ) ;
184-
185- // If no starting block is provided, the default is to start from
186- // startup_num_blocks_multiplier * number_of_blocks_for_mean before the tip of L1.
187- // Note that for new chains this subtraction may be negative,
188- // hence the use of saturating_sub.
189- latest. saturating_sub (
190- self . config . number_of_blocks_for_mean
191- * self . config . startup_num_blocks_multiplier ,
192- )
193- }
194- } ;
195- self . run ( start_from) . await . unwrap_or_else ( |e| panic ! ( "L1 gas price scraper failed: {e}" ) )
203+ // Loop and retry base layer until we successfully get the first block number.
204+ let first_block_number = self
205+ . get_first_block_number ( )
206+ . await
207+ . expect ( "get_first_block_number_should be in an endless loop until it succeeds" ) ;
208+
209+ // Run the scraper on an endless loop.
210+ self . run ( first_block_number)
211+ . await
212+ . unwrap_or_else ( |e| panic ! ( "L1 gas price scraper failed: {e}" ) )
196213 }
197214}
0 commit comments