From c67fefa62e43a1b86009831be239ca4497c2bd89 Mon Sep 17 00:00:00 2001 From: refcell Date: Fri, 18 Oct 2024 16:27:05 -0400 Subject: [PATCH] chore: hoist trait test utilities --- crates/derive/src/sources/blobs.rs | 2 +- crates/derive/src/sources/ethereum.rs | 3 +- crates/derive/src/sources/variant.rs | 2 +- crates/derive/src/stages/l1_retrieval.rs | 2 +- crates/derive/src/test_utils/blob_provider.rs | 52 ++++++++++++++++++ .../data_availability_provider.rs} | 54 ++----------------- crates/derive/src/test_utils/mod.rs | 11 +++- crates/derive/src/test_utils/pipeline.rs | 16 +++--- crates/derive/src/traits/mod.rs | 3 -- 9 files changed, 76 insertions(+), 69 deletions(-) create mode 100644 crates/derive/src/test_utils/blob_provider.rs rename crates/derive/src/{traits/test_utils.rs => test_utils/data_availability_provider.rs} (51%) diff --git a/crates/derive/src/sources/blobs.rs b/crates/derive/src/sources/blobs.rs index 61363f59c..e5a218259 100644 --- a/crates/derive/src/sources/blobs.rs +++ b/crates/derive/src/sources/blobs.rs @@ -391,7 +391,7 @@ where #[cfg(test)] pub(crate) mod tests { use super::*; - use crate::{errors::PipelineErrorKind, traits::test_utils::TestBlobProvider}; + use crate::{errors::PipelineErrorKind, test_utils::TestBlobProvider}; use alloy_rlp::Decodable; use kona_providers::test_utils::TestChainProvider; diff --git a/crates/derive/src/sources/ethereum.rs b/crates/derive/src/sources/ethereum.rs index bf0045c12..0b2edf2a6 100644 --- a/crates/derive/src/sources/ethereum.rs +++ b/crates/derive/src/sources/ethereum.rs @@ -96,7 +96,8 @@ mod tests { use crate::{ sources::{EthereumDataSource, EthereumDataSourceVariant}, - traits::{test_utils::TestBlobProvider, AsyncIterator, DataAvailabilityProvider}, + test_utils::TestBlobProvider, + traits::{AsyncIterator, DataAvailabilityProvider}, }; #[tokio::test] diff --git a/crates/derive/src/sources/variant.rs b/crates/derive/src/sources/variant.rs index be38a9fae..90f0e504b 100644 --- a/crates/derive/src/sources/variant.rs +++ b/crates/derive/src/sources/variant.rs @@ -49,7 +49,7 @@ mod tests { use crate::{ sources::{BlobData, EthereumDataSourceVariant}, - traits::test_utils::TestBlobProvider, + test_utils::TestBlobProvider, }; #[tokio::test] diff --git a/crates/derive/src/stages/l1_retrieval.rs b/crates/derive/src/stages/l1_retrieval.rs index b909b2b95..4886627b9 100644 --- a/crates/derive/src/stages/l1_retrieval.rs +++ b/crates/derive/src/stages/l1_retrieval.rs @@ -142,7 +142,7 @@ mod tests { use super::*; use crate::{ stages::l1_traversal::tests::*, - traits::test_utils::{TestDAP, TestIter}, + test_utils::{TestDAP, TestIter}, }; use alloc::vec; use alloy_primitives::Bytes; diff --git a/crates/derive/src/test_utils/blob_provider.rs b/crates/derive/src/test_utils/blob_provider.rs new file mode 100644 index 000000000..f3a4f9055 --- /dev/null +++ b/crates/derive/src/test_utils/blob_provider.rs @@ -0,0 +1,52 @@ +//! An implementation of the [BlobProvider] trait for tests. + +use alloc::{boxed::Box, vec::Vec}; +use alloy_eips::eip4844::Blob; +use alloy_primitives::{map::HashMap, B256}; +use async_trait::async_trait; +use op_alloy_protocol::BlockInfo; + +use crate::{errors::BlobProviderError, sources::IndexedBlobHash, traits::BlobProvider}; + +/// A mock blob provider for testing. +#[derive(Debug, Clone, Default)] +pub struct TestBlobProvider { + /// Maps block hashes to blob data. + pub blobs: HashMap, + /// whether the blob provider should return an error. + pub should_error: bool, +} + +impl TestBlobProvider { + /// Insert a blob into the mock blob provider. + pub fn insert_blob(&mut self, hash: B256, blob: Blob) { + self.blobs.insert(hash, blob); + } + + /// Clears blobs from the mock blob provider. + pub fn clear(&mut self) { + self.blobs.clear(); + } +} + +#[async_trait] +impl BlobProvider for TestBlobProvider { + type Error = BlobProviderError; + + async fn get_blobs( + &mut self, + _block_ref: &BlockInfo, + blob_hashes: &[IndexedBlobHash], + ) -> Result>, Self::Error> { + if self.should_error { + return Err(BlobProviderError::SlotDerivation); + } + let mut blobs = Vec::new(); + for blob_hash in blob_hashes { + if let Some(data) = self.blobs.get(&blob_hash.hash) { + blobs.push(Box::new(*data)); + } + } + Ok(blobs) + } +} diff --git a/crates/derive/src/traits/test_utils.rs b/crates/derive/src/test_utils/data_availability_provider.rs similarity index 51% rename from crates/derive/src/traits/test_utils.rs rename to crates/derive/src/test_utils/data_availability_provider.rs index e723027b4..aba59edcb 100644 --- a/crates/derive/src/traits/test_utils.rs +++ b/crates/derive/src/test_utils/data_availability_provider.rs @@ -1,14 +1,11 @@ -//! Test Utilities for derive traits +//! An implementation of the [DataAvailabilityProvider] trait for tests. use crate::{ - errors::{BlobProviderError, PipelineError, PipelineResult}, - sources::IndexedBlobHash, - traits::{AsyncIterator, BlobProvider, DataAvailabilityProvider}, + errors::{PipelineError, PipelineResult}, + traits::{AsyncIterator, DataAvailabilityProvider}, }; use alloc::{boxed::Box, vec, vec::Vec}; -use alloy_eips::eip4844::Blob; -use alloy_primitives::{map::HashMap, Address, Bytes, B256}; -use anyhow::Result; +use alloy_primitives::{Address, Bytes}; use async_trait::async_trait; use core::fmt::Debug; use op_alloy_protocol::BlockInfo; @@ -55,46 +52,3 @@ impl DataAvailabilityProvider for TestDAP { Ok(TestIter { open_data_calls: vec![(*block_ref, self.batch_inbox_address)], results }) } } - -/// A mock blob provider for testing. -#[derive(Debug, Clone, Default)] -pub struct TestBlobProvider { - /// Maps block hashes to blob data. - pub blobs: HashMap, - /// whether the blob provider should return an error. - pub should_error: bool, -} - -impl TestBlobProvider { - /// Insert a blob into the mock blob provider. - pub fn insert_blob(&mut self, hash: B256, blob: Blob) { - self.blobs.insert(hash, blob); - } - - /// Clears blobs from the mock blob provider. - pub fn clear(&mut self) { - self.blobs.clear(); - } -} - -#[async_trait] -impl BlobProvider for TestBlobProvider { - type Error = BlobProviderError; - - async fn get_blobs( - &mut self, - _block_ref: &BlockInfo, - blob_hashes: &[IndexedBlobHash], - ) -> Result>, Self::Error> { - if self.should_error { - return Err(BlobProviderError::SlotDerivation); - } - let mut blobs = Vec::new(); - for blob_hash in blob_hashes { - if let Some(data) = self.blobs.get(&blob_hash.hash) { - blobs.push(Box::new(*data)); - } - } - Ok(blobs) - } -} diff --git a/crates/derive/src/test_utils/mod.rs b/crates/derive/src/test_utils/mod.rs index 5de1eb96a..dc7d8d9a3 100644 --- a/crates/derive/src/test_utils/mod.rs +++ b/crates/derive/src/test_utils/mod.rs @@ -1,11 +1,18 @@ //! Test Utilities for `kona-derive`. -pub mod pipeline; +mod pipeline; pub use pipeline::{ new_test_pipeline, TestAttributesQueue, TestBatchQueue, TestBatchStream, TestChannelProvider, - TestFrameQueue, TestL1Retrieval, TestL1Traversal, TestNextAttributes, TestPipeline, + TestChannelReader, TestFrameQueue, TestL1Retrieval, TestL1Traversal, TestNextAttributes, + TestPipeline, }; +mod blob_provider; +pub use blob_provider::TestBlobProvider; + +mod data_availability_provider; +pub use data_availability_provider::{TestDAP, TestIter}; + mod batch_queue; pub use batch_queue::TestBatchQueueProvider; diff --git a/crates/derive/src/test_utils/pipeline.rs b/crates/derive/src/test_utils/pipeline.rs index 8705d58dd..25eb2d1af 100644 --- a/crates/derive/src/test_utils/pipeline.rs +++ b/crates/derive/src/test_utils/pipeline.rs @@ -2,26 +2,22 @@ //! as well as its stages and providers. use alloc::{boxed::Box, sync::Arc}; +use kona_providers::test_utils::{TestChainProvider, TestL2ChainProvider}; use op_alloy_genesis::RollupConfig; use op_alloy_protocol::{BlockInfo, L2BlockInfo}; use op_alloy_rpc_types_engine::OpAttributesWithParent; -use crate::stages::ChannelProvider; // Re-export these types used internally to the test pipeline. -pub use crate::{ - batch::SingleBatch, +use crate::{ errors::PipelineError, pipeline::{DerivationPipeline, PipelineBuilder, PipelineResult}, stages::{ - AttributesProvider, AttributesQueue, BatchQueue, BatchStream, ChannelBank, ChannelReader, - FrameQueue, L1Retrieval, L1Traversal, - }, - test_utils::TestAttributesBuilder, - traits::{ - test_utils::TestDAP, NextAttributes, OriginAdvancer, OriginProvider, Signal, SignalReceiver, + AttributesQueue, BatchQueue, BatchStream, ChannelProvider, ChannelReader, FrameQueue, + L1Retrieval, L1Traversal, }, + test_utils::{TestAttributesBuilder, TestDAP}, + traits::{NextAttributes, OriginAdvancer, OriginProvider, Signal, SignalReceiver}, }; -pub use kona_providers::test_utils::{TestChainProvider, TestL2ChainProvider}; /// A fully custom [NextAttributes]. #[derive(Default, Debug, Clone)] diff --git a/crates/derive/src/traits/mod.rs b/crates/derive/src/traits/mod.rs index b7e8fb518..dfe8951d4 100644 --- a/crates/derive/src/traits/mod.rs +++ b/crates/derive/src/traits/mod.rs @@ -15,6 +15,3 @@ pub use reset::ResetProvider; mod stages; pub use stages::{OriginAdvancer, OriginProvider, SignalReceiver}; - -#[cfg(any(test, feature = "test-utils"))] -pub mod test_utils;