adding unspent outputs stats

This commit is contained in:
aglkm
2024-09-30 16:31:40 +03:00
parent a8bf3f1b0a
commit aa49b2c20b
6 changed files with 240 additions and 104 deletions

View File

@@ -27,9 +27,10 @@ Grin is the very first, simple and fair MimbleWimble blockchain implementation.
You will see the following output:
```
[2024-06-19T13:12:34Z INFO grin_explorer] starting up.
[2024-06-19T13:12:34Z WARN rocket::launch] 🚀 Rocket has launched from http://127.0.0.1:8000
[2024-06-19T13:12:34Z INFO grin_explorer] ready.
[2024-09-30T13:30:02Z INFO grin_explorer] starting up.
[2024-09-30T13:30:02Z WARN rocket::launch] 🚀 Rocket has launched from http://127.0.0.1:8000
[2024-09-30T13:30:03Z INFO grin_explorer] worker::data ready.
[2024-09-30T13:30:10Z INFO grin_explorer] worker::stats ready.
```
5. Open explorer in your browser: http://127.0.0.1:8000

View File

@@ -47,6 +47,8 @@ pub struct Dashboard {
// mempool
pub txns: String,
pub stem: String,
// utxo
pub utxo_count: String,
}
impl Dashboard {
@@ -76,6 +78,7 @@ impl Dashboard {
breakeven_cost: String::new(),
txns: String::new(),
stem: String::new(),
utxo_count: String::new(),
}
}
}
@@ -241,6 +244,8 @@ pub struct Statistics {
// Transactions & fees
pub txns: Vec<String>,
pub fees: Vec<String>,
//UTXOs
pub utxo_count: Vec<String>,
}
impl Statistics {
@@ -253,6 +258,7 @@ impl Statistics {
hashrate: Vec::new(),
txns: Vec::new(),
fees: Vec::new(),
utxo_count: Vec::new(),
}
}
}

View File

