-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
send canisters back on failure of creation of token
- Loading branch information
1 parent
34e0178
commit 5c26a88
Showing
24 changed files
with
997 additions
and
446 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/canister/individual_user_template/src/api/cdao/delete_all_sns_creator_token.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use ic_cdk::api::management_canister::main::{deposit_cycles, CanisterIdRecord}; | ||
use ic_cdk_macros::update; | ||
use shared_utils::{ | ||
canister_specific::individual_user_template::types::cdao::DeployedCdaoCanisters, | ||
common::utils::permissions::is_caller_controller, | ||
}; | ||
|
||
use crate::CANISTER_DATA; | ||
|
||
use super::{ | ||
request_cycles_from_subnet_orchestrator, | ||
utils::uninstall_code_and_return_empty_canisters_to_subnet_backup_pool, SubnetOrchestrator, | ||
}; | ||
|
||
#[update(guard = "is_caller_controller")] | ||
pub fn delete_all_creator_token() { | ||
CANISTER_DATA.with_borrow_mut(|canister_data| { | ||
canister_data | ||
.cdao_canisters | ||
.iter() | ||
.for_each(|deployed_cdao_canisters| { | ||
delete_sns_creator_token(deployed_cdao_canisters); | ||
}); | ||
|
||
canister_data.cdao_canisters = vec![]; | ||
}) | ||
} | ||
|
||
pub fn delete_sns_creator_token(deployed_canisters: &DeployedCdaoCanisters) { | ||
let canister_ids = deployed_canisters.get_canister_ids(); | ||
|
||
const UNINSTALL_RECHARGE_AMOUNT: u128 = 300_000_000_000; | ||
|
||
ic_cdk::spawn(async move { | ||
let _ = request_cycles_from_subnet_orchestrator(5 * UNINSTALL_RECHARGE_AMOUNT).await; | ||
for canister_id in canister_ids.iter() { | ||
deposit_cycles( | ||
CanisterIdRecord { | ||
canister_id: *canister_id, | ||
}, | ||
UNINSTALL_RECHARGE_AMOUNT, | ||
) | ||
.await; | ||
} | ||
uninstall_code_and_return_empty_canisters_to_subnet_backup_pool(canister_ids).await; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/canister/individual_user_template/src/api/cdao/utils.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use candid::Principal; | ||
use ic_cdk::api::management_canister::main::{uninstall_code, CanisterIdRecord}; | ||
use shared_utils::common::utils::task::run_task_concurrently; | ||
|
||
use crate::CANISTER_DATA; | ||
|
||
use super::SubnetOrchestrator; | ||
|
||
pub(crate) async fn uninstall_code_and_return_empty_canisters_to_subnet_backup_pool( | ||
canister_ids: Vec<Principal>, | ||
) { | ||
let mut failed_to_return_canister_ids = vec![]; | ||
let subnet_orchestrator_res = SubnetOrchestrator::new().inspect_err(|_e| { | ||
CANISTER_DATA.with_borrow_mut(|canister_data| { | ||
canister_data | ||
.empty_canisters | ||
.append_empty_canisters(canister_ids.clone()) | ||
}) | ||
}); | ||
|
||
if let Ok(subnet_orchestrator) = subnet_orchestrator_res { | ||
let uninstall_code_tasks = canister_ids.iter().map(|canister_id| async { | ||
uninstall_code(CanisterIdRecord { | ||
canister_id: *canister_id, | ||
}) | ||
.await | ||
.map_err(|e| (*canister_id, e.1)) | ||
}); | ||
|
||
let uninstall_callback = |uninstall_result: Result<(), (Principal, String)>| { | ||
if let Err(e) = uninstall_result { | ||
ic_cdk::println!("Error Uninstall Code from canister {}. Error: {}", e.0, e.1); | ||
failed_to_return_canister_ids.push(e.0); | ||
} | ||
}; | ||
|
||
run_task_concurrently(uninstall_code_tasks, 10, uninstall_callback, || false); | ||
|
||
let inserting_canisters_into_subnet_backup_pool_res = subnet_orchestrator | ||
.insert_into_backup_pool( | ||
canister_ids | ||
.into_iter() | ||
.filter(|canister_id| !failed_to_return_canister_ids.contains(canister_id)) | ||
.collect(), | ||
) | ||
.await; | ||
|
||
if let Err(e) = inserting_canisters_into_subnet_backup_pool_res { | ||
failed_to_return_canister_ids.extend(e.into_iter()); | ||
} | ||
|
||
CANISTER_DATA.with_borrow_mut(|canister_data| { | ||
canister_data | ||
.empty_canisters | ||
.append_empty_canisters(failed_to_return_canister_ids); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.