Skip to content

Commit

Permalink
chore: refactor test providers (#623)
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell authored Oct 4, 2024
1 parent 44ad84c commit 56c42f6
Show file tree
Hide file tree
Showing 13 changed files with 253 additions and 196 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions crates/derive/src/attributes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,11 @@ async fn derive_deposits(
#[cfg(test)]
mod tests {
use super::*;
use crate::{
errors::ResetError, stages::test_utils::MockSystemConfigL2Fetcher,
traits::test_utils::TestChainProvider,
};
use crate::{errors::ResetError, stages::test_utils::MockSystemConfigL2Fetcher};
use alloc::vec;
use alloy_consensus::Header;
use alloy_primitives::{Log, LogData, B256, U256, U64};
use kona_providers::test_utils::TestChainProvider;
use op_alloy_genesis::SystemConfig;
use op_alloy_protocol::{BlockInfo, DepositError};

Expand Down
6 changes: 2 additions & 4 deletions crates/derive/src/batch/span_batch/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,14 +539,12 @@ impl SpanBatch {
#[cfg(test)]
mod tests {
use super::*;
use crate::{
stages::test_utils::{CollectingLayer, TraceStorage},
traits::test_utils::TestL2ChainProvider,
};
use crate::stages::test_utils::{CollectingLayer, TraceStorage};
use alloc::vec;
use alloy_consensus::Header;
use alloy_eips::BlockNumHash;
use alloy_primitives::{b256, Bytes};
use kona_providers::test_utils::TestL2ChainProvider;
use op_alloy_consensus::{OpBlock, OpTxType};
use op_alloy_genesis::ChainGenesis;
use tracing::Level;
Expand Down
6 changes: 2 additions & 4 deletions crates/derive/src/sources/ethereum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,13 @@ mod tests {
use alloy_consensus::TxEnvelope;
use alloy_eips::eip2718::Decodable2718;
use alloy_primitives::address;
use kona_providers::test_utils::TestChainProvider;
use op_alloy_genesis::{RollupConfig, SystemConfig};
use op_alloy_protocol::BlockInfo;

use crate::{
sources::{EthereumDataSource, EthereumDataSourceVariant},
traits::{
test_utils::{TestBlobProvider, TestChainProvider},
AsyncIterator, DataAvailabilityProvider,
},
traits::{test_utils::TestBlobProvider, AsyncIterator, DataAvailabilityProvider},
};

#[tokio::test]
Expand Down
10 changes: 4 additions & 6 deletions crates/derive/src/stages/batch_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,18 +490,16 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::{
stages::{
channel_reader::BatchReader,
test_utils::{CollectingLayer, MockBatchQueueProvider, TraceStorage},
},
traits::test_utils::TestL2ChainProvider,
use crate::stages::{
channel_reader::BatchReader,
test_utils::{CollectingLayer, MockBatchQueueProvider, TraceStorage},
};
use alloc::vec;
use alloy_consensus::Header;
use alloy_eips::{eip2718::Decodable2718, BlockNumHash};
use alloy_primitives::{address, b256, Address, Bytes, TxKind, B256, U256};
use alloy_rlp::{BytesMut, Encodable};
use kona_providers::test_utils::TestL2ChainProvider;
use op_alloy_consensus::{OpBlock, OpTxEnvelope, OpTxType, TxDeposit};
use op_alloy_genesis::ChainGenesis;
use op_alloy_protocol::{L1BlockInfoBedrock, L1BlockInfoTx};
Expand Down
2 changes: 1 addition & 1 deletion crates/derive/src/stages/batch_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ mod test {
use crate::{
batch::{SingleBatch, SpanBatchElement},
stages::test_utils::{CollectingLayer, MockBatchStreamProvider, TraceStorage},
traits::test_utils::TestL2ChainProvider,
};
use kona_providers::test_utils::TestL2ChainProvider;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

#[tokio::test]
Expand Down
3 changes: 2 additions & 1 deletion crates/derive/src/stages/l1_traversal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,11 @@ impl<F: ChainProvider + Send> ResettableStage for L1Traversal<F> {
#[cfg(test)]
pub(crate) mod tests {
use super::*;
use crate::{errors::PipelineErrorKind, traits::test_utils::TestChainProvider};
use crate::errors::PipelineErrorKind;
use alloc::vec;
use alloy_consensus::Receipt;
use alloy_primitives::{address, b256, hex, Bytes, Log, LogData, B256};
use kona_providers::test_utils::TestChainProvider;
use op_alloy_genesis::system::{CONFIG_UPDATE_EVENT_VERSION_0, CONFIG_UPDATE_TOPIC};

const L1_SYS_CONFIG_ADDR: Address = address!("1337000000000000000000000000000000000000");
Expand Down
178 changes: 2 additions & 176 deletions crates/derive/src/traits/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@ use crate::{
errors::{BlobProviderError, PipelineError, PipelineResult},
traits::{AsyncIterator, BlobProvider, DataAvailabilityProvider},
};
use alloc::{boxed::Box, sync::Arc, vec, vec::Vec};
use alloy_consensus::{Header, Receipt, TxEnvelope};
use alloc::{boxed::Box, vec, vec::Vec};
use alloy_eips::eip4844::Blob;
use alloy_primitives::{map::HashMap, Address, Bytes, B256};
use anyhow::Result;
use async_trait::async_trait;
use core::fmt::Debug;
use kona_primitives::IndexedBlobHash;
use kona_providers::{ChainProvider, L2ChainProvider};
use op_alloy_consensus::OpBlock;
use op_alloy_genesis::{RollupConfig, SystemConfig};
use op_alloy_protocol::{BlockInfo, L2BlockInfo};
use op_alloy_protocol::BlockInfo;

/// Mock data iterator
#[derive(Debug, Default, PartialEq)]
Expand Down Expand Up @@ -60,117 +56,6 @@ impl DataAvailabilityProvider for TestDAP {
}
}

/// A mock chain provider for testing.
#[derive(Debug, Clone, Default)]
pub struct TestChainProvider {
/// Maps block numbers to block information using a tuple list.
pub blocks: Vec<(u64, BlockInfo)>,
/// Maps block hashes to header information using a tuple list.
pub headers: Vec<(B256, Header)>,
/// Maps block hashes to receipts using a tuple list.
pub receipts: Vec<(B256, Vec<Receipt>)>,
/// Maps block hashes to transactions using a tuple list.
pub transactions: Vec<(B256, Vec<TxEnvelope>)>,
}

impl TestChainProvider {
/// Insert a block into the mock chain provider.
pub fn insert_block(&mut self, number: u64, block: BlockInfo) {
self.blocks.push((number, block));
}

/// Insert a block with transactions into the mock chain provider.
pub fn insert_block_with_transactions(
&mut self,
number: u64,
block: BlockInfo,
txs: Vec<TxEnvelope>,
) {
self.blocks.push((number, block));
self.transactions.push((block.hash, txs));
}

/// Insert receipts into the mock chain provider.
pub fn insert_receipts(&mut self, hash: B256, receipts: Vec<Receipt>) {
self.receipts.push((hash, receipts));
}

/// Insert a header into the mock chain provider.
pub fn insert_header(&mut self, hash: B256, header: Header) {
self.headers.push((hash, header));
}

/// Clears headers from the mock chain provider.
pub fn clear_headers(&mut self) {
self.headers.clear();
}

/// Clears blocks from the mock chain provider.
pub fn clear_blocks(&mut self) {
self.blocks.clear();
}

/// Clears receipts from the mock chain provider.
pub fn clear_receipts(&mut self) {
self.receipts.clear();
}

/// Clears all blocks and receipts from the mock chain provider.
pub fn clear(&mut self) {
self.clear_blocks();
self.clear_receipts();
self.clear_headers();
}
}

#[async_trait]
impl ChainProvider for TestChainProvider {
type Error = anyhow::Error;

async fn header_by_hash(&mut self, hash: B256) -> Result<Header> {
if let Some((_, header)) = self.headers.iter().find(|(_, b)| b.hash_slow() == hash) {
Ok(header.clone())
} else {
Err(anyhow::anyhow!("Header not found"))
}
}

async fn block_info_by_number(&mut self, _number: u64) -> Result<BlockInfo> {
if let Some((_, block)) = self.blocks.iter().find(|(n, _)| *n == _number) {
Ok(*block)
} else {
Err(anyhow::anyhow!("Block not found"))
}
}

async fn receipts_by_hash(&mut self, _hash: B256) -> Result<Vec<Receipt>> {
if let Some((_, receipts)) = self.receipts.iter().find(|(h, _)| *h == _hash) {
Ok(receipts.clone())
} else {
Err(anyhow::anyhow!("Receipts not found"))
}
}

async fn block_info_and_transactions_by_hash(
&mut self,
hash: B256,
) -> Result<(BlockInfo, Vec<TxEnvelope>)> {
let block = self
.blocks
.iter()
.find(|(_, b)| b.hash == hash)
.map(|(_, b)| *b)
.ok_or_else(|| anyhow::anyhow!("Block not found"))?;
let txs = self
.transactions
.iter()
.find(|(h, _)| *h == hash)
.map(|(_, txs)| txs.clone())
.unwrap_or_default();
Ok((block, txs))
}
}

/// A mock blob provider for testing.
#[derive(Debug, Clone, Default)]
pub struct TestBlobProvider {
Expand Down Expand Up @@ -208,62 +93,3 @@ impl BlobProvider for TestBlobProvider {
Ok(blobs)
}
}

/// An [L2ChainProvider] implementation for testing.
#[derive(Debug, Default)]
pub struct TestL2ChainProvider {
/// Blocks
pub blocks: Vec<L2BlockInfo>,
/// Short circuit the block return to be the first block.
pub short_circuit: bool,
/// Blocks
pub op_blocks: Vec<OpBlock>,
/// System configs
pub system_configs: HashMap<u64, SystemConfig>,
}

impl TestL2ChainProvider {
/// Creates a new [MockBlockFetcher] with the given origin and batches.
pub const fn new(
blocks: Vec<L2BlockInfo>,
op_blocks: Vec<OpBlock>,
system_configs: HashMap<u64, SystemConfig>,
) -> Self {
Self { blocks, short_circuit: false, op_blocks, system_configs }
}
}

#[async_trait]
impl L2ChainProvider for TestL2ChainProvider {
type Error = anyhow::Error;

async fn l2_block_info_by_number(&mut self, number: u64) -> Result<L2BlockInfo> {
if self.short_circuit {
return self.blocks.first().copied().ok_or_else(|| anyhow::anyhow!("Block not found"));
}
self.blocks
.iter()
.find(|b| b.block_info.number == number)
.cloned()
.ok_or_else(|| anyhow::anyhow!("Block not found"))
}

async fn block_by_number(&mut self, number: u64) -> Result<OpBlock> {
self.op_blocks
.iter()
.find(|p| p.header.number == number)
.cloned()
.ok_or_else(|| anyhow::anyhow!("L2 Block not found"))
}

async fn system_config_by_number(
&mut self,
number: u64,
_: Arc<RollupConfig>,
) -> Result<SystemConfig> {
self.system_configs
.get(&number)
.ok_or_else(|| anyhow::anyhow!("System config not found"))
.cloned()
}
}
4 changes: 4 additions & 0 deletions crates/providers-alloy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ serde_json.workspace = true
alloy-rpc-client.workspace = true
alloy-node-bindings.workspace = true
alloy-transport-http.workspace = true
kona-providers = { workspace = true, features = ["test-utils"] }
kona-derive = { workspace = true, features = ["serde", "test-utils"] }

[features]
default = []
Expand All @@ -62,4 +64,6 @@ test-utils = [
"dep:alloy-rpc-client",
"dep:alloy-node-bindings",
"dep:alloy-transport-http",
"kona-derive/test-utils",
"kona-providers/test-utils",
]
42 changes: 42 additions & 0 deletions crates/providers-alloy/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,45 @@ pub fn new_online_pipeline(
.origin(origin)
.build()
}

#[cfg(test)]
mod tests {
use super::*;
use crate::OnlineBlobProvider;
use kona_derive::prelude::OriginProvider;

#[test]
fn test_new_online_pipeline() {
let rollup_config = Arc::new(RollupConfig::default());
let chain_provider =
AlloyChainProvider::new_http("http://127.0.0.1:8545".try_into().unwrap());
let l2_chain_provider = AlloyL2ChainProvider::new_http(
"http://127.0.0.1:9545".try_into().unwrap(),
rollup_config.clone(),
);
let beacon_client = OnlineBeaconClient::new_http("http://127.0.0.1:5555".into());
let blob_provider: OnlineBlobProvider<_, SimpleSlotDerivation> =
OnlineBlobProvider::new(beacon_client, None, None);
let blob_provider = OnlineBlobProviderWithFallback::new(blob_provider, None);
let dap_source =
EthereumDataSource::new(chain_provider.clone(), blob_provider, &rollup_config);
let builder = StatefulAttributesBuilder::new(
rollup_config.clone(),
l2_chain_provider.clone(),
chain_provider.clone(),
);
let origin = BlockInfo::default();

let pipeline = new_online_pipeline(
rollup_config.clone(),
chain_provider,
dap_source,
l2_chain_provider,
builder,
origin,
);

assert_eq!(pipeline.rollup_config, rollup_config);
assert_eq!(pipeline.origin(), Some(origin));
}
}
7 changes: 7 additions & 0 deletions crates/providers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,10 @@ op-alloy-consensus = { workspace = true, features = ["k256"] }

# Misc
async-trait.workspace = true

# `test-utils` feature
anyhow = { workspace = true, optional = true }

[features]
default = []
test-utils = ["dep:anyhow"]
3 changes: 3 additions & 0 deletions crates/providers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ pub use l1_chain_provider::ChainProvider;

mod l2_chain_provider;
pub use l2_chain_provider::L2ChainProvider;

#[cfg(any(test, feature = "test-utils"))]
pub mod test_utils;
Loading

0 comments on commit 56c42f6

Please sign in to comment.