Skip to content

Commit

Permalink
fix: add more stats
Browse files Browse the repository at this point in the history
  • Loading branch information
stringhandler committed Oct 17, 2024
1 parent 10f41c9 commit 4e36f4d
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 59 deletions.
14 changes: 11 additions & 3 deletions Cargo.lock

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

16 changes: 9 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ name = "sha_p2pool"
version = "0.3.4"

[dependencies]
minotari_app_grpc = { git = "https://github.com/tari-project/tari.git" , rev = "2d0ccb121bb13990cac422fe72945cced06c1f74"}
minotari_node_grpc_client = { git = "https://github.com/tari-project/tari.git" , rev = "2d0ccb121bb13990cac422fe72945cced06c1f74"}
tari_common_types = { git = "https://github.com/tari-project/tari.git", rev = "2d0ccb121bb13990cac422fe72945cced06c1f74" }
tari_common = { git = "https://github.com/tari-project/tari.git", rev = "2d0ccb121bb13990cac422fe72945cced06c1f74" }
tari_core = { git = "https://github.com/tari-project/tari.git", rev = "2d0ccb121bb13990cac422fe72945cced06c1f74" }
tari_script = { git = "https://github.com/tari-project/tari.git" , rev = "2d0ccb121bb13990cac422fe72945cced06c1f74"}
tari_shutdown = { git = "https://github.com/tari-project/tari.git", rev = "2d0ccb121bb13990cac422fe72945cced06c1f74" }
minotari_app_grpc = {git = "https://github.com/tari-project/tari.git", rev = "2d0ccb121bb13990cac422fe72945cced06c1f74"}
minotari_node_grpc_client = {git = "https://github.com/tari-project/tari.git", rev = "2d0ccb121bb13990cac422fe72945cced06c1f74"}
tari_common = {git = "https://github.com/tari-project/tari.git", rev = "2d0ccb121bb13990cac422fe72945cced06c1f74"}
tari_common_types = {git = "https://github.com/tari-project/tari.git", rev = "2d0ccb121bb13990cac422fe72945cced06c1f74"}
tari_core = {git = "https://github.com/tari-project/tari.git", rev = "2d0ccb121bb13990cac422fe72945cced06c1f74"}
tari_crypto = "0.21.0"
tari_script = {git = "https://github.com/tari-project/tari.git", rev = "2d0ccb121bb13990cac422fe72945cced06c1f74"}
tari_shutdown = {git = "https://github.com/tari-project/tari.git", rev = "2d0ccb121bb13990cac422fe72945cced06c1f74"}
tari_utilities = {version = "0.8", features = ["borsh"]}

