canic 0.35.7

Canic — a canister orchestration and management toolkit for the Internet Computer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
// -----------------------------------------------------------------------------
// Start macros
// -----------------------------------------------------------------------------

// Lifecycle core for non-root Canic canisters.
#[doc(hidden)]
#[macro_export]
macro_rules! __canic_start_nonroot_lifecycle_core {
    ($canister_role:expr $(, $init:block)?) => {
        #[doc(hidden)]
        fn __canic_compiled_config() -> (
            $crate::__internal::core::bootstrap::compiled::ConfigModel,
            &'static str,
            &'static str,
        ) {
            let config_model = include!(env!("CANIC_CONFIG_MODEL_PATH"));
            let config_source = include_str!(env!("CANIC_CONFIG_SOURCE_PATH"));
            let config_path = env!("CANIC_CONFIG_PATH");
            (config_model, config_source, config_path)
        }

        #[::canic::cdk::init]
        fn init(payload: ::canic::dto::abi::v1::CanisterInitPayload, args: Option<Vec<u8>>) {
            let (config, config_source, config_path) = __canic_compiled_config();

            $crate::__internal::core::api::lifecycle::nonroot::LifecycleApi::init_nonroot_canister_before_bootstrap(
                $canister_role,
                payload,
                config,
                config_source,
                config_path,
                cfg!(canic_role_attestation_refresh),
            );

            $crate::__canic_after_optional_start_init_hook!(
                "canic:user:init_block",
                {
                    $crate::__internal::core::api::lifecycle::nonroot::LifecycleApi::schedule_init_nonroot_bootstrap(args.clone());
                    $crate::__canic_start_nonroot_user_timers!(args);
                }
                $(, $init)?
            );
        }

        #[::canic::cdk::post_upgrade]
        fn post_upgrade() {
            let (config, config_source, config_path) = __canic_compiled_config();

            $crate::__internal::core::api::lifecycle::nonroot::LifecycleApi::post_upgrade_nonroot_canister_before_bootstrap(
                $canister_role,
                config,
                config_source,
                config_path,
                cfg!(canic_role_attestation_refresh),
            );

            $crate::__canic_after_optional_start_init_hook!(
                "canic:user:post_upgrade_block",
                {
                    $crate::__internal::core::api::lifecycle::nonroot::LifecycleApi::schedule_post_upgrade_nonroot_bootstrap();
                    $crate::__canic_start_nonroot_upgrade_timers!();
                }
                $(, $init)?
            );
        }
    };
}

// Local-dev lifecycle core for standalone sandbox canisters.
#[doc(hidden)]
#[macro_export]
macro_rules! __canic_start_local_lifecycle_core {
    ($canister_role:expr $(, $init:block)?) => {
        #[doc(hidden)]
        fn __canic_compiled_config() -> (
            $crate::__internal::core::bootstrap::compiled::ConfigModel,
            &'static str,
            &'static str,
        ) {
            let config_model = include!(env!("CANIC_CONFIG_MODEL_PATH"));
            let config_source = include_str!(env!("CANIC_CONFIG_SOURCE_PATH"));
            let config_path = env!("CANIC_CONFIG_PATH");
            (config_model, config_source, config_path)
        }

        #[doc(hidden)]
        fn __canic_local_principal(byte: u8) -> ::canic::cdk::types::Principal {
            ::canic::cdk::types::Principal::from_slice(&[byte; 29])
        }

        #[doc(hidden)]
        fn __canic_local_init_payload(
            role: $crate::__internal::core::ids::CanisterRole,
        ) -> ::canic::dto::abi::v1::CanisterInitPayload {
            let root_pid = __canic_local_principal(1);
            let subnet_pid = __canic_local_principal(2);
            ::canic::dto::abi::v1::CanisterInitPayload {
                env: ::canic::dto::env::EnvBootstrapArgs {
                    prime_root_pid: Some(root_pid),
                    subnet_role: Some($crate::__internal::core::ids::SubnetRole::PRIME),
                    subnet_pid: Some(subnet_pid),
                    root_pid: Some(root_pid),
                    canister_role: Some(role),
                    parent_pid: Some(root_pid),
                },
                app_index: ::canic::dto::topology::AppIndexArgs(Vec::new()),
                subnet_index: ::canic::dto::topology::SubnetIndexArgs(Vec::new()),
            }
        }

        #[::canic::cdk::init]
        fn init(args: Option<Vec<u8>>) {
            let (config, config_source, config_path) = __canic_compiled_config();
            let role = $canister_role;
            let payload = __canic_local_init_payload(role.clone());

            $crate::__internal::core::api::lifecycle::nonroot::LifecycleApi::init_nonroot_canister_before_bootstrap(
                role,
                payload,
                config,
                config_source,
                config_path,
                cfg!(canic_role_attestation_refresh),
            );

            $crate::__canic_after_optional_start_init_hook!(
                "canic:user:init_block",
                {
                    $crate::__internal::core::api::lifecycle::nonroot::LifecycleApi::schedule_init_nonroot_bootstrap(args.clone());
                    $crate::__canic_start_nonroot_user_timers!(args);
                }
                $(, $init)?
            );
        }

        #[::canic::cdk::post_upgrade]
        fn post_upgrade() {
            let (config, config_source, config_path) = __canic_compiled_config();

            $crate::__internal::core::api::lifecycle::nonroot::LifecycleApi::post_upgrade_nonroot_canister_before_bootstrap(
                $canister_role,
                config,
                config_source,
                config_path,
                cfg!(canic_role_attestation_refresh),
            );

            $crate::__canic_after_optional_start_init_hook!(
                "canic:user:post_upgrade_block",
                {
                    $crate::__internal::core::api::lifecycle::nonroot::LifecycleApi::schedule_post_upgrade_nonroot_bootstrap();
                    $crate::__canic_start_nonroot_upgrade_timers!();
                }
                $(, $init)?
            );
        }
    };
}

