From 4fc1ef66974effef3f736b060fc929b1835dcff2 Mon Sep 17 00:00:00 2001 From: refcell Date: Tue, 5 Nov 2024 15:07:08 -0500 Subject: [PATCH] fix(derive): hoist types out of traits (#781) --- bin/client/src/l1/driver.rs | 6 +- crates/derive/src/attributes/stateful.rs | 5 +- crates/derive/src/errors/mod.rs | 4 +- crates/derive/src/errors/pipeline.rs | 3 - crates/derive/src/lib.rs | 1 + crates/derive/src/pipeline/core.rs | 14 +- crates/derive/src/sources/blobs.rs | 3 +- crates/derive/src/sources/calldata.rs | 3 +- crates/derive/src/sources/ethereum.rs | 2 +- crates/derive/src/sources/variant.rs | 2 +- crates/derive/src/stages/attributes_queue.rs | 7 +- .../derive/src/stages/batch/batch_provider.rs | 10 +- crates/derive/src/stages/batch/batch_queue.rs | 8 +- .../derive/src/stages/batch/batch_stream.rs | 7 +- .../src/stages/batch/batch_validator.rs | 10 +- crates/derive/src/stages/batch/mod.rs | 2 +- .../src/stages/channel/channel_assembler.rs | 4 +- .../derive/src/stages/channel/channel_bank.rs | 7 +- .../src/stages/channel/channel_provider.rs | 8 +- .../src/stages/channel/channel_reader.rs | 7 +- crates/derive/src/stages/channel/mod.rs | 2 +- crates/derive/src/stages/frame_queue.rs | 7 +- crates/derive/src/stages/l1_retrieval.rs | 6 +- crates/derive/src/stages/l1_traversal.rs | 8 +- .../derive/src/test_utils/attributes_queue.rs | 6 +- .../derive/src/test_utils/batch_provider.rs | 5 +- crates/derive/src/test_utils/batch_stream.rs | 5 +- .../derive/src/test_utils/channel_provider.rs | 5 +- .../derive/src/test_utils/channel_reader.rs | 5 +- .../test_utils/data_availability_provider.rs | 3 +- crates/derive/src/test_utils/frame_queue.rs | 5 +- crates/derive/src/test_utils/pipeline.rs | 6 +- crates/derive/src/traits/attributes.rs | 2 +- crates/derive/src/traits/data_sources.rs | 5 +- crates/derive/src/traits/mod.rs | 2 +- crates/derive/src/traits/pipeline.rs | 123 +----------------- crates/derive/src/traits/stages.rs | 2 +- crates/derive/src/types/mod.rs | 7 + crates/derive/src/types/results.rs | 19 +++ crates/derive/src/types/signals.rs | 113 ++++++++++++++++ 40 files changed, 236 insertions(+), 213 deletions(-) create mode 100644 crates/derive/src/types/mod.rs create mode 100644 crates/derive/src/types/results.rs create mode 100644 crates/derive/src/types/signals.rs diff --git a/bin/client/src/l1/driver.rs b/bin/client/src/l1/driver.rs index feb770c04..72648e5f1 100644 --- a/bin/client/src/l1/driver.rs +++ b/bin/client/src/l1/driver.rs @@ -16,7 +16,7 @@ use alloy_rlp::Decodable; use core::fmt::Debug; use kona_derive::{ attributes::StatefulAttributesBuilder, - errors::{PipelineError, PipelineErrorKind, PipelineResult, ResetError}, + errors::{PipelineError, PipelineErrorKind, ResetError}, pipeline::{DerivationPipeline, PipelineBuilder}, sources::EthereumDataSource, stages::{ @@ -24,9 +24,9 @@ use kona_derive::{ L1Retrieval, L1Traversal, }, traits::{ - ActivationSignal, BlobProvider, ChainProvider, L2ChainProvider, OriginProvider, Pipeline, - ResetSignal, Signal, SignalReceiver, StepResult, + BlobProvider, ChainProvider, L2ChainProvider, OriginProvider, Pipeline, SignalReceiver, }, + types::{ActivationSignal, PipelineResult, ResetSignal, Signal, StepResult}, }; use kona_executor::{KonaHandleRegister, StatelessL2BlockExecutor}; use kona_mpt::{TrieHinter, TrieProvider}; diff --git a/crates/derive/src/attributes/stateful.rs b/crates/derive/src/attributes/stateful.rs index 37ccc9dff..fc73aa6c8 100644 --- a/crates/derive/src/attributes/stateful.rs +++ b/crates/derive/src/attributes/stateful.rs @@ -1,10 +1,9 @@ //! The [`AttributesBuilder`] and it's default implementation. use crate::{ - errors::{ - BuilderError, PipelineEncodingError, PipelineError, PipelineErrorKind, PipelineResult, - }, + errors::{BuilderError, PipelineEncodingError, PipelineError, PipelineErrorKind}, traits::{AttributesBuilder, ChainProvider, L2ChainProvider}, + types::PipelineResult, }; use alloc::{boxed::Box, fmt::Debug, string::ToString, sync::Arc, vec, vec::Vec}; use alloy_consensus::{Eip658Value, Receipt}; diff --git a/crates/derive/src/errors/mod.rs b/crates/derive/src/errors/mod.rs index 4edd4b7f7..f4b66f72c 100644 --- a/crates/derive/src/errors/mod.rs +++ b/crates/derive/src/errors/mod.rs @@ -7,9 +7,7 @@ mod stages; pub use stages::BatchDecompressionError; mod pipeline; -pub use pipeline::{ - PipelineEncodingError, PipelineError, PipelineErrorKind, PipelineResult, ResetError, -}; +pub use pipeline::{PipelineEncodingError, PipelineError, PipelineErrorKind, ResetError}; mod sources; pub use sources::{BlobDecodingError, BlobProviderError}; diff --git a/crates/derive/src/errors/pipeline.rs b/crates/derive/src/errors/pipeline.rs index ee808e87a..98722fabf 100644 --- a/crates/derive/src/errors/pipeline.rs +++ b/crates/derive/src/errors/pipeline.rs @@ -6,9 +6,6 @@ use alloy_primitives::B256; use op_alloy_genesis::system::SystemConfigUpdateError; use op_alloy_protocol::{DepositError, SpanBatchError}; -/// A result type for the derivation pipeline stages. -pub type PipelineResult = Result; - /// [crate::ensure] is a short-hand for bubbling up errors in the case of a condition not being met. #[macro_export] macro_rules! ensure { diff --git a/crates/derive/src/lib.rs b/crates/derive/src/lib.rs index 254ad9cf9..64889cfb7 100644 --- a/crates/derive/src/lib.rs +++ b/crates/derive/src/lib.rs @@ -20,6 +20,7 @@ pub mod pipeline; pub mod sources; pub mod stages; pub mod traits; +pub mod types; #[cfg(any(test, feature = "test-utils"))] pub mod test_utils; diff --git a/crates/derive/src/pipeline/core.rs b/crates/derive/src/pipeline/core.rs index 2559a0dbf..d96da92b3 100644 --- a/crates/derive/src/pipeline/core.rs +++ b/crates/derive/src/pipeline/core.rs @@ -1,11 +1,11 @@ //! Contains the core derivation pipeline. use crate::{ - errors::{PipelineError, PipelineErrorKind, PipelineResult}, + errors::{PipelineError, PipelineErrorKind}, traits::{ - ActivationSignal, L2ChainProvider, NextAttributes, OriginAdvancer, OriginProvider, - Pipeline, ResetSignal, Signal, SignalReceiver, StepResult, + L2ChainProvider, NextAttributes, OriginAdvancer, OriginProvider, Pipeline, SignalReceiver, }, + types::{ActivationSignal, PipelineResult, ResetSignal, Signal, StepResult}, }; use alloc::{boxed::Box, collections::VecDeque, sync::Arc}; use async_trait::async_trait; @@ -172,12 +172,8 @@ where #[cfg(test)] mod tests { - use crate::{ - errors::PipelineError, - pipeline::DerivationPipeline, - test_utils::*, - traits::{ActivationSignal, Pipeline, ResetSignal, Signal, SignalReceiver, StepResult}, - }; + use super::*; + use crate::{pipeline::DerivationPipeline, test_utils::*}; use alloc::{string::ToString, sync::Arc}; use alloy_rpc_types_engine::PayloadAttributes; use op_alloy_genesis::{RollupConfig, SystemConfig}; diff --git a/crates/derive/src/sources/blobs.rs b/crates/derive/src/sources/blobs.rs index a486837ae..c20ddb0a2 100644 --- a/crates/derive/src/sources/blobs.rs +++ b/crates/derive/src/sources/blobs.rs @@ -1,8 +1,9 @@ //! Blob Data Source use crate::{ - errors::{BlobDecodingError, BlobProviderError, PipelineError, PipelineResult}, + errors::{BlobDecodingError, BlobProviderError, PipelineError}, traits::{AsyncIterator, BlobProvider, ChainProvider}, + types::PipelineResult, }; use alloc::{boxed::Box, string::ToString, vec, vec::Vec}; use alloy_consensus::{Transaction, TxEip4844Variant, TxEnvelope, TxType}; diff --git a/crates/derive/src/sources/calldata.rs b/crates/derive/src/sources/calldata.rs index d3adbc50c..b3af6ccc8 100644 --- a/crates/derive/src/sources/calldata.rs +++ b/crates/derive/src/sources/calldata.rs @@ -1,8 +1,9 @@ //! CallData Source use crate::{ - errors::{PipelineError, PipelineResult}, + errors::PipelineError, traits::{AsyncIterator, ChainProvider}, + types::PipelineResult, }; use alloc::{boxed::Box, collections::VecDeque}; use alloy_consensus::{Transaction, TxEnvelope}; diff --git a/crates/derive/src/sources/ethereum.rs b/crates/derive/src/sources/ethereum.rs index 3f92535d2..307d2c084 100644 --- a/crates/derive/src/sources/ethereum.rs +++ b/crates/derive/src/sources/ethereum.rs @@ -2,9 +2,9 @@ //! [DataAvailabilityProvider] trait for the Ethereum protocol. use crate::{ - errors::PipelineResult, sources::{BlobSource, CalldataSource, EthereumDataSourceVariant}, traits::{BlobProvider, ChainProvider, DataAvailabilityProvider}, + types::PipelineResult, }; use alloc::{boxed::Box, fmt::Debug}; use alloy_primitives::{Address, Bytes}; diff --git a/crates/derive/src/sources/variant.rs b/crates/derive/src/sources/variant.rs index 693d212c1..44588e39c 100644 --- a/crates/derive/src/sources/variant.rs +++ b/crates/derive/src/sources/variant.rs @@ -1,9 +1,9 @@ //! Data source use crate::{ - errors::PipelineResult, sources::{BlobSource, CalldataSource}, traits::{AsyncIterator, BlobProvider, ChainProvider}, + types::PipelineResult, }; use alloc::boxed::Box; use alloy_primitives::Bytes; diff --git a/crates/derive/src/stages/attributes_queue.rs b/crates/derive/src/stages/attributes_queue.rs index f746d365a..c02a67d91 100644 --- a/crates/derive/src/stages/attributes_queue.rs +++ b/crates/derive/src/stages/attributes_queue.rs @@ -1,11 +1,12 @@ //! Contains the logic for the `AttributesQueue` stage. use crate::{ - errors::{PipelineError, PipelineResult, ResetError}, + errors::{PipelineError, ResetError}, traits::{ AttributesBuilder, AttributesProvider, NextAttributes, OriginAdvancer, OriginProvider, - Signal, SignalReceiver, + SignalReceiver, }, + types::{PipelineResult, Signal}, }; use alloc::{boxed::Box, sync::Arc}; use async_trait::async_trait; @@ -198,7 +199,7 @@ mod tests { use crate::{ errors::{BuilderError, PipelineErrorKind}, test_utils::{new_test_attributes_provider, TestAttributesBuilder, TestAttributesProvider}, - traits::ResetSignal, + types::ResetSignal, }; use alloc::{sync::Arc, vec, vec::Vec}; use alloy_primitives::{b256, Address, Bytes, B256}; diff --git a/crates/derive/src/stages/batch/batch_provider.rs b/crates/derive/src/stages/batch/batch_provider.rs index 315aa74af..22a982669 100644 --- a/crates/derive/src/stages/batch/batch_provider.rs +++ b/crates/derive/src/stages/batch/batch_provider.rs @@ -2,11 +2,10 @@ use super::NextBatchProvider; use crate::{ - errors::{PipelineError, PipelineResult}, + errors::PipelineError, stages::{BatchQueue, BatchValidator}, - traits::{ - AttributesProvider, L2ChainProvider, OriginAdvancer, OriginProvider, Signal, SignalReceiver, - }, + traits::{AttributesProvider, L2ChainProvider, OriginAdvancer, OriginProvider, SignalReceiver}, + types::{PipelineResult, Signal}, }; use alloc::{boxed::Box, sync::Arc}; use async_trait::async_trait; @@ -179,7 +178,8 @@ mod test { use super::BatchProvider; use crate::{ test_utils::{TestL2ChainProvider, TestNextBatchProvider}, - traits::{OriginProvider, ResetSignal, SignalReceiver}, + traits::{OriginProvider, SignalReceiver}, + types::ResetSignal, }; use alloc::{sync::Arc, vec}; use op_alloy_genesis::RollupConfig; diff --git a/crates/derive/src/stages/batch/batch_queue.rs b/crates/derive/src/stages/batch/batch_queue.rs index dc8ba759f..99993408c 100644 --- a/crates/derive/src/stages/batch/batch_queue.rs +++ b/crates/derive/src/stages/batch/batch_queue.rs @@ -2,11 +2,9 @@ use super::NextBatchProvider; use crate::{ - errors::{PipelineEncodingError, PipelineError, PipelineErrorKind, PipelineResult, ResetError}, - traits::{ - AttributesProvider, L2ChainProvider, OriginAdvancer, OriginProvider, ResetSignal, Signal, - SignalReceiver, - }, + errors::{PipelineEncodingError, PipelineError, PipelineErrorKind, ResetError}, + traits::{AttributesProvider, L2ChainProvider, OriginAdvancer, OriginProvider, SignalReceiver}, + types::{PipelineResult, ResetSignal, Signal}, }; use alloc::{boxed::Box, sync::Arc, vec::Vec}; use async_trait::async_trait; diff --git a/crates/derive/src/stages/batch/batch_stream.rs b/crates/derive/src/stages/batch/batch_stream.rs index b932420f1..b1ab3749b 100644 --- a/crates/derive/src/stages/batch/batch_stream.rs +++ b/crates/derive/src/stages/batch/batch_stream.rs @@ -1,9 +1,10 @@ //! This module contains the `BatchStream` stage. use crate::{ - errors::{PipelineEncodingError, PipelineError, PipelineResult}, + errors::{PipelineEncodingError, PipelineError}, stages::NextBatchProvider, - traits::{L2ChainProvider, OriginAdvancer, OriginProvider, Signal, SignalReceiver}, + traits::{L2ChainProvider, OriginAdvancer, OriginProvider, SignalReceiver}, + types::{PipelineResult, Signal}, }; use alloc::{boxed::Box, collections::VecDeque, sync::Arc}; use async_trait::async_trait; @@ -222,7 +223,7 @@ mod test { use super::*; use crate::{ test_utils::{CollectingLayer, TestBatchStreamProvider, TestL2ChainProvider, TraceStorage}, - traits::ResetSignal, + types::ResetSignal, }; use alloc::vec; use op_alloy_protocol::{SingleBatch, SpanBatchElement}; diff --git a/crates/derive/src/stages/batch/batch_validator.rs b/crates/derive/src/stages/batch/batch_validator.rs index 96b7d380a..0702408ad 100644 --- a/crates/derive/src/stages/batch/batch_validator.rs +++ b/crates/derive/src/stages/batch/batch_validator.rs @@ -2,9 +2,10 @@ use super::NextBatchProvider; use crate::{ - errors::{PipelineResult, ResetError}, + errors::ResetError, prelude::{OriginProvider, PipelineError, PipelineErrorKind}, - traits::{AttributesProvider, OriginAdvancer, ResetSignal, Signal, SignalReceiver}, + traits::{AttributesProvider, OriginAdvancer, SignalReceiver}, + types::{PipelineResult, ResetSignal, Signal}, }; use alloc::{boxed::Box, sync::Arc, vec::Vec}; use async_trait::async_trait; @@ -310,10 +311,11 @@ where #[cfg(test)] mod test { use crate::{ - errors::{PipelineError, PipelineErrorKind, PipelineResult, ResetError}, + errors::{PipelineError, PipelineErrorKind, ResetError}, stages::{BatchValidator, NextBatchProvider}, test_utils::{CollectingLayer, TestNextBatchProvider, TraceStorage}, - traits::{AttributesProvider, OriginAdvancer, ResetSignal, Signal, SignalReceiver}, + traits::{AttributesProvider, OriginAdvancer, SignalReceiver}, + types::{PipelineResult, ResetSignal, Signal}, }; use alloc::{sync::Arc, vec, vec::Vec}; use alloy_eips::{BlockNumHash, NumHash}; diff --git a/crates/derive/src/stages/batch/mod.rs b/crates/derive/src/stages/batch/mod.rs index a03b22e8a..6f0bac4f2 100644 --- a/crates/derive/src/stages/batch/mod.rs +++ b/crates/derive/src/stages/batch/mod.rs @@ -11,7 +11,7 @@ //! [ChannelReader]: crate::stages::channel::ChannelReader //! [AttributesQueue]: crate::stages::attributes_queue::AttributesQueue -use crate::errors::PipelineResult; +use crate::types::PipelineResult; use alloc::boxed::Box; use async_trait::async_trait; use op_alloy_protocol::{Batch, BlockInfo, L2BlockInfo}; diff --git a/crates/derive/src/stages/channel/channel_assembler.rs b/crates/derive/src/stages/channel/channel_assembler.rs index 669ac471a..1b5a4940a 100644 --- a/crates/derive/src/stages/channel/channel_assembler.rs +++ b/crates/derive/src/stages/channel/channel_assembler.rs @@ -2,9 +2,9 @@ use super::{ChannelReaderProvider, NextFrameProvider}; use crate::{ - errors::PipelineResult, prelude::{OriginProvider, PipelineError}, - traits::{OriginAdvancer, Signal, SignalReceiver}, + traits::{OriginAdvancer, SignalReceiver}, + types::{PipelineResult, Signal}, }; use alloc::{boxed::Box, sync::Arc}; use alloy_primitives::{hex, Bytes}; diff --git a/crates/derive/src/stages/channel/channel_bank.rs b/crates/derive/src/stages/channel/channel_bank.rs index 47937e9da..6bdc6eb02 100644 --- a/crates/derive/src/stages/channel/channel_bank.rs +++ b/crates/derive/src/stages/channel/channel_bank.rs @@ -2,9 +2,10 @@ use super::NextFrameProvider; use crate::{ - errors::{PipelineError, PipelineErrorKind, PipelineResult}, + errors::{PipelineError, PipelineErrorKind}, stages::ChannelReaderProvider, - traits::{OriginAdvancer, OriginProvider, Signal, SignalReceiver}, + traits::{OriginAdvancer, OriginProvider, SignalReceiver}, + types::{PipelineResult, Signal}, }; use alloc::{boxed::Box, collections::VecDeque, sync::Arc}; use alloy_primitives::{hex, map::HashMap, Bytes}; @@ -245,7 +246,7 @@ mod tests { use super::*; use crate::{ test_utils::{CollectingLayer, TestNextFrameProvider, TraceStorage}, - traits::ResetSignal, + types::ResetSignal, }; use alloc::{vec, vec::Vec}; use op_alloy_genesis::{BASE_MAINNET_CONFIG, OP_MAINNET_CONFIG}; diff --git a/crates/derive/src/stages/channel/channel_provider.rs b/crates/derive/src/stages/channel/channel_provider.rs index 1dc868ac1..093e5dcd2 100644 --- a/crates/derive/src/stages/channel/channel_provider.rs +++ b/crates/derive/src/stages/channel/channel_provider.rs @@ -2,8 +2,9 @@ use super::{ChannelAssembler, ChannelBank, ChannelReaderProvider, NextFrameProvider}; use crate::{ - errors::{PipelineError, PipelineResult}, - traits::{OriginAdvancer, OriginProvider, Signal, SignalReceiver}, + errors::PipelineError, + traits::{OriginAdvancer, OriginProvider, SignalReceiver}, + types::{PipelineResult, Signal}, }; use alloc::{boxed::Box, sync::Arc}; use alloy_primitives::Bytes; @@ -158,7 +159,8 @@ mod test { prelude::{OriginProvider, PipelineError}, stages::ChannelReaderProvider, test_utils::TestNextFrameProvider, - traits::{ResetSignal, SignalReceiver}, + traits::SignalReceiver, + types::ResetSignal, }; use alloc::{sync::Arc, vec}; use op_alloy_genesis::RollupConfig; diff --git a/crates/derive/src/stages/channel/channel_reader.rs b/crates/derive/src/stages/channel/channel_reader.rs index 3f4f0a6f1..9f89e66a2 100644 --- a/crates/derive/src/stages/channel/channel_reader.rs +++ b/crates/derive/src/stages/channel/channel_reader.rs @@ -1,9 +1,10 @@ //! This module contains the `ChannelReader` struct. use crate::{ - errors::{PipelineError, PipelineResult}, + errors::PipelineError, stages::{decompress_brotli, BatchStreamProvider}, - traits::{OriginAdvancer, OriginProvider, Signal, SignalReceiver}, + traits::{OriginAdvancer, OriginProvider, SignalReceiver}, + types::{PipelineResult, Signal}, }; use alloc::{boxed::Box, sync::Arc, vec::Vec}; use alloy_primitives::Bytes; @@ -257,7 +258,7 @@ impl BatchReader { mod test { use super::*; use crate::{ - errors::PipelineErrorKind, test_utils::TestChannelReaderProvider, traits::ResetSignal, + errors::PipelineErrorKind, test_utils::TestChannelReaderProvider, types::ResetSignal, }; use alloc::vec; diff --git a/crates/derive/src/stages/channel/mod.rs b/crates/derive/src/stages/channel/mod.rs index 6c6f699ca..7e86273f6 100644 --- a/crates/derive/src/stages/channel/mod.rs +++ b/crates/derive/src/stages/channel/mod.rs @@ -11,7 +11,7 @@ //! [FrameQueue]: crate::stages::FrameQueue //! [BatchQueue]: crate::stages::BatchQueue -use crate::errors::PipelineResult; +use crate::types::PipelineResult; use alloc::boxed::Box; use async_trait::async_trait; use op_alloy_protocol::Frame; diff --git a/crates/derive/src/stages/frame_queue.rs b/crates/derive/src/stages/frame_queue.rs index 6e103785c..7ceb80e3c 100644 --- a/crates/derive/src/stages/frame_queue.rs +++ b/crates/derive/src/stages/frame_queue.rs @@ -1,9 +1,10 @@ //! This module contains the [FrameQueue] stage of the derivation pipeline. use crate::{ - errors::{PipelineError, PipelineResult}, + errors::PipelineError, stages::NextFrameProvider, - traits::{OriginAdvancer, OriginProvider, Signal, SignalReceiver}, + traits::{OriginAdvancer, OriginProvider, SignalReceiver}, + types::{PipelineResult, Signal}, }; use alloc::{boxed::Box, collections::VecDeque, sync::Arc}; use alloy_primitives::Bytes; @@ -193,7 +194,7 @@ where #[cfg(test)] pub(crate) mod tests { use super::*; - use crate::{test_utils::TestFrameQueueProvider, traits::ResetSignal}; + use crate::{test_utils::TestFrameQueueProvider, types::ResetSignal}; use alloc::vec; #[tokio::test] diff --git a/crates/derive/src/stages/l1_retrieval.rs b/crates/derive/src/stages/l1_retrieval.rs index 7fc7c298c..41c9db004 100644 --- a/crates/derive/src/stages/l1_retrieval.rs +++ b/crates/derive/src/stages/l1_retrieval.rs @@ -1,12 +1,12 @@ //! Contains the [L1Retrieval] stage of the derivation pipeline. use crate::{ - errors::{PipelineError, PipelineErrorKind, PipelineResult}, + errors::{PipelineError, PipelineErrorKind}, stages::FrameQueueProvider, traits::{ - ActivationSignal, AsyncIterator, DataAvailabilityProvider, OriginAdvancer, OriginProvider, - ResetSignal, Signal, SignalReceiver, + AsyncIterator, DataAvailabilityProvider, OriginAdvancer, OriginProvider, SignalReceiver, }, + types::{ActivationSignal, PipelineResult, ResetSignal, Signal}, }; use alloc::boxed::Box; use alloy_primitives::Address; diff --git a/crates/derive/src/stages/l1_traversal.rs b/crates/derive/src/stages/l1_traversal.rs index ac8a97745..a19f0e96b 100644 --- a/crates/derive/src/stages/l1_traversal.rs +++ b/crates/derive/src/stages/l1_traversal.rs @@ -1,12 +1,10 @@ //! Contains the [L1Traversal] stage of the derivation pipeline. use crate::{ - errors::{PipelineError, PipelineResult, ResetError}, + errors::{PipelineError, ResetError}, stages::L1RetrievalProvider, - traits::{ - ActivationSignal, ChainProvider, OriginAdvancer, OriginProvider, ResetSignal, Signal, - SignalReceiver, - }, + traits::{ChainProvider, OriginAdvancer, OriginProvider, SignalReceiver}, + types::{ActivationSignal, PipelineResult, ResetSignal, Signal}, }; use alloc::{boxed::Box, sync::Arc}; use alloy_primitives::Address; diff --git a/crates/derive/src/test_utils/attributes_queue.rs b/crates/derive/src/test_utils/attributes_queue.rs index 6d2ddb7cf..4e54032b0 100644 --- a/crates/derive/src/test_utils/attributes_queue.rs +++ b/crates/derive/src/test_utils/attributes_queue.rs @@ -1,11 +1,11 @@ //! Testing utilities for the attributes queue stage. use crate::{ - errors::{BuilderError, PipelineError, PipelineErrorKind, PipelineResult}, + errors::{BuilderError, PipelineError, PipelineErrorKind}, traits::{ - AttributesBuilder, AttributesProvider, OriginAdvancer, OriginProvider, Signal, - SignalReceiver, + AttributesBuilder, AttributesProvider, OriginAdvancer, OriginProvider, SignalReceiver, }, + types::{PipelineResult, Signal}, }; use alloc::{boxed::Box, string::ToString, vec::Vec}; use alloy_eips::BlockNumHash; diff --git a/crates/derive/src/test_utils/batch_provider.rs b/crates/derive/src/test_utils/batch_provider.rs index c862fdb99..1ae1329af 100644 --- a/crates/derive/src/test_utils/batch_provider.rs +++ b/crates/derive/src/test_utils/batch_provider.rs @@ -1,9 +1,10 @@ //! A mock implementation of the [`BatchQueue`] stage for testing. use crate::{ - errors::{PipelineError, PipelineResult}, + errors::PipelineError, stages::NextBatchProvider, - traits::{OriginAdvancer, OriginProvider, Signal, SignalReceiver}, + traits::{OriginAdvancer, OriginProvider, SignalReceiver}, + types::{PipelineResult, Signal}, }; use alloc::{boxed::Box, vec::Vec}; use async_trait::async_trait; diff --git a/crates/derive/src/test_utils/batch_stream.rs b/crates/derive/src/test_utils/batch_stream.rs index 91ad1a445..7ec1daa94 100644 --- a/crates/derive/src/test_utils/batch_stream.rs +++ b/crates/derive/src/test_utils/batch_stream.rs @@ -3,9 +3,10 @@ //! [`BatchStream`]: crate::stages::BatchStream use crate::{ - errors::{PipelineError, PipelineResult}, + errors::PipelineError, stages::BatchStreamProvider, - traits::{OriginAdvancer, OriginProvider, Signal, SignalReceiver}, + traits::{OriginAdvancer, OriginProvider, SignalReceiver}, + types::{PipelineResult, Signal}, }; use alloc::{boxed::Box, vec::Vec}; use async_trait::async_trait; diff --git a/crates/derive/src/test_utils/channel_provider.rs b/crates/derive/src/test_utils/channel_provider.rs index 26d694989..acbc8de71 100644 --- a/crates/derive/src/test_utils/channel_provider.rs +++ b/crates/derive/src/test_utils/channel_provider.rs @@ -3,9 +3,10 @@ //! [ChannelBank]: crate::stages::ChannelBank use crate::{ - errors::{PipelineError, PipelineResult}, + errors::PipelineError, stages::NextFrameProvider, - traits::{OriginAdvancer, OriginProvider, Signal, SignalReceiver}, + traits::{OriginAdvancer, OriginProvider, SignalReceiver}, + types::{PipelineResult, Signal}, }; use alloc::{boxed::Box, vec::Vec}; use async_trait::async_trait; diff --git a/crates/derive/src/test_utils/channel_reader.rs b/crates/derive/src/test_utils/channel_reader.rs index 043e83296..6e4ff00ac 100644 --- a/crates/derive/src/test_utils/channel_reader.rs +++ b/crates/derive/src/test_utils/channel_reader.rs @@ -3,9 +3,10 @@ //! [ChannelReader]: crate::stages::ChannelReader use crate::{ - errors::{PipelineError, PipelineResult}, + errors::PipelineError, stages::ChannelReaderProvider, - traits::{OriginAdvancer, OriginProvider, Signal, SignalReceiver}, + traits::{OriginAdvancer, OriginProvider, SignalReceiver}, + types::{PipelineResult, Signal}, }; use alloc::{boxed::Box, vec::Vec}; use alloy_primitives::Bytes; diff --git a/crates/derive/src/test_utils/data_availability_provider.rs b/crates/derive/src/test_utils/data_availability_provider.rs index aba59edcb..3628f585b 100644 --- a/crates/derive/src/test_utils/data_availability_provider.rs +++ b/crates/derive/src/test_utils/data_availability_provider.rs @@ -1,8 +1,9 @@ //! An implementation of the [DataAvailabilityProvider] trait for tests. use crate::{ - errors::{PipelineError, PipelineResult}, + errors::PipelineError, traits::{AsyncIterator, DataAvailabilityProvider}, + types::PipelineResult, }; use alloc::{boxed::Box, vec, vec::Vec}; use alloy_primitives::{Address, Bytes}; diff --git a/crates/derive/src/test_utils/frame_queue.rs b/crates/derive/src/test_utils/frame_queue.rs index db23a6a65..6f13e9f68 100644 --- a/crates/derive/src/test_utils/frame_queue.rs +++ b/crates/derive/src/test_utils/frame_queue.rs @@ -1,9 +1,10 @@ //! Mock types for the [FrameQueue] stage. use crate::{ - errors::{PipelineError, PipelineResult}, + errors::PipelineError, stages::FrameQueueProvider, - traits::{OriginAdvancer, OriginProvider, Signal, SignalReceiver}, + traits::{OriginAdvancer, OriginProvider, SignalReceiver}, + types::{PipelineResult, Signal}, }; use alloc::{boxed::Box, vec::Vec}; use alloy_primitives::Bytes; diff --git a/crates/derive/src/test_utils/pipeline.rs b/crates/derive/src/test_utils/pipeline.rs index 9da144f79..c55e17665 100644 --- a/crates/derive/src/test_utils/pipeline.rs +++ b/crates/derive/src/test_utils/pipeline.rs @@ -4,6 +4,7 @@ use crate::{ stages::BatchProvider, test_utils::{TestChainProvider, TestL2ChainProvider}, + types::PipelineResult, }; use alloc::{boxed::Box, sync::Arc}; use op_alloy_genesis::RollupConfig; @@ -12,14 +13,15 @@ use op_alloy_rpc_types_engine::OpAttributesWithParent; // Re-export these types used internally to the test pipeline. use crate::{ - errors::{PipelineError, PipelineResult}, + errors::PipelineError, pipeline::{DerivationPipeline, PipelineBuilder}, stages::{ AttributesQueue, BatchStream, ChannelProvider, ChannelReader, FrameQueue, L1Retrieval, L1Traversal, }, test_utils::{TestAttributesBuilder, TestDAP}, - traits::{NextAttributes, OriginAdvancer, OriginProvider, Signal, SignalReceiver}, + traits::{NextAttributes, OriginAdvancer, OriginProvider, SignalReceiver}, + types::Signal, }; /// A fully custom [NextAttributes]. diff --git a/crates/derive/src/traits/attributes.rs b/crates/derive/src/traits/attributes.rs index 070ac724e..42d0b3f23 100644 --- a/crates/derive/src/traits/attributes.rs +++ b/crates/derive/src/traits/attributes.rs @@ -1,6 +1,6 @@ //! Contains traits for working with payload attributes and their providers. -use crate::errors::PipelineResult; +use crate::types::PipelineResult; use alloc::boxed::Box; use alloy_eips::BlockNumHash; use async_trait::async_trait; diff --git a/crates/derive/src/traits/data_sources.rs b/crates/derive/src/traits/data_sources.rs index e21c82552..22937cc96 100644 --- a/crates/derive/src/traits/data_sources.rs +++ b/crates/derive/src/traits/data_sources.rs @@ -1,10 +1,7 @@ //! Contains traits that describe the functionality of various data sources used in the derivation //! pipeline's stages. -use crate::{ - errors::{PipelineErrorKind, PipelineResult}, - sources::IndexedBlobHash, -}; +use crate::{errors::PipelineErrorKind, sources::IndexedBlobHash, types::PipelineResult}; use alloc::{boxed::Box, fmt::Debug, string::ToString, vec::Vec}; use alloy_eips::eip4844::Blob; use alloy_primitives::Bytes; diff --git a/crates/derive/src/traits/mod.rs b/crates/derive/src/traits/mod.rs index 5fcb7ee17..88c2248b6 100644 --- a/crates/derive/src/traits/mod.rs +++ b/crates/derive/src/traits/mod.rs @@ -2,7 +2,7 @@ //! pipeline. mod pipeline; -pub use pipeline::{ActivationSignal, Pipeline, ResetSignal, Signal, StepResult}; +pub use pipeline::Pipeline; mod providers; pub use providers::{BatchValidationProviderDerive, ChainProvider, L2ChainProvider}; diff --git a/crates/derive/src/traits/pipeline.rs b/crates/derive/src/traits/pipeline.rs index 3b1f28d55..c5be6f52b 100644 --- a/crates/derive/src/traits/pipeline.rs +++ b/crates/derive/src/traits/pipeline.rs @@ -1,96 +1,13 @@ //! Defines the interface for the core derivation pipeline. use super::OriginProvider; -use crate::errors::PipelineErrorKind; +use crate::types::StepResult; use alloc::boxed::Box; use async_trait::async_trait; use core::iter::Iterator; -use op_alloy_genesis::SystemConfig; -use op_alloy_protocol::{BlockInfo, L2BlockInfo}; +use op_alloy_protocol::L2BlockInfo; use op_alloy_rpc_types_engine::OpAttributesWithParent; -/// A pipeline error. -#[derive(Debug, PartialEq, Eq)] -pub enum StepResult { - /// Attributes were successfully prepared. - PreparedAttributes, - /// Origin was advanced. - AdvancedOrigin, - /// Origin advance failed. - OriginAdvanceErr(PipelineErrorKind), - /// Step failed. - StepFailed(PipelineErrorKind), -} - -/// A signal to send to the pipeline. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -#[allow(clippy::large_enum_variant)] -pub enum Signal { - /// Reset the pipeline. - Reset(ResetSignal), - /// Hardfork Activation. - Activation(ActivationSignal), - /// Flush the currently active channel. - FlushChannel, -} - -impl Signal { - /// Sets the [SystemConfig] for the signal. - pub const fn with_system_config(self, system_config: SystemConfig) -> Self { - match self { - Self::Reset(reset) => reset.with_system_config(system_config).signal(), - Self::Activation(activation) => activation.with_system_config(system_config).signal(), - Self::FlushChannel => Self::FlushChannel, - } - } -} - -/// A pipeline reset signal. -#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] -pub struct ResetSignal { - /// The L2 safe head to reset to. - pub l2_safe_head: L2BlockInfo, - /// The L1 origin to reset to. - pub l1_origin: BlockInfo, - /// The optional [SystemConfig] to reset with. - pub system_config: Option, -} - -impl ResetSignal { - /// Creates a new [Signal::Reset] from the [ResetSignal]. - pub const fn signal(self) -> Signal { - Signal::Reset(self) - } - - /// Sets the [SystemConfig] for the signal. - pub const fn with_system_config(self, system_config: SystemConfig) -> Self { - Self { system_config: Some(system_config), ..self } - } -} - -/// A pipeline hardfork activation signal. -#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] -pub struct ActivationSignal { - /// The L2 safe head to reset to. - pub l2_safe_head: L2BlockInfo, - /// The L1 origin to reset to. - pub l1_origin: BlockInfo, - /// The optional [SystemConfig] to reset with. - pub system_config: Option, -} - -impl ActivationSignal { - /// Creates a new [Signal::Activation] from the [ActivationSignal]. - pub const fn signal(self) -> Signal { - Signal::Activation(self) - } - - /// Sets the [SystemConfig] for the signal. - pub const fn with_system_config(self, system_config: SystemConfig) -> Self { - Self { system_config: Some(system_config), ..self } - } -} - /// This trait defines the interface for interacting with the derivation pipeline. #[async_trait] pub trait Pipeline: OriginProvider + Iterator { @@ -100,39 +17,3 @@ pub trait Pipeline: OriginProvider + Iterator { /// Attempts to progress the pipeline. async fn step(&mut self, cursor: L2BlockInfo) -> StepResult; } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_reset_signal() { - let signal = ResetSignal::default(); - assert_eq!(signal.signal(), Signal::Reset(signal)); - } - - #[test] - fn test_activation_signal() { - let signal = ActivationSignal::default(); - assert_eq!(signal.signal(), Signal::Activation(signal)); - } - - #[test] - fn test_signal_with_system_config() { - let signal = ResetSignal::default(); - let system_config = SystemConfig::default(); - assert_eq!( - signal.with_system_config(system_config).signal(), - Signal::Reset(ResetSignal { system_config: Some(system_config), ..signal }) - ); - - let signal = ActivationSignal::default(); - let system_config = SystemConfig::default(); - assert_eq!( - signal.with_system_config(system_config).signal(), - Signal::Activation(ActivationSignal { system_config: Some(system_config), ..signal }) - ); - - assert_eq!(Signal::FlushChannel.with_system_config(system_config), Signal::FlushChannel); - } -} diff --git a/crates/derive/src/traits/stages.rs b/crates/derive/src/traits/stages.rs index 5bc9d8bc0..d4716178a 100644 --- a/crates/derive/src/traits/stages.rs +++ b/crates/derive/src/traits/stages.rs @@ -4,7 +4,7 @@ use alloc::boxed::Box; use async_trait::async_trait; use op_alloy_protocol::BlockInfo; -use crate::{errors::PipelineResult, traits::Signal}; +use crate::types::{PipelineResult, Signal}; /// Providers a way for the pipeline to accept a signal from the driver. #[async_trait] diff --git a/crates/derive/src/types/mod.rs b/crates/derive/src/types/mod.rs new file mode 100644 index 000000000..2ab302ab2 --- /dev/null +++ b/crates/derive/src/types/mod.rs @@ -0,0 +1,7 @@ +//! Primitive types for `kona-derive`. + +mod results; +pub use results::{PipelineResult, StepResult}; + +mod signals; +pub use signals::{ActivationSignal, ResetSignal, Signal}; diff --git a/crates/derive/src/types/results.rs b/crates/derive/src/types/results.rs new file mode 100644 index 000000000..4697e97a1 --- /dev/null +++ b/crates/derive/src/types/results.rs @@ -0,0 +1,19 @@ +//! Result types for the `kona-derive` pipeline. + +use crate::errors::PipelineErrorKind; + +/// A result type for the derivation pipeline stages. +pub type PipelineResult = Result; + +/// A pipeline error. +#[derive(Debug, PartialEq, Eq)] +pub enum StepResult { + /// Attributes were successfully prepared. + PreparedAttributes, + /// Origin was advanced. + AdvancedOrigin, + /// Origin advance failed. + OriginAdvanceErr(PipelineErrorKind), + /// Step failed. + StepFailed(PipelineErrorKind), +} diff --git a/crates/derive/src/types/signals.rs b/crates/derive/src/types/signals.rs new file mode 100644 index 000000000..105d82856 --- /dev/null +++ b/crates/derive/src/types/signals.rs @@ -0,0 +1,113 @@ +//! Signal types for the `kona-derive` pipeline. +//! +//! Signals are the primary method of communication in the downwards direction +//! of the pipeline. They allow the pipeline driver to perform actions such as +//! resetting all stages in the pipeline through message passing. + +use op_alloy_genesis::SystemConfig; +use op_alloy_protocol::{BlockInfo, L2BlockInfo}; + +/// A signal to send to the pipeline. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[allow(clippy::large_enum_variant)] +pub enum Signal { + /// Reset the pipeline. + Reset(ResetSignal), + /// Hardfork Activation. + Activation(ActivationSignal), + /// Flush the currently active channel. + FlushChannel, +} + +impl Signal { + /// Sets the [SystemConfig] for the signal. + pub const fn with_system_config(self, system_config: SystemConfig) -> Self { + match self { + Self::Reset(reset) => reset.with_system_config(system_config).signal(), + Self::Activation(activation) => activation.with_system_config(system_config).signal(), + Self::FlushChannel => Self::FlushChannel, + } + } +} + +/// A pipeline reset signal. +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] +pub struct ResetSignal { + /// The L2 safe head to reset to. + pub l2_safe_head: L2BlockInfo, + /// The L1 origin to reset to. + pub l1_origin: BlockInfo, + /// The optional [SystemConfig] to reset with. + pub system_config: Option, +} + +impl ResetSignal { + /// Creates a new [Signal::Reset] from the [ResetSignal]. + pub const fn signal(self) -> Signal { + Signal::Reset(self) + } + + /// Sets the [SystemConfig] for the signal. + pub const fn with_system_config(self, system_config: SystemConfig) -> Self { + Self { system_config: Some(system_config), ..self } + } +} + +/// A pipeline hardfork activation signal. +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] +pub struct ActivationSignal { + /// The L2 safe head to reset to. + pub l2_safe_head: L2BlockInfo, + /// The L1 origin to reset to. + pub l1_origin: BlockInfo, + /// The optional [SystemConfig] to reset with. + pub system_config: Option, +} + +impl ActivationSignal { + /// Creates a new [Signal::Activation] from the [ActivationSignal]. + pub const fn signal(self) -> Signal { + Signal::Activation(self) + } + + /// Sets the [SystemConfig] for the signal. + pub const fn with_system_config(self, system_config: SystemConfig) -> Self { + Self { system_config: Some(system_config), ..self } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_reset_signal() { + let signal = ResetSignal::default(); + assert_eq!(signal.signal(), Signal::Reset(signal)); + } + + #[test] + fn test_activation_signal() { + let signal = ActivationSignal::default(); + assert_eq!(signal.signal(), Signal::Activation(signal)); + } + + #[test] + fn test_signal_with_system_config() { + let signal = ResetSignal::default(); + let system_config = SystemConfig::default(); + assert_eq!( + signal.with_system_config(system_config).signal(), + Signal::Reset(ResetSignal { system_config: Some(system_config), ..signal }) + ); + + let signal = ActivationSignal::default(); + let system_config = SystemConfig::default(); + assert_eq!( + signal.with_system_config(system_config).signal(), + Signal::Activation(ActivationSignal { system_config: Some(system_config), ..signal }) + ); + + assert_eq!(Signal::FlushChannel.with_system_config(system_config), Signal::FlushChannel); + } +}