ic-storage 0.76.2

Experimental scoped native collections and portable recovery for the Internet Computer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! Supplier-owned allocation and lifecycle for bounded native ordered roots.
//! Host backing is process-local; Wasm requires the matching prepared artifact.
//! Private Rust headers stay transient. Ordinary scopes do not re-admit tables.
mod backing;
#[cfg(target_arch = "wasm32")]
mod canister;
mod directory;
mod pool;
#[cfg(target_arch = "wasm32")]
pub use canister::Canister;
#[cfg(test)]
mod tests;
pub use crate::ordered::session::Root;
use crate::ordered::{
    self, Map,
    session::{self, Participant, Session},
};
use alloc::{
    rc::{Rc, Weak},
    vec::Vec,
};
use backing::Backing;
use core::cell::RefCell;
use directory::Image;
#[doc(hidden)]
pub use pool::{Element, NativeAllocator};
type Node = ordered::Node<512, 16384>;
const _: () = assert!(size_of::<Node>() == 56 && align_of::<Node>() == 8);
const DIRECTORY_BYTES: usize = 4096;
const MAX_ROOTS: usize = 16;
type Nodes = NativeAllocator<Node>;
type Bytes = NativeAllocator<u8>;
type Native = Map<512, 16384, Nodes, Bytes>;
type Family = Session<512, 16384, Nodes, Bytes>;
/// Read-only scoped access to granted native roots; references cannot escape.
pub type Read<'a> = session::Read<'a, 512, 16384, Nodes, Bytes>;
/// Synchronous scoped mutation. Ordinary Err retains earlier writes; unwind
/// poisons the host family. The IC supplies transaction rollback on traps.
pub type Write<'a> = session::Write<'a, 512, 16384, Nodes, Bytes>;

