-
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.
added method to allot empty canisters to individual user canisters
- Loading branch information
1 parent
b234455
commit 306a00a
Showing
6 changed files
with
194 additions
and
8 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
39 changes: 39 additions & 0 deletions
39
src/canister/user_index/src/api/canister_management/allot_empty_canister.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,39 @@ | ||
use candid::Principal; | ||
use ic_cdk::caller; | ||
use ic_cdk_macros::update; | ||
use shared_utils::constant::{ | ||
get_backup_individual_user_canister_batch_size, get_backup_individual_user_canister_threshold, | ||
}; | ||
|
||
use crate::{ | ||
util::{ | ||
canister_management::provision_number_of_empty_canisters, | ||
types::individual_user_canister::IndividualUserCanister, | ||
}, | ||
CANISTER_DATA, | ||
}; | ||
|
||
#[update] | ||
fn allot_empty_canister() -> Result<Principal, String> { | ||
let registered_individual_canister = IndividualUserCanister::new(caller())?; | ||
let result = registered_individual_canister.allot_empty_canister(); | ||
|
||
let backup_canister_count = | ||
CANISTER_DATA.with_borrow(|canister_data| canister_data.backup_canister_pool.len() as u64); | ||
|
||
if backup_canister_count < get_backup_individual_user_canister_threshold() { | ||
let number_of_canisters = get_backup_individual_user_canister_batch_size(); | ||
let breaking_condition = || { | ||
CANISTER_DATA.with_borrow_mut(|canister_data| { | ||
canister_data.backup_canister_pool.len() as u64 | ||
> get_backup_individual_user_canister_batch_size() | ||
}) | ||
}; | ||
ic_cdk::spawn(provision_number_of_empty_canisters( | ||
number_of_canisters, | ||
breaking_condition, | ||
)); | ||
} | ||
|
||
result | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod provision_new_available_and_backup_canister_on_signups_if_required; | ||
pub mod recharge_individual_canister_when_requested; | ||
pub mod test_allot_empty_canisters_to_individual_canister; |
127 changes: 127 additions & 0 deletions
127
...tion_tests/tests/subnet_orchestrator/test_allot_empty_canisters_to_individual_canister.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,127 @@ | ||
use candid::Principal; | ||
use pocket_ic::WasmResult; | ||
use shared_utils::common::types::known_principal::KnownPrincipalType; | ||
use test_utils::setup::{ | ||
env::pocket_ic_env::get_new_pocket_ic_env, | ||
test_constants::{get_mock_user_alice_principal_id, get_mock_user_charlie_principal_id}, | ||
}; | ||
|
||
#[test] | ||
fn test_allot_empty_canisters_to_individual_canister() { | ||
let (pocket_ic, known_principal) = get_new_pocket_ic_env(); | ||
let platform_canister_id = known_principal | ||
.get(&KnownPrincipalType::CanisterIdPlatformOrchestrator) | ||
.cloned() | ||
.unwrap(); | ||
|
||
let super_admin = known_principal | ||
.get(&KnownPrincipalType::UserIdGlobalSuperAdmin) | ||
.cloned() | ||
.unwrap(); | ||
|
||
let application_subnets = pocket_ic.topology().get_app_subnets(); | ||
|
||
let charlie_global_admin = get_mock_user_charlie_principal_id(); | ||
|
||
pocket_ic | ||
.update_call( | ||
platform_canister_id, | ||
super_admin, | ||
"add_principal_as_global_admin", | ||
candid::encode_one(charlie_global_admin).unwrap(), | ||
) | ||
.unwrap(); | ||
|
||
let subnet_orchestrator_canister_id: Principal = pocket_ic | ||
.update_call( | ||
platform_canister_id, | ||
charlie_global_admin, | ||
"provision_subnet_orchestrator_canister", | ||
candid::encode_one(application_subnets[1]).unwrap(), | ||
) | ||
.map(|res| { | ||
let canister_id_result: Result<Principal, String> = match res { | ||
WasmResult::Reply(payload) => candid::decode_one(&payload).unwrap(), | ||
_ => panic!("Canister call failed"), | ||
}; | ||
canister_id_result.unwrap() | ||
}) | ||
.unwrap(); | ||
|
||
for i in 0..120 { | ||
pocket_ic.tick(); | ||
} | ||
|
||
let alice_yral_principal_id = get_mock_user_alice_principal_id(); | ||
let alice_yral_canister_id = pocket_ic | ||
.update_call( | ||
subnet_orchestrator_canister_id, | ||
alice_yral_principal_id, | ||
"get_requester_principals_canister_id_create_if_not_exists", | ||
candid::encode_one(()).unwrap(), | ||
) | ||
.map(|res| { | ||
let canister_id: Result<Principal, String> = match res { | ||
WasmResult::Reply(payload) => candid::decode_one(&payload).unwrap(), | ||
_ => panic!("Canister call failed"), | ||
}; | ||
canister_id | ||
}) | ||
.unwrap() | ||
.unwrap(); | ||
|
||
let mut intial_subnet_backup_capacity = pocket_ic | ||
.query_call( | ||
subnet_orchestrator_canister_id, | ||
super_admin, | ||
"get_subnet_backup_capacity", | ||
candid::encode_one(()).unwrap(), | ||
) | ||
.map(|reply_payload| { | ||
let subnet_capacity: u64 = match reply_payload { | ||
WasmResult::Reply(payload) => candid::decode_one(&payload).unwrap(), | ||
_ => panic!("\n🛑 get_subnet_backup_capacity failed\n"), | ||
}; | ||
subnet_capacity | ||
}) | ||
.unwrap(); | ||
|
||
for _ in 0..10 { | ||
pocket_ic | ||
.update_call( | ||
subnet_orchestrator_canister_id, | ||
alice_yral_canister_id, | ||
"allot_empty_canister", | ||
candid::encode_one(()).unwrap(), | ||
) | ||
.map(|res| { | ||
let canister_id: Result<Principal, String> = match res { | ||
WasmResult::Reply(payload) => candid::decode_one(&payload).unwrap(), | ||
_ => panic!("Canister call failed"), | ||
}; | ||
canister_id | ||
}) | ||
.unwrap() | ||
.unwrap(); | ||
|
||
intial_subnet_backup_capacity -= 1; | ||
} | ||
|
||
let final_backup_capacity = pocket_ic | ||
.query_call( | ||
subnet_orchestrator_canister_id, | ||
super_admin, | ||
"get_subnet_backup_capacity", | ||
candid::encode_one(()).unwrap(), | ||
) | ||
.map(|reply_payload| { | ||
let subnet_capacity: u64 = match reply_payload { | ||
WasmResult::Reply(payload) => candid::decode_one(&payload).unwrap(), | ||
_ => panic!("\n🛑 get_subnet_backup_capacity failed\n"), | ||
}; | ||
subnet_capacity | ||
}) | ||
.unwrap(); | ||
|
||
assert!(final_backup_capacity > intial_subnet_backup_capacity); | ||
} |