mirror of
https://github.com/transatoshi-mw/grin-explorer.git
synced 2025-10-21 13:33:41 +00:00
adding block size info
This commit is contained in:
15
src/data.rs
15
src/data.rs
@@ -1,5 +1,18 @@
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
|
||||
// Weights
|
||||
pub const KERNEL_WEIGHT: f64 = 3.0;
|
||||
pub const INPUT_WEIGHT: f64 = 1.0;
|
||||
pub const OUTPUT_WEIGHT: f64 = 21.0;
|
||||
|
||||
|
||||
// Sizes in bytes
|
||||
pub const KERNEL_SIZE: u64 = 1 + 8 + 8 + 33 + 64;
|
||||
pub const INPUT_SIZE: u64 = 1 + 33;
|
||||
pub const OUTPUT_SIZE: u64 = 674 + 33 + 1;
|
||||
|
||||
|
||||
// Dashboard data
|
||||
#[derive(Debug)]
|
||||
pub struct Dashboard {
|
||||
@@ -76,6 +89,7 @@ pub struct Block {
|
||||
pub time: String,
|
||||
pub version: String,
|
||||
pub weight: f64,
|
||||
pub size: String,
|
||||
pub fees: f64,
|
||||
pub kernels: Vec<(String, String, String)>,
|
||||
pub inputs: Vec<String>,
|
||||
@@ -94,6 +108,7 @@ impl Block {
|
||||
time: String::new(),
|
||||
version: String::new(),
|
||||
weight: 0.0,
|
||||
size: String::new(),
|
||||
fees: 0.0,
|
||||
kernels: Vec::new(),
|
||||
inputs: Vec::new(),
|
||||
|
11
src/main.rs
11
src/main.rs
@@ -615,6 +615,15 @@ fn block_fees(count: usize, blocks: &State<Arc<Mutex<Vec<Block>>>>) -> String {
|
||||
}
|
||||
|
||||
|
||||
#[get("/rpc/block/size?<count>")]
|
||||
fn block_size(count: usize, blocks: &State<Arc<Mutex<Vec<Block>>>>) -> String {
|
||||
let data = blocks.lock().unwrap();
|
||||
|
||||
data[count].size.clone()
|
||||
|
||||
}
|
||||
|
||||
|
||||
#[get("/rpc/block/weight?<count>")]
|
||||
fn block_weight(count: usize, blocks: &State<Arc<Mutex<Vec<Block>>>>) -> String {
|
||||
let data = blocks.lock().unwrap();
|
||||
@@ -694,7 +703,7 @@ async fn main() {
|
||||
network_difficulty, mempool_txns, mempool_stem, txns_count_1h,
|
||||
txns_count_24h, block_list, block_link, block_link_color,
|
||||
block_time, block_txns, block_inputs, block_outputs, block_fees,
|
||||
block_weight, block_details_by_height, block_header_by_hash,
|
||||
block_size, block_weight, block_details_by_height, block_header_by_hash,
|
||||
soft_supply, production_cost, reward_ratio, breakeven_cost,
|
||||
last_block_age, block_list_by_height, block_list_index, search, kernel,
|
||||
output, api_owner, api_foreign, stats])
|
||||
|
@@ -11,13 +11,8 @@ use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
use crate::data::Block;
|
||||
use crate::data::Dashboard;
|
||||
use crate::data::ExplorerConfig;
|
||||
use crate::data::Kernel;
|
||||
use crate::data::Output;
|
||||
use crate::data::Statistics;
|
||||
use crate::data::Transactions;
|
||||
use crate::data::{Block, Dashboard, ExplorerConfig, Kernel, Output, Statistics, Transactions};
|
||||
use crate::data::{KERNEL_WEIGHT, INPUT_WEIGHT, OUTPUT_WEIGHT, KERNEL_SIZE, INPUT_SIZE, OUTPUT_SIZE};
|
||||
|
||||
|
||||
// Static explorer config structure
|
||||
@@ -387,12 +382,6 @@ pub async fn get_mining_stats(dashboard: Arc<Mutex<Dashboard>>) -> Result<(), an
|
||||
// Collecting block data for recent blocks (block_list page).
|
||||
pub async fn get_block_list_data(height: &String, block: &mut Block)
|
||||
-> Result<(), anyhow::Error> {
|
||||
// Max block weight is 40000
|
||||
// One unit of weight is 32 bytes
|
||||
let kernel_weight = 3.0;
|
||||
let input_weight = 1.0;
|
||||
let output_weight = 21.0;
|
||||
|
||||
if height.is_empty() == false {
|
||||
let params = &format!("[{}, null, null]", height)[..];
|
||||
let resp = call("get_block", params, "1", "foreign").await?;
|
||||
@@ -419,17 +408,17 @@ pub async fn get_block_list_data(height: &String, block: &mut Block)
|
||||
let fee = kernel["fee"].to_string().parse::<f64>().unwrap();
|
||||
|
||||
block.fees += fee;
|
||||
block.weight += kernel_weight;
|
||||
block.weight += KERNEL_WEIGHT;
|
||||
block.ker_len = block.ker_len + 1;
|
||||
}
|
||||
|
||||
for _input in resp["result"]["Ok"]["inputs"].as_array().unwrap() {
|
||||
block.weight += input_weight;
|
||||
block.weight += INPUT_WEIGHT;
|
||||
block.in_len = block.in_len + 1;
|
||||
}
|
||||
|
||||
for _output in resp["result"]["Ok"]["outputs"].as_array().unwrap() {
|
||||
block.weight += output_weight;
|
||||
block.weight += OUTPUT_WEIGHT;
|
||||
block.out_len = block.out_len + 1;
|
||||
}
|
||||
} else {
|
||||
@@ -439,6 +428,16 @@ pub async fn get_block_list_data(height: &String, block: &mut Block)
|
||||
|
||||
block.weight = format!("{:.2}", block.weight / 40000.0 * 100.0).parse::<f64>().unwrap();
|
||||
|
||||
let block_size = ((block.ker_len * KERNEL_SIZE) + (block.in_len * INPUT_SIZE) + (block.out_len * OUTPUT_SIZE)) as f64;
|
||||
|
||||
if block_size > 1000000.0 {
|
||||
block.size = format!("{:.2} MB", block_size / 1000.0 / 1000.0);
|
||||
} else if block_size > 1000.0 {
|
||||
block.size = format!("{:.2} KB", block_size / 1000.0);
|
||||
} else {
|
||||
block.size = format!("{} B", block_size);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -446,12 +445,6 @@ pub async fn get_block_list_data(height: &String, block: &mut Block)
|
||||
// Collecting block data.
|
||||
pub async fn get_block_data(height: &str, block: &mut Block)
|
||||
-> Result<(), anyhow::Error> {
|
||||
// Max block weight is 40000
|
||||
// One unit of weight is 32 bytes
|
||||
let kernel_weight = 3.0;
|
||||
let input_weight = 1.0;
|
||||
let output_weight = 21.0;
|
||||
|
||||
if height.is_empty() == false {
|
||||
let params = &format!("[{}, null, null]", height)[..];
|
||||
|
||||
@@ -473,18 +466,18 @@ pub async fn get_block_data(height: &str, block: &mut Block)
|
||||
kernel["features"].as_str().unwrap().to_string(),
|
||||
(fee / 1000000000.0).to_string()));
|
||||
block.fees += fee;
|
||||
block.weight += kernel_weight;
|
||||
block.weight += KERNEL_WEIGHT;
|
||||
}
|
||||
|
||||
for input in resp["result"]["Ok"]["inputs"].as_array().unwrap() {
|
||||
block.inputs.push(input.as_str().unwrap().to_string());
|
||||
block.weight += input_weight;
|
||||
block.weight += INPUT_WEIGHT;
|
||||
}
|
||||
|
||||
for output in resp["result"]["Ok"]["outputs"].as_array().unwrap() {
|
||||
block.outputs.push((output["commit"].as_str().unwrap().to_string(),
|
||||
output["output_type"].as_str().unwrap().to_string()));
|
||||
block.weight += output_weight;
|
||||
block.weight += OUTPUT_WEIGHT;
|
||||
}
|
||||
|
||||
block.weight = format!("{:.2}", block.weight / 40000.0 * 100.0).parse::<f64>().unwrap();
|
||||
@@ -492,6 +485,16 @@ pub async fn get_block_data(height: &str, block: &mut Block)
|
||||
block.in_len = block.inputs.iter().count() as u64;
|
||||
block.out_len = block.outputs.iter().count() as u64;
|
||||
block.raw_data = serde_json::to_string_pretty(&resp).unwrap();
|
||||
|
||||
let block_size = ((block.ker_len * KERNEL_SIZE) + (block.in_len * INPUT_SIZE) + (block.out_len * OUTPUT_SIZE)) as f64;
|
||||
|
||||
if block_size > 1000000.0 {
|
||||
block.size = format!("{:.2} MB", block_size / 1000.0 / 1000.0);
|
||||
} else if block_size > 1000.0 {
|
||||
block.size = format!("{:.2} KB", block_size / 1000.0);
|
||||
} else {
|
||||
block.size = format!("{} B", block_size);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -23,6 +23,11 @@
|
||||
<div class="value-text text-end">ツ {{ block.fees / 1000000000.0 }}</div>
|
||||
</div>
|
||||
<br>
|
||||
<div class="d-flex justify-content-between">
|
||||
<div class="value-text">Size </div>
|
||||
<div class="value-text text-end">{{ block.size }}</div>
|
||||
</div>
|
||||
<br>
|
||||
<div class="d-flex justify-content-between">
|
||||
<div class="value-text">Weight </div>
|
||||
<div class="value-text text-end">{{ block.weight }} %</div>
|
||||
|
@@ -48,6 +48,13 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card border-bottom-0 border-end-0 rounded-0">
|
||||
<div class="card-body">
|
||||
<div class="darkorange-text">
|
||||
SIZE
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card border-bottom-0 border-end-0 rounded-0">
|
||||
<div class="card-body">
|
||||
<div class="darkorange-text">
|
||||
@@ -118,6 +125,15 @@
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
<div class="card border-bottom-0 border-end-0 rounded-0">
|
||||
<div class="card-body">
|
||||
{% if route == "block_list_by_height" %}
|
||||
<div class="value-text">{{ blocks[i].size }}</div>
|
||||
{% else %}
|
||||
<div class="value-text" hx-get="/rpc/block/size" hx-vals='{"count": "{{ i }}"}' hx-trigger="load, every 10s"></div>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
<div class="card border-bottom-0 border-end-0 rounded-0">
|
||||
<div class="card-body">
|
||||
{% if route == "block_list_by_height" %}
|
||||
@@ -195,6 +211,15 @@
|
||||
{% endif %}
|
||||
</div>
|
||||
<br>
|
||||
<div class="d-flex justify-content-between">
|
||||
<div class="value-text">Size</div>
|
||||
{% if route == "block_list_by_height" %}
|
||||
<div class="value-text">{{ blocks[i].size }}</div>
|
||||
{% else %}
|
||||
<div class="value-text text-end" hx-get="/rpc/block/size" hx-vals='{"count": "{{ i }}"}' hx-trigger="load, every 10s"></div>
|
||||
{% endif %}
|
||||
</div>
|
||||
<br>
|
||||
<div class="d-flex justify-content-between">
|
||||
<div class="value-text">Weight</div>
|
||||
{% if route == "block_list_by_height" %}
|
||||
|
Reference in New Issue
Block a user