prepare vpn client country reporting (#5134)
This commit is contained in:
@@ -25,7 +25,7 @@ use nym_gateway_requests::{
|
||||
CREDENTIAL_UPDATE_V2_PROTOCOL_VERSION, CURRENT_PROTOCOL_VERSION,
|
||||
};
|
||||
use nym_sphinx::forwarding::packet::MixPacket;
|
||||
use nym_statistics_common::clients::credential::CredentialStatsEvent;
|
||||
use nym_statistics_common::clients::connection::ConnectionStatsEvent;
|
||||
use nym_statistics_common::clients::ClientStatsSender;
|
||||
use nym_task::TaskClient;
|
||||
use nym_validator_client::nyxd::contract_traits::DkgQueryClient;
|
||||
@@ -766,7 +766,7 @@ impl<C, St> GatewayClient<C, St> {
|
||||
match self.claim_ecash_bandwidth(prepared_credential.data).await {
|
||||
Ok(_) => {
|
||||
self.stats_reporter.report(
|
||||
CredentialStatsEvent::TicketSpent {
|
||||
ConnectionStatsEvent::TicketSpent {
|
||||
typ: MIXNET_TICKET,
|
||||
amount: TICKETS_TO_SPEND,
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
// Copyright 2024 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use super::ClientStatsEvents;
|
||||
|
||||
use nym_credentials_interface::TicketType;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
|
||||
pub(crate) struct ConnectionStats {
|
||||
//tickets
|
||||
mixnet_entry_spent: u32,
|
||||
vpn_entry_spent: u32,
|
||||
mixnet_exit_spent: u32,
|
||||
vpn_exit_spent: u32,
|
||||
|
||||
//country_connection
|
||||
wg_exit_country_code: String,
|
||||
mix_exit_country_code: String,
|
||||
}
|
||||
|
||||
/// Event space for Nym API statistics tracking
|
||||
#[derive(Debug)]
|
||||
pub enum ConnectionStatsEvent {
|
||||
/// ecash ticket was spend
|
||||
TicketSpent {
|
||||
typ: TicketType,
|
||||
amount: u32,
|
||||
},
|
||||
WgCountry(String),
|
||||
MixCountry(String),
|
||||
}
|
||||
impl From<ConnectionStatsEvent> for ClientStatsEvents {
|
||||
fn from(event: ConnectionStatsEvent) -> ClientStatsEvents {
|
||||
ClientStatsEvents::Connection(event)
|
||||
}
|
||||
}
|
||||
|
||||
/// Nym API statistics tracking object
|
||||
#[derive(Default)]
|
||||
pub struct ConnectionStatsControl {
|
||||
// Keep track of packet statistics over time
|
||||
stats: ConnectionStats,
|
||||
}
|
||||
|
||||
impl ConnectionStatsControl {
|
||||
pub(crate) fn handle_event(&mut self, event: ConnectionStatsEvent) {
|
||||
match event {
|
||||
ConnectionStatsEvent::TicketSpent { typ, amount } => match typ {
|
||||
TicketType::V1MixnetEntry => self.stats.mixnet_entry_spent += amount,
|
||||
TicketType::V1MixnetExit => self.stats.mixnet_exit_spent += amount,
|
||||
TicketType::V1WireguardEntry => self.stats.vpn_entry_spent += amount,
|
||||
TicketType::V1WireguardExit => self.stats.vpn_exit_spent += amount,
|
||||
},
|
||||
ConnectionStatsEvent::WgCountry(cc) => {
|
||||
self.stats.wg_exit_country_code = cc;
|
||||
}
|
||||
ConnectionStatsEvent::MixCountry(cc) => {
|
||||
self.stats.mix_exit_country_code = cc;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn report(&self) -> ConnectionStats {
|
||||
self.stats.clone()
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
// Copyright 2024 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use super::ClientStatsEvents;
|
||||
|
||||
use nym_credentials_interface::TicketType;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Default, Debug, Clone, Copy, Serialize, Deserialize)]
|
||||
pub(crate) struct CredentialStats {
|
||||
mixnet_entry_spent: u32,
|
||||
vpn_entry_spent: u32,
|
||||
mixnet_exit_spent: u32,
|
||||
vpn_exit_spent: u32,
|
||||
}
|
||||
|
||||
/// Event space for Nym API statistics tracking
|
||||
#[derive(Debug)]
|
||||
pub enum CredentialStatsEvent {
|
||||
/// ecash ticket was spend
|
||||
TicketSpent { typ: TicketType, amount: u32 },
|
||||
}
|
||||
|
||||
impl From<CredentialStatsEvent> for ClientStatsEvents {
|
||||
fn from(event: CredentialStatsEvent) -> ClientStatsEvents {
|
||||
ClientStatsEvents::Credential(event)
|
||||
}
|
||||
}
|
||||
|
||||
/// Nym API statistics tracking object
|
||||
#[derive(Default)]
|
||||
pub struct CredentialStatsControl {
|
||||
// Keep track of packet statistics over time
|
||||
stats: CredentialStats,
|
||||
}
|
||||
|
||||
impl CredentialStatsControl {
|
||||
pub(crate) fn handle_event(&mut self, event: CredentialStatsEvent) {
|
||||
match event {
|
||||
CredentialStatsEvent::TicketSpent { typ, amount } => match typ {
|
||||
TicketType::V1MixnetEntry => self.stats.mixnet_entry_spent += amount,
|
||||
TicketType::V1MixnetExit => self.stats.mixnet_exit_spent += amount,
|
||||
TicketType::V1WireguardEntry => self.stats.vpn_entry_spent += amount,
|
||||
TicketType::V1WireguardExit => self.stats.vpn_exit_spent += amount,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn report(&self) -> CredentialStats {
|
||||
self.stats
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ pub mod nym_api_statistics;
|
||||
/// Packet count based statistics.
|
||||
pub mod packet_statistics;
|
||||
|
||||
pub mod credential;
|
||||
pub mod connection;
|
||||
|
||||
/// Channel receiving generic stats events to be used by a statistics aggregator.
|
||||
pub type ClientStatsReceiver = tokio::sync::mpsc::UnboundedReceiver<ClientStatsEvents>;
|
||||
@@ -52,7 +52,7 @@ pub enum ClientStatsEvents {
|
||||
/// Nym API connection events
|
||||
NymApi(nym_api_statistics::NymApiStatsEvent),
|
||||
/// Credential events
|
||||
Credential(credential::CredentialStatsEvent),
|
||||
Connection(connection::ConnectionStatsEvent),
|
||||
}
|
||||
|
||||
/// Controls stats event handling and reporting
|
||||
@@ -67,7 +67,7 @@ pub struct ClientStatsController {
|
||||
packet_stats: packet_statistics::PacketStatisticsControl,
|
||||
gateway_conn_stats: gateway_conn_statistics::GatewayStatsControl,
|
||||
nym_api_stats: nym_api_statistics::NymApiStatsControl,
|
||||
credential_stats: credential::CredentialStatsControl,
|
||||
connection_stats: connection::ConnectionStatsControl,
|
||||
}
|
||||
|
||||
impl ClientStatsController {
|
||||
@@ -81,7 +81,7 @@ impl ClientStatsController {
|
||||
packet_stats: Default::default(),
|
||||
gateway_conn_stats: Default::default(),
|
||||
nym_api_stats: Default::default(),
|
||||
credential_stats: Default::default(),
|
||||
connection_stats: Default::default(),
|
||||
}
|
||||
}
|
||||
/// Returns a static ClientStatsReport that can be sent somewhere
|
||||
@@ -94,7 +94,7 @@ impl ClientStatsController {
|
||||
packet_stats: self.packet_stats.report(),
|
||||
gateway_conn_stats: self.gateway_conn_stats.report(),
|
||||
nym_api_stats: self.nym_api_stats.report(),
|
||||
credential_stats: self.credential_stats.report(),
|
||||
connection_stats: self.connection_stats.report(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,7 +104,7 @@ impl ClientStatsController {
|
||||
ClientStatsEvents::PacketStatistics(event) => self.packet_stats.handle_event(event),
|
||||
ClientStatsEvents::GatewayConn(event) => self.gateway_conn_stats.handle_event(event),
|
||||
ClientStatsEvents::NymApi(event) => self.nym_api_stats.handle_event(event),
|
||||
ClientStatsEvents::Credential(event) => self.credential_stats.handle_event(event),
|
||||
ClientStatsEvents::Connection(event) => self.connection_stats.handle_event(event),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,7 +114,7 @@ impl ClientStatsController {
|
||||
pub fn reset(&mut self) {
|
||||
self.nym_api_stats = Default::default();
|
||||
self.gateway_conn_stats = Default::default();
|
||||
self.credential_stats = Default::default();
|
||||
self.connection_stats = Default::default();
|
||||
//no periodic reset for packet stats
|
||||
|
||||
self.last_update_time = ClientStatsController::get_update_time();
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use crate::clients::{
|
||||
credential::CredentialStats, gateway_conn_statistics::GatewayStats,
|
||||
connection::ConnectionStats, gateway_conn_statistics::GatewayStats,
|
||||
nym_api_statistics::NymApiStats, packet_statistics::PacketStatistics,
|
||||
};
|
||||
|
||||
@@ -22,7 +22,7 @@ pub struct ClientStatsReport {
|
||||
pub(crate) packet_stats: PacketStatistics,
|
||||
pub(crate) gateway_conn_stats: GatewayStats,
|
||||
pub(crate) nym_api_stats: NymApiStats,
|
||||
pub(crate) credential_stats: CredentialStats,
|
||||
pub(crate) connection_stats: ConnectionStats,
|
||||
}
|
||||
|
||||
impl From<ClientStatsReport> for Vec<u8> {
|
||||
|
||||
@@ -78,7 +78,9 @@ pub use nym_sphinx::{
|
||||
anonymous_replies::requests::AnonymousSenderTag,
|
||||
receiver::ReconstructedMessage,
|
||||
};
|
||||
pub use nym_statistics_common::clients::ClientStatsEvents;
|
||||
pub use nym_statistics_common::clients::{
|
||||
connection::ConnectionStatsEvent, ClientStatsEvents, ClientStatsSender,
|
||||
};
|
||||
pub use nym_task::connections::TransmissionLane;
|
||||
pub use nym_topology::{provider_trait::TopologyProvider, NymTopology};
|
||||
pub use paths::StoragePaths;
|
||||
|
||||
@@ -189,6 +189,11 @@ impl MixnetClient {
|
||||
self.stats_events_reporter.report(event);
|
||||
}
|
||||
|
||||
/// Get a clone of stats_events_reporter for easier use
|
||||
pub fn stats_events_reporter(&self) -> ClientStatsSender {
|
||||
self.stats_events_reporter.clone()
|
||||
}
|
||||
|
||||
/// Disconnect from the mixnet. Currently it is not supported to reconnect a disconnected
|
||||
/// client.
|
||||
pub async fn disconnect(mut self) {
|
||||
|
||||
Reference in New Issue
Block a user