refactor: replace direct block_on calls with utils::block_on for consistency

This commit is contained in:
Noor 2025-03-20 16:44:57 +05:30
parent f220e200ec
commit bb861fe817
Signed by: noormohammedb
GPG Key ID: D83EFB8B3B967146
7 changed files with 24 additions and 19 deletions

@ -3,6 +3,7 @@ use clap_complete::{generate, Shell};
use detee_cli::config::Config; use detee_cli::config::Config;
use detee_cli::*; use detee_cli::*;
use std::io; use std::io;
use utils::block_on;
const ABOUT: &str = r#"The DeTEE Admin CLI got created for the testnet. const ABOUT: &str = r#"The DeTEE Admin CLI got created for the testnet.
It allows you to: It allows you to:
@ -105,7 +106,7 @@ fn handle_completion(matches: &ArgMatches, mut cmd: Command) {
fn handle_account(matches: &ArgMatches) { fn handle_account(matches: &ArgMatches) {
match matches.subcommand() { match matches.subcommand() {
Some(("show", _)) => cli_print(Ok(config::Config::get_account_data())), 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) => { Ok(accounts) => {
for a in accounts { for a in accounts {
println!("{} {} {}", a.pubkey, a.balance, a.tmp_locked); println!("{} {} {}", a.pubkey, a.balance, a.tmp_locked);
@ -122,7 +123,7 @@ fn handle_account(matches: &ArgMatches) {
} }
fn handle_contracts(_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) => { Ok(contracts) => {
for c in contracts { for c in contracts {
println!( println!(
@ -138,7 +139,7 @@ fn handle_contracts(_matches: &ArgMatches) {
fn handle_airdrop(matches: &ArgMatches) { fn handle_airdrop(matches: &ArgMatches) {
let wallet: String = matches.get_one::<String>("wallet").unwrap().clone(); let wallet: String = matches.get_one::<String>("wallet").unwrap().clone();
let tokens = *matches.get_one::<u64>("amount").unwrap(); let tokens = *matches.get_one::<u64>("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."), Ok(()) => println!("Success."),
Err(e) => println!("Could not give airdrop due to error: {e:?}"), 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) { fn handle_slash(matches: &ArgMatches) {
let wallet: String = matches.get_one::<String>("wallet").unwrap().clone(); let wallet: String = matches.get_one::<String>("wallet").unwrap().clone();
let tokens = *matches.get_one::<u64>("amount").unwrap(); let tokens = *matches.get_one::<u64>("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."), Ok(()) => println!("Success."),
Err(e) => println!("Could not slash wallet due to error: {e:?}"), Err(e) => println!("Could not slash wallet due to error: {e:?}"),
} }

@ -321,7 +321,7 @@ impl Config {
match Self::get_detee_wallet() { match Self::get_detee_wallet() {
Ok(key) => { Ok(key) => {
account_data.wallet_address = key.to_string(); 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) => { Ok(account) => {
account_data.account_balance = account.balance as f64 / 1_000_000_000.0; 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; account_data.locked_funds = account.tmp_locked as f64 / 1_000_000_000.0;

@ -1,5 +1,6 @@
use crate::snp::grpc; use crate::snp::grpc;
use crate::snp::grpc::brain; use crate::snp::grpc::brain;
use crate::utils::block_on;
use tabled::Tabled; use tabled::Tabled;
#[derive(Tabled)] #[derive(Tabled)]
@ -26,7 +27,7 @@ impl From<grpc::brain::ListOperatorsResp> for TabledOperator {
} }
pub fn register(escrow: u64, email: String) -> Result<super::SimpleOutput, grpc::Error> { pub fn register(escrow: u64, email: String) -> Result<super::SimpleOutput, grpc::Error> {
grpc::block_on(grpc::register_operator(escrow, email))?; block_on(grpc::register_operator(escrow, email))?;
Ok(super::SimpleOutput::from("Successfully registered you as operator.")) 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. // 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. // We should figure how to architect the CLI so that this is a bit cleaner.
pub fn inspect_operator(wallet: String) -> Result<brain::InspectOperatorResp, grpc::Error> { pub fn inspect_operator(wallet: String) -> Result<brain::InspectOperatorResp, grpc::Error> {
grpc::block_on(grpc::inspect_operator(wallet)) block_on(grpc::inspect_operator(wallet))
} }
impl super::HumanOutput for Vec<brain::ListOperatorsResp> { impl super::HumanOutput for Vec<brain::ListOperatorsResp> {
@ -76,17 +77,17 @@ impl super::HumanOutput for Vec<brain::ListOperatorsResp> {
} }
pub fn print_operators() -> Result<Vec<brain::ListOperatorsResp>, grpc::Error> { pub fn print_operators() -> Result<Vec<brain::ListOperatorsResp>, grpc::Error> {
Ok(grpc::block_on(grpc::list_operators())?) Ok(block_on(grpc::list_operators())?)
} }
pub fn kick(contract_uuid: String, reason: String) -> Result<super::SimpleOutput, grpc::Error> { pub fn kick(contract_uuid: String, reason: String) -> Result<super::SimpleOutput, grpc::Error> {
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( Ok(super::SimpleOutput::from(
format!("Successfully terminated contract. Refunded {} nanoLP.", nano_lp).as_str(), format!("Successfully terminated contract. Refunded {} nanoLP.", nano_lp).as_str(),
)) ))
} }
pub fn ban(wallet: String) -> Result<super::SimpleOutput, grpc::Error> { pub fn ban(wallet: String) -> Result<super::SimpleOutput, grpc::Error> {
grpc::block_on(grpc::ban_user(wallet))?; block_on(grpc::ban_user(wallet))?;
Ok(super::SimpleOutput::from("Successfully banned user")) Ok(super::SimpleOutput::from("Successfully banned user"))
} }

@ -1,8 +1,9 @@
use super::{ use super::{
grpc::{self, block_on, brain}, grpc::{self, brain},
injector, Distro, Dtrfs, Error, VmSshArgs, DEFAULT_ARCHLINUX, DEFAULT_DTRFS, injector, Distro, Dtrfs, Error, VmSshArgs, DEFAULT_ARCHLINUX, DEFAULT_DTRFS,
}; };
use crate::config::Config; use crate::config::Config;
use crate::utils::block_on;
use log::{debug, info}; use log::{debug, info};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};

@ -371,9 +371,9 @@ pub async fn get_contract_by_uuid(uuid: &str) -> Result<brain::VmContract, Error
Ok(contracts[0].clone()) Ok(contracts[0].clone())
} }
pub fn block_on<F>(future: F) -> F::Output // pub fn block_on<F>(future: F) -> F::Output
where // where
F: std::future::Future, // F: std::future::Future,
{ // {
tokio::runtime::Runtime::new().unwrap().block_on(future) // tokio::runtime::Runtime::new().unwrap().block_on(future)
} // }

@ -2,12 +2,13 @@ pub mod deploy;
pub mod grpc; pub mod grpc;
mod injector; mod injector;
pub mod update; pub mod update;
use crate::utils::block_on;
use crate::{ use crate::{
config::{self, Config}, config::{self, Config},
snp, snp,
}; };
use grpc::{block_on, brain}; use grpc::brain;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tabled::Tabled; use tabled::Tabled;

@ -1,8 +1,9 @@
use super::{ use super::{
grpc::{self, block_on, brain}, grpc::{self, brain},
injector, Dtrfs, Error, injector, Dtrfs, Error,
}; };
use crate::config::Config; use crate::config::Config;
use crate::utils::block_on;
use log::{debug, info}; use log::{debug, info};
#[derive(Clone, Debug, Default, PartialEq)] #[derive(Clone, Debug, Default, PartialEq)]