Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/remove blobscan client #92

Merged
merged 2 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions Cargo.lock

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

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ edition = "2021"
[workspace]
members = [
"state-reconstruct-fetcher",
"state-reconstruct-fetcher/blobscan-client",
"state-reconstruct-storage",
]

[dependencies]
async-trait = "0.1.74"
bincode = "1"
blake2 = "0.10.6"
blobscan-client = { path = "./state-reconstruct-fetcher/blobscan-client" }
chrono = "0.4.31"
clap = { version = "4.4.7", features = ["derive", "env"] }
ethers = "1.0.2"
Expand Down
1 change: 0 additions & 1 deletion state-reconstruct-fetcher/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ edition = "2021"
[dependencies]
bincode = "1.3.3"
blake2 = "0.10.6"
blobscan-client = { path = "./blobscan-client" }
ethers = "1.0.2"
eyre = "0.6.8"
indexmap = { version = "2.0.2", features = ["serde"] }
Expand Down
14 changes: 0 additions & 14 deletions state-reconstruct-fetcher/blobscan-client/Cargo.toml

This file was deleted.

19 changes: 0 additions & 19 deletions state-reconstruct-fetcher/blobscan-client/src/blob_support.rs

This file was deleted.

4 changes: 0 additions & 4 deletions state-reconstruct-fetcher/blobscan-client/src/lib.rs

This file was deleted.

52 changes: 0 additions & 52 deletions state-reconstruct-fetcher/blobscan-client/src/scraping_support.rs

This file was deleted.

30 changes: 0 additions & 30 deletions state-reconstruct-fetcher/src/api_support.rs

This file was deleted.

38 changes: 30 additions & 8 deletions state-reconstruct-fetcher/src/blob_http_client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use blobscan_client::{BlobResponseFormatError, BlobSupport};
use serde::Deserialize;
use tokio::time::{sleep, Duration};

use crate::types::ParseError;
Expand All @@ -8,13 +8,18 @@ const MAX_RETRIES: u8 = 5;
/// The interval in seconds to wait before retrying to fetch a blob.
const FAILED_FETCH_RETRY_INTERVAL_S: u64 = 10;

#[derive(Deserialize)]
struct JsonResponse {
data: String,
}

pub struct BlobHttpClient {
client: reqwest::Client,
support: Box<dyn BlobSupport + Send + Sync>,
url_base: String,
}

impl BlobHttpClient {
pub fn new(support: Box<dyn BlobSupport + Send + Sync>) -> eyre::Result<Self> {
pub fn new(blob_url: String) -> eyre::Result<Self> {
let mut headers = reqwest::header::HeaderMap::new();
headers.insert(
"Accept",
Expand All @@ -23,28 +28,31 @@ impl BlobHttpClient {
let client = reqwest::Client::builder()
.default_headers(headers)
.build()?;
Ok(Self { client, support })
Ok(Self {
client,
url_base: blob_url,
})
}

pub async fn get_blob(&self, kzg_commitment: &[u8]) -> Result<Vec<u8>, ParseError> {
let url = self.support.format_url(kzg_commitment);
let url = self.format_url(kzg_commitment);
for attempt in 1..=MAX_RETRIES {
match self.client.get(&url).send().await {
Ok(response) => match response.text().await {
Ok(text) => match self.support.get_blob_data(&text) {
Ok(text) => match get_blob_data(&text) {
Ok(data) => {
let plain = if let Some(p) = data.strip_prefix("0x") {
p
} else {
&data
};
return hex::decode(plain).map_err(|e| {
BlobResponseFormatError(plain.to_string(), e.to_string()).into()
ParseError::BlobFormatError(plain.to_string(), e.to_string())
});
}
Err(e) => {
tracing::error!("failed parsing response of {url}");
return Err(e.into());
return Err(e);
}
},
Err(e) => {
Expand All @@ -60,4 +68,18 @@ impl BlobHttpClient {
}
Err(ParseError::BlobStorageError(url))
}

fn format_url(&self, kzg_commitment: &[u8]) -> String {
format!("{}0x{}", self.url_base, hex::encode(kzg_commitment))
}
}

fn get_blob_data(json_str: &str) -> Result<String, ParseError> {
match serde_json::from_str::<JsonResponse>(json_str) {
Ok(data) => Ok(data.data),
Err(e) => Err(ParseError::BlobFormatError(
json_str.to_string(),
e.to_string(),
)),
}
}
16 changes: 3 additions & 13 deletions state-reconstruct-fetcher/src/l1_fetcher.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{cmp, fs::File, future::Future, sync::Arc};

use blobscan_client::{BlobSupport, ScrapingSupport};
use ethers::{
abi::{Contract, Function},
prelude::*,
Expand All @@ -16,7 +15,6 @@ use tokio::{
use tokio_util::sync::CancellationToken;

use crate::{
api_support::ApiSupport,
blob_http_client::BlobHttpClient,
constants::ethereum::{BLOB_BLOCK, BLOCK_STEP, BOOJUM_BLOCK, GENESIS_BLOCK, ZK_SYNC_ADDR},
metrics::L1Metrics,
Expand Down Expand Up @@ -468,7 +466,7 @@ impl L1Fetcher {
) -> Result<tokio::task::JoinHandle<Option<u64>>> {
let metrics = self.metrics.clone();
let contracts = self.contracts.clone();
let client = BlobHttpClient::new(make_support(self.config.blobs_url.clone()))?;
let client = BlobHttpClient::new(self.config.blobs_url.clone())?;
Ok(tokio::spawn({
async move {
let mut boojum_mode = false;
Expand Down Expand Up @@ -507,8 +505,8 @@ impl L1Fetcher {
}
sleep(Duration::from_secs(LONG_POLLING_INTERVAL_S)).await;
}
ParseError::BlobFormatError(inner) => {
tracing::error!("Cannot parse {}: {}", inner.0, inner.1);
ParseError::BlobFormatError(data, inner) => {
tracing::error!("Cannot parse {}: {}", data, inner);
cancellation_token.cancel();
return last_block_number_processed;
}
Expand Down Expand Up @@ -564,14 +562,6 @@ impl L1Fetcher {
}
}

fn make_support(url: String) -> Box<dyn BlobSupport + Send + Sync> {
if url.starts_with("https://blobscan.com") {
Box::<ScrapingSupport>::default()
} else {
Box::new(ApiSupport::new(url))
}
}

pub async fn parse_calldata(
l1_block_number: u64,
commit_blocks_fn: &Function,
Expand Down
1 change: 0 additions & 1 deletion state-reconstruct-fetcher/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![feature(array_chunks)]
#![feature(iter_next_chunk)]

pub mod api_support;
pub mod blob_http_client;
pub mod constants;
pub mod l1_fetcher;
Expand Down
3 changes: 1 addition & 2 deletions state-reconstruct-fetcher/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use blobscan_client::BlobResponseFormatError;
use ethers::{abi, types::U256};
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -41,7 +40,7 @@ pub enum ParseError {
BlobStorageError(String),

#[error("blob format error: {0}")]
BlobFormatError(#[from] BlobResponseFormatError),
BlobFormatError(String, String),
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down