// Lifecycle core for the root Canic canister.
#[doc(hidden)]
#[macro_export]
macro_rules! __canic_start_root_lifecycle_core {
    ($( $init:block )?) => {
        #[doc(hidden)]
        fn __canic_compiled_config() -> (
            $crate::__internal::core::bootstrap::compiled::ConfigModel,
            &'static str,
            &'static str,
        ) {
            let config_model = include!(env!("CANIC_CONFIG_MODEL_PATH"));
            let config_source = include_str!(env!("CANIC_CONFIG_SOURCE_PATH"));
            let config_path = env!("CANIC_CONFIG_PATH");
            (config_model, config_source, config_path)
        }

        #[doc(hidden)]
        #[cfg(canic_has_root_wasm_store_bootstrap_release_set)]
        fn __canic_embedded_root_wasm_store_bootstrap_release_set(
        ) -> &'static [$crate::__internal::core::bootstrap::EmbeddedRootBootstrapEntry] {
            include!(env!("CANIC_ROOT_WASM_STORE_BOOTSTRAP_RELEASE_SET_PATH"))
        }

        #[doc(hidden)]
        #[cfg(not(canic_has_root_wasm_store_bootstrap_release_set))]
        fn __canic_embedded_root_wasm_store_bootstrap_release_set(
        ) -> &'static [$crate::__internal::core::bootstrap::EmbeddedRootBootstrapEntry] {
            &[]
        }

        #[::canic::cdk::init]
        fn init(identity: ::canic::dto::subnet::SubnetIdentity) {
            let (config, config_source, config_path) = __canic_compiled_config();
            let embedded_wasm_store_bootstrap_release_set =
                __canic_embedded_root_wasm_store_bootstrap_release_set();

            $crate::__internal::control_plane::api::lifecycle::LifecycleApi::init_root_canister_before_bootstrap(
                identity,
                config,
                config_source,
                config_path,
                embedded_wasm_store_bootstrap_release_set,
            );

            $crate::__canic_after_optional_start_init_hook!(
                "canic:user:init_block",
                {
                    $crate::__internal::control_plane::api::lifecycle::LifecycleApi::schedule_init_root_bootstrap();
                    $crate::__canic_start_root_user_timers!();
                }
                $(, $init)?
            );
        }

        #[::canic::cdk::post_upgrade]
        fn post_upgrade() {
            let (config, config_source, config_path) = __canic_compiled_config();
            let embedded_wasm_store_bootstrap_release_set =
                __canic_embedded_root_wasm_store_bootstrap_release_set();

            $crate::__internal::control_plane::api::lifecycle::LifecycleApi::post_upgrade_root_canister_before_bootstrap(
                config,
                config_source,
                config_path,
                embedded_wasm_store_bootstrap_release_set,
            );

            $crate::__canic_after_optional_start_init_hook!(
                "canic:user:post_upgrade_block",
                {
                    $crate::__internal::control_plane::api::lifecycle::LifecycleApi::schedule_post_upgrade_root_bootstrap();
                    $crate::__canic_start_root_upgrade_timers!();
                }
                $(, $init)?
            );
        }
    };
}

