2023-05-04 20:09:26 +03:00
|
|
|
// Copyright 2023 The Grim Developers
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2023-05-17 00:36:59 +03:00
|
|
|
|
2023-07-29 19:08:06 +03:00
|
|
|
use egui::{RichText, Rounding, ScrollArea, vec2};
|
2024-04-22 17:37:58 +03:00
|
|
|
use grin_servers::{DiffBlock, ServerStats};
|
2023-05-18 03:53:38 +03:00
|
|
|
|
2023-06-03 11:22:51 +03:00
|
|
|
use crate::gui::Colors;
|
2023-06-15 23:54:41 +03:00
|
|
|
use crate::gui::icons::{AT, COINS, CUBE_TRANSPARENT, HASH, HOURGLASS_LOW, HOURGLASS_MEDIUM, TIMER};
|
2023-06-21 02:13:47 +03:00
|
|
|
use crate::gui::platform::PlatformCallbacks;
|
2024-04-22 17:37:58 +03:00
|
|
|
use crate::gui::views::{NetworkContent, Root, View};
|
2023-08-03 04:11:25 +03:00
|
|
|
use crate::gui::views::network::types::{NetworkTab, NetworkTabType};
|
2023-05-17 00:36:59 +03:00
|
|
|
use crate::node::Node;
|
|
|
|
|
2023-07-13 03:54:27 +03:00
|
|
|
/// Chain metrics tab content.
|
2023-06-03 11:22:51 +03:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct NetworkMetrics;
|
2023-05-17 00:36:59 +03:00
|
|
|
|
2023-05-18 03:53:38 +03:00
|
|
|
const BLOCK_REWARD: f64 = 60.0;
|
|
|
|
// 1 year is calculated as 365 days and 6 hours (31557600).
|
|
|
|
const YEARLY_SUPPLY: f64 = ((60 * 60 * 24 * 365) + 6 * 60 * 60) as f64;
|
2023-05-17 00:36:59 +03:00
|
|
|
|
2023-05-18 03:53:38 +03:00
|
|
|
impl NetworkTab for NetworkMetrics {
|
2023-06-17 13:23:31 +03:00
|
|
|
fn get_type(&self) -> NetworkTabType {
|
|
|
|
NetworkTabType::Metrics
|
|
|
|
}
|
|
|
|
|
2023-08-03 04:11:25 +03:00
|
|
|
fn ui(&mut self, ui: &mut egui::Ui, _: &mut eframe::Frame, _: &dyn PlatformCallbacks) {
|
2024-05-14 17:36:49 +03:00
|
|
|
// Show an error content when available.
|
|
|
|
let node_err = Node::get_error();
|
|
|
|
if node_err.is_some() {
|
|
|
|
NetworkContent::node_error_ui(ui, node_err.unwrap());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-03 21:17:49 +03:00
|
|
|
// Show message to enable node when it's not running.
|
|
|
|
if !Node::is_running() {
|
2023-07-31 16:23:15 +03:00
|
|
|
NetworkContent::disabled_node_ui(ui);
|
2023-07-03 21:17:49 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show loading spinner when node is stopping.
|
|
|
|
if Node::is_stopping() {
|
2023-08-03 17:38:10 +03:00
|
|
|
NetworkContent::loading_ui(ui, None);
|
2023-07-03 21:17:49 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show message when metrics are not available.
|
2024-05-14 17:36:49 +03:00
|
|
|
let server_stats = Node::get_stats();
|
2023-07-01 21:04:52 +03:00
|
|
|
if server_stats.is_none() || Node::is_restarting()
|
|
|
|
|| server_stats.as_ref().unwrap().diff_stats.height == 0 {
|
2023-08-03 17:38:10 +03:00
|
|
|
NetworkContent::loading_ui(ui, Some(t!("network_metrics.loading")));
|
2023-05-18 03:53:38 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-11 01:02:08 +03:00
|
|
|
ui.add_space(1.0);
|
2024-04-22 17:37:58 +03:00
|
|
|
ui.vertical_centered(|ui| {
|
|
|
|
View::max_width_ui(ui, Root::SIDE_PANEL_WIDTH * 1.3, |ui| {
|
|
|
|
let stats = server_stats.as_ref().unwrap();
|
|
|
|
// Show emission and difficulty info.
|
|
|
|
info_ui(ui, stats);
|
|
|
|
// Show difficulty adjustment window blocks.
|
|
|
|
blocks_ui(ui, stats);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2023-08-11 01:02:08 +03:00
|
|
|
|
2024-04-22 17:37:58 +03:00
|
|
|
const BLOCK_ITEM_HEIGHT: f32 = 77.0;
|
2023-06-20 00:38:25 +03:00
|
|
|
|
2024-04-22 17:37:58 +03:00
|
|
|
/// Draw emission and difficulty info.
|
|
|
|
fn info_ui(ui: &mut egui::Ui, stats: &ServerStats) {
|
|
|
|
// Show emission info.
|
|
|
|
View::sub_title(ui, format!("{} {}", COINS, t!("network_metrics.emission")));
|
|
|
|
ui.columns(3, |columns| {
|
|
|
|
let supply = stats.header_stats.height as f64 * BLOCK_REWARD;
|
|
|
|
let rate = (YEARLY_SUPPLY * 100.0) / supply;
|
|
|
|
|
|
|
|
columns[0].vertical_centered(|ui| {
|
|
|
|
View::rounded_box(ui,
|
|
|
|
format!("{}ツ", BLOCK_REWARD),
|
|
|
|
t!("network_metrics.reward"),
|
|
|
|
[true, false, true, false]);
|
|
|
|
});
|
|
|
|
columns[1].vertical_centered(|ui| {
|
|
|
|
View::rounded_box(ui,
|
|
|
|
format!("{:.2}%", rate),
|
|
|
|
t!("network_metrics.inflation"),
|
|
|
|
[false, false, false, false]);
|
|
|
|
});
|
|
|
|
columns[2].vertical_centered(|ui| {
|
|
|
|
View::rounded_box(ui,
|
|
|
|
supply.to_string(),
|
|
|
|
t!("network_metrics.supply"),
|
|
|
|
[false, true, false, true]);
|
2023-05-18 03:53:38 +03:00
|
|
|
});
|
2024-04-22 17:37:58 +03:00
|
|
|
});
|
|
|
|
ui.add_space(5.0);
|
2023-05-18 03:53:38 +03:00
|
|
|
|
2024-04-22 17:37:58 +03:00
|
|
|
// Show difficulty adjustment window info.
|
|
|
|
let difficulty_title = t!(
|
2023-06-02 02:05:34 +03:00
|
|
|
"network_metrics.difficulty_window",
|
|
|
|
"size" => stats.diff_stats.window_size
|
|
|
|
);
|
2024-04-22 17:37:58 +03:00
|
|
|
View::sub_title(ui, format!("{} {}", HOURGLASS_MEDIUM, difficulty_title));
|
|
|
|
ui.columns(3, |columns| {
|
|
|
|
columns[0].vertical_centered(|ui| {
|
|
|
|
View::rounded_box(ui,
|
|
|
|
stats.diff_stats.height.to_string(),
|
|
|
|
t!("network_node.height"),
|
|
|
|
[true, false, true, false]);
|
|
|
|
});
|
|
|
|
columns[1].vertical_centered(|ui| {
|
|
|
|
View::rounded_box(ui,
|
|
|
|
format!("{}s", stats.diff_stats.average_block_time),
|
|
|
|
t!("network_metrics.block_time"),
|
|
|
|
[false, false, false, false]);
|
|
|
|
});
|
|
|
|
columns[2].vertical_centered(|ui| {
|
|
|
|
View::rounded_box(ui,
|
|
|
|
stats.diff_stats.average_difficulty.to_string(),
|
|
|
|
t!("network_node.difficulty"),
|
|
|
|
[false, true, false, true]);
|
2023-05-18 03:53:38 +03:00
|
|
|
});
|
2024-04-22 17:37:58 +03:00
|
|
|
});
|
|
|
|
}
|
2023-05-18 03:53:38 +03:00
|
|
|
|
2024-04-22 17:37:58 +03:00
|
|
|
/// Draw difficulty adjustment window blocks content.
|
|
|
|
fn blocks_ui(ui: &mut egui::Ui, stats: &ServerStats) {
|
|
|
|
let blocks_size = stats.diff_stats.last_blocks.len();
|
|
|
|
ui.add_space(4.0);
|
|
|
|
ScrollArea::vertical()
|
|
|
|
.id_source("difficulty_scroll")
|
|
|
|
.auto_shrink([false; 2])
|
|
|
|
.stick_to_bottom(true)
|
|
|
|
.show_rows(
|
|
|
|
ui,
|
|
|
|
BLOCK_ITEM_HEIGHT - 1.0,
|
|
|
|
blocks_size,
|
|
|
|
|ui, row_range| {
|
|
|
|
for index in row_range {
|
|
|
|
// Add space before the first item.
|
|
|
|
if index == 0 {
|
|
|
|
ui.add_space(4.0);
|
2023-05-23 01:46:42 +03:00
|
|
|
}
|
2024-04-22 17:37:58 +03:00
|
|
|
let db = stats.diff_stats.last_blocks.get(index).unwrap();
|
|
|
|
block_item_ui(ui, db, View::item_rounding(index, blocks_size, false));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2023-05-18 03:53:38 +03:00
|
|
|
}
|
|
|
|
|
2023-07-13 03:54:27 +03:00
|
|
|
/// Draw block difficulty item.
|
2023-07-29 19:08:06 +03:00
|
|
|
fn block_item_ui(ui: &mut egui::Ui, db: &DiffBlock, rounding: Rounding) {
|
2023-07-17 02:04:08 +03:00
|
|
|
let mut rect = ui.available_rect_before_wrap();
|
|
|
|
rect.set_height(BLOCK_ITEM_HEIGHT);
|
|
|
|
ui.allocate_ui_at_rect(rect, |ui| {
|
|
|
|
ui.horizontal(|ui| {
|
|
|
|
ui.add_space(6.0);
|
|
|
|
ui.vertical(|ui| {
|
|
|
|
// Draw round background.
|
|
|
|
rect.min += vec2(8.0, 0.0);
|
2024-04-20 14:25:03 +03:00
|
|
|
rect.max -= vec2(8.0, 0.0);
|
2023-07-29 19:08:06 +03:00
|
|
|
ui.painter().rect(rect, rounding, Colors::WHITE, View::ITEM_STROKE);
|
2023-07-17 02:04:08 +03:00
|
|
|
|
|
|
|
ui.add_space(2.0);
|
2023-05-18 03:53:38 +03:00
|
|
|
|
2023-06-20 00:38:25 +03:00
|
|
|
// Draw block hash.
|
2023-07-17 02:04:08 +03:00
|
|
|
ui.horizontal(|ui| {
|
|
|
|
ui.add_space(5.0);
|
|
|
|
ui.label(RichText::new(format!("{} {}", HASH, db.block_hash))
|
|
|
|
.color(Colors::BLACK)
|
|
|
|
.size(17.0));
|
|
|
|
});
|
2023-06-20 00:38:25 +03:00
|
|
|
// Draw block difficulty and height.
|
2023-07-17 02:04:08 +03:00
|
|
|
ui.horizontal(|ui| {
|
|
|
|
ui.add_space(6.0);
|
|
|
|
let diff_text = format!("{} {} {} {}",
|
|
|
|
CUBE_TRANSPARENT,
|
|
|
|
db.difficulty,
|
|
|
|
AT,
|
|
|
|
db.block_height);
|
|
|
|
ui.label(RichText::new(diff_text).color(Colors::TITLE).size(16.0));
|
|
|
|
});
|
2023-06-20 00:38:25 +03:00
|
|
|
// Draw block date.
|
2023-07-17 02:04:08 +03:00
|
|
|
ui.horizontal(|ui| {
|
|
|
|
ui.add_space(6.0);
|
2023-08-18 15:43:06 +03:00
|
|
|
let block_time = View::format_time(db.time as i64);
|
2023-07-17 02:04:08 +03:00
|
|
|
ui.label(RichText::new(format!("{} {}s {} {}",
|
|
|
|
TIMER,
|
|
|
|
db.duration,
|
|
|
|
HOURGLASS_LOW,
|
2023-08-18 15:43:06 +03:00
|
|
|
block_time))
|
2023-07-17 02:04:08 +03:00
|
|
|
.color(Colors::GRAY)
|
|
|
|
.size(16.0));
|
|
|
|
});
|
2023-06-20 00:38:25 +03:00
|
|
|
|
2023-05-18 03:53:38 +03:00
|
|
|
ui.add_space(2.0);
|
|
|
|
});
|
2024-04-20 14:25:03 +03:00
|
|
|
ui.add_space(8.0);
|
2023-05-18 21:08:23 +03:00
|
|
|
});
|
2023-05-18 03:53:38 +03:00
|
|
|
});
|
2023-05-17 00:36:59 +03:00
|
|
|
}
|