-
Notifications
You must be signed in to change notification settings - Fork 84
chore(registry): Migrate op-alloy-registry->op-rs/maili-registry
#366
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| //! Test utilities for the protocol crate. | ||
|
|
||
| use alloc::{boxed::Box, format, string::String, sync::Arc, vec::Vec}; | ||
| use async_trait::async_trait; | ||
| use op_alloy_consensus::OpBlock; | ||
| use spin::Mutex; | ||
| use tracing::{Event, Level, Subscriber}; | ||
| use tracing_subscriber::{layer::Context, Layer}; | ||
|
|
||
| use crate::{BatchValidationProvider, L2BlockInfo}; | ||
|
|
||
| /// An error for implementations of the [BatchValidationProvider] trait. | ||
| #[derive(Debug, thiserror::Error)] | ||
| pub enum TestBatchValidatorError { | ||
| /// The block was not found. | ||
| #[error("Block not found")] | ||
| BlockNotFound, | ||
| /// The L2 block was not found. | ||
| #[error("L2 Block not found")] | ||
| L2BlockNotFound, | ||
| } | ||
|
|
||
| /// An [TestBatchValidator] implementation for testing. | ||
| #[derive(Debug, Default, Clone)] | ||
| pub struct TestBatchValidator { | ||
| /// 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>, | ||
| } | ||
|
|
||
| impl TestBatchValidator { | ||
| /// Creates a new []TestBatchValidator with the given origin and batches. | ||
| pub const fn new(blocks: Vec<L2BlockInfo>, op_blocks: Vec<OpBlock>) -> Self { | ||
| Self { blocks, short_circuit: false, op_blocks } | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl BatchValidationProvider for TestBatchValidator { | ||
| type Error = TestBatchValidatorError; | ||
|
|
||
| async fn l2_block_info_by_number(&mut self, number: u64) -> Result<L2BlockInfo, Self::Error> { | ||
| if self.short_circuit { | ||
| return self | ||
| .blocks | ||
| .first() | ||
| .copied() | ||
| .ok_or_else(|| TestBatchValidatorError::BlockNotFound); | ||
| } | ||
| self.blocks | ||
| .iter() | ||
| .find(|b| b.block_info.number == number) | ||
| .cloned() | ||
| .ok_or_else(|| TestBatchValidatorError::BlockNotFound) | ||
| } | ||
|
|
||
| async fn block_by_number(&mut self, number: u64) -> Result<OpBlock, Self::Error> { | ||
| self.op_blocks | ||
| .iter() | ||
| .find(|p| p.header.number == number) | ||
| .cloned() | ||
| .ok_or_else(|| TestBatchValidatorError::L2BlockNotFound) | ||
| } | ||
| } | ||
|
|
||
| /// The storage for the collected traces. | ||
| #[derive(Debug, Default, Clone)] | ||
| pub struct TraceStorage(pub Arc<Mutex<Vec<(Level, String)>>>); | ||
|
|
||
| impl TraceStorage { | ||
| /// Returns the items in the storage that match the specified level. | ||
| pub fn get_by_level(&self, level: Level) -> Vec<String> { | ||
| self.0 | ||
| .lock() | ||
| .iter() | ||
| .filter_map(|(l, message)| if *l == level { Some(message.clone()) } else { None }) | ||
| .collect() | ||
| } | ||
|
|
||
| /// Returns if the storage is empty. | ||
| pub fn is_empty(&self) -> bool { | ||
| self.0.lock().is_empty() | ||
| } | ||
| } | ||
|
|
||
| /// A subscriber layer that collects traces and their log levels. | ||
| #[derive(Debug, Default)] | ||
| pub struct CollectingLayer { | ||
| /// The storage for the collected traces. | ||
| pub storage: TraceStorage, | ||
| } | ||
|
|
||
| impl CollectingLayer { | ||
| /// Creates a new collecting layer with the specified storage. | ||
| pub const fn new(storage: TraceStorage) -> Self { | ||
| Self { storage } | ||
| } | ||
| } | ||
|
|
||
| impl<S: Subscriber> Layer<S> for CollectingLayer { | ||
| fn on_event(&self, event: &Event<'_>, _ctx: Context<'_, S>) { | ||
| let metadata = event.metadata(); | ||
| let level = *metadata.level(); | ||
| let message = format!("{:?}", event); | ||
|
|
||
| let mut storage = self.storage.0.lock(); | ||
| storage.push((level, message)); | ||
| } | ||
| } |
emhane marked this conversation as resolved.
Show resolved
Hide resolved
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
emhane marked this conversation as resolved.
Show resolved
Hide resolved
|
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.