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

Closed
wants to merge 8 commits into from
Closed
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
57 changes: 20 additions & 37 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,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 @@ -102,46 +102,29 @@ impl AppState {
let db = Arc::new(Database::new("identities.db").unwrap());
db.initialize().unwrap();

let settings = db.get_settings().expect("expected to get settings");
let mainnet_app_context = AppContext::new(Network::Dash, db.clone());
let testnet_app_context = AppContext::new(Network::Testnet, db.clone());

let mainnet_app_context = match AppContext::new(Network::Dash, db.clone()) {
Some(context) => context,
None => {
eprintln!(
"Error: Failed to create the AppContext. Expected Dash config for mainnet."
);
std::process::exit(1);
}
let app_context = if mainnet_app_context.is_some() {
mainnet_app_context.clone().unwrap()
} else if testnet_app_context.is_some() {
testnet_app_context.clone().unwrap()
} else {
println!("No valid network configurations found in .env file or environment variables");
std::process::exit(1);
};
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 mut network_chooser_screen = NetworkChooserScreen::new(
&mainnet_app_context,
let identities_screen = IdentitiesScreen::new(&app_context);
let dpns_contested_names_screen = DPNSContestedNamesScreen::new(&app_context);
let transition_visualizer_screen = TransitionVisualizerScreen::new(&app_context);
let document_query_screen = DocumentQueryScreen::new(&app_context);
let network_chooser_screen = NetworkChooserScreen::new(
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;

if let Some((network, screen_type)) = settings {
selected_main_screen = screen_type;
chosen_network = network;
if 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);
transition_visualizer_screen = TransitionVisualizerScreen::new(testnet_app_context);
document_query_screen = DocumentQueryScreen::new(testnet_app_context);
}
network_chooser_screen.current_network = chosen_network;
}
let selected_main_screen = RootScreenType::RootScreenIdentities;

// // Create a channel with a buffer size of 32 (adjust as needed)
let (task_result_sender, task_result_receiver) = mpsc::channel(256);
Expand Down Expand Up @@ -175,7 +158,7 @@ impl AppState {
.into(),
selected_main_screen,
screen_stack: vec![],
chosen_network,
chosen_network: app_context.network,
mainnet_app_context,
testnet_app_context,
task_result_sender,
Expand All @@ -186,7 +169,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
10 changes: 1 addition & 9 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl Config {
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 @@ -93,14 +93,6 @@ impl Config {

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

Ok(Config {
Expand Down
2 changes: 1 addition & 1 deletion src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl AppContext {
Ok(config) => config,
Err(e) => {
println!("Failed to load config: {e}");
return None;
std::process::exit(1);
}
};

Expand Down
19 changes: 13 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 @@ -118,6 +120,11 @@ impl NetworkChooserScreen {

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;
}

Expand All @@ -135,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