-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from Blobscan/support-dual-indexing
feat: add reorgs support + indexer restructuring
- Loading branch information
Showing
25 changed files
with
1,217 additions
and
613 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
{ | ||
"editor.formatOnSave": true | ||
"editor.formatOnSave": true, | ||
"rust-analyzer.linkedProjects": ["./Cargo.toml"], | ||
"rust-analyzer.showUnlinkedFileNotification": false | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,20 @@ | ||
use clap::Parser; | ||
|
||
use crate::clients::beacon::types::BlockId; | ||
|
||
/// Blobscan's indexer for the EIP-4844 upgrade. | ||
#[derive(Parser, Debug)] | ||
#[command(author, version, about, long_about = None)] | ||
pub struct Args { | ||
/// Slot to start indexing from | ||
#[arg(short, long)] | ||
pub from_slot: Option<u32>, | ||
pub from_slot: Option<BlockId>, | ||
|
||
/// Number of threads used for parallel indexing | ||
#[arg(short, long)] | ||
pub num_threads: Option<u32>, | ||
|
||
/// Amount of slots to be processed before saving latest slot in the database | ||
#[arg(short, long, default_value_t = 1000)] | ||
pub slots_per_save: u32, | ||
#[arg(short, long)] | ||
pub slots_per_save: Option<u32>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,87 @@ | ||
use anyhow::Context as AnyhowContext; | ||
use backoff::ExponentialBackoff; | ||
use reqwest::{Client, Url}; | ||
use std::time::Duration; | ||
use reqwest_eventsource::EventSource; | ||
|
||
use crate::{clients::common::ClientResult, json_get}; | ||
use crate::{ | ||
clients::{beacon::types::BlockHeaderResponse, common::ClientResult}, | ||
json_get, | ||
}; | ||
|
||
use self::types::{Blob, BlobsResponse, BlockMessage as Block, BlockResponse}; | ||
use self::types::{Blob, BlobsResponse, Block, BlockHeader, BlockId, BlockResponse, Topic}; | ||
|
||
pub mod types; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct BeaconClient { | ||
base_url: Url, | ||
client: Client, | ||
exp_backoff: Option<ExponentialBackoff>, | ||
} | ||
|
||
pub struct Config { | ||
pub base_url: String, | ||
pub timeout: Option<Duration>, | ||
pub exp_backoff: Option<ExponentialBackoff>, | ||
} | ||
|
||
impl BeaconClient { | ||
pub fn try_with_client(client: Client, config: Config) -> ClientResult<Self> { | ||
let base_url = Url::parse(&format!("{}/eth/", config.base_url)) | ||
.with_context(|| "Failed to parse base URL")?; | ||
let exp_backoff = config.exp_backoff; | ||
|
||
Ok(Self { base_url, client }) | ||
Ok(Self { | ||
base_url, | ||
client, | ||
exp_backoff, | ||
}) | ||
} | ||
|
||
pub async fn get_block(&self, block_id: &BlockId) -> ClientResult<Option<Block>> { | ||
let path = format!("v2/beacon/blocks/{block_id}"); | ||
let url = self.base_url.join(path.as_str())?; | ||
|
||
json_get!(&self.client, url, BlockResponse, self.exp_backoff.clone()).map(|res| match res { | ||
Some(r) => Some(r.data), | ||
None => None, | ||
}) | ||
} | ||
|
||
pub async fn get_block(&self, slot: Option<u32>) -> ClientResult<Option<Block>> { | ||
let slot = match slot { | ||
Some(slot) => slot.to_string(), | ||
None => String::from("head"), | ||
}; | ||
let path = format!("v2/beacon/blocks/{slot}"); | ||
pub async fn get_block_header(&self, block_id: &BlockId) -> ClientResult<Option<BlockHeader>> { | ||
let path = format!("v1/beacon/headers/{block_id}"); | ||
let url = self.base_url.join(path.as_str())?; | ||
|
||
json_get!(&self.client, url, BlockResponse).map(|res| match res { | ||
Some(r) => Some(r.data.message), | ||
json_get!( | ||
&self.client, | ||
url, | ||
BlockHeaderResponse, | ||
self.exp_backoff.clone() | ||
) | ||
.map(|res| match res { | ||
Some(r) => Some(r.data), | ||
None => None, | ||
}) | ||
} | ||
|
||
pub async fn get_blobs(&self, slot: u32) -> ClientResult<Option<Vec<Blob>>> { | ||
let path = format!("v1/beacon/blob_sidecars/{slot}"); | ||
pub async fn get_blobs(&self, block_id: &BlockId) -> ClientResult<Option<Vec<Blob>>> { | ||
let path = format!("v1/beacon/blob_sidecars/{block_id}"); | ||
let url = self.base_url.join(path.as_str())?; | ||
|
||
json_get!(&self.client, url, BlobsResponse).map(|res| match res { | ||
json_get!(&self.client, url, BlobsResponse, self.exp_backoff.clone()).map(|res| match res { | ||
Some(r) => Some(r.data), | ||
None => None, | ||
}) | ||
} | ||
|
||
pub fn subscribe_to_events(&self, topics: Vec<Topic>) -> ClientResult<EventSource> { | ||
let topics = topics | ||
.iter() | ||
.map(|topic| topic.into()) | ||
.collect::<Vec<String>>() | ||
.join("&"); | ||
let path = format!("v1/events?topics={topics}"); | ||
let url = self.base_url.join(&path)?; | ||
|
||
Ok(EventSource::get(url)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.