Skip to content

Commit 03caa1f

Browse files
fix(apollo_l1_provider): change catch up height from sync to batcher height (#6120)
1 parent 0fedc82 commit 03caa1f

9 files changed

Lines changed: 149 additions & 93 deletions

File tree

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.

config/sequencer/testing/app_configs/distributed/deployment_test_distributed/l1.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,16 @@
5151
"class_manager_config.class_storage_config.class_hash_storage_config.path_prefix": "/data/node_0/executable_0/class_manager/class_hash_storage",
5252
"class_manager_config.class_storage_config.persistent_root": "/data/node_0/executable_0/class_manager/classes",
5353
"compiler_config.max_bytecode_size": 81920,
54-
"components.batcher.execution_mode": "Disabled",
54+
"components.batcher.execution_mode": "Remote",
5555
"components.batcher.ip": "0.0.0.0",
5656
"components.batcher.local_server_config.channel_buffer_size": 32,
5757
"components.batcher.max_concurrency": 10,
58-
"components.batcher.port": 0,
59-
"components.batcher.remote_client_config.idle_connections": 18446744073709551615,
58+
"components.batcher.port": 15000,
59+
"components.batcher.remote_client_config.idle_connections": 5,
6060
"components.batcher.remote_client_config.idle_timeout": 90,
6161
"components.batcher.remote_client_config.retries": 3,
6262
"components.batcher.remote_client_config.retry_interval": 3,
63-
"components.batcher.url": "localhost",
63+
"components.batcher.url": "sequencer-batcher-service",
6464
"components.class_manager.execution_mode": "Disabled",
6565
"components.class_manager.ip": "0.0.0.0",
6666
"components.class_manager.local_server_config.channel_buffer_size": 32,

crates/apollo_deployments/src/deployments/distributed.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ impl GetComponentConfigs for DistributedNodeServiceName {
114114
l1_gas_price_provider.local(),
115115
l1_provider.local(),
116116
state_sync.remote(),
117+
batcher.remote(),
117118
),
118119
DistributedNodeServiceName::Mempool => get_mempool_component_config(
119120
mempool.local(),
@@ -583,13 +584,16 @@ fn get_l1_component_config(
583584
l1_gas_price_provider_local_config: ReactiveComponentExecutionConfig,
584585
l1_provider_local_config: ReactiveComponentExecutionConfig,
585586
state_sync_remote_config: ReactiveComponentExecutionConfig,
587+
batcher_remote_config: ReactiveComponentExecutionConfig,
586588
) -> ComponentConfig {
587589
let mut config = ComponentConfig::disabled();
590+
588591
config.l1_gas_price_provider = l1_gas_price_provider_local_config;
589592
config.l1_gas_price_scraper = ActiveComponentExecutionConfig::enabled();
590593
config.l1_provider = l1_provider_local_config;
591594
config.l1_scraper = ActiveComponentExecutionConfig::enabled();
592595
config.state_sync = state_sync_remote_config;
593596
config.monitoring_endpoint = ActiveComponentExecutionConfig::enabled();
597+
config.batcher = batcher_remote_config;
594598
config
595599
}

crates/apollo_l1_provider/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ repository.workspace = true
66
license.workspace = true
77

88
[features]
9-
testing = ["pretty_assertions", "starknet_api/testing"]
9+
testing = ["apollo_batcher_types/testing", "pretty_assertions", "starknet_api/testing"]
1010

1111
[dependencies]
12+
apollo_batcher_types.workspace = true
1213
apollo_config.workspace = true
1314
apollo_infra.workspace = true
1415
apollo_l1_provider_types.workspace = true
@@ -29,6 +30,7 @@ validator.workspace = true
2930

3031
[dev-dependencies]
3132
alloy.workspace = true
33+
apollo_batcher_types = { workspace = true, features = ["testing"] }
3234
apollo_l1_provider_types = { workspace = true, features = ["testing"] }
3335
apollo_state_sync_types = { workspace = true, features = ["testing"] }
3436
assert_matches.workspace = true

crates/apollo_l1_provider/src/bootstrapper.rs

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,29 @@ use std::sync::atomic::{AtomicU8, Ordering};
33
use std::sync::Arc;
44
use std::time::Duration;
55

6+
use apollo_batcher_types::batcher_types::GetHeightResponse;
7+
use apollo_batcher_types::communication::SharedBatcherClient;
68
use apollo_l1_provider_types::SharedL1ProviderClient;
79
use apollo_state_sync_types::communication::SharedStateSyncClient;
810
use starknet_api::block::BlockNumber;
911
use starknet_api::transaction::TransactionHash;
1012
use tokio::sync::OnceCell;
11-
use tracing::{debug, info};
13+
use tracing::{debug, error, info};
1214

1315
pub type LazyCatchUpHeight = Arc<OnceCell<BlockNumber>>;
1416

1517
/// Cache's commits to be applied later. This flow is only relevant while the node is starting up.
1618
#[derive(Clone)]
1719
pub struct Bootstrapper {
18-
/// The catch-up height for the bootstrapper is the sync height (unless overridden explicitly).
19-
/// This value, due to infra constraints as of now, is only fetchable _after_ the provider is
20-
/// running, and not during its initialization, hence we are forced to lazily fetch it at
21-
/// runtime.
20+
/// The catch-up height for the bootstrapper is the batcher height (unless overridden
21+
/// explicitly). This value, due to infra constraints as of now, is only fetchable _after_
22+
/// the provider is running, and not during its initialization, hence we are forced to
23+
/// lazily fetch it at runtime.
2224
pub catch_up_height: LazyCatchUpHeight,
2325
pub sync_retry_interval: Duration,
2426
pub commit_block_backlog: Vec<CommitBlockBacklog>,
2527
pub l1_provider_client: SharedL1ProviderClient,
28+
pub batcher_client: SharedBatcherClient,
2629
pub sync_client: SharedStateSyncClient,
2730
// Keep track of sync task for health checks and logging status.
2831
pub sync_task_handle: SyncTaskHandle,
@@ -36,6 +39,7 @@ impl Bootstrapper {
3639

3740
pub fn new(
3841
l1_provider_client: SharedL1ProviderClient,
42+
batcher_client: SharedBatcherClient,
3943
sync_client: SharedStateSyncClient,
4044
sync_retry_interval: Duration,
4145
catch_up_height: LazyCatchUpHeight,
@@ -44,6 +48,7 @@ impl Bootstrapper {
4448
sync_retry_interval,
4549
commit_block_backlog: Default::default(),
4650
l1_provider_client,
51+
batcher_client,
4752
sync_client,
4853
sync_task_handle: SyncTaskHandle::NotStartedYet,
4954
n_sync_health_check_failures: Default::default(),
@@ -52,11 +57,12 @@ impl Bootstrapper {
5257
}
5358

5459
/// Check if the caller has caught up with the bootstrapper.
55-
/// If catch_up_height is unset, the sync isn't even ready yet.
60+
/// If catch_up_height is unset, the batcher isn't even ready yet.
5661
pub fn is_caught_up(&self, current_provider_height: BlockNumber) -> bool {
57-
let is_caught_up = self
58-
.catch_up_height()
59-
.is_some_and(|catch_up_height| current_provider_height > catch_up_height);
62+
let is_caught_up = match self.catch_up_height() {
63+
Some(catch_up_height) => current_provider_height > catch_up_height,
64+
None => current_provider_height == BlockNumber(0),
65+
};
6066

6167
self.sync_task_health_check(is_caught_up);
6268

@@ -80,14 +86,15 @@ impl Bootstrapper {
8086
}
8187

8288
/// Spawns async task that produces and sends commit block messages to the provider, according
83-
/// to information from the sync client, until the provider is caught up.
89+
/// to information from the batcher and sync clients, until the provider is caught up.
8490
pub async fn start_l2_sync(&mut self, current_provider_height: BlockNumber) {
8591
// FIXME: spawning a task like this is evil.
8692
// However, we aren't using the task executor, so no choice :(
8793
// Once we start using a centralized threadpool, spawn through it instead of the
8894
// tokio runtime.
8995
let sync_task_handle = tokio::spawn(l2_sync_task(
9096
self.l1_provider_client.clone(),
97+
self.batcher_client.clone(),
9198
self.sync_client.clone(),
9299
current_provider_height,
93100
self.catch_up_height.clone(),
@@ -145,27 +152,34 @@ impl std::fmt::Debug for Bootstrapper {
145152
}
146153
}
147154

155+
// TODO(noamsp): fix catch up height to use batcher height and not the latest block number in
156+
// storage.
148157
async fn l2_sync_task(
149158
l1_provider_client: SharedL1ProviderClient,
159+
batcher_client: SharedBatcherClient,
150160
sync_client: SharedStateSyncClient,
151161
mut current_height: BlockNumber,
152162
catch_up_height: LazyCatchUpHeight,
153163
retry_interval: Duration,
154164
) {
155-
// Currently infra doesn't support starting up the provider only after sync is ready.
156-
info!("Try fetching sync height to initialize catch up point");
165+
info!("Try fetching batcher height to initialize catch up point");
157166
while !catch_up_height.initialized() {
158-
let Some(sync_height) = sync_client
159-
.get_latest_block_number()
160-
.await
161-
.expect("network error handling not supported yet")
167+
let Ok(GetHeightResponse { height: batcher_height }) = batcher_client.get_height().await
162168
else {
163-
debug!("Sync state not ready yet, trying again later");
169+
error!("Batcher height request failed. Retrying...");
164170
tokio::time::sleep(retry_interval).await;
165171
continue;
166172
};
167-
info!("Catch up height set: {sync_height}");
168-
catch_up_height.set(sync_height).expect("This is the only write-point, cannot fail")
173+
174+
let Some(batcher_latest_block_number) = batcher_height.prev() else {
175+
info!("Batcher height is 0, no need to catch up. exiting...");
176+
return;
177+
};
178+
179+
info!("Catch up height set: {batcher_latest_block_number}");
180+
catch_up_height
181+
.set(batcher_latest_block_number)
182+
.expect("This is the only write-point, cannot fail")
169183
}
170184
let catch_up_height = *catch_up_height.get().expect("Initialized above");
171185

crates/apollo_l1_provider/src/l1_provider.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::cmp::Ordering::{Equal, Greater, Less};
22
use std::collections::HashSet;
33
use std::sync::Arc;
44

5+
use apollo_batcher_types::communication::SharedBatcherClient;
56
use apollo_infra::component_definitions::ComponentStarter;
67
use apollo_l1_provider_types::errors::L1ProviderError;
78
use apollo_l1_provider_types::{
@@ -264,6 +265,7 @@ impl ComponentStarter for L1Provider {}
264265
pub struct L1ProviderBuilder {
265266
pub config: L1ProviderConfig,
266267
pub l1_provider_client: SharedL1ProviderClient,
268+
pub batcher_client: SharedBatcherClient,
267269
pub state_sync_client: SharedStateSyncClient,
268270
startup_height: Option<BlockNumber>,
269271
catchup_height: Option<BlockNumber>,
@@ -273,11 +275,13 @@ impl L1ProviderBuilder {
273275
pub fn new(
274276
config: L1ProviderConfig,
275277
l1_provider_client: SharedL1ProviderClient,
278+
batcher_client: SharedBatcherClient,
276279
state_sync_client: SharedStateSyncClient,
277280
) -> Self {
278281
Self {
279282
config,
280283
l1_provider_client,
284+
batcher_client,
281285
state_sync_client,
282286
startup_height: None,
283287
catchup_height: None,
@@ -320,16 +324,17 @@ impl L1ProviderBuilder {
320324
warn!(
321325
"OVERRIDE L1 provider catch-up height: {catch_up_height_override}. WARNING: \
322326
this MUST be greater or equal to the default non-overridden value, which is \
323-
the (runtime fetched) sync height, or the sync will never complete!"
327+
the (runtime fetched) batcher height, or the sync will never complete!"
324328
);
325329
})
326330
.or(self.catchup_height)
327331
.map(|catchup_height| Arc::new(catchup_height.into()))
328-
// When kept None, this value is fetched from the sync by the bootstrapper at runtime.
332+
// When kept None, this value is fetched from the batcher by the bootstrapper at runtime.
329333
.unwrap_or_default();
330334

331335
let bootstrapper = Bootstrapper::new(
332336
self.l1_provider_client,
337+
self.batcher_client,
333338
self.state_sync_client,
334339
self.config.startup_sync_sleep_retry_interval_seconds,
335340
catchup_height,

crates/apollo_l1_provider/src/l1_provider_tests.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::collections::HashSet;
22
use std::sync::Arc;
33
use std::time::Duration;
44

5+
use apollo_batcher_types::communication::MockBatcherClient;
56
use apollo_l1_provider_types::errors::L1ProviderError;
67
use apollo_l1_provider_types::SessionState::{
78
self,
@@ -56,16 +57,17 @@ macro_rules! bootstrapper {
5657
height: BlockNumber($height),
5758
committed_txs: vec![$(tx_hash!($tx)),*]
5859
}),*
59-
].into_iter().collect(),
60-
catch_up_height: Arc::new(BlockNumber($catch).into()),
61-
l1_provider_client: Arc::new(FakeL1ProviderClient::default()),
62-
sync_client: Arc::new(MockStateSyncClient::default()),
63-
sync_task_handle: SyncTaskHandle::default(),
64-
n_sync_health_check_failures: Default::default(),
65-
sync_retry_interval: Duration::from_millis(10)
66-
}
67-
}};
68-
}
60+
].into_iter().collect(),
61+
catch_up_height: Arc::new(BlockNumber($catch).into()),
62+
l1_provider_client: Arc::new(FakeL1ProviderClient::default()),
63+
batcher_client: Arc::new(MockBatcherClient::default()),
64+
sync_client: Arc::new(MockStateSyncClient::default()),
65+
sync_task_handle: SyncTaskHandle::default(),
66+
n_sync_health_check_failures: Default::default(),
67+
sync_retry_interval: Duration::from_millis(10)
68+
}
69+
}};
70+
}
6971

7072
fn l1_handler_event(tx_hash: TransactionHash) -> Event {
7173
Event::L1HandlerTransaction(executable_l1_handler_tx(L1HandlerTxArgs {

0 commit comments

Comments
 (0)