Skip to content
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

feat(zk_inception): Add ecosystem ports scanner #2849

Open
wants to merge 50 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
3b55ab2
ecosystem ports scanner
sanekmelnikov Sep 4, 2024
14a28ca
Merge branch 'main' into sanekmelnikov/zk-inception-ports
matias-gonz Sep 16, 2024
3f6702a
Add support for docker compose
matias-gonz Sep 17, 2024
27c346c
Make sure it detects host amchine port and not container port
matias-gonz Sep 17, 2024
5d300eb
Add ZKsync repo directory to scan docker-compose files
matias-gonz Sep 17, 2024
1526dbe
Fix docker compose scan
matias-gonz Sep 18, 2024
c77a5a9
Use allocate ports instead of offset
matias-gonz Sep 18, 2024
36df52b
Merge branch 'main' into sanekmelnikov/zk-inception-ports
matias-gonz Sep 18, 2024
0d15f96
lint
matias-gonz Sep 18, 2024
861284d
lint
matias-gonz Sep 18, 2024
c75dfe3
lint
matias-gonz Sep 18, 2024
3712aee
Remove unused functions
matias-gonz Sep 18, 2024
cab60bb
Remove port offset for chain init
matias-gonz Sep 18, 2024
a72fe5e
Update explorer to ecosystem port scanner
matias-gonz Sep 18, 2024
d4e4529
Remove port offset
matias-gonz Sep 18, 2024
c1330c8
Merge branch 'main' into sanekmelnikov/zk-inception-ports
matias-gonz Sep 18, 2024
d30bae0
Remove find_offset
matias-gonz Sep 19, 2024
04c7bbe
Add default for EcosystemPorts
matias-gonz Sep 19, 2024
2ada84f
Merge branch 'main' into sanekmelnikov/zk-inception-ports
matias-gonz Sep 19, 2024
27723d7
merge
matias-gonz Sep 19, 2024
7f0623c
Update docker compose parse ports
matias-gonz Sep 19, 2024
54aec19
implement ConfigWithChainPorts for ExplorerPorts
matias-gonz Sep 20, 2024
c93d467
impl ConfigWithChainPorts for GeneralConfig
matias-gonz Sep 20, 2024
30e57c6
Update contract_verifier_prometheus_port
matias-gonz Sep 20, 2024
bf19838
Use new port allocation for en
matias-gonz Sep 20, 2024
8574c17
Delete update configs
matias-gonz Sep 20, 2024
03682ee
Refactor get_consensus_config
matias-gonz Sep 20, 2024
26188f7
Add get_consensus_port
matias-gonz Sep 20, 2024
52f3c5e
Remove PortsConfig
matias-gonz Sep 20, 2024
28272c6
Split traverse yaml into smaller functions
matias-gonz Sep 20, 2024
cffe123
Improve test_traverse_yaml
matias-gonz Sep 20, 2024
b0a673f
Add no ports reallocation option
matias-gonz Sep 23, 2024
c314a46
Merge branch 'main' into sanekmelnikov/zk-inception-ports
matias-gonz Sep 23, 2024
c36c1fc
Fix ecosystem directory
matias-gonz Sep 23, 2024
2eb62c5
Add default_value for no_port_reallocation
matias-gonz Sep 23, 2024
550c5e4
Remove default port constants
matias-gonz Sep 25, 2024
5175c07
Change name to traverse_allocate_ports_in_yaml
matias-gonz Sep 26, 2024
934c187
Use allocate_ports_in_yaml in chain init
matias-gonz Sep 26, 2024
7074c7f
Use allocate_ports_in_yaml for en
matias-gonz Sep 26, 2024
c1525aa
Add chain count to allocate_ports_in_yaml
matias-gonz Sep 26, 2024
91ab676
Fix consensus port allocation
matias-gonz Sep 26, 2024
364229c
Fix consensus port allocation in chain init
matias-gonz Sep 26, 2024
1415e2d
Fix consensus port offset
matias-gonz Sep 26, 2024
6c9494c
Fix prepare configs prot allocation
matias-gonz Sep 26, 2024
f3d97d6
Revert en changes
matias-gonz Sep 26, 2024
f13ff46
Allocate consensus port in chain init
matias-gonz Sep 26, 2024
00a934c
Merge branch 'main' into sanekmelnikov/zk-inception-ports
matias-gonz Sep 27, 2024
2405919
Use allocate_ports_in_yaml in en prepare configs
matias-gonz Sep 27, 2024
e174c3b
Change port allocation order
matias-gonz Sep 27, 2024
9ed7664
Fix offset
matias-gonz Sep 27, 2024
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
3 changes: 0 additions & 3 deletions .github/workflows/ci-core-reusable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ jobs:
--server-db-name=zksync_server_localhost_validium \
--prover-db-url=postgres://postgres:notsecurepassword@localhost:5432 \
--prover-db-name=zksync_prover_localhost_validium \
--port-offset 2000 \
--chain validium

