Skip to content

Commit

Permalink
chore(registry): Migrate op-alloy-registry->op-rs/maili-registry (#…
Browse files Browse the repository at this point in the history
…366)

Migrates `op-alloy-registry` to `op-rs/maili-registry`. Re-exports
`maili_registry::*`.
  • Loading branch information
emhane authored Jan 8, 2025
1 parent e7e0d36 commit ce6464f
Show file tree
Hide file tree
Showing 18 changed files with 426 additions and 3,589 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ alloy-primitives = { version = "0.8.12", default-features = false }
# Maili
maili-provider = { version = "0.1.0", default-features = false }
maili-protocol = { version = "0.1.0", default-features = false }
maili-registry = { version = "0.1.0", default-features = false }

# Revm
revm = "19.0.0"
Expand Down
112 changes: 112 additions & 0 deletions crates/protocol/src/test_utils.rs
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));
}
}
305 changes: 305 additions & 0 deletions crates/protocol/src/utils.rs

Large diffs are not rendered by default.

17 changes: 5 additions & 12 deletions crates/registry/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "op-alloy-registry"
description = "A registry of superchain configs"
description = "Re-export of op-rs/maili-registry"

version.workspace = true
edition.workspace = true
Expand All @@ -15,17 +15,10 @@ exclude.workspace = true
workspace = true

[dependencies]
alloy-primitives = { workspace = true, features = ["map"] }
op-alloy-genesis = { workspace = true, features = ["serde"] }
lazy_static = { workspace = true, features = ["spin_no_std"] }
serde = { workspace = true, features = ["derive", "alloc"] }
serde_json = { workspace = true, features = ["raw_value"] }

[dev-dependencies]
alloy-eips.workspace = true
maili-registry = { workspace = true, default-features = false }

[features]
default = ["std", "map-foldhash"]
map-hashbrown = ["alloy-primitives/map-hashbrown"]
map-foldhash = ["alloy-primitives/map-foldhash"]
std = ["op-alloy-genesis/std", "serde_json/std"]
map-hashbrown = ["maili-registry/map-hashbrown"]
map-foldhash = ["maili-registry/map-foldhash"]
std = ["maili-registry/std"]
9 changes: 0 additions & 9 deletions crates/registry/Justfile

This file was deleted.

93 changes: 2 additions & 91 deletions crates/registry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,95 +7,6 @@
<a href="https://alloy-rs.github.io/op-alloy"><img src="https://img.shields.io/badge/Book-854a15?logo=mdBook&labelColor=2a2f35" alt="Book"></a>


[`op-alloy-registry`][sc] is a `no_std` crate that exports rust type definitions for chains
in the [`superchain-registry`][osr]. Since it reads static files to read configurations for
various chains into instantiated objects, the [`op-alloy-registry`][sc] crate requires
[`serde`][serde] as a dependency. To use the [`op-alloy-registry`][sc] crate, add the crate
as a dependency to a `Cargo.toml`.
See [maili-registry]

```toml
op-alloy-registry = "0.6.7"
```

[`op-alloy-registry`][sc] declares lazy evaluated statics that expose `ChainConfig`s, `RollupConfig`s,
and `Chain` objects for all chains with static definitions in the superchain registry. The way this works
is the golang side of the superchain registry contains an "internal code generation" script that has
been modified to output configuration files to the [`crates/registry`][s] directory in the
`etc` folder that are read by the [`op-alloy-registry`][sc] rust crate. These static config files
contain an up-to-date list of all superchain configurations with their chain configs. It is expected
that if the commit hash of the [`superchain-registry`][osr] pulled in as a git submodule has breaking
changes, the tests in this crate (`op-alloy-registry`) will break and updates will need to be made.

There are three core statics exposed by the [`op-alloy-registry`][sc].
- `CHAINS`: A list of chain objects containing the superchain metadata for this chain.
- `OPCHAINS`: A map from chain id to `ChainConfig`.
- `ROLLUP_CONFIGS`: A map from chain id to `RollupConfig`.

[`op-alloy-registry`][sc] exports the _complete_ list of chains within the superchain, as well as each
chain's `RollupConfig`s and `ChainConfig`s.


### Usage

Add the following to your `Cargo.toml`.

```toml
[dependencies]
op-alloy-registry = "0.6.7"
```

To make `op-alloy-registry` `no_std`, toggle `default-features` off like so.

```toml
[dependencies]
op-alloy-registry = { version = "0.6.7", default-features = false }
```

Below demonstrates getting the `RollupConfig` for OP Mainnet (Chain ID `10`).

```rust
use op_alloy_registry::ROLLUP_CONFIGS;

let op_chain_id = 10;
let op_rollup_config = ROLLUP_CONFIGS.get(&op_chain_id);
println!("OP Mainnet Rollup Config: {:?}", op_rollup_config);
```

A mapping from chain id to `ChainConfig` is also available.

```rust
use op_alloy_registry::OPCHAINS;

let op_chain_id = 10;
let op_chain_config = OPCHAINS.get(&op_chain_id);
println!("OP Mainnet Chain Config: {:?}", op_chain_config);
```


### Feature Flags

- `std`: Uses the standard library to pull in environment variables.


### Credits

[superchain-registry][osr] contributors for building and maintaining superchain types.

[alloy] and [op-alloy] for creating and maintaining high quality Ethereum and Optimism types in rust.


<!-- Hyperlinks -->

[serde]: https://crates.io/crates/serde
[alloy]: https://github.com/alloy-rs/alloy
[op-alloy]: https://github.com/alloy-rs/op-alloy
[op-superchain]: https://docs.optimism.io/stack/explainer
[osr]: https://github.com/ethereum-optimism/superchain-registry

[s]: ./crates/registry
[sc]: https://crates.io/crates/op-alloy-registry

[oag]: https://crates.io/crates/op-alloy-genesis
[chains]: https://docs.rs/op-alloy-registry/latest/superchain/struct.CHAINS.html
[opchains]: https://docs.rs/op-alloy-registry/latest/superchain/struct.OPCHAINS.html
[rollups]: https://docs.rs/op-alloy-registry/latest/superchain/struct.ROLLUP_CONFIGS.html
[maili-registry]: https://github.com/op-rs/maili/blob/main/crates/registry/README.md
24 changes: 0 additions & 24 deletions crates/registry/etc/bind.sh

This file was deleted.

Loading

0 comments on commit ce6464f

Please sign in to comment.