Skip to content

Commit

Permalink
feat: update console wallet notifications
Browse files Browse the repository at this point in the history
This PR creates console wallet notifications for transaction events and also adds a hotkey to clear notifications.

A bug was also spotted in the way that Faux transaction were validated that resulted in the last Faux transaction confirmed event to be emitted after each validation. The validation was updated to only emit this event if the transaction was previously not confirmed.
  • Loading branch information
philipr-za committed Mar 2, 2022
1 parent 4274f25 commit e3e8b3d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,21 @@ impl NotificationTab {

fn draw_notifications<B>(&mut self, f: &mut Frame<B>, area: Rect, app_state: &AppState)
where B: Backend {
let span_vec = vec![
Span::raw("Press "),
Span::styled("C", Style::default().add_modifier(Modifier::BOLD)),
Span::raw(" to clear notifications"),
];

let instructions = Paragraph::new(Spans::from(span_vec)).wrap(Wrap { trim: false });

let block = Block::default().borders(Borders::ALL).title(Span::styled(
"Notifications",
Style::default().fg(Color::White).add_modifier(Modifier::BOLD),
));
f.render_widget(block, area);
let notifications_area = Layout::default()
.constraints([Constraint::Min(42)].as_ref())
.constraints([Constraint::Length(1), Constraint::Min(42)].as_ref())
.margin(1)
.split(area);
let text = app_state
Expand All @@ -53,7 +61,9 @@ impl NotificationTab {
})
.collect::<Vec<_>>();
let paragraph = Paragraph::new(text).wrap(Wrap { trim: true });
f.render_widget(paragraph, notifications_area[0]);

f.render_widget(instructions, notifications_area[0]);
f.render_widget(paragraph, notifications_area[1]);
}
}

Expand All @@ -80,4 +90,10 @@ impl<B: Backend> Component<B> for NotificationTab {
false => Spans::from(Span::styled(title.to_owned(), Style::default().fg(Color::White))),
}
}

fn on_key(&mut self, app_state: &mut AppState, c: char) {
if c == 'c' {
Handle::current().block_on(app_state.clear_notifications());
}
}
}
11 changes: 11 additions & 0 deletions applications/tari_console_wallet/src/ui/state/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,11 @@ impl AppState {
}
}

pub async fn clear_notifications(&mut self) {
let mut inner = self.inner.write().await;
inner.clear_notifications();
}

