Skip to content

Commit

Permalink
feat(starknet_l1_provider_types): add client
Browse files Browse the repository at this point in the history
  • Loading branch information
Gilad Chase committed Dec 10, 2024
1 parent 241ef4a commit f7f22e7
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Cargo.lock

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

12 changes: 12 additions & 0 deletions crates/starknet_l1_provider_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions crates/starknet_l1_provider_types/src/errors.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -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),
}
61 changes: 60 additions & 1 deletion crates/starknet_l1_provider_types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,71 @@
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<T> = Result<T, L1ProviderError>;
pub type L1ProviderClientResult<T> = Result<T, L1ProviderClientError>;
pub type SharedL1ProviderClient = Arc<dyn L1ProviderClient>;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ValidationStatus {
Validated,
AlreadyIncludedOnL2,
ConsumedOnL1OrUnknown,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum L1ProviderRequest {
GetTransactions(usize),
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum L1ProviderResponse {
GetTransactions(L1ProviderResult<Vec<L1HandlerTransaction>>),
}

/// 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<Vec<L1HandlerTransaction>>;
async fn validate(&self, _tx_hash: TransactionHash)
-> L1ProviderClientResult<ValidationStatus>;
}

#[async_trait]
impl<ComponentClientType> L1ProviderClient for ComponentClientType
where
ComponentClientType: Send + Sync + ComponentClient<L1ProviderRequest, L1ProviderResponse>,
{
#[instrument(skip(self))]
async fn get_txs(&self, n_txs: usize) -> L1ProviderClientResult<Vec<L1HandlerTransaction>> {
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<ValidationStatus> {
todo!();
}
}

0 comments on commit f7f22e7

Please sign in to comment.