diff --git a/src/data.rs b/src/data.rs index 7e3809b..de4ba57 100644 --- a/src/data.rs +++ b/src/data.rs @@ -49,7 +49,7 @@ pub enum Error { pub struct AccountData { pub balance: u64, pub tmp_locked: u64, - // holds reasons why VMs of this account got kicked + // holds reasons why Contracts of this account got kicked pub kicked_for: Vec, pub last_kick: chrono::DateTime, // holds accounts that banned this account @@ -510,7 +510,6 @@ impl BrainData { nodes.push(node); } - // todo: this should also support Apps /// Receives: operator, contract uuid, reason of kick pub async fn kick_contract( &self, @@ -519,17 +518,46 @@ impl BrainData { reason: &str, ) -> Result { log::debug!("Operator {operator} requested a kick of {uuid} for reason: {reason}"); - let contract = self.find_contract_by_uuid(uuid)?; + + let (admin_pubkey, node_pubkey, updated_at, price_per_mint, is_vm) = + match self.find_any_contract_by_uuid(uuid) { + Ok((Some(vm), _)) => { + let price_per_mint = vm.price_per_minute(); + ( + vm.admin_pubkey, + vm.node_pubkey, + vm.updated_at, + price_per_mint, + true, + ) + } + Ok((_, Some(app))) => { + let price_per_mint = app.price_per_minute(); + + ( + app.admin_pubkey, + app.node_pubkey, + app.updated_at, + price_per_mint, + false, + ) + } + _ => { + log::error!("Could not find contract {uuid}"); + return Err(Error::ContractNotFound(uuid.to_string())); + } + }; + let mut operator_data = self .operators .get_mut(operator) .ok_or(Error::AccessDenied)?; - if !operator_data.vm_nodes.contains(&contract.node_pubkey) { + if !operator_data.vm_nodes.contains(&node_pubkey) { return Err(Error::AccessDenied); } let mut minutes_to_refund = chrono::Utc::now() - .signed_duration_since(contract.updated_at) + .signed_duration_since(updated_at) .num_minutes() .abs() as u64; // cap refund at 1 week @@ -537,10 +565,10 @@ impl BrainData { minutes_to_refund = 10080; } - let mut refund_amount = minutes_to_refund * contract.price_per_minute(); + let mut refund_amount = minutes_to_refund * price_per_mint; let mut admin_account = self .accounts - .get_mut(&contract.admin_pubkey) + .get_mut(&admin_pubkey) .ok_or(Error::ImpossibleError)?; // check if he got kicked within the last day @@ -564,15 +592,22 @@ impl BrainData { admin_account.kicked_for.push(reason.to_string()); operator_data.escrow -= refund_amount; - let admin_pubkey = contract.admin_pubkey.clone(); + let admin_pubkey = admin_pubkey.clone(); drop(admin_account); - drop(contract); - self.delete_vm(grpc::DeleteVmReq { - uuid: uuid.to_string(), - admin_pubkey, - }) - .await?; + if is_vm { + self.delete_vm(grpc::DeleteVmReq { + uuid: uuid.to_string(), + admin_pubkey, + }) + .await?; + } else { + self.send_del_container_req(DelAppReq { + uuid: uuid.to_string(), + admin_pubkey, + }) + .await?; + } Ok(refund_amount) }