From 0b3f420d7229c700dbfe2c071415b91ad574fd61 Mon Sep 17 00:00:00 2001 From: Gilad Chase Date: Tue, 10 Dec 2024 14:42:06 +0200 Subject: [PATCH] chore(starknet_l1_provider): getting things ready for the client Adding provider-side boilerplate for provider-batcher communication --- Cargo.lock | 5 +++ crates/starknet_l1_provider/Cargo.toml | 8 +++++ .../starknet_l1_provider/src/communication.rs | 31 +++++++++++++++++++ crates/starknet_l1_provider/src/lib.rs | 30 +++++++++++------- 4 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 crates/starknet_l1_provider/src/communication.rs diff --git a/Cargo.lock b/Cargo.lock index c4269a4fb9..09d78918e7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10496,12 +10496,17 @@ name = "starknet_l1_provider" version = "0.0.0" dependencies = [ "assert_matches", + "async-trait", "indexmap 2.6.0", "papyrus_base_layer", "pretty_assertions", + "serde", "starknet_api", "starknet_l1_provider_types", + "starknet_sequencer_infra", "thiserror", + "tracing", + "validator", ] [[package]] diff --git a/crates/starknet_l1_provider/Cargo.toml b/crates/starknet_l1_provider/Cargo.toml index 3006d7b3ea..60a979bbd1 100644 --- a/crates/starknet_l1_provider/Cargo.toml +++ b/crates/starknet_l1_provider/Cargo.toml @@ -5,12 +5,20 @@ edition.workspace = true repository.workspace = true license.workspace = true +[features] +testing = [] + [dependencies] +async-trait.workspace = true indexmap.workspace = true papyrus_base_layer.workspace = true +serde.workspace = true starknet_api.workspace = true starknet_l1_provider_types.workspace = true +starknet_sequencer_infra.workspace = true thiserror.workspace = true +tracing.workspace = true +validator.workspace = true [dev-dependencies] assert_matches.workspace = true diff --git a/crates/starknet_l1_provider/src/communication.rs b/crates/starknet_l1_provider/src/communication.rs new file mode 100644 index 0000000000..ced6603500 --- /dev/null +++ b/crates/starknet_l1_provider/src/communication.rs @@ -0,0 +1,31 @@ +use async_trait::async_trait; +use starknet_l1_provider_types::{L1ProviderRequest, L1ProviderResponse}; +use starknet_sequencer_infra::component_client::{LocalComponentClient, RemoteComponentClient}; +use starknet_sequencer_infra::component_definitions::{ + ComponentRequestAndResponseSender, + ComponentRequestHandler, +}; +use starknet_sequencer_infra::component_server::{LocalComponentServer, RemoteComponentServer}; +use tracing::instrument; + +use crate::L1Provider; + +pub type LocalL1ProviderServer = + LocalComponentServer; +pub type RemoteL1ProviderServer = RemoteComponentServer; +pub type L1ProviderRequestAndResponseSender = + ComponentRequestAndResponseSender; +pub type LocalL1ProviderClient = LocalComponentClient; +pub type RemoteL1ProviderClient = RemoteComponentClient; + +#[async_trait] +impl ComponentRequestHandler for L1Provider { + #[instrument(skip(self))] + async fn handle_request(&mut self, request: L1ProviderRequest) -> L1ProviderResponse { + match request { + L1ProviderRequest::GetTransactions(n_txs) => { + L1ProviderResponse::GetTransactions(self.get_txs(n_txs)) + } + } + } +} diff --git a/crates/starknet_l1_provider/src/lib.rs b/crates/starknet_l1_provider/src/lib.rs index 207de91693..cd3b7d8f75 100644 --- a/crates/starknet_l1_provider/src/lib.rs +++ b/crates/starknet_l1_provider/src/lib.rs @@ -1,13 +1,17 @@ +pub mod communication; pub mod errors; #[cfg(test)] pub mod test_utils; use indexmap::{IndexMap, IndexSet}; +use serde::{Deserialize, Serialize}; use starknet_api::executable_transaction::L1HandlerTransaction; use starknet_api::transaction::TransactionHash; use starknet_l1_provider_types::errors::L1ProviderError; use starknet_l1_provider_types::{L1ProviderResult, ValidationStatus}; +use starknet_sequencer_infra::component_definitions::ComponentStarter; +use validator::Validate; #[cfg(test)] #[path = "l1_provider_tests.rs"] @@ -24,11 +28,8 @@ pub struct L1Provider { } impl L1Provider { - pub async fn new(_config: L1ProviderConfig) -> L1ProviderResult { - todo!( - "init crawler to start next crawl from ~1 hour ago, this can have l1 errors when \ - finding the latest block on L1 to 'subtract' 1 hour from." - ); + pub fn new(_config: L1ProviderConfig) -> L1ProviderResult { + todo!("Init crawler in uninitialized_state from config, to initialize call `reset`."); } /// Retrieves up to `n_txs` transactions that have yet to be proposed or accepted on L2. @@ -75,8 +76,8 @@ impl L1Provider { /// Simple recovery from L1 and L2 reorgs by reseting the service, which rewinds L1 and L2 /// information. - pub fn handle_reorg(&mut self) -> L1ProviderResult<()> { - self.reset() + pub async fn handle_reorg(&mut self) -> L1ProviderResult<()> { + self.reset().await } // TODO: this will likely change during integration with infra team. @@ -88,15 +89,18 @@ impl L1Provider { ) } - fn reset(&mut self) -> L1ProviderResult<()> { + pub async fn reset(&mut self) -> L1ProviderResult<()> { todo!( "resets internal buffers and rewinds the internal crawler _pointer_ back for ~1 \ hour,so that the main loop will start collecting from that time gracefully. May hit \ - base layer errors." + base layer errors when finding the latest block on l1 to 'subtract' 1 hour from. \ + Then, transition to Pending." ); } } +impl ComponentStarter for L1Provider {} + #[derive(Debug, Default)] struct TransactionManager { txs: IndexMap, @@ -173,8 +177,8 @@ impl ProviderState { match self { ProviderState::Pending => "Pending", ProviderState::Propose => "Propose", + ProviderState::Uninitialized => "Uninitialized", ProviderState::Validate => "Validate", - ProviderState::Uninitialized => "Validate", } } } @@ -185,5 +189,9 @@ impl std::fmt::Display for ProviderState { } } -#[derive(Debug)] +#[derive(Clone, Debug, Default, Serialize, Deserialize, Validate, PartialEq)] pub struct L1ProviderConfig; + +pub fn create_l1_provider(_config: L1ProviderConfig) -> L1Provider { + L1Provider { state: ProviderState::Propose, ..Default::default() } +}