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

Handle reorgs #51

Closed
wants to merge 2 commits into from
Closed
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
45 changes: 45 additions & 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ ethers = "1.0.2"
futures = "0.3.25"
hex = "0.4.3"
reqwest = { version = "0.11.13", features = ["json"] }
reqwest-eventsource = "0.5.0"
url = { version = "2.3.1", features = ["serde"] }
serde = { version = "1.0.150", features = ["derive"] }
tokio = { version = "1.23.0", features = ["full"] }
Expand Down
103 changes: 103 additions & 0 deletions src/chain_reorg_handler/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
use anyhow::Result;
use futures::{future::join_all, StreamExt};
use reqwest_eventsource::Event;
use tokio::task::JoinHandle;
use tracing::{debug, error, Instrument};

use crate::{
clients::beacon::types::{ChainReorgResponse, Topic},
context::Context,
slots_processor::slot_processor::SlotProcessor,
};

pub struct ChainReorgHandler {
context: Context,
}

impl ChainReorgHandler {
pub fn new(context: Context) -> ChainReorgHandler {
Self { context }
}

pub fn run(&self) -> Result<JoinHandle<Result<(), anyhow::Error>>> {
let thread_context = self.context.clone();

let handle = tokio::spawn(async move {
let blobscan_client = thread_context.blobscan_client();
let beacon_client = thread_context.beacon_client();
let mut event_source = match beacon_client.subscribe_to_events(vec![Topic::ChainReorg])
{
Ok(es) => es,
Err(error) => {
error!(
target = "chain_reorg_handler",
?error,
"Failed to subscribe to chain reorg events"
);

return Err(error.into());
}
};

while let Some(event) = event_source.next().await {
match event {
Ok(Event::Open) => debug!(
target = "chain_reorg_handler",
"Listening to chain reorg events…"
),
Ok(Event::Message(message)) => {
let chain_reorg_data =
serde_json::from_str::<ChainReorgResponse>(&message.data)?;

let new_head_slot = chain_reorg_data.slot.parse::<u32>()?;
let reorg_depth = chain_reorg_data.depth.parse::<u32>()?;

blobscan_client
.handle_reorg(new_head_slot, reorg_depth)
.await?;

// reprocess reorganized slots
let slot_processor = SlotProcessor::new(thread_context.clone());

let mut futures = Vec::new();

for slot in new_head_slot..new_head_slot + reorg_depth {
let slot_span = tracing::trace_span!("slot_processor");

futures.push(slot_processor.process_slot(slot).instrument(slot_span));
}

let results = join_all(futures).await;

for result in results {
if let Err(error) = result {
error!(
target = "chain_reorg_handler",
?error,
"Failed to process reorg slot"
);

return Err(error.into());
}
}
}
Err(e) => {
error!(
target = "chain_reorg_handler",
?e,
"Failed to receive chain reorg event"
);

event_source.close();

return Err(e.into());
}
}
}

Ok(())
});

Ok(handle)
}
}
17 changes: 16 additions & 1 deletion src/clients/beacon/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use anyhow::Context as AnyhowContext;
use reqwest::{Client, Url};
use reqwest_eventsource::EventSource;
use std::time::Duration;

use crate::{clients::common::ClientResult, json_get};

use self::types::{Blob, BlobsResponse, BlockMessage as Block, BlockResponse};
use self::types::{Blob, BlobsResponse, BlockMessage as Block, BlockResponse, Topic};

pub mod types;

Expand Down Expand Up @@ -50,4 +51,18 @@ impl BeaconClient {
None => None,
})
}

pub fn subscribe_to_events(&self, topics: Vec<Topic>) -> ClientResult<EventSource> {
let topics = topics
.iter()
.map(|topic| topic.to_string())
.collect::<Vec<String>>()
.join("&");
let path = format!("v1/events?topics={topics}");
let url = self.base_url.join(&path)?;

let event_source = EventSource::get(url);

Ok(event_source)
}
}
20 changes: 20 additions & 0 deletions src/clients/beacon/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
use ethers::types::{Bytes, H256};
use serde::Deserialize;

