Skip to content
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

[GUI] Don't treat wallets using bitcoind as syncing #1387

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions gui/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ 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,
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 Expand Up @@ -242,7 +247,7 @@ impl App {

pub fn stop(&mut self) {
info!("Close requested");
if self.daemon.backend() == DaemonBackend::EmbeddedLianad {
if self.daemon.backend().is_embedded() {
if let Err(e) = Handle::current().block_on(async { self.daemon.stop().await }) {
error!("{}", e);
} else {
Expand Down
51 changes: 36 additions & 15 deletions gui/src/app/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ use super::{cache::Cache, error::Error, menu::Menu, message::Message, view, wall

pub const HISTORY_EVENT_PAGE_SIZE: u64 = 20;

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

fn wallet_is_syncing(daemon_backend: DaemonBackend, blockheight: i32) -> bool {
match daemon_backend {
// 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.
DaemonBackend::EmbeddedLianad(Some(NodeType::Bitcoind))
| DaemonBackend::EmbeddedLianad(None)
| DaemonBackend::ExternalLianad => false,
_ => blockheight <= 0,
}
}

pub struct Home {
wallet: Arc<Wallet>,
blockheight: i32,
wallet_is_syncing: bool,
balance: Amount,
unconfirmed_balance: Amount,
remaining_sequence: Option<u32>,
Expand All @@ -83,7 +101,12 @@ 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,
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 +120,11 @@ impl Home {
},
);

let wallet_is_syncing = wallet_is_syncing(daemon_backend, blockheight);

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

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

impl State for Home {
Expand Down Expand Up @@ -148,7 +169,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 +247,10 @@ 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(), 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 +331,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
2 changes: 1 addition & 1 deletion gui/src/app/state/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl State for SettingsState {
BitcoindSettingsState::new(
daemon.config().cloned(),
cache,
daemon.backend() != DaemonBackend::EmbeddedLianad,
!daemon.backend().is_embedded(),
self.internal_bitcoind,
)
.into(),
Expand Down
9 changes: 7 additions & 2 deletions gui/src/daemon/embedded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::{HashMap, HashSet};
use std::path::Path;
use tokio::sync::Mutex;

use super::{model::*, Daemon, DaemonBackend, DaemonError};
use super::{model::*, node, Daemon, DaemonBackend, DaemonError};
use async_trait::async_trait;
use liana::{
commands::{CoinStatus, LabelItem},
Expand Down Expand Up @@ -51,7 +51,12 @@ impl std::fmt::Debug for EmbeddedDaemon {
#[async_trait]
impl Daemon for EmbeddedDaemon {
fn backend(&self) -> DaemonBackend {
DaemonBackend::EmbeddedLianad
let node_type = self
.config
.bitcoin_backend
.as_ref()
.map(node::NodeType::from);
DaemonBackend::EmbeddedLianad(node_type)
}

fn config(&self) -> Option<&Config> {
Expand Down
10 changes: 8 additions & 2 deletions gui/src/daemon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use liana::{
StartupError,
};

use crate::hw::HardwareWalletConfig;
use crate::{hw::HardwareWalletConfig, node};

#[derive(Debug)]
pub enum DaemonError {
Expand Down Expand Up @@ -62,11 +62,17 @@ impl std::fmt::Display for DaemonError {

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DaemonBackend {
EmbeddedLianad,
EmbeddedLianad(Option<node::NodeType>),
ExternalLianad,
RemoteBackend,
}

impl DaemonBackend {
pub fn is_embedded(&self) -> bool {
matches!(self, DaemonBackend::EmbeddedLianad(_))
}
}

#[async_trait]
pub trait Daemon: Debug {
fn backend(&self) -> DaemonBackend;
Expand Down
3 changes: 1 addition & 2 deletions gui/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use liana_ui::{
widget::*,
};

use crate::daemon::DaemonBackend;
use crate::{
app::{
cache::Cache,
Expand Down Expand Up @@ -227,7 +226,7 @@ impl Loader {
pub fn stop(&mut self) {
info!("Close requested");
if let Step::Syncing { daemon, .. } = &mut self.step {
if daemon.backend() == DaemonBackend::EmbeddedLianad {
if daemon.backend().is_embedded() {
info!("Stopping internal daemon...");
if let Err(e) = Handle::current().block_on(async { daemon.stop().await }) {
warn!("Internal daemon failed to stop: {}", e);
Expand Down
11 changes: 11 additions & 0 deletions gui/src/node/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use liana::config::BitcoinBackend;

pub mod bitcoind;
pub mod electrum;

Expand All @@ -6,3 +8,12 @@ pub enum NodeType {
Bitcoind,
Electrum,
}

impl From<&BitcoinBackend> for NodeType {
fn from(bitcoin_backend: &BitcoinBackend) -> Self {
match bitcoin_backend {
BitcoinBackend::Bitcoind(_) => Self::Bitcoind,
BitcoinBackend::Electrum(_) => Self::Electrum,
}
}
}
Loading