Skip to content

Commit

Permalink
fix(core): fail fast if provider fails to provide a block
Browse files Browse the repository at this point in the history
  • Loading branch information
0xdeafbeef committed Aug 2, 2024
1 parent bcb6b73 commit 09ff720
Showing 1 changed file with 18 additions and 24 deletions.
42 changes: 18 additions & 24 deletions core/src/block_strider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ where
JoinTask::new(self.fetch_next_master_block(&self.state.load_last_mc_block_id()));

while let Some(next) = next_master_fut.await {
let next = next?;
// NOTE: Start fetching the next master block in parallel to the processing of the current one
next_master_fut = JoinTask::new(self.fetch_next_master_block(next.id()));

Expand Down Expand Up @@ -299,41 +300,34 @@ where
fn fetch_next_master_block(
&self,
prev_block_id: &BlockId,
) -> impl Future<Output = Option<BlockStuffAug>> + Send + 'static {
) -> impl Future<Output = OptionalBlockStuff> + Send + 'static {
let _histogram = HistogramGuard::begin("tycho_core_download_mc_block_time");

tracing::debug!(%prev_block_id, "fetching next master block");

let provider = self.provider.clone();
let prev_block_id = *prev_block_id;
async move {
loop {
match provider.get_next_block(&prev_block_id).await? {
Ok(block) => break Some(block),
Err(e) => {
tracing::error!(?prev_block_id, "error while fetching master block: {e:?}");
// TODO: backoff

// NOTE: Give some time to breathe to tokio
tokio::task::yield_now().await;
}
}
}
provider.get_next_block(&prev_block_id).await.map(|r| {
r.map_err(|e| {
e.context(format!(
"BUGGY PROVIDER. failed to fetch master block: {}",
prev_block_id
))
})
})
}
}

async fn fetch_block(&self, block_id: &BlockId) -> Result<BlockStuffAug> {
loop {
match self.provider.get_block(block_id).await {
Some(Ok(block)) => break Ok(block),
Some(Err(e)) => {
tracing::error!("error while fetching block: {e:?}");
tokio::task::yield_now().await;
// TODO: backoff
}
None => {
anyhow::bail!("block not found: {block_id}")
}
match self.provider.get_block(block_id).await {
Some(Ok(block)) => Ok(block),
Some(Err(e)) => Err(e.context(format!(
"BUGGY PROVIDER. failed to fetch block: {}",
block_id
))),
None => {
anyhow::bail!("block not found: {block_id}")
}
}
}
Expand Down

0 comments on commit 09ff720

Please sign in to comment.