pub fn get_default_fee_per_gram(&self) -> MicroTari {
// this should not be empty as we this should have been created, but lets just be safe and use the default value
// from the config
Expand Down Expand Up @@ -962,6 +967,12 @@ impl AppStateInner {
self.updated = true;
}

pub fn clear_notifications(&mut self) {
self.data.notifications.clear();
self.data.new_notification_count = 0;
self.updated = true;
}

pub fn get_software_updater(&self) -> SoftwareUpdaterHandle {
self.wallet.get_software_updater()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,29 +96,44 @@ impl WalletEventMonitor {
self.trigger_tx_state_refresh(tx_id).await;
self.trigger_balance_refresh();
notifier.transaction_received(tx_id);
self.add_notification(format!("Finalized Transaction Received - TxId: {}", tx_id)).await;
},
TransactionEvent::TransactionMinedUnconfirmed{tx_id, num_confirmations, is_valid: _} |
TransactionEvent::FauxTransactionUnconfirmed{tx_id, num_confirmations, is_valid: _}=> {
self.trigger_confirmations_refresh(tx_id, num_confirmations).await;
self.trigger_tx_state_refresh(tx_id).await;
self.trigger_balance_refresh();
notifier.transaction_mined_unconfirmed(tx_id, num_confirmations);
self.add_notification(format!("Transaction Mined Unconfirmed with {} confirmations - TxId: {}", num_confirmations, tx_id)).await;
},
TransactionEvent::TransactionMined{tx_id, is_valid: _} |
TransactionEvent::FauxTransactionConfirmed{tx_id, is_valid: _}=> {
self.trigger_confirmations_cleanup(tx_id).await;
self.trigger_tx_state_refresh(tx_id).await;
self.trigger_balance_refresh();
notifier.transaction_mined(tx_id);
self.add_notification(format!("Transaction Confirmed - TxId: {}", tx_id)).await;
},
TransactionEvent::TransactionCancelled(tx_id, _) => {
self.trigger_tx_state_refresh(tx_id).await;
self.trigger_balance_refresh();
notifier.transaction_cancelled(tx_id);
},
TransactionEvent::ReceivedTransaction(tx_id) |
TransactionEvent::ReceivedTransactionReply(tx_id) |
TransactionEvent::TransactionBroadcast(tx_id) |
TransactionEvent::ReceivedTransaction(tx_id) => {
self.trigger_tx_state_refresh(tx_id).await;
self.trigger_balance_refresh();
self.add_notification(format!("Transaction Received - TxId: {}", tx_id)).await;
},
TransactionEvent::ReceivedTransactionReply(tx_id) => {
self.trigger_tx_state_refresh(tx_id).await;
self.trigger_balance_refresh();
self.add_notification(format!("Transaction Reply Received - TxId: {}", tx_id)).await;
},
TransactionEvent::TransactionBroadcast(tx_id) => {
self.trigger_tx_state_refresh(tx_id).await;
self.trigger_balance_refresh();
self.add_notification(format!("Transaction Broadcast to Mempool - TxId: {}", tx_id)).await;
},
TransactionEvent::TransactionMinedRequestTimedOut(tx_id) |
TransactionEvent::TransactionImported(tx_id) => {
self.trigger_tx_state_refresh(tx_id).await;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use std::sync::Arc;

use log::*;
use tari_common_types::types::BlockHash;
use tari_common_types::{transaction::TransactionStatus, types::BlockHash};

use crate::{
output_manager_service::{handle::OutputManagerHandle, storage::OutputStatus},
Expand Down Expand Up @@ -112,6 +112,7 @@ pub async fn check_faux_transactions<TBackend: 'static + TransactionBackend>(
vec![0u8; 32]
};
let is_valid = tip_height >= mined_height;
let was_confirmed = tx.status == TransactionStatus::FauxConfirmed;
let is_confirmed = tip_height.saturating_sub(mined_height) >=
TransactionServiceConfig::default().num_confirmations_required;
let num_confirmations = tip_height - mined_height;
Expand Down Expand Up @@ -141,25 +142,29 @@ pub async fn check_faux_transactions<TBackend: 'static + TransactionBackend>(
"Error setting faux transaction to mined confirmed: {}", e
);
} else {
let transaction_event = match is_confirmed {
false => TransactionEvent::FauxTransactionUnconfirmed {
tx_id: tx.tx_id,
num_confirmations: 0,
is_valid,
},
true => TransactionEvent::FauxTransactionConfirmed {
tx_id: tx.tx_id,
is_valid,
},
};
let _ = event_publisher.send(Arc::new(transaction_event)).map_err(|e| {
trace!(
target: LOG_TARGET,
"Error sending event, usually because there are no subscribers: {:?}",
// Only send an event if the transaction was not previously confirmed OR was previously confirmed and is
// now not confirmed (i.e. confirmation changed)
if !(was_confirmed && is_confirmed) {
let transaction_event = match is_confirmed {
false => TransactionEvent::FauxTransactionUnconfirmed {
tx_id: tx.tx_id,
num_confirmations: 0,
is_valid,
},
true => TransactionEvent::FauxTransactionConfirmed {
tx_id: tx.tx_id,
is_valid,
},
};
let _ = event_publisher.send(Arc::new(transaction_event)).map_err(|e| {
trace!(
target: LOG_TARGET,
"Error sending event, usually because there are no subscribers: {:?}",
e
);
e
);
e
});
});
}
}
}
}
Expand Down

0 comments on commit e3e8b3d

Please sign in to comment.