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