node: ability to reset peers data

This commit is contained in:
ardocrat 2024-05-18 20:58:34 +03:00
parent 119ca7d201
commit 55011c31de
4 changed files with 58 additions and 24 deletions

View file

@ -274,6 +274,8 @@ network_settings:
ban_window_desc: The decision to ban is made by node, based on the correctness of the data received from the peer.
max_inbound_count: 'Maximum number of inbound peer connections:'
max_outbound_count: 'Maximum number of outbound peer connections:'
reset_peers_desc: Reset peers data. Use it only if there are problems with finding peers.
reset_peers: Reset peers
modal:
cancel: Cancel
save: Save

View file

@ -274,6 +274,8 @@ network_settings:
ban_window_desc: Решение о запрете принимается узлом, основываясь на корректности данных полученных от пира.
max_inbound_count: 'Максимальное количество входящих подключений пиров:'
max_outbound_count: 'Максимальное количество исходящих подключений к пирам:'
reset_peers_desc: Сбросить данные пиров. Использовать только при наличии проблем с поиском пиров.
reset_peers: Сбросить пиры
modal:
cancel: Отмена
save: Сохранить

View file

@ -63,6 +63,9 @@ pub struct P2PSetup {
/// Maximum number of outbound peer connections.
max_outbound_count: String,
/// Flag to check if reset of peers was called.
peers_reset: bool,
/// [`Modal`] identifiers allowed at this ui container.
modal_ids: Vec<&'static str>
}
@ -107,6 +110,7 @@ impl Default for P2PSetup {
ban_window_edit: NodeConfig::get_p2p_ban_window(),
max_inbound_count: NodeConfig::get_max_inbound_peers(),
max_outbound_count: NodeConfig::get_max_outbound_peers(),
peers_reset: false,
modal_ids: vec![
PORT_MODAL,
CUSTOM_SEED_MODAL,
@ -222,6 +226,15 @@ impl P2PSetup {
// Show maximum outbound peers value setup.
self.max_outbound_ui(ui, cb);
if !Node::is_restarting() && !self.peers_reset {
ui.add_space(6.0);
View::horizontal_line(ui, Colors::ITEM_STROKE);
ui.add_space(6.0);
// Show peers data reset content.
self.reset_peers_ui(ui);
}
});
}
@ -722,6 +735,22 @@ impl P2PSetup {
ui.add_space(6.0);
});
}
/// Draw content to reset peers data.
fn reset_peers_ui(&mut self, ui: &mut egui::Ui) {
ui.add_space(4.0);
let button_text = format!("{} {}", TRASH, t!("network_settings.reset_peers"));
View::colored_text_button(ui, button_text, Colors::RED, Colors::BUTTON, || {
Node::reset_peers(false);
self.peers_reset = true;
});
ui.add_space(6.0);
ui.label(RichText::new(t!("network_settings.reset_peers_desc"))
.size(16.0)
.color(Colors::INACTIVE_TEXT)
);
}
}
/// Draw peer list item.

View file

