From e419784e9f2da76e8e86c03c8773f0dcb2a883da Mon Sep 17 00:00:00 2001 From: Michael Mallan Date: Fri, 1 Nov 2024 09:08:46 +0000 Subject: [PATCH] gui: move sync status function to wallet module This will make it easier to reuse elsewhere. --- gui/src/app/state/mod.rs | 50 ++++----------------------------------- gui/src/app/wallet.rs | 51 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 47 deletions(-) diff --git a/gui/src/app/state/mod.rs b/gui/src/app/state/mod.rs index 00b1489e..6694b248 100644 --- a/gui/src/app/state/mod.rs +++ b/gui/src/app/state/mod.rs @@ -25,17 +25,14 @@ use super::{ menu::Menu, message::Message, view, - wallet::{SyncStatus, Wallet}, + wallet::{sync_status, SyncStatus, Wallet}, }; pub const HISTORY_EVENT_PAGE_SIZE: u64 = 20; -use crate::{ - daemon::{ - model::{remaining_sequence, Coin, HistoryTransaction, Labelled}, - Daemon, DaemonBackend, - }, - node::NodeType, +use crate::daemon::{ + model::{remaining_sequence, Coin, HistoryTransaction, Labelled}, + Daemon, DaemonBackend, }; pub use coins::CoinsPanel; use label::LabelsEdited; @@ -76,45 +73,6 @@ pub fn redirect(menu: Menu) -> Command { }) } -fn sync_status( - daemon_backend: DaemonBackend, - blockheight: i32, - sync_progress: f64, - last_poll: Option, - last_poll_at_startup: Option, -) -> SyncStatus { - if sync_progress < 1.0 { - return SyncStatus::BlockchainSync(sync_progress); - } else if blockheight <= 0 { - // If blockheight <= 0, then this is a newly created wallet. - // If user imported descriptor and is using a local bitcoind, a rescan - // will need to be performed in order to see past transactions and so the - // syncing status could be misleading as it could suggest the rescan is - // being performed. - // For external daemon or if we otherwise don't know the node type, - // treat it the same as bitcoind to be sure we don't mislead the user. - if daemon_backend == DaemonBackend::RemoteBackend - || daemon_backend == DaemonBackend::EmbeddedLianad(Some(NodeType::Electrum)) - { - return SyncStatus::WalletFullScan; - } - } - // For an existing wallet with any local node type, if the first poll has - // not completed, then the wallet has not yet caught up with the tip. - // An existing wallet with remote backend remains synced so we can ignore it. - // If external daemon, we cannot be sure it will return last poll as it - // depends on the version, so assume it won't unless the last poll at - // startup is set. - // TODO: should we check the daemon version at GUI startup? - else if last_poll <= last_poll_at_startup - && (daemon_backend.is_embedded() - || (daemon_backend == DaemonBackend::ExternalLianad && last_poll_at_startup.is_some())) - { - return SyncStatus::LatestWalletSync; - } - SyncStatus::Synced -} - pub struct Home { wallet: Arc, sync_status: SyncStatus, diff --git a/gui/src/app/wallet.rs b/gui/src/app/wallet.rs index 506e3984..d694cb5d 100644 --- a/gui/src/app/wallet.rs +++ b/gui/src/app/wallet.rs @@ -2,7 +2,9 @@ use std::collections::{HashMap, HashSet}; use std::path::Path; use std::sync::Arc; -use crate::{app::settings, hw::HardwareWalletConfig, signer::Signer}; +use crate::{ + app::settings, daemon::DaemonBackend, hw::HardwareWalletConfig, node::NodeType, signer::Signer, +}; use liana::{miniscript::bitcoin, signer::HotSigner}; @@ -206,3 +208,50 @@ impl SyncStatus { self == &SyncStatus::Synced } } + +/// Get the [`SyncStatus`]. +/// +/// The `last_poll_at_startup` is the timestamp of the last poll +/// of the blockchain when the application was first loaded, while +/// `last_poll` refers to the most recent poll. +/// +/// `sync_progress` is the blockchain synchronization progress as +/// a number between `0.0` and `1.0`. +pub fn sync_status( + daemon_backend: DaemonBackend, + blockheight: i32, + sync_progress: f64, + last_poll: Option, + last_poll_at_startup: Option, +) -> SyncStatus { + if sync_progress < 1.0 { + return SyncStatus::BlockchainSync(sync_progress); + } else if blockheight <= 0 { + // If blockheight <= 0, then this is a newly created wallet. + // If user imported descriptor and is using a local bitcoind, a rescan + // will need to be performed in order to see past transactions and so the + // syncing status could be misleading as it could suggest the rescan is + // being performed. + // For external daemon or if we otherwise don't know the node type, + // treat it the same as bitcoind to be sure we don't mislead the user. + if daemon_backend == DaemonBackend::RemoteBackend + || daemon_backend == DaemonBackend::EmbeddedLianad(Some(NodeType::Electrum)) + { + return SyncStatus::WalletFullScan; + } + } + // For an existing wallet with any local node type, if the first poll has + // not completed, then the wallet has not yet caught up with the tip. + // An existing wallet with remote backend remains synced so we can ignore it. + // If external daemon, we cannot be sure it will return last poll as it + // depends on the version, so assume it won't unless the last poll at + // startup is set. + // TODO: should we check the daemon version at GUI startup? + else if last_poll <= last_poll_at_startup + && (daemon_backend.is_embedded() + || (daemon_backend == DaemonBackend::ExternalLianad && last_poll_at_startup.is_some())) + { + return SyncStatus::LatestWalletSync; + } + SyncStatus::Synced +}