Skip to content

Commit

Permalink
gui(home): consider wallet syncing until first poll
Browse files Browse the repository at this point in the history
This only applies to local backends.
  • Loading branch information
jp1ac4 committed Oct 15, 2024
1 parent 609d999 commit 97c68a2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
8 changes: 7 additions & 1 deletion gui/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ impl Panels {
) -> Panels {
Self {
current: Menu::Home,
home: Home::new(wallet.clone(), &cache.coins, cache.blockheight),
home: Home::new(
wallet.clone(),
&cache.coins,
cache.blockheight,
cache.last_poll_timestamp,
daemon_backend.clone(),
),
coins: CoinsPanel::new(&cache.coins, wallet.main_descriptor.first_timelock_value()),
transactions: TransactionsPanel::new(wallet.clone()),
psbts: PsbtsPanel::new(wallet.clone()),
Expand Down
50 changes: 37 additions & 13 deletions gui/src/app/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub const HISTORY_EVENT_PAGE_SIZE: u64 = 20;

use crate::daemon::{
model::{remaining_sequence, Coin, HistoryTransaction, Labelled},
Daemon,
Daemon, DaemonBackend,
};
pub use coins::CoinsPanel;
use label::LabelsEdited;
Expand Down Expand Up @@ -66,9 +66,22 @@ pub fn redirect(menu: Menu) -> Command<Message> {
})
}

fn wallet_is_syncing(
backend: DaemonBackend,
last_poll_at_startup: Option<u64>,
last_poll: Option<u64>,
blockheight: i32,
) -> bool {
match backend {
DaemonBackend::RemoteBackend => blockheight <= 0,
_ => last_poll <= last_poll_at_startup,
}
}

pub struct Home {
wallet: Arc<Wallet>,
blockheight: i32,
wallet_is_syncing: bool,
last_poll_at_startup: Option<u64>,
balance: Amount,
unconfirmed_balance: Amount,
remaining_sequence: Option<u32>,
Expand All @@ -83,7 +96,13 @@ pub struct Home {
}

impl Home {
pub fn new(wallet: Arc<Wallet>, coins: &[Coin], blockheight: i32) -> Self {
pub fn new(
wallet: Arc<Wallet>,
coins: &[Coin],
blockheight: i32,
last_poll: Option<u64>,
daemon_backend: DaemonBackend,
) -> Self {
let (balance, unconfirmed_balance) = coins.iter().fold(
(Amount::from_sat(0), Amount::from_sat(0)),
|(balance, unconfirmed_balance), coin| {
Expand All @@ -97,9 +116,13 @@ impl Home {
},
);

let wallet_is_syncing =
wallet_is_syncing(daemon_backend, last_poll, last_poll, blockheight);

Self {
wallet,
blockheight,
wallet_is_syncing,
last_poll_at_startup: last_poll,
balance,
unconfirmed_balance,
remaining_sequence: None,
Expand All @@ -113,10 +136,6 @@ impl Home {
processing: false,
}
}

fn wallet_is_syncing(&self) -> bool {
self.blockheight <= 0
}
}

impl State for Home {
Expand Down Expand Up @@ -148,7 +167,7 @@ impl State for Home {
&self.events,
self.is_last_page,
self.processing,
self.wallet_is_syncing(),
self.wallet_is_syncing,
),
)
}
Expand Down Expand Up @@ -226,10 +245,15 @@ impl State for Home {
}
},
Message::UpdatePanelCache(is_current, Ok(cache)) => {
let wallet_was_syncing = self.wallet_is_syncing();
self.blockheight = cache.blockheight;
let wallet_was_syncing = self.wallet_is_syncing;
self.wallet_is_syncing = wallet_is_syncing(
daemon.backend(),
self.last_poll_at_startup,
cache.last_poll_timestamp,
cache.blockheight,
);
// If this is the current panel, reload it if wallet is no longer syncing.
if is_current && wallet_was_syncing && !self.wallet_is_syncing() {
if is_current && wallet_was_syncing && !self.wallet_is_syncing {
return self.reload(daemon, self.wallet.clone());
}
}
Expand Down Expand Up @@ -310,7 +334,7 @@ impl State for Home {
wallet: Arc<Wallet>,
) -> Command<Message> {
// Wait for wallet to finish syncing before reloading data.
if self.wallet_is_syncing() {
if self.wallet_is_syncing {
return Command::none();
}
self.selected_event = None;
Expand Down

0 comments on commit 97c68a2

Please sign in to comment.