From 3cd2e6e2df761750b78214ce15940f056dc66973 Mon Sep 17 00:00:00 2001 From: 0xPenryn Date: Sat, 22 Nov 2025 16:30:18 -0300 Subject: [PATCH] feat: pull from scr github --- Cargo.lock | 2 ++ crates/optimism/chainspec/Cargo.toml | 10 +++++- .../chainspec/src/superchain/configs.rs | 32 ++++++++++++++++++- crates/optimism/cli/Cargo.toml | 4 +-- 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 448e638b649..1d5a87980e4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9364,6 +9364,7 @@ dependencies = [ "op-alloy-consensus", "op-alloy-rpc-types", "paste", + "reqwest", "reth-chainspec", "reth-ethereum-forks", "reth-network-peers", @@ -9374,6 +9375,7 @@ dependencies = [ "serde_json", "tar-no-std", "thiserror 2.0.17", + "toml", ] [[package]] diff --git a/crates/optimism/chainspec/Cargo.toml b/crates/optimism/chainspec/Cargo.toml index a4ef9263b1c..108ec9bcf6d 100644 --- a/crates/optimism/chainspec/Cargo.toml +++ b/crates/optimism/chainspec/Cargo.toml @@ -45,6 +45,8 @@ derive_more.workspace = true paste = { workspace = true, optional = true } thiserror = { workspace = true, optional = true } op-alloy-consensus.workspace = true +reqwest = { workspace = true, features = ["rustls-tls-native-roots", "blocking"], optional = true } +toml = { workspace = true, optional = true } [dev-dependencies] reth-chainspec = { workspace = true, features = ["test-utils"] } @@ -52,7 +54,13 @@ alloy-op-hardforks.workspace = true [features] default = ["std"] -superchain-configs = ["miniz_oxide", "paste", "tar-no-std", "thiserror", "thiserror", "dep:serde"] +superchain-configs = ["miniz_oxide", "paste", "tar-no-std", "thiserror", "dep:serde"] +pull-superchain-configs = [ + "std", + "superchain-configs", + "dep:reqwest", + "dep:toml", +] std = [ "alloy-chains/std", "alloy-genesis/std", diff --git a/crates/optimism/chainspec/src/superchain/configs.rs b/crates/optimism/chainspec/src/superchain/configs.rs index bb1929646a0..daa9dc6a1dc 100644 --- a/crates/optimism/chainspec/src/superchain/configs.rs +++ b/crates/optimism/chainspec/src/superchain/configs.rs @@ -7,6 +7,11 @@ use alloc::{ use alloy_genesis::Genesis; use miniz_oxide::inflate::decompress_to_vec_zlib_with_limit; use tar_no_std::{CorruptDataError, TarArchiveRef}; +#[cfg(feature = "pull-superchain-configs")] +use { + reqwest::blocking::Client, + std::time::Duration, +}; /// A genesis file can be up to 100MiB. This is a reasonable limit for the genesis file size. const MAX_GENESIS_SIZE: usize = 100 * 1024 * 1024; // 100MiB @@ -58,19 +63,44 @@ pub(crate) fn read_superchain_genesis( Ok(genesis) } -/// Reads the [`ChainMetadata`] from the superchain config tar file for a superchain. +/// Reads the [`ChainMetadata`] for a superchain from the superchain registry github, falling back to the local config tar file. /// For example, `read_superchain_config("unichain", "mainnet")`. fn read_superchain_metadata( name: &str, environment: &str, archive: &TarArchiveRef<'_>, ) -> Result { + #[cfg(feature = "pull-superchain-configs")] + if let Some(remote_config) = fetch_superchain_registry_metadata(name, environment) { + return Ok(remote_config) + } + let config_file = read_file(archive, &format!("configs/{environment}/{name}.json"))?; let config_content = String::from_utf8(config_file)?; let chain_config: ChainMetadata = serde_json::from_str(&config_content)?; Ok(chain_config) } +#[cfg(feature = "pull-superchain-configs")] +fn fetch_superchain_registry_metadata( + name: &str, + environment: &str, +) -> Option { + const URL: &str = + "https://raw.githubusercontent.com/ethereum-optimism/superchain-registry/main/superchain/configs"; + const TIMEOUT: Duration = Duration::from_secs(5); + + let client = Client::builder().timeout(TIMEOUT).build().ok()?; + let url = format!("{URL}/{environment}/{name}.toml"); + let response = client.get(&url).send().ok()?; + if !response.status().is_success() { + return None + } + let body = response.text().ok()?; + let config: ChainMetadata = toml::from_str(&body).ok()?; + Some(config) +} + /// Reads a file from the tar archive. The file path is relative to the root of the tar archive. fn read_file( archive: &TarArchiveRef<'_>, diff --git a/crates/optimism/cli/Cargo.toml b/crates/optimism/cli/Cargo.toml index bdb680b2062..eb77ac3044f 100644 --- a/crates/optimism/cli/Cargo.toml +++ b/crates/optimism/cli/Cargo.toml @@ -35,7 +35,7 @@ reth-node-metrics.workspace = true ## optimism reth-optimism-primitives.workspace = true -reth-optimism-chainspec = { workspace = true, features = ["superchain-configs"] } +reth-optimism-chainspec = { workspace = true, features = ["pull-superchain-configs"] } reth-optimism-consensus.workspace = true reth-chainspec.workspace = true @@ -71,7 +71,7 @@ tempfile.workspace = true reth-stages = { workspace = true, features = ["test-utils"] } [build-dependencies] -reth-optimism-chainspec = { workspace = true, features = ["std", "superchain-configs"] } +reth-optimism-chainspec = { workspace = true, features = ["std", "pull-superchain-configs"] } [features] default = []