Skip to content

Commit d272a72

Browse files
apollo_storage,apollo_batcher: spawn storage reader server independently
1 parent 4f29510 commit d272a72

File tree

4 files changed

+37
-8
lines changed

4 files changed

+37
-8
lines changed

crates/apollo_batcher/src/batcher.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ use starknet_api::core::{ContractAddress, GlobalRoot, Nonce};
6969
use starknet_api::state::{StateNumber, ThinStateDiff};
7070
use starknet_api::transaction::TransactionHash;
7171
use tokio::sync::Mutex;
72+
use tokio::task::JoinHandle;
7273
use tracing::{debug, error, info, instrument, trace, Instrument};
7374

7475
use crate::block_builder::{
@@ -173,11 +174,10 @@ pub struct Batcher {
173174
/// This is returned by the decision_reached function.
174175
prev_proposal_commitment: Option<(BlockNumber, ProposalCommitment)>,
175176

176-
/// Optional storage reader server for handling remote storage reader queries.
177-
/// Kept alive to maintain the server running.
178-
#[allow(dead_code)]
179-
storage_reader_server: Option<GenericStorageReaderServer>,
180177
commitment_manager: ApolloCommitmentManager,
178+
179+
/// Task handle for the storage reader server, if enabled.
180+
storage_reader_server_handle: Option<JoinHandle<()>>,
181181
}
182182

183183
impl Batcher {
@@ -192,8 +192,8 @@ impl Batcher {
192192
transaction_converter: TransactionConverter,
193193
block_builder_factory: Box<dyn BlockBuilderFactoryTrait>,
194194
pre_confirmed_block_writer_factory: Box<dyn PreconfirmedBlockWriterFactoryTrait>,
195-
storage_reader_server: Option<GenericStorageReaderServer>,
196195
commitment_manager: ApolloCommitmentManager,
196+
storage_reader_server_handle: Option<JoinHandle<()>>,
197197
) -> Self {
198198
Self {
199199
config,
@@ -214,8 +214,8 @@ impl Batcher {
214214
// Allow the first few proposals to be without L1 txs while system starts up.
215215
proposals_counter: 1,
216216
prev_proposal_commitment: None,
217-
storage_reader_server,
218217
commitment_manager,
218+
storage_reader_server_handle,
219219
}
220220
}
221221

@@ -1255,6 +1255,15 @@ fn log_txs_execution_result(
12551255
}
12561256
}
12571257

1258+
impl Drop for Batcher {
1259+
fn drop(&mut self) {
1260+
// Abort the storage reader server task if it was spawned.
1261+
if let Some(handle) = self.storage_reader_server_handle.take() {
1262+
handle.abort();
1263+
}
1264+
}
1265+
}
1266+
12581267
pub async fn create_batcher(
12591268
config: BatcherConfig,
12601269
committer_client: SharedCommitterClient,
@@ -1271,6 +1280,9 @@ pub async fn create_batcher(
12711280
)
12721281
.expect("Failed to open batcher's storage");
12731282

1283+
let storage_reader_server_handle =
1284+
GenericStorageReaderServer::spawn_if_enabled(storage_reader_server);
1285+
12741286
let execute_config = &config.block_builder_config.execute_config;
12751287
let worker_pool = Arc::new(WorkerPool::start(execute_config));
12761288
let pre_confirmed_block_writer_factory = Box::new(PreconfirmedBlockWriterFactory {
@@ -1310,8 +1322,8 @@ pub async fn create_batcher(
13101322
transaction_converter,
13111323
block_builder_factory,
13121324
pre_confirmed_block_writer_factory,
1313-
storage_reader_server,
13141325
commitment_manager,
1326+
storage_reader_server_handle,
13151327
)
13161328
}
13171329

crates/apollo_batcher/src/batcher_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ async fn create_batcher_impl<R: BatcherStorageReader + 'static>(
194194
TransactionConverter::new(clients.class_manager_client, CHAIN_ID_FOR_TESTS.clone()),
195195
Box::new(clients.block_builder_factory),
196196
Box::new(clients.pre_confirmed_block_writer_factory),
197-
None,
198197
commitment_manager,
198+
None,
199199
);
200200
// Call post-creation functionality (e.g., metrics registration).
201201
batcher.start().await;

crates/apollo_storage/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ starknet-types-core = { workspace = true, features = ["papyrus-serialization"] }
3636
starknet_api.workspace = true
3737
tempfile = { workspace = true, optional = true }
3838
thiserror.workspace = true
39+
tokio.workspace = true
3940
tower = { workspace = true, optional = true }
4041
tracing = { workspace = true, features = ["log"] }
4142
validator = { workspace = true, features = ["derive"] }

crates/apollo_storage/src/storage_reader_server.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,22 @@ where
160160
StorageError::IOError(io::Error::other(e))
161161
})
162162
}
163+
164+
/// Spawns the storage reader server in a background task if it's enabled.
165+
pub fn spawn_if_enabled(server: Option<Self>) -> Option<tokio::task::JoinHandle<()>>
166+
where
167+
RequestHandler: Send + Sync + 'static,
168+
Request: Send + Sync + 'static,
169+
Response: Send + Sync + 'static,
170+
{
171+
server.map(|server| {
172+
tokio::spawn(async move {
173+
if let Err(e) = server.run().await {
174+
tracing::error!("Storage reader server error: {:?}", e);
175+
}
176+
})
177+
})
178+
}
163179
}
164180

165181
/// Axum handler for storage query requests.

0 commit comments

Comments
 (0)