forked from ghe0/brain-mock
fix: locked fund
fix: node offline error on cli revert locked fund when app deploye failed delete app contract returns contract's locked LP into admin
This commit is contained in:
parent
41352f2c33
commit
ddbde12f42
52
src/data.rs
52
src/data.rs
@ -38,6 +38,9 @@ pub enum Error {
|
||||
ImpossibleError,
|
||||
#[error("You don't have the required permissions for this operation.")]
|
||||
AccessDenied,
|
||||
|
||||
#[error("Could not find contract {0}")]
|
||||
AppContractNotFound(String),
|
||||
}
|
||||
|
||||
#[derive(Clone, Default, Serialize, Deserialize, Debug)]
|
||||
@ -1196,9 +1199,13 @@ impl BrainData {
|
||||
});
|
||||
}
|
||||
|
||||
pub fn find_app_contract_by_uuid(&self, uuid: &str) -> Option<AppContract> {
|
||||
pub fn find_app_contract_by_uuid(&self, uuid: &str) -> Result<AppContract, Error> {
|
||||
let contracts = self.app_contracts.read().unwrap();
|
||||
contracts.iter().find(|c| c.uuid == uuid).cloned()
|
||||
contracts
|
||||
.iter()
|
||||
.find(|c| c.uuid == uuid)
|
||||
.cloned()
|
||||
.ok_or(Error::AppContractNotFound(uuid.to_string()))
|
||||
}
|
||||
|
||||
pub fn find_app_node_by_pubkey(&self, public_key: &str) -> Option<AppNode> {
|
||||
@ -1349,15 +1356,25 @@ impl BrainData {
|
||||
})
|
||||
.await;
|
||||
}
|
||||
} else {
|
||||
self.send_new_container_resp(NewAppRes {
|
||||
status: "failed".to_string(),
|
||||
error: "Daemon is offline.".to_string(),
|
||||
uuid: req.uuid,
|
||||
..Default::default()
|
||||
})
|
||||
.await;
|
||||
}
|
||||
// TODO: implement daemon offline handling
|
||||
}
|
||||
|
||||
pub async fn send_del_container_req(
|
||||
&self,
|
||||
req: DelAppReq,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
if let Some(app_contract) = self.find_app_contract_by_uuid(&req.uuid) {
|
||||
pub async fn send_del_container_req(&self, req: DelAppReq) -> Result<(), Error> {
|
||||
log::debug!("Starting deletion of app {}", req.uuid);
|
||||
let app_contract = self.find_app_contract_by_uuid(&req.uuid)?;
|
||||
|
||||
if app_contract.admin_pubkey != req.admin_pubkey {
|
||||
return Err(Error::AccessDenied);
|
||||
}
|
||||
|
||||
info!("Found app contract {}. Deleting...", &req.uuid);
|
||||
if let Some(app_daemon_tx) = self.app_daemon_tx.get(&app_contract.node_pubkey) {
|
||||
debug!(
|
||||
@ -1365,11 +1382,7 @@ impl BrainData {
|
||||
app_contract.node_pubkey, &req.uuid
|
||||
);
|
||||
let msg = BrainMessageApp {
|
||||
msg: Some(
|
||||
detee_shared::sgx::pb::brain::brain_message_app::Msg::DeleteAppReq(
|
||||
req.clone(),
|
||||
),
|
||||
),
|
||||
msg: Some(brain_message_app::Msg::DeleteAppReq(req.clone())),
|
||||
};
|
||||
|
||||
if let Err(e) = app_daemon_tx.send(msg).await {
|
||||
@ -1382,13 +1395,11 @@ impl BrainData {
|
||||
}
|
||||
}
|
||||
|
||||
self.add_nano_to_wallet(&app_contract.admin_pubkey, app_contract.locked_nano);
|
||||
let mut app_contracts = self.app_contracts.write().unwrap();
|
||||
app_contracts.retain(|c| c.uuid != req.uuid);
|
||||
|
||||
Ok(())
|
||||
} else {
|
||||
Err("Contract not found".into())
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn send_new_container_resp(&self, new_container_resp: NewAppRes) {
|
||||
@ -1412,9 +1423,18 @@ impl BrainData {
|
||||
}
|
||||
|
||||
if new_container_resp.error != "" {
|
||||
if let Some(mut admin_wallet) = self.accounts.get_mut(&new_container_req.0.admin_pubkey)
|
||||
{
|
||||
admin_wallet.balance += new_container_req.0.locked_nano;
|
||||
admin_wallet.tmp_locked -= new_container_req.0.locked_nano;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if let Some(mut admin_wallet) = self.accounts.get_mut(&new_container_req.0.admin_pubkey) {
|
||||
admin_wallet.tmp_locked -= new_container_req.0.locked_nano;
|
||||
}
|
||||
|
||||
let requested_resource = new_container_req.0.resource.clone().unwrap_or_default();
|
||||
|
||||
let app_contracts = AppContract {
|
||||
|
Loading…
Reference in New Issue
Block a user