Skip to main content

canic_core/
lib.rs

1//! Core Canic library used inside canisters.
2//!
3//! Canister roles should depend on the `canic` facade crate, which exposes the
4//! supported API/DTO/ID surfaces and the common entrypoint macros:
5//! - `canic::build!` (in `build.rs`) to validate/embed `canic.toml`
6//! - `canic::start!` (in `lib.rs`) to wire lifecycle hooks and export endpoints
7//!
8//! ## Layering
9//!
10//! Canic is organized to keep endpoint code thin and coordination centralized:
11//! - `access/` contains access expressions, predicates, and metrics for boundary enforcement.
12//! - `workflow/` implements orchestration and lifecycle workflows.
13//! - `domain/policy/pure/` contains pure policy decisions invoked by workflow.
14//! - `ops/` provides deterministic state access, conversion, and approved single-step effects.
15//! - `model/` owns authoritative runtime state and storage invariants.
16//! - `storage/` contains passive persisted schemas and stable-memory representations.
17//! - `view/` exposes internal read-only projections over stored/runtime state.
18//! - macro entrypoints live in the `canic` facade crate.
19//!
20//! The dependency flow is: endpoints → workflow → policy → ops → model.
21
22#[doc(hidden)]
23pub mod access;
24pub mod api;
25#[doc(hidden)]
26pub mod bootstrap;
27#[doc(hidden)]
28pub mod cdk;
29#[doc(hidden)]
30pub mod control_plane_support;
31#[doc(hidden)]
32pub mod dispatch;
33pub mod dto;
34#[doc(hidden)]
35mod error;
36mod format;
37pub mod ids;
38#[doc(hidden)]
39pub mod ingress;
40pub mod log;
41pub mod memory;
42mod memory_macros;
43pub mod perf;
44pub mod protocol;
45pub mod replay_policy;
46#[doc(hidden)]
47pub mod role_contract;
48#[doc(hidden)]
49pub mod shared_support;
50#[doc(hidden)]
51pub mod state_contract;
52#[cfg(test)]
53pub mod test;
54
55pub(crate) mod config;
56pub(crate) mod domain;
57pub(crate) mod infra;
58pub(crate) mod lifecycle;
59pub(crate) mod model;
60pub(crate) mod ops;
61pub(crate) mod storage;
62pub(crate) mod view;
63pub(crate) mod workflow;
64
65pub(crate) use error::{InternalError, InternalErrorClass, InternalErrorOrigin};
66
67/// Internal re-exports required for macro expansion.
68/// Not part of the public API.
69#[doc(hidden)]
70pub mod __reexports {
71    pub use ::ic_memory;
72    pub use ::ic_memory::__reexports::ctor;
73}
74
75///
76/// Consts
77///
78
79pub const CRATE_NAME: &str = env!("CARGO_PKG_NAME");
80pub const VERSION: &str = env!("CARGO_PKG_VERSION");
81pub const CANIC_MEMORY_MIN: u8 = role_contract::allocation::CANIC_CORE_MIN_ID;
82pub const CANIC_MEMORY_MAX: u8 = role_contract::allocation::CANIC_CORE_MAX_ID;
83// Canonical hardcoded 1 MiB chunk size for Canic wasm staging/install flows.
84// The management canister wasm chunk store rejects larger payloads.
85pub const CANIC_WASM_CHUNK_BYTES: usize = 1_048_576;
86
87crate::ic_memory_range!(
88    authority = CANIC_CORE_MEMORY_AUTHORITY,
89    start = role_contract::allocation::CANIC_CORE_MIN_ID,
90    end = role_contract::allocation::CANIC_CORE_MAX_ID,
91);
92
93#[cfg(test)]
94const _: () = {
95    fn __canic_memory_test_bootstrap() {
96        crate::api::runtime::MemoryRuntimeApi::bootstrap_registry()
97            .expect("test stable-memory bootstrap");
98    }
99
100    #[crate::__reexports::ctor::ctor(
101        unsafe,
102        anonymous,
103        crate_path = crate::__reexports::ctor
104    )]
105    fn __canic_install_memory_test_bootstrap_hook() {
106        crate::memory::runtime::install_test_bootstrap_hook(__canic_memory_test_bootstrap);
107    }
108};
109
110#[macro_export]
111macro_rules! perf {
112    ($($label:tt)*) => {{
113        $crate::perf::PERF_LAST.with(|last| {
114            let now = $crate::perf::perf_counter();
115            let then = *last.borrow();
116            let delta = now.saturating_sub(then);
117
118            *last.borrow_mut() = now;
119
120            let label = format!($($label)*);
121            $crate::perf::record_checkpoint(module_path!(), &label, delta);
122        });
123    }};
124}
125
126#[cfg(test)]
127#[macro_export]
128macro_rules! assert_err_variant {
129    ($err:expr, $pat:pat $(if $guard:expr)? $(,)?) => {{
130        match $err {
131            $pat $(if $guard)? => {}
132            other => panic!("unexpected error variant: {other:?}"),
133        }
134    }};
135}
136
137#[cfg(test)]
138mod memory_bootstrap_tests {
139    #[test]
140    fn installs_host_test_bootstrap_hook() {
141        assert!(crate::memory::runtime::has_test_bootstrap_hook());
142    }
143}