1use crate::LedgerCommitStore;
2use ic_stable_structures::{Memory, Storable, storable::Bound};
3use serde::{Deserialize, Serialize};
4use std::borrow::Cow;
5use thiserror::Error;
6
7pub const STABLE_CELL_MAGIC: &[u8; 3] = b"SCL";
9pub const STABLE_CELL_LAYOUT_VERSION: u8 = 1;
11pub const STABLE_CELL_HEADER_SIZE: usize = 8;
13pub const STABLE_CELL_VALUE_OFFSET: u64 = 8;
15const WASM_PAGE_SIZE: u64 = 65_536;
16
17#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
28pub struct StableCellLedgerRecord {
29 store: LedgerCommitStore,
30}
31
32impl StableCellLedgerRecord {
33 #[must_use]
35 pub const fn new(store: LedgerCommitStore) -> Self {
36 Self { store }
37 }
38
39 #[must_use]
41 pub const fn store(&self) -> &LedgerCommitStore {
42 &self.store
43 }
44
45 pub const fn store_mut(&mut self) -> &mut LedgerCommitStore {
47 &mut self.store
48 }
49
50 #[must_use]
52 pub fn into_store(self) -> LedgerCommitStore {
53 self.store
54 }
55}
56
57impl Storable for StableCellLedgerRecord {
58 const BOUND: Bound = Bound::Unbounded;
59
60 fn to_bytes(&self) -> Cow<'_, [u8]> {
61 Cow::Owned(serialize_record(self))
62 }
63
64 fn into_bytes(self) -> Vec<u8> {
65 serialize_record(&self)
66 }
67
68 fn from_bytes(bytes: Cow<'_, [u8]>) -> Self {
69 decode_stable_cell_ledger_record(&bytes).unwrap_or_else(|err| {
70 panic!("StableCellLedgerRecord deserialize failed: {err}");
71 })
72 }
73}
74
75#[derive(Clone, Debug, Eq, Error, PartialEq)]
80pub enum StableCellPayloadError {
81 #[error("memory is not an ic-stable-structures Cell")]
83 NotStableCell,
84 #[error("unsupported stable-cell layout version {version}")]
86 UnsupportedVersion {
87 version: u8,
89 },
90 #[error("stable-cell payload length {value_len} exceeds available bytes {available_bytes}")]
92 InvalidLength {
93 value_len: u64,
95 available_bytes: u64,
97 },
98 #[error("stable-cell payload length {value_len} cannot fit in usize")]
100 LengthOverflow {
101 value_len: u64,
103 },
104}
105
106#[derive(Debug, Error)]
111pub enum StableCellLedgerError {
112 #[error(transparent)]
114 Payload(#[from] StableCellPayloadError),
115 #[error("stable-cell ledger record decode failed")]
117 Record(#[source] serde_cbor::Error),
118}
119
120pub fn decode_stable_cell_payload<M: Memory>(
126 memory: &M,
127) -> Result<Vec<u8>, StableCellPayloadError> {
128 let mut header = [0; STABLE_CELL_HEADER_SIZE];
129 memory.read(0, &mut header);
130 if &header[0..3] != STABLE_CELL_MAGIC {
131 return Err(StableCellPayloadError::NotStableCell);
132 }
133 if header[3] != STABLE_CELL_LAYOUT_VERSION {
134 return Err(StableCellPayloadError::UnsupportedVersion { version: header[3] });
135 }
136
137 let value_len = u64::from(u32::from_le_bytes([
138 header[4], header[5], header[6], header[7],
139 ]));
140 let available_bytes = memory.size().saturating_mul(WASM_PAGE_SIZE);
141 let payload_capacity = available_bytes.saturating_sub(STABLE_CELL_VALUE_OFFSET);
142 if value_len > payload_capacity {
143 return Err(StableCellPayloadError::InvalidLength {
144 value_len,
145 available_bytes: payload_capacity,
146 });
147 }
148 let value_len = usize::try_from(value_len)
149 .map_err(|_| StableCellPayloadError::LengthOverflow { value_len })?;
150
151 let mut bytes = vec![0; value_len];
152 memory.read(STABLE_CELL_VALUE_OFFSET, &mut bytes);
153 Ok(bytes)
154}
155
156pub fn decode_stable_cell_ledger_record(
162 bytes: &[u8],
163) -> Result<StableCellLedgerRecord, serde_cbor::Error> {
164 serde_cbor::from_slice(bytes)
165}
166
167pub fn validate_stable_cell_ledger_memory<M: Memory>(
175 memory: &M,
176) -> Result<(), StableCellLedgerError> {
177 if memory.size() == 0 {
178 return Ok(());
179 }
180
181 let payload = decode_stable_cell_payload(memory)?;
182 decode_stable_cell_ledger_record(&payload).map_err(StableCellLedgerError::Record)?;
183 Ok(())
184}
185
186fn serialize_record(record: &StableCellLedgerRecord) -> Vec<u8> {
187 serde_cbor::to_vec(record).unwrap_or_else(|err| {
188 panic!("StableCellLedgerRecord serialize failed: {err}");
189 })
190}
191
192#[cfg(test)]
193mod tests {
194 use super::*;
195 use ic_stable_structures::{Cell, VectorMemory};
196
197 #[test]
198 fn stable_cell_ledger_record_round_trips_through_cell() {
199 let memory = VectorMemory::default();
200 let record = StableCellLedgerRecord::default();
201 let cell = Cell::init(memory.clone(), record.clone());
202
203 assert_eq!(cell.get(), &record);
204 let payload = decode_stable_cell_payload(&memory).expect("decode stable cell payload");
205 let decoded = StableCellLedgerRecord::from_bytes(Cow::Owned(payload));
206 assert_eq!(decoded, record);
207 }
208
209 #[test]
210 fn stable_cell_payload_rejects_non_cell_memory() {
211 let memory = VectorMemory::default();
212 memory.grow(1);
213 memory.write(0, b"BAD");
214
215 assert_eq!(
216 decode_stable_cell_payload(&memory),
217 Err(StableCellPayloadError::NotStableCell)
218 );
219 }
220
221 #[test]
222 fn stable_cell_ledger_preflight_classifies_bad_record_without_panic() {
223 let memory = VectorMemory::default();
224 memory.grow(1);
225 memory.write(0, STABLE_CELL_MAGIC);
226 memory.write(3, &[STABLE_CELL_LAYOUT_VERSION]);
227 memory.write(4, &1_u32.to_le_bytes());
228 memory.write(STABLE_CELL_VALUE_OFFSET, &[0xff]);
229
230 let err =
231 validate_stable_cell_ledger_memory(&memory).expect_err("bad record must be classified");
232
233 assert!(matches!(err, StableCellLedgerError::Record(_)));
234 }
235}