Skip to content

Commit

Permalink
Merge branch 'main' into airdrop-integrations
Browse files Browse the repository at this point in the history
  • Loading branch information
Krakaw authored Sep 26, 2024
2 parents f167b82 + 56b2758 commit 33d86b8
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 18 deletions.
17 changes: 10 additions & 7 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,13 +473,15 @@ async fn setup_inner(
//drop binary resolver to release the lock
drop(binary_resolver);

let _ = state
.gpu_miner
.write()
.await
.detect(config_dir.clone())
.await
.inspect_err(|e| error!(target: LOG_TARGET, "Could not detect gpu miner: {:?}", e));
drop(
state
.gpu_miner
.write()
.await
.detect(config_dir.clone())
.await
.inspect_err(|e| error!(target: LOG_TARGET, "Could not detect gpu miner: {:?}", e)),
);

for _i in 0..2 {
match state
Expand Down Expand Up @@ -1350,6 +1352,7 @@ fn main() {
app_config.clone(),
app_in_memory_config.clone(),
Some(Network::default()),
p2pool_manager.clone(),
);

let feedback = Feedback::new(app_in_memory_config.clone(), app_config.clone());
Expand Down
8 changes: 8 additions & 0 deletions src-tauri/src/p2pool_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ impl P2poolManager {
Err(anyhow!("Failed to get stats"))
}

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

pub async fn ensure_started(
&self,
app_shutdown: ShutdownSignal,
Expand Down
63 changes: 52 additions & 11 deletions src-tauri/src/telemetry_manager.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
use crate::app_in_memory_config::AppInMemoryConfig;
use crate::p2pool_manager::{self, P2poolManager};
use crate::{
app_config::{AppConfig, MiningMode},
cpu_miner::CpuMiner,
gpu_miner::GpuMiner,
hardware_monitor::HardwareMonitor,
node_manager::NodeManager,
};
use anyhow::Result;
use blake2::digest::Update;
use blake2::digest::VariableOutput;
Expand All @@ -16,15 +25,6 @@ use tari_utilities::encoding::Base58;
use tokio::sync::RwLock;
use tokio_util::sync::CancellationToken;

use crate::app_in_memory_config::AppInMemoryConfig;
use crate::{
app_config::{AppConfig, MiningMode},
cpu_miner::CpuMiner,
gpu_miner::GpuMiner,
hardware_monitor::HardwareMonitor,
node_manager::NodeManager,
};

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

struct TelemetryFrequency(u64);
Expand Down Expand Up @@ -58,6 +58,7 @@ pub enum TelemetryResource {
Gpu,
#[serde(rename(serialize = "cpu-gpu"))]
CpuGpu,
None,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
Expand Down Expand Up @@ -159,6 +160,11 @@ pub struct TelemetryData {
pub gpu_make: Option<String>,
pub mode: TelemetryMiningMode,
pub version: String,
pub p2pool_enabled: bool,
pub cpu_tribe_name: Option<String>,
pub cpu_tribe_id: Option<String>,
pub gpu_tribe_name: Option<String>,
pub gpu_tribe_id: Option<String>,
}

pub struct TelemetryManager {
Expand All @@ -169,6 +175,7 @@ pub struct TelemetryManager {
in_memory_config: Arc<RwLock<AppInMemoryConfig>>,
pub cancellation_token: CancellationToken,
node_network: Option<Network>,
p2pool_manager: P2poolManager,
}

impl TelemetryManager {
Expand All @@ -179,6 +186,7 @@ impl TelemetryManager {
config: Arc<RwLock<AppConfig>>,
in_memory_config: Arc<RwLock<AppInMemoryConfig>>,
network: Option<Network>,
p2pool_manager: P2poolManager,
) -> Self {
let cancellation_token = CancellationToken::new();
Self {
Expand All @@ -189,6 +197,7 @@ impl TelemetryManager {
cancellation_token,
node_network: network,
in_memory_config,
p2pool_manager,
}
}

Expand Down Expand Up @@ -243,6 +252,7 @@ impl TelemetryManager {
let network = self.node_network;
let config_cloned = self.config.clone();
let in_memory_config_cloned = self.in_memory_config.clone();
let p2pool_manager_cloned = self.p2pool_manager.clone();
tokio::spawn(async move {
tokio::select! {
_ = async {
Expand All @@ -251,7 +261,7 @@ impl TelemetryManager {
let telemetry_collection_enabled = config_cloned.read().await.allow_telemetry();
if telemetry_collection_enabled {
let airdrop_access_token_validated = validate_jwt(airdrop_access_token.clone()).await;
let telemetry_data = get_telemetry_data(cpu_miner.clone(), gpu_miner.clone(), node_manager.clone(), config.clone(), network).await;
let telemetry_data = get_telemetry_data(cpu_miner.clone(), gpu_miner.clone(), node_manager.clone(), p2pool_manager_cloned.clone(), config.clone(), network).await;
let airdrop_api_url = in_memory_config_cloned.read().await.airdrop_api_url.clone();
handle_telemetry_data(telemetry_data, airdrop_api_url, airdrop_access_token_validated, window.clone()).await;
}
Expand Down Expand Up @@ -305,6 +315,7 @@ async fn get_telemetry_data(
cpu_miner: Arc<RwLock<CpuMiner>>,
gpu_miner: Arc<RwLock<GpuMiner>>,
node_manager: NodeManager,
p2pool_manager: p2pool_manager::P2poolManager,
config: Arc<RwLock<AppConfig>>,
network: Option<Network>,
) -> Result<TelemetryData, TelemetryManagerError> {
Expand Down Expand Up @@ -336,6 +347,8 @@ async fn get_telemetry_data(
.await
.read_hardware_parameters();

let p2pool_stats = p2pool_manager.stats().await;

let config_guard = config.read().await;
let is_mining_active = is_synced && (cpu.hash_rate > 0.0 || gpu_status.hash_rate > 0);
let cpu_hash_rate = Some(cpu.hash_rate);
Expand All @@ -345,6 +358,29 @@ async fn get_telemetry_data(
let gpu_utilization = hardware_status.gpu.clone().map(|c| c.usage_percentage);
let gpu_make = hardware_status.gpu.clone().map(|c| c.label);
let version = env!("CARGO_PKG_VERSION").to_string();
let gpu_mining_used =
config_guard.gpu_mining_enabled() && gpu_make.is_some() && gpu_hash_rate.is_some();
let cpu_resource_used =
config_guard.cpu_mining_enabled() && cpu_make.is_some() && cpu_hash_rate.is_some();
let resource_used = match (gpu_mining_used, cpu_resource_used) {
(true, true) => TelemetryResource::CpuGpu,
(true, false) => TelemetryResource::Gpu,
(false, true) => TelemetryResource::Cpu,
(false, false) => TelemetryResource::None,
};

let p2pool_gpu_stats_sha3 = p2pool_stats.get("sha3").map(|stats| stats.squad.clone());
let p2pool_cpu_stats_randomx = p2pool_stats.get("randomx").map(|stats| stats.squad.clone());
let p2pool_enabled =
config_guard.p2pool_enabled() && p2pool_manager.is_running().await.unwrap_or(false);
let cpu_tribe_name = p2pool_cpu_stats_randomx
.clone()
.map(|tribe| tribe.name.clone());
let cpu_tribe_id = p2pool_cpu_stats_randomx.map(|tribe| tribe.id.clone());
let gpu_tribe_name = p2pool_gpu_stats_sha3
.clone()
.map(|tribe| tribe.name.clone());
let gpu_tribe_id = p2pool_gpu_stats_sha3.map(|tribe| tribe.id.clone());

Ok(TelemetryData {
app_id: config_guard.anon_id().to_string(),
Expand All @@ -358,8 +394,13 @@ async fn get_telemetry_data(
gpu_make,
gpu_hash_rate,
gpu_utilization,
resource_used: TelemetryResource::Cpu,
resource_used,
version,
p2pool_enabled,
cpu_tribe_name,
cpu_tribe_id,
gpu_tribe_name,
gpu_tribe_id,
})
}

Expand Down

0 comments on commit 33d86b8

Please sign in to comment.