Trait-abstracted Providers
Kona's derivation pipeline pulls in data from sources that are trait abstracted so the pipeline can be generic over various data sources. Note, "data sources" is used interchangeably with "trait-abstracted providers" for the purpose of this document.
The key traits required for the pipeline are the following.
The kona-derive-alloy
crate provides std
implementations
of these traits using Alloy's reqwest
-backed providers.
Provider Usage
Although trait-abstracted Providers are used throughout the pipeline and
its stages, the PipelineBuilder
makes constructing the pipeline
generic over the providers. An example is shown below, where the three
required trait implementations are the providers stubbed with todo!()
.
#![allow(unused)] fn main() { use std::sync::Arc; use op_alloy_genesis::RollupConfig; use kona_derive::pipeline::PipelineBuilder; use kona_derive::attributes::StatefulAttributesBuilder; // The rollup config for your chain. let cfg = Arc::new(RollupConfig::default()); // Must implement the `ChainProvider` trait. let chain_provider = todo!("your chain provider"); // Must implement the `L2ChainProvider` trait. let l2_chain_provider = todo!("your l2 chain provider"); // Must implement the `DataAvailabilityProvider` trait. let dap = todo!("your data availability provider"); // Generic over the providers. let attributes = StatefulAttributesBuilder::new( cfg.clone(), l2_chain_provider.clone(), chain_provider.clone(), ); // Construct a new derivation pipeline. let pipeline = PipelineBuilder::new() .rollup_config(cfg) .dap_source(dap) .l2_chain_provider(l2_chain_provider) .chain_provider(chain_provider) .builder(attributes) .origin(BlockInfo::default()) .build(); }
Implementing a Custom Data Availability Provider
Notice
The only required method for the
DataAvailabilityProvider
trait is thenext
method.
#![allow(unused)] fn main() { use async_trait::async_trait; use alloy_primitives::Bytes; use op_alloy_protocol::BlockInfo; use kona_derive::traits::DataAvailabilityProvider; use kona_derive::errors::PipelineResult; /// ExampleAvail /// /// An example implementation of the `DataAvailabilityProvider` trait. #[derive(Debug)] pub struct ExampleAvail { // Place your data in here } #[async_trait] impl DataAvailabilityProvider for ExampleAvail { type Item = Bytes; async fn next(&self, block_ref: &BlockInfo) -> PipelineResult<Self::Item> { todo!("return an AsyncIterator implementation here") } } }