Skip to content

Commit 02d3633

Browse files
apollo_l1_provider: add flow test for finality (#10894)
1 parent b58d052 commit 02d3633

File tree

5 files changed

+86
-6
lines changed

5 files changed

+86
-6
lines changed

crates/apollo_l1_provider/tests/flow_test_cancellation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ async fn new_l1_handler_tx_propose_validate_cancellation_timelock() {
3333
let (l2_hash, nonce) = send_message_from_l1_to_l2(&mut base_layer, CALL_DATA).await;
3434

3535
let l1_provider_client =
36-
setup_scraper_and_provider(base_layer.ethereum_base_layer.clone()).await;
36+
setup_scraper_and_provider(base_layer.ethereum_base_layer.clone(), None).await;
3737

3838
// Test.
3939
tokio::time::pause();

crates/apollo_l1_provider/tests/flow_test_consumed.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ async fn l1_handler_tx_consumed_txs() {
9999
Ok(all_events[start..end_exclusive].to_vec())
100100
});
101101

102-
let l1_provider_client = setup_scraper_and_provider(base_layer).await;
102+
let l1_provider_client = setup_scraper_and_provider(base_layer, None).await;
103103

104104
tokio::time::pause();
105105
let snapshot = l1_provider_client.get_l1_provider_snapshot().await.unwrap();
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
}

crates/apollo_l1_provider/tests/flow_test_happy_flow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ async fn new_l1_handler_tx_propose_validate_cooldown() {
3131
let (l2_hash, _nonce) = send_message_from_l1_to_l2(&mut base_layer, CALL_DATA).await;
3232

3333
let l1_provider_client =
34-
setup_scraper_and_provider(base_layer.ethereum_base_layer.clone()).await;
34+
setup_scraper_and_provider(base_layer.ethereum_base_layer.clone(), None).await;
3535

3636
tokio::time::pause();
3737

crates/apollo_l1_provider/tests/utils/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub(crate) const COOLDOWN_DURATION: Duration = Duration::from_secs(30);
4949
pub(crate) const TIMELOCK_DURATION: Duration = Duration::from_secs(30);
5050
pub(crate) const WAIT_FOR_ASYNC_PROCESSING_DURATION: Duration = Duration::from_millis(50);
5151
const NUMBER_OF_BLOCKS_TO_MINE: u64 = 100;
52-
const CHAIN_ID: ChainId = ChainId::Mainnet;
52+
pub(crate) const CHAIN_ID: ChainId = ChainId::Mainnet;
5353
pub(crate) const L1_CONTRACT_ADDRESS: u64 = 1;
5454
pub(crate) const L2_ENTRY_POINT: u64 = 34;
5555
pub(crate) const CALL_DATA: &[u8] = &[1_u8, 2_u8];
@@ -82,6 +82,7 @@ pub(crate) async fn setup_scraper_and_provider<
8282
E: Error + Send + Sync + Debug + 'static,
8383
>(
8484
base_layer: T,
85+
l1_scraper_config: Option<L1ScraperConfig>,
8586
) -> LocalComponentClient<L1ProviderRequest, L1ProviderResponse> {
8687
let fake_clock = Arc::new(TokioLinkedClock::new());
8788

@@ -123,11 +124,11 @@ pub(crate) async fn setup_scraper_and_provider<
123124
});
124125

125126
// Set up the L1 scraper and run it as a server.
126-
let l1_scraper_config = L1ScraperConfig {
127+
let l1_scraper_config = l1_scraper_config.unwrap_or_else(|| L1ScraperConfig {
127128
polling_interval_seconds: POLLING_INTERVAL_DURATION,
128129
chain_id: CHAIN_ID,
129130
..Default::default()
130-
};
131+
});
131132
let mut scraper = L1Scraper::new(
132133
l1_scraper_config,
133134
Arc::new(l1_provider_client.clone()),

0 commit comments

Comments
 (0)