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(launchpad): enable user to reset nodes #1837

Merged
merged 2 commits into from
Jun 4, 2024
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
5 changes: 3 additions & 2 deletions node-launchpad/.config/config.json5
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
"<Ctrl-g>": {"HomeActions":"TriggerManageNodes"},
"<Ctrl-G>": {"HomeActions":"TriggerManageNodes"},
"<Ctrl-Shift-g>": {"HomeActions":"TriggerManageNodes"},
"<Shift-o>": {"HomeActions":"TriggerManageNodes"},
"<h>": {"HomeActions":"TriggerHelp"},
"<H>": {"HomeActions":"TriggerHelp"},
"<Shift-h>": {"HomeActions":"TriggerHelp"},

"<Ctrl-r>": {"HomeActions":"TriggerResetNodesPopUp"},
"<Ctrl-R>": {"HomeActions":"TriggerResetNodesPopUp"},
"<Ctrl-Shift-r>": {"HomeActions":"TriggerResetNodesPopUp"},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Three combinations?

Copy link
Member Author

@RolandSherwin RolandSherwin Jun 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Ctrl-Shift-r is just for a fall back. I'm not sure if it even works, i.e, some might think that [Ctrl+R] in the docs/footer might refer to capital R. So they'd try to perform ctrl + shift + r. It is just for that. It might just work without that, I haven't tested it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have the same pattern for the other keybinds.
It might be worth checking and cleaning that up later!