- name: Create and initialize chain with Custom Token
Expand All @@ -269,7 +268,6 @@ jobs:
--server-db-name=zksync_server_localhost_custom_token \
--prover-db-url=postgres://postgres:notsecurepassword@localhost:5432 \
--prover-db-name=zksync_prover_localhost_custom_token \
--port-offset 3000 \
--chain custom_token

- name: Create and register chain with transactions signed "offline"
Expand Down Expand Up @@ -328,7 +326,6 @@ jobs:
--server-db-name=zksync_server_localhost_consensus \
--prover-db-url=postgres://postgres:notsecurepassword@localhost:5432 \
--prover-db-name=zksync_prover_localhost_consensus \
--port-offset 4000 \
--chain consensus

- name: Build test dependencies
Expand Down
1 change: 1 addition & 0 deletions zk_toolbox/crates/config/src/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ impl FileConfigWithDefaultName for GeneralConfig {
const FILE_NAME: &'static str = GENERAL_FILE;
}

#[derive(Debug, Default, Clone)]
pub struct PortsConfig {
pub web3_json_rpc_http_port: u16,
pub web3_json_rpc_ws_port: u16,
Expand Down
36 changes: 1 addition & 35 deletions zk_toolbox/crates/zk_inception/src/commands/chain/args/init.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::str::FromStr;

use clap::Parser;
use common::{forge::ForgeScriptArgs, Prompt};
use config::ChainConfig;
Expand All @@ -13,35 +11,10 @@ use crate::{
defaults::LOCAL_RPC_URL,
messages::{
MSG_DEPLOY_PAYMASTER_PROMPT, MSG_GENESIS_ARGS_HELP, MSG_L1_RPC_URL_HELP,
MSG_L1_RPC_URL_INVALID_ERR, MSG_L1_RPC_URL_PROMPT, MSG_PORT_OFFSET_HELP,
MSG_L1_RPC_URL_INVALID_ERR, MSG_L1_RPC_URL_PROMPT,
},
};

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PortOffset(u16);

impl PortOffset {
pub fn from_chain_id(chain_id: u16) -> Self {
Self((chain_id - 1) * 100)
}
}

impl FromStr for PortOffset {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
s.parse::<u16>()
.map(PortOffset)
.map_err(|_| "Invalid port offset".to_string())
}
}

impl From<PortOffset> for u16 {
fn from(port_offset: PortOffset) -> Self {
port_offset.0
}
}

#[derive(Debug, Clone, Serialize, Deserialize, Parser)]
pub struct InitArgs {
/// All ethereum environment related arguments
Expand All @@ -55,8 +28,6 @@ pub struct InitArgs {
pub deploy_paymaster: Option<bool>,
#[clap(long, help = MSG_L1_RPC_URL_HELP)]
pub l1_rpc_url: Option<String>,
#[clap(long, help = MSG_PORT_OFFSET_HELP)]
pub port_offset: Option<PortOffset>,
}

impl InitArgs {
Expand Down Expand Up @@ -86,10 +57,6 @@ impl InitArgs {
genesis_args: self.genesis_args.fill_values_with_prompt(config),
deploy_paymaster,
l1_rpc_url,
port_offset: self
.port_offset
.unwrap_or(PortOffset::from_chain_id(config.id as u16))
.into(),
}
}
}
Expand All @@ -100,5 +67,4 @@ pub struct InitArgsFinal {
pub genesis_args: GenesisArgsFinal,
pub deploy_paymaster: bool,
pub l1_rpc_url: String,
pub port_offset: u16,
}
matias-gonz marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add --no-ports-reallocation instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the use-case?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for devops team.
They prefer to have the same ports for all envs, since they are all launching in independent envs

