Skip to content

Commit

Permalink
init channel bank
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby committed Feb 25, 2024
1 parent 68bb004 commit d373c17
Show file tree
Hide file tree
Showing 12 changed files with 268 additions and 21 deletions.
49 changes: 49 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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ verify an [L2 output root][g-output-root] from the L1 inputs it was [derived fro

- [`common`](./crates/common): A suite of utilities for developing `client` programs to be ran on top of Fault Proof VMs.
- [`preimage`](./crates/preimage): High level interfaces to the [`PreimageOracle`][fpp-specs] ABI
- [`derive`](./crates/derive): `no_std` compatible implementation of the [derivation pipeline][g-derivation-pipeline].

## Book

Expand Down
1 change: 1 addition & 0 deletions crates/derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ alloy-primitives = { version = "0.6.3", default-features = false, features = ["r
alloy-rlp = { version = "0.3.4", default-features = false, features = ["derive"] }
alloy-sol-types = { version = "0.6.3", default-features = false }
async-trait = "0.1.77"
hashbrown = "0.14.3"

# Optional
serde = { version = "1.0.197", default-features = false, features = ["derive"], optional = true }
Expand Down
3 changes: 3 additions & 0 deletions crates/derive/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# `kona-derive`

> [!WARNING]
> WIP
A `no_std` compatible implementation of the OP Stack's
[derivation pipeline](https://specs.optimism.io/protocol/derivation.html#l2-chain-derivation-specification).
2 changes: 1 addition & 1 deletion crates/derive/src/params.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! This module contains the parameters and identifying types for the derivation pipeline.
/// Count the tagging info as 200 in terms of buffer size.
pub const FRAME_OVERHEAD: u64 = 200;
pub const FRAME_OVERHEAD: usize = 200;

/// The version of the derivation pipeline.
pub const DERIVATION_VERSION_0: u8 = 0;
Expand Down
48 changes: 48 additions & 0 deletions crates/derive/src/stages/channel_bank.rs
Original file line number Diff line number Diff line change
@@ -1 +1,49 @@
//! This module contains the `ChannelBank` struct.
use alloc::collections::VecDeque;
use alloy_primitives::Bytes;
use hashbrown::HashMap;

use crate::{
params::ChannelID,
traits::{ChainProvider, DataAvailabilityProvider},
types::{Channel, RollupConfig},
};

use super::l1_retrieval::L1Retrieval;

/// [ChannelBank] is a stateful stage that does the following:
/// 1. Unmarshalls frames from L1 transaction data
/// 2. Applies those frames to a channel
/// 3. Attempts to read from the channel when it is ready
/// 4. Prunes channels (not frames) when the channel bank is too large.
///
/// Note: we prune before we ingest data.
/// As we switch between ingesting data & reading, the prune step occurs at an odd point
/// Specifically, the channel bank is not allowed to become too large between successive calls
/// to `IngestData`. This means that we can do an ingest and then do a read while becoming too large.
/// [ChannelBank] buffers channel frames, and emits full channel data
pub struct ChannelBank<DAP, CP>
where
DAP: DataAvailabilityProvider,
CP: ChainProvider,
{
/// The rollup configuration.
cfg: RollupConfig,
/// Map of channels by ID.
channels: HashMap<ChannelID, Channel>,
/// Channels in FIFO order.
channel_queue: VecDeque<ChannelID>,
/// The previous stage of the derivation pipeline.
prev: L1Retrieval<DAP, CP>,
/// Chain provider.
chain_provider: CP,
}

impl<DAP, CP> ChannelBank<DAP, CP>
where
DAP: DataAvailabilityProvider,
CP: ChainProvider,
{
// TODO
}
13 changes: 5 additions & 8 deletions crates/derive/src/stages/frame_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,24 @@ use alloy_primitives::Bytes;
use anyhow::{anyhow, bail, Result};
use async_trait::async_trait;

pub struct FrameQueue<T, DAP, CP>
pub struct FrameQueue<DAP, CP>
where
T: Into<Bytes>,
DAP: DataAvailabilityProvider,
CP: ChainProvider,
{
/// The previous stage in the pipeline.
pub prev: L1Retrieval<T, DAP, CP>,
pub prev: L1Retrieval<DAP, CP>,
/// The current frame queue.
queue: VecDeque<Frame>,
}

impl<T, DAP, CP> FrameQueue<T, DAP, CP>
impl<DAP, CP> FrameQueue<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 {
pub fn new(prev: L1Retrieval<DAP, CP>) -> Self {
Self {
prev,
queue: VecDeque::new(),
Expand Down Expand Up @@ -67,9 +65,8 @@ where
}

#[async_trait]
impl<T, DAP, CP> ResettableStage for FrameQueue<T, DAP, CP>
impl<DAP, CP> ResettableStage for FrameQueue<DAP, CP>
where
T: Into<Bytes>,
DAP: DataAvailabilityProvider + Send,
CP: ChainProvider + Send,
{
Expand Down
14 changes: 6 additions & 8 deletions crates/derive/src/stages/l1_retrieval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use crate::{
traits::{ChainProvider, DataAvailabilityProvider, DataIter, ResettableStage},
types::{BlockInfo, SystemConfig},
};
use alloc::boxed::Box;
use alloc::{boxed::Box, vec::Vec};
use alloy_primitives::Bytes;
use anyhow::{anyhow, Result};
use async_trait::async_trait;

/// The L1 retrieval stage of the derivation pipeline.
#[derive(Debug)]
pub struct L1Retrieval<T, DAP, CP>
pub struct L1Retrieval<DAP, CP>
where
DAP: DataAvailabilityProvider,
CP: ChainProvider,
Expand All @@ -22,12 +22,11 @@ where
/// The data availability provider to use for the L1 retrieval stage.
pub provider: DAP,
/// The current data iterator.
data: Option<DAP::DataIter<T>>,
data: Option<DAP::DataIter<Bytes>>,
}

impl<T, DAP, CP> L1Retrieval<T, DAP, CP>
impl<DAP, CP> L1Retrieval<DAP, CP>
where
T: Into<Bytes>,
DAP: DataAvailabilityProvider,
CP: ChainProvider,
{
Expand Down Expand Up @@ -66,14 +65,13 @@ where
self.data = None;
anyhow!("No more data to retrieve")
})?;
Ok(data.into())
Ok(data)
}
}

#[async_trait]
impl<T, DAP, CP> ResettableStage for L1Retrieval<T, DAP, CP>
impl<DAP, CP> ResettableStage for L1Retrieval<DAP, CP>
where
T: Into<Bytes>,
DAP: DataAvailabilityProvider + Send,
CP: ChainProvider + Send,
{
Expand Down
6 changes: 3 additions & 3 deletions crates/derive/src/traits/data_sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// use alloy_rpc_types::Block;
use crate::types::{BlockInfo, Receipt};
use alloc::{boxed::Box, vec::Vec};
use alloy_primitives::{Address, B256};
use alloy_primitives::{Address, Bytes, B256};
use anyhow::Result;
use async_trait::async_trait;

Expand All @@ -22,11 +22,11 @@ pub trait ChainProvider {
#[async_trait]
pub trait DataAvailabilityProvider {
/// A data iterator for the data source to return.
type DataIter<T>: DataIter<T> + Send;
type DataIter<T: Into<Bytes>>: DataIter<T> + Send;

/// Returns the data availability for the block with the given hash, or an error if the block does not exist in the
/// data source.
async fn open_data<T>(
async fn open_data<T: Into<Bytes>>(
&self,
block_ref: &BlockInfo,
batcher_address: Address,
Expand Down
Loading

0 comments on commit d373c17

Please sign in to comment.