Skip to content

Commit

Permalink
Use allocate_ports_in_yaml in en prepare configs
Browse files Browse the repository at this point in the history
  • Loading branch information
matias-gonz committed Sep 27, 2024
1 parent 00a934c commit 2405919
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use std::{collections::BTreeMap, path::Path, str::FromStr};
use anyhow::Context;
use common::{config::global_config, logger};
use config::{
external_node::ENConfig, get_consensus_port, set_rocks_db_config,
traits::SaveConfigWithBasePath, ChainConfig, EcosystemConfig, SecretsConfig,
external_node::ENConfig,
get_consensus_port, set_rocks_db_config,
traits::{FileConfigWithDefaultName, SaveConfigWithBasePath},
ChainConfig, EcosystemConfig, GeneralConfig, SecretsConfig,
};
use xshell::Shell;
use zksync_basic_types::url::SensitiveUrl;
Expand Down Expand Up @@ -76,7 +78,6 @@ fn prepare_configs(
gateway_url: None,
};
let mut general_en = general.clone();
ports.allocate_ports_with_offset_from_defaults(&mut general_en, config.id)?;
let consensus_port = get_consensus_port(&general_en);

// Set consensus config
Expand Down Expand Up @@ -124,5 +125,11 @@ fn prepare_configs(
general_en.save_with_base_path(shell, en_configs_path)?;
en_config.save_with_base_path(shell, en_configs_path)?;

ports.allocate_ports_in_yaml(
shell,
&GeneralConfig::get_path_with_base_path(en_configs_path),
config.id,
)?;

Ok(())
}
93 changes: 1 addition & 92 deletions zk_toolbox/crates/zk_inception/src/utils/ports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use std::{

use anyhow::{bail, Context, Result};
use config::{
explorer_compose::ExplorerBackendPorts, update_port_in_host, update_port_in_url,
EcosystemConfig, GeneralConfig, DEFAULT_CONSENSUS_PORT, DEFAULT_EXPLORER_API_PORT,
explorer_compose::ExplorerBackendPorts, EcosystemConfig, DEFAULT_EXPLORER_API_PORT,
DEFAULT_EXPLORER_DATA_FETCHER_PORT, DEFAULT_EXPLORER_WORKER_PORT,
};
use serde_yaml::Value;
Expand Down Expand Up @@ -355,96 +354,6 @@ impl ConfigWithChainPorts for ExplorerBackendPorts {
}
}

impl ConfigWithChainPorts for GeneralConfig {
fn get_default_ports(&self) -> anyhow::Result<HashMap<String, u16>> {
let api = self
.api_config
.clone()
.context("Api config is not presented")?;
let contract_verifier = self
.contract_verifier
.clone()
.context("Contract Verifier config is not presented")?;
let prometheus = self
.prometheus_config
.clone()
.context("Prometheus config is not presented")?;

let consensus_port = if let Some(consensus) = self.consensus_config.clone() {
consensus.server_addr.port()
} else {
DEFAULT_CONSENSUS_PORT
};

Ok(HashMap::from([
(
"web3_json_rpc_http_port".to_string(),
api.web3_json_rpc.http_port,
),
(
"web3_json_rpc_ws_port".to_string(),
api.web3_json_rpc.ws_port,
),
("healthcheck_port".to_string(), api.healthcheck.port),
("merkle_tree_port".to_string(), api.merkle_tree.port),
(
"prometheus_listener_port".to_string(),
prometheus.listener_port,
),
("contract_verifier_port".to_string(), contract_verifier.port),
("consensus_port".to_string(), consensus_port),
]))
}

fn set_ports(&mut self, ports: HashMap<String, u16>) -> anyhow::Result<()> {
if ports.len() != self.get_default_ports()?.len() {
bail!("Incorrect number of ports provided");
}

let api = self
.api_config
.as_mut()
.context("Api config is not presented")?;
let contract_verifier = self
.contract_verifier
.as_mut()
.context("Contract Verifier config is not presented")?;
let prometheus = self
.prometheus_config
.as_mut()
.context("Prometheus config is not presented")?;

for (desc, port) in ports {
match desc.as_str() {
"web3_json_rpc_http_port" => {
api.web3_json_rpc.http_port = port;
update_port_in_url(&mut api.web3_json_rpc.http_url, port)?;
}
"web3_json_rpc_ws_port" => {
api.web3_json_rpc.ws_port = port;
update_port_in_url(&mut api.web3_json_rpc.ws_url, port)?;
}
"healthcheck_port" => api.healthcheck.port = port,
"merkle_tree_port" => api.merkle_tree.port = port,
"prometheus_listener_port" => prometheus.listener_port = port,
"contract_verifier_port" => {
contract_verifier.port = port;
update_port_in_url(&mut contract_verifier.url, port)?;
}
"consensus_port" => {
if let Some(consensus) = self.consensus_config.as_mut() {
consensus.server_addr.set_port(port);
update_port_in_host(&mut consensus.public_addr, port)?;
}
}
_ => bail!("Unknown port descriptor: {}", desc),
}
}

Ok(())
}
}

#[cfg(test)]
mod tests {
use std::path::PathBuf;
Expand Down

0 comments on commit 2405919

Please sign in to comment.