From ca5f0ee70569fe78c19ddae77078ef2da9bfc142 Mon Sep 17 00:00:00 2001 From: David Main <51991544+StriderDM@users.noreply.github.com> Date: Wed, 22 Dec 2021 09:28:18 +0200 Subject: [PATCH] feat: base_node switching for console_wallet when status is offline (#3639) Description --- This PR allows the console_wallet to periodically attempt to connect to another base_node in the list should it be found to be offline. Motivation and Context --- Usability. How Has This Been Tested? --- cargo test --all nvm use 12.22.6 && node_modules/.bin/cucumber-js --profile "ci" --tags "not @long-running and not @broken" manually --- .../src/ui/components/base_node.rs | 2 +- .../tari_console_wallet/src/ui/mod.rs | 2 ++ .../src/ui/state/app_state.rs | 26 ++++++++++++++++--- .../mock_base_node_service.rs | 1 + 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/applications/tari_console_wallet/src/ui/components/base_node.rs b/applications/tari_console_wallet/src/ui/components/base_node.rs index 0e47820a19..f180800f21 100644 --- a/applications/tari_console_wallet/src/ui/components/base_node.rs +++ b/applications/tari_console_wallet/src/ui/components/base_node.rs @@ -43,6 +43,7 @@ impl BaseNode { impl Component for BaseNode { fn draw(&mut self, f: &mut Frame, area: Rect, app_state: &AppState) where B: Backend { + let base_node_state = app_state.get_base_node_state(); let current_online_status = app_state.get_wallet_connectivity().get_connectivity_status(); let chain_info = match current_online_status { @@ -57,7 +58,6 @@ impl Component for BaseNode { Span::styled("Offline", Style::default().fg(Color::Red)), ]), OnlineStatus::Online => { - let base_node_state = app_state.get_base_node_state(); if let Some(ref metadata) = base_node_state.chain_metadata { let tip = metadata.height_of_longest_chain(); diff --git a/applications/tari_console_wallet/src/ui/mod.rs b/applications/tari_console_wallet/src/ui/mod.rs index 0113d6cb75..3468bbb785 100644 --- a/applications/tari_console_wallet/src/ui/mod.rs +++ b/applications/tari_console_wallet/src/ui/mod.rs @@ -62,6 +62,8 @@ pub fn run(app: App>) -> Result<(), ExitCodes> { app.app_state.refresh_contacts_state().await?; trace!(target: LOG_TARGET, "Refreshing connected peers state"); app.app_state.refresh_connected_peers_state().await?; + trace!(target: LOG_TARGET, "Checking connectivity"); + app.app_state.check_connectivity().await; trace!(target: LOG_TARGET, "Starting balance enquiry debouncer"); app.app_state.start_balance_enquiry_debouncer().await?; trace!(target: LOG_TARGET, "Starting app state event monitor"); diff --git a/applications/tari_console_wallet/src/ui/state/app_state.rs b/applications/tari_console_wallet/src/ui/state/app_state.rs index 64e643f399..6b5558ae0f 100644 --- a/applications/tari_console_wallet/src/ui/state/app_state.rs +++ b/applications/tari_console_wallet/src/ui/state/app_state.rs @@ -52,7 +52,7 @@ use tari_p2p::auto_update::SoftwareUpdaterHandle; use tari_shutdown::ShutdownSignal; use tari_wallet::{ base_node_service::{handle::BaseNodeEventReceiver, service::BaseNodeState}, - connectivity_service::WalletConnectivityHandle, + connectivity_service::{OnlineStatus, WalletConnectivityHandle, WalletConnectivityInterface}, contacts_service::storage::database::Contact, output_manager_service::{handle::OutputManagerEventReceiver, service::Balance}, transaction_service::{handle::TransactionEventReceiver, storage::models::CompletedTransaction}, @@ -157,6 +157,7 @@ impl AppState { } pub async fn refresh_connected_peers_state(&mut self) -> Result<(), UiError> { + self.check_connectivity().await; let mut inner = self.inner.write().await; inner.refresh_connected_peers_state().await?; drop(inner); @@ -180,6 +181,27 @@ impl AppState { } } + pub async fn check_connectivity(&mut self) { + if self.get_custom_base_node().is_none() && + self.wallet_connectivity.get_connectivity_status() == OnlineStatus::Offline + { + let current = self.get_selected_base_node(); + let list = self.get_base_node_list().clone(); + let mut index: usize = list.iter().position(|(_, p)| p == current).unwrap_or_default(); + if !list.is_empty() { + if index == list.len() - 1 { + index = 0; + } else { + index += 1; + } + let (_, next) = &list[index]; + if let Err(e) = self.set_base_node_peer(next.clone()).await { + error!(target: LOG_TARGET, "Base node offline: {:?}", e); + } + } + } + } + pub async fn upsert_contact(&mut self, alias: String, public_key_or_emoji_id: String) -> Result<(), UiError> { let mut inner = self.inner.write().await; @@ -682,7 +704,6 @@ impl AppStateInner { pub async fn refresh_connected_peers_state(&mut self) -> Result<(), UiError> { let connections = self.wallet.comms.connectivity().get_active_connections().await?; - let peer_manager = self.wallet.comms.peer_manager(); let mut peers = Vec::with_capacity(connections.len()); for c in connections.iter() { @@ -690,7 +711,6 @@ impl AppStateInner { peers.push(p); } } - self.data.connected_peers = peers; self.updated = true; Ok(()) diff --git a/base_layer/wallet/src/base_node_service/mock_base_node_service.rs b/base_layer/wallet/src/base_node_service/mock_base_node_service.rs index e3d6e01525..18eda7a77c 100644 --- a/base_layer/wallet/src/base_node_service/mock_base_node_service.rs +++ b/base_layer/wallet/src/base_node_service/mock_base_node_service.rs @@ -86,6 +86,7 @@ impl MockBaseNodeService { }, None => (None, None), }; + self.state = BaseNodeState { chain_metadata, is_synced,