Skip to content

Commit

Permalink
gui(settings): allow to change node type
Browse files Browse the repository at this point in the history
  • Loading branch information
jp1ac4 committed Sep 5, 2024
1 parent 5c79a44 commit 8f5225c
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 29 deletions.
58 changes: 44 additions & 14 deletions gui/src/app/state/settings/bitcoind.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)]
Expand All @@ -40,17 +43,33 @@ 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 {
warning: None,
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")
Expand All @@ -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,
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -208,6 +222,7 @@ impl From<BitcoindSettingsState> for Box<dyn State> {

#[derive(Debug)]
pub struct BitcoindSettings {
configured_node_type: Option<NodeType>,
bitcoind_config: BitcoindConfig,
bitcoin_config: BitcoinConfig,
edit: bool,
Expand All @@ -221,6 +236,7 @@ pub struct BitcoindSettings {

impl BitcoindSettings {
fn new(
configured_node_type: Option<NodeType>,
bitcoin_config: BitcoinConfig,
bitcoind_config: BitcoindConfig,
daemon_is_external: bool,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -360,6 +383,7 @@ impl BitcoindSettings {
)
} else {
view::settings::bitcoind(
is_configured_node_type,
self.bitcoin_config.network,
&self.bitcoind_config,
cache.blockheight,
Expand All @@ -372,6 +396,7 @@ impl BitcoindSettings {

#[derive(Debug)]
pub struct ElectrumSettings {
configured_node_type: Option<NodeType>,
electrum_config: ElectrumConfig,
bitcoin_config: BitcoinConfig,
edit: bool,
Expand All @@ -382,12 +407,14 @@ pub struct ElectrumSettings {

impl ElectrumSettings {
fn new(
configured_node_type: Option<NodeType>,
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,
Expand Down Expand Up @@ -450,15 +477,18 @@ 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,
self.processing,
)
} else {
view::settings::electrum(
is_configured_node_type,
self.bitcoin_config.network,
&self.electrum_config,
cache.blockheight,
Expand Down
48 changes: 33 additions & 15 deletions gui/src/app/view/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
Expand All @@ -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()
Expand Down Expand Up @@ -444,14 +445,15 @@ pub fn bitcoind_edit<'a>(
}

pub fn bitcoind<'a>(
is_configured_node_type: bool,
network: Network,
config: &liana::config::BitcoindConfig,
blockheight: i32,
is_running: Option<bool>,
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()
Expand Down Expand Up @@ -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 {
Expand All @@ -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),
Expand All @@ -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<String>,
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()
Expand Down Expand Up @@ -621,14 +630,15 @@ pub fn electrum_edit<'a>(
}

pub fn electrum<'a>(
is_configured_node_type: bool,
network: Network,
config: &liana::config::ElectrumConfig,
blockheight: i32,
is_running: Option<bool>,
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()
Expand Down Expand Up @@ -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 {
Expand All @@ -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),
Expand Down

0 comments on commit 8f5225c

Please sign in to comment.