Skip to content

Commit

Permalink
chore(starknet_l1_provider): getting things ready for the client
Browse files Browse the repository at this point in the history
Adding provider-side boilerplate for provider-batcher communication
  • Loading branch information
Gilad Chase committed Dec 12, 2024
1 parent dc8ca7a commit 0b3f420
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 11 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

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

8 changes: 8 additions & 0 deletions crates/starknet_l1_provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions crates/starknet_l1_provider/src/communication.rs
Original file line number Diff line number Diff line change
@@ -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<L1Provider, L1ProviderRequest, L1ProviderResponse>;
pub type RemoteL1ProviderServer = RemoteComponentServer<L1ProviderRequest, L1ProviderResponse>;
pub type L1ProviderRequestAndResponseSender =
ComponentRequestAndResponseSender<L1ProviderRequest, L1ProviderResponse>;
pub type LocalL1ProviderClient = LocalComponentClient<L1ProviderRequest, L1ProviderResponse>;
pub type RemoteL1ProviderClient = RemoteComponentClient<L1ProviderRequest, L1ProviderResponse>;

#[async_trait]
impl ComponentRequestHandler<L1ProviderRequest, L1ProviderResponse> 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))
}
}
}
}
30 changes: 19 additions & 11 deletions crates/starknet_l1_provider/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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"]
Expand All @@ -24,11 +28,8 @@ pub struct L1Provider {
}

impl L1Provider {
pub async fn new(_config: L1ProviderConfig) -> L1ProviderResult<Self> {
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<Self> {
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.
Expand Down Expand Up @@ -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.
Expand All @@ -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<TransactionHash, L1HandlerTransaction>,
Expand Down Expand Up @@ -173,8 +177,8 @@ impl ProviderState {
match self {
ProviderState::Pending => "Pending",
ProviderState::Propose => "Propose",
ProviderState::Uninitialized => "Uninitialized",
ProviderState::Validate => "Validate",
ProviderState::Uninitialized => "Validate",
}
}
}
Expand All @@ -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() }
}

0 comments on commit 0b3f420

Please sign in to comment.