canic_core/ops/
root.rs

1use crate::{
2    Error,
3    cdk::api::canister_self,
4    interface::ic::get_current_subnet_pid,
5    log::Topic,
6    ops::{
7        config::ConfigOps,
8        model::memory::{EnvOps, topology::SubnetCanisterRegistryOps},
9        prelude::*,
10        request::{CreateCanisterParent, create_canister_request},
11    },
12};
13
14pub async fn root_set_subnet_id() {
15    // set subnet_id asynchrously, but before its needed
16    if let Ok(Some(subnet_pid)) = get_current_subnet_pid().await {
17        EnvOps::set_subnet_pid(subnet_pid);
18        return;
19    }
20
21    // fallback for environments without the registry (e.g., PocketIC)
22    let fallback = canister_self();
23    EnvOps::set_subnet_pid(fallback);
24    log!(
25        Topic::Topology,
26        Warn,
27        "get_current_subnet_pid unavailable; using self as subnet: {fallback}"
28    );
29}
30
31/// Ensure all auto-create canisters exist and log the current topology.
32pub async fn root_create_canisters() -> Result<(), Error> {
33    let subnet_cfg = ConfigOps::current_subnet()?;
34
35    // create pass
36    for ty in &subnet_cfg.auto_create {
37        create_canister_request::<()>(ty, CreateCanisterParent::Root, None).await?;
38    }
39
40    // Report pass
41    for canister in SubnetCanisterRegistryOps::export() {
42        log!(Topic::Init, Info, "🥫 {} ({})", canister.ty, canister.pid);
43    }
44
45    Ok(())
46}