From 054e3b10e980802beaa8f38ccb16de0d2f6c6fe5 Mon Sep 17 00:00:00 2001 From: SW van Heerden Date: Mon, 13 Nov 2023 13:43:46 +0200 Subject: [PATCH] Print out warning if base node is stale block wallet from sending if metadata is old --- .../src/ui/components/base_node.rs | 26 +++++++++++++---- .../wallet/src/base_node_service/service.rs | 29 ++++++++++++++----- 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/applications/minotari_console_wallet/src/ui/components/base_node.rs b/applications/minotari_console_wallet/src/ui/components/base_node.rs index 97866d9cf51..c7a00acdf1d 100644 --- a/applications/minotari_console_wallet/src/ui/components/base_node.rs +++ b/applications/minotari_console_wallet/src/ui/components/base_node.rs @@ -20,6 +20,7 @@ // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +use chrono::Utc; use minotari_wallet::connectivity_service::{OnlineStatus, WalletConnectivityInterface}; use tui::{ backend::Backend, @@ -88,6 +89,8 @@ impl Component for BaseNode { ) }; + let updated = base_node_state.updated.unwrap_or(Utc::now().naive_utc()); + let latency = base_node_state.latency.unwrap_or_default().as_millis(); let latency_color = match latency { 0 => Color::Gray, // offline? default duration is 0 @@ -96,19 +99,32 @@ impl Component for BaseNode { _ => Color::Red, }; - let tip_info = vec![ + let mut tip_info = vec![ Span::styled("Chain Tip:", Style::default().fg(Color::Magenta)), Span::raw(" "), Span::styled(format!("#{}", tip), Style::default().fg(tip_color)), Span::raw(" "), Span::styled(sync_text.to_string(), Style::default().fg(Color::White)), Span::raw(" "), - Span::styled("Latency", Style::default().fg(Color::White)), - Span::raw(" "), - Span::styled(latency.to_string(), Style::default().fg(latency_color)), - Span::styled(" ms", Style::default().fg(Color::DarkGray)), ]; + let mut latency_span = if Utc::now().naive_utc().timestamp() - updated.timestamp() > 15 * 60 { + vec![ + Span::styled("Last updated", Style::default().fg(Color::Red)), + Span::raw(" "), + Span::styled(updated.to_string(), Style::default().fg(Color::Red)), + Span::styled(" s", Style::default().fg(Color::Red)), + ] + } else { + vec![ + Span::styled("Latency", Style::default().fg(Color::White)), + Span::raw(" "), + Span::styled(latency.to_string(), Style::default().fg(latency_color)), + Span::styled(" ms", Style::default().fg(Color::DarkGray)), + ] + }; + tip_info.append(&mut latency_span); + Spans::from(tip_info) } else { Spans::from(vec![ diff --git a/base_layer/wallet/src/base_node_service/service.rs b/base_layer/wallet/src/base_node_service/service.rs index beb52c877c8..3fa2f929588 100644 --- a/base_layer/wallet/src/base_node_service/service.rs +++ b/base_layer/wallet/src/base_node_service/service.rs @@ -22,7 +22,7 @@ use std::{sync::Arc, time::Duration}; -use chrono::NaiveDateTime; +use chrono::{NaiveDateTime, Utc}; use futures::{future, StreamExt}; use log::*; use tari_common_types::chain_metadata::ChainMetadata; @@ -152,13 +152,26 @@ where T: WalletBackend + 'static "Handling Wallet Base Node Service Request: {:?}", request ); match request { - BaseNodeServiceRequest::GetChainMetadata => match self.get_state().await.chain_metadata { - Some(metadata) => Ok(BaseNodeServiceResponse::ChainMetadata(Some(metadata))), - None => { - // if we don't have live state, check if we've previously stored state in the wallet db - let metadata = self.db.get_chain_metadata()?; - Ok(BaseNodeServiceResponse::ChainMetadata(metadata)) - }, + BaseNodeServiceRequest::GetChainMetadata => { + // if the wallet has not gotten a ChainMetaData in the last 15 minutes, we dont return one as this can + // be an issue using old data to send transactions. Wallets should have up to date ChainMetaData as they + // need to have an active connection to a base node to send transactions. + let last_updated = self + .get_state() + .await + .updated + .ok_or(BaseNodeServiceError::NoChainMetadata)?; + if Utc::now().naive_utc().timestamp() - last_updated.timestamp() > 15 * 60 { + return Err(BaseNodeServiceError::NoChainMetadata); + } + match self.get_state().await.chain_metadata { + Some(metadata) => Ok(BaseNodeServiceResponse::ChainMetadata(Some(metadata))), + None => { + // if we don't have live state, check if we've previously stored state in the wallet db + let metadata = self.db.get_chain_metadata()?; + Ok(BaseNodeServiceResponse::ChainMetadata(metadata)) + }, + } }, BaseNodeServiceRequest::GetBaseNodeLatency => { Ok(BaseNodeServiceResponse::Latency(self.state.read().await.latency))