diff --git a/Cargo.lock b/Cargo.lock index 849e52446a..3d3c1db43c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10508,8 +10508,14 @@ dependencies = [ name = "starknet_l1_provider_types" version = "0.0.0" dependencies = [ + "async-trait", + "mockall", + "papyrus_proc_macros", "serde", + "starknet_api", + "starknet_sequencer_infra", "thiserror", + "tracing", ] [[package]] diff --git a/crates/starknet_l1_provider_types/Cargo.toml b/crates/starknet_l1_provider_types/Cargo.toml index 3af64c191b..726fd9eb20 100644 --- a/crates/starknet_l1_provider_types/Cargo.toml +++ b/crates/starknet_l1_provider_types/Cargo.toml @@ -5,9 +5,21 @@ edition.workspace = true repository.workspace = true license.workspace = true +[features] +testing = ["mockall"] + [dependencies] +async-trait.workspace = true +mockall = { workspace = true, optional = true } +papyrus_proc_macros.workspace = true serde.workspace = true +starknet_api.workspace = true +starknet_sequencer_infra.workspace = true thiserror.workspace = true +tracing.workspace = true + +[dev-dependencies] +mockall.workspace = true [lints] workspace = true diff --git a/crates/starknet_l1_provider_types/src/errors.rs b/crates/starknet_l1_provider_types/src/errors.rs index 087762af47..0aa9bbfbad 100644 --- a/crates/starknet_l1_provider_types/src/errors.rs +++ b/crates/starknet_l1_provider_types/src/errors.rs @@ -1,6 +1,7 @@ use std::fmt::Debug; use serde::{Deserialize, Serialize}; +use starknet_sequencer_infra::component_client::ClientError; use thiserror::Error; #[derive(Clone, Debug, Error, PartialEq, Eq, Serialize, Deserialize)] @@ -26,3 +27,11 @@ impl L1ProviderError { Self::UnexpectedProviderStateTransition { from: from.to_string(), to: to.to_string() } } } + +#[derive(Clone, Debug, Error)] +pub enum L1ProviderClientError { + #[error(transparent)] + ClientError(#[from] ClientError), + #[error(transparent)] + L1ProviderError(#[from] L1ProviderError), +} diff --git a/crates/starknet_l1_provider_types/src/lib.rs b/crates/starknet_l1_provider_types/src/lib.rs index c4be886d80..b3eec3e52c 100644 --- a/crates/starknet_l1_provider_types/src/lib.rs +++ b/crates/starknet_l1_provider_types/src/lib.rs @@ -1,8 +1,23 @@ pub mod errors; -use crate::errors::L1ProviderError; +use std::sync::Arc; + +use async_trait::async_trait; +#[cfg(any(feature = "testing", test))] +use mockall::automock; +use papyrus_proc_macros::handle_response_variants; +use serde::{Deserialize, Serialize}; +use starknet_api::executable_transaction::L1HandlerTransaction; +use starknet_api::transaction::TransactionHash; +use starknet_sequencer_infra::component_client::ClientError; +use starknet_sequencer_infra::component_definitions::ComponentClient; +use tracing::instrument; + +use crate::errors::{L1ProviderClientError, L1ProviderError}; pub type L1ProviderResult = Result; +pub type L1ProviderClientResult = Result; +pub type SharedL1ProviderClient = Arc; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum ValidationStatus { @@ -10,3 +25,47 @@ pub enum ValidationStatus { AlreadyIncludedOnL2, ConsumedOnL1OrUnknown, } + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub enum L1ProviderRequest { + GetTransactions(usize), +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub enum L1ProviderResponse { + GetTransactions(L1ProviderResult>), +} + +/// Serves as the mempool's shared interface. Requires `Send + Sync` to allow transferring and +/// sharing resources (inputs, futures) across threads. +#[cfg_attr(any(feature = "testing", test), automock)] +#[async_trait] +pub trait L1ProviderClient: Send + Sync { + async fn get_txs(&self, n_txs: usize) -> L1ProviderClientResult>; + async fn validate(&self, _tx_hash: TransactionHash) + -> L1ProviderClientResult; +} + +#[async_trait] +impl L1ProviderClient for ComponentClientType +where + ComponentClientType: Send + Sync + ComponentClient, +{ + #[instrument(skip(self))] + async fn get_txs(&self, n_txs: usize) -> L1ProviderClientResult> { + let request = L1ProviderRequest::GetTransactions(n_txs); + let response = self.send(request).await; + handle_response_variants!( + L1ProviderResponse, + GetTransactions, + L1ProviderClientError, + L1ProviderError + ) + } + async fn validate( + &self, + _tx_hash: TransactionHash, + ) -> L1ProviderClientResult { + todo!(); + } +}