@@ -1,4 +1,5 @@
#[macro_use] extern crate rocket;
use chrono::Utc;
use either::Either;
use rocket_dyn_templates::{Template, context};
use rocket::fs::FileServer;
@@ -9,18 +10,13 @@ use std::sync::{Arc, Mutex};
use std::time::Duration;
use serde_json::Value;
use crate::data::{Block, Dashboard, Kernel, Output, Statistics, Transactions, OUTPUT_SIZE};
use crate::requests::CONFIG;
mod data;
mod requests;
mod worker;
use crate::data::Block;
use crate::data::Dashboard;
use crate::data::Kernel;
use crate::data::Output;
use crate::data::Statistics;
use crate::data::Transactions;
use crate::requests::CONFIG;
// Rendering main (Dashboard) page.
#[get("/")]
@@ -219,14 +215,16 @@ fn stats(statistics: &State<Arc<Mutex<Statistics>>>) -> Template {
let data = statistics.lock().unwrap();
Template::render("stats", context! {
route: "stats",
date: data.date.clone(),
user_agent: data.user_agent.clone(),
count: data.count.clone(),
total: data.total,
hashrate: data.hashrate.clone(),
txns: data.txns.clone(),
fees: data.fees.clone(),
route: "stats",
date: data.date.clone(),
user_agent: data.user_agent.clone(),
count: data.count.clone(),
total: data.total,
hashrate: data.hashrate.clone(),
txns: data.txns.clone(),
fees: data.fees.clone(),
utxo_count: data.utxo_count.clone(),
output_size: OUTPUT_SIZE,
})
}
@@ -669,27 +667,51 @@ async fn main() {
let txns_clone = txns.clone();
let stats = Arc::new(Mutex::new(Statistics::new()));
let stats_clone = stats.clone();
let mut ready = false;
// Starting the Worker
let mut ready_data = false;
let mut ready_stats = false;
let mut date = "".to_string();
// Collecting main data
tokio::spawn(async move {
loop {
let result = worker::run(dash_clone.clone(), blocks_clone.clone(),
txns_clone.clone(), stats_clone.clone()).await;
let result = worker::data(dash_clone.clone(), blocks_clone.clone(),
txns_clone.clone(), stats_clone.clone()).await;
match result {
Ok(_v) => {
if ready == false {
ready = true;
info!("ready.");
if ready_data == false {
ready_data = true;
info!("worker::data ready.");
}
},
Err(e) => {
ready = false;
ready_data = false;
error!("{}", e);
},
}
let date_now = format!("\"{}\"", Utc::now().format("%d-%m-%Y"));
if date.is_empty() || date != date_now {
date = date_now;
let result = worker::stats(dash_clone.clone(), txns_clone.clone(),
stats_clone.clone()).await;
match result {
Ok(_v) => {
if ready_stats == false {
ready_stats = true;
info!("worker::stats ready.");
}
},
Err(e) => {
ready_stats = false;
error!("{}", e);
},
}
}
tokio::time::sleep(Duration::from_secs(15)).await;
}
});

View File

@@ -748,3 +748,42 @@ pub async fn get_block_list_by_height(height: &str, blocks: &mut Vec<Block>,
Ok(())
}
// Collecting unspent outputs.
pub async fn get_unspent_outputs(dashboard: Arc<Mutex<Dashboard>>) -> Result<(), anyhow::Error> {
let mut highest_mmr_index = 0;
let mut current_mmr_index = 0;
let mut utxo_count = 0;
// Get the highest MMR index
let resp = call("get_unspent_outputs", "[1, null, 10000, false]", "1", "foreign").await?;
if resp != Value::Null {
highest_mmr_index = resp["result"]["Ok"]["highest_index"].to_string().parse::<u64>().unwrap();
current_mmr_index = resp["result"]["Ok"]["outputs"].as_array().unwrap().last().unwrap()["mmr_index"].to_string().parse::<u64>().unwrap();
}
// Get all unspent outputs
while current_mmr_index < highest_mmr_index {
current_mmr_index = current_mmr_index + 1;
let params = &format!("[{}, {}, 10000, false]", current_mmr_index, highest_mmr_index)[..];
let resp = call("get_unspent_outputs", params, "1", "foreign").await?;
if resp != Value::Null {
if let Some(v) = resp["result"]["Ok"]["outputs"].as_array().unwrap().last() {
current_mmr_index = v["mmr_index"].to_string().parse::<u64>().unwrap();
utxo_count = utxo_count + resp["result"]["Ok"]["outputs"].as_array().unwrap().len();
} else {
// Break the loop if we got no outputs from the node request
break;
}
}
}
let mut data = dashboard.lock().unwrap();
data.utxo_count = utxo_count.to_string();
Ok(())
}

View File

@@ -9,10 +9,9 @@ use crate::data::Transactions;
use crate::requests;
// Tokio Runtime Worker.
// Collecting all the data.
pub async fn run(dash: Arc<Mutex<Dashboard>>, blocks: Arc<Mutex<Vec<Block>>>,
txns: Arc<Mutex<Transactions>>, stats: Arc<Mutex<Statistics>>) -> Result<(), anyhow::Error> {
// Collecting main data.
pub async fn data(dash: Arc<Mutex<Dashboard>>, blocks: Arc<Mutex<Vec<Block>>>,
txns: Arc<Mutex<Transactions>>, stats: Arc<Mutex<Statistics>>) -> Result<(), anyhow::Error> {
let _ = requests::get_status(dash.clone()).await?;
let _ = requests::get_mempool(dash.clone()).await?;
let _ = requests::get_connected_peers(dash.clone(), stats.clone()).await?;
@@ -22,33 +21,31 @@ pub async fn run(dash: Arc<Mutex<Dashboard>>, blocks: Arc<Mutex<Vec<Block>>>,
let _ = requests::get_recent_blocks(dash.clone(), blocks.clone()).await?;
let _ = requests::get_txn_stats(dash.clone(), txns.clone()).await?;
Ok(())
}
// Collecting statistics.
pub async fn stats(dash: Arc<Mutex<Dashboard>>, txns: Arc<Mutex<Transactions>>, stats: Arc<Mutex<Statistics>>) -> Result<(), anyhow::Error> {
let _ = requests::get_unspent_outputs(dash.clone()).await?;
let mut stats = stats.lock().unwrap();
let dash = dash.lock().unwrap();
let txns = txns.lock().unwrap();
// Every day push new stats into the vectors
if let Some(v) = stats.date.last() {
// If new day, push the stats
if *v != format!("\"{}\"", Utc::now().format("%d-%m-%Y")) {
if stats.date.len() == 30 {
stats.date.remove(0);
stats.hashrate.remove(0);
stats.txns.remove(0);
stats.fees.remove(0);
}
stats.date.push(format!("\"{}\"", Utc::now().format("%d-%m-%Y")));
stats.hashrate.push(dash.hashrate_kgs.clone());
stats.txns.push(txns.period_24h.clone());
stats.fees.push(txns.fees_24h.clone());
}
} else {
// Vector is empty, pushing current stats
stats.date.push(format!("\"{}\"", Utc::now().format("%d-%m-%Y")));
stats.hashrate.push(dash.hashrate_kgs.clone());
stats.txns.push(txns.period_24h.clone());
stats.fees.push(txns.fees_24h.clone());
if stats.date.len() == 30 {
stats.date.remove(0);
stats.hashrate.remove(0);
stats.txns.remove(0);
stats.fees.remove(0);
stats.utxo_count.remove(0);
}
stats.date.push(format!("\"{}\"", Utc::now().format("%d-%m-%Y")));
stats.hashrate.push(dash.hashrate_kgs.clone());
stats.txns.push(txns.period_24h.clone());
stats.fees.push(txns.fees_24h.clone());
stats.utxo_count.push(dash.utxo_count.clone());
Ok(())
}

View File

@@ -18,7 +18,7 @@
<div class="card border-bottom-0 border-start-0 border-end-0 rounded-0">
<div class="card-body" align="center">
<div class="value-text">
<div class="darkorange-text"><i class="bi bi-activity"></i> HASHRATE</div>
<div class="darkorange-text"><i class="bi bi-speedometer2"></i> TRANSACTIONS & FEES</div>
<div style="position: relative; height:60vh; width:90vw"><canvas id="2"></canvas></div>
</div>
</div>
@@ -27,20 +27,25 @@
<div class="card border-bottom-0 border-start-0 border-end-0 rounded-0">
<div class="card-body" align="center">
<div class="value-text">
<div class="darkorange-text"><i class="bi bi-speedometer2"></i> TRANSACTIONS & FEES</div>
<div class="darkorange-text"><i class="bi bi-activity"></i> HASHRATE</div>
<div style="position: relative; height:60vh; width:90vw"><canvas id="3"></canvas></div>
</div>
</div>
</div>
<div class="card border-bottom-0 border-start-0 border-end-0 rounded-0">
<div class="card-body" align="center">
<div class="value-text">
<div class="darkorange-text"><i class="bi bi-card-list"></i> UNSPENT OUTPUTS</div>
<div style="position: relative; height:60vh; width:90vw"><canvas id="4"></canvas></div>
</div>
</div>
</div>
</code>
<script>
var ctx = document.getElementById('2').getContext('2d');
var gradient = ctx.createLinearGradient(0, 0, 0, 600);
gradient.addColorStop(0, 'rgba(178, 81, 16, 1)');
gradient.addColorStop(1, 'rgba(255, 158, 93, 0)');
<!-- Node Version Chart -->
var options = {
maintainAspectRatio: false,
@@ -69,51 +74,6 @@
options: options
});
<!-- Hashrate Chart -->
var options = {
maintainAspectRatio: false,
interaction: {
intersect: false,
},
plugins: {
legend: {
display: false
},
},
scales: {
x: {
grid: {
display: false
}
},
y: {
grid: {
display: false
}
},
}
};
var data = {
labels: {{ date }},
datasets: [{
label: 'Hashrate (kG/s)',
data: {{ hashrate }},
fill: true,
borderColor: "#b25110",
backgroundColor: gradient,
tension: 0.1
}]
};
new Chart(document.getElementById("2"), {
type: 'line',
data: data,
options: options
});
<!-- Transactions & Fees Chart -->
var options = {
maintainAspectRatio: false,
@@ -157,12 +117,123 @@
]
};
new Chart(document.getElementById("2"), {
type: 'line',
data: data,
options: options
});
<!-- Hashrate Chart -->
var ctx_hash = document.getElementById('2').getContext('2d');
var gradient_hash = ctx_hash.createLinearGradient(0, 0, 0, 600);
gradient_hash.addColorStop(0, 'rgba(178, 81, 16, 1)');
gradient_hash.addColorStop(1, 'rgba(255, 158, 93, 0)');
var options = {
maintainAspectRatio: false,
interaction: {
intersect: false,
},
plugins: {
legend: {
display: false
},
},
scales: {
x: {
grid: {
display: false
}
},
y: {
grid: {
display: false
}
},
}
};
var data = {
labels: {{ date }},
datasets: [{
label: 'Hashrate (kG/s)',
data: {{ hashrate }},
fill: true,
borderColor: "#b25110",
backgroundColor: gradient_hash,
tension: 0.1
}]
};
new Chart(document.getElementById("3"), {
type: 'line',
data: data,
options: options
});
<!-- Unspent Outputs Chart -->
var ctx_utxo = document.getElementById('2').getContext('2d');
var gradient_utxo = ctx_hash.createLinearGradient(0, 0, 0, 600);
gradient_utxo.addColorStop(0, 'rgba(178, 81, 16, 1)');
gradient_utxo.addColorStop(1, 'rgba(255, 158, 93, 0)');
var options = {
maintainAspectRatio: false,
interaction: {
intersect: false,
},
plugins: {
legend: {
display: false
},
tooltip: {
callbacks: {
footer: function(tooltipItems) {
let size = 0;
tooltipItems.forEach(function(tooltipItem) {
size = tooltipItem.parsed.y * {{ output_size }} / 1000 / 1000;
});
return 'Size: ' + size.toFixed(2) + ' MB';
}
}
}
},
scales: {
x: {
grid: {
display: false
}
},
y: {
grid: {
display: false
}
},
}
};
var data = {
labels: {{ date }},
datasets: [
{
label: 'Unspent Outputs',
data: {{ utxo_count }},
fill: true,
borderColor: "#b25110",
backgroundColor: gradient_utxo,
tension: 0.1
}
]
};
new Chart(document.getElementById("4"), {
type: 'line',
data: data,
options: options
});
</script>