From a7f231f0f9ac4b4c0b1a67b0f04d2fafd0e9ab66 Mon Sep 17 00:00:00 2001 From: Mohammad Nassar Date: Mon, 18 Nov 2024 18:40:00 +0200 Subject: [PATCH] feat(l1-provider): build l1 provider starter --- Cargo.lock | 15 ++++++ Cargo.toml | 2 + .../starknet_l1_provider_starter/Cargo.toml | 20 +++++++ .../src/communication.rs | 5 ++ .../src/config.rs | 30 +++++++++++ .../src/errors.rs | 7 +++ .../src/l1_provider_starter.rs | 53 +++++++++++++++++++ .../starknet_l1_provider_starter/src/lib.rs | 4 ++ 8 files changed, 136 insertions(+) create mode 100644 crates/starknet_l1_provider_starter/Cargo.toml create mode 100644 crates/starknet_l1_provider_starter/src/communication.rs create mode 100644 crates/starknet_l1_provider_starter/src/config.rs create mode 100644 crates/starknet_l1_provider_starter/src/errors.rs create mode 100644 crates/starknet_l1_provider_starter/src/l1_provider_starter.rs create mode 100644 crates/starknet_l1_provider_starter/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 60350ac406..50ab0aec97 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10335,6 +10335,21 @@ dependencies = [ "starknet_l1_provider_types", ] +[[package]] +name = "starknet_l1_provider_starter" +version = "0.0.0" +dependencies = [ + "async-trait", + "papyrus_config", + "serde", + "starknet_l1_provider_types", + "starknet_sequencer_infra", + "thiserror", + "tokio", + "tracing", + "validator", +] + [[package]] name = "starknet_l1_provider_types" version = "0.0.0" diff --git a/Cargo.toml b/Cargo.toml index c3c9c2d5ba..fec039161a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,6 +39,7 @@ members = [ "crates/starknet_http_server", "crates/starknet_integration_tests", "crates/starknet_l1_provider", + "crates/starknet_l1_provider_starter", "crates/starknet_l1_provider_types", "crates/starknet_mempool", "crates/starknet_mempool_p2p", @@ -221,6 +222,7 @@ starknet_gateway = { path = "crates/starknet_gateway", version = "0.0.0" } starknet_gateway_types = { path = "crates/starknet_gateway_types", version = "0.0.0" } starknet_http_server = { path = "crates/starknet_http_server", version = "0.0.0" } starknet_l1_provider = { path = "crates/starknet_l1_provider", version = "0.0.0" } +starknet_l1_provider_starter = { path = "crates/starknet_l1_provider_starter", version = "0.0.0" } starknet_l1_provider_types = { path = "crates/starknet_l1_provider_types", version = "0.0.0" } starknet_mempool = { path = "crates/starknet_mempool", version = "0.0.0" } starknet_mempool_p2p = { path = "crates/starknet_mempool_p2p", version = "0.0.0" } diff --git a/crates/starknet_l1_provider_starter/Cargo.toml b/crates/starknet_l1_provider_starter/Cargo.toml new file mode 100644 index 0000000000..a0cf9abc89 --- /dev/null +++ b/crates/starknet_l1_provider_starter/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "starknet_l1_provider_starter" +version.workspace = true +edition.workspace = true +repository.workspace = true +license.workspace = true + +[dependencies] +async-trait.workspace = true +papyrus_config.workspace = true +serde.workspace = true +starknet_l1_provider_types.workspace = true +starknet_sequencer_infra.workspace = true +thiserror.workspace = true +tokio = { workspace = true, features = ["full", "sync"] } +tracing.workspace = true +validator.workspace = true + +[lints] +workspace = true diff --git a/crates/starknet_l1_provider_starter/src/communication.rs b/crates/starknet_l1_provider_starter/src/communication.rs new file mode 100644 index 0000000000..4ff5a319f8 --- /dev/null +++ b/crates/starknet_l1_provider_starter/src/communication.rs @@ -0,0 +1,5 @@ +use starknet_sequencer_infra::component_server::WrapperServer; + +use crate::l1_provider_starter::L1ProviderStarter as L1ProviderStarterComponent; + +pub type L1ProviderStarter = WrapperServer; diff --git a/crates/starknet_l1_provider_starter/src/config.rs b/crates/starknet_l1_provider_starter/src/config.rs new file mode 100644 index 0000000000..87623cd7a0 --- /dev/null +++ b/crates/starknet_l1_provider_starter/src/config.rs @@ -0,0 +1,30 @@ +use std::collections::BTreeMap; +use std::time::Duration; + +use papyrus_config::dumping::{ser_param, SerializeConfig}; +use papyrus_config::{ParamPath, ParamPrivacyInput, SerializedParam}; +use serde::{Deserialize, Serialize}; +use validator::Validate; + +/// The http server connection related configuration. +#[derive(Clone, Debug, Serialize, Deserialize, Validate, PartialEq)] +pub struct L1ProviderStarterConfig { + pub interval: Duration, +} + +impl SerializeConfig for L1ProviderStarterConfig { + fn dump(&self) -> BTreeMap { + BTreeMap::from_iter([ser_param( + "interval", + &self.interval.as_secs().to_string(), + "The is loop interval.", + ParamPrivacyInput::Public, + )]) + } +} + +impl Default for L1ProviderStarterConfig { + fn default() -> Self { + Self { interval: Duration::from_secs(10) } + } +} diff --git a/crates/starknet_l1_provider_starter/src/errors.rs b/crates/starknet_l1_provider_starter/src/errors.rs new file mode 100644 index 0000000000..d99541d808 --- /dev/null +++ b/crates/starknet_l1_provider_starter/src/errors.rs @@ -0,0 +1,7 @@ +use thiserror::Error; + +#[derive(Debug, Error)] +pub enum L1ProviderRunError { + #[error("L1 provider client failed to start: {0}")] + L1ProviderClientError(String), +} diff --git a/crates/starknet_l1_provider_starter/src/l1_provider_starter.rs b/crates/starknet_l1_provider_starter/src/l1_provider_starter.rs new file mode 100644 index 0000000000..998ddbea14 --- /dev/null +++ b/crates/starknet_l1_provider_starter/src/l1_provider_starter.rs @@ -0,0 +1,53 @@ +use std::any::type_name; + +use async_trait::async_trait; +use starknet_l1_provider_types::communication::SharedL1ProviderClient; +use starknet_sequencer_infra::component_definitions::ComponentStarter; +use starknet_sequencer_infra::errors::ComponentError; +use tokio::time; +use tracing::info; + +use crate::config::L1ProviderStarterConfig; +use crate::errors::L1ProviderRunError; + +pub struct L1ProviderStarter { + pub config: L1ProviderStarterConfig, + pub l1_provider_client: SharedL1ProviderClient, +} + +impl L1ProviderStarter { + pub fn new( + config: L1ProviderStarterConfig, + l1_provider_client: SharedL1ProviderClient, + ) -> Self { + L1ProviderStarter { config, l1_provider_client } + } + + pub async fn run(&mut self) -> Result<(), L1ProviderRunError> { + let mut ticker = time::interval(self.config.interval); + + loop { + ticker.tick().await; + if let Err(e) = self.l1_provider_client.start().await { + return Err(L1ProviderRunError::L1ProviderClientError(format!( + "Failed to call start: {e}" + ))); + } + } + } +} + +#[async_trait] +impl ComponentStarter for L1ProviderStarter { + async fn start(&mut self) -> Result<(), ComponentError> { + info!("Starting component {}.", type_name::()); + self.run().await.map_err(|_| ComponentError::InternalComponentError) + } +} + +pub fn create_l1_provider_start_er( + config: L1ProviderStarterConfig, + l1_provider_client: SharedL1ProviderClient, +) -> L1ProviderStarter { + L1ProviderStarter::new(config, l1_provider_client) +} diff --git a/crates/starknet_l1_provider_starter/src/lib.rs b/crates/starknet_l1_provider_starter/src/lib.rs new file mode 100644 index 0000000000..b2882262bc --- /dev/null +++ b/crates/starknet_l1_provider_starter/src/lib.rs @@ -0,0 +1,4 @@ +pub mod communication; +pub mod config; +pub mod errors; +pub mod l1_provider_starter;