-
Notifications
You must be signed in to change notification settings - Fork 220
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
fix: reduce console wallet tui memory usage #3389
Merged
aviator-app
merged 3 commits into
tari-project:development
from
hansieodendaal:ho_reduce_tui_memory_usage
Oct 4, 2021
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,7 @@ use std::{ | |
}; | ||
|
||
use bitflags::bitflags; | ||
use chrono::{DateTime, Local}; | ||
use chrono::{DateTime, Local, NaiveDateTime}; | ||
use log::*; | ||
use qrcode::{render::unicode, QrCode}; | ||
use tari_crypto::{ristretto::RistrettoPublicKey, tari_utilities::hex::Hex}; | ||
|
@@ -75,7 +75,10 @@ use crate::{ | |
utils::db::{CUSTOM_BASE_NODE_ADDRESS_KEY, CUSTOM_BASE_NODE_PUBLIC_KEY_KEY}, | ||
wallet_modes::PeerConfig, | ||
}; | ||
use tari_wallet::output_manager_service::handle::OutputManagerHandle; | ||
use tari_wallet::{ | ||
output_manager_service::handle::OutputManagerHandle, | ||
transaction_service::storage::models::TransactionDirection, | ||
}; | ||
|
||
const LOG_TARGET: &str = "wallet::console_wallet::app_state"; | ||
|
||
|
@@ -320,27 +323,27 @@ impl AppState { | |
&self.cached_data.contacts[start..end] | ||
} | ||
|
||
pub fn get_pending_txs(&self) -> &Vec<CompletedTransaction> { | ||
pub fn get_pending_txs(&self) -> &Vec<CompletedTransactionInfo> { | ||
&self.cached_data.pending_txs | ||
} | ||
|
||
pub fn get_pending_txs_slice(&self, start: usize, end: usize) -> &[CompletedTransaction] { | ||
pub fn get_pending_txs_slice(&self, start: usize, end: usize) -> &[CompletedTransactionInfo] { | ||
if self.cached_data.pending_txs.is_empty() || start > end || end > self.cached_data.pending_txs.len() { | ||
return &[]; | ||
} | ||
|
||
&self.cached_data.pending_txs[start..end] | ||
} | ||
|
||
pub fn get_pending_tx(&self, index: usize) -> Option<&CompletedTransaction> { | ||
pub fn get_pending_tx(&self, index: usize) -> Option<&CompletedTransactionInfo> { | ||
if index < self.cached_data.pending_txs.len() { | ||
Some(&self.cached_data.pending_txs[index]) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
pub fn get_completed_txs(&self) -> Vec<&CompletedTransaction> { | ||
pub fn get_completed_txs(&self) -> Vec<&CompletedTransactionInfo> { | ||
if self | ||
.completed_tx_filter | ||
.contains(TransactionFilter::ABANDONED_COINBASES) | ||
|
@@ -359,7 +362,7 @@ impl AppState { | |
(&self.cached_data.confirmations).get(tx_id) | ||
} | ||
|
||
pub fn get_completed_tx(&self, index: usize) -> Option<&CompletedTransaction> { | ||
pub fn get_completed_tx(&self, index: usize) -> Option<&CompletedTransactionInfo> { | ||
let filtered_completed_txs = self.get_completed_txs(); | ||
if index < filtered_completed_txs.len() { | ||
Some(filtered_completed_txs[index]) | ||
|
@@ -519,7 +522,10 @@ impl AppStateInner { | |
pending_transactions.sort_by(|a: &CompletedTransaction, b: &CompletedTransaction| { | ||
b.timestamp.partial_cmp(&a.timestamp).unwrap() | ||
}); | ||
self.data.pending_txs = pending_transactions; | ||
self.data.pending_txs = pending_transactions | ||
.iter() | ||
.map(|tx| CompletedTransactionInfo::from(tx.clone())) | ||
.collect(); | ||
|
||
let mut completed_transactions: Vec<CompletedTransaction> = Vec::new(); | ||
completed_transactions.extend( | ||
|
@@ -548,7 +554,10 @@ impl AppStateInner { | |
.expect("Should be able to compare timestamps") | ||
}); | ||
|
||
self.data.completed_txs = completed_transactions; | ||
self.data.completed_txs = completed_transactions | ||
.iter() | ||
.map(|tx| CompletedTransactionInfo::from(tx.clone())) | ||
.collect(); | ||
self.updated = true; | ||
Ok(()) | ||
} | ||
|
@@ -590,7 +599,7 @@ impl AppStateInner { | |
}); | ||
}, | ||
Some(tx) => { | ||
let tx = CompletedTransaction::from(tx); | ||
let tx = CompletedTransactionInfo::from(CompletedTransaction::from(tx)); | ||
if let Some(index) = self.data.pending_txs.iter().position(|i| i.tx_id == tx_id) { | ||
if tx.status == TransactionStatus::Pending && !tx.cancelled { | ||
self.data.pending_txs[index] = tx; | ||
|
@@ -852,10 +861,72 @@ impl AppStateInner { | |
} | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct CompletedTransactionInfo { | ||
pub tx_id: TxId, | ||
pub source_public_key: CommsPublicKey, | ||
pub destination_public_key: CommsPublicKey, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
pub amount: MicroTari, | ||
pub fee: MicroTari, | ||
pub excess_signature: String, | ||
pub maturity: u64, | ||
pub status: TransactionStatus, | ||
pub message: String, | ||
pub timestamp: NaiveDateTime, | ||
pub cancelled: bool, | ||
pub direction: TransactionDirection, | ||
pub valid: bool, | ||
pub mined_height: Option<u64>, | ||
pub is_coinbase: bool, | ||
pub weight: u64, | ||
pub inputs_count: usize, | ||
pub outputs_count: usize, | ||
} | ||
|
||
impl From<CompletedTransaction> for CompletedTransactionInfo { | ||
fn from(completed_transaction: CompletedTransaction) -> Self { | ||
let excess_signature = if completed_transaction.transaction.body.kernels().is_empty() { | ||
"".to_string() | ||
} else { | ||
completed_transaction.transaction.body.kernels()[0] | ||
.excess_sig | ||
.get_signature() | ||
.to_hex() | ||
}; | ||
|
||
Self { | ||
tx_id: completed_transaction.tx_id, | ||
source_public_key: completed_transaction.source_public_key.clone(), | ||
destination_public_key: completed_transaction.destination_public_key.clone(), | ||
amount: completed_transaction.amount, | ||
fee: completed_transaction.fee, | ||
excess_signature, | ||
maturity: completed_transaction | ||
.transaction | ||
.body | ||
.outputs() | ||
.first() | ||
.map(|o| o.features.maturity) | ||
.unwrap_or_else(|| 0), | ||
status: completed_transaction.status.clone(), | ||
message: completed_transaction.message.clone(), | ||
timestamp: completed_transaction.timestamp, | ||
cancelled: completed_transaction.cancelled, | ||
direction: completed_transaction.direction.clone(), | ||
valid: completed_transaction.valid, | ||
mined_height: completed_transaction.mined_height, | ||
is_coinbase: completed_transaction.is_coinbase(), | ||
weight: completed_transaction.transaction.calculate_weight(), | ||
inputs_count: completed_transaction.transaction.body.inputs().len(), | ||
outputs_count: completed_transaction.transaction.body.outputs().len(), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Clone)] | ||
struct AppStateData { | ||
pending_txs: Vec<CompletedTransaction>, | ||
completed_txs: Vec<CompletedTransaction>, | ||
pending_txs: Vec<CompletedTransactionInfo>, | ||
completed_txs: Vec<CompletedTransactionInfo>, | ||
confirmations: HashMap<TxId, u64>, | ||
my_identity: MyIdentity, | ||
contacts: Vec<UiContact>, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can reduce memory usage even more by storing the text representation instead of the crypto primitive