Skip to content

Commit

Permalink
feat: allow the use of block IDs for the --from-slot flag.
Browse files Browse the repository at this point in the history
  • Loading branch information
PJColombo committed Feb 2, 2024
1 parent 239345b commit 3dc2c9a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/args.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
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)]
Expand Down
21 changes: 19 additions & 2 deletions src/clients/beacon/types.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::fmt;
use std::{fmt, str::FromStr};

use ethers::types::{Bytes, H256};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Debug)]
#[derive(Serialize, Debug, Clone)]
pub enum BlockId {
Head,
Finalized,
Expand Down Expand Up @@ -103,6 +103,23 @@ impl fmt::Display for BlockId {
}
}

impl FromStr for BlockId {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"head" => Ok(BlockId::Head),
"finalized" => Ok(BlockId::Finalized),
_ => match s.parse::<u32>() {
Ok(num) => Ok(BlockId::Slot(num)),
Err(_) => {
Err("Invalid block ID. Expected 'head', 'finalized' or a number.".to_string())
}
},
}
}
}

impl From<&Topic> for String {
fn from(value: &Topic) -> Self {
match value {
Expand Down
10 changes: 5 additions & 5 deletions src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,29 @@ impl Indexer {
})
}

pub async fn run(&mut self, start_slot: Option<u32>) -> AnyhowResult<()> {
pub async fn run(&mut self, start_block_id: Option<BlockId>) -> AnyhowResult<()> {
let beacon_client = self.context.beacon_client();
let blobscan_client = self.context.blobscan_client();
let mut event_source = beacon_client.subscribe_to_events(vec![Topic::Head])?;

let current_slot = match start_slot {
let current_block_id = match start_block_id {
Some(start_slot) => start_slot,
None => match blobscan_client.get_slot().await {
Err(error) => {
error!(target = "indexer", ?error, "Failed to fetch latest slot");

return Err(error.into());
}
Ok(res) => match res {
Ok(res) => BlockId::Slot(match res {
Some(latest_slot) => latest_slot + 1,
None => 0,
},
}),
},
};

let finalized_block_header = self
.synchronizer
.run(&BlockId::Slot(current_slot), &BlockId::Finalized)
.run(&current_block_id, &BlockId::Finalized)
.await?;

// We disable parallel processing for better handling of possible reorgs
Expand Down

0 comments on commit 3dc2c9a

Please sign in to comment.