diff --git a/src/general/grpc.rs b/src/general/grpc.rs new file mode 100644 index 0000000..5a27f26 --- /dev/null +++ b/src/general/grpc.rs @@ -0,0 +1,98 @@ +use crate::config::Config; +use crate::utils::sign_request; +use log::{debug, warn}; +use tokio_stream::StreamExt; +// use tonic::metadata::errors::InvalidMetadataValue; + +pub mod proto { + pub use detee_shared::common_proto::*; + pub use detee_shared::general_proto::*; +} + +use proto::brain_general_cli_client::BrainGeneralCliClient; +use proto::{ + BanUserReq, Empty, InspectOperatorResp, KickReq, ListOperatorsResp, Pubkey, RegOperatorReq, +}; + +#[derive(thiserror::Error, Debug)] +pub enum Error { + #[error("Failed to connect to the brain: {0}")] + BrainConnection(#[from] tonic::transport::Error), + #[error("Received error from brain: status: {}, message: {}", + _0.code().to_string(), _0.message())] + ResponseStatus(#[from] tonic::Status), + // #[error(transparent)] + // ConfigError(#[from] crate::config::Error), + // #[error("Could not find contract {0}")] + // VmContractNotFound(String), + // #[error(transparent)] + // InternalError(#[from] InvalidMetadataValue), + #[error(transparent)] + InternalError(#[from] crate::utils::Error), + #[error(transparent)] + ConfigError(#[from] crate::config::Error), +} + +pub async fn register_operator(escrow: u64, email: String) -> Result<(), Error> { + debug!("Connecting to brain to register operator..."); + let mut client = BrainGeneralCliClient::connect(Config::get_brain_url()).await?; + client + .register_operator(sign_request(RegOperatorReq { + pubkey: Config::get_detee_wallet()?, + escrow, + email, + })?) + .await?; + Ok(()) +} + +pub async fn inspect_operator(wallet: String) -> Result { + debug!("Getting information about operator {wallet} from brain."); + let mut client = BrainGeneralCliClient::connect(Config::get_brain_url()).await?; + Ok(client.inspect_operator(Pubkey { pubkey: wallet }).await?.into_inner()) +} + +pub async fn list_operators() -> Result, Error> { + debug!("Getting contracts from brain..."); + let mut client = BrainGeneralCliClient::connect(Config::get_brain_url()).await?; + let mut operators = Vec::new(); + let mut grpc_stream = client.list_operators(sign_request(Empty {})?).await?.into_inner(); + while let Some(stream_update) = grpc_stream.next().await { + match stream_update { + Ok(op) => { + operators.push(op); + } + Err(e) => { + warn!("Received error instead of operators: {e:?}"); + } + } + } + debug!("Brain terminated list_operators stream."); + Ok(operators) +} + +pub async fn kick_contract(contract_uuid: String, reason: String) -> Result { + debug!("gRPC module: connecting to brain and kicking contract {contract_uuid} for reason: {reason}"); + let mut client = BrainGeneralCliClient::connect(Config::get_brain_url()).await?; + Ok(client + .kick_contract(sign_request(KickReq { + operator_wallet: Config::get_detee_wallet()?, + contract_uuid, + reason, + })?) + .await? + .into_inner() + .nano_lp) +} + +pub async fn ban_user(user_wallet: String) -> Result<(), Error> { + debug!("Connecting to brain to ban user..."); + let mut client = BrainGeneralCliClient::connect(Config::get_brain_url()).await?; + client + .ban_user(sign_request(BanUserReq { + operator_wallet: Config::get_detee_wallet()?, + user_wallet, + })?) + .await?; + Ok(()) +} diff --git a/src/general/mod.rs b/src/general/mod.rs index 94d2e08..cfd66e5 100644 --- a/src/general/mod.rs +++ b/src/general/mod.rs @@ -1,4 +1,4 @@ pub mod cli_handler; +pub mod grpc; pub mod operators; pub mod packagers; -// pub mod grpc; diff --git a/src/general/operators.rs b/src/general/operators.rs index c33a2eb..ab9304f 100644 --- a/src/general/operators.rs +++ b/src/general/operators.rs @@ -1,5 +1,4 @@ -use crate::snp::grpc; -use crate::snp::grpc::brain; +use crate::general::grpc; use crate::utils::block_on; use tabled::Tabled; @@ -13,8 +12,8 @@ struct TabledOperator { reports: u64, } -impl From for TabledOperator { - fn from(brain_operator: grpc::brain::ListOperatorsResp) -> Self { +impl From for TabledOperator { + fn from(brain_operator: grpc::proto::ListOperatorsResp) -> Self { TabledOperator { wallet: brain_operator.pubkey, escrow: brain_operator.escrow, @@ -31,7 +30,7 @@ pub fn register(escrow: u64, email: String) -> Result Result { +pub fn inspect_operator(wallet: String) -> Result { block_on(grpc::inspect_operator(wallet)) } -impl crate::HumanOutput for Vec { +impl crate::HumanOutput for Vec { fn human_cli_print(&self) { let operators: Vec = self.iter().map(|op| op.clone().into()).collect(); @@ -73,7 +68,7 @@ impl crate::HumanOutput for Vec { } } -pub fn print_operators() -> Result, grpc::Error> { +pub fn print_operators() -> Result, grpc::Error> { block_on(grpc::list_operators()) } diff --git a/src/snp/grpc.rs b/src/snp/grpc.rs index b689343..f40f677 100644 --- a/src/snp/grpc.rs +++ b/src/snp/grpc.rs @@ -8,12 +8,10 @@ pub mod brain { use crate::config::Config; use crate::snp::brain::Account; use crate::snp::brain::AccountBalance; -use crate::snp::brain::InspectOperatorResp; use crate::snp::brain::Pubkey; use brain::{ - brain_vm_cli_client::BrainVmCliClient, BanUserReq, DeleteVmReq, Empty, ExtendVmReq, KickReq, - ListOperatorsResp, ListVmContractsReq, NewVmReq, NewVmResp, RegOperatorReq, ReportNodeReq, - UpdateVmReq, UpdateVmResp, VmContract, VmNodeFilters, VmNodeListResp, + brain_vm_cli_client::BrainVmCliClient, DeleteVmReq, ExtendVmReq, ListVmContractsReq, NewVmReq, + NewVmResp, ReportNodeReq, UpdateVmReq, UpdateVmResp, VmContract, VmNodeFilters, VmNodeListResp, }; use detee_shared::general_proto::brain_general_cli_client::BrainGeneralCliClient; use lazy_static::lazy_static; @@ -175,70 +173,6 @@ pub async fn report_node( Ok(()) } -pub async fn inspect_operator(wallet: String) -> Result { - debug!("Getting information about operator {wallet} from brain."); - let mut client = BrainGeneralCliClient::connect(Config::get_brain_url()).await?; - Ok(client.inspect_operator(Pubkey { pubkey: wallet }).await?.into_inner()) -} - -pub async fn list_operators() -> Result, Error> { - debug!("Getting contracts from brain..."); - let mut client = BrainGeneralCliClient::connect(Config::get_brain_url()).await?; - let mut operators = Vec::new(); - let mut grpc_stream = client.list_operators(sign_request(Empty {})?).await?.into_inner(); - while let Some(stream_update) = grpc_stream.next().await { - match stream_update { - Ok(op) => { - operators.push(op); - } - Err(e) => { - warn!("Received error instead of operators: {e:?}"); - } - } - } - debug!("Brain terminated list_operators stream."); - Ok(operators) -} - -pub async fn register_operator(escrow: u64, email: String) -> Result<(), Error> { - debug!("Connecting to brain to register operator..."); - let mut client = BrainGeneralCliClient::connect(Config::get_brain_url()).await?; - client - .register_operator(sign_request(RegOperatorReq { - pubkey: Config::get_detee_wallet()?, - escrow, - email, - })?) - .await?; - Ok(()) -} - -pub async fn kick_contract(contract_uuid: String, reason: String) -> Result { - debug!("gRPC module: connecting to brain and kicking contract {contract_uuid} for reason: {reason}"); - let mut client = BrainGeneralCliClient::connect(Config::get_brain_url()).await?; - Ok(client - .kick_contract(sign_request(KickReq { - operator_wallet: Config::get_detee_wallet()?, - contract_uuid, - reason, - })?) - .await? - .into_inner() - .nano_lp) -} - -pub async fn ban_user(user_wallet: String) -> Result<(), Error> { - debug!("Connecting to brain to ban user..."); - let mut client = BrainGeneralCliClient::connect(Config::get_brain_url()).await?; - client - .ban_user(sign_request(BanUserReq { - operator_wallet: Config::get_detee_wallet()?, - user_wallet, - })?) - .await?; - Ok(()) -} - pub async fn list_contracts(req: ListVmContractsReq) -> Result, Error> { debug!("Getting contracts from brain..."); let mut client = BrainVmCliClient::connect(Config::get_brain_url()).await?;