-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
frame queue
- Loading branch information
Showing
3 changed files
with
74 additions
and
11 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,17 +1,80 @@ | ||
//! This module contains the [FrameQueue] stage of the derivation pipeline. | ||
use super::l1_retrieval::L1Retrieval; | ||
use crate::traits::{ChainProvider, DataAvailabilityProvider}; | ||
use alloc::collections::VecDeque; | ||
use crate::{ | ||
traits::{ChainProvider, DataAvailabilityProvider, ResettableStage}, | ||
types::{BlockInfo, Frame, SystemConfig}, | ||
}; | ||
use alloc::{boxed::Box, collections::VecDeque}; | ||
use alloy_primitives::Bytes; | ||
use anyhow::{anyhow, bail, Result}; | ||
use async_trait::async_trait; | ||
|
||
pub struct FrameQueue<T, DAP, CP> | ||
where | ||
T: Into<Bytes>, | ||
DAP: DataAvailabilityProvider, | ||
CP: ChainProvider, | ||
{ | ||
/// The previous stage in the pipeline. | ||
pub prev: L1Retrieval<T, DAP, CP>, | ||
/// The current frame queue. | ||
queue: VecDeque<Bytes>, | ||
queue: VecDeque<Frame>, | ||
} | ||
|
||
impl<T, DAP, CP> FrameQueue<T, DAP, CP> | ||
where | ||
T: Into<Bytes>, | ||
DAP: DataAvailabilityProvider, | ||
CP: ChainProvider, | ||
{ | ||
/// Create a new frame queue stage. | ||
pub fn new(prev: L1Retrieval<T, DAP, CP>) -> Self { | ||
Self { | ||
prev, | ||
queue: VecDeque::new(), | ||
} | ||
} | ||
|
||
/// Returns the L1 origin [BlockInfo]. | ||
pub fn origin(&self) -> Option<&BlockInfo> { | ||
self.prev.origin() | ||
} | ||
|
||
/// Fetches the next frame from the frame queue. | ||
pub async fn next_frame(&mut self) -> Result<Frame> { | ||
if self.queue.is_empty() { | ||
match self.prev.next_data().await { | ||
Ok(data) => { | ||
if let Ok(frames) = Frame::parse_frames(data.as_ref()) { | ||
self.queue.extend(frames); | ||
} | ||
} | ||
Err(e) => { | ||
bail!("Error fetching next data: {e}") | ||
} | ||
} | ||
} | ||
// If we did not add more frames but still have more data, retry this function. | ||
if self.queue.is_empty() { | ||
bail!("Not enough data"); | ||
} | ||
|
||
self.queue | ||
.pop_front() | ||
.ok_or_else(|| anyhow!("Frame queue is impossibly empty.")) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl<T, DAP, CP> ResettableStage for FrameQueue<T, DAP, CP> | ||
where | ||
T: Into<Bytes>, | ||
DAP: DataAvailabilityProvider + Send, | ||
CP: ChainProvider + Send, | ||
{ | ||
async fn reset(&mut self, base: BlockInfo, cfg: SystemConfig) -> Result<()> { | ||
self.queue = VecDeque::default(); | ||
Ok(()) | ||
} | ||
} |
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