From 7ddc32d7ed1913db0013c73c77cb6ec2f4ed8091 Mon Sep 17 00:00:00 2001 From: Eric Swanson <64809312+ericswanson-dfinity@users.noreply.github.com> Date: Tue, 18 Oct 2022 06:17:27 -0700 Subject: [PATCH] feat: dfx info replica-port (#2700) Fixes https://dfinity.atlassian.net/browse/SDK-777 --- CHANGELOG.md | 4 ++++ docs/cli-reference/dfx-info.md | 1 + e2e/tests-dfx/info.bash | 14 +++++++++++++ src/dfx/src/commands/info/mod.rs | 5 +++++ src/dfx/src/commands/info/replica_port.rs | 24 +++++++++++++++++++++++ src/dfx/src/util/network.rs | 17 ++++++++++------ 6 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 src/dfx/src/commands/info/replica_port.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e29a9d7ce..76776c6ebb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -277,6 +277,10 @@ There is also a new configuration file: `$HOME/.config/dfx/networks.json`. Its This displays the port that the icx-proxy process listens on, meaning the port to connect to with curl or from a web browser. +#### feat: `dfx info replica-port` + +This displays the listening port of the replica. + #### feat: `dfx info replica-rev` This displays the revision of the replica bundled with dfx, which is the same revision referenced in replica election governance proposals. diff --git a/docs/cli-reference/dfx-info.md b/docs/cli-reference/dfx-info.md index 87cba37d4d..3d30a75c9f 100644 --- a/docs/cli-reference/dfx-info.md +++ b/docs/cli-reference/dfx-info.md @@ -13,6 +13,7 @@ These are the types of information that the `dfx info` command can display. | Option | Description | |---------------------|------------------------------------------------| | networks-json-path | Path to network definition file networks.json. | +| replica-port | The listening port of the replica. | | replica-rev | The revision of the bundled replica. | | webserver-port | The local webserver (icx-proxy) port. | diff --git a/e2e/tests-dfx/info.bash b/e2e/tests-dfx/info.bash index e2eef05325..765423f3b9 100644 --- a/e2e/tests-dfx/info.bash +++ b/e2e/tests-dfx/info.bash @@ -14,6 +14,20 @@ teardown() { standard_teardown } +@test "displays the replica port" { + assert_command_fail dfx info replica-port + assert_contains "No replica port found" + + dfx_start + assert_command dfx info replica-port + if [ "$USE_IC_REF" ] + then + assert_eq "$(get_ic_ref_port)" + else + assert_eq "$(get_replica_port)" + fi +} + @test "displays the default webserver port for the local shared network" { assert_command dfx info webserver-port assert_eq "4943" diff --git a/src/dfx/src/commands/info/mod.rs b/src/dfx/src/commands/info/mod.rs index 392b64ceb7..27d149a15a 100644 --- a/src/dfx/src/commands/info/mod.rs +++ b/src/dfx/src/commands/info/mod.rs @@ -1,15 +1,19 @@ +mod replica_port; mod webserver_port; +use crate::commands::info::replica_port::get_replica_port; use crate::commands::info::webserver_port::get_webserver_port; use crate::config::dfinity::NetworksConfig; use crate::lib::error::DfxResult; use crate::lib::info; use crate::Environment; + use anyhow::Context; use clap::Parser; #[derive(clap::ValueEnum, Clone, Debug)] enum InfoType { + ReplicaPort, ReplicaRev, WebserverPort, NetworksJsonPath, @@ -24,6 +28,7 @@ pub struct InfoOpts { pub fn exec(env: &dyn Environment, opts: InfoOpts) -> DfxResult { let value = match opts.info_type { + InfoType::ReplicaPort => get_replica_port(env)?, InfoType::ReplicaRev => info::replica_rev().to_string(), InfoType::WebserverPort => get_webserver_port(env)?, InfoType::NetworksJsonPath => NetworksConfig::new()? diff --git a/src/dfx/src/commands/info/replica_port.rs b/src/dfx/src/commands/info/replica_port.rs new file mode 100644 index 0000000000..9b5a16e3b6 --- /dev/null +++ b/src/dfx/src/commands/info/replica_port.rs @@ -0,0 +1,24 @@ +use crate::lib::error::DfxResult; +use crate::lib::provider::{create_network_descriptor, LocalBindDetermination}; +use crate::util::network::get_running_replica_port; +use crate::Environment; + +use anyhow::bail; + +pub(crate) fn get_replica_port(env: &dyn Environment) -> DfxResult { + let network_descriptor = create_network_descriptor( + env.get_config(), + env.get_networks_config(), + None, + None, + LocalBindDetermination::AsConfigured, + )?; + + if let Some(port) = + get_running_replica_port(None, network_descriptor.local_server_descriptor()?)? + { + Ok(format!("{}", port)) + } else { + bail!("No replica port found"); + } +} diff --git a/src/dfx/src/util/network.rs b/src/dfx/src/util/network.rs index 944711e259..8d900d4a63 100644 --- a/src/dfx/src/util/network.rs +++ b/src/dfx/src/util/network.rs @@ -4,7 +4,7 @@ use crate::lib::network::local_server_descriptor::LocalServerDescriptor; use crate::lib::network::network_descriptor::NetworkDescriptor; use fn_error_context::context; -use slog::info; +use slog::{info, Logger}; use std::path::Path; use url::Url; @@ -13,10 +13,9 @@ use url::Url; /// # Prerequisites /// - A local replica or emulator needs to be running, e.g. with `dfx start`. pub fn get_running_replica_port( - env: &dyn Environment, + logger: Option<&Logger>, local_server_descriptor: &LocalServerDescriptor, ) -> DfxResult> { - let logger = env.get_logger(); // dfx start and dfx replica both write these as empty, and then // populate one with a port. let emulator_port_path = local_server_descriptor.ic_ref_port_path(); @@ -24,12 +23,16 @@ pub fn get_running_replica_port( match read_port_from(&replica_port_path)? { Some(port) => { - info!(logger, "Found local replica running on port {}", port); + if let Some(logger) = logger { + info!(logger, "Found local replica running on port {}", port); + } Ok(Some(port)) } None => match read_port_from(&emulator_port_path)? { Some(port) => { - info!(logger, "Found local emulator running on port {}", port); + if let Some(logger) = logger { + info!(logger, "Found local emulator running on port {}", port); + } Ok(Some(port)) } None => Ok(None), @@ -75,7 +78,9 @@ pub fn get_replica_urls( ) -> DfxResult> { if network_descriptor.name == "local" { let local_server_descriptor = network_descriptor.local_server_descriptor()?; - if let Some(port) = get_running_replica_port(env, local_server_descriptor)? { + if let Some(port) = + get_running_replica_port(Some(env.get_logger()), local_server_descriptor)? + { let mut socket_addr = local_server_descriptor.bind_address; socket_addr.set_port(port); let url = format!("http://{}", socket_addr);