// Run the optional init block from a lifecycle timer, then schedule continuation timers.
#[doc(hidden)]
#[macro_export]
macro_rules! __canic_after_optional_start_init_hook {
    ($label:expr, $after:block) => {{
        $after
    }};
    ($label:expr, $after:block, $init:block) => {{
        $crate::__internal::core::api::timer::TimerApi::set_lifecycle_timer(
            ::core::time::Duration::ZERO,
            $label,
            async move {
                $init
                $after
            },
        );
    }};
}

// User lifecycle timer bundle for non-root init.
#[doc(hidden)]
#[macro_export]
macro_rules! __canic_start_nonroot_user_timers {
    ($args:expr) => {
        $crate::__internal::core::api::timer::TimerApi::set_lifecycle_timer(
            ::std::time::Duration::ZERO,
            "canic:user:init",
            async move {
                canic_setup().await;
                canic_install($args).await;
            },
        );
    };
}

// User lifecycle timer bundle for non-root upgrades.
#[doc(hidden)]
#[macro_export]
macro_rules! __canic_start_nonroot_upgrade_timers {
    () => {
        $crate::__internal::core::api::timer::TimerApi::set_lifecycle_timer(
            ::core::time::Duration::ZERO,
            "canic:user:init",
            async move {
                canic_setup().await;
                canic_upgrade().await;
            },
        );
    };
}

// User lifecycle timer bundle for root init.
#[doc(hidden)]
#[macro_export]
macro_rules! __canic_start_root_user_timers {
    () => {
        $crate::__internal::core::api::timer::TimerApi::set_lifecycle_timer(
            ::core::time::Duration::ZERO,
            "canic:user:init",
            async move {
                canic_setup().await;
                canic_install().await;
            },
        );
    };
}

// User lifecycle timer bundle for root upgrades.
#[doc(hidden)]
#[macro_export]
macro_rules! __canic_start_root_upgrade_timers {
    () => {
        $crate::__internal::core::api::timer::TimerApi::set_lifecycle_timer(
            ::core::time::Duration::ZERO,
            "canic:user:init",
            async move {
                canic_setup().await;
                canic_upgrade().await;
            },
        );
    };
}

// Default non-root capability bundle composition.
#[doc(hidden)]
#[macro_export]
macro_rules! __canic_start_nonroot_capability_bundles {
    () => {
        $crate::canic_bundle_shared_runtime_endpoints!();
        $crate::canic_bundle_nonroot_only_endpoints!();
    };
}

// Default root capability bundle composition.
#[doc(hidden)]
#[macro_export]
macro_rules! __canic_start_root_capability_bundles {
    () => {
        $crate::canic_bundle_shared_runtime_endpoints!();
        $crate::canic_bundle_root_only_endpoints!();
    };
}

// Ingress inspect-message hook shared by Canic-managed canisters.
#[doc(hidden)]
#[macro_export]
macro_rules! __canic_start_ingress_payload_inspect {
    () => {
        #[::canic::cdk::inspect_message]
        fn canic_inspect_message() {
            $crate::__internal::core::ingress::payload::inspect_update_message();
        }
    };
}

// Require canisters using the Canic lifecycle macros to close the file with
// `canic::finish!()`. Rust item order does not affect this marker lookup, while
// Candid export order still requires the finish macro to appear after endpoints.
#[doc(hidden)]
#[macro_export]
macro_rules! __canic_require_finish {
    () => {
        #[doc(hidden)]
        const _: fn() = __canic_missing_finish_macro_add_canic_finish_at_end;
    };
}