/// Disjoint retained domains under the supported Wasm runtime contract.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Bank {
    /// First native allocation domain.
    A,
    /// Separate domain for explicit portable reconstruction.
    B,
}
impl Bank {
    fn base(self) -> usize {
        match self {
            Self::A => backing::A,
            Self::B => backing::B,
        }
    }
}
/// Physical native capacity, separate from per-root logical limits. Capacities
/// include growth coexistence and fragmentation; per-root maxima need not all fit.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Geometry {
    /// Native allocation domain; cannot change on compatible reopen.
    pub bank: Bank,
    /// Total 56-byte node slots across every map, from 1 through 16,384.
    pub nodes: usize,
    /// Shared byte-payload capacity, from 1 through 15 MiB.
    pub bytes: usize,
}
impl Geometry {
    fn check(self) -> Result<(), Error> {
        if !(1..=16384).contains(&self.nodes) || !(1..=15 * 1024 * 1024).contains(&self.bytes) {
            Err(Error::Geometry)
        } else {
            Ok(())
        }
    }
}
/// Allocation, admission or lifecycle refusal. No failed reopen initializes data.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Error {
    /// Unsupported physical capacity or invalid/overlapping retained spans.
    Geometry,
    /// Invalid or changed root/schema/bounds declarations.
    Declaration,
    /// Missing, corrupt, open or incompatible retained directory.
    Directory,
    /// Explicit creation found existing retained bytes.
    NotFresh,
    /// Allocation refused before ownership publication.
    Allocation,
    /// An active callback prevents lifecycle/access reentry.
    Busy,
    /// Wrong lifecycle phase.
    State,
    /// Foreign, stale or already issued participant identity.
    Authority,
    /// Public collection/session refusal, including host unwind poisoning.
    Session(session::Error),
    /// Wasm was not prepared for this runtime contract.
    Startup,
}
impl From<session::Error> for Error {
    fn from(error: session::Error) -> Self {
        Self::Session(error)
    }
}
impl From<ordered::Error> for Error {
    fn from(error: ordered::Error) -> Self {
        Self::Session(session::Error::Collection(error))
    }
}
/// Non-cloneable participant capability. Close revokes it; reopening never
/// revives stale grants, even if a caller kept or forgot their token.
pub struct Grant {
    identity: Weak<()>,
    index: usize,
}
struct Holder {
    id: u64,
    issued: bool,
    grant: Participant<512, 16384, Nodes, Bytes>,
}
struct Open {
    identity: Rc<()>,
    family: Family,
    holders: Vec<Holder>,
    nodes: Nodes,
    bytes: Bytes,
}
/// Native host owner and internal canister engine. No pointer, allocator or map
/// ownership is exposed. Host construction owns backing until all native owners end.
///
/// ```
/// use ic_storage::retained::{Bank, Geometry, Root, Runtime};
/// let roots = [Root { id: 1, participant: 1, schema: 1, entries: 32,
///     key_bytes: 512, value_bytes: 4096 }];
/// let runtime = Runtime::host(Geometry { bank: Bank::A, nodes: 128,
///     bytes: 65536 }, &roots).unwrap();
/// let grant = runtime.grant(1).unwrap();
/// runtime.update(&[&grant], |mut v| v.insert(1, b"key", b"value").unwrap()).unwrap();
/// runtime.close().unwrap();
/// runtime.reopen(&roots).unwrap();
/// assert!(runtime.read(&[&grant], |_| ()).is_err());
/// let next = runtime.grant(1).unwrap();
/// runtime.read(&[&next], |v| assert!(v.contains_key(1, b"key").unwrap())).unwrap();
/// ```
///
/// Borrowed data cannot outlive its runtime scope:
/// ```compile_fail
/// use ic_storage::retained::{Grant, Runtime};
/// fn escape(runtime: &Runtime, grant: &Grant) {
///     let value = runtime.read(&[grant], |v| v.get(1, b"key").unwrap());
/// }
/// ```
pub struct Runtime {
    backing: Rc<Backing>,
    roots: Vec<Root>,
    state: RefCell<Option<Open>>,
}
fn declarations(roots: &[Root]) -> Result<(), Error> {
    if roots.is_empty() || roots.len() > MAX_ROOTS {
        return Err(Error::Declaration);
    }
    let mut prior = 0;
    for root in roots {
        if root.id <= prior
            || root.participant == 0
            || root.schema == 0
            || root.key_bytes > 512
            || root.value_bytes > 16384
        {
            return Err(Error::Declaration);
        }
        prior = root.id;
    }
    Ok(())
}
impl Runtime {
    /// Create fresh process-local host backing with the same allocator and public
    /// scopes as the canister binding. Does not model process restart or IC rollback.
    ///
    /// # Errors
    /// Refuses invalid declarations/capacity or allocation failure.
    #[cfg(not(target_arch = "wasm32"))]
    pub fn host(geometry: Geometry, roots: &[Root]) -> Result<Self, Error> {
        declarations(roots)?;
        Self::create(Backing::host(geometry)?, roots)
    }
    fn create(backing: Rc<Backing>, roots: &[Root]) -> Result<Self, Error> {
        declarations(roots)?;
        if !backing.fresh() {
            return Err(Error::NotFresh);
        }
        let runtime = Self {
            backing,
            roots: roots.to_vec(),
            state: RefCell::new(None),
        };
        runtime.backing.publish(directory::encode(
            runtime.backing.geometry,
            roots,
            &[Image::default(); MAX_ROOTS][..roots.len()],
            true,
        ));
        runtime.reopen(roots)?;
        Ok(runtime)
    }
    /// Reopen closed roots under exactly the expected declarations. Native
    /// admission occurs here, once per reopening, never on ordinary access.
    ///
    /// # Errors
    /// Refuses active/poisoned owners, changed declarations or invalid retained data.
    pub fn reopen(&self, expected: &[Root]) -> Result<(), Error> {
        if expected != self.roots {
            return Err(Error::Declaration);
        }
        let mut state = self.state.try_borrow_mut().map_err(|_| Error::Busy)?;
        if state.is_some() {
            return Err(Error::State);
        }
        let images = directory::decode(&self.backing.header(), self.backing.geometry, &self.roots)?;
        let node_spans: Vec<_> = images[..self.roots.len()]
            .iter()
            .filter(|i| i.nodes.cap != 0)
            .map(|i| i.nodes)
            .collect();
        let byte_spans: Vec<_> = images[..self.roots.len()]
            .iter()
            .filter(|i| i.bytes.cap != 0)
            .map(|i| i.bytes)
            .collect();
        let nodes = Nodes::claim(self.backing.clone(), &node_spans)?;
        let bytes = Bytes::claim(self.backing.clone(), &byte_spans)?;
        let mut maps = Vec::new();
        maps.try_reserve_exact(self.roots.len())
            .map_err(|_| Error::Allocation)?;
        let (mut n, mut b) = (0, 0);
        for (root, image) in self.roots.iter().copied().zip(images) {
            let map = if image.nodes.cap == 0 {
                Native::new_in_allocators(root.entries, nodes.clone(), bytes.clone())
            } else {
                let node_vec = nodes.restore(n)?;
                n += 1;
                let byte_vec = if image.bytes.cap == 0 {
                    Vec::new_in(bytes.clone())
                } else {
                    let value = bytes.restore(b)?;
                    b += 1;
                    value
                };
                Native::admit(
                    ordered::Image {
                        nodes: node_vec,
                        payload: byte_vec,
                        root: image.root,
                        free: image.free,
                        len: image.len,
                    },
                    root.entries,
                )
                .map_err(|e| Error::from(e.error))?
            };
            maps.push((root, map));
        }
        let mut family = Session::from_maps(maps, &self.roots).map_err(|e| Error::from(e.error))?;
        let mut holders: Vec<Holder> = Vec::new();
        holders
            .try_reserve_exact(self.roots.len())
            .map_err(|_| Error::Allocation)?;
        for root in &self.roots {
            if !holders.iter().any(|h| h.id == root.participant) {
                holders.push(Holder {
                    id: root.participant,
                    issued: false,
                    grant: family.take(root.participant)?,
                });
            }
        }
        *state = Some(Open {
            identity: Rc::new(()),
            family,
            holders,
            nodes,
            bytes,
        });
        self.backing.publish(directory::encode(
            self.backing.geometry,
            &self.roots,
            &images[..self.roots.len()],
            false,
        ));
        Ok(())
    }
    /// Issue a participant once in this generation. Dropping a token does not
    /// reissue it. Close/reopen creates a new generation and revokes old tokens.
    ///
    /// # Errors
    /// Refuses a closed/busy runtime, unknown identity or duplicate issuance.
    pub fn grant(&self, participant: u64) -> Result<Grant, Error> {
        let mut state = self.state.try_borrow_mut().map_err(|_| Error::Busy)?;
        let open = state.as_mut().ok_or(Error::State)?;
        let index = open
            .holders
            .iter()
            .position(|h| h.id == participant)
            .ok_or(Error::Authority)?;
        if open.holders[index].issued {
            return Err(Error::Authority);
        }
        open.holders[index].issued = true;
        Ok(Grant {
            identity: Rc::downgrade(&open.identity),
            index,
        })
    }
    fn indices(open: &Open, grants: &[&Grant]) -> Result<Vec<usize>, Error> {
        if grants.is_empty() || grants.len() > MAX_ROOTS {
            return Err(Error::Authority);
        }
        let mut indices = Vec::new();
        indices
            .try_reserve_exact(grants.len())
            .map_err(|_| Error::Allocation)?;
        for grant in grants {
            if !grant.identity.ptr_eq(&Rc::downgrade(&open.identity))
                || indices.contains(&grant.index)
                || grant.index >= open.holders.len()
            {
                return Err(Error::Authority);
            }
            indices.push(grant.index);
        }
        Ok(indices)
    }
    /// Read composed grants without scanning or readmitting native tables.
    ///
    /// # Errors
    /// Refuses closed, busy, foreign/stale/duplicate grants or host poisoning.
    pub fn read<R>(
        &self,
        grants: &[&Grant],
        operation: impl for<'a> FnOnce(Read<'a>) -> R,
    ) -> Result<R, Error> {
        let state = self.state.try_borrow().map_err(|_| Error::Busy)?;
        let open = state.as_ref().ok_or(Error::State)?;
        let indices = Self::indices(open, grants)?;
        let grants: Vec<_> = indices.iter().map(|i| &open.holders[*i].grant).collect();
        Ok(open.family.read(&grants, operation)?)
    }
    /// Mutate composed grants synchronously. Err preserves preceding writes;
    /// escaping unwind poisons the host family. IC traps roll back the message.
    ///
    /// # Errors
    /// Refuses closed/busy state, invalid grants or poisoned native owners.
    pub fn update<R>(
        &self,
        grants: &[&Grant],
        operation: impl for<'a> FnOnce(Write<'a>) -> R,
    ) -> Result<R, Error> {
        let mut state = self.state.try_borrow_mut().map_err(|_| Error::Busy)?;
        let open = state.as_mut().ok_or(Error::State)?;
        let indices = Self::indices(open, grants)?;
        let grants: Vec<_> = indices.iter().map(|i| &open.holders[*i].grant).collect();
        Ok(open.family.update(&grants, operation)?)
    }
    /// Revoke grants, end every owning map and settle both pools before publishing
    /// the closed directory. Live callbacks and poisoned hosts cannot close.
    ///
    /// # Errors
    /// Refuses closed/busy/poisoned state or an inconsistent custody ledger.
    pub fn close(&self) -> Result<(), Error> {
        let mut state = self.state.try_borrow_mut().map_err(|_| Error::Busy)?;
        let open = state.as_ref().ok_or(Error::State)?;
        let grants: Vec<_> = open.holders.iter().map(|h| &h.grant).collect();
        open.family.read(&grants, |_| ())?;
        drop(grants);
        let Open {
            identity: _,
            family,
            holders,
            nodes,
            bytes,
        } = state.take().ok_or(Error::State)?;
        drop(holders);
        let maps = family.into_maps().map_err(|_| Error::State)?;
        let mut images = [Image::default(); MAX_ROOTS];
        for (index, (_, map)) in maps.into_iter().enumerate() {
            let image = map.release()?;
            images[index] = Image {
                nodes: nodes.release(image.nodes)?.unwrap_or_default(),
                bytes: bytes.release(image.payload)?.unwrap_or_default(),
                root: image.root,
                free: image.free,
                len: image.len,
            };
        }
        if !nodes.settled() || !bytes.settled() {
            return Err(Error::State);
        }
        drop(nodes);
        drop(bytes);
        self.backing.publish(directory::encode(
            self.backing.geometry,
            &self.roots,
            &images[..self.roots.len()],
            true,
        ));
        Ok(())
    }
}