wallet: sync status
This commit is contained in:
parent
8b579c088f
commit
8ff6fd1c20
1 changed files with 31 additions and 15 deletions
|
@ -91,6 +91,8 @@ pub struct Wallet {
|
||||||
data: Arc<RwLock<Option<WalletData>>>,
|
data: Arc<RwLock<Option<WalletData>>>,
|
||||||
/// Attempts amount to update wallet data.
|
/// Attempts amount to update wallet data.
|
||||||
sync_attempts: Arc<AtomicU8>,
|
sync_attempts: Arc<AtomicU8>,
|
||||||
|
/// Flag to check if wallet is syncing.
|
||||||
|
syncing: Arc<AtomicBool>,
|
||||||
|
|
||||||
/// Flag to check if wallet repairing and restoring missing outputs is needed.
|
/// Flag to check if wallet repairing and restoring missing outputs is needed.
|
||||||
repair_needed: Arc<AtomicBool>,
|
repair_needed: Arc<AtomicBool>,
|
||||||
|
@ -118,6 +120,7 @@ impl Wallet {
|
||||||
accounts: Arc::new(RwLock::new(vec![])),
|
accounts: Arc::new(RwLock::new(vec![])),
|
||||||
data: Arc::new(RwLock::new(None)),
|
data: Arc::new(RwLock::new(None)),
|
||||||
sync_attempts: Arc::new(AtomicU8::new(0)),
|
sync_attempts: Arc::new(AtomicU8::new(0)),
|
||||||
|
syncing: Arc::new(AtomicBool::new(false)),
|
||||||
repair_needed: Arc::new(AtomicBool::new(false)),
|
repair_needed: Arc::new(AtomicBool::new(false)),
|
||||||
repair_progress: Arc::new(AtomicU8::new(0))
|
repair_progress: Arc::new(AtomicU8::new(0))
|
||||||
}
|
}
|
||||||
|
@ -539,7 +542,7 @@ impl Wallet {
|
||||||
r_data.clone()
|
r_data.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wake up wallet thread to sync wallet data and update statuses.
|
/// Sync wallet data.
|
||||||
pub fn sync(&self) {
|
pub fn sync(&self) {
|
||||||
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() {
|
||||||
|
@ -547,6 +550,11 @@ impl Wallet {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Check if wallet is syncing.
|
||||||
|
pub fn syncing(&self) -> bool {
|
||||||
|
self.syncing.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
|
||||||
/// Get running Foreign API server port.
|
/// Get running Foreign API server port.
|
||||||
pub fn foreign_api_port(&self) -> Option<u16> {
|
pub fn foreign_api_port(&self) -> Option<u16> {
|
||||||
let r_api = self.foreign_api_server.read();
|
let r_api = self.foreign_api_server.read();
|
||||||
|
@ -1007,7 +1015,24 @@ fn start_sync(mut wallet: Wallet) -> Thread {
|
||||||
wallet.txs_sync_progress.store(0, Ordering::Relaxed);
|
wallet.txs_sync_progress.store(0, Ordering::Relaxed);
|
||||||
wallet.repair_progress.store(0, Ordering::Relaxed);
|
wallet.repair_progress.store(0, Ordering::Relaxed);
|
||||||
|
|
||||||
|
// To call on sync thread stop.
|
||||||
|
let on_thread_stop = |wallet: Wallet| {
|
||||||
|
// Clear thread instance.
|
||||||
|
let mut thread_w = wallet.sync_thread.write();
|
||||||
|
*thread_w = None;
|
||||||
|
|
||||||
|
// Clear wallet info.
|
||||||
|
let mut w_data = wallet.data.write();
|
||||||
|
*w_data = None;
|
||||||
|
|
||||||
|
// Clear syncing status.
|
||||||
|
wallet.syncing.store(false, Ordering::Relaxed);
|
||||||
|
};
|
||||||
|
|
||||||
thread::spawn(move || loop {
|
thread::spawn(move || loop {
|
||||||
|
// Set syncing status.
|
||||||
|
wallet.syncing.store(true, Ordering::Relaxed);
|
||||||
|
|
||||||
// Close wallet on chain type change.
|
// Close wallet on chain type change.
|
||||||
if wallet.get_config().chain_type != AppConfig::chain_type() {
|
if wallet.get_config().chain_type != AppConfig::chain_type() {
|
||||||
wallet.close();
|
wallet.close();
|
||||||
|
@ -1015,13 +1040,7 @@ fn start_sync(mut wallet: Wallet) -> Thread {
|
||||||
|
|
||||||
// Stop syncing if wallet was closed.
|
// Stop syncing if wallet was closed.
|
||||||
if !wallet.is_open() {
|
if !wallet.is_open() {
|
||||||
// Clear thread instance.
|
on_thread_stop(wallet);
|
||||||
let mut thread_w = wallet.sync_thread.write();
|
|
||||||
*thread_w = None;
|
|
||||||
|
|
||||||
// Clear wallet info.
|
|
||||||
let mut w_data = wallet.data.write();
|
|
||||||
*w_data = None;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1053,13 +1072,7 @@ fn start_sync(mut wallet: Wallet) -> Thread {
|
||||||
|
|
||||||
// Stop sync if wallet was closed.
|
// Stop sync if wallet was closed.
|
||||||
if !wallet.is_open() {
|
if !wallet.is_open() {
|
||||||
// Clear thread instance.
|
on_thread_stop(wallet);
|
||||||
let mut thread_w = wallet.sync_thread.write();
|
|
||||||
*thread_w = None;
|
|
||||||
|
|
||||||
// Clear wallet info.
|
|
||||||
let mut w_data = wallet.data.write();
|
|
||||||
*w_data = None;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1094,6 +1107,9 @@ fn start_sync(mut wallet: Wallet) -> Thread {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clear syncing status.
|
||||||
|
wallet.syncing.store(false, Ordering::Relaxed);
|
||||||
|
|
||||||
// Repeat after default or attempt delay if synchronization was not successful.
|
// Repeat after default or attempt delay if synchronization was not successful.
|
||||||
let delay = if failed_sync {
|
let delay = if failed_sync {
|
||||||
ATTEMPT_DELAY
|
ATTEMPT_DELAY
|
||||||
|
|
Loading…
Reference in a new issue