From 08eee40fd121c2fd5a17d98ea890f5b6ae3c7686 Mon Sep 17 00:00:00 2001 From: refcell Date: Tue, 8 Oct 2024 12:43:44 -0400 Subject: [PATCH] chore(derive): hoist attributes queue test utils --- crates/derive/src/pipeline/core.rs | 14 ++- crates/derive/src/stages/attributes_queue.rs | 18 ++- crates/derive/src/stages/test_utils/mod.rs | 5 - crates/derive/src/test_utils/mod.rs | 116 +----------------- crates/derive/src/test_utils/pipeline.rs | 113 +++++++++++++++++ .../stages}/attributes_queue.rs | 26 ++-- crates/derive/src/test_utils/stages/mod.rs | 6 + 7 files changed, 156 insertions(+), 142 deletions(-) create mode 100644 crates/derive/src/test_utils/pipeline.rs rename crates/derive/src/{stages/test_utils => test_utils/stages}/attributes_queue.rs (77%) create mode 100644 crates/derive/src/test_utils/stages/mod.rs diff --git a/crates/derive/src/pipeline/core.rs b/crates/derive/src/pipeline/core.rs index 7619cdc33..96028b45b 100644 --- a/crates/derive/src/pipeline/core.rs +++ b/crates/derive/src/pipeline/core.rs @@ -198,11 +198,17 @@ where #[cfg(test)] mod tests { - use super::*; - use crate::test_utils::*; + use crate::{ + pipeline::{DerivationPipeline, PipelineError, StepResult}, + test_utils::*, + traits::{Pipeline, Signal}, + }; + use alloc::sync::Arc; use alloy_rpc_types_engine::PayloadAttributes; - use op_alloy_genesis::SystemConfig; - use op_alloy_rpc_types_engine::OptimismPayloadAttributes; + use kona_providers::test_utils::TestL2ChainProvider; + use op_alloy_genesis::{RollupConfig, SystemConfig}; + use op_alloy_protocol::{BlockInfo, L2BlockInfo}; + use op_alloy_rpc_types_engine::{OptimismAttributesWithParent, OptimismPayloadAttributes}; fn default_test_payload_attributes() -> OptimismAttributesWithParent { OptimismAttributesWithParent { diff --git a/crates/derive/src/stages/attributes_queue.rs b/crates/derive/src/stages/attributes_queue.rs index 7a414b325..b6c4ee7f8 100644 --- a/crates/derive/src/stages/attributes_queue.rs +++ b/crates/derive/src/stages/attributes_queue.rs @@ -232,9 +232,7 @@ mod tests { use super::*; use crate::{ errors::{BuilderError, PipelineErrorKind}, - stages::test_utils::{ - new_attributes_provider, MockAttributesBuilder, MockAttributesProvider, - }, + test_utils::{new_test_attributes_provider, TestAttributesBuilder, TestAttributesProvider}, }; use alloc::{sync::Arc, vec, vec::Vec}; use alloy_primitives::{b256, Address, Bytes, B256}; @@ -260,10 +258,10 @@ mod tests { cfg: Option, origin: Option, batches: Vec>, - ) -> AttributesQueue { + ) -> AttributesQueue { let cfg = cfg.unwrap_or_default(); - let mock_batch_queue = new_attributes_provider(origin, batches); - let mock_attributes_builder = MockAttributesBuilder::default(); + let mock_batch_queue = new_test_attributes_provider(origin, batches); + let mock_attributes_builder = TestAttributesBuilder::default(); AttributesQueue::new(Arc::new(cfg), mock_batch_queue, mock_attributes_builder) } @@ -349,10 +347,10 @@ mod tests { #[tokio::test] async fn test_create_next_attributes_success() { let cfg = RollupConfig::default(); - let mock = new_attributes_provider(None, vec![]); + let mock = new_test_attributes_provider(None, vec![]); let mut payload_attributes = default_optimism_payload_attributes(); let mock_builder = - MockAttributesBuilder { attributes: vec![Ok(payload_attributes.clone())] }; + TestAttributesBuilder { attributes: vec![Ok(payload_attributes.clone())] }; let mut aq = AttributesQueue::new(Arc::new(cfg), mock, mock_builder); let parent = L2BlockInfo::default(); let txs = vec![Bytes::default(), Bytes::default()]; @@ -378,9 +376,9 @@ mod tests { #[tokio::test] async fn test_next_attributes_load_batch_last_in_span() { let cfg = RollupConfig::default(); - let mock = new_attributes_provider(None, vec![Ok(Default::default())]); + let mock = new_test_attributes_provider(None, vec![Ok(Default::default())]); let mut pa = default_optimism_payload_attributes(); - let mock_builder = MockAttributesBuilder { attributes: vec![Ok(pa.clone())] }; + let mock_builder = TestAttributesBuilder { attributes: vec![Ok(pa.clone())] }; let mut aq = AttributesQueue::new(Arc::new(cfg), mock, mock_builder); // If we load the batch, we should get the last in span. // But it won't take it so it will be available in the next_attributes call. diff --git a/crates/derive/src/stages/test_utils/mod.rs b/crates/derive/src/stages/test_utils/mod.rs index 5c0b1c929..e84147bc4 100644 --- a/crates/derive/src/stages/test_utils/mod.rs +++ b/crates/derive/src/stages/test_utils/mod.rs @@ -7,11 +7,6 @@ pub use batch_queue::MockBatchQueueProvider; mod batch_stream; pub use batch_stream::MockBatchStreamProvider; -mod attributes_queue; -pub use attributes_queue::{ - new_attributes_provider, MockAttributesBuilder, MockAttributesProvider, -}; - mod frame_queue; pub use frame_queue::MockFrameQueueProvider; diff --git a/crates/derive/src/test_utils/mod.rs b/crates/derive/src/test_utils/mod.rs index 85de9c286..9a6a7376d 100644 --- a/crates/derive/src/test_utils/mod.rs +++ b/crates/derive/src/test_utils/mod.rs @@ -1,114 +1,10 @@ //! Test Utilities for `kona-derive`. -//! -//! This includes top-level [crate::pipeline::DerivationPipeline] -//! test utilities as well as individual stage test utilities. -use alloc::{boxed::Box, sync::Arc}; -use op_alloy_genesis::{RollupConfig, SystemConfig}; -use op_alloy_protocol::{BlockInfo, L2BlockInfo}; -use op_alloy_rpc_types_engine::OptimismAttributesWithParent; - -// Re-export these types used internally to the test pipeline. -pub use crate::{ - batch::SingleBatch, - errors::PipelineError, - pipeline::{DerivationPipeline, PipelineBuilder, PipelineResult}, - stages::{ - test_utils::MockAttributesBuilder, AttributesProvider, AttributesQueue, BatchQueue, - BatchStream, ChannelBank, ChannelReader, FrameQueue, L1Retrieval, L1Traversal, - }, - traits::{ - test_utils::TestDAP, FlushableStage, NextAttributes, OriginAdvancer, OriginProvider, - ResettableStage, - }, +pub mod pipeline; +pub use pipeline::{ + new_test_pipeline, TestAttributesQueue, TestBatchQueue, TestBatchStream, TestChannelBank, + TestFrameQueue, TestL1Retrieval, TestL1Traversal, TestNextAttributes, TestPipeline, }; -pub use kona_providers::test_utils::{TestChainProvider, TestL2ChainProvider}; - -/// A fully custom [NextAttributes]. -#[derive(Default, Debug, Clone)] -pub struct TestNextAttributes { - /// The next [OptimismAttributesWithParent] to return. - pub next_attributes: Option, -} - -#[async_trait::async_trait] -impl FlushableStage for TestNextAttributes { - /// Flushes the stage. - async fn flush_channel(&mut self) -> PipelineResult<()> { - Ok(()) - } -} - -#[async_trait::async_trait] -impl ResettableStage for TestNextAttributes { - /// Resets the derivation stage to its initial state. - async fn reset(&mut self, _: BlockInfo, _: &SystemConfig) -> PipelineResult<()> { - Ok(()) - } -} - -#[async_trait::async_trait] -impl OriginProvider for TestNextAttributes { - /// Returns the current origin. - fn origin(&self) -> Option { - Some(BlockInfo::default()) - } -} - -#[async_trait::async_trait] -impl OriginAdvancer for TestNextAttributes { - /// Advances the origin to the given block. - async fn advance_origin(&mut self) -> PipelineResult<()> { - Ok(()) - } -} - -#[async_trait::async_trait] -impl NextAttributes for TestNextAttributes { - /// Returns the next valid attributes. - async fn next_attributes( - &mut self, - _: L2BlockInfo, - ) -> PipelineResult { - self.next_attributes.take().ok_or(PipelineError::Eof.temp()) - } -} - -/// An [L1Traversal] using test providers and sources. -pub type TestL1Traversal = L1Traversal; - -/// An [L1Retrieval] stage using test providers and sources. -pub type TestL1Retrieval = L1Retrieval; - -/// A [FrameQueue] using test providers and sources. -pub type TestFrameQueue = FrameQueue; - -/// A [ChannelBank] using test providers and sources. -pub type TestChannelBank = ChannelBank; - -/// A [ChannelReader] using test providers and sources. -pub type TestChannelReader = ChannelReader; - -/// A [BatchStream] using test providers and sources. -pub type TestBatchStream = BatchStream; - -/// A [BatchQueue] using test providers and sources. -pub type TestBatchQueue = BatchQueue; - -/// An [AttributesQueue] using test providers and sources. -pub type TestAttributesQueue = AttributesQueue; - -/// A [DerivationPipeline] using test providers and sources. -pub type TestPipeline = DerivationPipeline; -/// Constructs a [DerivationPipeline] using test providers and sources. -pub fn new_test_pipeline() -> TestPipeline { - PipelineBuilder::new() - .rollup_config(Arc::new(RollupConfig::default())) - .origin(BlockInfo::default()) - .dap_source(TestDAP::default()) - .builder(MockAttributesBuilder::default()) - .chain_provider(TestChainProvider::default()) - .l2_chain_provider(TestL2ChainProvider::default()) - .build() -} +pub mod stages; +pub use stages::{new_test_attributes_provider, TestAttributesBuilder, TestAttributesProvider}; diff --git a/crates/derive/src/test_utils/pipeline.rs b/crates/derive/src/test_utils/pipeline.rs new file mode 100644 index 000000000..4f04cb742 --- /dev/null +++ b/crates/derive/src/test_utils/pipeline.rs @@ -0,0 +1,113 @@ +//! Test Utilities for the [crate::pipeline::DerivationPipeline] +//! as well as its stages and providers. + +use alloc::{boxed::Box, sync::Arc}; +use op_alloy_genesis::{RollupConfig, SystemConfig}; +use op_alloy_protocol::{BlockInfo, L2BlockInfo}; +use op_alloy_rpc_types_engine::OptimismAttributesWithParent; + +// Re-export these types used internally to the test pipeline. +pub use crate::{ + batch::SingleBatch, + errors::PipelineError, + pipeline::{DerivationPipeline, PipelineBuilder, PipelineResult}, + stages::{ + AttributesProvider, AttributesQueue, BatchQueue, BatchStream, ChannelBank, ChannelReader, + FrameQueue, L1Retrieval, L1Traversal, + }, + test_utils::TestAttributesBuilder, + traits::{ + test_utils::TestDAP, FlushableStage, NextAttributes, OriginAdvancer, OriginProvider, + ResettableStage, + }, +}; +pub use kona_providers::test_utils::{TestChainProvider, TestL2ChainProvider}; + +/// A fully custom [NextAttributes]. +#[derive(Default, Debug, Clone)] +pub struct TestNextAttributes { + /// The next [OptimismAttributesWithParent] to return. + pub next_attributes: Option, +} + +#[async_trait::async_trait] +impl FlushableStage for TestNextAttributes { + /// Flushes the stage. + async fn flush_channel(&mut self) -> PipelineResult<()> { + Ok(()) + } +} + +#[async_trait::async_trait] +impl ResettableStage for TestNextAttributes { + /// Resets the derivation stage to its initial state. + async fn reset(&mut self, _: BlockInfo, _: &SystemConfig) -> PipelineResult<()> { + Ok(()) + } +} + +#[async_trait::async_trait] +impl OriginProvider for TestNextAttributes { + /// Returns the current origin. + fn origin(&self) -> Option { + Some(BlockInfo::default()) + } +} + +#[async_trait::async_trait] +impl OriginAdvancer for TestNextAttributes { + /// Advances the origin to the given block. + async fn advance_origin(&mut self) -> PipelineResult<()> { + Ok(()) + } +} + +#[async_trait::async_trait] +impl NextAttributes for TestNextAttributes { + /// Returns the next valid attributes. + async fn next_attributes( + &mut self, + _: L2BlockInfo, + ) -> PipelineResult { + self.next_attributes.take().ok_or(PipelineError::Eof.temp()) + } +} + +/// An [L1Traversal] using test providers and sources. +pub type TestL1Traversal = L1Traversal; + +/// An [L1Retrieval] stage using test providers and sources. +pub type TestL1Retrieval = L1Retrieval; + +/// A [FrameQueue] using test providers and sources. +pub type TestFrameQueue = FrameQueue; + +/// A [ChannelBank] using test providers and sources. +pub type TestChannelBank = ChannelBank; + +/// A [ChannelReader] using test providers and sources. +pub type TestChannelReader = ChannelReader; + +/// A [BatchStream] using test providers and sources. +pub type TestBatchStream = BatchStream; + +/// A [BatchQueue] using test providers and sources. +pub type TestBatchQueue = BatchQueue; + +/// An [AttributesQueue] using test providers and sources. +pub type TestAttributesQueue = AttributesQueue; + +/// A [DerivationPipeline] using test providers and sources. +pub type TestPipeline = DerivationPipeline; + +/// Constructs a [DerivationPipeline] using test providers and sources. +pub fn new_test_pipeline() -> TestPipeline { + PipelineBuilder::new() + .rollup_config(Arc::new(RollupConfig::default())) + .origin(BlockInfo::default()) + .dap_source(TestDAP::default()) + .builder(TestAttributesBuilder::default()) + .chain_provider(TestChainProvider::default()) + .l2_chain_provider(TestL2ChainProvider::default()) + .build() +} diff --git a/crates/derive/src/stages/test_utils/attributes_queue.rs b/crates/derive/src/test_utils/stages/attributes_queue.rs similarity index 77% rename from crates/derive/src/stages/test_utils/attributes_queue.rs rename to crates/derive/src/test_utils/stages/attributes_queue.rs index bdd3ca3b6..6f0409d4c 100644 --- a/crates/derive/src/stages/test_utils/attributes_queue.rs +++ b/crates/derive/src/test_utils/stages/attributes_queue.rs @@ -3,7 +3,7 @@ use crate::{ batch::SingleBatch, errors::{BuilderError, PipelineError, PipelineErrorKind, PipelineResult}, - stages::attributes_queue::AttributesProvider, + stages::AttributesProvider, traits::{AttributesBuilder, OriginAdvancer, OriginProvider, ResettableStage}, }; use alloc::{boxed::Box, string::ToString, vec::Vec}; @@ -15,14 +15,14 @@ use op_alloy_rpc_types_engine::OptimismPayloadAttributes; /// A mock implementation of the [`AttributesBuilder`] for testing. #[derive(Debug, Default)] -pub struct MockAttributesBuilder { +pub struct TestAttributesBuilder { /// The attributes to return. pub attributes: Vec>, } #[async_trait] -impl AttributesBuilder for MockAttributesBuilder { - /// Prepares the [PayloadAttributes] for the next payload. +impl AttributesBuilder for TestAttributesBuilder { + /// Prepares the [OptimismPayloadAttributes] for the next payload. async fn prepare_payload_attributes( &mut self, _l2_parent: L2BlockInfo, @@ -40,35 +40,35 @@ impl AttributesBuilder for MockAttributesBuilder { /// A mock implementation of the [`BatchQueue`] stage for testing. #[derive(Debug, Default)] -pub struct MockAttributesProvider { +pub struct TestAttributesProvider { /// The origin of the L1 block. origin: Option, /// A list of batches to return. batches: Vec>, } -impl OriginProvider for MockAttributesProvider { +impl OriginProvider for TestAttributesProvider { fn origin(&self) -> Option { self.origin } } #[async_trait] -impl OriginAdvancer for MockAttributesProvider { +impl OriginAdvancer for TestAttributesProvider { async fn advance_origin(&mut self) -> PipelineResult<()> { Ok(()) } } #[async_trait] -impl ResettableStage for MockAttributesProvider { +impl ResettableStage for TestAttributesProvider { async fn reset(&mut self, _base: BlockInfo, _cfg: &SystemConfig) -> PipelineResult<()> { Ok(()) } } #[async_trait] -impl AttributesProvider for MockAttributesProvider { +impl AttributesProvider for TestAttributesProvider { async fn next_batch(&mut self, _parent: L2BlockInfo) -> PipelineResult { self.batches.pop().ok_or(PipelineError::Eof.temp())? } @@ -78,10 +78,10 @@ impl AttributesProvider for MockAttributesProvider { } } -/// Creates a new [`MockAttributesProvider`] with the given origin and batches. -pub const fn new_attributes_provider( +/// Creates a new [`TestAttributesProvider`] with the given origin and batches. +pub const fn new_test_attributes_provider( origin: Option, batches: Vec>, -) -> MockAttributesProvider { - MockAttributesProvider { origin, batches } +) -> TestAttributesProvider { + TestAttributesProvider { origin, batches } } diff --git a/crates/derive/src/test_utils/stages/mod.rs b/crates/derive/src/test_utils/stages/mod.rs new file mode 100644 index 000000000..2a2f56929 --- /dev/null +++ b/crates/derive/src/test_utils/stages/mod.rs @@ -0,0 +1,6 @@ +//! Test Utilities for `kona-derive`'s stages. + +pub mod attributes_queue; +pub use attributes_queue::{ + new_test_attributes_provider, TestAttributesBuilder, TestAttributesProvider, +};