Skip to main content

ic_backup/policy/ic_snapshot_upload_observation/settlement/
mod.rs

1//! Pure original metadata-allocation settlement admission.
2use super::validate_response;
3use crate::{
4    model::{
5        artifacts::ArtifactChecksumRecord,
6        attempt_journal::{AttemptJournalRecord, ObservationOutcomeRecord},
7        ic_observation::IcObservationResponse,
8        ic_snapshot_reply::{IcSnapshotInfo, IcSnapshotReply},
9        ic_snapshot_upload::IcSnapshotUploadError,
10        ic_snapshot_upload_observation::{
11            IcSnapshotUploadAttribution, IcSnapshotUploadObservationRequest,
12            IcSnapshotUploadSettlement,
13        },
14    },
15    policy::{
16        ic_observation::{
17            IcObservationAssociationError, IcObservationReplyView, IcObservationResponseView,
18        },
19        inventory_settlement::{
20            InventoryAttribution, InventorySettlementIdentity, InventorySettlementMismatch,
21            admitted_outcome, validate_pair,
22        },
23        settlement_identity::SettlementIdentity,
24        snapshot_inventory_delta::SnapshotInventoryDeltaError,
25    },
26};
27use thiserror::Error;
28
29/// Exact local evidence and passive attribution; never an authenticated receipt.
30#[derive(Debug)]
31pub struct IcSnapshotUploadSettlementView<'a> {
32    observation: IcObservationResponseView<'a>,
33    baseline: &'a IcSnapshotReply<'a>,
34    settlement: &'a IcSnapshotUploadSettlement,
35    allocated_snapshot: Option<usize>,
36    outcome: ObservationOutcomeRecord,
37}
38impl<'a> IcSnapshotUploadSettlementView<'a> {
39    /// Read the passive claim; only a qualified integration may record it.
40    #[must_use]
41    pub const fn outcome(&self) -> ObservationOutcomeRecord {
42        self.outcome
43    }
44    /// Read the original independently retained baseline.
45    #[must_use]
46    pub const fn baseline(&self) -> &'a IcSnapshotReply<'a> {
47        self.baseline
48    }
49    /// Read the exact bounded current list association.
50    #[must_use]
51    pub const fn observation(&self) -> &IcObservationResponseView<'a> {
52        &self.observation
53    }
54    /// Read the complete retained integration claims.
55    #[must_use]
56    pub const fn settlement(&self) -> &'a IcSnapshotUploadSettlement {
57        self.settlement
58    }
59    /// Read only the explicitly attributed new descriptor, with no singleton inference.
60    #[must_use]
61    pub fn allocated_snapshot(&self) -> Option<&IcSnapshotInfo> {
62        self.observation
63            .inventory_snapshot(self.allocated_snapshot?)
64    }
65}
66
67/// Admit separately qualified allocation claims against exact original and current lists.
68///
69/// Every baseline ID and its metadata must remain unchanged. Applied requires an
70/// explicitly attributed new ID distinct from the retained source; several candidates
71/// may exist. Zero/one/many candidates imply no outcome. Negative evidence must exclude
72/// transient allocation/deletion. Unresolved requires a settled authenticated list,
73/// never a lost response. Integrations own original baseline chronology, actual
74/// authentication, freshness, attribution and stable custody; matching digests prove
75/// none of these. No calls, IO, receipts, retries, refunds or release admission occur.
76/// # Errors
77/// Rejects changed reservations/claims/wire, baseline drift, settlement identity drift
78/// and an Applied ID outside the exact new candidate set or original destination bounds.
79pub fn validate_settlement<'a>(
80    request: &IcSnapshotUploadObservationRequest<'a, '_>,
81    journal: &AttemptJournalRecord,
82    baseline: &'a IcSnapshotReply<'a>,
83    response: &'a IcObservationResponse,
84    challenge: &ArtifactChecksumRecord,
85    settlement: &'a IcSnapshotUploadSettlement,
86) -> Result<IcSnapshotUploadSettlementView<'a>, IcSnapshotUploadSettlementError> {
87    let observation = validate_response(request, journal, response)?;
88    let IcObservationReplyView::Inventory(inventory) = observation.reply() else {
89        return Err(IcSnapshotUploadSettlementError::WrongInventory);
90    };
91    let authority = request.authority().digest();
92    let candidates = validate_pair::<IcSnapshotUploadSettlementError>(
93        request.payload().target(),
94        baseline,
95        inventory,
96        &InventorySettlementIdentity {
97            operation: SettlementIdentity {
98                authority: &settlement.authority,
99                mutation_attempt: settlement.mutation_attempt,
100                observation_attempt: settlement.observation_attempt,
101                challenge: &settlement.challenge,
102            },
103            baseline: &settlement.baseline,
104            inventory: &settlement.inventory,
105            observation_evidence: &settlement.observation_evidence,
106        },
107        &SettlementIdentity {
108            authority: &authority,
109            mutation_attempt: request.mutation_attempt(),
110            observation_attempt: request.observation_attempt(),
111            challenge,
112        },
113        &response.input().evidence,
114    )?;
115    if let IcSnapshotUploadAttribution::Applied { snapshot_id, .. } = &settlement.attribution {
116        request.mutation().validate_data_destination(snapshot_id)?;
117    }
118    let (outcome, allocated_snapshot) = admitted_outcome(
119        inventory,
120        &candidates,
121        InventoryAttribution::from(&settlement.attribution),
122    )
123    .map_err(|()| IcSnapshotUploadSettlementError::NotNewCandidate)?;
124    Ok(IcSnapshotUploadSettlementView {
125        observation,
126        baseline,
127        settlement,
128        allocated_snapshot,
129        outcome,
130    })
131}
132
133/// Local admission denial; original spending and obligations remain retained.
134#[derive(Debug, Error)]
135pub enum IcSnapshotUploadSettlementError {
136    /// Existing exact reservation, claim or bounded wire admission failed.
137    #[error(transparent)]
138    Observation(#[from] IcObservationAssociationError),
139    /// Existing canonical closed-baseline comparison failed.
140    #[error(transparent)]
141    Baseline(#[from] SnapshotInventoryDeltaError),
142    /// Existing raw destination bounds or source-ID exclusion failed.
143    #[error(transparent)]
144    Destination(#[from] IcSnapshotUploadError),
145    /// A status projection cannot substitute for the original list.
146    #[error("metadata settlement requires snapshot inventory")]
147    WrongInventory,
148    /// Exact original settlement identity or retained evidence differs.
149    #[error(transparent)]
150    Identity(#[from] InventorySettlementMismatch),
151    /// The attributed ID is not a new candidate in the exact current inventory.
152    #[error("metadata settlement ID is not a new candidate")]
153    NotNewCandidate,
154}
155
156#[cfg(test)]
157mod tests;