Skip to content

Commit 77f1085

Browse files
committed
apollo_l1_gas_price: add test for low latest L1 block number
1 parent 7b71af3 commit 77f1085

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/apollo_l1_gas_price/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ apollo_infra_utils.workspace = true
1212
apollo_l1_gas_price_provider_config.workspace = true
1313
apollo_l1_gas_price_types.workspace = true
1414
apollo_metrics.workspace = true
15+
assert_matches.workspace = true
1516
async-trait.workspace = true
1617
futures.workspace = true
1718
lru.workspace = true

crates/apollo_l1_gas_price/src/l1_gas_price_scraper_test.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
22
use std::sync::Arc;
33

44
use apollo_l1_gas_price_types::{GasPriceData, MockL1GasPriceProviderClient};
5+
use assert_matches::assert_matches;
56
use papyrus_base_layer::{L1BlockHash, L1BlockHeader, MockBaseLayerContract};
67
use rstest::rstest;
78
use 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

Comments
 (0)