Skip to content

Commit

Permalink
feat(plasma): Add PlasmaDataSource
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby committed Apr 28, 2024
1 parent cd01020 commit 2c41ed8
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 12 deletions.
3 changes: 2 additions & 1 deletion crates/derive/src/sources/ethereum.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Contains a Factory for creating a calldata and blob provider.
//! Contains the [EthereumDataSource], which is a concrete implementation of the
//! [DataAvailabilityProvider] trait for the Ethereum protocol.

use crate::{
sources::{BlobSource, CalldataSource, EthereumDataSourceVariant},
Expand Down
4 changes: 2 additions & 2 deletions crates/derive/src/sources/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ pub use blobs::BlobSource;
mod calldata;
pub use calldata::CalldataSource;

mod source;
pub use source::EthereumDataSourceVariant;
mod variant;
pub use variant::EthereumDataSourceVariant;
File renamed without changes.
9 changes: 0 additions & 9 deletions crates/derive/src/traits/data_sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@ pub trait BlobProvider {
) -> Result<Vec<Blob>>;
}

/// The PlasmaProvider trait specifies the functionality of a data source that can fetch plasma
/// inputs.
#[async_trait]
#[allow(dead_code)]
pub(crate) trait PlasmaProvider {
/// Fetches the plasma input for the given commitment at the given block number.
async fn get_input(&self, commitment: &[u8], block_number: u64) -> Result<Bytes>;
}

/// Describes the functionality of a data source that can provide data availability information.
#[async_trait]
pub trait DataAvailabilityProvider {
Expand Down
1 change: 1 addition & 0 deletions crates/plasma/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

extern crate alloc;

pub mod plasma;
pub mod source;
pub mod traits;
pub mod types;
Expand Down
72 changes: 72 additions & 0 deletions crates/plasma/src/plasma.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//! Contains the [PlasmaDataSource], which is a concrete implementation of the
//! [DataAvailabilityProvider] trait for the Plasma protocol. Used as an adapter to the
//! [kona_derive] crate's derivation pipeline construction.
//!
//! [DataAvailabilityProvider]: kona_derive::traits::DataAvailabilityProvider

use crate::{source::PlasmaSource, traits::PlasmaInputFetcher};
use alloc::{boxed::Box, fmt::Debug};
use alloy_primitives::{Address, Bytes};
use anyhow::Result;
use async_trait::async_trait;
use kona_derive::{
traits::{ChainProvider, DataAvailabilityProvider},
types::{BlockInfo, RollupConfig},
};
use kona_primitives::BlockID;

/// A factory for creating an Ethereum data source provider.
#[derive(Debug, Clone, Copy)]
pub struct PlasmaDataSource<C, PIF, I>
where
C: ChainProvider + Send + Clone,
PIF: PlasmaInputFetcher<C> + Clone,
I: Iterator<Item = Bytes> + Send + Clone,
{
/// The chain provider to use for the factory.
pub chain_provider: C,
/// The plasma iterator.
pub plasma_source: I,
/// The plasma input fetcher.
pub plasma_input_fetcher: PIF,
/// The L1 Signer.
pub signer: Address,
}

impl<C, PIF, I> PlasmaDataSource<C, PIF, I>
where
C: ChainProvider + Send + Clone + Debug,
PIF: PlasmaInputFetcher<C> + Clone,
I: Iterator<Item = Bytes> + Send + Clone,
{
/// Creates a new factory.
pub fn new(provider: C, pif: PIF, s: I, cfg: &RollupConfig) -> Self {
Self {
chain_provider: provider,
plasma_source: s,
plasma_input_fetcher: pif,
signer: cfg.genesis.system_config.batcher_addr,
}
}
}

#[async_trait]
impl<C, PIF, I> DataAvailabilityProvider for PlasmaDataSource<C, PIF, I>
where
C: ChainProvider + Send + Clone + Debug + Sync,
PIF: PlasmaInputFetcher<C> + Clone + Debug + Send + Sync,
I: Iterator<Item = Bytes> + Send + Clone + Debug + Sync,
{
type Item = Bytes;
type DataIter = PlasmaSource<C, PIF, I>;

async fn open_data(&self, block_ref: &BlockInfo, _: Address) -> Result<Self::DataIter> {
let id = BlockID { hash: block_ref.hash, number: block_ref.number };
Ok(PlasmaSource::new(
self.chain_provider.clone(),
self.plasma_input_fetcher.clone(),
self.plasma_source.clone(),
id,
))
}
}

0 comments on commit 2c41ed8

Please sign in to comment.