From 8f5225c7b9473584b2ff90830ae4716b3a2a7815 Mon Sep 17 00:00:00 2001 From: Michael Mallan Date: Thu, 5 Sep 2024 07:58:22 +0100 Subject: [PATCH] gui(settings): allow to change node type --- gui/src/app/state/settings/bitcoind.rs | 58 +++++++++++++++++++------- gui/src/app/view/settings.rs | 48 ++++++++++++++------- 2 files changed, 77 insertions(+), 29 deletions(-) diff --git a/gui/src/app/state/settings/bitcoind.rs b/gui/src/app/state/settings/bitcoind.rs index b2f02c9c2..0d2d88693 100644 --- a/gui/src/app/state/settings/bitcoind.rs +++ b/gui/src/app/state/settings/bitcoind.rs @@ -1,5 +1,5 @@ use std::convert::{From, TryInto}; -use std::net::SocketAddr; +use std::net::{SocketAddr, SocketAddrV4}; use std::path::PathBuf; use std::str::FromStr; use std::sync::Arc; @@ -20,7 +20,10 @@ use liana_ui::{component::form, widget::Element}; use crate::{ app::{cache::Cache, error::Error, message::Message, state::settings::State, view}, daemon::Daemon, - node::bitcoind::{RpcAuthType, RpcAuthValues}, + node::{ + bitcoind::{RpcAuthType, RpcAuthValues}, + NodeType, + }, }; #[derive(Debug)] @@ -40,10 +43,25 @@ impl BitcoindSettingsState { daemon_is_external: bool, bitcoind_is_internal: bool, ) -> Self { + let mut configured_node_type = None; let (bitcoind_config, electrum_config) = match config.clone().and_then(|c| c.bitcoin_backend) { - Some(BitcoinBackend::Bitcoind(bitcoind_config)) => (Some(bitcoind_config), None), - Some(BitcoinBackend::Electrum(electrum_config)) => (None, Some(electrum_config)), + Some(BitcoinBackend::Bitcoind(bitcoind_config)) => { + configured_node_type = Some(NodeType::Bitcoind); + let dummy_electrum = ElectrumConfig { + addr: String::default(), + }; + (Some(bitcoind_config), Some(dummy_electrum)) + } + Some(BitcoinBackend::Electrum(electrum_config)) => { + configured_node_type = Some(NodeType::Electrum); + // The dummy values will be ignored. + let dummy_bitcoind = BitcoindConfig { + addr: SocketAddr::V4(SocketAddrV4::from_str("127.0.0.1:10000").unwrap()), + rpc_auth: BitcoindRpcAuth::CookieFile(PathBuf::from_str("").unwrap()), + }; + (Some(dummy_bitcoind), Some(electrum_config)) + } _ => (None, None), }; BitcoindSettingsState { @@ -51,6 +69,7 @@ impl BitcoindSettingsState { config_updated: false, node_settings: bitcoind_config.map(|bitcoind_config| { BitcoindSettings::new( + configured_node_type, config .clone() .expect("config must exist if bitcoind_config exists") @@ -62,6 +81,7 @@ impl BitcoindSettingsState { }), electrum_settings: electrum_config.map(|electrum_config| { ElectrumSettings::new( + configured_node_type, config .expect("config must exist if electrum_config exists") .bitcoin_config, @@ -169,15 +189,9 @@ impl State for BitcoindSettingsState { .map(move |msg| { view::Message::Settings(view::SettingsMessage::BitcoindSettings(msg)) }), - self.rescan_settings - .view(cache, can_do_rescan) - .map(move |msg| { - view::Message::Settings(view::SettingsMessage::RescanSettings(msg)) - }), - ] - } else if let Some(settings) = &self.electrum_settings { - vec![ - settings + self.electrum_settings + .as_ref() + .expect("If we have bitcoind, we must also have electrum") .view(cache, can_edit_electrum_settings) .map(move |msg| { view::Message::Settings(view::SettingsMessage::ElectrumSettings(msg)) @@ -208,6 +222,7 @@ impl From for Box { #[derive(Debug)] pub struct BitcoindSettings { + configured_node_type: Option, bitcoind_config: BitcoindConfig, bitcoin_config: BitcoinConfig, edit: bool, @@ -221,6 +236,7 @@ pub struct BitcoindSettings { impl BitcoindSettings { fn new( + configured_node_type: Option, bitcoin_config: BitcoinConfig, bitcoind_config: BitcoindConfig, daemon_is_external: bool, @@ -253,8 +269,13 @@ impl BitcoindSettings { RpcAuthType::UserPass, ), }; - let addr = bitcoind_config.addr.to_string(); + let addr = if configured_node_type == Some(NodeType::Bitcoind) { + bitcoind_config.addr.to_string() + } else { + String::default() + }; BitcoindSettings { + configured_node_type, daemon_is_external, bitcoind_is_internal, bitcoind_config, @@ -349,8 +370,10 @@ impl BitcoindSettings { } fn view<'a>(&self, cache: &'a Cache, can_edit: bool) -> Element<'a, view::SettingsEditMessage> { + let is_configured_node_type = self.configured_node_type == Some(NodeType::Bitcoind); if self.edit { view::settings::bitcoind_edit( + is_configured_node_type, self.bitcoin_config.network, cache.blockheight, &self.addr, @@ -360,6 +383,7 @@ impl BitcoindSettings { ) } else { view::settings::bitcoind( + is_configured_node_type, self.bitcoin_config.network, &self.bitcoind_config, cache.blockheight, @@ -372,6 +396,7 @@ impl BitcoindSettings { #[derive(Debug)] pub struct ElectrumSettings { + configured_node_type: Option, electrum_config: ElectrumConfig, bitcoin_config: BitcoinConfig, edit: bool, @@ -382,12 +407,14 @@ pub struct ElectrumSettings { impl ElectrumSettings { fn new( + configured_node_type: Option, bitcoin_config: BitcoinConfig, electrum_config: ElectrumConfig, daemon_is_external: bool, ) -> ElectrumSettings { let addr = electrum_config.addr.to_string(); ElectrumSettings { + configured_node_type, daemon_is_external, electrum_config, bitcoin_config, @@ -450,8 +477,10 @@ impl ElectrumSettings { } fn view<'a>(&self, cache: &'a Cache, can_edit: bool) -> Element<'a, view::SettingsEditMessage> { + let is_configured_node_type = self.configured_node_type == Some(NodeType::Electrum); if self.edit { view::settings::electrum_edit( + is_configured_node_type, self.bitcoin_config.network, cache.blockheight, &self.addr, @@ -459,6 +488,7 @@ impl ElectrumSettings { ) } else { view::settings::electrum( + is_configured_node_type, self.bitcoin_config.network, &self.electrum_config, cache.blockheight, diff --git a/gui/src/app/view/settings.rs b/gui/src/app/view/settings.rs index b6b310b55..95782c51a 100644 --- a/gui/src/app/view/settings.rs +++ b/gui/src/app/view/settings.rs @@ -298,6 +298,7 @@ pub fn remote_backend_section<'a>( } pub fn bitcoind_edit<'a>( + is_configured_node_type: bool, network: Network, blockheight: i32, addr: &form::Value, @@ -306,7 +307,7 @@ pub fn bitcoind_edit<'a>( processing: bool, ) -> Element<'a, SettingsEditMessage> { let mut col = Column::new().spacing(20); - if blockheight != 0 { + if is_configured_node_type && blockheight != 0 { col = col .push( Row::new() @@ -444,6 +445,7 @@ pub fn bitcoind_edit<'a>( } pub fn bitcoind<'a>( + is_configured_node_type: bool, network: Network, config: &liana::config::BitcoindConfig, blockheight: i32, @@ -451,7 +453,7 @@ pub fn bitcoind<'a>( can_edit: bool, ) -> Element<'a, SettingsEditMessage> { let mut col = Column::new().spacing(20); - if blockheight != 0 { + if is_configured_node_type && blockheight != 0 { col = col .push( Row::new() @@ -482,16 +484,18 @@ pub fn bitcoind<'a>( } let mut rows = vec![]; - match &config.rpc_auth { - BitcoindRpcAuth::CookieFile(path) => { - rows.push(("Cookie file path:", path.to_str().unwrap().to_string())); - } - BitcoindRpcAuth::UserPass(user, password) => { - rows.push(("User:", user.clone())); - rows.push(("Password:", password.clone())); + if is_configured_node_type { + match &config.rpc_auth { + BitcoindRpcAuth::CookieFile(path) => { + rows.push(("Cookie file path:", path.to_str().unwrap().to_string())); + } + BitcoindRpcAuth::UserPass(user, password) => { + rows.push(("User:", user.clone())); + rows.push(("Password:", password.clone())); + } } + rows.push(("Socket address:", config.addr.to_string())); } - rows.push(("Socket address:", config.addr.to_string())); let mut col_fields = Column::new(); for (k, v) in rows { @@ -510,7 +514,11 @@ pub fn bitcoind<'a>( Row::new() .push(badge::Badge::new(icon::bitcoin_icon())) .push(text("Bitcoin Core").bold()) - .push(is_running_label(is_running)) + .push_maybe(if is_configured_node_type { + Some(is_running_label(is_running)) + } else { + None + }) .spacing(20) .align_items(Alignment::Center) .width(Length::Fill), @@ -533,13 +541,14 @@ pub fn bitcoind<'a>( } pub fn electrum_edit<'a>( + is_configured_node_type: bool, network: Network, blockheight: i32, addr: &form::Value, processing: bool, ) -> Element<'a, SettingsEditMessage> { let mut col = Column::new().spacing(20); - if blockheight != 0 { + if is_configured_node_type && blockheight != 0 { col = col .push( Row::new() @@ -621,6 +630,7 @@ pub fn electrum_edit<'a>( } pub fn electrum<'a>( + is_configured_node_type: bool, network: Network, config: &liana::config::ElectrumConfig, blockheight: i32, @@ -628,7 +638,7 @@ pub fn electrum<'a>( can_edit: bool, ) -> Element<'a, SettingsEditMessage> { let mut col = Column::new().spacing(20); - if blockheight != 0 { + if is_configured_node_type && blockheight != 0 { col = col .push( Row::new() @@ -658,7 +668,11 @@ pub fn electrum<'a>( .push(separation().width(Length::Fill)); } - let rows = vec![("Address:", config.addr.to_string())]; + let rows = if is_configured_node_type { + vec![("Address:", config.addr.to_string())] + } else { + vec![] + }; let mut col_fields = Column::new(); for (k, v) in rows { @@ -677,7 +691,11 @@ pub fn electrum<'a>( Row::new() .push(badge::Badge::new(icon::bitcoin_icon())) .push(text("Electrum").bold()) - .push(is_running_label(is_running)) + .push_maybe(if is_configured_node_type { + Some(is_running_label(is_running)) + } else { + None + }) .spacing(20) .align_items(Alignment::Center) .width(Length::Fill),