Skip to content

Commit

Permalink
Merge branch 'main' into fix/wallet-ui
Browse files Browse the repository at this point in the history
  • Loading branch information
shanimal08 committed Nov 28, 2024
2 parents 1e1321f + ca2701e commit ea6b61d
Show file tree
Hide file tree
Showing 16 changed files with 228 additions and 73 deletions.
2 changes: 1 addition & 1 deletion public/assets/glApp.js

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions src-tauri/src/cpu_miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ impl CpuMiner {
Ok(())
}

pub async fn is_running(&self) -> bool {
let lock = self.watcher.read().await;
lock.is_running()
}

pub async fn is_pid_file_exists(&self, base_path: PathBuf) -> bool {
let lock = self.watcher.read().await;
lock.is_pid_file_exists(base_path)
}

pub async fn status(
&self,
network_hash_rate: u64,
Expand Down
10 changes: 10 additions & 0 deletions src-tauri/src/gpu_miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ impl GpuMiner {
Ok(())
}

pub async fn is_running(&self) -> bool {
let process_watcher = self.watcher.read().await;
process_watcher.is_running()
}

pub async fn is_pid_file_exists(&self, base_path: PathBuf) -> bool {
let lock = self.watcher.read().await;
lock.is_pid_file_exists(base_path)
}

pub async fn status(
&self,
network_hash_rate: u64,
Expand Down
57 changes: 7 additions & 50 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use tauri::{Manager, RunEvent, UpdaterEvent, Window};
use tokio::sync::{Mutex, RwLock};
use tor_adapter::TorConfig;
use utils::logging_utils::setup_logging;
use utils::shutdown_utils::stop_all_processes;
use wallet_adapter::TransactionInfo;

use app_config::{AppConfig, GpuThreads};
Expand Down Expand Up @@ -128,50 +129,6 @@ struct UpdateProgressRustEvent {
downloaded: u64,
}

async fn stop_all_miners(state: UniverseAppState, sleep_secs: u64) -> Result<(), String> {
state
.cpu_miner
.write()
.await
.stop()
.await
.map_err(|e| e.to_string())?;
state
.gpu_miner
.write()
.await
.stop()
.await
.map_err(|e| e.to_string())?;
let exit_code = state
.wallet_manager
.stop()
.await
.map_err(|e| e.to_string())?;
info!(target: LOG_TARGET, "Wallet manager stopped with exit code: {}", exit_code);
state
.mm_proxy_manager
.stop()
.await
.map_err(|e| e.to_string())?;
let exit_code = state.node_manager.stop().await.map_err(|e| e.to_string())?;
info!(target: LOG_TARGET, "Node manager stopped with exit code: {}", exit_code);
let exit_code = state
.p2pool_manager
.stop()
.await
.map_err(|e| e.to_string())?;
info!(target: LOG_TARGET, "P2Pool manager stopped with exit code: {}", exit_code);

let exit_code = state.tor_manager.stop().await.map_err(|e| e.to_string())?;
info!(target: LOG_TARGET, "Tor manager stopped with exit code: {}", exit_code);
state.shutdown.clone().trigger();

// TODO: Find a better way of knowing that all miners have stopped
sleep(std::time::Duration::from_secs(sleep_secs));
Ok(())
}

#[tauri::command]
async fn set_mode(
mode: String,
Expand Down Expand Up @@ -501,7 +458,7 @@ async fn restart_application(
app: tauri::AppHandle,
) -> Result<(), String> {
if should_stop_miners {
stop_all_miners(state.inner().clone(), 5).await?;
stop_all_processes(state.inner().clone(), true).await?;
}

app.restart();
Expand All @@ -514,7 +471,7 @@ async fn exit_application(
state: tauri::State<'_, UniverseAppState>,
app: tauri::AppHandle,
) -> Result<(), String> {
stop_all_miners(state.inner().clone(), 5).await?;
stop_all_processes(state.inner().clone(), true).await?;

app.exit(0);
Ok(())
Expand Down Expand Up @@ -1120,7 +1077,7 @@ async fn import_seed_words(
.app_local_data_dir()
.expect("Could not get data dir");

stop_all_miners(state.inner().clone(), 5).await?;
stop_all_processes(state.inner().clone(), false).await?;

tauri::async_runtime::spawn(async move {
match InternalWallet::create_from_seed(config_path, seed_words).await {
Expand Down Expand Up @@ -1861,7 +1818,7 @@ async fn reset_settings<'r>(
state: tauri::State<'_, UniverseAppState>,
app: tauri::AppHandle,
) -> Result<(), String> {
stop_all_miners(state.inner().clone(), 5).await?;
stop_all_processes(state.inner().clone(), true).await?;
let network = Network::get_current_or_user_setting_or_default().as_key_str();

let app_config_dir = app.path_resolver().app_config_dir();
Expand Down Expand Up @@ -2368,12 +2325,12 @@ fn main() {
tauri::RunEvent::ExitRequested { api: _, .. } => {
// api.prevent_exit();
info!(target: LOG_TARGET, "App shutdown caught");
let _unused = block_on(stop_all_miners(app_state.clone(), 2));
let _unused = block_on(stop_all_processes(app_state.clone(), true));
info!(target: LOG_TARGET, "App shutdown complete");
}
tauri::RunEvent::Exit => {
info!(target: LOG_TARGET, "App shutdown caught");
let _unused = block_on(stop_all_miners(app_state.clone(), 2));
let _unused = block_on(stop_all_processes(app_state.clone(), true));
info!(target: LOG_TARGET, "Tari Universe v{} shut down successfully", _app_handle.package_info().version);
}
RunEvent::MainEventsCleared => {
Expand Down
11 changes: 11 additions & 0 deletions src-tauri/src/mm_proxy_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ impl MmProxyManager {
let mut current_start_config = self.start_config.write().await;
*current_start_config = Some(config.clone());
let mut process_watcher = self.watcher.write().await;

let new_config = MergeMiningProxyConfig {
tari_address: config.tari_address.clone(),
base_node_grpc_port: config.base_node_grpc_port,
Expand Down Expand Up @@ -135,4 +136,14 @@ impl MmProxyManager {
process_watcher.stop().await?;
Ok(())
}

pub async fn is_running(&self) -> bool {
let lock = self.watcher.read().await;
lock.is_running()
}

pub async fn is_pid_file_exists(&self, base_path: PathBuf) -> bool {
let lock = self.watcher.read().await;
lock.is_pid_file_exists(base_path)
}
}
11 changes: 11 additions & 0 deletions src-tauri/src/node_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ impl NodeManager {
) -> Result<(), NodeManagerError> {
{
let mut process_watcher = self.watcher.write().await;

process_watcher.adapter.use_tor = use_tor;
process_watcher.adapter.tor_control_port = tor_control_port;
process_watcher.stop_on_exit_codes = vec![114];
Expand Down Expand Up @@ -206,6 +207,16 @@ impl NodeManager {
Ok(exit_code)
}

pub async fn is_running(&self) -> bool {
let process_watcher = self.watcher.read().await;
process_watcher.is_running()
}

pub async fn is_pid_file_exists(&self, base_path: PathBuf) -> bool {
let lock = self.watcher.read().await;
lock.is_pid_file_exists(base_path)
}

pub async fn check_if_is_orphan_chain(&self) -> Result<bool, anyhow::Error> {
let mut status_monitor_lock = self.watcher.write().await;
let status_monitor = status_monitor_lock
Expand Down
22 changes: 13 additions & 9 deletions src-tauri/src/p2pool_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;

use futures_util::future::FusedFuture;
use log::warn;
use tari_shutdown::ShutdownSignal;
use tokio::sync::RwLock;
Expand Down Expand Up @@ -96,12 +97,14 @@ impl P2poolManager {
}
}

pub async fn is_running(&self) -> Result<bool, anyhow::Error> {
pub async fn is_running(&self) -> bool {
let process_watcher = self.watcher.read().await;
if process_watcher.is_running() {
return Ok(true);
}
Ok(false)
process_watcher.is_running()
}

pub async fn is_pid_file_exists(&self, base_path: PathBuf) -> bool {
let lock = self.watcher.read().await;
lock.is_pid_file_exists(base_path)
}

pub async fn ensure_started(
Expand All @@ -113,15 +116,13 @@ impl P2poolManager {
log_path: PathBuf,
) -> Result<(), anyhow::Error> {
let mut process_watcher = self.watcher.write().await;
if process_watcher.is_running() {
return Ok(());
}

process_watcher.adapter.config = Some(config);
process_watcher.health_timeout = Duration::from_secs(28);
process_watcher.poll_time = Duration::from_secs(30);
process_watcher
.start(
app_shutdown,
app_shutdown.clone(),
base_path,
config_path,
log_path,
Expand All @@ -131,6 +132,9 @@ impl P2poolManager {
process_watcher.wait_ready().await?;
if let Some(status_monitor) = &process_watcher.status_monitor {
loop {
if app_shutdown.is_terminated() || app_shutdown.is_triggered() {
break;
}
sleep(Duration::from_secs(5)).await;
if let Ok(_stats) = status_monitor.status().await {
break;
Expand Down
14 changes: 7 additions & 7 deletions src-tauri/src/port_allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use anyhow::{anyhow, Error};
use log::{error, info, warn};
use std::net::TcpListener;

const LOG_TARGET: &str = "tari::universe::systemtray_manager";
const LOG_TARGET: &str = "tari::universe::port_allocator";
const ADDRESS: &str = "127.0.0.1";
const MAX_TRIES: u16 = 10;
const MAX_RETRIES: u16 = 10;
const FALLBACK_PORT_RANGE: std::ops::Range<u16> = 49152..65535;

pub struct PortAllocator {}
Expand Down Expand Up @@ -55,9 +55,9 @@ impl PortAllocator {
while !self.check_if_port_is_free(port) {
port = self.get_port()?;
tries += 1;
if tries >= MAX_TRIES {
warn!(target: LOG_TARGET, "Failed to assign port after {} tries", MAX_TRIES);
return Err(anyhow!("Failed to assign port after {} tries", MAX_TRIES));
if tries >= MAX_RETRIES {
warn!(target: LOG_TARGET, "Failed to assign port after {} tries", MAX_RETRIES);
return Err(anyhow!("Failed to assign port after {} tries", MAX_RETRIES));
}
}

Expand All @@ -76,8 +76,8 @@ impl PortAllocator {
.get_port()
.unwrap_or_else(|_| self.asign_port_from_fallback_range());
tries += 1;
if tries >= MAX_TRIES {
warn!(target: LOG_TARGET, "Failed to assign port after {} tries", MAX_TRIES);
if tries >= MAX_RETRIES {
warn!(target: LOG_TARGET, "Failed to assign port after {} tries", MAX_RETRIES);
info!(target: LOG_TARGET, "Assigning port from fallback range");
return self.asign_port_from_fallback_range();
}
Expand Down
13 changes: 13 additions & 0 deletions src-tauri/src/process_adapter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::{anyhow, Error};
use async_trait::async_trait;
use futures_util::future::FusedFuture;
use log::{error, info, warn};
use sentry::protocol::Event;
use sentry_tauri::sentry;
Expand Down Expand Up @@ -41,6 +42,12 @@ pub(crate) trait ProcessAdapter {

fn pid_file_name(&self) -> &str;

fn pid_file_exisits(&self, base_folder: PathBuf) -> bool {
std::path::Path::new(&base_folder)
.join(self.pid_file_name())
.exists()
}

async fn kill_previous_instances(&self, base_folder: PathBuf) -> Result<(), Error> {
info!(target: LOG_TARGET, "Killing previous instances of {}", self.name());
match fs::read_to_string(base_folder.join(self.pid_file_name())) {
Expand Down Expand Up @@ -119,6 +126,12 @@ impl ProcessInstance {
// Reset the shutdown each time.
self.shutdown = Shutdown::new();
let shutdown_signal = self.shutdown.to_signal();

if shutdown_signal.is_terminated() || shutdown_signal.is_triggered() {
warn!(target: LOG_TARGET, "Shutdown signal is triggered. Not starting process");
return Ok(());
};

self.handle = Some(tokio::spawn(async move {
crate::download_utils::set_permissions(&spec.file_path).await?;
// start
Expand Down
10 changes: 10 additions & 0 deletions src-tauri/src/process_watcher.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::binaries::{Binaries, BinaryResolver};
use crate::process_adapter::{HealthStatus, ProcessAdapter, ProcessInstance, StatusMonitor};
use futures_util::future::FusedFuture;
use log::{error, info, warn};
use std::path::PathBuf;
use std::time::{Duration, Instant};
Expand Down Expand Up @@ -53,6 +54,11 @@ impl<TAdapter: ProcessAdapter> ProcessWatcher<TAdapter> {
log_path: PathBuf,
binary: Binaries,
) -> Result<(), anyhow::Error> {
info!(target: LOG_TARGET, "App shutdown triggered or terminated status for {} = {} | {}", self.adapter.name(),app_shutdown.is_triggered(),app_shutdown.is_terminated());
if app_shutdown.is_terminated() || app_shutdown.is_triggered() {
return Ok(());
}

let name = self.adapter.name().to_string();
if self.watcher_task.is_some() {
warn!(target: LOG_TARGET, "Tried to start process watcher for {} twice", name);
Expand Down Expand Up @@ -122,6 +128,10 @@ impl<TAdapter: ProcessAdapter> ProcessWatcher<TAdapter> {
}
}

pub fn is_pid_file_exists(&self, base_path: PathBuf) -> bool {
self.adapter.pid_file_exisits(base_path)
}

pub async fn wait_ready(&self) -> Result<(), anyhow::Error> {
if let Some(ref task) = self.watcher_task {
if task.inner().is_finished() {
Expand Down
3 changes: 1 addition & 2 deletions src-tauri/src/telemetry_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,7 @@ async fn get_telemetry_data(

// let p2pool_gpu_stats_sha3 = p2pool_stats.as_ref().map(|s| s.sha3x_stats.clone());
// let p2pool_cpu_stats_randomx = p2pool_stats.as_ref().map(|s| s.randomx_stats.clone());
let p2pool_enabled =
config_guard.p2pool_enabled() && p2pool_manager.is_running().await.unwrap_or(false);
let p2pool_enabled = config_guard.p2pool_enabled() && p2pool_manager.is_running().await;
// let (cpu_tribe_name, cpu_tribe_id) = if p2pool_enabled {
// if let Some(randomx_stats) = p2pool_cpu_stats_randomx {
// (
Expand Down
16 changes: 13 additions & 3 deletions src-tauri/src/tor_manager.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::{path::PathBuf, sync::Arc};
use tokio::sync::RwLock;

use crate::process_watcher::ProcessWatcher;
use crate::tor_adapter::{TorAdapter, TorConfig};
use std::{path::PathBuf, sync::Arc};
use tari_shutdown::ShutdownSignal;
use tokio::sync::RwLock;

pub(crate) struct TorManager {
watcher: Arc<RwLock<ProcessWatcher<TorAdapter>>>,
Expand Down Expand Up @@ -36,6 +35,7 @@ impl TorManager {
) -> Result<(), anyhow::Error> {
{
let mut process_watcher = self.watcher.write().await;

process_watcher
.adapter
.load_or_create_config(config_path.clone())
Expand Down Expand Up @@ -102,4 +102,14 @@ impl TorManager {
let exit_code = process_watcher.stop().await?;
Ok(exit_code)
}

pub async fn is_running(&self) -> bool {
let process_watcher = self.watcher.read().await;
process_watcher.is_running()
}

pub async fn is_pid_file_exists(&self, base_path: PathBuf) -> bool {
let lock = self.watcher.read().await;
lock.is_pid_file_exists(base_path)
}
}
1 change: 1 addition & 0 deletions src-tauri/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod file_utils;
pub mod logging_utils;
pub mod platform_utils;
pub mod setup_utils;
pub mod shutdown_utils;
Loading

0 comments on commit ea6b61d

Please sign in to comment.