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

feat: load app with only testnet config #16

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
34 changes: 19 additions & 15 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::context::AppContext;
use crate::database::Database;
use crate::logging::initialize_logger;
use crate::main;
use crate::platform::{BackendTask, BackendTaskSuccessResult};
use crate::ui::document_query_screen::DocumentQueryScreen;
use crate::ui::dpns_contested_names_screen::DPNSContestedNamesScreen;
Expand Down Expand Up @@ -39,7 +40,7 @@ pub struct AppState {
pub selected_main_screen: RootScreenType,
pub screen_stack: Vec<Screen>,
pub chosen_network: Network,
pub mainnet_app_context: Arc<AppContext>,
pub mainnet_app_context: Option<Arc<AppContext>>,
pauldelucia marked this conversation as resolved.
Show resolved Hide resolved
pub testnet_app_context: Option<Arc<AppContext>>,
pub task_result_sender: mpsc::Sender<TaskResult>, // Channel sender for sending task results
pub task_result_receiver: mpsc::Receiver<TaskResult>, // Channel receiver for receiving task results
Expand Down Expand Up @@ -104,29 +105,32 @@ impl AppState {

let settings = db.get_settings().expect("expected to get settings");

let mainnet_app_context =
AppContext::new(Network::Dash, db.clone()).expect("expected Dash config for mainnet");
let mainnet_app_context = AppContext::new(Network::Dash, db.clone());
let testnet_app_context = AppContext::new(Network::Testnet, db.clone());

let mut identities_screen = IdentitiesScreen::new(&mainnet_app_context);
let mut dpns_contested_names_screen = DPNSContestedNamesScreen::new(&mainnet_app_context);
let mut transition_visualizer_screen =
TransitionVisualizerScreen::new(&mainnet_app_context);
let mut document_query_screen = DocumentQueryScreen::new(&mainnet_app_context);
let app_context = if testnet_app_context.is_some() && mainnet_app_context.is_none() {
testnet_app_context.clone().unwrap()
} else {
mainnet_app_context.clone().unwrap()
};

let mut identities_screen = IdentitiesScreen::new(&app_context);
let mut dpns_contested_names_screen = DPNSContestedNamesScreen::new(&app_context);
let mut transition_visualizer_screen = TransitionVisualizerScreen::new(&app_context);
let mut document_query_screen = DocumentQueryScreen::new(&app_context);
let mut network_chooser_screen = NetworkChooserScreen::new(
&mainnet_app_context,
mainnet_app_context.as_ref(),
testnet_app_context.as_ref(),
Network::Dash,
app_context.network,
);

let mut selected_main_screen = RootScreenType::RootScreenIdentities;

let mut chosen_network = Network::Dash;
let chosen_network = app_context.network;

if let Some((network, screen_type)) = settings {
if let Some((_, screen_type)) = settings {
selected_main_screen = screen_type;
chosen_network = network;
if network == Network::Testnet && testnet_app_context.is_some() {
if chosen_network == Network::Testnet && testnet_app_context.is_some() {
let testnet_app_context = testnet_app_context.as_ref().unwrap();
identities_screen = IdentitiesScreen::new(testnet_app_context);
dpns_contested_names_screen = DPNSContestedNamesScreen::new(testnet_app_context);
Expand Down Expand Up @@ -179,7 +183,7 @@ impl AppState {

pub fn current_app_context(&self) -> &Arc<AppContext> {
match self.chosen_network {
Network::Dash => &self.mainnet_app_context,
Network::Dash => self.mainnet_app_context.as_ref().expect("expected mainnet"),
Network::Testnet => self.testnet_app_context.as_ref().expect("expected testnet"),
Network::Devnet => todo!(),
Network::Regtest => todo!(),
Expand Down
26 changes: 22 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ impl Config {
}
}

#[derive(Debug, thiserror::Error)]
pub enum ConfigError {
#[error("{0}")]
LoadError(String),
#[error("No valid network configurations found in .env file or environment variables")]
NoValidConfigs,
}

#[derive(Debug, Deserialize, Clone)]
pub struct NetworkConfig {
/// Hostname of the Dash Platform node to connect to
Expand All @@ -49,10 +57,10 @@ pub struct NetworkConfig {

impl Config {
/// Loads the configuration for all networks from environment variables and `.env` file.
pub fn load() -> Self {
pub fn load() -> Result<Self, ConfigError> {
// Load the .env file if available
if let Err(err) = dotenvy::from_path(".env") {
tracing::warn!(
tracing::error!(
?err,
"Failed to load .env file. Continuing with environment variables."
);
Expand Down Expand Up @@ -83,10 +91,20 @@ impl Config {
}
};

Config {
if mainnet_config.is_none() && testnet_config.is_none() {
return Err(ConfigError::NoValidConfigs);
} else if mainnet_config.is_none() {
tracing::warn!("Failed to load mainnet configuration");
} else if testnet_config.is_none() {
tracing::warn!(
"Failed to load testnet configuration, but successfully loaded mainnet config"
);
}

Ok(Config {
mainnet_config,
testnet_config,
}
})
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ pub struct AppContext {

impl AppContext {
pub fn new(network: Network, db: Arc<Database>) -> Option<Arc<Self>> {
let config = Config::load();
let config = match Config::load() {
Ok(config) => config,
Err(e) => {
println!("Failed to load config: {e}");
std::process::exit(1);
}
};

let network_config = config.config_for_network(network).clone()?;

Expand Down
24 changes: 18 additions & 6 deletions src/ui/network_chooser_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH};
use std::{env, io};

pub struct NetworkChooserScreen {
pub mainnet_app_context: Arc<AppContext>,
pub mainnet_app_context: Option<Arc<AppContext>>,
pub testnet_app_context: Option<Arc<AppContext>>,
pub current_network: Network,
pub mainnet_core_status_online: bool,
Expand All @@ -28,12 +28,12 @@ pub struct NetworkChooserScreen {

impl NetworkChooserScreen {
pub fn new(
mainnet_app_context: &Arc<AppContext>,
mainnet_app_context: Option<&Arc<AppContext>>,
testnet_app_context: Option<&Arc<AppContext>>,
current_network: Network,
) -> Self {
Self {
mainnet_app_context: mainnet_app_context.clone(),
mainnet_app_context: mainnet_app_context.cloned(),
testnet_app_context: testnet_app_context.cloned(),
current_network,
mainnet_core_status_online: false,
Expand All @@ -45,11 +45,13 @@ impl NetworkChooserScreen {

pub fn context_for_network(&self, network: Network) -> &Arc<AppContext> {
match network {
Network::Dash => &self.mainnet_app_context,
Network::Dash if self.mainnet_app_context.is_some() => {
self.mainnet_app_context.as_ref().unwrap()
}
Network::Testnet if self.testnet_app_context.is_some() => {
self.testnet_app_context.as_ref().unwrap()
}
_ => &self.mainnet_app_context,
_ => unreachable!(),
}
}

Expand Down Expand Up @@ -116,6 +118,16 @@ impl NetworkChooserScreen {
// Display status indicator
ui.colored_label(status_color, if is_working { "Online" } else { "Offline" });

if network == Network::Testnet && self.testnet_app_context.is_none() {
ui.label("(No configs for testnet loaded)");
ui.end_row();
return AppAction::None;
} else if network == Network::Dash && self.mainnet_app_context.is_none() {
ui.label("(No configs for mainnet loaded)");
ui.end_row();
return AppAction::None;
}

// Display wallet count
let wallet_count = format!(
"{}",
Expand All @@ -130,7 +142,7 @@ impl NetworkChooserScreen {
// Add a button to start the network
if ui.button("+").clicked() {
let context = if network == Network::Dash || self.testnet_app_context.is_none() {
&self.mainnet_app_context
&self.mainnet_app_context.as_ref().unwrap()
} else {
&self.testnet_app_context.as_ref().unwrap()
};
Expand Down