From 50173f92215c8699541c35d735c9c0f573a52dac Mon Sep 17 00:00:00 2001 From: aglkm <39521015+aglkm@users.noreply.github.com> Date: Mon, 20 May 2024 16:37:24 +0300 Subject: [PATCH] Generalize returned Error --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/requests.rs | 39 ++++++++++++++++++++------------------- src/worker.rs | 3 +-- 4 files changed, 29 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 979161b..911b7e4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -41,6 +41,12 @@ dependencies = [ "libc", ] +[[package]] +name = "anyhow" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" + [[package]] name = "arrayvec" version = "0.7.4" @@ -732,6 +738,7 @@ dependencies = [ name = "grin-explorer" version = "0.1.3" dependencies = [ + "anyhow", "chrono", "colored", "config", diff --git a/Cargo.toml b/Cargo.toml index 96efd10..164b703 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ config = "0.14.0" lazy_static = "1.4.0" shellexpand = "3.1.0" either = "1.11.0" +anyhow = "1.0.86" [dependencies.reqwest] version = "0.11.23" diff --git a/src/requests.rs b/src/requests.rs index f1b4839..4fe3375 100644 --- a/src/requests.rs +++ b/src/requests.rs @@ -53,7 +53,7 @@ lazy_static! { // RPC requests to grin node. -async fn call(method: &str, params: &str, rpc_type: &str) -> Result { +async fn call(method: &str, params: &str, rpc_type: &str) -> Result { let rpc_url; let secret; @@ -74,14 +74,14 @@ async fn call(method: &str, params: &str, rpc_type: &str) -> Result>) -> Result<(), Error> { +pub async fn get_status(dashboard: Arc>) -> Result<(), anyhow::Error> { let resp = call("get_status", "[]", "owner").await?; let mut data = dashboard.lock().unwrap(); @@ -101,7 +101,7 @@ pub async fn get_status(dashboard: Arc>) -> Result<(), Error> { // Collecting: txns, stem. -pub async fn get_mempool(dashboard: Arc>) -> Result<(), Error> { +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?; @@ -117,7 +117,8 @@ pub async fn get_mempool(dashboard: Arc>) -> Result<(), Error> // Collecting: inbound, outbound. -pub async fn get_connected_peers(dashboard: Arc>) -> Result<(), Error> { +pub async fn get_connected_peers(dashboard: Arc>) + -> Result<(), anyhow::Error> { let resp = call("get_connected_peers", "[]", "owner").await?; let mut data = dashboard.lock().unwrap(); @@ -216,7 +217,7 @@ pub fn get_disk_usage(dashboard: Arc>) -> Result<(), Error> { // Collecting: hashrate, difficulty, production cost, breakeven cost. -pub async fn get_mining_stats(dashboard: Arc>) -> Result<(), Error> { +pub async fn get_mining_stats(dashboard: Arc>) -> Result<(), anyhow::Error> { let difficulty_window = 1440; let height = get_current_height(dashboard.clone()); @@ -265,7 +266,7 @@ pub async fn get_mining_stats(dashboard: Arc>) -> Result<(), Er // Collecting block data for recent blocks (block_list page). pub async fn get_block_list_data(height: &String, block: &mut Block) - -> Result<(), Error> { + -> Result<(), anyhow::Error> { // Max block weight is 40000 // One unit of weight is 32 bytes let kernel_weight = 3.0; @@ -273,11 +274,11 @@ pub async fn get_block_list_data(height: &String, block: &mut Block) let output_weight = 21.0; if height.is_empty() == false { - let params = &format!("[{}, null, null]", height)[..]; - let resp = call("get_block", params, "foreign").await.unwrap(); + let params = &format!("[{}, null, null]", height)[..]; + let resp = call("get_block", params, "foreign").await?; if resp["result"]["Ok"].is_null() == false { - block.height = resp["result"]["Ok"]["header"]["height"].to_string(); + block.height = resp["result"]["Ok"]["header"]["height"].to_string(); let dt: DateTime = resp["result"]["Ok"]["header"]["timestamp"] .as_str().unwrap().to_string().parse().unwrap(); @@ -324,7 +325,7 @@ 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<(), Error> { + -> Result<(), anyhow::Error> { // Max block weight is 40000 // One unit of weight is 32 bytes let kernel_weight = 3.0; @@ -380,10 +381,10 @@ pub async fn get_block_data(height: &str, block: &mut Block) // Get block height by hash. pub async fn get_block_header(hash: &str, height: &mut String) - -> Result<(), Error> { + -> Result<(), anyhow::Error> { let params = &format!("[null, \"{}\", null]", hash)[..]; - let resp = call("get_header", params, "foreign").await.unwrap(); + let resp = call("get_header", params, "foreign").await?; if resp["result"]["Ok"].is_null() == false { *height = resp["result"]["Ok"]["height"].to_string(); @@ -395,10 +396,10 @@ pub async fn get_block_header(hash: &str, height: &mut String) // Get kernel. pub async fn get_kernel(kernel: &str, height: &mut String) - -> Result<(), Error> { + -> Result<(), anyhow::Error> { let params = &format!("[\"{}\", null, null]", kernel)[..]; - let resp = call("get_kernel", params, "foreign").await.unwrap(); + let resp = call("get_kernel", params, "foreign").await?; if resp["result"]["Ok"].is_null() == false { *height = resp["result"]["Ok"]["height"].to_string(); @@ -410,11 +411,11 @@ pub async fn get_kernel(kernel: &str, height: &mut String) // Collecting block kernels for transactions stats. pub async fn get_block_kernels(height: &String, blocks: &mut Vec) - -> Result<(), Error> { + -> Result<(), anyhow::Error> { if height.is_empty() == false { let params = &format!("[{}, {}, 720, false]", height.parse::().unwrap() - 720, height)[..]; - let resp = call("get_blocks", params, "foreign").await.unwrap(); + let resp = call("get_blocks", params, "foreign").await?; for resp_block in resp["result"]["Ok"]["blocks"].as_array().unwrap() { let mut block = Block::new(); @@ -528,11 +529,11 @@ pub async fn get_recent_blocks(dashboard: Arc>, // Collecting a specified list of blocks. pub async fn get_block_list_by_height(height: &str, blocks: &mut Vec, - latest_height: &mut u64) -> Result<(), Error> { + latest_height: &mut u64) -> Result<(), anyhow::Error> { let mut i = 0; let height = height.to_string(); - let resp = call("get_status", "[]", "owner").await.unwrap(); + let resp = call("get_status", "[]", "owner").await?; if resp != Value::Null { *latest_height = resp["result"]["Ok"]["tip"]["height"].to_string().parse::().unwrap(); diff --git a/src/worker.rs b/src/worker.rs index b75343f..4a4f17e 100644 --- a/src/worker.rs +++ b/src/worker.rs @@ -1,5 +1,4 @@ use std::sync::{Arc, Mutex}; -use reqwest::Error; use crate::data::Dashboard; use crate::data::Block; @@ -10,7 +9,7 @@ use crate::requests; // Tokio Runtime Worker. // Collecting all the data. pub async fn run(dash: Arc>, blocks: Arc>>, - txns: Arc>) -> Result<(), Error> { + txns: Arc>) -> Result<(), anyhow::Error> { let _ = requests::get_status(dash.clone()).await?; let _ = requests::get_mempool(dash.clone()).await?; let _ = requests::get_connected_peers(dash.clone()).await?;