added bool to list gateways to indicate currently active gateway

This commit is contained in:
Jędrzej Stuczyński
2024-03-12 13:24:51 +00:00
parent 062f4517d6
commit c07b782afa
2 changed files with 49 additions and 31 deletions
@@ -3,8 +3,10 @@
use crate::cli_helpers::{CliClient, CliClientConfig};
use crate::client::base_client::non_wasm_helpers::setup_fs_gateways_storage;
use crate::client::base_client::storage::helpers::get_gateway_registrations;
use nym_client_core_gateways_storage::{GatewayDetails, GatewayRegistration, GatewayType};
use crate::client::base_client::storage::helpers::{
get_active_gateway_identity, get_gateway_registrations,
};
use nym_client_core_gateways_storage::{GatewayDetails, GatewayType};
use nym_crypto::asymmetric::identity;
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter};
@@ -23,12 +25,6 @@ pub struct CommonClientListGatewaysArgs {
#[serde(transparent)]
pub struct RegisteredGateways(Vec<GatewayInfo>);
impl From<Vec<GatewayRegistration>> for RegisteredGateways {
fn from(value: Vec<GatewayRegistration>) -> Self {
RegisteredGateways(value.into_iter().map(Into::into).collect())
}
}
impl Display for RegisteredGateways {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
for (i, gateway) in self.0.iter().enumerate() {
@@ -42,35 +38,18 @@ impl Display for RegisteredGateways {
pub struct GatewayInfo {
pub registration: OffsetDateTime,
pub identity: identity::PublicKey,
pub active: bool,
pub typ: String,
pub endpoint: Option<Url>,
pub wg_tun_address: Option<Url>,
}
impl From<GatewayRegistration> for GatewayInfo {
fn from(value: GatewayRegistration) -> Self {
match value.details {
GatewayDetails::Remote(remote_details) => GatewayInfo {
registration: value.registration_timestamp,
identity: remote_details.gateway_id,
typ: GatewayType::Remote.to_string(),
endpoint: Some(remote_details.gateway_listener),
wg_tun_address: remote_details.wg_tun_address,
},
GatewayDetails::Custom(_) => GatewayInfo {
registration: value.registration_timestamp,
identity: value.details.gateway_id(),
typ: value.details.typ().to_string(),
endpoint: None,
wg_tun_address: None,
},
}
}
}
impl Display for GatewayInfo {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if self.active {
write!(f, "[ACTIVE] ")?;
}
write!(
f,
"{} gateway '{}' registered at: {}",
@@ -100,7 +79,30 @@ where
let details_store = setup_fs_gateways_storage(&paths.gateway_registrations).await?;
let gateways = get_gateway_registrations(&details_store).await?;
let active_gateway = get_active_gateway_identity(&details_store).await?;
Ok(gateways.into())
let gateways = get_gateway_registrations(&details_store).await?;
let mut info = Vec::with_capacity(gateways.len());
for gateway in gateways {
match gateway.details {
GatewayDetails::Remote(remote_details) => info.push(GatewayInfo {
registration: gateway.registration_timestamp,
identity: remote_details.gateway_id,
active: active_gateway == Some(remote_details.gateway_id),
typ: GatewayType::Remote.to_string(),
endpoint: Some(remote_details.gateway_listener),
wg_tun_address: remote_details.wg_tun_address,
}),
GatewayDetails::Custom(_) => info.push(GatewayInfo {
registration: gateway.registration_timestamp,
identity: gateway.details.gateway_id(),
active: active_gateway == Some(gateway.details.gateway_id()),
typ: gateway.details.typ().to_string(),
endpoint: None,
wg_tun_address: None,
}),
};
}
Ok(RegisteredGateways(info))
}
@@ -24,6 +24,22 @@ where
})
}
pub async fn get_active_gateway_identity<D>(
details_store: &D,
) -> Result<Option<identity::PublicKey>, ClientCoreError>
where
D: GatewaysDetailsStore,
D::StorageError: Send + Sync + 'static,
{
details_store
.active_gateway()
.await
.map_err(|source| ClientCoreError::GatewaysDetailsStoreError {
source: Box::new(source),
})
.map(|a| a.registration.map(|r| r.details.gateway_id()))
}
pub async fn get_all_registered_identities<D>(
details_store: &D,
) -> Result<Vec<identity::PublicKey>, ClientCoreError>