anyhow = "1.0"
Expand All @@ -24,6 +24,8 @@ digest = "0.10.7"
dirs = "4.0.0"
hex = "0.4.3"
hickory-resolver = {version = "*", features = ["dns-over-rustls"]}
human_format = "1.1.0"
humantime = "2.1.0"
itertools = "0.13.0"
lazy_static = "1.5.0"
libp2p = {version = "0.54.0", features = [
Expand Down
5 changes: 5 additions & 0 deletions src/server/grpc/p2pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,17 @@ where S: ShareChain
new_tip_block.fix_hash();

if let Some(miner_data) = response.miner_data.as_mut() {
let _ = self
.stats_broadcast
.send_network_difficulty(pow_algo, Difficulty::from_u64(miner_data.target_difficulty).unwrap());
// what happens p2pool difficulty > base chain diff
if target_difficulty.as_u64() < miner_data.target_difficulty {
miner_data.target_difficulty = target_difficulty.as_u64();
}
}

let _ = self.stats_broadcast.send_target_difficulty(pow_algo, target_difficulty);

// save template
let mut list_of_template_write_lock = self.list_of_templates.write().await;
list_of_template_write_lock.push_back(tari_hash.clone());
Expand Down
179 changes: 130 additions & 49 deletions src/server/http/stats_collector.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::time::Duration;

use human_format::Formatter;
use log::{error, info};
use tari_core::proof_of_work::{Difficulty, PowAlgorithm};
use tari_shutdown::ShutdownSignal;
Expand All @@ -18,7 +21,10 @@ pub(crate) struct StatsCollector {
miner_rejected: u64,
pool_accepted: u64,
pool_rejected: u64,
network_difficulty: Difficulty,
sha_network_difficulty: Difficulty,
sha_target_difficulty: Difficulty,
randomx_network_difficulty: Difficulty,
randomx_target_difficulty: Difficulty,
sha3x_chain_height: u64,
sha3x_chain_length: u64,
randomx_chain_height: u64,
Expand Down Expand Up @@ -48,7 +54,10 @@ impl StatsCollector {
total_peers: 0,
total_grey_list: 0,
total_black_list: 0,
network_difficulty: Difficulty::min(),
sha_network_difficulty: Difficulty::min(),
randomx_network_difficulty: Difficulty::min(),
sha_target_difficulty: Difficulty::min(),
randomx_target_difficulty: Difficulty::min(),
}
}

Expand Down Expand Up @@ -95,6 +104,30 @@ impl StatsCollector {
self.total_grey_list = total_grey_list;
self.total_black_list = total_black_list;
},
StatData::TargetDifficultyChanged {
target_difficulty,
pow_algo,
timestamp,
} => match pow_algo {
PowAlgorithm::Sha3x => {
self.sha_target_difficulty = target_difficulty;
},
PowAlgorithm::RandomX => {
self.randomx_target_difficulty = target_difficulty;
},
},
StatData::NetworkDifficultyChanged {
network_difficulty,
pow_algo,
timestamp,
} => match pow_algo {
PowAlgorithm::Sha3x => {
self.sha_network_difficulty = network_difficulty;
},
PowAlgorithm::RandomX => {
self.randomx_network_difficulty = network_difficulty;
},
},
}
}