@ -55,8 +55,8 @@ pub struct Node {
stop_needed: AtomicBool,
/// Flag to check if app exit is needed after [`Server`] stop.
exit_after_stop: AtomicBool,
/// Flag to clean-up peers and restart the [`Server`].
clean_up_peers: AtomicBool,
/// Flag to reset peers data and restart the [`Server`].
reset_peers: AtomicBool,
/// An error occurred on [`Server`] start.
error: Arc<RwLock<Option<Error>>>
@ -74,7 +74,7 @@ impl Default for Node {
exit_after_stop: AtomicBool::new(false),
start_stratum_needed: AtomicBool::new(false),
error: Arc::new(RwLock::new(None)),
clean_up_peers: AtomicBool::new(false),
reset_peers: AtomicBool::new(false),
}
}
}
@ -147,12 +147,12 @@ impl Node {
/// Check if [`Node`] is restarting.
pub fn is_restarting() -> bool {
NODE_STATE.restart_needed.load(Ordering::Relaxed)
NODE_STATE.restart_needed.load(Ordering::Relaxed) || Self::reset_peers_needed()
}
/// Check if clean-up of [`Server`] peers is needed.
fn clean_up_peers_needed() -> bool {
NODE_STATE.clean_up_peers.load(Ordering::Relaxed)
/// Check if reset of [`Server`] peers is needed.
fn reset_peers_needed() -> bool {
NODE_STATE.reset_peers.load(Ordering::Relaxed)
}
/// Get node [`Server`] statistics.
@ -175,7 +175,7 @@ impl Node {
return Some(SyncStatus::Shutdown);
}
// Return Initial status when node is starting or restarting.
// Return Initial status when node is starting or restarting or peers are deleting.
if Self::is_starting() || Self::is_restarting() {
return Some(SyncStatus::Initial);
}
@ -231,21 +231,19 @@ impl Node {
fn start_server_thread() {
thread::spawn(move || {
NODE_STATE.starting.store(true, Ordering::Relaxed);
// Start the server.
match start_node_server() {
Ok(mut server) => {
let mut first_start = true;
loop {
// Restart server if request or peers clean up is needed
let clean_up_peers = Self::clean_up_peers_needed();
if Self::is_restarting() || clean_up_peers {
if Self::is_restarting() {
server.stop();
// Wait server after stop.
thread::sleep(Duration::from_millis(5000));
// Clean-up peers of requested.
if clean_up_peers {
Node::clean_up_peers(true);
// Reset peers data if requested.
if Self::reset_peers_needed() {
Node::reset_peers(true);
}
// Reset stratum stats.
{
@ -310,6 +308,7 @@ impl Node {
}
}
// Reset stratum server start flag.
if stratum_start_requested && NODE_STATE.stratum_stats.read().is_running {
NODE_STATE.start_stratum_needed.store(false, Ordering::Relaxed);
}
@ -380,24 +379,24 @@ impl Node {
}
}
/// Clean-up [`Server`] peers from storage.
pub fn clean_up_peers(force: bool) {
/// Reset [`Server`] peers data.
pub fn reset_peers(force: bool) {
if force || !Node::is_running() {
NODE_STATE.clean_up_peers.store(false, Ordering::Relaxed);
// Get saved server config.
let config = NodeConfig::node_server_config();
let mut server_config = config.server.clone();
// Remove peers folder.
let mut tmp_dir = PathBuf::from(&server_config.db_root);
tmp_dir.push("peers");
if tmp_dir.exists() {
match fs::remove_dir_all(tmp_dir) {
let mut peers_dir = PathBuf::from(&server_config.db_root);
peers_dir.push("peer");
if peers_dir.exists() {
match fs::remove_dir_all(peers_dir) {
Ok(_) => {}
Err(_) => { println!("Cannot remove peers dir") }
Err(_) => {}
}
}
NODE_STATE.reset_peers.store(false, Ordering::Relaxed);
} else {
NODE_STATE.clean_up_peers.store(true, Ordering::Relaxed);
NODE_STATE.reset_peers.store(true, Ordering::Relaxed);
}
}
@ -526,9 +525,11 @@ fn start_node_server() -> Result<Server, Error> {
// Get saved server config.
let config = NodeConfig::node_server_config();
let mut server_config = config.server.clone();
// Fix to avoid too many opened files.
server_config.p2p_config.peer_min_preferred_outbound_count =
server_config.p2p_config.peer_max_outbound_count;
// Remove temporary file dir.
{
let mut tmp_dir = PathBuf::from(&server_config.db_root);
@ -537,7 +538,7 @@ fn start_node_server() -> Result<Server, Error> {
if tmp_dir.exists() {
match fs::remove_dir_all(tmp_dir) {
Ok(_) => {}
Err(_) => { println!("Cannot remove tmp dir") }
Err(_) => {}
}
}
}