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

feat: base_node switching for console_wallet when status is offline #3639

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,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();
sdbondi marked this conversation as resolved.
Show resolved Hide resolved
let current_online_status = app_state.get_wallet_connectivity().get_connectivity_status();

let chain_info = match current_online_status {
Expand All @@ -57,7 +58,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 @@ -62,6 +62,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
26 changes: 23 additions & 3 deletions applications/tari_console_wallet/src/ui/state/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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);
Expand All @@ -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;

Expand Down Expand Up @@ -682,15 +704,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 @@ -86,6 +86,7 @@ impl MockBaseNodeService {
},
None => (None, None),
};

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