Skip to content

Commit

Permalink
feat(client+host): Dynamic RollupConfig in bootloader (#439)
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby authored Aug 21, 2024
1 parent a61ab47 commit e588222
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

6 changes: 2 additions & 4 deletions bin/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ spin.workspace = true
async-trait.workspace = true
tracing.workspace = true
revm.workspace = true
serde = { version = "1.0.208", default-features = false, features = ["derive"] }
serde_json = { version = "1.0.125", default-features = false }

# local
kona-common = { path = "../../crates/common", version = "0.0.2" }
Expand All @@ -35,10 +37,6 @@ kona-executor = { path = "../../crates/executor", version = "0.0.1" }
# external
tracing-subscriber = { version = "0.3.18", optional = true }

[dev-dependencies]
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.117"

[features]
tracing-subscriber = ["dep:tracing-subscriber"]

Expand Down
18 changes: 16 additions & 2 deletions bin/client/src/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use alloy_primitives::{B256, U256};
use anyhow::{anyhow, Result};
use kona_preimage::{PreimageKey, PreimageOracleClient};
use kona_primitives::RollupConfig;
use tracing::warn;

/// 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,8 +90,21 @@ impl BootInfo {
.try_into()
.map_err(|_| anyhow!("Failed to convert L2 chain ID to u64"))?,
);
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}"))?;

// Attempt to load the rollup config from the chain ID. If there is no config for the chain,
// fall back to loading the config from the preimage oracle.
let rollup_config = if let Some(config) = RollupConfig::from_l2_chain_id(chain_id) {
config
} else {
warn!(
target: "boot-loader",
"No rollup config found for chain ID {}, falling back to preimage oracle. This is insecure in production without additional validation!",
chain_id
);
let ser_cfg = oracle.get(PreimageKey::new_local(L2_ROLLUP_CONFIG_KEY.to())).await?;
serde_json::from_slice(&ser_cfg)
.map_err(|e| anyhow!("Failed to deserialize rollup config: {}", e))?
};

Ok(Self { l1_head, l2_output_root, l2_claim, l2_claim_block, chain_id, rollup_config })
}
Expand Down
3 changes: 2 additions & 1 deletion bin/host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ reqwest = "0.12"
tokio = { version = "1.37.0", features = ["full"] }
futures = "0.3"
clap = { version = "4.5.4", features = ["derive", "env"] }
serde = { version = "1.0.198", features = ["derive"] }
serde = { version = "1.0.208", features = ["derive"] }
serde_json = "1.0.125"
tracing-subscriber = "0.3.18"
command-fds = { version = "0.3", features = ["tokio"] }
os_pipe = "1.2.1"
22 changes: 22 additions & 0 deletions bin/host/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use crate::kv::{
SplitKeyValueStore,
};
use alloy_primitives::B256;
use anyhow::{anyhow, Result};
use clap::{ArgAction, Parser};
use kona_derive::types::RollupConfig;
use serde::Serialize;
use std::{path::PathBuf, sync::Arc};
use tokio::sync::RwLock;
Expand Down Expand Up @@ -59,6 +61,9 @@ pub struct HostCli {
/// Run in pre-image server mode without executing any client program. Defaults to `false`.
#[clap(long)]
pub server: bool,
/// Path to rollup config
#[clap(long)]
pub rollup_config_path: Option<PathBuf>,
}

impl HostCli {
Expand Down Expand Up @@ -86,4 +91,21 @@ impl HostCli {

kv_store
}

/// Reads the [RollupConfig] from the file system and returns it as a string.
pub fn read_rollup_config(&self) -> Result<RollupConfig> {
let path = self.rollup_config_path.as_ref().ok_or_else(|| {
anyhow::anyhow!(
"No rollup config path provided. Please provide a path to the rollup config."
)
})?;

// Read the serialized config from the file system.
let ser_config = std::fs::read_to_string(path)
.map_err(|e| anyhow!("Error reading RollupConfig file: {e}"))?;

// Deserialize the config and return it.
serde_json::from_str(&ser_config)
.map_err(|e| anyhow!("Error deserializing RollupConfig: {e}"))
}
}
6 changes: 5 additions & 1 deletion bin/host/src/kv/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ impl KeyValueStore for LocalKeyValueStore {
L2_CLAIM_KEY => Some(self.cfg.l2_claim.to_vec()),
L2_CLAIM_BLOCK_NUMBER_KEY => Some(self.cfg.l2_block_number.to_be_bytes().to_vec()),
L2_CHAIN_ID_KEY => Some(self.cfg.l2_chain_id.to_be_bytes().to_vec()),
L2_ROLLUP_CONFIG_KEY => unimplemented!("L2RollupConfig fetching in local store not implemented. Necessary for chain IDs without a known rollup config."),
L2_ROLLUP_CONFIG_KEY => {
let rollup_config = self.cfg.read_rollup_config().ok()?;
let serialized = serde_json::to_vec(&rollup_config).ok()?;
Some(serialized)
}
_ => None,
}
}
Expand Down

0 comments on commit e588222

Please sign in to comment.