Skip to content

Commit

Permalink
feat: dfx info replica-port (#2700)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericswanson-dfinity authored Oct 18, 2022
1 parent d8f5198 commit 7ddc32d
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions docs/cli-reference/dfx-info.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |

Expand Down
14 changes: 14 additions & 0 deletions e2e/tests-dfx/info.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 5 additions & 0 deletions src/dfx/src/commands/info/mod.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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()?
Expand Down
24 changes: 24 additions & 0 deletions src/dfx/src/commands/info/replica_port.rs
Original file line number Diff line number Diff line change
@@ -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<String> {
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");
}
}
17 changes: 11 additions & 6 deletions src/dfx/src/util/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -13,23 +13,26 @@ 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<Option<u16>> {
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();
let replica_port_path = local_server_descriptor.replica_port_path();

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),
Expand Down Expand Up @@ -75,7 +78,9 @@ pub fn get_replica_urls(
) -> DfxResult<Vec<Url>> {
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);
Expand Down

0 comments on commit 7ddc32d

Please sign in to comment.