Skip to main content

ic_memory/ledger/
error.rs

1use crate::{key::StableKey, physical::CommitRecoveryError, slot::MemoryManagerSlot};
2
3///
4/// LedgerIntegrityError
5///
6/// Decoded ledger violates current ownership invariants.
7///
8
9#[non_exhaustive]
10#[derive(Clone, Debug, Eq, thiserror::Error, PartialEq)]
11pub enum LedgerIntegrityError {
12    /// Current structural recovery ceiling exceeded.
13    #[error("ledger exceeds {resource} limit {limit}")]
14    LimitExceeded {
15        resource: &'static str,
16        limit: usize,
17    },
18    /// Stable key appears in more than one retained allocation record.
19    #[error("stable key '{stable_key}' appears in more than one allocation record")]
20    DuplicateStableKey { stable_key: StableKey },
21    /// Memory ID appears in more than one retained allocation record.
22    #[error("allocation slot '{slot:?}' appears in more than one allocation record")]
23    DuplicateSlot { slot: MemoryManagerSlot },
24    /// Commit counter zero is reserved for empty genesis.
25    #[error("genesis ledger contains allocation records")]
26    NonemptyGenesis,
27}
28
29///
30/// LedgerCommitError
31///
32/// Failure to recover or commit a logical allocation ledger.
33#[non_exhaustive]
34#[derive(Clone, Debug, Eq, thiserror::Error, PartialEq)]
35pub enum LedgerCommitError {
36    /// Protected physical commit recovery failed.
37    #[error(transparent)]
38    Recovery(CommitRecoveryError),
39    /// Physical slot generation and decoded logical ledger generation disagree.
40    #[error(
41        "physical generation {physical_generation} does not match logical ledger generation {logical_generation}"
42    )]
43    PhysicalLogicalGenerationMismatch {
44        /// Generation encoded in the physical snapshot.
45        physical_generation: u64,
46        /// Generation decoded from the logical allocation ledger.
47        logical_generation: u64,
48    },
49    /// Built-in ledger decoding failed.
50    #[error("allocation ledger codec failed: {0}")]
51    Codec(String),
52    /// Decoded ledger violates current ownership invariants.
53    #[error(transparent)]
54    Integrity(LedgerIntegrityError),
55}
56
57///
58/// AllocationStageError
59///
60/// Failure to stage a validated allocation generation.
61///
62
63#[non_exhaustive]
64#[derive(Clone, Debug, Eq, thiserror::Error, PartialEq)]
65pub enum AllocationStageError {
66    /// The commit counter cannot advance without overflow.
67    #[error("ledger generation {generation} cannot be advanced without overflow")]
68    GenerationOverflow { generation: u64 },
69    #[error(transparent)]
70    Integrity(#[from] LedgerIntegrityError),
71}