|
| 1 | +#![cfg(any(test, feature = "testing"))] |
| 2 | +mod utils; |
| 3 | + |
| 4 | +use apollo_l1_provider_types::{ |
| 5 | + InvalidValidationStatus, |
| 6 | + L1ProviderClient, |
| 7 | + SessionState, |
| 8 | + ValidationStatus, |
| 9 | +}; |
| 10 | +use apollo_l1_scraper_config::config::L1ScraperConfig; |
| 11 | +use starknet_api::block::BlockNumber; |
| 12 | +use utils::{ |
| 13 | + send_message_from_l1_to_l2, |
| 14 | + setup_anvil_base_layer, |
| 15 | + setup_scraper_and_provider, |
| 16 | + CALL_DATA, |
| 17 | + CALL_DATA_2, |
| 18 | + CHAIN_ID, |
| 19 | + POLLING_INTERVAL_DURATION, |
| 20 | + ROUND_TO_SEC_MARGIN_DURATION, |
| 21 | + TARGET_L2_HEIGHT, |
| 22 | + WAIT_FOR_ASYNC_PROCESSING_DURATION, |
| 23 | +}; |
| 24 | + |
| 25 | +#[tokio::test] |
| 26 | +async fn only_scrape_after_finality() { |
| 27 | + // Setup. |
| 28 | + const FINALITY: u64 = 3; |
| 29 | + |
| 30 | + // Setup the base layer. |
| 31 | + let mut base_layer = setup_anvil_base_layer().await; |
| 32 | + |
| 33 | + let (l2_hash, _nonce) = send_message_from_l1_to_l2(&mut base_layer, CALL_DATA).await; |
| 34 | + |
| 35 | + let l1_scraper_config = L1ScraperConfig { |
| 36 | + finality: FINALITY, |
| 37 | + polling_interval_seconds: POLLING_INTERVAL_DURATION, |
| 38 | + chain_id: CHAIN_ID, |
| 39 | + ..Default::default() |
| 40 | + }; |
| 41 | + let l1_provider_client = |
| 42 | + setup_scraper_and_provider(base_layer.ethereum_base_layer.clone(), Some(l1_scraper_config)) |
| 43 | + .await; |
| 44 | + |
| 45 | + tokio::time::pause(); |
| 46 | + |
| 47 | + // Test. |
| 48 | + let next_block_height = BlockNumber(TARGET_L2_HEIGHT.0 + 1); |
| 49 | + |
| 50 | + // The transaction is not yet scraped, it hasn't got enough blocks after it. |
| 51 | + l1_provider_client.start_block(SessionState::Validate, next_block_height).await.unwrap(); |
| 52 | + assert_eq!( |
| 53 | + l1_provider_client.validate(l2_hash, next_block_height).await.unwrap(), |
| 54 | + ValidationStatus::Invalid(InvalidValidationStatus::NotFound) |
| 55 | + ); |
| 56 | + |
| 57 | + // Send few more blocks (by sending more txs). |
| 58 | + for _ in 0..FINALITY { |
| 59 | + let (other_l2_hash, _nonce) = |
| 60 | + send_message_from_l1_to_l2(&mut base_layer, CALL_DATA_2).await; |
| 61 | + assert_ne!(l2_hash, other_l2_hash); |
| 62 | + } |
| 63 | + |
| 64 | + // Wait for another scraping. |
| 65 | + tokio::time::advance(POLLING_INTERVAL_DURATION + ROUND_TO_SEC_MARGIN_DURATION).await; |
| 66 | + for _i in 0..100 { |
| 67 | + let snapshot = l1_provider_client.get_l1_provider_snapshot().await.unwrap(); |
| 68 | + if snapshot.uncommitted_transactions.contains(&l2_hash) { |
| 69 | + break; |
| 70 | + } |
| 71 | + tokio::time::sleep(WAIT_FOR_ASYNC_PROCESSING_DURATION).await; |
| 72 | + } |
| 73 | + |
| 74 | + // Check that we can validate the message now. |
| 75 | + assert_eq!( |
| 76 | + l1_provider_client.validate(l2_hash, next_block_height).await.unwrap(), |
| 77 | + ValidationStatus::Validated |
| 78 | + ); |
| 79 | +} |
0 commit comments