Skip to content

Commit

Permalink
Merge branch 'refs/heads/main' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
stringhandler committed Aug 7, 2024
2 parents d12ab94 + 76d7cc4 commit c6b18ae
Show file tree
Hide file tree
Showing 20 changed files with 237 additions and 124 deletions.
1 change: 1 addition & 0 deletions dist-isolation/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
window.__TAURI_ISOLATION_HOOK__ = (payload) => {
console.log("Isolation hook called with payload:", payload);
// TODO: Prevent command execution
// TODO: Perhaps whitelist commands
return payload
Expand Down
13 changes: 13 additions & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ async-trait = "0.1.81"
sysinfo = "0.31.2"
log4rs = "1.3.0"
keyring = "3.0.5"
nix = {version ="0.29.0", features = ["signal"] }
# static bind lzma
xz2 = { version = "0.1.7", features = ["static"] }

Expand Down
2 changes: 2 additions & 0 deletions src-tauri/src/binary_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ impl LatestVersionApiAdapter for GithubReleasesAdapter {
name_suffix = "linux-x86_64.zip";
}

info!(target: LOG_TARGET, "Looking for platform with suffix: {}", name_suffix);

let platform = version
.assets
.iter()
Expand Down
7 changes: 5 additions & 2 deletions src-tauri/src/cpu_miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ impl CpuMiner {
cpu_miner_config: &CpuMinerConfig,
local_mm_proxy: &MmProxyManager,
base_path: PathBuf,
cache_dir: PathBuf,
log_dir: PathBuf,
window: tauri::Window,
) -> Result<(), anyhow::Error> {
if self.watcher_task.is_some() {
Expand All @@ -54,7 +56,7 @@ impl CpuMiner {
local_mm_proxy
.start(
app_shutdown.clone(),
base_path,
base_path.clone(),
cpu_miner_config.tari_address.clone(),
window.clone(),
)
Expand All @@ -69,7 +71,8 @@ impl CpuMiner {
}
};
let xmrig = XmrigAdapter::new(xmrig_node_connection, "44AFFq5kSiGBoZ4NMDwYtN18obc8AemS33DBLWs3H7otXft3XjrpDtQGv7SqSsaBYBb98uNbr2VBBEt7f2wfn3RVGQBEP3A".to_string() );
let (mut _rx, mut xmrig_child, client) = xmrig.spawn(window.clone())?;
let (mut _rx, mut xmrig_child, client) =
xmrig.spawn(cache_dir, log_dir, base_path, window.clone())?;
self.api_client = Some(client);

self.watcher_task = Some(tauri::async_runtime::spawn(async move {
Expand Down
13 changes: 8 additions & 5 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod node_manager;
mod process_adapter;
mod wallet_manager;

mod process_killer;
mod wallet_adapter;

use crate::cpu_miner::CpuMiner;
Expand Down Expand Up @@ -65,6 +66,7 @@ async fn setup_application<'r>(
},
);
let data_dir = app.path_resolver().app_local_data_dir().unwrap();
let cache_dir = app.path_resolver().app_cache_dir().unwrap();
let task1 = state
.node_manager
.ensure_started(state.shutdown.to_signal(), data_dir.clone(), window.clone())
Expand All @@ -81,11 +83,10 @@ async fn setup_application<'r>(
e.to_string()
});

let task3 =
XmrigAdapter::ensure_latest(cache_dir().unwrap(), false, window.clone()).map_err(|e| {
error!(target: LOG_TARGET, "Could not download xmrig: {:?}", e);
e.to_string()
});
let task3 = XmrigAdapter::ensure_latest(cache_dir, false, window.clone()).map_err(|e| {
error!(target: LOG_TARGET, "Could not download xmrig: {:?}", e);
e.to_string()
});

match try_join!(task1, task2, task3) {
Ok(_) => {
Expand Down Expand Up @@ -133,6 +134,8 @@ async fn start_mining<'r>(
&config,
&mm_proxy_manager,
app.path_resolver().app_local_data_dir().unwrap(),
app.path_resolver().app_cache_dir().unwrap(),
app.path_resolver().app_log_dir().unwrap(),
window.clone(),
)
.await
Expand Down
26 changes: 21 additions & 5 deletions src-tauri/src/merge_mining_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ use crate::process_adapter::{ProcessAdapter, ProcessInstance, StatusMonitor};
use anyhow::Error;
use async_trait::async_trait;
use dirs_next::data_local_dir;
use log::warn;
use std::fs;
use std::path::PathBuf;
use tari_common_types::tari_address::TariAddress;
use tari_shutdown::Shutdown;
use tokio::runtime::Handle;
use tokio::select;
use tokio::task::JoinHandle;

const LOG_TARGET: &str = "tari::universe::merge_mining_proxy_adapter";

pub struct MergeMiningProxyAdapter {
force_download: bool,
pub(crate) tari_address: TariAddress,
Expand All @@ -30,16 +34,13 @@ impl ProcessAdapter for MergeMiningProxyAdapter {

fn spawn_inner(
&self,
_log_folder: PathBuf,
data_dir: PathBuf,
window: tauri::Window,
) -> Result<(Self::Instance, Self::StatusMonitor), Error> {
let inner_shutdown = Shutdown::new();
let shutdown_signal = inner_shutdown.to_signal();

let working_dir = data_local_dir()
.unwrap()
.join("tari-universe")
.join("mmproxy");
let working_dir = data_dir.join("mmproxy");
std::fs::create_dir_all(&working_dir)?;
let args: Vec<String> = vec![
"-b".to_string(),
Expand Down Expand Up @@ -75,6 +76,10 @@ impl ProcessAdapter for MergeMiningProxyAdapter {
.kill_on_drop(true)
.spawn()?;

if let Some(id) = child.id() {
fs::write(data_dir.join("mmproxy_pid"), id.to_string())?;
}

select! {
_res = shutdown_signal =>{
child.kill().await?;
Expand All @@ -84,6 +89,13 @@ impl ProcessAdapter for MergeMiningProxyAdapter {
dbg!("Exited badly:", res2?);
},
};

match fs::remove_file(data_dir.join("mmproxy_pid")) {
Ok(_) => {}
Err(e) => {
warn!(target: LOG_TARGET, "Could not clear node's pid file");
}
}
Ok(())
})),
},
Expand All @@ -94,6 +106,10 @@ impl ProcessAdapter for MergeMiningProxyAdapter {
fn name(&self) -> &str {
"minotari_merge_mining_proxy"
}

fn pid_file_name(&self) -> &str {
"mmproxy_pid"
}
}

pub struct MergeMiningProxyInstance {
Expand Down
25 changes: 19 additions & 6 deletions src-tauri/src/minotari_node_adapter.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use crate::binary_resolver::{Binaries, BinaryResolver};
use crate::node_manager::NodeIdentity;
use crate::process_adapter::{ProcessAdapter, ProcessInstance, StatusMonitor};
use crate::process_killer::kill_process;
use crate::xmrig_adapter::XmrigInstance;
use anyhow::{anyhow, Error};
use async_trait::async_trait;
use dirs_next::data_local_dir;
use log::info;
use log::{info, warn};
use minotari_node_grpc_client::grpc::{
Empty, GetHeaderByHashRequest, HeightRequest, NewBlockTemplateRequest, PowAlgo,
};
use minotari_node_grpc_client::BaseNodeGrpcClient;
use std::fs;
use std::path::PathBuf;
use tari_core::transactions::tari_amount::MicroMinotari;
use tari_crypto::ristretto::RistrettoPublicKey;
Expand Down Expand Up @@ -42,14 +44,14 @@ impl ProcessAdapter for MinotariNodeAdapter {

fn spawn_inner(
&self,
_log_path: PathBuf,
data_dir: PathBuf,
window: tauri::Window,
) -> Result<(Self::Instance, Self::StatusMonitor), Error> {
let inner_shutdown = Shutdown::new();
let shutdown_signal = inner_shutdown.to_signal();

info!(target: LOG_TARGET, "Starting minotari node");
let working_dir = data_local_dir().unwrap().join("tari-universe").join("node");
let working_dir = data_dir.join("node");
std::fs::create_dir_all(&working_dir)?;

let mut args: Vec<String> = vec![
Expand Down Expand Up @@ -100,6 +102,10 @@ impl ProcessAdapter for MinotariNodeAdapter {
.kill_on_drop(true)
.spawn()?;

if let Some(id) = child.id() {
fs::write(data_dir.join("node_pid"), id.to_string())?;
}

select! {
_res = shutdown_signal =>{
child.kill().await?;
Expand All @@ -111,9 +117,12 @@ impl ProcessAdapter for MinotariNodeAdapter {
};
println!("Stopping minotari node");

// child.kill().await?;
// let out = child.wait_with_output().await?;
// println!("stdout: {}", String::from_utf8_lossy(&out.stdout));
match fs::remove_file(data_dir.join("node_pid")) {
Ok(_) => {}
Err(e) => {
warn!(target: LOG_TARGET, "Could not clear node's pid file");
}
}
Ok(())
})),
},
Expand All @@ -124,6 +133,10 @@ impl ProcessAdapter for MinotariNodeAdapter {
fn name(&self) -> &str {
"minotari_node"
}

fn pid_file_name(&self) -> &str {
"node_pid"
}
}

pub struct MinotariNodeInstance {
Expand Down
21 changes: 21 additions & 0 deletions src-tauri/src/process_adapter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
use crate::process_killer::kill_process;
use anyhow::Error;
use async_trait::async_trait;
use log::warn;
use std::fs;
use std::path::PathBuf;

const LOG_TARGET: &str = "tari::universe::process_adapter";

pub trait ProcessAdapter {
type Instance: ProcessInstance;
type StatusMonitor: StatusMonitor;
Expand All @@ -19,6 +25,21 @@ pub trait ProcessAdapter {
) -> Result<(Self::Instance, Self::StatusMonitor), anyhow::Error> {
self.spawn_inner(base_folder, window)
}

fn pid_file_name(&self) -> &str;

fn kill_previous_instances(&self, base_folder: PathBuf) -> Result<(), Error> {
match fs::read_to_string(base_folder.join(self.pid_file_name())) {
Ok(pid) => {
let pid = pid.trim().parse::<u32>()?;
kill_process(pid)?;
}
Err(e) => {
warn!(target: LOG_TARGET, "Could not read node's pid file: {}", e);
}
}
Ok(())
}
}

pub trait StatusMonitor {
Expand Down
16 changes: 16 additions & 0 deletions src-tauri/src/process_killer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::process::Command;

pub fn kill_process(pid: u32) -> Result<(), anyhow::Error> {
if cfg!(target_os = "windows") {
let _ = Command::new("taskkill")
.args(&["/F", "/PID", &pid.to_string()])
.output()?;
} else {
use nix::sys::signal::{self, Signal};
use nix::unistd::Pid;

let pid = Pid::from_raw(pid as i32);
signal::kill(pid, Signal::SIGTERM);
}
Ok(())
}
10 changes: 10 additions & 0 deletions src-tauri/src/process_watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ impl<TAdapter: ProcessAdapter> ProcessWatcher<TAdapter> {
}

impl<TAdapter: ProcessAdapter> ProcessWatcher<TAdapter> {
pub async fn kill_previous_instances(
&mut self,
base_path: PathBuf,
) -> Result<(), anyhow::Error> {
self.adapter.kill_previous_instances(base_path)?;
Ok(())
}

pub async fn start(
&mut self,
app_shutdown: ShutdownSignal,
Expand All @@ -40,6 +48,8 @@ impl<TAdapter: ProcessAdapter> ProcessWatcher<TAdapter> {
println!("Tried to start process watcher for {} twice", name);
return Ok(());
}
info!(target: LOG_TARGET, "Starting process watcher for {}", name);
self.kill_previous_instances(base_path.clone()).await?;

self.internal_shutdown = Shutdown::new();
let mut inner_shutdown = self.internal_shutdown.to_signal();
Expand Down
Loading

0 comments on commit c6b18ae

Please sign in to comment.