Skip to content

Commit

Permalink
refactor and add method to upgrade governance canisters in specific i…
Browse files Browse the repository at this point in the history
…ndividual canisters
  • Loading branch information
ravi-sawlani-yral committed Nov 13, 2024
1 parent 04b5327 commit 580daaa
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub async fn upgrade_creator_dao_governance_canisters(wasm_module: Vec<u8>) -> R
.collect()
});

let recharge_amount = 100_000_000_000;
let recharge_amount = 100_000_000_000; //100B

let futures = governance_canisters
.iter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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;
pub mod provision_subnet_orchestrator;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use candid::Principal;
use ic_cdk::api::management_canister::main::{canister_info, CanisterInfoRequest};
use ic_cdk_macros::update;

use crate::{
guard::is_caller::is_caller_global_admin_or_controller,
utils::registered_subnet_orchestrator::RegisteredSubnetOrchestrator,
};

#[update(guard = "is_caller_global_admin_or_controller")]
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_info,) = canister_info(CanisterInfoRequest {
canister_id: individual_canister_id,
num_requested_changes: None,
})
.await
.map_err(|e| e.1)?;

let subnet_orchestrator_canister_id = individual_canister_info
.controllers
.get(0)
.copied()
.ok_or("Subnet orchestrator canister id not found")?;

let registered_subnet_orchestrator =
RegisteredSubnetOrchestrator::new(subnet_orchestrator_canister_id)?;

registered_subnet_orchestrator
.notify_specific_individual_canister_to_upgrade_creator_dao_governance_canisters(
individual_canister_id,
wasm_module,
)
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use candid::Principal;
use ic_cdk_macros::update;

use crate::utils::registered_subnet_orchestrator::RegisteredSubnetOrchestrator;
use crate::{
guard::is_caller::is_caller_global_admin_or_controller,
utils::registered_subnet_orchestrator::RegisteredSubnetOrchestrator,
};

