Skip to content

Commit

Permalink
feat: refactor upgrading creator-dao canisters and skip if upgrades i…
Browse files Browse the repository at this point in the history
…f deplo… (#479)

refactor uprading creator dao canisters and skip if upgrades if deployed versio is already present
  • Loading branch information
ravi-sawlani-yral authored Dec 6, 2024
1 parent e5bda38 commit d40432b
Show file tree
Hide file tree
Showing 15 changed files with 924 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ use shared_utils::{
SNS_TOKEN_INDEX_MODULE_HASH, SNS_TOKEN_LEDGER_MODULE_HASH, SNS_TOKEN_ROOT_MODULE_HASH,
SNS_TOKEN_SWAP_MODULE_HASH,
},
types::sns_canisters::sns_governance::{self, GetRunningSnsVersionArg},
};

#[update(guard = "is_caller_controller_or_global_admin")]
pub async fn upgrade_creator_dao_governance_canisters(wasm_module: Vec<u8>) -> Result<(), String> {
notify_to_recharge_canister();

let mut res: Result<(), String>;
let governance_canisters: Vec<candid::Principal> =
CANISTER_DATA.with_borrow_mut(|canister_data| {
canister_data
.cdao_canisters
.iter()
.map(|canisters| canisters.governance)
.collect()
});
let governance_canisters: Vec<candid::Principal> = CANISTER_DATA.with_borrow(|canister_data| {
canister_data
.cdao_canisters
.iter()
.map(|canisters| canisters.governance)
.collect()
});

let recharge_amount = 100_000_000_000; //100B

Expand Down Expand Up @@ -89,6 +89,17 @@ async fn recharge_and_upgrade_canister(
.await
.map_err(|e| e.1)?;

let sns_governance = sns_governance::Service(canister_id);

let (sns_running_version,) = sns_governance
.get_running_sns_version(GetRunningSnsVersionArg {})
.await
.map_err(|e| e.1)?;

if sns_running_version.deployed_version.is_some() {
return Ok(());
}

let gov_hash = hex::decode(SNS_TOKEN_GOVERNANCE_MODULE_HASH).unwrap();
let ledger_hash = hex::decode(SNS_TOKEN_LEDGER_MODULE_HASH).unwrap();
let root_hash = hex::decode(SNS_TOKEN_ROOT_MODULE_HASH).unwrap();
Expand Down
4 changes: 1 addition & 3 deletions src/canister/platform_orchestrator/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,6 @@ service : (PlatformOrchestratorInitArgs) -> {
make_individual_canister_logs_public : (principal) -> (Result);
make_subnet_orchestrator_logs_private : (principal) -> (Result);
make_subnet_orchestrator_logs_public : (principal) -> (Result);
notify_all_individual_canisters_to_upgrade_creator_dao_governance_canisters : (
blob,
) -> (Result);
notify_specific_individual_canister_to_upgrade_creator_dao_governance_canisters : (
principal,
blob,
Expand Down Expand Up @@ -146,6 +143,7 @@ service : (PlatformOrchestratorInitArgs) -> {
KnownPrincipalType,
principal,
) -> (Result_1);
upgrade_all_creator_dao_governance_canisters_in_the_network : (blob) -> ();
upgrade_canisters_in_network : (UpgradeCanisterArg) -> (Result_1);
upgrade_individual_canisters_in_a_subnet_with_latest_wasm : (principal) -> (
Result,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ mod get_subnets_upgrade_status_report;
mod global_admin;
mod known_principal;
pub mod logging;
pub mod notify_all_individual_canisters_to_upgrade_creator_dao_governance_canisters;
pub mod notify_specific_individual_canister_to_upgrade_creator_dao_governance_canisters;
mod populate_known_principal_for_all_subnet;
pub mod provision_empty_canisters_in_a_subnet;
Expand All @@ -30,6 +29,7 @@ mod subnet_orchestrator_maxed_out;
mod update_canisters_last_access_time;
mod update_profile_owner_for_individual_users;
pub mod update_timers_for_hon_game;
pub mod upgrade_all_creator_dao_governance_canisters_in_the_network;
pub mod upgrade_canisters_in_network;
mod upgrade_individual_canisters_in_a_subnet_with_latest_wasm;
mod upgrade_specific_individual_canister;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use ic_cdk_macros::update;
use shared_utils::common::{types::wasm, utils::task::run_task_concurrently};

use crate::{guard::is_caller::is_caller_global_admin_or_controller, CANISTER_DATA};

#[update(guard = "is_caller_global_admin_or_controller")]
pub fn upgrade_all_creator_dao_governance_canisters_in_the_network(wasm_module: Vec<u8>) {
let subnet_orchestrators = CANISTER_DATA
.with_borrow(|canister_data| canister_data.all_subnet_orchestrator_canisters_list.clone());

let upgrade_governance_canister_tasks =
subnet_orchestrators
.into_iter()
.map(move |subnet_orchestrator| {
let wasm = wasm_module.clone();
async move {
ic_cdk::call::<_, ()>(
subnet_orchestrator,
"upgrade_all_creator_dao_governance_canisters_in_the_network",
(wasm,),
)
.await
.map_err(|e| e.1)
}
});

ic_cdk::spawn(run_task_concurrently(
upgrade_governance_canister_tasks,
10,
|result| {
if let Err(e) = result {
ic_cdk::println!("Error upgrading governance canister in the subnet. {}", e);
}
},
|| false,
));
}
4 changes: 1 addition & 3 deletions src/canister/user_index/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,6 @@ service : (UserIndexInitArgs) -> {
issue_rewards_for_referral : (principal, principal, principal) -> (Result_2);
make_individual_canister_logs_private : (principal) -> (Result_3);
make_individual_canister_logs_public : (principal) -> (Result_3);
notify_all_individual_canisters_to_upgrade_creator_dao_governance_canisters : (
blob,
) -> (Result_3);
notify_specific_individual_canister_to_upgrade_creator_dao_governance_canisters : (
principal,
blob,
Expand Down Expand Up @@ -199,6 +196,7 @@ service : (UserIndexInitArgs) -> {
update_profile_owner_for_individual_canisters : () -> ();
update_restart_timers_hon_game : () -> (text);
update_well_known_principal : (KnownPrincipalType, principal) -> ();
upgrade_all_creator_dao_governance_canisters_in_the_network : (blob) -> ();
upgrade_specific_individual_user_canister_with_latest_wasm : (
principal,
opt principal,
Expand Down
2 changes: 1 addition & 1 deletion src/canister/user_index/src/api/canister_management/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ pub mod get_subnet_available_capacity;
pub mod get_subnet_backup_capacity;
pub mod make_individual_canister_logs_private;
pub mod make_individual_canister_logs_public;
pub mod notify_all_individual_canisters_to_upgrade_creator_dao_governance_canisters;
pub mod notify_specific_individual_canister_to_upgrade_creator_dao_governance_canisters;
pub mod provision_empty_canisters;
pub mod receive_empty_canister_from_individual_canister;
Expand All @@ -50,6 +49,7 @@ pub mod reset_user_canister_ml_feed_cache;
pub mod start_upgrades_for_individual_canisters;
pub mod update_canisters_access_time;
pub mod update_user_canister_restart_timers;
pub mod upgrade_all_creator_dao_governance_canisters_in_the_network;

#[update]
pub async fn get_user_canister_status(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use candid::Principal;
use ic_cdk_macros::update;
use shared_utils::common::utils::{
permissions::is_caller_controller_or_global_admin, task::run_task_concurrently,
};

use crate::CANISTER_DATA;

#[update(guard = "is_caller_controller_or_global_admin")]
pub fn upgrade_all_creator_dao_governance_canisters_in_the_network(wasm_module: Vec<u8>) {
let individual_canisters: Vec<Principal> = CANISTER_DATA.with_borrow(|canister_data| {
canister_data
.user_principal_id_to_canister_id_map
.values()
.cloned()
.collect()
});

let upgrade_governance_canister_tasks =
individual_canisters.into_iter().map(move |canister_id| {
let wasm = wasm_module.clone();
async move {
ic_cdk::call::<_, (Result<(), String>,)>(
canister_id,
"upgrade_creator_dao_governance_canisters",
(wasm,),
)
.await
.map_err(|e| format!("Error: {:?}", e))?
.0
}
});

ic_cdk::spawn(run_task_concurrently(
upgrade_governance_canister_tasks,
10,
|result| {
if let Err(e) = result {
ic_cdk::println!("Error upgrading governance canister. Error: {}", e);
}
},
|| false,
));
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,37 +197,37 @@ async fn provision_new_available_canisters(individual_user_template_canister_was
CANISTER_DATA.with_borrow(|canister_data| canister_data.available_canisters.len() as u64);
let max_canister_count = available_canister_count + canister_count;

let install_canister_wasm_futures = CANISTER_DATA.with_borrow(|canister_data| {
let mut backup_pool_canister = canister_data.backup_canister_pool.clone().into_iter();
(0..canister_count).map(move |_| {
let individual_user_template_canister_wasm_version =
individual_user_template_canister_wasm.version.clone();
let mut backup_pool_canister = CANISTER_DATA
.with_borrow(|canister_data| canister_data.backup_canister_pool.clone())
.into_iter();

let individual_user_template_canister_wasm =
individual_user_template_canister_wasm.wasm_blob.clone();
let install_canister_wasm_futures = (0..canister_count).map(move |_| {
let individual_user_template_canister_wasm_version =
individual_user_template_canister_wasm.version.clone();

let canister_id = backup_pool_canister.next().unwrap();
let individual_user_template_canister_wasm =
individual_user_template_canister_wasm.wasm_blob.clone();

// Remove the canister id from backup pool so no one else access it
CANISTER_DATA.with_borrow_mut(|canister_data| {
canister_data.backup_canister_pool.remove(&canister_id)
});
let canister_id = backup_pool_canister.next().unwrap();

async move {
let _ = check_and_request_cycles_from_platform_orchestrator().await;
//recharge backup canister if required
recharge_canister_for_installing_wasm(canister_id)
.await
.map_err(|e| (canister_id, e))?;
install_canister_wasm(
canister_id,
None,
individual_user_template_canister_wasm_version,
individual_user_template_canister_wasm,
)
CANISTER_DATA.with_borrow_mut(|canister_data| {
canister_data.backup_canister_pool.remove(&canister_id)
});

async move {
let _ = check_and_request_cycles_from_platform_orchestrator().await;
//recharge backup canister if required
recharge_canister_for_installing_wasm(canister_id)
.await
}
})
.map_err(|e| (canister_id, e))?;
install_canister_wasm(
canister_id,
None,
individual_user_template_canister_wasm_version,
individual_user_template_canister_wasm,
)
.await
}
});

let result_callback = |install_canister_wasm_result: Result<Principal, (Principal, String)>| {
Expand Down
1 change: 1 addition & 0 deletions src/lib/integration_tests/tests/creator_dao/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod test_custom_governance_upgrade;
pub mod test_deletion_of_creator_tokens;
pub mod test_number_of_creator_tokens;
pub mod types;
Expand Down
Loading

0 comments on commit d40432b

Please sign in to comment.