Skip to content

Commit

Permalink
add wait_for_block to L1Client
Browse files Browse the repository at this point in the history
Use this method in validation and in `wait_for_l1_finalized`.
  • Loading branch information
tbro committed Oct 1, 2024
1 parent 4f47401 commit 064eda5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
54 changes: 29 additions & 25 deletions types/src/v0/impls/l1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,34 @@ impl L1Client {
L1Snapshot { head, finalized }
}

pub async fn wait_for_block(&self, number: u64) -> L1BlockInfo {
let interval = self.provider.get_interval();
loop {
let block = match self.provider.get_block(number).await {
Ok(Some(block)) => block,
Ok(None) => {
tracing::error!(number, "no such block");
sleep(interval).await;
continue;
}
Err(err) => {
tracing::error!(%err, number, "failed to get L1 block");
sleep(interval).await;
continue;
}
};
let Some(hash) = block.hash else {
tracing::error!(number, ?block, "L1 block has no hash");
sleep(interval).await;
continue;
};
break L1BlockInfo {
number,
hash,
timestamp: block.timestamp,
};
}
}
/// Get information about the given block.
///
/// If the desired block number is not finalized yet, this function will block until it becomes
Expand Down Expand Up @@ -107,31 +135,7 @@ impl L1Client {

// The finalized block may have skipped over the block of interest. In this case, our block
// is still finalized, since it is before the finalized block. We just need to fetch it.
loop {
let block = match self.provider.get_block(number).await {
Ok(Some(block)) => block,
Ok(None) => {
tracing::error!(number, "no such block");
sleep(interval).await;
continue;
}
Err(err) => {
tracing::error!(%err, number, "failed to get L1 block");
sleep(interval).await;
continue;
}
};
let Some(hash) = block.hash else {
tracing::error!(number, ?block, "L1 block has no hash");
sleep(interval).await;
continue;
};
break L1BlockInfo {
number,
hash,
timestamp: block.timestamp,
};
}
self.wait_for_block(number).await
}

/// Proxy to `Provider.get_block_number`.
Expand Down
9 changes: 2 additions & 7 deletions types/src/v0/impls/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,16 +714,11 @@ impl HotShotState<SeqTypes> for ValidatedState {
return Err(BlockError::InvalidBlockHeader);
}

let l1_head = instance
let _ = instance
.l1_client
.wait_for_finalized_block(proposed_header.l1_head())
.wait_for_block(proposed_header.l1_head())
.await;

if l1_head.number() != proposed_header.l1_head() {
tracing::error!("Invalid proposal: l1_head mismatch");
return Err(BlockError::InvalidBlockHeader);
}

// validate the proposal
if let Err(err) = validate_proposal(
&validated_state,
Expand Down

0 comments on commit 064eda5

Please sign in to comment.