pub enum Topic {
ChainReorg,
}

impl Topic {
pub fn to_string(&self) -> String {
match self {
Topic::ChainReorg => String::from("chain_reorg"),
}
}

Check failure on line 13 in src/clients/beacon/types.rs

View workflow job for this annotation

GitHub Actions / lint

implementation of inherent method `to_string(&self) -> String` for type `clients::beacon::types::Topic`

error: implementation of inherent method `to_string(&self) -> String` for type `clients::beacon::types::Topic` --> src/clients/beacon/types.rs:9:5 | 9 | / pub fn to_string(&self) -> String { 10 | | match self { 11 | | Topic::ChainReorg => String::from("chain_reorg"), 12 | | } 13 | | } | |_____^ | = help: implement trait `Display` for type `clients::beacon::types::Topic` instead = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#inherent_to_string = note: `-D clippy::inherent-to-string` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::inherent_to_string)]`
}

#[derive(Deserialize, Debug)]
pub struct ExecutionPayload {
pub block_hash: H256,
Expand Down Expand Up @@ -38,3 +50,11 @@
pub struct BlobsResponse {
pub data: Vec<Blob>,
}

#[derive(Deserialize, Debug)]
pub struct ChainReorgResponse {
pub slot: String,
pub depth: String,
pub old_head_block: String,
pub new_head_block: String,
}
14 changes: 12 additions & 2 deletions src/clients/blobscan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use crate::{clients::common::ClientResult, json_get, json_put};

use self::{
jwt_manager::{Config as JWTManagerConfig, JWTManager},
types::{Blob, Block, IndexRequest, SlotRequest, SlotResponse, Transaction},
types::{
Blob, Block, IndexRequest, ReorgedSlotRequest, SlotRequest, SlotResponse, Transaction,
},
};

mod jwt_manager;
Expand Down Expand Up @@ -58,10 +60,18 @@ impl BlobscanClient {
json_put!(&self.client, url, token, &req).map(|_: Option<()>| ())
}

pub async fn handle_reorg(&self, slot: u32, depth: u32) -> ClientResult<()> {
let url = self.base_url.join("reorged-slot")?;
let token = self.jwt_manager.get_token()?;
let req = ReorgedSlotRequest { slot, depth };

json_put!(&self.client, url, token, &req).map(|_: Option<()>| ())
}

pub async fn update_slot(&self, slot: u32) -> ClientResult<()> {
let url = self.base_url.join("slot")?;
let token = self.jwt_manager.get_token()?;
let req = SlotRequest { slot };
let url = self.base_url.join("slot")?;

json_put!(&self.client, url, token, &req).map(|_: Option<()>| ())
}
Expand Down
6 changes: 6 additions & 0 deletions src/clients/blobscan/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ pub struct IndexRequest {
pub blobs: Vec<Blob>,
}

#[derive(Serialize, Debug)]
pub struct ReorgedSlotRequest {
pub slot: u32,
pub depth: u32,
}

impl fmt::Debug for Blob {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
Expand Down
5 changes: 5 additions & 0 deletions src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use tracing::{debug, error, info, warn, Instrument};

use crate::{
args::Args,
chain_reorg_handler::ChainReorgHandler,
context::{Config as ContextConfig, Context},
env::Environment,
slots_processor::{Config as SlotsProcessorConfig, SlotsProcessor},
Expand Down Expand Up @@ -58,6 +59,10 @@ pub async fn run(env: Environment) -> Result<()> {
}
};

let chain_reorg_handler = ChainReorgHandler::new(context.clone());

let _handler = chain_reorg_handler.run();

let beacon_client = context.beacon_client();
let blobscan_client = context.blobscan_client();

Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use env::Environment;
use utils::telemetry::{get_subscriber, init_subscriber};

mod args;
mod chain_reorg_handler;
mod clients;
mod context;
mod env;
Expand Down
2 changes: 1 addition & 1 deletion src/slots_processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use self::{
use crate::context::Context;

mod error;
mod slot_processor;
pub mod slot_processor;

pub struct SlotsProcessor {
context: Context,
Expand Down
Loading