@@ -2,6 +2,7 @@ use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
22use std:: sync:: Arc ;
33
44use apollo_l1_gas_price_types:: { GasPriceData , MockL1GasPriceProviderClient } ;
5+ use assert_matches:: assert_matches;
56use papyrus_base_layer:: { L1BlockHash , L1BlockHeader , MockBaseLayerContract } ;
67use rstest:: rstest;
78use starknet_api:: block:: GasPrice ;
@@ -279,4 +280,47 @@ async fn l1_short_reorg_gas_price_scraper_is_fine(#[case] finality: u64) {
279280 }
280281}
281282
283+ #[ tokio:: test]
284+ async fn base_layer_returns_block_number_below_finality ( ) {
285+ // Setup.
286+ const FINALITY : u64 = 10 ;
287+ const INITIAL_L1_BLOCK_NUMBER : u64 = 100 ;
288+ const WRONG_L1_BLOCK_NUMBER : u64 = 5 ;
289+
290+ let latest_l1_block_number_response = Arc :: new ( AtomicU64 :: new ( INITIAL_L1_BLOCK_NUMBER ) ) ;
291+ let latest_l1_block_number_response_clone = latest_l1_block_number_response. clone ( ) ;
292+
293+ let mut dummy_base_layer: MockBaseLayerContract = MockBaseLayerContract :: new ( ) ;
294+
295+ dummy_base_layer
296+ . expect_latest_l1_block_number ( )
297+ . returning ( move || Ok ( latest_l1_block_number_response_clone. load ( Ordering :: Relaxed ) ) ) ;
298+ dummy_base_layer. expect_get_block_header ( ) . returning ( move |block_number| {
299+ if block_number >= INITIAL_L1_BLOCK_NUMBER {
300+ Ok ( None )
301+ } else {
302+ Ok ( Some ( create_l1_block_header ( block_number) ) )
303+ }
304+ } ) ;
305+
306+ let mut scraper = L1GasPriceScraper :: new (
307+ L1GasPriceScraperConfig { finality : FINALITY , ..Default :: default ( ) } ,
308+ Arc :: new ( MockL1GasPriceProviderClient :: new ( ) ) ,
309+ dummy_base_layer,
310+ ) ;
311+
312+ // Test.
313+ let mut block_number = INITIAL_L1_BLOCK_NUMBER ;
314+ assert_matches ! ( scraper. update_prices( & mut block_number) . await , Ok ( ( ) ) ) ;
315+
316+ // Simulate a base layer returning a lower block number.
317+ latest_l1_block_number_response. store ( WRONG_L1_BLOCK_NUMBER , Ordering :: Relaxed ) ;
318+
319+ // The scraper should return a finality too high error.
320+ assert_matches ! (
321+ scraper. update_prices( & mut block_number) . await ,
322+ Err ( L1GasPriceScraperError :: FinalityTooHigh { .. } )
323+ ) ;
324+ }
325+
282326// TODO(guyn): test scraper with a provider timeout
0 commit comments