This repository was archived by the owner on Jan 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 212
feat(docs): Custom Derivation Pipeline Example #2702
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
199 changes: 199 additions & 0 deletions
199
docs/docs/pages/sdk/examples/custom-derivation-pipeline.mdx
This file contains hidden or 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,199 @@ | ||
| import { Callout } from 'vocs/components' | ||
|
|
||
| # Custom Derivation Pipeline Stage | ||
|
|
||
| Extend Kona's derivation pipeline by wrapping the top-level `AttributesQueue` stage with custom logic for monitoring, validation, or transformation. | ||
|
|
||
| ## Core Concepts | ||
|
|
||
| The derivation pipeline uses a stage-based architecture where each stage wraps the previous one: | ||
|
|
||
| ``` | ||
| L1Traversal → L1Retrieval → FrameQueue → ChannelProvider → | ||
| ChannelReader → BatchStream → BatchProvider → AttributesQueue | ||
| ``` | ||
|
|
||
| ### Key Traits | ||
|
|
||
| Custom stages that wrap the `AttributesQueue` must implement: | ||
| - `NextAttributes` - Provides payload attributes for block building | ||
| - `OriginProvider` - Provides current L1 origin | ||
| - `SignalReceiver` - Handles pipeline resets | ||
| - `OriginAdvancer` - Advances L1 origin | ||
|
|
||
| ## Example: Monitoring Stage | ||
|
|
||
| Wrap the `AttributesQueue` to add metrics tracking: | ||
|
|
||
| ```rust | ||
| use kona_derive::{ | ||
| NextAttributes, OriginProvider, SignalReceiver, OriginAdvancer, | ||
| PipelineResult, Signal, OpAttributesWithParent | ||
| }; | ||
| use kona_protocol::{BlockInfo, L2BlockInfo}; | ||
| use async_trait::async_trait; | ||
| use std::time::Instant; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct LoggingStage<S> { | ||
| inner: S, | ||
| attributes_count: u64, | ||
| last_origin: Option<BlockInfo>, | ||
| } | ||
|
|
||
| impl<S> LoggingStage<S> { | ||
| pub fn new(inner: S) -> Self { | ||
| Self { | ||
| inner, | ||
| attributes_count: 0, | ||
| last_origin: None, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl<S> NextAttributes for LoggingStage<S> | ||
| where | ||
| S: NextAttributes + Send + Sync, | ||
| { | ||
| async fn next_attributes( | ||
| &mut self, | ||
| parent: L2BlockInfo | ||
| ) -> PipelineResult<OpAttributesWithParent> { | ||
| let start = Instant::now(); | ||
|
|
||
| // Delegate to inner stage | ||
| let attributes = self.inner.next_attributes(parent).await?; | ||
|
|
||
| // Track metrics | ||
| self.attributes_count += 1; | ||
| let duration = start.elapsed(); | ||
|
|
||
| info!( | ||
| target: "pipeline::logging", | ||
| count = self.attributes_count, | ||
| duration_ms = duration.as_millis(), | ||
| parent_hash = ?parent.block_info.hash, | ||
| "Generated attributes" | ||
| ); | ||
|
|
||
| Ok(attributes) | ||
| } | ||
| } | ||
|
|
||
| impl<S> OriginProvider for LoggingStage<S> | ||
| where | ||
| S: OriginProvider, | ||
| { | ||
| fn origin(&self) -> Option<BlockInfo> { | ||
| self.inner.origin() | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl<S> SignalReceiver for LoggingStage<S> | ||
| where | ||
| S: SignalReceiver + Send + Sync, | ||
| { | ||
| async fn signal(&mut self, signal: Signal) -> PipelineResult<()> { | ||
| info!(target: "pipeline::logging", ?signal, "Received signal"); | ||
|
||
|
|
||
| // Track origin changes on reset | ||
| if let Signal::Reset(reset) = &signal { | ||
| self.last_origin = Some(reset.l1_origin); | ||
| self.attributes_count = 0; // Reset counter | ||
| } | ||
|
|
||
| self.inner.signal(signal).await | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl<S> OriginAdvancer for LoggingStage<S> | ||
| where | ||
| S: OriginAdvancer + Send + Sync, | ||
| { | ||
| async fn advance_origin(&mut self) -> PipelineResult<()> { | ||
| let prev_origin = self.inner.origin(); | ||
| self.inner.advance_origin().await?; | ||
| let new_origin = self.inner.origin(); | ||
|
|
||
| if prev_origin != new_origin { | ||
| info!( | ||
| target: "pipeline::logging", | ||
| prev = ?prev_origin, | ||
| new = ?new_origin, | ||
| "Advanced origin" | ||
| ); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| <Callout type="info"> | ||
| Custom stages wrap the `AttributesQueue` (top-level stage). For deeper pipeline modifications, you'd need to rebuild the entire pipeline. | ||
| </Callout> | ||
|
|
||
| ## Integration | ||
|
|
||
| ```rust | ||
| use kona_derive::{PipelineBuilder, DerivationPipeline}; | ||
| use kona_node::{StatefulAttributesBuilder}; | ||
| use alloc::sync::Arc; | ||
|
|
||
| // Build standard pipeline | ||
| let pipeline = PipelineBuilder::new() | ||
| .rollup_config(rollup_config.clone()) | ||
| .origin(origin) | ||
| .chain_provider(chain_provider) | ||
| .l2_chain_provider(l2_chain_provider.clone()) | ||
| .dap_source(dap_source) | ||
| .builder(attributes_builder) | ||
| .build_polled(); | ||
|
|
||
| // Wrap with monitoring | ||
| let monitoring_stage = LoggingStage::new(pipeline.attributes); | ||
|
|
||
| // Create new pipeline | ||
| let custom_pipeline = DerivationPipeline::new( | ||
| monitoring_stage, | ||
| rollup_config, | ||
| l2_chain_provider, | ||
| ); | ||
| ``` | ||
|
|
||
| ## Testing | ||
|
|
||
| ```rust | ||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use kona_derive::test_utils::TestNextAttributes; | ||
|
|
||
| #[tokio::test] | ||
| async fn test_logging_stage() { | ||
| let mock_inner = TestNextAttributes::new(); | ||
| let mut stage = LoggingStage::new(mock_inner); | ||
|
|
||
| // Test attributes generation | ||
| let parent = L2BlockInfo::default(); | ||
| let result = stage.next_attributes(parent).await; | ||
| assert!(result.is_ok()); | ||
| assert_eq!(stage.attributes_count, 1); | ||
|
|
||
| // Test signal handling | ||
| let signal = Signal::Reset(Default::default()); | ||
| stage.signal(signal).await.unwrap(); | ||
| assert_eq!(stage.attributes_count, 0); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Related Resources | ||
|
|
||
| - [kona-derive](https://github.com/op-rs/kona/tree/main/crates/protocol/derive) - Core derivation pipeline | ||
| - [Pipeline Traits](https://github.com/op-rs/kona/tree/main/crates/protocol/derive/src/traits) - Trait definitions | ||
| - [Stage Examples](https://github.com/op-rs/kona/tree/main/crates/protocol/derive/src/stages) - Built-in stages | ||
| - [OP Stack Derivation Spec](https://specs.optimism.io/protocol/derivation.html) - Protocol specification | ||
This file contains hidden or 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 | ||
|---|---|---|---|---|
|
|
@@ -3,7 +3,9 @@ | |||
| Examples for working with `kona` crates. | ||||
|
|
||||
| - [Load a Rollup Config for a Chain ID](/sdk/examples/load-a-rollup-config) | ||||
| - [Create a new L1BlockInfoTx Hardfork Variant](/sdk/examples/new-l1-block-info-tx-hardfork) | ||||
| - [Transform Frames to a Batch](/sdk/examples/frames-to-batch) | ||||
| - [Transform a Batch to Frames](/sdk/examples/batch-to-frames) | ||||
| - [Create a new L1BlockInfoTx Hardfork Variant](/sdk/examples/new-l1-block-info-tx-hardfork) | ||||
|
||||
| - [Create a new L1BlockInfoTx Hardfork Variant](/sdk/examples/new-l1-block-info-tx-hardfork) |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code example uses the
info!macro without importing or explaining the logging framework. Add an import statement likeuse log::info;oruse tracing::info;to make the example complete and executable.