Skip to content
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

[Feature]: Add Opstack superchain registry support for genesis files #14260

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions crates/optimism/op-chain-registry/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "op-chain-registry"
version.workspace = true
edition.workspace = true
rust-version.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true
exclude.workspace = true

[dependencies]
reqwest = { version = "0.12.12", features = ["stream"] }
tokio = { version = "1.0", features = ["full"] }
serde_json = "1.0"
zstd = "0.12"
anyhow = "1.0"

Comment on lines +12 to +17
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should use workspace

[lints]
workspace = true
Binary file not shown.
59 changes: 59 additions & 0 deletions crates/optimism/op-chain-registry/src/chain_registry.rs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool, these functions just need some docs

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use reqwest;
use serde_json::Value;
use zstd::stream::read::Decoder;
use zstd::dict::DecoderDictionary;
use anyhow::{Result, Context};

async fn download_file(url: &str) -> Result<Vec<u8>> {
println!("Downloading from URL: {}", url);
let response = reqwest::get(url)
.await
.context("Failed to download file")?;

if !response.status().is_success() {
anyhow::bail!("Failed to download: Status {}", response.status());
}

Ok(response.bytes().await?.to_vec())
}

async fn download_and_parse_zst(url: &str, dict_url: &str) -> Result<Value> {
// Download the dictionary
let dict_bytes = download_file(dict_url).await?;
println!("Downloaded dictionary: {} bytes", dict_bytes.len());

let dictionary = DecoderDictionary::copy(&dict_bytes);

// Download the main file
let compressed_bytes = download_file(url).await?;
println!("Downloaded compressed file: {} bytes", compressed_bytes.len());

// Create decoder with dictionary
let decoder = Decoder::with_prepared_dictionary(&compressed_bytes[..], &dictionary)
.context("Failed to create decoder with dictionary")?;

// Parse JSON
println!("Parsing JSON...");
let json: Value = serde_json::from_reader(decoder)
.context("Failed to parse JSON")?;

Ok(json)
}

#[cfg(test)]
mod tests {
use super::*;
use anyhow::Result;

#[tokio::test]
async fn test_download_and_parse_genesis() -> Result<()> {
let file_url = "https://raw.githubusercontent.com/ethereum-optimism/superchain-registry/main/superchain/extra/genesis/mainnet/arena-z.json.zst";
let dict_url = "https://raw.githubusercontent.com/ethereum-optimism/superchain-registry/main/superchain/extra/dictionary";
Comment on lines +50 to +51
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should convert the base paths into constants


let json_data = download_and_parse_zst(file_url, dict_url).await?;

assert!(json_data.is_object(), "Parsed JSON should be an object");

Ok(())
}
}
1 change: 1 addition & 0 deletions crates/optimism/op-chain-registry/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod chain_registry;