#[update]
#[update(guard = "is_caller_global_admin_or_controller")]
async fn upgrade_individual_canisters_in_a_subnet_with_latest_wasm(
subnet_orchestrator_canister_id: Principal,
) -> Result<(), String> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use candid::{Nat, Principal};
use ic_cdk::api::management_canister::main::{
canister_status, deposit_cycles, start_canister, update_settings, CanisterIdRecord,
CanisterSettings, LogVisibility, UpdateSettingsArgument,
use ic_cdk::{
api::management_canister::main::{
canister_status, deposit_cycles, start_canister, update_settings, CanisterIdRecord,
CanisterSettings, LogVisibility, UpdateSettingsArgument,
},
notify,
};
use shared_utils::{
common::types::wasm::WasmType, constant::SUBNET_ORCHESTRATOR_CANISTER_CYCLES_THRESHOLD,
Expand Down Expand Up @@ -211,4 +214,17 @@ impl RegisteredSubnetOrchestrator {
.await
.map_err(|e| e.1)
}

pub fn notify_specific_individual_canister_to_upgrade_creator_dao_governance_canisters(
&self,
individual_canister_id: Principal,
wasm_module: Vec<u8>,
) -> Result<(), String> {
notify(
self.canister_id,
"notify_specific_individual_canister_to_upgrade_creator_dao_governance_canisters",
(individual_canister_id, wasm_module),
)
.map_err(|e| format!("Notify to subnet orchestrator failed {:?}", e))
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use candid::Principal;
use ic_cdk_macros::update;
use shared_utils::common::utils::permissions::{
is_caller_controller, is_caller_controller_or_global_admin,
};
use shared_utils::common::utils::permissions::is_caller_controller_or_global_admin;

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

Expand Down
99 changes: 64 additions & 35 deletions src/lib/integration_tests/tests/creator_dao/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -795,13 +795,21 @@ fn creator_dao_tests() {
.unwrap();

// simulating off-chain allocation (kinda)
let decimals = pocket_ic.query_call(ledger_canister, alice_canister_id, "icrc1_decimals", Encode!(&()).unwrap()).map(|res|{
let response: u8 = match res {
WasmResult::Reply(payload) => Decode!(&payload, u8).unwrap(),
_ => panic!("\n🛑 icrc1_transfer failed with: {:?}", res),
};
response
}).unwrap();
let decimals = pocket_ic
.query_call(
ledger_canister,
alice_canister_id,
"icrc1_decimals",
Encode!(&()).unwrap(),
)
.map(|res| {
let response: u8 = match res {
WasmResult::Reply(payload) => Decode!(&payload, u8).unwrap(),
_ => panic!("\n🛑 icrc1_transfer failed with: {:?}", res),
};
response
})
.unwrap();

let transfer_args = types::TransferArg {
from_subaccount: None,
Expand Down Expand Up @@ -835,10 +843,17 @@ fn creator_dao_tests() {
let res = pocket_ic
.update_call(
alice_canister_id,
bob,
"request_airdrop",
encode_args((root_canister, None::<Memo>, Nat::from(100u64) * 10u64.pow(decimals.into()), bob_canister_id)).unwrap())
.map(|reply_payload|{
bob,
"request_airdrop",
encode_args((
root_canister,
None::<Memo>,
Nat::from(100u64) * 10u64.pow(decimals.into()),
bob_canister_id,
))
.unwrap(),
)
.map(|reply_payload| {
let response: Result<(), AirdropError> = match reply_payload {
WasmResult::Reply(payload) => Decode!(&payload, Result<(), AirdropError>).unwrap(),
_ => panic!("\n🛑 get requester principals canister id failed\n"),
Expand All @@ -850,18 +865,25 @@ fn creator_dao_tests() {

// trying to claim the airdrop again
let res: Result<Result<(), AirdropError>, pocket_ic::UserError> = pocket_ic
.update_call(
alice_canister_id,
bob,
"request_airdrop",
encode_args((root_canister, None::<Memo>, Nat::from(100u64) * 10u64.pow(decimals.into()), bob_canister_id)).unwrap())
.map(|reply_payload|{
let response: Result<(), AirdropError> = match reply_payload {
WasmResult::Reply(payload) => Decode!(&payload, Result<(), AirdropError>).unwrap(),
_ => panic!("\n🛑 get requester principals canister id failed\n"),
};
response
});
.update_call(
alice_canister_id,
bob,
"request_airdrop",
encode_args((
root_canister,
None::<Memo>,
Nat::from(100u64) * 10u64.pow(decimals.into()),
bob_canister_id,
))
.unwrap(),
)
.map(|reply_payload| {
let response: Result<(), AirdropError> = match reply_payload {
WasmResult::Reply(payload) => Decode!(&payload, Result<(), AirdropError>).unwrap(),
_ => panic!("\n🛑 get requester principals canister id failed\n"),
};
response
});

ic_cdk::println!("🧪 Result: {:?}", res);
assert!(
Expand All @@ -870,18 +892,25 @@ fn creator_dao_tests() {

// trying to claim the airdrop with the wrong canister id
let res: Result<Result<(), AirdropError>, pocket_ic::UserError> = pocket_ic
.update_call(
alice_canister_id,
bob,
"request_airdrop",
encode_args((root_canister, None::<Memo>, Nat::from(100u64) * 10u64.pow(decimals.into()), Principal::anonymous())).unwrap())
.map(|reply_payload|{
let response: Result<(), AirdropError> = match reply_payload {
WasmResult::Reply(payload) => Decode!(&payload, Result<(), AirdropError>).unwrap(),
_ => panic!("\n🛑 get requester principals canister id failed\n"),
};
response
});
.update_call(
alice_canister_id,
bob,
"request_airdrop",
encode_args((
root_canister,
None::<Memo>,
Nat::from(100u64) * 10u64.pow(decimals.into()),
Principal::anonymous(),
))
.unwrap(),
)
.map(|reply_payload| {
let response: Result<(), AirdropError> = match reply_payload {
WasmResult::Reply(payload) => Decode!(&payload, Result<(), AirdropError>).unwrap(),
_ => panic!("\n🛑 get requester principals canister id failed\n"),
};
response
});

ic_cdk::println!("🧪 Result: {:?}", res);
assert!(res.unwrap().is_err());
Expand Down

0 comments on commit 580daaa

Please sign in to comment.