-
Notifications
You must be signed in to change notification settings - Fork 64
support chain spec #138
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
support chain spec #138
Changes from 5 commits
e231a43
5737c00
3533cf4
332fd0b
f75054a
7ce9d11
72bd5d4
e2b126a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,10 +3,10 @@ use std::{path::Path, vec}; | |
| use cb_common::{ | ||
| config::{ | ||
| CommitBoostConfig, LogsSettings, ModuleKind, BUILDER_PORT_ENV, BUILDER_URLS_ENV, | ||
| CONFIG_DEFAULT, CONFIG_ENV, JWTS_ENV, LOGS_DIR_DEFAULT, LOGS_DIR_ENV, METRICS_PORT_ENV, | ||
| MODULE_ID_ENV, MODULE_JWT_ENV, PBS_MODULE_NAME, SIGNER_DEFAULT, SIGNER_DIR_KEYS_DEFAULT, | ||
| SIGNER_DIR_KEYS_ENV, SIGNER_DIR_SECRETS, SIGNER_DIR_SECRETS_ENV, SIGNER_KEYS_ENV, | ||
| SIGNER_MODULE_NAME, SIGNER_PORT_ENV, SIGNER_URL_ENV, | ||
| CHAIN_SPEC_ENV, CONFIG_DEFAULT, CONFIG_ENV, JWTS_ENV, LOGS_DIR_DEFAULT, LOGS_DIR_ENV, | ||
| METRICS_PORT_ENV, MODULE_ID_ENV, MODULE_JWT_ENV, PBS_MODULE_NAME, SIGNER_DEFAULT, | ||
| SIGNER_DIR_KEYS_DEFAULT, SIGNER_DIR_KEYS_ENV, SIGNER_DIR_SECRETS, SIGNER_DIR_SECRETS_ENV, | ||
| SIGNER_KEYS_ENV, SIGNER_MODULE_NAME, SIGNER_PORT_ENV, SIGNER_URL_ENV, | ||
| }, | ||
| loader::SignerLoader, | ||
| types::ModuleId, | ||
|
|
@@ -37,6 +37,7 @@ const SIGNER_NETWORK: &str = "signer_network"; | |
| pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()> { | ||
| println!("Initializing Commit-Boost with config file: {}", config_path); | ||
| let cb_config = CommitBoostConfig::from_file(&config_path)?; | ||
| let chain_spec_path = CommitBoostConfig::chain_spec_file(&config_path); | ||
|
|
||
| let metrics_enabled = cb_config.metrics.is_some(); | ||
| let log_to_file = cb_config.logs.is_some(); | ||
|
|
@@ -46,6 +47,17 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()> | |
|
|
||
| // config volume to pass to all services | ||
| let config_volume = Volumes::Simple(format!("./{}:{}:ro", config_path, CONFIG_DEFAULT)); | ||
| let chain_spec_volume = chain_spec_path.as_ref().and_then(|p| { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe: let get_file_name_str = |p: &Path| -> Option<&str> {
p.file_name()?.to_str()
};
// chain_spec_volume using the helper
let chain_spec_volume = chain_spec_path.as_ref().and_then(|p| {
let file_name = get_file_name_str(p)?;
Some(Volumes::Simple(format!("{}:/{}:ro", p.display(), file_name)))
});
// chain_spec_env using the helper
let chain_spec_env = chain_spec_path.and_then(|p| {
let file_name = get_file_name_str(&p)?;
Some(get_env_val(CHAIN_SPEC_ENV, &format!("/{file_name}")))
});
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mm not sure this is more legible |
||
| // this is ok since the config has already been loaded once | ||
| let file_name = p.file_name()?.to_str()?; | ||
| Some(Volumes::Simple(format!("{}:/{}:ro", p.display(), file_name))) | ||
| }); | ||
|
|
||
| let chain_spec_env = chain_spec_path.and_then(|p| { | ||
| // this is ok since the config has already been loaded once | ||
| let file_name = p.file_name()?.to_str()?; | ||
| Some(get_env_val(CHAIN_SPEC_ENV, &format!("/{file_name}"))) | ||
| }); | ||
|
|
||
| let mut jwts = IndexMap::new(); | ||
| // envs to write in .env file | ||
|
|
@@ -105,6 +117,9 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()> | |
| // Set environment file | ||
| let env_file = module.env_file.map(EnvFile::Simple); | ||
|
|
||
| if let Some((key, val)) = chain_spec_env.clone() { | ||
| module_envs.insert(key, val); | ||
| } | ||
| if metrics_enabled { | ||
| let (key, val) = get_env_uval(METRICS_PORT_ENV, metrics_port as u64); | ||
| module_envs.insert(key, val); | ||
|
|
@@ -125,6 +140,7 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()> | |
|
|
||
| // volumes | ||
| let mut module_volumes = vec![config_volume.clone()]; | ||
| module_volumes.extend(chain_spec_volume.clone()); | ||
| module_volumes.extend(get_log_volume(&cb_config.logs, &module.id)); | ||
|
|
||
| Service { | ||
|
|
@@ -151,6 +167,9 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()> | |
| get_env_uval(BUILDER_PORT_ENV, builder_events_port), | ||
| ]); | ||
|
|
||
| if let Some((key, val)) = chain_spec_env.clone() { | ||
| module_envs.insert(key, val); | ||
| } | ||
| if metrics_enabled { | ||
| let (key, val) = get_env_uval(METRICS_PORT_ENV, metrics_port as u64); | ||
| module_envs.insert(key, val); | ||
|
|
@@ -169,6 +188,7 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()> | |
|
|
||
| // volumes | ||
| let mut module_volumes = vec![config_volume.clone()]; | ||
| module_volumes.extend(chain_spec_volume.clone()); | ||
| module_volumes.extend(get_log_volume(&cb_config.logs, &module.id)); | ||
|
|
||
| Service { | ||
|
|
@@ -197,6 +217,9 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()> | |
|
|
||
| let mut pbs_envs = IndexMap::from([get_env_val(CONFIG_ENV, CONFIG_DEFAULT)]); | ||
|
|
||
| if let Some((key, val)) = chain_spec_env.clone() { | ||
| pbs_envs.insert(key, val); | ||
| } | ||
| if metrics_enabled { | ||
| let (key, val) = get_env_uval(METRICS_PORT_ENV, metrics_port as u64); | ||
| pbs_envs.insert(key, val); | ||
|
|
@@ -213,6 +236,7 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()> | |
|
|
||
| // volumes | ||
| let mut pbs_volumes = vec![config_volume.clone()]; | ||
| pbs_volumes.extend(chain_spec_volume.clone()); | ||
| pbs_volumes.extend(get_log_volume(&cb_config.logs, PBS_MODULE_NAME)); | ||
|
|
||
| // networks | ||
|
|
@@ -256,6 +280,9 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()> | |
| get_env_uval(SIGNER_PORT_ENV, signer_port as u64), | ||
| ]); | ||
|
|
||
| if let Some((key, val)) = chain_spec_env.clone() { | ||
| signer_envs.insert(key, val); | ||
| } | ||
| if metrics_enabled { | ||
| let (key, val) = get_env_uval(METRICS_PORT_ENV, metrics_port as u64); | ||
| signer_envs.insert(key, val); | ||
|
|
@@ -270,6 +297,7 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()> | |
|
|
||
| // volumes | ||
| let mut volumes = vec![config_volume.clone()]; | ||
| volumes.extend(chain_spec_volume.clone()); | ||
|
|
||
| // TODO: generalize this, different loaders may not need volumes but eg ports | ||
| match signer_config.loader { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,18 @@ | ||
| use std::path::PathBuf; | ||
|
|
||
| use eyre::Result; | ||
| use serde::{Deserialize, Serialize}; | ||
|
|
||
| use crate::types::Chain; | ||
| use crate::types::{load_chain_from_file, Chain}; | ||
|
|
||
| mod constants; | ||
| mod log; | ||
| mod metrics; | ||
| mod module; | ||
| mod pbs; | ||
| mod signer; | ||
| mod utils; | ||
|
|
||
| mod log; | ||
|
|
||
| pub use constants::*; | ||
| pub use log::*; | ||
| pub use metrics::*; | ||
|
|
@@ -22,7 +23,6 @@ pub use utils::*; | |
|
|
||
| #[derive(Debug, Deserialize, Serialize)] | ||
| pub struct CommitBoostConfig { | ||
| // TODO: generalize this with a spec file | ||
| pub chain: Chain, | ||
| pub relays: Vec<RelayConfig>, | ||
| pub pbs: StaticPbsConfig, | ||
|
|
@@ -45,9 +45,53 @@ impl CommitBoostConfig { | |
| Ok(config) | ||
| } | ||
|
|
||
| // When loading the config from the environment, it's important that every path | ||
| // is replaced with the correct value if the config is loaded inside a container | ||
| pub fn from_env_path() -> Result<Self> { | ||
| let config: Self = load_file_from_env(CONFIG_ENV)?; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wonder if we can do something like: impl TryFrom<&'static str> for CommitBoostConfigand then let config = CommitBoostConfig::try_from(CONFIG_ENV)?;
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah think we can improve in general how configs are loaded, will keep it in a separate PR |
||
| let config = if let Ok(path) = std::env::var(CHAIN_SPEC_ENV) { | ||
| // if the chain spec file is set, load it separately | ||
| let chain: Chain = load_chain_from_file(path.parse()?)?; | ||
| let rest_config: HelperConfig = load_file_from_env(CONFIG_ENV)?; | ||
|
|
||
| CommitBoostConfig { | ||
| chain, | ||
| relays: rest_config.relays, | ||
| pbs: rest_config.pbs, | ||
| modules: rest_config.modules, | ||
| signer: rest_config.signer, | ||
| metrics: rest_config.metrics, | ||
| logs: rest_config.logs, | ||
| } | ||
| } else { | ||
| load_file_from_env(CONFIG_ENV)? | ||
| }; | ||
|
|
||
| config.validate()?; | ||
| Ok(config) | ||
| } | ||
|
|
||
| /// Returns the path to the chain spec file if any | ||
| pub fn chain_spec_file(path: &str) -> Option<PathBuf> { | ||
| match load_from_file::<ChainConfig>(path) { | ||
| Ok(config) => Some(config.chain), | ||
| Err(_) => None, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Helper struct to load the chain spec file | ||
| #[derive(Deserialize)] | ||
| struct ChainConfig { | ||
| chain: PathBuf, | ||
| } | ||
|
|
||
| /// Helper struct to load the rest of the config | ||
| #[derive(Deserialize)] | ||
| struct HelperConfig { | ||
| relays: Vec<RelayConfig>, | ||
| pbs: StaticPbsConfig, | ||
| modules: Option<Vec<StaticModuleConfig>>, | ||
| signer: Option<SignerConfig>, | ||
| metrics: Option<MetricsConfig>, | ||
| logs: Option<LogsSettings>, | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.