Skip to content

Commit

Permalink
added empty canister store in individual canister
Browse files Browse the repository at this point in the history
  • Loading branch information
ravi-sawlani-yral committed Nov 26, 2024
1 parent 54692ec commit 34e0178
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
11 changes: 9 additions & 2 deletions src/canister/individual_user_template/src/api/cdao/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,20 @@ async fn deploy_cdao_sns(
let subnet_orchestrator = SubnetOrchestrator::new()
.map_err(|e| CdaoDeployError::CallError(RejectionCode::CanisterError, e))?;

let canister_ids: Vec<Principal> = (0..5)
.map(|_| subnet_orchestrator.get_empty_canister())
let number_of_canisters_required_to_be_alloted =
CANISTER_DATA.with_borrow(|canister_data| 5 - 5.min(canister_data.empty_canisters.len()));

(0..number_of_canisters_required_to_be_alloted)
.map(|_| subnet_orchestrator.allot_empty_canister())
.collect::<FuturesOrdered<_>>()
.try_collect()
.await
.map_err(|e| CdaoDeployError::CallError(RejectionCode::CanisterError, e))?;

let canister_ids = CANISTER_DATA
.with_borrow_mut(|canister_data| canister_data.empty_canisters.get_number_of_canister(5))
.map_err(|e| CdaoDeployError::CallError(RejectionCode::CanisterError, e))?;

canister_ids
.iter()
.map(|canister_id| {
Expand Down
36 changes: 35 additions & 1 deletion src/canister/individual_user_template/src/data_model/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
collections::{BTreeMap, BTreeSet},
collections::{BTreeMap, BTreeSet, HashSet},
time::SystemTime,
};

Expand Down Expand Up @@ -95,6 +95,39 @@ pub struct CanisterData {
pub token_roots: ic_stable_structures::btreemap::BTreeMap<Principal, (), Memory>,
#[serde(default)]
pub ml_data: MLData,
#[serde(default)]
pub empty_canisters: AllotedEmptyCanister,
}

#[derive(Serialize, Deserialize, Default)]
pub struct AllotedEmptyCanister {
canister_ids: HashSet<Principal>,
}

impl AllotedEmptyCanister {
pub fn get_number_of_canister(&mut self, number: usize) -> Result<Vec<Principal>, String> {
let mut canister_ids = vec![];
let mut iterator = self.canister_ids.iter().copied();
for _ in 0..number {
if let Some(canister_id) = iterator.next() {
canister_ids.push(canister_id);
} else {
return Err(format!("{} number of canisters not available", number));
}
}

self.canister_ids = iterator.collect();

Ok(canister_ids)
}

pub fn insert_empty_canister(&mut self, canister_id: Principal) -> bool {
self.canister_ids.insert(canister_id)
}

pub fn len(&self) -> usize {
self.canister_ids.len()
}
}

pub fn _default_room_details(
Expand Down Expand Up @@ -168,6 +201,7 @@ impl Default for CanisterData {
cdao_canisters: Vec::new(),
token_roots: _default_token_list(),
ml_data: MLData::default(),
empty_canisters: AllotedEmptyCanister::default(),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,23 @@ impl SubnetOrchestrator {
subnet_orchestrator.ok_or("Subnet Orchestrator canister not found".into())
}

pub async fn get_empty_canister(&self) -> Result<Principal, String> {
let (result,) = ic_cdk::call(self.canister_id, "allot_empty_canister", ())
.await
.map_err(|e| e.1)?;
pub async fn allot_empty_canister(&self) -> Result<(), String> {
let canister_id: Principal = ic_cdk::call::<_, (Result<Principal, String>,)>(
self.canister_id,
"allot_empty_canister",
(),
)
.await
.map_err(|e| e.1)?
.0?;

CANISTER_DATA.with_borrow_mut(|canister_data| {
canister_data
.empty_canisters
.insert_empty_canister(canister_id)
});

result
Ok(())
}

pub fn notify_to_receive_cycles_from_subnet_orchestrator(&self) -> Result<(), String> {
Expand Down

0 comments on commit 34e0178

Please sign in to comment.