canic_memory/
registry.rs

1//! NOTE: All stable registry access is TLS-thread-local.
2//! This ensures atomicity on the IC’s single-threaded execution model.
3use crate::{impl_storable_bounded, manager::MEMORY_MANAGER};
4use candid::CandidType;
5use canic_cdk::{
6    structures::{
7        BTreeMap as StableBTreeMap, DefaultMemoryImpl,
8        memory::{MemoryId, VirtualMemory},
9    },
10    utils::time::now_secs,
11};
12use canic_types::BoundedString256;
13use serde::{Deserialize, Serialize};
14use std::cell::RefCell;
15use thiserror::Error as ThisError;
16
17///
18/// Reserved for the registry system itself
19///
20pub const MEMORY_REGISTRY_ID: u8 = 0;
21pub const MEMORY_RANGES_ID: u8 = 1;
22
23//
24// MEMORY_REGISTRY
25//
26
27thread_local! {
28    static MEMORY_REGISTRY: RefCell<StableBTreeMap<u8, MemoryRegistryEntry, VirtualMemory<DefaultMemoryImpl>>> =
29        RefCell::new(StableBTreeMap::init(
30            MEMORY_MANAGER.with_borrow(|this| {
31                this.get(MemoryId::new(MEMORY_REGISTRY_ID))
32            }),
33        ));
34}
35
36//
37// MEMORY_RANGES
38//
39
40thread_local! {
41    static MEMORY_RANGES: RefCell<StableBTreeMap<String, MemoryRange, VirtualMemory<DefaultMemoryImpl>>> =
42        RefCell::new(StableBTreeMap::init(
43            MEMORY_MANAGER.with_borrow(|mgr| {
44                mgr.get(MemoryId::new(MEMORY_RANGES_ID))
45            }),
46        ));
47}
48
49//
50// PENDING_REGISTRATIONS
51//
52// Queue of memory registrations produced during TLS initialization
53// Each entry is (id, crate_name, label).
54// These are deferred until `flush_pending_registrations()` is called,
55// which validates and inserts them into the global MemoryRegistry.
56//
57
58thread_local! {
59    static PENDING_REGISTRATIONS: RefCell<Vec<(u8, &'static str, &'static str)>> = const {
60        RefCell::new(Vec::new())
61    };
62}
63
64// public as it gets called from macros
65pub fn defer_register(id: u8, crate_name: &'static str, label: &'static str) {
66    PENDING_REGISTRATIONS.with(|q| {
67        q.borrow_mut().push((id, crate_name, label));
68    });
69}
70
71/// Drain (and clear) all pending registrations.
72/// Intended to be called from the ops layer during init/post-upgrade.
73#[must_use]
74pub fn drain_pending_registrations() -> Vec<(u8, &'static str, &'static str)> {
75    PENDING_REGISTRATIONS.with(|q| q.borrow_mut().drain(..).collect())
76}
77
78//
79// PENDING_RANGES
80//
81
82thread_local! {
83    pub static PENDING_RANGES: RefCell<Vec<(&'static str, u8, u8)>> = const {
84        RefCell::new(Vec::new())
85    };
86}
87
88// public as it gets called from macros
89pub fn defer_reserve_range(crate_name: &'static str, start: u8, end: u8) {
90    PENDING_RANGES.with(|q| q.borrow_mut().push((crate_name, start, end)));
91}
92
93/// Drain (and clear) all pending ranges.
94/// Intended to be called from the ops layer during init/post-upgrade.
95#[must_use]
96pub fn drain_pending_ranges() -> Vec<(&'static str, u8, u8)> {
97    PENDING_RANGES.with(|q| q.borrow_mut().drain(..).collect())
98}
99
100///
101/// MemoryRegistryError
102///
103
104#[derive(Debug, ThisError)]
105pub enum MemoryRegistryError {
106    #[error("ID {0} is already registered with type {1}, tried to register type {2}")]
107    AlreadyRegistered(u8, String, String),
108
109    #[error("crate `{0}` already has a reserved range")]
110    DuplicateRange(String),
111
112    #[error("crate `{0}` provided invalid range {1}-{2} (start > end)")]
113    InvalidRange(String, u8, u8),
114
115    #[error("crate `{0}` attempted to register ID {1}, but it is outside its allowed ranges")]
116    OutOfRange(String, u8),
117
118    #[error("crate `{0}` range {1}-{2} overlaps with crate `{3}` range {4}-{5}")]
119    Overlap(String, u8, u8, String, u8, u8),
120
121    #[error("crate `{0}` has not reserved any memory range")]
122    NoRange(String),
123}
124
125///
126/// MemoryRange
127///
128
129#[derive(Clone, Debug, Deserialize, Serialize)]
130pub struct MemoryRange {
131    pub crate_key: BoundedString256,
132    pub start: u8,
133    pub end: u8,
134    pub created_at: u64,
135}
136
137impl MemoryRange {
138    #[must_use]
139    pub(crate) fn new(crate_key: &str, start: u8, end: u8) -> Self {
140        Self {
141            crate_key: BoundedString256::new(crate_key),
142            start,
143            end,
144            created_at: now_secs(),
145        }
146    }
147
148    #[must_use]
149    pub fn contains(&self, id: u8) -> bool {
150        (self.start..=self.end).contains(&id)
151    }
152}
153
154impl_storable_bounded!(MemoryRange, 320, false);
155
156///
157/// MemoryRegistryEntry
158///
159
160#[derive(CandidType, Clone, Debug, Deserialize, Serialize)]
161pub struct MemoryRegistryEntry {
162    pub label: BoundedString256,
163    pub created_at: u64,
164}
165
166impl MemoryRegistryEntry {
167    #[must_use]
168    pub(crate) fn new(label: &str) -> Self {
169        Self {
170            label: BoundedString256::new(label),
171            created_at: now_secs(),
172        }
173    }
174}
175
176impl_storable_bounded!(MemoryRegistryEntry, 320, false);
177
178///
179/// MemoryRegistryView
180///
181
182pub type MemoryRegistryView = Vec<(u8, MemoryRegistryEntry)>;
183
184///
185/// MemoryRegistry
186///
187
188pub struct MemoryRegistry;
189
190impl MemoryRegistry {
191    /// Register an ID, enforcing crate’s allowed range.
192    ///
193    /// Pure domain/model-level function:
194    /// - no logging
195    /// - no unwrap
196    /// - no mapping to `crate::Error`
197    pub fn register(id: u8, crate_name: &str, label: &str) -> Result<(), MemoryRegistryError> {
198        let crate_key = crate_name.to_string();
199
200        // 1. Check reserved range
201        let range = MEMORY_RANGES.with_borrow(|ranges| ranges.get(&crate_key));
202        match range {
203            None => {
204                return Err(MemoryRegistryError::NoRange(crate_key));
205            }
206            Some(r) if !r.contains(id) => {
207                return Err(MemoryRegistryError::OutOfRange(crate_key, id));
208            }
209            Some(_) => {
210                // OK, continue
211            }
212        }
213
214        // 2. Check already registered
215        let existing = MEMORY_REGISTRY.with_borrow(|map| map.get(&id));
216        if let Some(existing) = existing {
217            if existing.label.as_ref() != label {
218                return Err(MemoryRegistryError::AlreadyRegistered(
219                    id,
220                    existing.label.to_string(),
221                    label.to_string(),
222                ));
223            }
224
225            // idempotent case
226            return Ok(());
227        }
228
229        // 3. Insert
230        MEMORY_REGISTRY.with_borrow_mut(|map| {
231            map.insert(id, MemoryRegistryEntry::new(label));
232        });
233
234        Ok(())
235    }
236
237    /// Reserve a block of memory IDs for a crate.
238    ///
239    /// Pure domain/model-level function, no logging or unwrap.
240    pub fn reserve_range(crate_name: &str, start: u8, end: u8) -> Result<(), MemoryRegistryError> {
241        if start > end {
242            return Err(MemoryRegistryError::InvalidRange(
243                crate_name.to_string(),
244                start,
245                end,
246            ));
247        }
248
249        let crate_key = crate_name.to_string();
250
251        // 1. Check for conflicts (existing ranges)
252        let conflict = MEMORY_RANGES.with_borrow(|ranges| {
253            if ranges.contains_key(&crate_key) {
254                return Some(MemoryRegistryError::DuplicateRange(crate_key.clone()));
255            }
256
257            for entry in ranges.iter() {
258                let other_crate = entry.key();
259                let other_range = entry.value();
260
261                if !(end < other_range.start || start > other_range.end) {
262                    return Some(MemoryRegistryError::Overlap(
263                        crate_key.clone(),
264                        start,
265                        end,
266                        other_crate.clone(),
267                        other_range.start,
268                        other_range.end,
269                    ));
270                }
271            }
272
273            None
274        });
275
276        if let Some(err) = conflict {
277            return Err(err);
278        }
279
280        // 2. Insert
281        MEMORY_RANGES.with_borrow_mut(|ranges| {
282            let range = MemoryRange::new(crate_name, start, end);
283            ranges.insert(crate_name.to_string(), range);
284        });
285
286        Ok(())
287    }
288
289    #[must_use]
290    pub fn get(id: u8) -> Option<MemoryRegistryEntry> {
291        MEMORY_REGISTRY.with_borrow(|map| map.get(&id))
292    }
293
294    #[must_use]
295    pub fn export() -> MemoryRegistryView {
296        MEMORY_REGISTRY.with_borrow(|map| {
297            map.iter()
298                .map(|entry| (*entry.key(), entry.value()))
299                .collect()
300        })
301    }
302
303    #[must_use]
304    pub fn export_ranges() -> Vec<(String, MemoryRange)> {
305        MEMORY_RANGES.with_borrow(|ranges| {
306            ranges
307                .iter()
308                .map(|e| (e.key().clone(), e.value()))
309                .collect()
310        })
311    }
312}
313
314#[cfg(test)]
315pub(crate) fn reset_for_tests() {
316    MEMORY_REGISTRY.with_borrow_mut(StableBTreeMap::clear);
317    MEMORY_RANGES.with_borrow_mut(StableBTreeMap::clear);
318    PENDING_REGISTRATIONS.with(|q| q.borrow_mut().clear());
319    PENDING_RANGES.with(|q| q.borrow_mut().clear());
320}
321
322///
323/// TESTS
324///
325
326#[cfg(test)]
327mod tests {
328    use super::*;
329
330    #[test]
331    fn reserve_range_happy_path_and_reject_overlap() {
332        reset_for_tests();
333        MemoryRegistry::reserve_range("crate_a", 10, 20).unwrap();
334
335        // Overlap with existing should error
336        let err = MemoryRegistry::reserve_range("crate_b", 15, 25).unwrap_err();
337        matches!(err, MemoryRegistryError::Overlap(_, _, _, _, _, _));
338
339        // Disjoint should succeed
340        MemoryRegistry::reserve_range("crate_b", 30, 40).unwrap();
341
342        let ranges = MemoryRegistry::export_ranges();
343        assert_eq!(ranges.len(), 2);
344    }
345
346    #[test]
347    fn reserve_range_rejects_invalid_order() {
348        reset_for_tests();
349        let err = MemoryRegistry::reserve_range("crate_a", 5, 4).unwrap_err();
350        matches!(err, MemoryRegistryError::InvalidRange(_, _, _));
351        assert!(MemoryRegistry::export_ranges().is_empty());
352    }
353
354    #[test]
355    fn register_id_requires_range_and_checks_bounds() {
356        reset_for_tests();
357        MemoryRegistry::reserve_range("crate_a", 1, 3).unwrap();
358
359        // Out of range
360        let err = MemoryRegistry::register(5, "crate_a", "Foo").unwrap_err();
361        matches!(err, MemoryRegistryError::OutOfRange(_, _));
362
363        // Happy path
364        MemoryRegistry::register(2, "crate_a", "Foo").unwrap();
365
366        // Idempotent same label
367        MemoryRegistry::register(2, "crate_a", "Foo").unwrap();
368
369        // Different label should error
370        let err = MemoryRegistry::register(2, "crate_a", "Bar").unwrap_err();
371        matches!(err, MemoryRegistryError::AlreadyRegistered(_, _, _));
372
373        let view = MemoryRegistry::export();
374        assert_eq!(view.len(), 1);
375        assert_eq!(view[0].0, 2);
376    }
377
378    #[test]
379    fn pending_queues_drain_in_order() {
380        reset_for_tests();
381        defer_reserve_range("crate_a", 1, 2);
382        defer_reserve_range("crate_b", 3, 4);
383        defer_register(1, "crate_a", "A1");
384        defer_register(3, "crate_b", "B3");
385
386        let ranges = drain_pending_ranges();
387        assert_eq!(ranges, vec![("crate_a", 1, 2), ("crate_b", 3, 4)]);
388        let regs = drain_pending_registrations();
389        assert_eq!(regs, vec![(1, "crate_a", "A1"), (3, "crate_b", "B3")]);
390
391        // queues are empty after drain
392        assert!(drain_pending_ranges().is_empty());
393        assert!(drain_pending_registrations().is_empty());
394    }
395}