Skip to content

Commit

Permalink
fix(examples): Dynamic Rollup Config Loading (#293)
Browse files Browse the repository at this point in the history
* fix: dynamically load the rollup config by l1 chain id

* fix(client): dynamic config loading
  • Loading branch information
refcell authored Jun 21, 2024
1 parent 5655e48 commit 916339a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 43 deletions.
58 changes: 29 additions & 29 deletions Cargo.lock

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

13 changes: 3 additions & 10 deletions bin/client/src/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use alloy_primitives::{B256, U256};
use anyhow::{anyhow, Result};
use kona_preimage::{PreimageKey, PreimageOracleClient};
use kona_primitives::{RollupConfig, OP_MAINNET_CONFIG};
use kona_primitives::RollupConfig;

/// The local key ident for the L1 head hash.
pub const L1_HEAD_KEY: U256 = U256::from_be_slice(&[1]);
Expand Down Expand Up @@ -89,16 +89,9 @@ impl BootInfo {
.try_into()
.map_err(|_| anyhow!("Failed to convert L2 chain ID to u64"))?,
);
let rollup_config = rollup_config_from_chain_id(chain_id)?;
let rollup_config = RollupConfig::from_l2_chain_id(chain_id)
.ok_or_else(|| anyhow!("Failed to get rollup config for L2 Chain ID: {chain_id}"))?;

Ok(Self { l1_head, l2_output_root, l2_claim, l2_claim_block, chain_id, rollup_config })
}
}

/// Returns the rollup config for the given chain ID.
fn rollup_config_from_chain_id(chain_id: u64) -> Result<RollupConfig> {
match chain_id {
10 => Ok(OP_MAINNET_CONFIG),
_ => anyhow::bail!("Unsupported chain ID: {}", chain_id),
}
}
11 changes: 11 additions & 0 deletions crates/derive/src/online/alloy_providers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,17 @@ impl AlloyL2ChainProvider {
}
}

/// Returns the chain ID.
pub async fn chain_id(&mut self) -> Result<u64> {
let chain_id: TransportResult<alloc::string::String> =
self.inner.raw_request("eth_chainId".into(), ()).await;
let chain_id = match chain_id {
Ok(s) => alloc::string::String::from(s.trim_start_matches("0x")),
Err(e) => return Err(anyhow!(e)),
};
u64::from_str_radix(&chain_id, 16).map_err(|e| anyhow!(e))
}

/// Creates a new [AlloyL2ChainProvider] from the provided [reqwest::Url].
pub fn new_http(url: reqwest::Url, rollup_config: Arc<RollupConfig>) -> Self {
let inner = ReqwestProvider::new_http(url);
Expand Down
16 changes: 12 additions & 4 deletions examples/trusted-sync/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::{anyhow, Result};
use clap::Parser;
use kona_derive::{online::*, types::OP_MAINNET_CONFIG};
use kona_derive::online::*;
use reqwest::Url;
use std::sync::Arc;
use tracing::{debug, error, info, warn, Level};
Expand Down Expand Up @@ -37,10 +37,18 @@ async fn sync(cli_cfg: crate::cli::Cli) -> Result<()> {
let beacon_url: String =
cli_cfg.beacon_url.unwrap_or_else(|| std::env::var(BEACON_URL).unwrap());

// Construct the pipeline and payload validator.
let cfg = Arc::new(OP_MAINNET_CONFIG);
let start = cli_cfg.start_l2_block.unwrap_or(cfg.genesis.l2.number);
// Query for the L2 Chain ID
let mut l2_provider =
AlloyL2ChainProvider::new_http(l2_rpc_url.clone(), Arc::new(RollupConfig::default()));
let l2_chain_id =
l2_provider.chain_id().await.expect("Failed to fetch chain ID from L2 provider");
let cfg = RollupConfig::from_l2_chain_id(l2_chain_id)
.expect("Failed to fetch rollup config from L2 chain ID");
let cfg = Arc::new(cfg);

// Construct the pipeline
let mut l1_provider = AlloyChainProvider::new_http(l1_rpc_url);
let start = cli_cfg.start_l2_block.unwrap_or(cfg.genesis.l2.number);
let mut l2_provider = AlloyL2ChainProvider::new_http(l2_rpc_url.clone(), cfg.clone());
let attributes =
StatefulAttributesBuilder::new(cfg.clone(), l2_provider.clone(), l1_provider.clone());
Expand Down

0 comments on commit 916339a

Please sign in to comment.