Skip to content

Commit

Permalink
feat(starknet_l1_provider): add scraper
Browse files Browse the repository at this point in the history
- base layer constant file will hold the identifiers of Starknet Events.
- fetch_events will get events from base layer and send to provider
  • Loading branch information
Gilad Chase committed Jan 5, 2025
1 parent 2debbf9 commit e97dcf6
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 130 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/papyrus_base_layer/src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub type EventIdentifier = &'static str;
6 changes: 5 additions & 1 deletion crates/papyrus_base_layer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use std::error::Error;
use std::fmt::{Debug, Display};

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use starknet_api::block::BlockHashAndNumber;
use starknet_api::core::{ContractAddress, EntryPointSelector, EthAddress, Nonce};
use starknet_api::transaction::fields::{Calldata, Fee};
use starknet_api::transaction::L1HandlerTransaction;

pub mod constants;
pub mod ethereum_base_layer_contract;

pub(crate) mod eth_events;
Expand All @@ -18,7 +22,7 @@ mod base_layer_test;
/// Interface for getting data from the Starknet base contract.
#[async_trait]
pub trait BaseLayerContract {
type Error;
type Error: Error + Display + Debug;

/// Get the latest Starknet block that is proved on the base layer.
/// Optionally, require minimum confirmations.
Expand Down
1 change: 1 addition & 0 deletions crates/starknet_l1_provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ starknet_api.workspace = true
starknet_l1_provider_types.workspace = true
starknet_sequencer_infra.workspace = true
thiserror.workspace = true
tokio.workspace = true
tracing.workspace = true
validator.workspace = true

Expand Down
129 changes: 0 additions & 129 deletions crates/starknet_l1_provider/src/l1_provider_tests.rs

This file was deleted.

79 changes: 79 additions & 0 deletions crates/starknet_l1_provider/src/l1_scraper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use std::time::Duration;

use papyrus_base_layer::constants::EventIdentifier;
use papyrus_base_layer::BaseLayerContract;
use papyrus_config::converters::deserialize_seconds_to_duration;
use papyrus_config::validators::validate_ascii;
use serde::{Deserialize, Serialize};
use starknet_api::core::ChainId;
use starknet_l1_provider_types::SharedL1ProviderClient;
use thiserror::Error;
use tokio::time::sleep;
use tracing::error;
use validator::Validate;

type L1ScraperResult<T, B> = Result<T, L1ScraperError<B>>;

pub struct L1Scraper<B: BaseLayerContract> {
pub config: L1ScraperConfig,
pub base_layer: B,
pub last_block_number_processed: u64,
pub l1_provider_client: SharedL1ProviderClient,
_tracked_event_identifiers: Vec<EventIdentifier>,
}

impl<B: BaseLayerContract + Send + Sync> L1Scraper<B> {
pub fn new(
config: L1ScraperConfig,
l1_provider_client: SharedL1ProviderClient,
base_layer: B,
events_identifiers_to_track: &[EventIdentifier],
) -> Self {
Self {
l1_provider_client,
base_layer,
last_block_number_processed: config.l1_block_to_start_scraping_from,
config,
_tracked_event_identifiers: events_identifiers_to_track.to_vec(),
}
}

pub async fn fetch_events(&mut self) -> L1ScraperResult<(), B> {
todo!()
}

async fn _run(&mut self) -> L1ScraperResult<(), B> {
loop {
sleep(self.config.polling_interval).await;
// TODO: retry.
self.fetch_events().await?;
}
}
}

#[derive(Clone, Debug, Serialize, Deserialize, Validate, PartialEq)]
pub struct L1ScraperConfig {
pub l1_block_to_start_scraping_from: u64,
#[validate(custom = "validate_ascii")]
pub chain_id: ChainId,
pub finality: u64,
#[serde(deserialize_with = "deserialize_seconds_to_duration")]
pub polling_interval: Duration,
}

impl Default for L1ScraperConfig {
fn default() -> Self {
Self {
l1_block_to_start_scraping_from: 0,
chain_id: ChainId::Mainnet,
finality: 0,
polling_interval: Duration::from_secs(1),
}
}
}

#[derive(Error, Debug)]
pub enum L1ScraperError<T: BaseLayerContract + Send + Sync> {
#[error("Base layer error: {0}")]
BaseLayer(T::Error),
}
Empty file.
1 change: 1 addition & 0 deletions crates/starknet_l1_provider/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod communication;
pub mod errors;
pub mod l1_scraper;

#[cfg(test)]
pub mod test_utils;
Expand Down

0 comments on commit e97dcf6

Please sign in to comment.