-
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.
feat(derive): Channel Reader Implementation (#65)
* feat(derive): batch type for the channel reader * fix(derive): batch type lints * feat(derive): channel reader implementation with batch reader * fix(derive): channel bank impl * Update crates/derive/src/types/batch_type.rs Co-authored-by: clabby <[email protected]> * fix(derive): channel reader fixes * fix(derive): revert unfurrling change * fix(derive): batch decoding --------- Co-authored-by: clabby <[email protected]>
- Loading branch information
Showing
9 changed files
with
264 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 +1,111 @@ | ||
//! This module contains the `ChannelReader` struct. | ||
use super::channel_bank::ChannelBank; | ||
use crate::{ | ||
traits::{ChainProvider, DataAvailabilityProvider}, | ||
types::{Batch, BlockInfo, StageError, StageResult}, | ||
}; | ||
use alloc::vec::Vec; | ||
use anyhow::anyhow; | ||
use core::fmt::Debug; | ||
use miniz_oxide::inflate::decompress_to_vec; | ||
|
||
/// [ChannelReader] is a stateful stage that does the following: | ||
#[derive(Debug)] | ||
pub struct ChannelReader<DAP, CP> | ||
where | ||
DAP: DataAvailabilityProvider + Debug, | ||
CP: ChainProvider + Debug, | ||
{ | ||
/// The previous stage of the derivation pipeline. | ||
prev: ChannelBank<DAP, CP>, | ||
/// The batch reader. | ||
next_batch: Option<BatchReader>, | ||
} | ||
|
||
impl<DAP, CP> ChannelReader<DAP, CP> | ||
where | ||
DAP: DataAvailabilityProvider + Debug, | ||
CP: ChainProvider + Debug, | ||
{ | ||
/// Create a new [ChannelReader] stage. | ||
pub fn new(prev: ChannelBank<DAP, CP>) -> Self { | ||
Self { | ||
prev, | ||
next_batch: None, | ||
} | ||
} | ||
|
||
/// Pulls out the next Batch from the available channel. | ||
pub async fn next_batch(&mut self) -> StageResult<Batch> { | ||
if let Err(e) = self.set_batch_reader().await { | ||
self.next_channel(); | ||
return Err(e); | ||
} | ||
match self | ||
.next_batch | ||
.as_mut() | ||
.unwrap() | ||
.next_batch() | ||
.ok_or(StageError::NotEnoughData) | ||
{ | ||
Ok(batch) => Ok(batch), | ||
Err(e) => { | ||
self.next_channel(); | ||
Err(e) | ||
} | ||
} | ||
} | ||
|
||
/// Creates the batch reader from available channel data. | ||
async fn set_batch_reader(&mut self) -> StageResult<()> { | ||
if self.next_batch.is_none() { | ||
let channel = self.prev.next_data().await?.ok_or(anyhow!("no channel"))?; | ||
self.next_batch = Some(BatchReader::from(&channel[..])); | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// Returns the L1 origin [BlockInfo]. | ||
pub fn origin(&self) -> Option<&BlockInfo> { | ||
self.prev.origin() | ||
} | ||
|
||
/// Forces the read to continue with the next channel, resetting any | ||
/// decoding / decompression state to a fresh start. | ||
pub fn next_channel(&mut self) { | ||
self.next_batch = None; | ||
} | ||
} | ||
|
||
/// Batch Reader provides a function that iteratively consumes batches from the reader. | ||
/// The L1Inclusion block is also provided at creation time. | ||
/// Warning: the batch reader can read every batch-type. | ||
/// The caller of the batch-reader should filter the results. | ||
#[derive(Debug)] | ||
pub(crate) struct BatchReader { | ||
/// The raw data to decode. | ||
data: Option<Vec<u8>>, | ||
/// Decompressed data. | ||
decompressed: Vec<u8>, | ||
} | ||
|
||
impl BatchReader { | ||
/// Pulls out the next batch from the reader. | ||
pub(crate) fn next_batch(&mut self) -> Option<Batch> { | ||
if let Some(data) = self.data.take() { | ||
self.decompressed = decompress_to_vec(&data).ok()?; | ||
} | ||
let batch = Batch::decode(&mut self.decompressed.as_ref()).ok()?; | ||
Some(batch) | ||
} | ||
} | ||
|
||
impl From<&[u8]> for BatchReader { | ||
fn from(data: &[u8]) -> Self { | ||
Self { | ||
data: Some(data.to_vec()), | ||
decompressed: Vec::new(), | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//! This module contains the enumerable [Batch]. | ||
use super::batch_type::BatchType; | ||
use super::single_batch::SingleBatch; | ||
use crate::types::errors::DecodeError; | ||
|
||
use alloy_rlp::Decodable; | ||
|
||
// TODO: replace this with a span batch | ||
/// Span Batch. | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub struct SpanBatch {} | ||
|
||
/// A Batch. | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub enum Batch { | ||
/// A single batch | ||
Single(SingleBatch), | ||
/// Span Batches | ||
Span(SpanBatch), | ||
} | ||
|
||
impl Batch { | ||
/// Attempts to decode a batch from a byte slice. | ||
pub fn decode(r: &mut &[u8]) -> Result<Self, DecodeError> { | ||
if r.is_empty() { | ||
return Err(DecodeError::EmptyBuffer); | ||
} | ||
match BatchType::from(r[0]) { | ||
BatchType::Single => { | ||
let single_batch = SingleBatch::decode(r)?; | ||
Ok(Batch::Single(single_batch)) | ||
} | ||
BatchType::Span => { | ||
// TODO: implement span batch decoding | ||
unimplemented!() | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
//! Contains the [BatchType] and its encodings. | ||
use alloy_rlp::{Decodable, Encodable}; | ||
|
||
/// The single batch type identifier. | ||
pub(crate) const SINGLE_BATCH_TYPE: u8 = 0x01; | ||
|
||
/// The span batch type identifier. | ||
pub(crate) const SPAN_BATCH_TYPE: u8 = 0x02; | ||
|
||
/// The Batch Type. | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
#[repr(u8)] | ||
pub enum BatchType { | ||
/// Single Batch. | ||
Single = SINGLE_BATCH_TYPE, | ||
/// Span Batch. | ||
Span = SPAN_BATCH_TYPE, | ||
} | ||
|
||
impl From<u8> for BatchType { | ||
fn from(val: u8) -> Self { | ||
match val { | ||
SINGLE_BATCH_TYPE => BatchType::Single, | ||
SPAN_BATCH_TYPE => BatchType::Span, | ||
_ => panic!("Invalid batch type"), | ||
} | ||
} | ||
} | ||
|
||
impl From<&[u8]> for BatchType { | ||
fn from(buf: &[u8]) -> Self { | ||
BatchType::from(buf[0]) | ||
} | ||
} | ||
|
||
impl Encodable for BatchType { | ||
fn encode(&self, out: &mut dyn alloy_rlp::BufMut) { | ||
let val = match self { | ||
BatchType::Single => SINGLE_BATCH_TYPE, | ||
BatchType::Span => SPAN_BATCH_TYPE, | ||
}; | ||
val.encode(out); | ||
} | ||
} | ||
|
||
impl Decodable for BatchType { | ||
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> { | ||
let val = u8::decode(buf)?; | ||
Ok(BatchType::from(val)) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
use alloc::vec::Vec; | ||
|
||
#[test] | ||
fn test_batch_type() { | ||
let batch_type = BatchType::Single; | ||
let mut buf = Vec::new(); | ||
batch_type.encode(&mut buf); | ||
let decoded = BatchType::decode(&mut buf.as_slice()).unwrap(); | ||
assert_eq!(batch_type, decoded); | ||
} | ||
} |
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