App engine features #1

Merged
noormohammedb merged 4 commits from app_engine_features into staging 2025-04-15 11:40:29 +00:00
Showing only changes of commit d7da1824cf - Show all commits

@ -49,7 +49,7 @@ pub enum Error {
pub struct AccountData { pub struct AccountData {
pub balance: u64, pub balance: u64,
pub tmp_locked: 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<String>, pub kicked_for: Vec<String>,
pub last_kick: chrono::DateTime<Utc>, pub last_kick: chrono::DateTime<Utc>,
// holds accounts that banned this account // holds accounts that banned this account
@ -510,7 +510,6 @@ impl BrainData {
nodes.push(node); nodes.push(node);
} }
// todo: this should also support Apps
/// Receives: operator, contract uuid, reason of kick /// Receives: operator, contract uuid, reason of kick
pub async fn kick_contract( pub async fn kick_contract(
&self, &self,
@ -519,17 +518,46 @@ impl BrainData {
reason: &str, reason: &str,
) -> Result<u64, Error> { ) -> Result<u64, Error> {
log::debug!("Operator {operator} requested a kick of {uuid} for reason: {reason}"); 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 let mut operator_data = self
.operators .operators
.get_mut(operator) .get_mut(operator)
.ok_or(Error::AccessDenied)?; .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); return Err(Error::AccessDenied);
} }
let mut minutes_to_refund = chrono::Utc::now() let mut minutes_to_refund = chrono::Utc::now()
.signed_duration_since(contract.updated_at) .signed_duration_since(updated_at)
.num_minutes() .num_minutes()
.abs() as u64; .abs() as u64;
// cap refund at 1 week // cap refund at 1 week
@ -537,10 +565,10 @@ impl BrainData {
minutes_to_refund = 10080; 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 let mut admin_account = self
.accounts .accounts
.get_mut(&contract.admin_pubkey) .get_mut(&admin_pubkey)
.ok_or(Error::ImpossibleError)?; .ok_or(Error::ImpossibleError)?;
// check if he got kicked within the last day // check if he got kicked within the last day
@ -564,15 +592,22 @@ impl BrainData {
admin_account.kicked_for.push(reason.to_string()); admin_account.kicked_for.push(reason.to_string());
operator_data.escrow -= refund_amount; operator_data.escrow -= refund_amount;
let admin_pubkey = contract.admin_pubkey.clone(); let admin_pubkey = admin_pubkey.clone();
drop(admin_account); drop(admin_account);
drop(contract);
self.delete_vm(grpc::DeleteVmReq { if is_vm {
uuid: uuid.to_string(), self.delete_vm(grpc::DeleteVmReq {
admin_pubkey, uuid: uuid.to_string(),
}) admin_pubkey,
.await?; })
.await?;
} else {
self.send_del_container_req(DelAppReq {
uuid: uuid.to_string(),
admin_pubkey,
})
.await?;
}
Ok(refund_amount) Ok(refund_amount)
} }