Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(l1-provider): build l1 provider starter #2152

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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" }
Expand Down
20 changes: 20 additions & 0 deletions crates/starknet_l1_provider_starter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions crates/starknet_l1_provider_starter/src/communication.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use starknet_sequencer_infra::component_server::WrapperServer;

use crate::l1_provider_starter::L1ProviderStarter as L1ProviderStarterComponent;

pub type L1ProviderStarter = WrapperServer<L1ProviderStarterComponent>;
30 changes: 30 additions & 0 deletions crates/starknet_l1_provider_starter/src/config.rs
Original file line number Diff line number Diff line change
@@ -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<ParamPath, SerializedParam> {
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) }
}
}
7 changes: 7 additions & 0 deletions crates/starknet_l1_provider_starter/src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use thiserror::Error;

#[derive(Debug, Error)]
pub enum L1ProviderRunError {
#[error("L1 provider client failed to start: {0}")]
L1ProviderClientError(String),
}
53 changes: 53 additions & 0 deletions crates/starknet_l1_provider_starter/src/l1_provider_starter.rs
Original file line number Diff line number Diff line change
@@ -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>());
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)
}
4 changes: 4 additions & 0 deletions crates/starknet_l1_provider_starter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod communication;
pub mod config;
pub mod errors;
pub mod l1_provider_starter;
Loading