Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…d-canister into airdrop_amt_fix
  • Loading branch information
JoeruCodes committed Nov 11, 2024
2 parents 6ef9f15 + 90197f7 commit 2b9b2fb
Show file tree
Hide file tree
Showing 18 changed files with 398 additions and 72 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ serde_json = "1.0.113"
ic-sns-init = { git = "https://github.com/dfinity/ic", rev = "tags/release-2024-05-29_23-02-base" }
ic-sns-root = { git = "https://github.com/dfinity/ic", rev = "tags/release-2024-05-29_23-02-base" }
ic-sns-wasm = { git = "https://github.com/dfinity/ic", rev = "tags/release-2024-05-29_23-02-base" }
ic-sns-governance = { git = "https://github.com/dfinity/ic", rev = "tags/release-2024-05-29_23-02-base" }
ic-base-types = { git = "https://github.com/dfinity/ic", rev = "tags/release-2024-05-29_23-02-base" }
ic-nns-constants = { git = "https://github.com/dfinity/ic", rev = "tags/release-2024-05-29_23-02-base" }
ic-nervous-system-proto = { git = "https://github.com/dfinity/ic", rev = "tags/release-2024-05-29_23-02-base" }
Expand Down
1 change: 1 addition & 0 deletions src/canister/individual_user_template/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ futures = { workspace = true }
ic-sns-init.workspace = true
ic-sns-root.workspace = true
ic-sns-wasm.workspace = true
ic-sns-governance.workspace = true
ic-nns-governance.workspace = true
ic-base-types.workspace = true
ic-nns-constants.workspace = true
Expand Down
1 change: 1 addition & 0 deletions src/canister/individual_user_template/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,7 @@ service : (IndividualUserTemplateInitArgs) -> {
update_user_propensity : (float64) -> (Result_25);
update_watch_history : (WatchHistoryItem) -> (Result_25);
update_well_known_principal : (KnownPrincipalType, principal) -> ();
upgrade_creator_dao_governance_canisters : (blob) -> (Result_27);
write_key_value_pair : (nat64, text, text) -> (Result_5);
write_multiple_key_value_pairs : (nat64, vec record { text; text }) -> (
Result_6,
Expand Down
14 changes: 11 additions & 3 deletions src/canister/individual_user_template/src/api/cdao/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod token;
mod airdrop;
mod token;
use std::collections::{HashMap, HashSet, VecDeque};

use candid::{Encode, Principal};
Expand Down Expand Up @@ -29,7 +29,11 @@ use ic_nns_governance::pb::v1::{
use shared_utils::{
canister_specific::individual_user_template::{
consts::CDAO_TOKEN_LIMIT,
types::{cdao::{AirdropInfo, DeployedCdaoCanisters}, error::CdaoDeployError, session::SessionType},
types::{
cdao::{AirdropInfo, DeployedCdaoCanisters},
error::CdaoDeployError,
session::SessionType,
},
},
common::types::known_principal::KnownPrincipalType,
constant::{NNS_LEDGER_CANISTER_ID, USER_SNS_CANISTER_INITIAL_CYCLES},
Expand All @@ -42,6 +46,8 @@ use crate::{
CANISTER_DATA,
};

pub mod upgrade_creator_dao_governance_canisters;

#[update]
pub async fn settle_neurons_fund_participation(
request: SettleNeuronsFundParticipationRequest,
Expand Down Expand Up @@ -274,7 +280,9 @@ async fn deploy_cdao_sns(
root: root.0,
swap: swap.0,
index: index.0,
airdrop_info: AirdropInfo { principals_who_successfully_claimed: HashMap::new() },
airdrop_info: AirdropInfo {
principals_who_successfully_claimed: HashMap::new(),
},
};
CANISTER_DATA.with(|cdata| {
let mut cdata = cdata.borrow_mut();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
use crate::{util::cycles::request_cycles_from_subnet_orchestrator, CANISTER_DATA};
use candid::Principal;
use ic_cdk::api::management_canister::main::{
deposit_cycles, CanisterIdRecord, CanisterInstallMode, InstallCodeArgument,
};

use ic_cdk_macros::update;
use ic_sns_governance::{init::GovernanceCanisterInitPayloadBuilder, pb::v1::governance::Version};

use shared_utils::{
common::utils::{
permissions::is_caller_controller_or_global_admin, task::run_task_concurrently,
upgrade_canister::upgrade_canister_util,
},
constant::{
SNS_TOKEN_ARCHIVE_MODULE_HASH, SNS_TOKEN_GOVERNANCE_MODULE_HASH,
SNS_TOKEN_INDEX_MODULE_HASH, SNS_TOKEN_LEDGER_MODULE_HASH, SNS_TOKEN_ROOT_MODULE_HASH,
SNS_TOKEN_SWAP_MODULE_HASH,
},
};

#[update(guard = "is_caller_controller_or_global_admin")]
pub async fn upgrade_creator_dao_governance_canisters(wasm_module: Vec<u8>) -> 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 recharge_amount = 100_000_000_000;

let futures = governance_canisters
.iter()
.map(|governance_canister_id| async {
let result = recharge_and_upgrade_canister(
*governance_canister_id,
wasm_module.clone(),
recharge_amount,
)
.await;

match result {
Ok(()) => Ok(*governance_canister_id),
Err(e) => Err((*governance_canister_id, e)),
}
});

run_task_concurrently(
futures,
10,
|result| match result {
Ok(canister_id) => ic_cdk::println!(
"Governance canister {} upgraded Successfully",
canister_id.to_text()
),
Err(e) => ic_cdk::println!(
"Failed upgrading governance Canister {} . Error: {}",
e.0,
e.1
),
},
|| false,
)
.await;

Ok(())
}

async fn recharge_and_upgrade_canister(
canister_id: Principal,
wasm_module: Vec<u8>,
recharge_amount: u128,
) -> Result<(), String> {
//Add cycles in individual canister
request_cycles_from_subnet_orchestrator(recharge_amount).await?;

deposit_cycles(CanisterIdRecord { canister_id }, recharge_amount)
.await
.map_err(|e| e.1)?;

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();
let swap_hash = hex::decode(SNS_TOKEN_SWAP_MODULE_HASH).unwrap();
let index_hash = hex::decode(SNS_TOKEN_INDEX_MODULE_HASH).unwrap();
let archive_hash = hex::decode(SNS_TOKEN_ARCHIVE_MODULE_HASH).unwrap();

let mut governance_init_payload = GovernanceCanisterInitPayloadBuilder::new().build();

governance_init_payload.deployed_version = Some(Version {
governance_wasm_hash: gov_hash,
ledger_wasm_hash: ledger_hash,
root_wasm_hash: root_hash,
swap_wasm_hash: swap_hash,
archive_wasm_hash: archive_hash,
index_wasm_hash: index_hash,
});

let upgrade_arg = candid::encode_one(governance_init_payload).map_err(|e| e.to_string())?;

let install_code_argument = InstallCodeArgument {
mode: CanisterInstallMode::Upgrade(None),
canister_id,
wasm_module,
arg: upgrade_arg,
};

upgrade_canister_util(install_code_argument)
.await
.map_err(|e| e.1)
}
3 changes: 3 additions & 0 deletions src/canister/platform_orchestrator/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ service : (PlatformOrchestratorInitArgs) -> {
make_individual_canister_logs_public : (principal) -> (Result_1);
make_subnet_orchestrator_logs_private : (principal) -> (Result_1);
make_subnet_orchestrator_logs_public : (principal) -> (Result_1);
notify_all_individual_canisters_to_upgrade_creator_dao_governance_canisters : (
blob,
) -> (Result_1);
platform_orchestrator_generic_function : (
PlatformOrchestratorGenericArgumentType,
) -> (PlatformOrchestratorGenericResultType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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;
mod populate_known_principal_for_all_subnet;
pub mod provision_empty_canisters_in_a_subnet;
pub mod provision_subnet_orchestrator;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use ic_cdk_macros::update;

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

#[update(guard = "is_caller_global_admin_or_controller")]
pub fn notify_all_individual_canisters_to_upgrade_creator_dao_governance_canisters(
wasm_module: Vec<u8>,
) -> Result<(), String> {
CANISTER_DATA.with_borrow(|canister_data| {
let subnet_orchestrators = canister_data.all_subnet_orchestrator_canisters_list.iter();

for canister_id in subnet_orchestrators {
ic_cdk::notify::<_>(
*canister_id,
"notify_all_individual_canisters_to_upgrade_creator_dao_governance_canisters",
(wasm_module.clone(),),
)
.map_err(|e| format!("Error: {:?}", e))?;
}

Ok(())
})
}
7 changes: 7 additions & 0 deletions src/canister/user_index/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ service : (UserIndexInitArgs) -> {
issue_rewards_for_referral : (principal, principal, principal) -> (Result_1);
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,
) -> (Result_3);
provision_empty_canisters : (nat64) -> ();
recharge_individual_user_canister : () -> (Result_3);
reclaim_cycles_from_individual_canisters : () -> ();
Expand Down
2 changes: 2 additions & 0 deletions src/canister/user_index/src/api/canister_management/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ 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 recharge_individual_user_canister;
pub mod recycle_canisters;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use candid::Principal;
use ic_cdk_macros::update;
use shared_utils::common::utils::permissions::is_caller_controller_or_global_admin;

use crate::CANISTER_DATA;

#[update(guard = "is_caller_controller_or_global_admin")]
pub fn notify_all_individual_canisters_to_upgrade_creator_dao_governance_canisters(
wasm_module: Vec<u8>,
) -> Result<(), String> {
CANISTER_DATA.with_borrow(|canister_data| {
let individual_canisters = canister_data.user_principal_id_to_canister_id_map.iter();

for (_, canister_id) in individual_canisters {
ic_cdk::notify::<_>(
*canister_id,
"upgrade_creator_dao_governance_canisters",
(wasm_module.clone(),),
)
.map_err(|e| format!("Error: {:?}", e))?;
}

Ok(())
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use candid::Principal;
use ic_cdk_macros::update;
use shared_utils::common::utils::permissions::{
is_caller_controller, is_caller_controller_or_global_admin,
};

use crate::util::types::individual_user_canister::IndividualUserCanister;

#[update(guard = "is_caller_controller_or_global_admin")]
pub async fn notify_specific_individual_canister_to_upgrade_creator_dao_governance_canisters(
individual_canister_id: Principal,
wasm_module: Vec<u8>,
) -> Result<(), String> {
let individual_canister = IndividualUserCanister::new(individual_canister_id)?;
individual_canister.notify_to_upgrade_creator_dao_governance_canisters(wasm_module)
}
12 changes: 12 additions & 0 deletions src/canister/user_index/src/util/types/individual_user_canister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,16 @@ impl IndividualUserCanister {

alloted_canister_id_res
}

pub fn notify_to_upgrade_creator_dao_governance_canisters(
&self,
wasm_module: Vec<u8>,
) -> Result<(), String> {
ic_cdk::notify::<_>(
self.canister_id,
"upgrade_creator_dao_governance_canisters",
(wasm_module,),
)
.map_err(|e| format!("Error: {:?}", e))
}
}
Loading

0 comments on commit 2b9b2fb

Please sign in to comment.