1use super::{AllocationRetirementError, LedgerIntegrityError};
2use crate::{
3 declaration::{AllocationDeclaration, DeclarationSnapshotError, validate_runtime_fingerprint},
4 key::StableKey,
5 schema::{SchemaMetadata, SchemaMetadataError},
6 slot::AllocationSlotDescriptor,
7 validation::Validate,
8};
9use serde::{Deserialize, Serialize};
10
11#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
28#[serde(deny_unknown_fields)]
29pub struct AllocationLedger {
30 pub(crate) current_generation: u64,
32 pub(crate) allocation_history: AllocationHistory,
34}
35
36#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
45#[serde(deny_unknown_fields)]
46pub struct AllocationHistory {
47 #[serde(deserialize_with = "crate::cbor::deserialize_records")]
49 pub(crate) records: Vec<AllocationRecord>,
50 #[serde(deserialize_with = "crate::cbor::deserialize_history")]
52 pub(crate) generations: Vec<GenerationRecord>,
53}
54
55#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
66#[serde(deny_unknown_fields)]
67pub struct AllocationRecord {
68 pub(crate) stable_key: StableKey,
70 pub(crate) slot: AllocationSlotDescriptor,
72 pub(crate) state: AllocationState,
74 pub(crate) first_generation: u64,
76 pub(crate) last_seen_generation: u64,
78 #[serde(deserialize_with = "crate::cbor::deserialize_history")]
80 pub(crate) schema_history: Vec<SchemaMetadataRecord>,
81}
82
83#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
91#[serde(deny_unknown_fields)]
92pub struct AllocationRetirement {
93 pub(crate) stable_key: StableKey,
95 pub(crate) slot: AllocationSlotDescriptor,
97}
98
99impl AllocationRetirement {
100 pub fn new(
102 stable_key: impl AsRef<str>,
103 slot: AllocationSlotDescriptor,
104 ) -> Result<Self, AllocationRetirementError> {
105 let retirement = Self {
106 stable_key: StableKey::parse(stable_key).map_err(AllocationRetirementError::Key)?,
107 slot,
108 };
109 retirement.validate()?;
110 Ok(retirement)
111 }
112
113 #[must_use]
115 pub const fn stable_key(&self) -> &StableKey {
116 &self.stable_key
117 }
118
119 #[must_use]
121 pub const fn slot(&self) -> &AllocationSlotDescriptor {
122 &self.slot
123 }
124
125 pub fn validate(&self) -> Result<(), AllocationRetirementError> {
127 self.stable_key
128 .validate()
129 .map_err(AllocationRetirementError::Key)?;
130 self.slot
131 .validate()
132 .map_err(AllocationRetirementError::MemoryManagerSlot)
133 }
134}
135
136#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
143#[serde(deny_unknown_fields)]
144pub enum AllocationState {
145 Reserved,
147 Active,
149 Retired {
151 generation: u64,
153 },
154}
155
156#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
165#[serde(deny_unknown_fields)]
166pub struct SchemaMetadataRecord {
167 pub(crate) generation: u64,
169 pub(crate) schema: SchemaMetadata,
171}
172
173#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
180#[serde(deny_unknown_fields)]
181pub struct GenerationRecord {
182 pub(crate) generation: u64,
184 pub(crate) parent_generation: u64,
186 #[serde(deserialize_with = "crate::cbor::deserialize_present_option")]
188 pub(crate) runtime_fingerprint: Option<String>,
189 pub(crate) declaration_count: u32,
191 #[serde(deserialize_with = "crate::cbor::deserialize_present_option")]
193 pub(crate) committed_at: Option<u64>,
194}
195
196#[derive(Clone, Debug, Eq, PartialEq)]
212pub struct RecoveredLedger {
213 ledger: AllocationLedger,
214}
215
216impl RecoveredLedger {
217 pub(crate) const fn from_trusted_ledger(ledger: AllocationLedger) -> Self {
218 Self { ledger }
219 }
220
221 #[must_use]
227 pub const fn ledger(&self) -> &AllocationLedger {
228 &self.ledger
229 }
230
231 #[must_use]
234 pub const fn physical_generation(&self) -> u64 {
235 self.ledger.current_generation
236 }
237
238 #[must_use]
240 pub const fn current_generation(&self) -> u64 {
241 self.ledger.current_generation
242 }
243
244 pub(crate) fn into_ledger(self) -> AllocationLedger {
245 self.ledger
246 }
247}
248
249impl AllocationHistory {
250 #[cfg(test)]
251 pub(crate) const fn from_parts(
252 records: Vec<AllocationRecord>,
253 generations: Vec<GenerationRecord>,
254 ) -> Self {
255 Self {
256 records,
257 generations,
258 }
259 }
260
261 #[must_use]
263 pub fn records(&self) -> &[AllocationRecord] {
264 &self.records
265 }
266
267 #[must_use]
269 pub fn generations(&self) -> &[GenerationRecord] {
270 &self.generations
271 }
272
273 #[must_use]
275 pub const fn is_empty(&self) -> bool {
276 self.records.is_empty() && self.generations.is_empty()
277 }
278
279 pub(crate) const fn records_mut(&mut self) -> &mut Vec<AllocationRecord> {
280 &mut self.records
281 }
282
283 #[cfg(test)]
284 pub(crate) const fn generations_mut(&mut self) -> &mut Vec<GenerationRecord> {
285 &mut self.generations
286 }
287
288 pub(crate) fn push_record(&mut self, record: AllocationRecord) {
289 self.records.push(record);
290 }
291
292 pub(crate) fn push_generation(&mut self, generation: GenerationRecord) {
293 self.generations.push(generation);
294 }
295}
296
297impl SchemaMetadataRecord {
298 pub fn new(generation: u64, schema: SchemaMetadata) -> Result<Self, SchemaMetadataError> {
300 schema.validate()?;
301 Ok(Self { generation, schema })
302 }
303
304 #[must_use]
306 pub const fn generation(&self) -> u64 {
307 self.generation
308 }
309
310 #[must_use]
312 pub const fn schema(&self) -> &SchemaMetadata {
313 &self.schema
314 }
315}
316
317impl GenerationRecord {
318 pub fn new(
320 generation: u64,
321 parent_generation: u64,
322 runtime_fingerprint: Option<String>,
323 declaration_count: u32,
324 committed_at: Option<u64>,
325 ) -> Result<Self, DeclarationSnapshotError> {
326 validate_runtime_fingerprint(runtime_fingerprint.as_deref())?;
327 Ok(Self {
328 generation,
329 parent_generation,
330 runtime_fingerprint,
331 declaration_count,
332 committed_at,
333 })
334 }
335
336 #[must_use]
338 pub const fn generation(&self) -> u64 {
339 self.generation
340 }
341
342 #[must_use]
344 pub const fn parent_generation(&self) -> u64 {
345 self.parent_generation
346 }
347
348 #[must_use]
350 pub fn runtime_fingerprint(&self) -> Option<&str> {
351 self.runtime_fingerprint.as_deref()
352 }
353
354 #[must_use]
356 pub const fn declaration_count(&self) -> u32 {
357 self.declaration_count
358 }
359
360 #[must_use]
362 pub const fn committed_at(&self) -> Option<u64> {
363 self.committed_at
364 }
365}
366
367impl AllocationRecord {
368 fn from_declaration(
371 generation: u64,
372 declaration: AllocationDeclaration,
373 state: AllocationState,
374 ) -> Self {
375 Self {
376 stable_key: declaration.stable_key,
377 slot: declaration.slot,
378 state,
379 first_generation: generation,
380 last_seen_generation: generation,
381 schema_history: vec![SchemaMetadataRecord {
382 generation,
383 schema: declaration.schema,
384 }],
385 }
386 }
387
388 pub(crate) fn active(generation: u64, declaration: AllocationDeclaration) -> Self {
390 Self::from_declaration(generation, declaration, AllocationState::Active)
391 }
392
393 pub(crate) fn reserved(generation: u64, declaration: AllocationDeclaration) -> Self {
395 Self::from_declaration(generation, declaration, AllocationState::Reserved)
396 }
397
398 #[must_use]
400 pub const fn stable_key(&self) -> &StableKey {
401 &self.stable_key
402 }
403
404 #[must_use]
406 pub const fn slot(&self) -> &AllocationSlotDescriptor {
407 &self.slot
408 }
409
410 #[must_use]
412 pub const fn state(&self) -> AllocationState {
413 self.state
414 }
415
416 #[must_use]
418 pub const fn first_generation(&self) -> u64 {
419 self.first_generation
420 }
421
422 #[must_use]
424 pub const fn last_seen_generation(&self) -> u64 {
425 self.last_seen_generation
426 }
427
428 #[must_use]
430 pub fn schema_history(&self) -> &[SchemaMetadataRecord] {
431 &self.schema_history
432 }
433
434 pub(crate) fn observe_declaration(
435 &mut self,
436 generation: u64,
437 declaration: &AllocationDeclaration,
438 ) {
439 if self.state == AllocationState::Reserved {
440 self.state = AllocationState::Active;
441 }
442 self.observe_schema(generation, &declaration.schema);
443 }
444
445 pub(super) fn observe_schema(&mut self, generation: u64, schema: &SchemaMetadata) {
446 self.last_seen_generation = generation;
447
448 let latest_schema = self.schema_history.last().map(|record| &record.schema);
449 if latest_schema != Some(schema) {
450 self.schema_history.push(SchemaMetadataRecord {
451 generation,
452 schema: schema.clone(),
453 });
454 }
455 }
456}
457
458impl AllocationLedger {
459 pub(crate) fn empty_genesis() -> Self {
461 Self {
464 current_generation: 0,
465 allocation_history: AllocationHistory::default(),
466 }
467 }
468
469 pub fn new(
477 current_generation: u64,
478 allocation_history: AllocationHistory,
479 ) -> Result<Self, LedgerIntegrityError> {
480 let ledger = Self {
481 current_generation,
482 allocation_history,
483 };
484 ledger.validate_integrity()?;
485 Ok(ledger)
486 }
487
488 pub fn new_committed(
494 current_generation: u64,
495 allocation_history: AllocationHistory,
496 ) -> Result<Self, LedgerIntegrityError> {
497 let ledger = Self {
498 current_generation,
499 allocation_history,
500 };
501 ledger.validate_committed_integrity()?;
502 Ok(ledger)
503 }
504
505 #[must_use]
507 pub const fn current_generation(&self) -> u64 {
508 self.current_generation
509 }
510
511 #[must_use]
513 pub const fn allocation_history(&self) -> &AllocationHistory {
514 &self.allocation_history
515 }
516}