diff --git a/src/data.rs b/src/data.rs index 8666de9..71dc5b4 100644 --- a/src/data.rs +++ b/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, @@ -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(), diff --git a/src/main.rs b/src/main.rs index feb8408..5a6aa5c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -615,6 +615,15 @@ fn block_fees(count: usize, blocks: &State>>>) -> String { } +#[get("/rpc/block/size?")] +fn block_size(count: usize, blocks: &State>>>) -> String { + let data = blocks.lock().unwrap(); + + data[count].size.clone() + +} + + #[get("/rpc/block/weight?")] fn block_weight(count: usize, blocks: &State>>>) -> 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]) diff --git a/src/requests.rs b/src/requests.rs index a58e649..ba831f7 100644 --- a/src/requests.rs +++ b/src/requests.rs @@ -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>) -> 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::().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 { @@ -437,7 +426,17 @@ pub async fn get_block_list_data(height: &String, block: &mut Block) } } - block.weight = format!("{:.2}", block.weight / 40000.0 * 100.0).parse::().unwrap(); + block.weight = format!("{:.2}", block.weight / 40000.0 * 100.0).parse::().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::().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); + } } } diff --git a/templates/block_details.html.tera b/templates/block_details.html.tera index 4b53b86..936122e 100644 --- a/templates/block_details.html.tera +++ b/templates/block_details.html.tera @@ -23,6 +23,11 @@
ツ {{ block.fees / 1000000000.0 }}

+
+
Size 
+
{{ block.size }}
+
+
Weight 
{{ block.weight }} %
diff --git a/templates/block_list.html.tera b/templates/block_list.html.tera index f96751e..3ef5c35 100644 --- a/templates/block_list.html.tera +++ b/templates/block_list.html.tera @@ -48,6 +48,13 @@
+
+
+
+ SIZE +
+
+
@@ -118,6 +125,15 @@ {% endif %}
+
+
+ {% if route == "block_list_by_height" %} +
{{ blocks[i].size }}
+ {% else %} +
+ {% endif %} +
+
{% if route == "block_list_by_height" %} @@ -195,6 +211,15 @@ {% endif %}

+
+
Size
+ {% if route == "block_list_by_height" %} +
{{ blocks[i].size }}
+ {% else %} +
+ {% endif %} +
+
Weight
{% if route == "block_list_by_height" %}