Skip to content

Commit

Permalink
chore: hoist trait test utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell committed Oct 18, 2024
1 parent a53374c commit c67fefa
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 69 deletions.
2 changes: 1 addition & 1 deletion crates/derive/src/sources/blobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion crates/derive/src/sources/ethereum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion crates/derive/src/sources/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ mod tests {

use crate::{
sources::{BlobData, EthereumDataSourceVariant},
traits::test_utils::TestBlobProvider,
test_utils::TestBlobProvider,
};

#[tokio::test]
Expand Down
2 changes: 1 addition & 1 deletion crates/derive/src/stages/l1_retrieval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
52 changes: 52 additions & 0 deletions crates/derive/src/test_utils/blob_provider.rs
Original file line number Diff line number Diff line change
@@ -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<B256, Blob>,
/// 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<Vec<Box<Blob>>, 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)
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<B256, Blob>,
/// 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<Vec<Box<Blob>>, 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)
}
}
11 changes: 9 additions & 2 deletions crates/derive/src/test_utils/mod.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
16 changes: 6 additions & 10 deletions crates/derive/src/test_utils/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
3 changes: 0 additions & 3 deletions crates/derive/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit c67fefa

Please sign in to comment.