Skip to content
Open
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
2 changes: 2 additions & 0 deletions Cargo.lock

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

10 changes: 9 additions & 1 deletion crates/optimism/chainspec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,22 @@ 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"] }
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",
Expand Down
32 changes: 31 additions & 1 deletion crates/optimism/chainspec/src/superchain/configs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<ChainMetadata, SuperchainConfigError> {
#[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<ChainMetadata> {
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<'_>,
Expand Down
4 changes: 2 additions & 2 deletions crates/optimism/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = []
Expand Down