Skip to main content

canic_core/dto/
memory.rs

1use crate::dto::prelude::*;
2
3pub use crate::domain::memory::{
4    MemoryAllocationState, MemoryCommitRecoveryErrorResponse, MemoryRangeAuthorityMode,
5};
6
7///
8/// MemoryLedgerResponse
9///
10
11#[derive(CandidType, Clone, Debug, Deserialize)]
12pub struct MemoryLedgerResponse {
13    pub ledger_schema_version: u32,
14    pub physical_format_id: u32,
15    pub current_generation: u64,
16    pub commit_recovery: MemoryCommitRecoveryResponse,
17    pub authorities: Vec<MemoryRangeAuthorityEntry>,
18    pub memories: Vec<MemoryLedgerMemoryEntry>,
19    pub records: Vec<MemoryAllocationRecordEntry>,
20    pub generations: Vec<MemoryLedgerGenerationEntry>,
21}
22
23///
24/// MemoryCommitRecoveryResponse
25///
26
27#[derive(CandidType, Clone, Debug, Deserialize)]
28pub struct MemoryCommitRecoveryResponse {
29    pub slot0: MemoryCommitSlotResponse,
30    pub slot1: MemoryCommitSlotResponse,
31    pub authoritative_generation: Option<u64>,
32    pub recovery_error: Option<MemoryCommitRecoveryErrorResponse>,
33}
34
35///
36/// MemoryCommitSlotResponse
37///
38
39#[derive(CandidType, Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
40pub struct MemoryCommitSlotResponse {
41    pub present: bool,
42    pub generation: Option<u64>,
43    pub valid: bool,
44}
45
46///
47/// MemoryRangeAuthorityEntry
48///
49
50#[derive(CandidType, Clone, Debug, Deserialize)]
51pub struct MemoryRangeAuthorityEntry {
52    pub owner: String,
53    pub start: u8,
54    pub end: u8,
55    pub mode: MemoryRangeAuthorityMode,
56    pub purpose: String,
57}
58
59///
60/// MemoryLedgerMemoryEntry
61///
62
63#[derive(CandidType, Clone, Debug, Deserialize, Eq, PartialEq)]
64pub struct MemoryLedgerMemoryEntry {
65    pub memory_manager_id: u8,
66    pub stable_key: String,
67    pub state: MemoryAllocationState,
68    pub size: MemoryAllocationSizeEntry,
69}
70
71///
72/// MemoryAllocationRecordEntry
73///
74
75#[derive(CandidType, Clone, Debug, Deserialize)]
76pub struct MemoryAllocationRecordEntry {
77    pub memory_manager_id: Option<u8>,
78    pub stable_key: String,
79    pub state: MemoryAllocationState,
80    pub memory_size: Option<MemoryAllocationSizeEntry>,
81    pub first_generation: u64,
82    pub last_seen_generation: u64,
83    pub retired_generation: Option<u64>,
84    pub schema_history: Vec<MemorySchemaMetadataEntry>,
85}
86
87///
88/// MemoryAllocationSizeEntry
89///
90
91#[derive(CandidType, Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
92pub struct MemoryAllocationSizeEntry {
93    pub wasm_pages: u64,
94    pub bytes: u64,
95}
96
97///
98/// MemorySchemaMetadataEntry
99///
100
101#[derive(CandidType, Clone, Debug, Deserialize)]
102pub struct MemorySchemaMetadataEntry {
103    pub generation: u64,
104    pub schema_version: Option<u32>,
105    pub schema_fingerprint: Option<String>,
106}
107
108///
109/// MemoryLedgerGenerationEntry
110///
111
112#[derive(CandidType, Clone, Debug, Deserialize)]
113pub struct MemoryLedgerGenerationEntry {
114    pub generation: u64,
115    pub parent_generation: Option<u64>,
116    pub runtime_fingerprint: Option<String>,
117    pub declaration_count: u32,
118    pub committed_at: Option<u64>,
119}
120
121#[cfg(test)]
122mod tests {
123    use super::*;
124    use candid::{Decode, Encode};
125    use serde::de::DeserializeOwned;
126    use std::fmt::Debug;
127
128    #[test]
129    fn memory_enums_roundtrip_candid_with_existing_variant_labels() {
130        assert_enum_candid_contract(MemoryCommitRecoveryErrorResponse::UnexpectedGeneration);
131        assert_enum_candid_contract(MemoryRangeAuthorityMode::Allowed);
132        assert_enum_candid_contract(MemoryAllocationState::Retired);
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 memory enum");
140        let decoded = Decode!(&bytes, T).expect("decode memory enum");
141
142        assert_eq!(decoded, value);
143    }
144}