Skip to content

Commit

Permalink
feat: base_node switching for console_wallet when status is offline
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
StriderDM committed Dec 8, 2021
1 parent 363b254 commit c9f9f04
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl BaseNode {
impl<B: Backend> Component<B> for BaseNode {
fn draw(&mut self, f: &mut Frame<B>, 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 {
Expand All @@ -56,7 +57,6 @@ impl<B: Backend> Component<B> 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();

Expand Down
2 changes: 2 additions & 0 deletions applications/tari_console_wallet/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ pub fn run(app: App<CrosstermBackend<Stdout>>) -> 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");
Expand Down
25 changes: 23 additions & 2 deletions applications/tari_console_wallet/src/ui/state/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ use crate::{
utils::db::{CUSTOM_BASE_NODE_ADDRESS_KEY, CUSTOM_BASE_NODE_PUBLIC_KEY_KEY},
wallet_modes::PeerConfig,
};
use tari_wallet::connectivity_service::{OnlineStatus, WalletConnectivityInterface};

const LOG_TARGET: &str = "wallet::console_wallet::app_state";

Expand Down Expand Up @@ -158,6 +159,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);
Expand All @@ -181,6 +183,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;

Expand Down Expand Up @@ -683,15 +706,13 @@ 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() {
if let Ok(p) = peer_manager.find_by_node_id(c.peer_node_id()).await {
peers.push(p);
}
}

self.data.connected_peers = peers;
self.updated = true;
Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ impl MockBaseNodeService {
},
None => (None, None),
};

self.state = BaseNodeState {
chain_metadata,
is_synced,
Expand Down

0 comments on commit c9f9f04

Please sign in to comment.