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 status: CanisterStatusType,
56    pub settings: CanisterSettings,
57    pub module_hash: Option<Vec<u8>>,
58    pub memory_size: Nat,
59    pub memory_metrics: MemoryMetrics,
60    pub cycles: Nat,
61    pub reserved_cycles: Nat,
62    pub idle_cycles_burned_per_day: Nat,
63    pub query_stats: QueryStats,
64}
65
66//
67// CanisterSettings
68//
69
70#[derive(CandidType, Clone, Debug, Deserialize)]
71pub struct CanisterSettings {
72    pub controllers: Vec<Principal>,
73    pub compute_allocation: Nat,
74    pub memory_allocation: Nat,
75    pub freezing_threshold: Nat,
76    pub reserved_cycles_limit: Nat,
77    pub log_visibility: LogVisibility,
78    pub log_memory_limit: Nat,
79    pub wasm_memory_limit: Nat,
80    pub wasm_memory_threshold: Nat,
81    pub environment_variables: Vec<EnvironmentVariable>,
82}
83
84//
85// EnvironmentVariable
86//
87
88#[derive(CandidType, Clone, Debug, Deserialize)]
89pub struct EnvironmentVariable {
90    pub name: String,
91    pub value: String,
92}
93
94//
95// MemoryMetrics
96//
97
98#[derive(CandidType, Clone, Debug, Deserialize)]
99pub struct MemoryMetrics {
100    pub wasm_memory_size: Nat,
101    pub stable_memory_size: Nat,
102    pub global_memory_size: Nat,
103    pub wasm_binary_size: Nat,
104    pub custom_sections_size: Nat,
105    pub canister_history_size: Nat,
106    pub wasm_chunk_store_size: Nat,
107    pub snapshots_size: Nat,
108}
109
110//
111// QueryStats
112//
113
114#[derive(CandidType, Clone, Debug, Deserialize)]
115pub struct QueryStats {
116    pub num_calls_total: Nat,
117    pub num_instructions_total: Nat,
118    pub request_payload_bytes_total: Nat,
119    pub response_payload_bytes_total: Nat,
120}
121
122#[cfg(test)]
123mod tests {
124    use super::*;
125    use candid::{Decode, Encode};
126    use serde::de::DeserializeOwned;
127    use std::fmt::Debug;
128
129    #[test]
130    fn canister_status_enums_roundtrip_candid_through_dto_path() {
131        assert_enum_candid_contract(CanisterStatusType::Stopping);
132        assert_enum_candid_contract(LogVisibility::AllowedViewers(vec![Principal::anonymous()]));
133    }
134
135    fn assert_enum_candid_contract<T>(value: T)
136    where
137        T: CandidType + Clone + Debug + DeserializeOwned + Eq,
138    {
139        let bytes = Encode!(&value).expect("encode canister enum");
140        let decoded = Decode!(&bytes, T).expect("decode canister enum");
141
142        assert_eq!(decoded, value);
143    }
144}