refactor: reorganize gRPC module and update operator handling
This commit is contained in:
parent
a3d604845b
commit
62999d2469
98
src/general/grpc.rs
Normal file
98
src/general/grpc.rs
Normal file
@ -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<InspectOperatorResp, Error> {
|
||||
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<Vec<ListOperatorsResp>, 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<u64, Error> {
|
||||
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(())
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
pub mod cli_handler;
|
||||
pub mod grpc;
|
||||
pub mod operators;
|
||||
pub mod packagers;
|
||||
// pub mod grpc;
|
||||
|
@ -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<grpc::brain::ListOperatorsResp> for TabledOperator {
|
||||
fn from(brain_operator: grpc::brain::ListOperatorsResp) -> Self {
|
||||
impl From<grpc::proto::ListOperatorsResp> 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<crate::SimpleOutput, grpc:
|
||||
Ok(crate::SimpleOutput::from("Successfully registered you as operator."))
|
||||
}
|
||||
|
||||
impl crate::HumanOutput for brain::InspectOperatorResp {
|
||||
impl crate::HumanOutput for grpc::proto::InspectOperatorResp {
|
||||
fn human_cli_print(&self) {
|
||||
if let Some(op) = &self.operator {
|
||||
println!("The operator {} supplies {} nanoLP as escrow,", op.pubkey, op.escrow,);
|
||||
@ -54,15 +53,11 @@ impl crate::HumanOutput for brain::InspectOperatorResp {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: move non-SNP gRPC requrests out of the SNP module (currently in crate::snp::grpc)
|
||||
// This is an example of why we need refactoring... requesting information about operators is not
|
||||
// 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<brain::InspectOperatorResp, grpc::Error> {
|
||||
pub fn inspect_operator(wallet: String) -> Result<grpc::proto::InspectOperatorResp, grpc::Error> {
|
||||
block_on(grpc::inspect_operator(wallet))
|
||||
}
|
||||
|
||||
impl crate::HumanOutput for Vec<brain::ListOperatorsResp> {
|
||||
impl crate::HumanOutput for Vec<grpc::proto::ListOperatorsResp> {
|
||||
fn human_cli_print(&self) {
|
||||
let operators: Vec<TabledOperator> = self.iter().map(|op| op.clone().into()).collect();
|
||||
|
||||
@ -73,7 +68,7 @@ impl crate::HumanOutput for Vec<brain::ListOperatorsResp> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn print_operators() -> Result<Vec<brain::ListOperatorsResp>, grpc::Error> {
|
||||
pub fn print_operators() -> Result<Vec<grpc::proto::ListOperatorsResp>, grpc::Error> {
|
||||
block_on(grpc::list_operators())
|
||||
}
|
||||
|
||||
|
@ -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<InspectOperatorResp, Error> {
|
||||
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<Vec<ListOperatorsResp>, 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<u64, Error> {
|
||||
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<Vec<VmContract>, Error> {
|
||||
debug!("Getting contracts from brain...");
|
||||
let mut client = BrainVmCliClient::connect(Config::get_brain_url()).await?;
|
||||
|
Loading…
Reference in New Issue
Block a user