Skip to content

Commit

Permalink
Print out warning if base node is stale
Browse files Browse the repository at this point in the history
block wallet from sending if metadata is old
  • Loading branch information
SWvheerden committed Nov 13, 2023
1 parent 7acc44d commit 054e3b1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -88,6 +89,8 @@ impl<B: Backend> Component<B> 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
Expand All @@ -96,19 +99,32 @@ impl<B: Backend> Component<B> 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![
Expand Down
29 changes: 21 additions & 8 deletions base_layer/wallet/src/base_node_service/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 054e3b1

Please sign in to comment.