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

Move account controller checks into the state machine connecting state #2020

Merged
merged 9 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
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: 3 additions & 0 deletions nym-vpn-core/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions nym-vpn-core/crates/nym-vpn-account-controller/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ nym-http-api-client.workspace = true
nym-sdk.workspace = true
nym-validator-client.workspace = true
nym-vpn-api-client = { path = "../nym-vpn-api-client" }
nym-vpn-lib-types = { path = "../nym-vpn-lib-types", features = ["nym-type-conversions"] }
nym-vpn-network-config = { path = "../nym-vpn-network-config" }
nym-vpn-store = { path = "../nym-vpn-store" }
nym-wg-gateway-client = { path = "../nym-wg-gateway-client" }
Expand Down
51 changes: 29 additions & 22 deletions nym-vpn-core/crates/nym-vpn-account-controller/src/commander.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
// SPDX-License-Identifier: GPL-3.0-only

use nym_vpn_api_client::response::{NymVpnAccountSummaryResponse, NymVpnDevice, NymVpnUsage};
use nym_vpn_lib_types::{
AccountCommandError, RegisterDeviceError, RequestZkNymError, SyncAccountError, SyncDeviceError,
};
use nym_vpn_store::mnemonic::Mnemonic;
use tokio::sync::mpsc::UnboundedSender;

use crate::{
commands::{
request_zknym::RequestZkNymSummary, AccountCommand, AccountCommandError, ReturnSender,
},
commands::{request_zknym::RequestZkNymSummary, AccountCommand, ReturnSender},
error::Error,
shared_state::{AccountRegistered, DeviceState, SharedAccountState},
AvailableTicketbooks,
Expand Down Expand Up @@ -56,20 +57,20 @@ impl AccountControllerCommander {

pub async fn sync_account_state(
&self,
) -> Result<NymVpnAccountSummaryResponse, AccountCommandError> {
) -> Result<NymVpnAccountSummaryResponse, SyncAccountError> {
let (tx, rx) = ReturnSender::new();
self.command_tx
.send(AccountCommand::SyncAccountState(Some(tx)))
.map_err(AccountCommandError::internal)?;
rx.await.map_err(AccountCommandError::internal)?
.map_err(SyncAccountError::internal)?;
rx.await.map_err(SyncAccountError::internal)?
}

pub async fn sync_device_state(&self) -> Result<DeviceState, AccountCommandError> {
pub async fn sync_device_state(&self) -> Result<DeviceState, SyncDeviceError> {
let (tx, rx) = ReturnSender::new();
self.command_tx
.send(AccountCommand::SyncDeviceState(Some(tx)))
.map_err(AccountCommandError::internal)?;
rx.await.map_err(AccountCommandError::internal)?
.map_err(SyncDeviceError::internal)?;
rx.await.map_err(SyncDeviceError::internal)?
}

pub async fn get_usage(&self) -> Result<Vec<NymVpnUsage>, AccountCommandError> {
Expand All @@ -88,12 +89,12 @@ impl AccountControllerCommander {
rx.await.map_err(AccountCommandError::internal)?
}

pub async fn register_device(&self) -> Result<NymVpnDevice, AccountCommandError> {
pub async fn register_device(&self) -> Result<NymVpnDevice, RegisterDeviceError> {
let (tx, rx) = ReturnSender::new();
self.command_tx
.send(AccountCommand::RegisterDevice(Some(tx)))
.map_err(AccountCommandError::internal)?;
rx.await.map_err(AccountCommandError::internal)?
.map_err(RegisterDeviceError::internal)?;
rx.await.map_err(RegisterDeviceError::internal)?
}

pub async fn get_devices(&self) -> Result<Vec<NymVpnDevice>, AccountCommandError> {
Expand All @@ -120,12 +121,12 @@ impl AccountControllerCommander {
rx.await.map_err(AccountCommandError::internal)?
}

pub async fn request_zk_nyms(&self) -> Result<RequestZkNymSummary, AccountCommandError> {
pub async fn request_zk_nyms(&self) -> Result<RequestZkNymSummary, RequestZkNymError> {
let (tx, rx) = ReturnSender::new();
self.command_tx
.send(AccountCommand::RequestZkNym(Some(tx)))
.map_err(AccountCommandError::internal)?;
rx.await.map_err(AccountCommandError::internal)?
.map_err(RequestZkNymError::internal)?;
rx.await.map_err(RequestZkNymError::internal)?
}
}

Expand All @@ -135,7 +136,8 @@ impl AccountControllerCommander {
impl AccountControllerCommander {
pub async fn ensure_update_account(
&self,
) -> Result<Option<NymVpnAccountSummaryResponse>, AccountCommandError> {
) -> Result<Option<NymVpnAccountSummaryResponse>, SyncAccountError> {
tracing::debug!("Ensuring account is synced");
let state = self.shared_state.lock().await.clone();
match state.account_registered {
Some(AccountRegistered::Registered) => return Ok(None),
Expand All @@ -144,7 +146,8 @@ impl AccountControllerCommander {
self.sync_account_state().await.map(Some)
}

pub async fn ensure_update_device(&self) -> Result<DeviceState, AccountCommandError> {
pub async fn ensure_update_device(&self) -> Result<DeviceState, SyncDeviceError> {
tracing::debug!("Ensuring device is synced");
let state = self.shared_state.lock().await.clone();
match state.device {
Some(DeviceState::Active) => return Ok(DeviceState::Active),
Expand All @@ -156,7 +159,8 @@ impl AccountControllerCommander {
self.sync_device_state().await
}

pub async fn ensure_register_device(&self) -> Result<(), AccountCommandError> {
pub async fn ensure_register_device(&self) -> Result<(), RegisterDeviceError> {
tracing::debug!("Ensuring device is registered");
let state = self.shared_state.lock().await.clone();
match state.device {
Some(DeviceState::Active) => return Ok(()),
Expand All @@ -168,10 +172,12 @@ impl AccountControllerCommander {
self.register_device().await.map(|_device| ())
}

pub async fn ensure_available_zk_nyms(&self) -> Result<(), AccountCommandError> {
pub async fn ensure_available_zk_nyms(&self) -> Result<(), RequestZkNymError> {
tracing::debug!("Ensuring available zk-nyms in the local credential store");
if self
.get_available_tickets()
.await?
.await
.map_err(|err| RequestZkNymError::CredentialStorage(err.to_string()))?
.is_all_ticket_types_above_threshold(0)
{
// If all ticket types are above zero, we're good to go. Additional ticketbooks will
Expand All @@ -183,8 +189,8 @@ impl AccountControllerCommander {
let results = self.request_zk_nyms().await?;

// If any of them failed, return an error
if results.iter().any(Result::is_err) {
Err(AccountCommandError::from(results))
if let Some(Err(err)) = results.into_iter().find(Result::is_err) {
Err(err.clone())
} else {
Ok(())
}
Expand All @@ -194,6 +200,7 @@ impl AccountControllerCommander {
&self,
credential_mode: bool,
) -> Result<(), AccountCommandError> {
tracing::debug!("Waiting for account to be ready to connect");
self.ensure_update_account().await?;
self.ensure_update_device().await?;
self.ensure_register_device().await?;
Expand Down
Loading
Loading