25 changes: 9 additions & 16 deletions zk_toolbox/crates/zk_inception/src/commands/chain/init.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use anyhow::{bail, Context};
use anyhow::Context;
use common::{config::global_config, git, logger, spinner::Spinner};
use config::{
copy_configs, ports_config, set_l1_rpc_url, traits::SaveConfigWithBasePath,
update_from_chain_config, update_ports, ChainConfig, EcosystemConfig, GeneralConfig,
update_from_chain_config, ChainConfig, EcosystemConfig,
};
use types::BaseToken;
use xshell::Shell;
Expand All @@ -27,7 +27,10 @@ use crate::{
MSG_REGISTERING_CHAIN_SPINNER, MSG_SELECTED_CONFIG,
MSG_UPDATING_TOKEN_MULTIPLIER_SETTER_SPINNER, MSG_WALLET_TOKEN_MULTIPLIER_SETTER_NOT_FOUND,
},
utils::consensus::{generate_consensus_keys, get_consensus_config, get_consensus_secrets},
utils::{
consensus::{generate_consensus_keys, get_consensus_config, get_consensus_secrets},
ports::EcosystemPortsScanner,
},
};

pub(crate) async fn run(args: InitArgs, shell: &Shell) -> anyhow::Result<()> {
Expand All @@ -54,10 +57,12 @@ pub async fn init(
ecosystem_config: &EcosystemConfig,
chain_config: &ChainConfig,
) -> anyhow::Result<()> {
let mut ecosystem_ports = EcosystemPortsScanner::scan(shell)?;
copy_configs(shell, &ecosystem_config.link_to_code, &chain_config.configs)?;

let mut general_config = chain_config.get_general_config()?;
apply_port_offset(init_args.port_offset, &mut general_config)?;
ecosystem_ports.allocate_ports(&mut general_config)?;

sanekmelnikov marked this conversation as resolved.
Show resolved Hide resolved
let ports = ports_config(&general_config).context(MSG_PORTS_CONFIG_ERR)?;

let consensus_keys = generate_consensus_keys();
Expand Down Expand Up @@ -176,15 +181,3 @@ pub async fn init(

Ok(())
}

fn apply_port_offset(port_offset: u16, general_config: &mut GeneralConfig) -> anyhow::Result<()> {
let Some(mut ports_config) = ports_config(general_config) else {
bail!("Missing ports config");
};

ports_config.apply_offset(port_offset);

update_ports(general_config, &ports_config)?;

Ok(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use super::{
use crate::{
accept_ownership::{accept_admin, accept_owner},
commands::{
chain::{self, args::init::PortOffset},
chain::{self},
ecosystem::create_configs::{
create_erc20_deployment_config, create_initial_deployments_config,
},
Expand Down Expand Up @@ -112,7 +112,6 @@ pub async fn run(args: EcosystemInitArgs, shell: &Shell) -> anyhow::Result<()> {
genesis_args: genesis_args.clone().fill_values_with_prompt(&chain_config),
deploy_paymaster: final_ecosystem_args.deploy_paymaster,
l1_rpc_url: final_ecosystem_args.ecosystem.l1_rpc_url.clone(),
port_offset: PortOffset::from_chain_id(chain_config.id as u16).into(),
};

chain::init::init(
Expand Down
32 changes: 22 additions & 10 deletions zk_toolbox/crates/zk_inception/src/commands/explorer/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ use url::Url;
use xshell::Shell;

use crate::{
commands::chain::args::init::PortOffset,
consts::L2_BASE_TOKEN_ADDRESS,
defaults::{generate_explorer_db_name, DATABASE_EXPLORER_URL},
defaults::{generate_explorer_db_name, DATABASE_EXPLORER_URL, PORT_RANGE},
messages::{
msg_chain_load_err, msg_explorer_db_name_prompt, msg_explorer_db_url_prompt,
msg_explorer_initializing_database_for, MSG_EXPLORER_FAILED_TO_DROP_DATABASE_ERR,
MSG_EXPLORER_INITIALIZED,
},
utils::ports::{EcosystemPorts, EcosystemPortsScanner},
};

pub(crate) async fn run(shell: &Shell) -> anyhow::Result<()> {
Expand All @@ -28,6 +28,7 @@ pub(crate) async fn run(shell: &Shell) -> anyhow::Result<()> {
Some(ref chain_name) => vec![chain_name.clone()],
None => ecosystem_config.list_of_chains(),
};
let mut ports = EcosystemPortsScanner::scan(shell)?;
// Initialize chains one by one
let mut explorer_config = ExplorerConfig::read_or_create_default(shell)?;
for chain_name in chains_enabled.iter() {
Expand All @@ -36,7 +37,7 @@ pub(crate) async fn run(shell: &Shell) -> anyhow::Result<()> {
.load_chain(Some(chain_name.clone()))
.context(msg_chain_load_err(chain_name))?;
// Build backend config - parameters required to create explorer backend services
let backend_config = build_backend_config(&chain_config);
let backend_config = build_backend_config(&mut ports, &chain_config)?;
// Initialize explorer database
initialize_explorer_database(&backend_config.database_url).await?;
// Create explorer backend docker compose file
Expand All @@ -58,16 +59,22 @@ pub(crate) async fn run(shell: &Shell) -> anyhow::Result<()> {
Ok(())
}

fn build_backend_config(chain_config: &ChainConfig) -> ExplorerBackendConfig {
fn build_backend_config(
ports: &mut EcosystemPorts,
chain_config: &ChainConfig,
) -> anyhow::Result<ExplorerBackendConfig> {
// Prompt explorer database name
logger::info(msg_explorer_initializing_database_for(&chain_config.name));
let db_config = fill_database_values_with_prompt(chain_config);

// Allocate ports for backend services
let backend_ports = allocate_explorer_services_ports(chain_config);
let backend_ports = allocate_explorer_services_ports(ports)?;

// Build explorer backend config
ExplorerBackendConfig::new(db_config.full_url(), &backend_ports)
Ok(ExplorerBackendConfig::new(
db_config.full_url(),
&backend_ports,
))
}

async fn initialize_explorer_database(db_url: &Url) -> anyhow::Result<()> {
Expand All @@ -92,10 +99,15 @@ fn fill_database_values_with_prompt(config: &ChainConfig) -> db::DatabaseConfig
db::DatabaseConfig::new(explorer_db_url, explorer_db_name)
}

fn allocate_explorer_services_ports(chain_config: &ChainConfig) -> ExplorerBackendPorts {
// Try to allocate intuitive ports with an offset from the defaults
let offset: u16 = PortOffset::from_chain_id(chain_config.id as u16).into();
ExplorerBackendPorts::default().with_offset(offset)
fn allocate_explorer_services_ports(
ports: &mut EcosystemPorts,
) -> anyhow::Result<ExplorerBackendPorts> {
Ok(ExplorerBackendPorts {
api_http_port: ports.allocate_port(PORT_RANGE, "Explorer API".to_string())?,
data_fetcher_http_port: ports
.allocate_port(PORT_RANGE, "Explorer Data Fetcher".to_string())?,
worker_http_port: ports.allocate_port(PORT_RANGE, "Explorer Worker".to_string())?,
})
}

fn build_explorer_chain_config(
Expand Down
7 changes: 7 additions & 0 deletions zk_toolbox/crates/zk_inception/src/defaults.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::ops::Range;

use config::ChainConfig;
use lazy_static::lazy_static;
use url::Url;
Expand All @@ -11,6 +13,11 @@ lazy_static! {
Url::parse("postgres://postgres:notsecurepassword@localhost:5432").unwrap();
}

// Default port range
pub const PORT_RANGE_START: u16 = 3000;
pub const PORT_RANGE_END: u16 = 5000;
pub const PORT_RANGE: Range<u16> = PORT_RANGE_START..PORT_RANGE_END;

pub const ROCKS_DB_STATE_KEEPER: &str = "state_keeper";
pub const ROCKS_DB_TREE: &str = "tree";
pub const ROCKS_DB_PROTECTIVE_READS: &str = "protective_reads";
Expand Down
1 change: 0 additions & 1 deletion zk_toolbox/crates/zk_inception/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ pub(super) fn msg_path_to_zksync_does_not_exist_err(path: &str) -> String {

/// Ecosystem and chain init related messages
pub(super) const MSG_L1_RPC_URL_HELP: &str = "L1 RPC URL";
pub(super) const MSG_PORT_OFFSET_HELP: &str = "Add a costant offset to the ports exposed by the components. Useful when running multiple chains on the same machine";
pub(super) const MSG_GENESIS_ARGS_HELP: &str = "Genesis options";
pub(super) const MSG_DEV_ARG_HELP: &str =
"Deploy ecosystem using all defaults. Suitable for local development";
Expand Down
1 change: 1 addition & 0 deletions zk_toolbox/crates/zk_inception/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod consensus;
pub mod forge;
pub mod ports;
pub mod rocks_db;
Loading
Loading