/// Finish a Canic canister module.
///
/// Place this macro at the end of the canister's crate root after
/// `start!`, `start_root!`, or `start_wasm_store!` and after any extra
/// endpoint definitions. In debug builds it exports Candid for local `.did`
/// generation; in non-debug builds it only satisfies the required Canic finish
/// marker.
#[macro_export]
macro_rules! finish {
    () => {
        #[doc(hidden)]
        #[allow(dead_code)]
        fn __canic_missing_finish_macro_add_canic_finish_at_end() {}

        #[cfg(debug_assertions)]
        $crate::cdk::export_candid!();
    };
}

/// Configure lifecycle hooks for **non-root** Canic canisters.
///
/// This macro defines the IC-required `init` and `post_upgrade` entry points
/// at the crate root and immediately delegates lifecycle semantics to runtime
/// adapters after performing minimal bootstrap.
///
/// IMPORTANT:
/// - This macro must remain **thin**
/// - It must not perform orchestration
/// - It must not perform async work inline
/// - It must not encode policy
/// - It may schedule async hooks via timers, but must never await them
///
/// Its sole responsibility is to bridge IC lifecycle hooks to runtime code.
#[macro_export]
macro_rules! start {
    ($canister_role:expr $(, init = $init:block)? $(,)?) => {
        $crate::__canic_require_finish!();
        $crate::__canic_start_nonroot_lifecycle_core!($canister_role $(, $init)?);
        $crate::__canic_start_ingress_payload_inspect!();
        $crate::__canic_start_nonroot_capability_bundles!();
    };
}

/// Configure a local-only non-root Canic canister for manual development.
///
/// `start_local!` is intentionally for standalone dev canisters such as a
/// sandbox. It synthesizes a minimal local environment during `init`, so
/// `icp deploy <canister>` can run without entering the full CANIC bootstrap
/// payload by hand.
///
/// Do not use this macro for production canisters, root-managed child
/// canisters, release-set members, or test fixtures that need real topology
/// metadata. Those should use [`start!`] and receive explicit lifecycle args.
#[macro_export]
macro_rules! start_local {
    ($canister_role:expr $(, init = $init:block)? $(,)?) => {
        $crate::__canic_require_finish!();
        $crate::__canic_start_local_lifecycle_core!($canister_role $(, $init)?);
        $crate::__canic_start_ingress_payload_inspect!();
        $crate::__canic_start_nonroot_capability_bundles!();
    };
}

/// Configure lifecycle hooks for the **root orchestrator** canister.
///
/// This macro behaves like [`start!`], but delegates to root-specific
/// lifecycle adapters.
///
/// IMPORTANT:
/// - The macro does NOT perform root orchestration
/// - The macro does NOT import WASMs
/// - The macro does NOT create canisters
/// - The macro may schedule async hooks via timers, but must never await them
///
#[macro_export]
macro_rules! start_root {
    ($(init = $init:block)? $(,)?) => {
        $crate::__canic_require_finish!();
        $crate::__canic_start_root_lifecycle_core!($($init)?);
        $crate::__canic_start_ingress_payload_inspect!();
        $crate::__canic_start_root_capability_bundles!();
    };
}

/// Configure lifecycle hooks and the canonical endpoint bundle for a subnet-local
/// `wasm_store` canister.
///
/// This specialized macro exists so downstreams can use the built-in Canic
/// `wasm_store` role without copying the reference canister implementation.
///
/// Unlike the ordinary non-root bundle, this surface intentionally excludes most
/// generic observability and topology-view queries that are not part of the
/// canonical `wasm_store` contract. It still exposes the standard cycle tracker
/// so fleet metrics can treat the store like every other managed canister.
#[macro_export]
macro_rules! start_wasm_store {
    ($(init = $init:block)? $(,)?) => {
        $crate::__canic_require_finish!();
        #[expect(clippy::unused_async)]
        async fn canic_setup() {}

        #[expect(clippy::unused_async)]
        async fn canic_install(_: Option<Vec<u8>>) {}

        #[expect(clippy::unused_async)]
        async fn canic_upgrade() {}

        $crate::__canic_start_nonroot_lifecycle_core!(
            $crate::api::canister::CanisterRole::WASM_STORE
            $(, $init)?
        );
        $crate::__canic_start_ingress_payload_inspect!();
        $crate::canic_bundle_wasm_store_runtime_endpoints!();
    };
}