From 2aaebee5f46b255fde2fdef16cce5eb69077ade2 Mon Sep 17 00:00:00 2001 From: aglkm <39521015+aglkm@users.noreply.github.com> Date: Fri, 24 May 2024 15:14:16 +0300 Subject: [PATCH] Added kernel page, respect id field in api --- src/data.rs | 23 +++++++++++++++++ src/main.rs | 30 ++++++++++++---------- src/requests.rs | 47 +++++++++++++++++++++------------- templates/base.html.tera | 4 +-- templates/kernel.html.tera | 52 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 124 insertions(+), 32 deletions(-) create mode 100644 templates/kernel.html.tera diff --git a/src/data.rs b/src/data.rs index caca842..d529320 100644 --- a/src/data.rs +++ b/src/data.rs @@ -111,6 +111,29 @@ impl Block { } +// Kernel data +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Kernel { + pub height: String, + pub excess: String, + pub ker_type: String, + pub fee: String, + pub raw_data: String, +} + +impl Kernel { + pub fn new() -> Kernel { + Kernel { + height: String::new(), + excess: String::new(), + ker_type: String::new(), + fee: String::new(), + raw_data: String::new(), + } + } +} + + // Transactions data #[derive(Debug)] pub struct Transactions { diff --git a/src/main.rs b/src/main.rs index 26207b6..3584b85 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,7 @@ mod data; use crate::data::Dashboard; use crate::data::Block; use crate::data::Transactions; +use crate::data::Kernel; // Rendering main (Dashboard) page. @@ -119,21 +120,22 @@ async fn block_header_by_hash(hash: &str) -> Either { // Rendering page for a specified kernel. -#[get("/kernel/")] -async fn kernel(kernel: &str) -> Either { - let mut height = String::new(); +#[get("/kernel/")] +async fn kernel(excess: &str) -> Template { + let mut kernel = Kernel::new(); - let _ = requests::get_kernel(&kernel, &mut height).await; + let _ = requests::get_kernel(&excess, &mut kernel).await; - if kernel.is_empty() == false { - if height.is_empty() == false { - return Either::Right(Redirect::to(uri!(block_details_by_height(height.as_str())))); - } + if kernel.height.is_empty() == false { + return Template::render("kernel", context! { + route: "kernel", + kernel, + }) } - return Either::Left(Template::render("error", context! { + return Template::render("error", context! { route: "error", - })) + }) } @@ -183,10 +185,10 @@ async fn api_owner(data: &str) -> String { Some(value) => value, _ => return "{\"error\":\"bad syntax\"}".to_string(), }; - + // Whitelisted methods: get_connected_peer, get_peers, get_status. if method == "get_connected_peers" || method == "get_peers" || method == "get_status" { - let resp = requests::call(method, v["params"].to_string().as_str(), "owner").await; + let resp = requests::call(method, v["params"].to_string().as_str(), v["id"].to_string().as_str(), "owner").await; let result = match resp { Ok(value) => value, @@ -216,7 +218,9 @@ async fn api_foreign(data: &str) -> String { _ => return "{\"error\":\"bad syntax\"}".to_string(), }; - let resp = requests::call(method, v["params"].to_string().as_str(), "foreign").await; + println!("{}", method); + println!("{}", data); + let resp = requests::call(method, v["params"].to_string().as_str(), v["id"].to_string().as_str(), "foreign").await; let result = match resp { Ok(value) => value, diff --git a/src/requests.rs b/src/requests.rs index dbcfef4..0fc6020 100644 --- a/src/requests.rs +++ b/src/requests.rs @@ -16,6 +16,7 @@ use crate::data::Dashboard; use crate::data::Block; use crate::data::Transactions; use crate::data::ExplorerConfig; +use crate::Kernel; // Static explorer config structure @@ -53,7 +54,7 @@ lazy_static! { // RPC requests to grin node. -pub async fn call(method: &str, params: &str, rpc_type: &str) -> Result { +pub async fn call(method: &str, params: &str, id: &str, rpc_type: &str) -> Result { let rpc_url; let secret; @@ -68,7 +69,7 @@ pub async fn call(method: &str, params: &str, rpc_type: &str) -> Result Result>) -> Result<(), anyhow::Error> { - let resp = call("get_status", "[]", "owner").await?; + let resp = call("get_status", "[]", "1", "owner").await?; let mut data = dashboard.lock().unwrap(); @@ -102,8 +103,8 @@ pub async fn get_status(dashboard: Arc>) -> Result<(), anyhow:: // Collecting: txns, stem. pub async fn get_mempool(dashboard: Arc>) -> Result<(), anyhow::Error> { - let resp1 = call("get_pool_size", "[]", "foreign").await?; - let resp2 = call("get_stempool_size", "[]", "foreign").await?; + let resp1 = call("get_pool_size", "[]", "1", "foreign").await?; + let resp2 = call("get_stempool_size", "[]", "1", "foreign").await?; let mut data = dashboard.lock().unwrap(); @@ -119,7 +120,7 @@ pub async fn get_mempool(dashboard: Arc>) -> Result<(), anyhow: // Collecting: inbound, outbound. pub async fn get_connected_peers(dashboard: Arc>) -> Result<(), anyhow::Error> { - let resp = call("get_connected_peers", "[]", "owner").await?; + let resp = call("get_connected_peers", "[]", "1", "owner").await?; let mut data = dashboard.lock().unwrap(); @@ -225,8 +226,8 @@ pub async fn get_mining_stats(dashboard: Arc>) -> Result<(), an let params1 = &format!("[{}, null, null]", height)[..]; let params2 = &format!("[{}, null, null]", height.parse::().unwrap() - difficulty_window)[..]; - let resp1 = call("get_block", params1, "foreign").await?; - let resp2 = call("get_block", params2, "foreign").await?; + let resp1 = call("get_block", params1, "1", "foreign").await?; + let resp2 = call("get_block", params2, "1", "foreign").await?; let mut data = dashboard.lock().unwrap(); @@ -283,7 +284,7 @@ pub async fn get_block_list_data(height: &String, block: &mut Block) if height.is_empty() == false { let params = &format!("[{}, null, null]", height)[..]; - let resp = call("get_block", params, "foreign").await?; + let resp = call("get_block", params, "1", "foreign").await?; if resp["result"]["Ok"].is_null() == false { block.height = resp["result"]["Ok"]["header"]["height"].to_string(); @@ -343,7 +344,7 @@ pub async fn get_block_data(height: &str, block: &mut Block) if height.is_empty() == false { let params = &format!("[{}, null, null]", height)[..]; - let resp = call("get_block", params, "foreign").await?; + let resp = call("get_block", params, "1", "foreign").await?; if resp["result"]["Ok"].is_null() == false { block.hash = resp["result"]["Ok"]["header"]["hash"].as_str().unwrap().to_string(); @@ -392,7 +393,7 @@ pub async fn get_block_header(hash: &str, height: &mut String) -> Result<(), anyhow::Error> { let params = &format!("[null, \"{}\", null]", hash)[..]; - let resp = call("get_header", params, "foreign").await?; + let resp = call("get_header", params, "1", "foreign").await?; if resp["result"]["Ok"].is_null() == false { *height = resp["result"]["Ok"]["height"].to_string(); @@ -403,14 +404,26 @@ pub async fn get_block_header(hash: &str, height: &mut String) // Get kernel. -pub async fn get_kernel(kernel: &str, height: &mut String) +pub async fn get_kernel(excess: &str, kernel: &mut Kernel) -> Result<(), anyhow::Error> { - let params = &format!("[\"{}\", null, null]", kernel)[..]; + let params = &format!("[\"{}\", null, null]", excess)[..]; - let resp = call("get_kernel", params, "foreign").await?; + let resp = call("get_kernel", params, "1", "foreign").await?; if resp["result"]["Ok"].is_null() == false { - *height = resp["result"]["Ok"]["height"].to_string(); + kernel.height = resp["result"]["Ok"]["height"].to_string(); + kernel.excess = resp["result"]["Ok"]["tx_kernel"]["excess"].as_str().unwrap().to_string(); + if resp["result"]["Ok"]["tx_kernel"]["features"]["Plain"].is_null() == false { + kernel.ker_type = "Plain".to_string(); + kernel.fee = format!("ツ {}", + resp["result"]["Ok"]["tx_kernel"]["features"]["Plain"]["fee"] + .to_string().parse::().unwrap() / 1000000000.0); + } else { + kernel.ker_type = resp["result"]["Ok"]["tx_kernel"]["features"].as_str().unwrap().to_string(); + kernel.fee = "ツ 0".to_string(); + } + + kernel.raw_data = serde_json::to_string_pretty(&resp).unwrap() } Ok(()) @@ -423,7 +436,7 @@ pub async fn get_block_kernels(height: &String, blocks: &mut Vec) if height.is_empty() == false { let params = &format!("[{}, {}, 720, false]", height.parse::().unwrap() - 720, height)[..]; - let resp = call("get_blocks", params, "foreign").await?; + let resp = call("get_blocks", params, "1", "foreign").await?; for resp_block in resp["result"]["Ok"]["blocks"].as_array().unwrap() { let mut block = Block::new(); @@ -541,7 +554,7 @@ pub async fn get_block_list_by_height(height: &str, blocks: &mut Vec, let mut i = 0; let height = height.to_string(); - let resp = call("get_status", "[]", "owner").await?; + let resp = call("get_status", "[]", "1", "owner").await?; if resp != Value::Null { *latest_height = resp["result"]["Ok"]["tip"]["height"].to_string().parse::().unwrap(); diff --git a/templates/base.html.tera b/templates/base.html.tera index 701ee1d..c9d1e2a 100644 --- a/templates/base.html.tera +++ b/templates/base.html.tera @@ -1,5 +1,5 @@ - + Grin Blockchain Explorer @@ -78,7 +78,7 @@ {% block content %}{% endblock content %} -