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/// Protected reserve sample for an exact target's management status call.
12/// A preflight query is indicative; only an attempted call reports SDK admission failure.
13#[derive(CandidType, Clone, Debug, Deserialize, Eq, PartialEq)]
14pub struct CanisterInspectionReserveResponse {
15    pub caller: Principal,
16    pub canister_id: Principal,
17    pub native_cycles: u128,
18    pub available_liquid_cycles: u128,
19    pub required_liquid_cycles: u128,
20}
21
22/// Protected inspection result; a reserve failure never implies that the target is absent.
23#[derive(CandidType, Deserialize)]
24pub enum CanisterInspectionOutcome {
25    Status(Box<CanisterStatusResponse>),
26    ReserveRequired(CanisterInspectionReserveResponse),
27}
28
29/// Replicated upstream management history for one exact inspection target.
30#[derive(CandidType, Clone, Debug, Deserialize, Eq, PartialEq)]
31pub struct CanisterHistoryResponse {
32    pub canister_id: Principal,
33    pub history_candid: Vec<u8>,
34}
35
36//
37// CanisterInfo
38//
39
40#[derive(CandidType, Clone, Debug, Deserialize, Eq, PartialEq)]
41pub struct CanisterInfo {
42    pub pid: Principal,
43    pub role: CanisterRole,
44    pub parent_pid: Option<Principal>,
45    pub module_hash: Option<Vec<u8>>,
46    pub created_at: u64,
47}
48
49//
50// CanisterStatusResponse
51//
52
53#[derive(CandidType, Clone, Debug, Deserialize)]
54pub struct CanisterStatusResponse {
55    pub version: u64,
56    pub status: CanisterStatusType,
57    pub settings: CanisterSettings,
58    pub module_hash: Option<Vec<u8>>,
59    pub memory_size: Nat,
60    pub memory_metrics: MemoryMetrics,
61    pub cycles: Nat,
62    pub reserved_cycles: Nat,
63    pub idle_cycles_burned_per_day: Nat,
64    pub query_stats: QueryStats,
65}
66
67//
68// CanisterSettings
69//
70
71#[derive(CandidType, Clone, Debug, Deserialize)]
72pub struct CanisterSettings {
73    pub controllers: Vec<Principal>,
74    pub compute_allocation: Nat,
75    pub memory_allocation: Nat,
76    pub freezing_threshold: Nat,
77    pub reserved_cycles_limit: Nat,
78    pub log_visibility: LogVisibility,
79    pub log_memory_limit: Nat,
80    pub wasm_memory_limit: Nat,
81    pub wasm_memory_threshold: Nat,
82    pub environment_variables: Vec<EnvironmentVariable>,
83}
84
85//
86// EnvironmentVariable
87//
88
89#[derive(CandidType, Clone, Debug, Deserialize)]
90pub struct EnvironmentVariable {
91    pub name: String,
92    pub value: String,
93}
94
95//
96// MemoryMetrics
97//
98
99#[derive(CandidType, Clone, Debug, Deserialize)]
100pub struct MemoryMetrics {
101    pub wasm_memory_size: Nat,
102    pub stable_memory_size: Nat,
103    pub global_memory_size: Nat,
104    pub wasm_binary_size: Nat,
105    pub custom_sections_size: Nat,
106    pub canister_history_size: Nat,
107    pub wasm_chunk_store_size: Nat,
108    pub snapshots_size: Nat,
109}
110
111//
112// QueryStats
113//
114
115#[derive(CandidType, Clone, Debug, Deserialize)]
116pub struct QueryStats {
117    pub num_calls_total: Nat,
118    pub num_instructions_total: Nat,
119    pub request_payload_bytes_total: Nat,
120    pub response_payload_bytes_total: Nat,
121}
122
123#[cfg(test)]
124mod tests {
125    use super::*;
126    use candid::{Decode, Encode};
127    use serde::de::DeserializeOwned;
128    use std::fmt::Debug;
129
130    #[test]
131    fn canister_status_enums_roundtrip_candid_through_dto_path() {
132        assert_enum_candid_contract(CanisterStatusType::Stopping);
133        assert_enum_candid_contract(LogVisibility::AllowedViewers(vec![Principal::anonymous()]));
134    }
135
136    fn assert_enum_candid_contract<T>(value: T)
137    where
138        T: CandidType + Clone + Debug + DeserializeOwned + Eq,
139    {
140        let bytes = Encode!(&value).expect("encode canister enum");
141        let decoded = Decode!(&bytes, T).expect("decode canister enum");
142
143        assert_eq!(decoded, value);
144    }
145}