wallet: sync data from local database after actions and after opening

This commit is contained in:
ardocrat 2024-05-17 23:12:17 +03:00
parent 098d0a9611
commit 960c806c5e
3 changed files with 69 additions and 51 deletions

View file

@ -20,7 +20,7 @@ use grin_core::core::amount_to_hr_string;
use crate::AppConfig; use crate::AppConfig;
use crate::gui::Colors; use crate::gui::Colors;
use crate::gui::icons::{BRIDGE, CHAT_CIRCLE_TEXT, CHECK, CHECK_FAT, COPY, FOLDER_USER, GEAR_FINE, GRAPH, PACKAGE, PATH, POWER, REPEAT, SCAN, USERS_THREE}; use crate::gui::icons::{ARROWS_CLOCKWISE, BRIDGE, CHAT_CIRCLE_TEXT, CHECK, CHECK_FAT, COPY, FOLDER_USER, GEAR_FINE, GRAPH, PACKAGE, PATH, POWER, SCAN, USERS_THREE};
use crate::gui::platform::PlatformCallbacks; use crate::gui::platform::PlatformCallbacks;
use crate::gui::views::{CameraContent, Modal, Root, View}; use crate::gui::views::{CameraContent, Modal, Root, View};
use crate::gui::views::types::{ModalPosition, QrScanResult, TextEditOptions}; use crate::gui::views::types::{ModalPosition, QrScanResult, TextEditOptions};
@ -83,8 +83,9 @@ impl WalletContent {
let data = wallet.get_data(); let data = wallet.get_data();
let data_empty = data.is_none(); let data_empty = data.is_none();
// Show wallet balance panel when data is not empty and current tab is not Settings. // Show wallet balance panel when there is no error and data is not empty.
let show_balance = self.current_tab.get_type() != WalletTabType::Settings && !data_empty; let show_balance = self.current_tab.get_type() != WalletTabType::Settings && !data_empty
&& !wallet.sync_error();
egui::TopBottomPanel::top("wallet_balance") egui::TopBottomPanel::top("wallet_balance")
.frame(egui::Frame { .frame(egui::Frame {
fill: Colors::FILL, fill: Colors::FILL,
@ -561,7 +562,7 @@ impl WalletContent {
let text = t!("wallets.wallet_loading_err", "settings" => GEAR_FINE); let text = t!("wallets.wallet_loading_err", "settings" => GEAR_FINE);
ui.label(RichText::new(text).size(16.0).color(Colors::INACTIVE_TEXT)); ui.label(RichText::new(text).size(16.0).color(Colors::INACTIVE_TEXT));
ui.add_space(8.0); ui.add_space(8.0);
let retry_text = format!("{} {}", REPEAT, t!("retry")); let retry_text = format!("{} {}", ARROWS_CLOCKWISE, t!("retry"));
View::button(ui, retry_text, Colors::GOLD, || { View::button(ui, retry_text, Colors::GOLD, || {
wallet.set_sync_error(false); wallet.set_sync_error(false);
}); });

View file

@ -213,7 +213,7 @@ impl WalletTransactions {
// Sync wallet on refresh. // Sync wallet on refresh.
if refresh_resp.should_refresh() { if refresh_resp.should_refresh() {
wallet.sync(); wallet.sync(true);
self.manual_sync = true; self.manual_sync = true;
} }
} }

View file

@ -419,7 +419,7 @@ impl Wallet {
wallet_close.is_open.store(false, Ordering::Relaxed); wallet_close.is_open.store(false, Ordering::Relaxed);
// Wake up thread to exit. // Wake up thread to exit.
wallet_close.sync(); wallet_close.sync(true);
}); });
} }
@ -446,7 +446,7 @@ impl Wallet {
} }
// Sync wallet data. // Sync wallet data.
self.sync(); self.sync(false);
Ok(()) Ok(())
}) })
} }
@ -480,7 +480,7 @@ impl Wallet {
self.txs_sync_progress.store(0, Ordering::Relaxed); self.txs_sync_progress.store(0, Ordering::Relaxed);
// Sync wallet data. // Sync wallet data.
self.sync(); self.sync(false);
Ok(()) Ok(())
} }
@ -542,12 +542,19 @@ impl Wallet {
r_data.clone() r_data.clone()
} }
/// Sync wallet data. /// Sync wallet data from node or locally.
pub fn sync(&self) { pub fn sync(&self, from_node: bool) {
if from_node {
let thread_r = self.sync_thread.read(); let thread_r = self.sync_thread.read();
if let Some(thread) = thread_r.as_ref() { if let Some(thread) = thread_r.as_ref() {
thread.unpark(); thread.unpark();
} }
} else {
let wallet = self.clone();
thread::spawn(move || {
sync_wallet_data(&wallet, false);
});
}
} }
/// Check if wallet is syncing. /// Check if wallet is syncing.
@ -677,7 +684,7 @@ impl Wallet {
let message_resp = self.create_slatepack_message(&slate)?; let message_resp = self.create_slatepack_message(&slate)?;
// Sync wallet info. // Sync wallet info.
self.sync(); self.sync(false);
Ok((slate, message_resp)) Ok((slate, message_resp))
} }
@ -718,7 +725,7 @@ impl Wallet {
*w_data = Some(data); *w_data = Some(data);
} }
// Refresh wallet info to update statuses. // Refresh wallet info to update statuses.
self.sync(); self.sync(false);
}; };
// Initialize parameters. // Initialize parameters.
@ -801,7 +808,7 @@ impl Wallet {
let response = self.create_slatepack_message(&slate.clone())?; let response = self.create_slatepack_message(&slate.clone())?;
// Sync wallet info. // Sync wallet info.
self.sync(); self.sync(false);
Ok((slate, response)) Ok((slate, response))
} }
@ -825,7 +832,7 @@ impl Wallet {
let response = self.create_slatepack_message(&slate)?; let response = self.create_slatepack_message(&slate)?;
// Sync wallet info. // Sync wallet info.
self.sync(); self.sync(false);
Ok(response) Ok(response)
} }
@ -842,7 +849,7 @@ impl Wallet {
let response = self.create_slatepack_message(&slate)?; let response = self.create_slatepack_message(&slate)?;
// Sync wallet info. // Sync wallet info.
self.sync(); self.sync(false);
Ok(response) Ok(response)
} }
@ -856,8 +863,6 @@ impl Wallet {
let _ = self.create_slatepack_message(&slate)?; let _ = self.create_slatepack_message(&slate)?;
// Post transaction to blockchain. // Post transaction to blockchain.
let _ = self.post(&slate, dandelion); let _ = self.post(&slate, dandelion);
// Sync wallet info.
self.sync();
Ok(slate) Ok(slate)
} }
@ -885,8 +890,8 @@ impl Wallet {
} }
*w_data = Some(data); *w_data = Some(data);
} }
// Sync wallet info. // Sync local wallet info.
self.sync(); self.sync(false);
Ok(()) Ok(())
} }
@ -912,6 +917,7 @@ impl Wallet {
let instance = wallet.instance.clone().unwrap(); let instance = wallet.instance.clone().unwrap();
cancel_tx(instance, None, &None, Some(id), None).unwrap(); cancel_tx(instance, None, &None, Some(id), None).unwrap();
// Setup posting flag, and ability to finalize. // Setup posting flag, and ability to finalize.
{
let mut w_data = wallet.data.write(); let mut w_data = wallet.data.write();
let mut data = w_data.clone().unwrap(); let mut data = w_data.clone().unwrap();
let txs = data.txs.iter_mut().map(|tx| { let txs = data.txs.iter_mut().map(|tx| {
@ -929,8 +935,9 @@ impl Wallet {
}).collect::<Vec<WalletTransaction>>(); }).collect::<Vec<WalletTransaction>>();
data.txs = txs; data.txs = txs;
*w_data = Some(data); *w_data = Some(data);
}
// Refresh wallet info to update statuses. // Refresh wallet info to update statuses.
wallet.sync(); sync_wallet_data(&wallet, false);
}); });
} }
@ -945,7 +952,7 @@ impl Wallet {
/// Initiate wallet repair by scanning its outputs. /// Initiate wallet repair by scanning its outputs.
pub fn repair(&self) { pub fn repair(&self) {
self.repair_needed.store(true, Ordering::Relaxed); self.repair_needed.store(true, Ordering::Relaxed);
self.sync(); self.sync(true);
} }
/// Check if wallet is repairing. /// Check if wallet is repairing.
@ -989,7 +996,7 @@ impl Wallet {
wallet_delete.deleted.store(true, Ordering::Relaxed); wallet_delete.deleted.store(true, Ordering::Relaxed);
// Start sync to exit. // Start sync to exit.
wallet_delete.sync(); wallet_delete.sync(true);
}); });
} }
@ -1066,7 +1073,11 @@ fn start_sync(mut wallet: Wallet) -> Thread {
if wallet.is_repairing() { if wallet.is_repairing() {
repair_wallet(&wallet) repair_wallet(&wallet)
} else { } else {
sync_wallet_data(&wallet); // Retrieve data from database first on empty data.
if wallet.get_data().is_none() {
sync_wallet_data(&wallet, false);
}
sync_wallet_data(&wallet, true);
} }
} }
@ -1120,8 +1131,8 @@ fn start_sync(mut wallet: Wallet) -> Thread {
}).thread().clone() }).thread().clone()
} }
/// Retrieve [`WalletData`] from node. /// Retrieve [`WalletData`] from local base or node.
fn sync_wallet_data(wallet: &Wallet) { fn sync_wallet_data(wallet: &Wallet, from_node: bool) {
let wallet_info = wallet.clone(); let wallet_info = wallet.clone();
let (info_tx, info_rx) = mpsc::channel::<StatusMessage>(); let (info_tx, info_rx) = mpsc::channel::<StatusMessage>();
// Update info sync progress at separate thread. // Update info sync progress at separate thread.
@ -1150,7 +1161,7 @@ fn sync_wallet_data(wallet: &Wallet) {
instance.clone(), instance.clone(),
None, None,
&Some(info_tx), &Some(info_tx),
true, from_node,
config.min_confirmations config.min_confirmations
) { ) {
// Do not retrieve txs if wallet was closed. // Do not retrieve txs if wallet was closed.
@ -1158,7 +1169,7 @@ fn sync_wallet_data(wallet: &Wallet) {
return; return;
} }
if wallet.info_sync_progress() == 100 { if wallet.info_sync_progress() == 100 || !from_node {
// Retrieve accounts data. // Retrieve accounts data.
let last_height = info.1.last_confirmed_height; let last_height = info.1.last_confirmed_height;
let spendable = if wallet.get_data().is_none() { let spendable = if wallet.get_data().is_none() {
@ -1197,7 +1208,7 @@ fn sync_wallet_data(wallet: &Wallet) {
if let Ok(txs) = retrieve_txs(instance.clone(), if let Ok(txs) = retrieve_txs(instance.clone(),
None, None,
&Some(txs_tx), &Some(txs_tx),
true, from_node,
None, None,
None, None,
Some(txs_args)) { Some(txs_args)) {
@ -1206,7 +1217,7 @@ fn sync_wallet_data(wallet: &Wallet) {
return; return;
} }
// Save data if loading was completed. // Save data if loading was completed.
if wallet.txs_sync_progress() == 100 { if wallet.txs_sync_progress() == 100 || !from_node {
// Reset attempts. // Reset attempts.
wallet.reset_sync_attempts(); wallet.reset_sync_attempts();
@ -1279,13 +1290,19 @@ fn sync_wallet_data(wallet: &Wallet) {
false false
}; };
// Setup reposting height. // Setup reposting height and cancelling status.
let mut repost_height = None; let mut repost_height = None;
let mut cancelling = false;
if posting { if posting {
if let Some(data) = wallet.get_data() { if let Some(data) = wallet.get_data() {
for t in data.txs { for t in data.txs {
if t.data.id == tx.id { if t.data.id == tx.id {
repost_height = t.repost_height; repost_height = t.repost_height;
if t.cancelling &&
tx.tx_type != TxLogEntryType::TxReceivedCancelled &&
tx.tx_type != TxLogEntryType::TxSentCancelled {
cancelling = true;
}
break; break;
} }
} }
@ -1296,7 +1313,7 @@ fn sync_wallet_data(wallet: &Wallet) {
new_txs.push(WalletTransaction { new_txs.push(WalletTransaction {
data: tx.clone(), data: tx.clone(),
amount, amount,
cancelling: false, cancelling,
posting, posting,
can_finalize, can_finalize,
repost_height, repost_height,