@@ -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_then_run ( & mut self ) -> L1GasPriceScraperResult < ( ) , B > {
67+ let start_from = loop {
68+ match self . config . starting_block {
69+ Some ( block) => break block,
70+ None => {
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+ tokio:: time:: sleep ( self . config . polling_interval ) . await ;
75+ continue ;
76+ } ;
77+ // If no starting block is provided, the default is to start from
78+ // startup_num_blocks_multiplier * number_of_blocks_for_mean before the tip of
79+ // L1. Note that for new chains this subtraction may be
80+ // negative, hence the use of saturating_sub.
81+ let latest = latest. saturating_sub (
82+ self . config . number_of_blocks_for_mean
83+ * self . config . startup_num_blocks_multiplier ,
84+ ) ;
85+ break latest;
86+ }
87+ }
88+ } ;
89+ self . run ( start_from) . await
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,8 @@ 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+ self . get_first_block_then_run ( )
204+ . await
205+ . unwrap_or_else ( |e| panic ! ( "L1 gas price scraper failed: {e}" ) )
196206 }
197207}
0 commit comments