1use crate::dto::prelude::*;
2
3pub use crate::domain::memory::{
4 MemoryAllocationState, MemoryCommitRecoveryErrorResponse, MemoryRangeAuthorityMode,
5};
6
7#[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#[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#[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#[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#[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#[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#[derive(CandidType, Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
92pub struct MemoryAllocationSizeEntry {
93 pub wasm_pages: u64,
94 pub bytes: u64,
95}
96
97#[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#[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}