refactor: General grpc update client
reorganize proto imports fix rename Brain vm client
This commit is contained in:
		
							parent
							
								
									6bf97faac3
								
							
						
					
					
						commit
						aeca70e062
					
				
							
								
								
									
										2
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										2
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							| @ -1109,7 +1109,7 @@ dependencies = [ | ||||
| [[package]] | ||||
| name = "detee-shared" | ||||
| version = "0.1.0" | ||||
| source = "git+ssh://git@gitea.detee.cloud/testnet/proto.git?branch=main#be4e41db050c6d59e9fb5abf47e647f5bbdc24b2" | ||||
| source = "git+ssh://git@gitea.detee.cloud/testnet/proto.git?branch=main#cf0c9a2c0d2edf9254f25c6faa7494afcfa00d64" | ||||
| dependencies = [ | ||||
|  "base64", | ||||
|  "prost", | ||||
|  | ||||
| @ -43,14 +43,14 @@ impl super::HumanOutput for brain::InspectOperatorResp { | ||||
|             } | ||||
|             None => (), | ||||
|         } | ||||
|         if self.nodes.len() == 0 { | ||||
|         if self.vm_nodes.len() == 0 { | ||||
|             return; | ||||
|         } | ||||
|         println!("\n-- VM NODES --"); | ||||
|         let mut count = 1; | ||||
|         for node in self.nodes.iter() { | ||||
|         for vm_node in self.vm_nodes.iter() { | ||||
|             println!("\nNODE #{count}:"); | ||||
|             node.human_cli_print(); | ||||
|             vm_node.human_cli_print(); | ||||
|             count += 1; | ||||
|         } | ||||
|     } | ||||
|  | ||||
| @ -1,5 +1,5 @@ | ||||
| use detee_shared::sgx::pb::brain::brain_app_cli_client::BrainAppCliClient; | ||||
| use detee_shared::sgx::pb::brain::{ | ||||
| use detee_shared::app_proto::brain_app_cli_client::BrainAppCliClient; | ||||
| use detee_shared::app_proto::{ | ||||
|     AppContract, AppNodeFilters, AppNodeListResp, DelAppReq, ListAppContractsReq, NewAppReq, | ||||
|     NewAppRes, | ||||
| }; | ||||
|  | ||||
| @ -1,12 +1,12 @@ | ||||
| use detee_sgx::{prelude::*, HRaTlsConfigBuilder}; | ||||
| use detee_shared::sgx::pb::dtpm::DtpmGetConfigReq; | ||||
| use detee_shared::sgx::pb::dtpm_proto::DtpmGetConfigReq; | ||||
| use hyper_rustls::HttpsConnectorBuilder; | ||||
| use rustls::ClientConfig; | ||||
| use std::sync::{Arc, RwLock}; | ||||
| use tonic::transport::{Channel, Endpoint}; | ||||
| 
 | ||||
| use detee_shared::sgx::{ | ||||
|     pb::dtpm::{ | ||||
|     pb::dtpm_proto::{ | ||||
|         dtpm_config_manager_client::DtpmConfigManagerClient, DtpmConfigData, DtpmSetConfigReq, | ||||
|     }, | ||||
|     types::dtpm::DtpmConfig, | ||||
|  | ||||
| @ -1,8 +1,8 @@ | ||||
| use detee_shared::sgx::{ | ||||
|     pb::brain::{ | ||||
| use detee_shared::{ | ||||
|     app_proto::{ | ||||
|         AppContract as AppContractPB, AppNodeFilters, AppNodeListResp, AppResource, NewAppRes, | ||||
|     }, | ||||
|     types::brain::Resource, | ||||
|     sgx::types::brain::Resource, | ||||
| }; | ||||
| use grpc_brain::get_one_app_node; | ||||
| 
 | ||||
|  | ||||
| @ -1,6 +1,8 @@ | ||||
| pub mod brain { | ||||
|     // tonic::include_proto!("vm_proto");
 | ||||
|     pub use detee_shared::snp::pb::vm::*; | ||||
|     pub use detee_shared::common_proto::*; | ||||
|     pub use detee_shared::general_proto::*; | ||||
|     pub use detee_shared::vm_proto::*; | ||||
| } | ||||
| 
 | ||||
| use crate::config::Config; | ||||
| @ -9,10 +11,11 @@ use crate::snp::brain::AccountBalance; | ||||
| use crate::snp::brain::InspectOperatorResp; | ||||
| use crate::snp::brain::Pubkey; | ||||
| use brain::{ | ||||
|     brain_cli_client::BrainCliClient, BanUserReq, DeleteVmReq, Empty, ExtendVmReq, KickReq, | ||||
|     brain_vm_cli_client::BrainVmCliClient, BanUserReq, DeleteVmReq, Empty, ExtendVmReq, KickReq, | ||||
|     ListOperatorsResp, ListVmContractsReq, NewVmReq, NewVmResp, RegOperatorReq, ReportNodeReq, | ||||
|     UpdateVmReq, UpdateVmResp, VmContract, VmNodeFilters, VmNodeListResp, | ||||
| }; | ||||
| use detee_shared::general_proto::brain_general_cli_client::BrainGeneralCliClient; | ||||
| use lazy_static::lazy_static; | ||||
| use log::{debug, info, warn}; | ||||
| use tokio_stream::StreamExt; | ||||
| @ -99,7 +102,7 @@ fn sign_request<T: std::fmt::Debug>(req: T) -> Result<Request<T>, Error> { | ||||
| 
 | ||||
| pub async fn get_node_list(req: VmNodeFilters) -> Result<Vec<VmNodeListResp>, Error> { | ||||
|     debug!("Getting nodes from brain..."); | ||||
|     let mut client = BrainCliClient::connect(Config::get_brain_url()).await?; | ||||
|     let mut client = BrainVmCliClient::connect(Config::get_brain_url()).await?; | ||||
|     let mut nodes = Vec::new(); | ||||
|     let mut grpc_stream = client.list_vm_nodes(sign_request(req)?).await?.into_inner(); | ||||
|     while let Some(stream_update) = grpc_stream.next().await { | ||||
| @ -118,7 +121,7 @@ pub async fn get_node_list(req: VmNodeFilters) -> Result<Vec<VmNodeListResp>, Er | ||||
| } | ||||
| 
 | ||||
| pub async fn get_balance(account: &str) -> Result<AccountBalance, Error> { | ||||
|     let mut client = BrainCliClient::connect(Config::get_brain_url()).await?; | ||||
|     let mut client = BrainGeneralCliClient::connect(Config::get_brain_url()).await?; | ||||
|     let response = | ||||
|         client.get_balance(sign_request(Pubkey { pubkey: account.to_string() })?).await?; | ||||
|     log::info!("Received account from brain: {response:?}"); | ||||
| @ -126,27 +129,27 @@ pub async fn get_balance(account: &str) -> Result<AccountBalance, Error> { | ||||
| } | ||||
| 
 | ||||
| pub async fn admin_airdrop(pubkey: String, tokens: u64) -> Result<(), Error> { | ||||
|     let mut client = BrainCliClient::connect(Config::get_brain_url()).await?; | ||||
|     let mut client = BrainGeneralCliClient::connect(Config::get_brain_url()).await?; | ||||
|     let req = sign_request(brain::AirdropReq { pubkey, tokens })?; | ||||
|     let _ = client.airdrop(req).await?; | ||||
|     Ok(()) | ||||
| } | ||||
| 
 | ||||
| pub async fn admin_slash(pubkey: String, tokens: u64) -> Result<(), Error> { | ||||
|     let mut client = BrainCliClient::connect(Config::get_brain_url()).await?; | ||||
|     let mut client = BrainGeneralCliClient::connect(Config::get_brain_url()).await?; | ||||
|     let req = sign_request(brain::SlashReq { pubkey, tokens })?; | ||||
|     let _ = client.slash(req).await?; | ||||
|     Ok(()) | ||||
| } | ||||
| 
 | ||||
| pub async fn get_one_node(req: VmNodeFilters) -> Result<VmNodeListResp, Error> { | ||||
|     let mut client = BrainCliClient::connect(Config::get_brain_url()).await?; | ||||
|     let mut client = BrainVmCliClient::connect(Config::get_brain_url()).await?; | ||||
|     let response = client.get_one_vm_node(sign_request(req)?).await?; | ||||
|     Ok(response.into_inner()) | ||||
| } | ||||
| 
 | ||||
| pub async fn create_vm(req: NewVmReq) -> Result<NewVmResp, Error> { | ||||
|     let mut client = BrainCliClient::connect(Config::get_brain_url()).await?; | ||||
|     let mut client = BrainVmCliClient::connect(Config::get_brain_url()).await?; | ||||
|     debug!("Sending NewVmReq to brain: {req:?}"); | ||||
|     match client.new_vm(sign_request(req)?).await { | ||||
|         Ok(resp) => Ok(resp.into_inner()), | ||||
| @ -160,7 +163,7 @@ pub async fn report_node( | ||||
|     reason: String, | ||||
| ) -> Result<(), Error> { | ||||
|     debug!("Getting contracts from brain..."); | ||||
|     let mut client = BrainCliClient::connect(Config::get_brain_url()).await?; | ||||
|     let mut client = BrainGeneralCliClient::connect(Config::get_brain_url()).await?; | ||||
|     client | ||||
|         .report_node(sign_request(ReportNodeReq { | ||||
|             admin_pubkey: Config::get_detee_wallet()?, | ||||
| @ -174,13 +177,13 @@ pub async fn report_node( | ||||
| 
 | ||||
| pub async fn inspect_operator(wallet: String) -> Result<InspectOperatorResp, Error> { | ||||
|     debug!("Getting information about operator {wallet} from brain."); | ||||
|     let mut client = BrainCliClient::connect(Config::get_brain_url()).await?; | ||||
|     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 = BrainCliClient::connect(Config::get_brain_url()).await?; | ||||
|     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 { | ||||
| @ -199,7 +202,7 @@ pub async fn list_operators() -> Result<Vec<ListOperatorsResp>, Error> { | ||||
| 
 | ||||
| pub async fn register_operator(escrow: u64, email: String) -> Result<(), Error> { | ||||
|     debug!("Connecting to brain to register operator..."); | ||||
|     let mut client = BrainCliClient::connect(Config::get_brain_url()).await?; | ||||
|     let mut client = BrainGeneralCliClient::connect(Config::get_brain_url()).await?; | ||||
|     client | ||||
|         .register_operator(sign_request(RegOperatorReq { | ||||
|             pubkey: Config::get_detee_wallet()?, | ||||
| @ -212,7 +215,7 @@ pub async fn register_operator(escrow: u64, email: String) -> Result<(), Error> | ||||
| 
 | ||||
| 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 = BrainCliClient::connect(Config::get_brain_url()).await?; | ||||
|     let mut client = BrainGeneralCliClient::connect(Config::get_brain_url()).await?; | ||||
|     Ok(client | ||||
|         .kick_contract(sign_request(KickReq { | ||||
|             operator_wallet: Config::get_detee_wallet()?, | ||||
| @ -226,7 +229,7 @@ pub async fn kick_contract(contract_uuid: String, reason: String) -> Result<u64, | ||||
| 
 | ||||
| pub async fn ban_user(user_wallet: String) -> Result<(), Error> { | ||||
|     debug!("Connecting to brain to ban user..."); | ||||
|     let mut client = BrainCliClient::connect(Config::get_brain_url()).await?; | ||||
|     let mut client = BrainGeneralCliClient::connect(Config::get_brain_url()).await?; | ||||
|     client | ||||
|         .ban_user(sign_request(BanUserReq { | ||||
|             operator_wallet: Config::get_detee_wallet()?, | ||||
| @ -238,7 +241,7 @@ pub async fn ban_user(user_wallet: String) -> Result<(), Error> { | ||||
| 
 | ||||
| pub async fn list_contracts(req: ListVmContractsReq) -> Result<Vec<VmContract>, Error> { | ||||
|     debug!("Getting contracts from brain..."); | ||||
|     let mut client = BrainCliClient::connect(Config::get_brain_url()).await?; | ||||
|     let mut client = BrainVmCliClient::connect(Config::get_brain_url()).await?; | ||||
|     let mut contracts = Vec::new(); | ||||
|     let mut grpc_stream = client.list_vm_contracts(sign_request(req)?).await?.into_inner(); | ||||
|     while let Some(stream_update) = grpc_stream.next().await { | ||||
| @ -257,7 +260,7 @@ pub async fn list_contracts(req: ListVmContractsReq) -> Result<Vec<VmContract>, | ||||
| } | ||||
| 
 | ||||
| pub async fn admin_list_contracts() -> Result<Vec<VmContract>, Error> { | ||||
|     let mut client = BrainCliClient::connect(Config::get_brain_url()).await?; | ||||
|     let mut client = BrainGeneralCliClient::connect(Config::get_brain_url()).await?; | ||||
|     let mut contracts = Vec::new(); | ||||
|     let mut grpc_stream = | ||||
|         client.list_all_vm_contracts(sign_request(brain::Empty {})?).await?.into_inner(); | ||||
| @ -277,7 +280,7 @@ pub async fn admin_list_contracts() -> Result<Vec<VmContract>, Error> { | ||||
| } | ||||
| 
 | ||||
| pub async fn admin_list_accounts() -> Result<Vec<Account>, Error> { | ||||
|     let mut client = BrainCliClient::connect(Config::get_brain_url()).await?; | ||||
|     let mut client = BrainGeneralCliClient::connect(Config::get_brain_url()).await?; | ||||
|     let mut accounts = Vec::new(); | ||||
|     let mut grpc_stream = client.list_accounts(sign_request(brain::Empty {})?).await?.into_inner(); | ||||
|     while let Some(stream_update) = grpc_stream.next().await { | ||||
| @ -296,7 +299,7 @@ pub async fn admin_list_accounts() -> Result<Vec<Account>, Error> { | ||||
| } | ||||
| 
 | ||||
| pub async fn delete_vm(uuid: &str) -> Result<(), Error> { | ||||
|     let mut client = BrainCliClient::connect(Config::get_brain_url()).await?; | ||||
|     let mut client = BrainVmCliClient::connect(Config::get_brain_url()).await?; | ||||
|     let req = DeleteVmReq { uuid: uuid.to_string(), admin_pubkey: Config::get_detee_wallet()? }; | ||||
|     let result = client.delete_vm(sign_request(req)?).await; | ||||
|     match result { | ||||
| @ -312,7 +315,7 @@ pub async fn delete_vm(uuid: &str) -> Result<(), Error> { | ||||
| } | ||||
| 
 | ||||
| pub async fn extend_vm(uuid: String, admin_pubkey: String, locked_nano: u64) -> Result<(), Error> { | ||||
|     let mut client = BrainCliClient::connect(Config::get_brain_url()).await?; | ||||
|     let mut client = BrainVmCliClient::connect(Config::get_brain_url()).await?; | ||||
|     let req = ExtendVmReq { admin_pubkey, uuid, locked_nano }; | ||||
|     let result = client.extend_vm(sign_request(req)?).await; | ||||
|     match result { | ||||
| @ -334,7 +337,7 @@ pub async fn extend_vm(uuid: String, admin_pubkey: String, locked_nano: u64) -> | ||||
| 
 | ||||
| pub async fn update_vm(req: UpdateVmReq) -> Result<UpdateVmResp, Error> { | ||||
|     info!("Updating VM {req:?}"); | ||||
|     let mut client = BrainCliClient::connect(Config::get_brain_url()).await?; | ||||
|     let mut client = BrainVmCliClient::connect(Config::get_brain_url()).await?; | ||||
|     let result = client.update_vm(sign_request(req)?).await; | ||||
|     match result { | ||||
|         Ok(resp) => { | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user