From bb861fe817d822955664927a60cf622a795b3c46 Mon Sep 17 00:00:00 2001 From: Noor Date: Thu, 20 Mar 2025 16:44:57 +0530 Subject: [PATCH] refactor: replace direct block_on calls with utils::block_on for consistency --- src/bin/super-detee-cli.rs | 9 +++++---- src/config.rs | 2 +- src/operators.rs | 11 ++++++----- src/snp/deploy.rs | 3 ++- src/snp/grpc.rs | 12 ++++++------ src/snp/mod.rs | 3 ++- src/snp/update.rs | 3 ++- 7 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/bin/super-detee-cli.rs b/src/bin/super-detee-cli.rs index 5017c04..67d7baa 100644 --- a/src/bin/super-detee-cli.rs +++ b/src/bin/super-detee-cli.rs @@ -3,6 +3,7 @@ use clap_complete::{generate, Shell}; use detee_cli::config::Config; use detee_cli::*; use std::io; +use utils::block_on; const ABOUT: &str = r#"The DeTEE Admin CLI got created for the testnet. It allows you to: @@ -105,7 +106,7 @@ fn handle_completion(matches: &ArgMatches, mut cmd: Command) { fn handle_account(matches: &ArgMatches) { match matches.subcommand() { Some(("show", _)) => cli_print(Ok(config::Config::get_account_data())), - Some(("list", _)) => match snp::grpc::block_on(snp::grpc::admin_list_accounts()) { + Some(("list", _)) => match block_on(snp::grpc::admin_list_accounts()) { Ok(accounts) => { for a in accounts { println!("{} {} {}", a.pubkey, a.balance, a.tmp_locked); @@ -122,7 +123,7 @@ fn handle_account(matches: &ArgMatches) { } fn handle_contracts(_matches: &ArgMatches) { - match snp::grpc::block_on(snp::grpc::admin_list_contracts()) { + match block_on(snp::grpc::admin_list_contracts()) { Ok(contracts) => { for c in contracts { println!( @@ -138,7 +139,7 @@ fn handle_contracts(_matches: &ArgMatches) { fn handle_airdrop(matches: &ArgMatches) { let wallet: String = matches.get_one::("wallet").unwrap().clone(); let tokens = *matches.get_one::("amount").unwrap(); - match snp::grpc::block_on(snp::grpc::admin_airdrop(wallet, tokens)) { + match block_on(snp::grpc::admin_airdrop(wallet, tokens)) { Ok(()) => println!("Success."), Err(e) => println!("Could not give airdrop due to error: {e:?}"), } @@ -147,7 +148,7 @@ fn handle_airdrop(matches: &ArgMatches) { fn handle_slash(matches: &ArgMatches) { let wallet: String = matches.get_one::("wallet").unwrap().clone(); let tokens = *matches.get_one::("amount").unwrap(); - match snp::grpc::block_on(snp::grpc::admin_slash(wallet, tokens)) { + match block_on(snp::grpc::admin_slash(wallet, tokens)) { Ok(()) => println!("Success."), Err(e) => println!("Could not slash wallet due to error: {e:?}"), } diff --git a/src/config.rs b/src/config.rs index 1b8f151..b5d8a14 100644 --- a/src/config.rs +++ b/src/config.rs @@ -321,7 +321,7 @@ impl Config { match Self::get_detee_wallet() { Ok(key) => { account_data.wallet_address = key.to_string(); - match crate::snp::grpc::block_on(crate::snp::grpc::get_balance(&key)) { + match crate::utils::block_on(crate::snp::grpc::get_balance(&key)) { Ok(account) => { account_data.account_balance = account.balance as f64 / 1_000_000_000.0; account_data.locked_funds = account.tmp_locked as f64 / 1_000_000_000.0; diff --git a/src/operators.rs b/src/operators.rs index 644f2cb..398fa24 100644 --- a/src/operators.rs +++ b/src/operators.rs @@ -1,5 +1,6 @@ use crate::snp::grpc; use crate::snp::grpc::brain; +use crate::utils::block_on; use tabled::Tabled; #[derive(Tabled)] @@ -26,7 +27,7 @@ impl From for TabledOperator { } pub fn register(escrow: u64, email: String) -> Result { - grpc::block_on(grpc::register_operator(escrow, email))?; + block_on(grpc::register_operator(escrow, email))?; Ok(super::SimpleOutput::from("Successfully registered you as operator.")) } @@ -61,7 +62,7 @@ impl super::HumanOutput for brain::InspectOperatorResp { // bound in any way to the SNP module, however our gRPC module is stuck in the SNP module. // We should figure how to architect the CLI so that this is a bit cleaner. pub fn inspect_operator(wallet: String) -> Result { - grpc::block_on(grpc::inspect_operator(wallet)) + block_on(grpc::inspect_operator(wallet)) } impl super::HumanOutput for Vec { @@ -76,17 +77,17 @@ impl super::HumanOutput for Vec { } pub fn print_operators() -> Result, grpc::Error> { - Ok(grpc::block_on(grpc::list_operators())?) + Ok(block_on(grpc::list_operators())?) } pub fn kick(contract_uuid: String, reason: String) -> Result { - let nano_lp = grpc::block_on(grpc::kick_contract(contract_uuid, reason))?; + let nano_lp = block_on(grpc::kick_contract(contract_uuid, reason))?; Ok(super::SimpleOutput::from( format!("Successfully terminated contract. Refunded {} nanoLP.", nano_lp).as_str(), )) } pub fn ban(wallet: String) -> Result { - grpc::block_on(grpc::ban_user(wallet))?; + block_on(grpc::ban_user(wallet))?; Ok(super::SimpleOutput::from("Successfully banned user")) } diff --git a/src/snp/deploy.rs b/src/snp/deploy.rs index 1d33caf..1f234bb 100644 --- a/src/snp/deploy.rs +++ b/src/snp/deploy.rs @@ -1,8 +1,9 @@ use super::{ - grpc::{self, block_on, brain}, + grpc::{self, brain}, injector, Distro, Dtrfs, Error, VmSshArgs, DEFAULT_ARCHLINUX, DEFAULT_DTRFS, }; use crate::config::Config; +use crate::utils::block_on; use log::{debug, info}; use serde::{Deserialize, Serialize}; diff --git a/src/snp/grpc.rs b/src/snp/grpc.rs index 8366386..b689343 100644 --- a/src/snp/grpc.rs +++ b/src/snp/grpc.rs @@ -371,9 +371,9 @@ pub async fn get_contract_by_uuid(uuid: &str) -> Result(future: F) -> F::Output -where - F: std::future::Future, -{ - tokio::runtime::Runtime::new().unwrap().block_on(future) -} +// pub fn block_on(future: F) -> F::Output +// where +// F: std::future::Future, +// { +// tokio::runtime::Runtime::new().unwrap().block_on(future) +// } diff --git a/src/snp/mod.rs b/src/snp/mod.rs index 49952ca..8e9f964 100644 --- a/src/snp/mod.rs +++ b/src/snp/mod.rs @@ -2,12 +2,13 @@ pub mod deploy; pub mod grpc; mod injector; pub mod update; +use crate::utils::block_on; use crate::{ config::{self, Config}, snp, }; -use grpc::{block_on, brain}; +use grpc::brain; use lazy_static::lazy_static; use serde::{Deserialize, Serialize}; use tabled::Tabled; diff --git a/src/snp/update.rs b/src/snp/update.rs index f50aa9d..879d672 100644 --- a/src/snp/update.rs +++ b/src/snp/update.rs @@ -1,8 +1,9 @@ use super::{ - grpc::{self, block_on, brain}, + grpc::{self, brain}, injector, Dtrfs, Error, }; use crate::config::Config; +use crate::utils::block_on; use log::{debug, info}; #[derive(Clone, Debug, Default, PartialEq)]