diff --git a/servers/src/common/stats.rs b/servers/src/common/stats.rs index 2fa74147..02a96688 100644 --- a/servers/src/common/stats.rs +++ b/servers/src/common/stats.rs @@ -20,6 +20,7 @@ use std::sync::Arc; use std::time::SystemTime; use crate::core::consensus::graph_weight; +use crate::core::core::hash::Hash; use chrono::prelude::*; @@ -118,8 +119,10 @@ pub struct DiffStats { /// Last n blocks for difficulty calculation purposes #[derive(Clone, Debug)] pub struct DiffBlock { - /// Block number (can be negative for a new chain) - pub block_number: i64, + /// Block height (can be negative for a new chain) + pub block_height: i64, + /// Block hash (may be synthetic for a new chain) + pub block_hash: Hash, /// Block network difficulty pub difficulty: u64, /// Time block was found (epoch seconds) diff --git a/servers/src/grin/server.rs b/servers/src/grin/server.rs index 976933a6..6f267de4 100644 --- a/servers/src/grin/server.rs +++ b/servers/src/grin/server.rs @@ -27,6 +27,7 @@ use crate::common::adapters::{ }; use crate::common::stats::{DiffBlock, DiffStats, PeerStats, ServerStateInfo, ServerStats}; use crate::common::types::{Error, ServerConfig, StratumServerConfig, SyncState, SyncStatus}; +use crate::core::core::hash::{Hashed, ZERO_HASH}; use crate::core::core::verifier_cache::{LruVerifierCache, VerifierCache}; use crate::core::{consensus, genesis, global, pow}; use crate::grin::{dandelion_monitor, seed, sync}; @@ -368,29 +369,39 @@ impl Server { let last_blocks: Vec = global::difficulty_data_to_vector(self.chain.difficulty_iter()) .into_iter() - .take(consensus::DIFFICULTY_ADJUST_WINDOW as usize) .collect(); - let mut last_time = last_blocks[0].timestamp; let tip_height = self.chain.head().unwrap().height as i64; - let earliest_block_height = tip_height as i64 - last_blocks.len() as i64; - let mut i = 1; + let mut height = tip_height as i64 - last_blocks.len() as i64 + 1; let diff_entries: Vec = last_blocks - .iter() - .skip(1) - .map(|n| { - let dur = n.timestamp - last_time; - let height = earliest_block_height + i; - i += 1; - last_time = n.timestamp; + .windows(2) + .map(|pair| { + let prev = &pair[0]; + let next = &pair[1]; + + height += 1; + + // Use header hash if real header. + // Default to "zero" hash if synthetic header_info. + let hash = if height >= 0 { + if let Ok(header) = self.chain.get_header_by_height(height as u64) { + header.hash() + } else { + ZERO_HASH + } + } else { + ZERO_HASH + }; + DiffBlock { - block_number: height, - difficulty: n.difficulty.to_num(), - time: n.timestamp, - duration: dur, - secondary_scaling: n.secondary_scaling, - is_secondary: n.is_secondary, + block_height: height, + block_hash: hash, + difficulty: next.difficulty.to_num(), + time: next.timestamp, + duration: next.timestamp - prev.timestamp, + secondary_scaling: next.secondary_scaling, + is_secondary: next.is_secondary, } }) .collect(); @@ -398,7 +409,7 @@ impl Server { let block_time_sum = diff_entries.iter().fold(0, |sum, t| sum + t.duration); let block_diff_sum = diff_entries.iter().fold(0, |sum, d| sum + d.difficulty); DiffStats { - height: tip_height as u64, + height: height as u64, last_blocks: diff_entries, average_block_time: block_time_sum / (consensus::DIFFICULTY_ADJUST_WINDOW - 1), average_difficulty: block_diff_sum / (consensus::DIFFICULTY_ADJUST_WINDOW - 1), diff --git a/src/bin/tui/mining.rs b/src/bin/tui/mining.rs index 01b8c5c7..74135d3c 100644 --- a/src/bin/tui/mining.rs +++ b/src/bin/tui/mining.rs @@ -99,7 +99,8 @@ impl TableViewItem for WorkerStats { } #[derive(Copy, Clone, PartialEq, Eq, Hash)] enum DiffColumn { - BlockNumber, + Height, + Hash, PoWType, Difficulty, SecondaryScaling, @@ -110,7 +111,8 @@ enum DiffColumn { impl DiffColumn { fn _as_str(&self) -> &str { match *self { - DiffColumn::BlockNumber => "Block Number", + DiffColumn::Height => "Height", + DiffColumn::Hash => "Hash", DiffColumn::PoWType => "Type", DiffColumn::Difficulty => "Network Difficulty", DiffColumn::SecondaryScaling => "Sec. Scaling", @@ -130,7 +132,8 @@ impl TableViewItem for DiffBlock { }; match column { - DiffColumn::BlockNumber => self.block_number.to_string(), + DiffColumn::Height => self.block_height.to_string(), + DiffColumn::Hash => self.block_hash.to_string(), DiffColumn::PoWType => pow_type, DiffColumn::Difficulty => self.difficulty.to_string(), DiffColumn::SecondaryScaling => self.secondary_scaling.to_string(), @@ -144,7 +147,8 @@ impl TableViewItem for DiffBlock { Self: Sized, { match column { - DiffColumn::BlockNumber => Ordering::Equal, + DiffColumn::Height => Ordering::Equal, + DiffColumn::Hash => Ordering::Equal, DiffColumn::PoWType => Ordering::Equal, DiffColumn::Difficulty => Ordering::Equal, DiffColumn::SecondaryScaling => Ordering::Equal, @@ -264,9 +268,8 @@ impl TUIStatusListener for TUIMiningView { ); let diff_table_view = TableView::::new() - .column(DiffColumn::BlockNumber, "Block Number", |c| { - c.width_percent(15) - }) + .column(DiffColumn::Height, "Height", |c| c.width_percent(10)) + .column(DiffColumn::Hash, "Hash", |c| c.width_percent(10)) .column(DiffColumn::PoWType, "Type", |c| c.width_percent(10)) .column(DiffColumn::Difficulty, "Network Difficulty", |c| { c.width_percent(15)