Skip to main content

canic_core/dto/
canister.rs

1use crate::dto::prelude::*;
2
3pub use crate::domain::canister::{CanisterStatusType, LogVisibility};
4
5/// Exact management-canister inspection target selected by a Root controller.
6#[derive(CandidType, Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
7pub struct CanisterInspectionRequest {
8    pub canister_id: Principal,
9}
10
11/// Replicated upstream management history for one exact inspection target.
12#[derive(CandidType, Clone, Debug, Deserialize, Eq, PartialEq)]
13pub struct CanisterHistoryResponse {
14    pub canister_id: Principal,
15    pub history_candid: Vec<u8>,
16}
17
18//
19// CanisterInfo
20//
21
22#[derive(CandidType, Clone, Debug, Deserialize, Eq, PartialEq)]
23pub struct CanisterInfo {
24    pub pid: Principal,
25    pub role: CanisterRole,
26    pub parent_pid: Option<Principal>,
27    pub module_hash: Option<Vec<u8>>,
28    pub created_at: u64,
29}
30
31//
32// CanisterStatusResponse
33//
34
35#[derive(CandidType, Clone, Debug, Deserialize)]
36pub struct CanisterStatusResponse {
37    pub status: CanisterStatusType,
38    pub settings: CanisterSettings,
39    pub module_hash: Option<Vec<u8>>,
40    pub memory_size: Nat,
41    pub memory_metrics: MemoryMetrics,
42    pub cycles: Nat,
43    pub reserved_cycles: Nat,
44    pub idle_cycles_burned_per_day: Nat,
45    pub query_stats: QueryStats,
46}
47
48//
49// CanisterSettings
50//
51
52#[derive(CandidType, Clone, Debug, Deserialize)]
53pub struct CanisterSettings {
54    pub controllers: Vec<Principal>,
55    pub compute_allocation: Nat,
56    pub memory_allocation: Nat,
57    pub freezing_threshold: Nat,
58    pub reserved_cycles_limit: Nat,
59    pub log_visibility: LogVisibility,
60    pub log_memory_limit: Nat,
61    pub wasm_memory_limit: Nat,
62    pub wasm_memory_threshold: Nat,
63    pub environment_variables: Vec<EnvironmentVariable>,
64}
65
66//
67// EnvironmentVariable
68//
69
70#[derive(CandidType, Clone, Debug, Deserialize)]
71pub struct EnvironmentVariable {
72    pub name: String,
73    pub value: String,
74}
75
76//
77// MemoryMetrics
78//
79
80#[derive(CandidType, Clone, Debug, Deserialize)]
81pub struct MemoryMetrics {
82    pub wasm_memory_size: Nat,
83    pub stable_memory_size: Nat,
84    pub global_memory_size: Nat,
85    pub wasm_binary_size: Nat,
86    pub custom_sections_size: Nat,
87    pub canister_history_size: Nat,
88    pub wasm_chunk_store_size: Nat,
89    pub snapshots_size: Nat,
90}
91
92//
93// QueryStats
94//
95
96#[derive(CandidType, Clone, Debug, Deserialize)]
97pub struct QueryStats {
98    pub num_calls_total: Nat,
99    pub num_instructions_total: Nat,
100    pub request_payload_bytes_total: Nat,
101    pub response_payload_bytes_total: Nat,
102}
103
104#[cfg(test)]
105mod tests {
106    use super::*;
107    use candid::{Decode, Encode};
108    use serde::de::DeserializeOwned;
109    use std::fmt::Debug;
110
111    #[test]
112    fn canister_status_enums_roundtrip_candid_through_dto_path() {
113        assert_enum_candid_contract(CanisterStatusType::Stopping);
114        assert_enum_candid_contract(LogVisibility::AllowedViewers(vec![Principal::anonymous()]));
115    }
116
117    fn assert_enum_candid_contract<T>(value: T)
118    where
119        T: CandidType + Clone + Debug + DeserializeOwned + Eq,
120    {
121        let bytes = Encode!(&value).expect("encode canister enum");
122        let decoded = Decode!(&bytes, T).expect("decode canister enum");
123
124        assert_eq!(decoded, value);
125    }
126}