Skip to content

Commit

Permalink
feat: add filtering of abandoned coinbase txs to console wallet (#3032)
Browse files Browse the repository at this point in the history
  • Loading branch information
stringhandler committed Jun 30, 2021
2 parents 229830e + 91af287 commit ae15fd9
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 31 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions applications/tari_console_wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ tari_app_grpc = { path = "../tari_app_grpc" }
tari_shutdown = { path = "../../infrastructure/shutdown" }
tari_key_manager = { path = "../../base_layer/key_manager" }

bitflags = "1.2.1"
chrono = { version = "0.4.6", features = ["serde"]}
chrono-english = "0.1"
futures = { version = "^0.3.1", default-features = false, features = ["alloc"]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,13 @@ impl TransactionsTab {
.title(Span::styled("Completed (T)ransactions", style));
f.render_widget(block, list_areas[1]);

self.completed_list_state
.set_num_items(app_state.get_completed_txs().len());
let completed_txs = app_state.get_completed_txs();
self.completed_list_state.set_num_items(completed_txs.len());
let mut completed_list_state = self
.completed_list_state
.get_list_state((list_areas[1].height as usize).saturating_sub(3));
let window = self.completed_list_state.get_start_end();
let windowed_view = app_state.get_completed_txs_slice(window.0, window.1);
let windowed_view = &completed_txs[window.0..window.1];

let mut column0_items = Vec::new();
let mut column1_items = Vec::new();
Expand Down Expand Up @@ -188,7 +188,9 @@ impl TransactionsTab {
format!("{}", local_time.format("%Y-%m-%d %H:%M:%S")),
Style::default().fg(text_color),
)));
let status = if t.cancelled {
let status = if t.cancelled && t.status == TransactionStatus::Coinbase {
"Abandoned".to_string()
} else if t.cancelled {
"Cancelled".to_string()
} else if !t.valid {
"Invalid".to_string()
Expand Down Expand Up @@ -425,6 +427,8 @@ impl<B: Backend> Component<B> for TransactionsTab {
span_vec.push(Span::raw(" selects a transaction, "));
span_vec.push(Span::styled("C", Style::default().add_modifier(Modifier::BOLD)));
span_vec.push(Span::raw(" cancels a selected Pending Tx, "));
span_vec.push(Span::styled("A", Style::default().add_modifier(Modifier::BOLD)));
span_vec.push(Span::raw(" shows abandoned coinbase Txs, "));
span_vec.push(Span::styled("Esc", Style::default().add_modifier(Modifier::BOLD)));
span_vec.push(Span::raw(" exits the list."));

Expand Down Expand Up @@ -512,6 +516,7 @@ impl<B: Backend> Component<B> for TransactionsTab {
self.confirmation_dialog = true;
}
},
'a' => app_state.toggle_abandoned_coinbase_filter(),
'\n' => match self.selected_tx_list {
SelectedTransactionList::None => {},
SelectedTransactionList::PendingTxs => {
Expand Down
69 changes: 42 additions & 27 deletions applications/tari_console_wallet/src/ui/state/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use crate::{
utils::db::{CUSTOM_BASE_NODE_ADDRESS_KEY, CUSTOM_BASE_NODE_PUBLIC_KEY_KEY},
wallet_modes::PeerConfig,
};
use bitflags::bitflags;
use futures::{stream::Fuse, StreamExt};
use log::*;
use qrcode::{render::unicode, QrCode};
Expand Down Expand Up @@ -71,6 +72,7 @@ const LOG_TARGET: &str = "wallet::console_wallet::app_state";
pub struct AppState {
inner: Arc<RwLock<AppStateInner>>,
cached_data: AppStateData,
completed_tx_filter: TransactionFilter,
node_config: GlobalConfig,
}

Expand All @@ -85,9 +87,11 @@ impl AppState {
) -> Self {
let inner = AppStateInner::new(node_identity, network, wallet, base_node_selected, base_node_config);
let cached_data = inner.data.clone();

Self {
inner: Arc::new(RwLock::new(inner)),
cached_data,
completed_tx_filter: TransactionFilter::ABANDONED_COINBASES,
node_config,
}
}
Expand All @@ -100,33 +104,31 @@ impl AppState {
pub async fn refresh_transaction_state(&mut self) -> Result<(), UiError> {
let mut inner = self.inner.write().await;
inner.refresh_full_transaction_state().await?;
if let Some(data) = inner.get_updated_app_state() {
self.cached_data = data;
}
drop(inner);
self.update_cache().await;
Ok(())
}

pub async fn refresh_contacts_state(&mut self) -> Result<(), UiError> {
let mut inner = self.inner.write().await;
inner.refresh_contacts_state().await?;
if let Some(data) = inner.get_updated_app_state() {
self.cached_data = data;
}
drop(inner);
self.update_cache().await;
Ok(())
}

pub async fn refresh_connected_peers_state(&mut self) -> Result<(), UiError> {
let mut inner = self.inner.write().await;
inner.refresh_connected_peers_state().await?;
if let Some(data) = inner.get_updated_app_state() {
self.cached_data = data;
}
drop(inner);
self.update_cache().await;
Ok(())
}

pub async fn update_cache(&mut self) {
let mut inner = self.inner.write().await;
if let Some(data) = inner.get_updated_app_state() {
let updated_state = inner.get_updated_app_state();
if let Some(data) = updated_state {
self.cached_data = data;
}
}
Expand All @@ -145,9 +147,8 @@ impl AppState {
inner.wallet.contacts_service.upsert_contact(contact).await?;

inner.refresh_contacts_state().await?;
if let Some(data) = inner.get_updated_app_state() {
self.cached_data = data;
}
drop(inner);
self.update_cache().await;
Ok(())
}

Expand All @@ -161,9 +162,8 @@ impl AppState {
inner.wallet.contacts_service.remove_contact(public_key).await?;

inner.refresh_contacts_state().await?;
if let Some(data) = inner.get_updated_app_state() {
self.cached_data = data;
}
drop(inner);
self.update_cache().await;
Ok(())
}

Expand Down Expand Up @@ -274,25 +274,29 @@ impl AppState {
}
}

pub fn get_completed_txs_slice(&self, start: usize, end: usize) -> &[CompletedTransaction] {
if self.cached_data.completed_txs.is_empty() || start > end || end > self.cached_data.completed_txs.len() {
return &[];
pub fn get_completed_txs(&self) -> Vec<&CompletedTransaction> {
if self
.completed_tx_filter
.contains(TransactionFilter::ABANDONED_COINBASES)
{
self.cached_data
.completed_txs
.iter()
.filter(|tx| !(tx.cancelled && tx.status == TransactionStatus::Coinbase))
.collect()
} else {
self.cached_data.completed_txs.iter().collect()
}

&self.cached_data.completed_txs[start..end]
}

pub fn get_completed_txs(&self) -> &Vec<CompletedTransaction> {
&self.cached_data.completed_txs
}

pub fn get_confirmations(&self, tx_id: &TxId) -> Option<&u64> {
(&self.cached_data.confirmations).get(tx_id)
}

pub fn get_completed_tx(&self, index: usize) -> Option<&CompletedTransaction> {
if index < self.cached_data.completed_txs.len() {
Some(&self.cached_data.completed_txs[index])
let filtered_completed_txs = self.get_completed_txs();
if index < filtered_completed_txs.len() {
Some(filtered_completed_txs[index])
} else {
None
}
Expand Down Expand Up @@ -363,6 +367,10 @@ impl AppState {
pub fn get_required_confirmations(&self) -> u64 {
(&self.node_config.transaction_num_confirmations_required).to_owned()
}

pub fn toggle_abandoned_coinbase_filter(&mut self) {
self.completed_tx_filter.toggle(TransactionFilter::ABANDONED_COINBASES);
}
}

pub struct AppStateInner {
Expand Down Expand Up @@ -864,3 +872,10 @@ pub enum UiTransactionSendStatus {
SentViaSaf,
Error(String),
}

bitflags! {
pub struct TransactionFilter: u8 {
const NONE = 0b0000_0000;
const ABANDONED_COINBASES = 0b0000_0001;
}
}

0 comments on commit ae15fd9

Please sign in to comment.