"<q>": "Quit",
"<Shift-q>": "Quit",
Expand Down
4 changes: 3 additions & 1 deletion node-launchpad/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ pub enum Action {

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Display, Deserialize)]
pub enum HomeActions {
ResetNodes,
StartNodes,
StopNodes,
StartNodesCompleted,
StopNodesCompleted,
ResetNodesCompleted,
ResetNodesCompleted { trigger_start_node: bool },
SuccessfullyDetectedNatStatus,
ErrorWhileRunningNatDetection,

Expand All @@ -49,6 +50,7 @@ pub enum HomeActions {
TriggerBetaProgramme,
TriggerManageNodes,
TriggerHelp,
TriggerResetNodesPopUp,

PreviousTableItem,
NextTableItem,
Expand Down
4 changes: 3 additions & 1 deletion node-launchpad/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
action::Action,
components::{
beta_programme::BetaProgramme, footer::Footer, help::HelpPopUp, home::Home,
manage_nodes::ManageNodes, Component,
manage_nodes::ManageNodes, reset_popup::ResetNodesPopup, Component,
},
config::{AppData, Config},
mode::{InputMode, Scene},
Expand Down Expand Up @@ -58,6 +58,7 @@ impl App {
let manage_nodes = ManageNodes::new(app_data.nodes_to_start)?;
let footer = Footer::new(app_data.nodes_to_start > 0);
let help = HelpPopUp::default();
let reset_nodes = ResetNodesPopup::default();

Ok(Self {
config,
Expand All @@ -70,6 +71,7 @@ impl App {
Box::new(discord_username_input),
Box::new(manage_nodes),
Box::new(help),
Box::new(reset_nodes),
],
should_quit: false,
should_suspend: false,
Expand Down
1 change: 1 addition & 0 deletions node-launchpad/src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod help;
pub mod home;
pub mod manage_nodes;
pub mod options;
pub mod reset_popup;
pub mod tab;
pub mod utils;

Expand Down
8 changes: 4 additions & 4 deletions node-launchpad/src/components/beta_programme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ impl Component for BetaProgramme {
);
f.render_widget(input, layer_two[1]);

let text = Paragraph::new(" Changing the username will lead to loss of earned.\n rewards. The nodes will be restarted to reflect\n the change.");
let text = Paragraph::new(" Changing your Username will reset all nodes,\n and any Nanos left on this device will be\n lost.");
f.render_widget(text.fg(GHOST_WHITE), layer_two[2]);

let dash = Block::new()
Expand All @@ -240,8 +240,8 @@ impl Component for BetaProgramme {
f.render_widget(dash, layer_two[3]);

let buttons_layer = Layout::horizontal(vec![
Constraint::Percentage(50),
Constraint::Percentage(50),
Constraint::Percentage(55),
Constraint::Percentage(45),
])
.split(layer_two[4]);

Expand All @@ -257,7 +257,7 @@ impl Component for BetaProgramme {
Style::default().fg(EUCALYPTUS)
};
let button_yes = Line::from(vec![Span::styled(
"Modify Username [Enter]",
"Save Username [Enter]",
button_yes_style,
)]);
f.render_widget(button_yes, buttons_layer[1]);
Expand Down
6 changes: 5 additions & 1 deletion node-launchpad/src/components/footer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ impl Component for Footer {
};

let (line1, line2) = match self.current_scene {
Scene::Home | Scene::BetaProgramme | Scene::HelpPopUp | Scene::ManageNodes => {
Scene::Home
| Scene::BetaProgramme
| Scene::HelpPopUp
| Scene::ManageNodes
| Scene::ResetPopUp => {
let line1 = Line::from(vec![
Span::styled(" [Ctrl+S] ", command_style),
Span::styled("Start all Nodes ", text_style),
Expand Down
49 changes: 35 additions & 14 deletions node-launchpad/src/components/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,10 @@ impl Component for Home {
// make sure we're in navigation mode
return Ok(Some(Action::SwitchInputMode(InputMode::Navigation)));
}
Scene::BetaProgramme | Scene::ManageNodes | Scene::HelpPopUp => self.active = true,
Scene::BetaProgramme
| Scene::ManageNodes
| Scene::HelpPopUp
| Scene::ResetPopUp => self.active = true,
_ => self.active = false,
},
Action::StoreNodesToStart(count) => {
Expand All @@ -232,18 +235,16 @@ impl Component for Home {
}
}
Action::StoreDiscordUserName(username) => {
let reset_safenode_services = (self.discord_username != username)
&& !self.discord_username.is_empty()
&& !self.node_services.is_empty();
let has_changed = self.discord_username != username;
let we_have_nodes = !self.node_services.is_empty();

self.discord_username = username;

// todo: The discord_username popup should warn people that if nodes are running, they will be reset.
// And the earnings will be lost.
if reset_safenode_services {
if we_have_nodes && has_changed {
self.lock_registry = Some(LockRegistryState::ResettingNodes);
info!("Resetting safenode services because the discord username was reset.");
let action_sender = self.get_actions_sender()?;
reset_nodes(action_sender);
reset_nodes(action_sender, true);
}
}
Action::HomeActions(HomeActions::StartNodes) => {
Expand Down Expand Up @@ -283,6 +284,18 @@ impl Component for Home {

stop_nodes(running_nodes, action_sender);
}
Action::HomeActions(HomeActions::ResetNodes) => {
if self.lock_registry.is_some() {
error!("Registry is locked. Cannot reset nodes now.");
return Ok(None);
}

self.lock_registry = Some(LockRegistryState::ResettingNodes);
let action_sender = self.get_actions_sender()?;
info!("Got action to reset nodes");
reset_nodes(action_sender, false);
}

Action::Tick => {
self.try_update_node_stats(false)?;
}
Expand All @@ -294,12 +307,15 @@ impl Component for Home {
self.lock_registry = None;
self.load_node_registry_and_update_states()?;
}
Action::HomeActions(HomeActions::ResetNodesCompleted) => {
Action::HomeActions(HomeActions::ResetNodesCompleted { trigger_start_node }) => {
self.lock_registry = None;
self.load_node_registry_and_update_states()?;

// trigger start nodes.
return Ok(Some(Action::HomeActions(HomeActions::StartNodes)));
if trigger_start_node {
debug!("Reset nodes completed. Triggering start nodes.");
return Ok(Some(Action::HomeActions(HomeActions::StartNodes)));
}
debug!("Reset nodes completed");
}
Action::HomeActions(HomeActions::SuccessfullyDetectedNatStatus) => {
debug!("Successfully detected nat status, is_nat_status_determined set to true");
Expand All @@ -322,7 +338,9 @@ impl Component for Home {
Action::HomeActions(HomeActions::TriggerHelp) => {
return Ok(Some(Action::SwitchScene(Scene::HelpPopUp)));
}

Action::HomeActions(HomeActions::TriggerResetNodesPopUp) => {
return Ok(Some(Action::SwitchScene(Scene::ResetPopUp)));
}
Action::HomeActions(HomeActions::PreviousTableItem) => {
self.select_previous_table_item();
}
Expand Down Expand Up @@ -650,14 +668,17 @@ fn maintain_n_running_nodes(
});
}

fn reset_nodes(action_sender: UnboundedSender<Action>) {
fn reset_nodes(action_sender: UnboundedSender<Action>, start_nodes_after_reset: bool) {
tokio::task::spawn_local(async move {
if let Err(err) = sn_node_manager::cmd::node::reset(true, VerbosityLevel::Minimal).await {
error!("Error while resetting services {err:?}");
} else {
info!("Successfully reset services");
}
if let Err(err) = action_sender.send(Action::HomeActions(HomeActions::ResetNodesCompleted))
if let Err(err) =
action_sender.send(Action::HomeActions(HomeActions::ResetNodesCompleted {
trigger_start_node: start_nodes_after_reset,
}))
{
error!("Error while sending action: {err:?}");
}
Expand Down
Loading
Loading