diff --git a/grin/src/server.rs b/grin/src/server.rs index 1fd819611..765fdf722 100644 --- a/grin/src/server.rs +++ b/grin/src/server.rs @@ -306,12 +306,10 @@ impl Server { let (time, diff) = n.clone().unwrap(); let dur = time - last_time; let height = earliest_block_height + i + 1; - let index = tip_height - height; i += 1; last_time = time; DiffBlock { block_number: height, - block_index: index, difficulty: diff.into_num(), time: time, duration: dur, diff --git a/grin/src/stats.rs b/grin/src/stats.rs index 4ff5b7dd3..5f682b614 100644 --- a/grin/src/stats.rs +++ b/grin/src/stats.rs @@ -102,8 +102,6 @@ pub struct DiffStats { pub struct DiffBlock { /// Block number (can be negative for a new chain) pub block_number: i64, - /// Ordinal index from current block - pub block_index: i64, /// Block network difficulty pub difficulty: u64, /// Time block was found (epoch seconds) @@ -121,8 +119,10 @@ pub struct PeerStats { pub addr: String, /// version running pub version: u32, - /// version running + /// difficulty repored by peer pub total_difficulty: u64, + /// height reported by peer on ping + pub height: u64, /// direction pub direction: String, } @@ -148,6 +148,7 @@ impl PeerStats { addr: addr, version: peer.info.version, total_difficulty: peer.info.total_difficulty.into_num(), + height: peer.info.height, direction: direction.to_string(), } } diff --git a/p2p/src/handshake.rs b/p2p/src/handshake.rs index 3f6d06ab2..19ca2fc54 100644 --- a/p2p/src/handshake.rs +++ b/p2p/src/handshake.rs @@ -98,6 +98,7 @@ impl Handshake { addr: peer_addr, version: shake.version, total_difficulty: shake.total_difficulty, + height: 0, direction: Direction::Outbound, }; @@ -153,6 +154,7 @@ impl Handshake { addr: extract_ip(&hand.sender_addr.0, &conn), version: hand.version, total_difficulty: hand.total_difficulty, + height: 0, direction: Direction::Inbound, }; diff --git a/p2p/src/peers.rs b/p2p/src/peers.rs index f8d80d19f..42ee96f17 100644 --- a/p2p/src/peers.rs +++ b/p2p/src/peers.rs @@ -637,6 +637,7 @@ impl NetAdapter for Peers { if let Some(peer) = self.get_connected_peer(&addr) { let mut peer = peer.write().unwrap(); peer.info.total_difficulty = diff; + peer.info.height = height; } } } diff --git a/p2p/src/types.rs b/p2p/src/types.rs index b34bc492b..78f0695ce 100644 --- a/p2p/src/types.rs +++ b/p2p/src/types.rs @@ -202,6 +202,7 @@ pub struct PeerInfo { pub version: u32, pub addr: SocketAddr, pub total_difficulty: Difficulty, + pub height: u64, pub direction: Direction, } diff --git a/src/bin/tui/mining.rs b/src/bin/tui/mining.rs index 4ff2b6811..37762cdc6 100644 --- a/src/bin/tui/mining.rs +++ b/src/bin/tui/mining.rs @@ -107,7 +107,6 @@ impl TableViewItem<MiningDeviceColumn> for CuckooMinerDeviceStats { #[derive(Copy, Clone, PartialEq, Eq, Hash)] enum DiffColumn { BlockNumber, - Index, Difficulty, Time, Duration, @@ -117,7 +116,6 @@ impl DiffColumn { fn _as_str(&self) -> &str { match *self { DiffColumn::BlockNumber => "Block Number", - DiffColumn::Index => "Block Index", DiffColumn::Difficulty => "Network Difficulty", DiffColumn::Time => "Block Time", DiffColumn::Duration => "Duration", @@ -132,7 +130,6 @@ impl TableViewItem<DiffColumn> for DiffBlock { match column { DiffColumn::BlockNumber => self.block_number.to_string(), - DiffColumn::Index => self.block_index.to_string(), DiffColumn::Difficulty => self.difficulty.to_string(), DiffColumn::Time => format!("{}", datetime).to_string(), DiffColumn::Duration => format!("{}s", self.duration).to_string(), @@ -145,7 +142,6 @@ impl TableViewItem<DiffColumn> for DiffBlock { { match column { DiffColumn::BlockNumber => Ordering::Equal, - DiffColumn::Index => Ordering::Equal, DiffColumn::Difficulty => Ordering::Equal, DiffColumn::Time => Ordering::Equal, DiffColumn::Duration => Ordering::Equal, @@ -246,16 +242,13 @@ impl TUIStatusListener for TUIMiningView { let diff_table_view = TableView::<DiffBlock, DiffColumn>::new() .column(DiffColumn::BlockNumber, "Block Number", |c| { - c.width_percent(20) - }) - .column(DiffColumn::Index, "Distance from Head", |c| { - c.width_percent(20) + c.width_percent(25) }) .column(DiffColumn::Difficulty, "Network Difficulty", |c| { - c.width_percent(20) + c.width_percent(25) }) - .column(DiffColumn::Time, "Block Time", |c| c.width_percent(20)) - .column(DiffColumn::Duration, "Duration", |c| c.width_percent(20)); + .column(DiffColumn::Time, "Block Time", |c| c.width_percent(25)) + .column(DiffColumn::Duration, "Duration", |c| c.width_percent(25)); let mining_difficulty_view = LinearLayout::new(Orientation::Vertical) .child(diff_status_view) diff --git a/src/bin/tui/peers.rs b/src/bin/tui/peers.rs index 25f91bc8f..24418dbb0 100644 --- a/src/bin/tui/peers.rs +++ b/src/bin/tui/peers.rs @@ -20,7 +20,8 @@ use grin::stats::{PeerStats, ServerStats}; use cursive::Cursive; use cursive::view::View; -use cursive::views::{BoxView, Dialog}; +use cursive::views::{BoxView, Dialog, LinearLayout, TextView}; +use cursive::direction::Orientation; use cursive::traits::*; use tui::table::{TableView, TableViewItem}; @@ -53,7 +54,9 @@ impl TableViewItem<PeerColumn> for PeerStats { match column { PeerColumn::Address => self.addr.clone(), PeerColumn::State => self.state.clone(), - PeerColumn::TotalDifficulty => self.total_difficulty.to_string(), + PeerColumn::TotalDifficulty => { + format!("{} D @ {} H", self.total_difficulty, self.height).to_string() + } PeerColumn::Direction => self.direction.clone(), PeerColumn::Version => self.version.to_string(), } @@ -86,20 +89,50 @@ impl TUIStatusListener for TUIPeerView { c.width_percent(20) }) .column(PeerColumn::Version, "Version", |c| c.width_percent(20)); - let peer_status_view = BoxView::with_full_screen( - Dialog::around(table_view.with_id(TABLE_PEER_STATUS).min_size((50, 20))) - .title("Connected Peers"), + LinearLayout::new(Orientation::Vertical) + .child( + LinearLayout::new(Orientation::Horizontal) + .child(TextView::new("Total Peers: ")) + .child(TextView::new(" ").with_id("peers_total")), + ) + .child( + LinearLayout::new(Orientation::Horizontal) + .child(TextView::new("Longest Chain: ")) + .child(TextView::new(" ").with_id("longest_work_peer")), + ) + .child(TextView::new(" ")) + .child( + Dialog::around(table_view.with_id(TABLE_PEER_STATUS).min_size((50, 20))) + .title("Connected Peers"), + ), ).with_id(VIEW_PEER_SYNC); Box::new(peer_status_view) } fn update(c: &mut Cursive, stats: &ServerStats) { + let lp = stats + .peer_stats + .iter() + .max_by(|x, y| x.total_difficulty.cmp(&y.total_difficulty)); + let lp_str = match lp { + Some(l) => format!( + "{} D @ {} H vs Us: {} D @ {} H", + l.total_difficulty, l.height, stats.head.total_difficulty, stats.head.height + ).to_string(), + None => "".to_string(), + }; let _ = c.call_on_id( TABLE_PEER_STATUS, |t: &mut TableView<PeerStats, PeerColumn>| { t.set_items(stats.peer_stats.clone()); }, ); + let _ = c.call_on_id("peers_total", |t: &mut TextView| { + t.set_content(stats.peer_stats.len().to_string()); + }); + let _ = c.call_on_id("longest_work_peer", |t: &mut TextView| { + t.set_content(lp_str); + }); } } diff --git a/src/bin/tui/status.rs b/src/bin/tui/status.rs index 97b5d024e..c9009129e 100644 --- a/src/bin/tui/status.rs +++ b/src/bin/tui/status.rs @@ -47,6 +47,11 @@ impl TUIStatusListener for TUIStatusView { .child(TextView::new("Chain Height: ")) .child(TextView::new(" ").with_id("chain_height")), ) + .child( + LinearLayout::new(Orientation::Horizontal) + .child(TextView::new("Total Difficulty: ")) + .child(TextView::new(" ").with_id("basic_total_difficulty")), + ) .child( LinearLayout::new(Orientation::Horizontal) .child(TextView::new("------------------------")), @@ -126,6 +131,9 @@ impl TUIStatusListener for TUIStatusView { c.call_on_id("chain_height", |t: &mut TextView| { t.set_content(stats.head.height.to_string()); }); + c.call_on_id("basic_total_difficulty", |t: &mut TextView| { + t.set_content(stats.head.total_difficulty.to_string()); + }); c.call_on_id("basic_mining_config_status", |t: &mut TextView| { t.set_content(basic_mining_config_status); });