Skip to content

Commit

Permalink
chore(derive): hoist attributes queue test utils (#654)
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell authored Oct 8, 2024
1 parent fc39e3b commit b1d89dd
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 142 deletions.
14 changes: 10 additions & 4 deletions crates/derive/src/pipeline/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 8 additions & 10 deletions crates/derive/src/stages/attributes_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -260,10 +258,10 @@ mod tests {
cfg: Option<RollupConfig>,
origin: Option<BlockInfo>,
batches: Vec<PipelineResult<SingleBatch>>,
) -> AttributesQueue<MockAttributesProvider, MockAttributesBuilder> {
) -> AttributesQueue<TestAttributesProvider, TestAttributesBuilder> {
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)
}

Expand Down Expand Up @@ -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()];
Expand All @@ -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.
Expand Down
5 changes: 0 additions & 5 deletions crates/derive/src/stages/test_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
116 changes: 6 additions & 110 deletions crates/derive/src/test_utils/mod.rs
Original file line number Diff line number Diff line change
@@ -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<OptimismAttributesWithParent>,
}

#[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<BlockInfo> {
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<OptimismAttributesWithParent> {
self.next_attributes.take().ok_or(PipelineError::Eof.temp())
}
}

/// An [L1Traversal] using test providers and sources.
pub type TestL1Traversal = L1Traversal<TestChainProvider>;

/// An [L1Retrieval] stage using test providers and sources.
pub type TestL1Retrieval = L1Retrieval<TestDAP, TestL1Traversal>;

/// A [FrameQueue] using test providers and sources.
pub type TestFrameQueue = FrameQueue<TestL1Retrieval>;

/// A [ChannelBank] using test providers and sources.
pub type TestChannelBank = ChannelBank<TestFrameQueue>;

/// A [ChannelReader] using test providers and sources.
pub type TestChannelReader = ChannelReader<TestChannelBank>;

/// A [BatchStream] using test providers and sources.
pub type TestBatchStream = BatchStream<TestChannelReader, TestL2ChainProvider>;

/// A [BatchQueue] using test providers and sources.
pub type TestBatchQueue = BatchQueue<TestBatchStream, TestL2ChainProvider>;

/// An [AttributesQueue] using test providers and sources.
pub type TestAttributesQueue = AttributesQueue<TestBatchQueue, MockAttributesBuilder>;

/// A [DerivationPipeline] using test providers and sources.
pub type TestPipeline = DerivationPipeline<TestAttributesQueue, TestL2ChainProvider>;

/// 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};
113 changes: 113 additions & 0 deletions crates/derive/src/test_utils/pipeline.rs
Original file line number Diff line number Diff line change
@@ -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<OptimismAttributesWithParent>,
}

#[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<BlockInfo> {
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<OptimismAttributesWithParent> {
self.next_attributes.take().ok_or(PipelineError::Eof.temp())
}
}

/// An [L1Traversal] using test providers and sources.
pub type TestL1Traversal = L1Traversal<TestChainProvider>;

/// An [L1Retrieval] stage using test providers and sources.
pub type TestL1Retrieval = L1Retrieval<TestDAP, TestL1Traversal>;

/// A [FrameQueue] using test providers and sources.
pub type TestFrameQueue = FrameQueue<TestL1Retrieval>;

/// A [ChannelBank] using test providers and sources.
pub type TestChannelBank = ChannelBank<TestFrameQueue>;

/// A [ChannelReader] using test providers and sources.
pub type TestChannelReader = ChannelReader<TestChannelBank>;

/// A [BatchStream] using test providers and sources.
pub type TestBatchStream = BatchStream<TestChannelReader, TestL2ChainProvider>;

/// A [BatchQueue] using test providers and sources.
pub type TestBatchQueue = BatchQueue<TestBatchStream, TestL2ChainProvider>;

/// An [AttributesQueue] using test providers and sources.
pub type TestAttributesQueue = AttributesQueue<TestBatchQueue, TestAttributesBuilder>;

/// A [DerivationPipeline] using test providers and sources.
pub type TestPipeline = DerivationPipeline<TestAttributesQueue, TestL2ChainProvider>;

/// 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()
}
Loading

0 comments on commit b1d89dd

Please sign in to comment.