Expand All @@ -104,57 +137,67 @@ impl StatsCollector {

loop {
tokio::select! {
_ = self.shutdown_signal.wait() => {
break;
},
_ = stats_report_timer.tick() => {
info!(target: LOG_TARGET,
"========= Chains: Rx {}..{}, Sha3 {}..{}. Miner(A/R): {}/{}. Pool(A/R) {}/{}. Peers(a/g/b) {}/{}/{} ==== ",
self.randomx_chain_height.saturating_sub(self.randomx_chain_length),
self.randomx_chain_height,
self.sha3x_chain_height.saturating_sub(self.sha3x_chain_length),
self.sha3x_chain_height,
self.miner_accepted,
self.miner_rejected,
self.pool_accepted,
self.pool_rejected,
self.total_peers,
self.total_grey_list,
self.total_black_list
);
},
res = self.request_rx.recv() => {
match res {
Some(StatsRequest::GetStats(_pow, _tx)) => {
todo!();
// let _ = tx.send(hashrate);
},
None => {
_ = self.shutdown_signal.wait() => {
break;
}
}
},
res = self.stats_broadcast_receiver.recv() => {
match res {
Ok(sample) => {
if self.first_stat_received.is_none() {
self.first_stat_received = Some(sample.timestamp());
},
_ = stats_report_timer.tick() => {
let formatter = Formatter::new();

info!(target: LOG_TARGET,
"========= Uptime: {}. Chains: Rx {}..{}, Sha3 {}..{}. Difficulty (Target/Network): Rx: {}/{} Sha3x: {}/{} Miner(A/R): {}/{}. Pool(A/R) {}/{}. Peers(a/g/b) {}/{}/{} ==== ",
humantime::format_duration(Duration::from_secs(
EpochTime::now().as_u64().checked_sub(
self.first_stat_received.unwrap_or_else(|| EpochTime::now()).as_u64())
.unwrap_or_default())),
self.randomx_chain_height.saturating_sub(self.randomx_chain_length.saturating_sub(1)),
self.randomx_chain_height,
self.sha3x_chain_height.saturating_sub(self.sha3x_chain_length.saturating_sub(1)),
self.sha3x_chain_height,
formatter.format(self.randomx_target_difficulty.as_u64() as f64 ),
formatter.format( self.randomx_network_difficulty.as_u64() as f64),
formatter.format(self.sha_target_difficulty.as_u64() as f64),
formatter.format(self.sha_network_difficulty.as_u64() as f64),
self.miner_accepted,
self.miner_rejected,
self.pool_accepted,
self.pool_rejected,
self.total_peers,
self.total_grey_list,
self.total_black_list
);
},
res = self.request_rx.recv() => {
match res {
Some(StatsRequest::GetStats(_pow, _tx)) => {
todo!();
// let _ = tx.send(hashrate);
},
None => {
break;
}
}
self.handle_stat(sample);
// Expect 2 samples per second per device
// let entry = self.hashrate_samples.entry(sample.device_id).or_insert_with(|| VecDeque::with_capacity(181));
// if entry.len() > 180 {
// entry.pop_front();
// }
// entry.push_back(sample);
},
Err(e) => {
error!(target: LOG_TARGET, "Error receiving hashrate sample: {:?}", e);
break;
}
}
res = self.stats_broadcast_receiver.recv() => {
match res {
Ok(sample) => {
if self.first_stat_received.is_none() {
self.first_stat_received = Some(sample.timestamp());
}
}
self.handle_stat(sample);
// Expect 2 samples per second per device
// let entry = self.hashrate_samples.entry(sample.device_id).or_insert_with(|| VecDeque::with_capacity(181));
// if entry.len() > 180 {
// entry.pop_front();
// }
// entry.push_back(sample);
},
Err(e) => {
error!(target: LOG_TARGET, "Error receiving hashrate sample: {:?}", e);
break;
}
}
}
}
}
Ok(())
}
Expand All @@ -175,6 +218,16 @@ pub(crate) struct ChainStats {

#[derive(Clone)]
pub(crate) enum StatData {
TargetDifficultyChanged {
target_difficulty: Difficulty,
pow_algo: PowAlgorithm,
timestamp: EpochTime,
},
NetworkDifficultyChanged {
network_difficulty: Difficulty,
pow_algo: PowAlgorithm,
timestamp: EpochTime,
},
ChainStats {
chain: ChainStats,
timestamp: EpochTime,
Expand Down Expand Up @@ -220,6 +273,8 @@ impl StatData {
StatData::PoolBlockRejected { timestamp, .. } => *timestamp,
StatData::ChainChanged { timestamp, .. } => *timestamp,
StatData::NewPeer { timestamp, .. } => *timestamp,
StatData::TargetDifficultyChanged { timestamp, .. } => *timestamp,
StatData::NetworkDifficultyChanged { timestamp, .. } => *timestamp,
}
}
}
Expand Down Expand Up @@ -310,4 +365,30 @@ impl StatsBroadcastClient {
timestamp: EpochTime::now(),
})
}

pub fn send_target_difficulty(
&self,
pow_algo: PowAlgorithm,
target_difficulty: Difficulty,
) -> Result<(), anyhow::Error> {
let data = StatData::TargetDifficultyChanged {
target_difficulty,
pow_algo,
timestamp: EpochTime::now(),
};
self.broadcast(data)
}

pub fn send_network_difficulty(
&self,
pow_algo: PowAlgorithm,
network_difficulty: Difficulty,
) -> Result<(), anyhow::Error> {
let data = StatData::NetworkDifficultyChanged {
network_difficulty,
pow_algo,
timestamp: EpochTime::now(),
};
self.broadcast(data)
}
}

0 comments on commit 4e36f4d

Please sign in to comment.