use super::{
IcObservationAssociationError, IcObservationReplyView, IcObservationResponseView,
validate_response,
};
use crate::{
model::{
artifacts::ArtifactChecksumRecord,
attempt_journal::{AttemptJournalRecord, ObservationOutcomeRecord},
ic_observation::{
IcCaptureAttribution, IcCaptureSettlement, IcObservationRequest, IcObservationResponse,
},
ic_request::IcManagementMethodRecord as Method,
ic_snapshot_reply::{IcSnapshotInfo, IcSnapshotReply},
},
policy::{
inventory_settlement::{
InventorySettlementIdentity, InventorySettlementMismatch, new_candidate_index,
},
snapshot_inventory_delta::{SnapshotInventoryDeltaError, compare_inventories},
},
};
use thiserror::Error;
#[derive(Debug)]
pub struct IcCaptureSettlementView<'a> {
observation: IcObservationResponseView<'a>,
baseline: &'a IcSnapshotReply<'a>,
settlement: &'a IcCaptureSettlement,
captured_snapshot: Option<usize>,
outcome: ObservationOutcomeRecord,
}
impl<'a> IcCaptureSettlementView<'a> {
#[must_use]
pub const fn outcome(&self) -> ObservationOutcomeRecord {
self.outcome
}
#[must_use]
pub const fn baseline(&self) -> &'a IcSnapshotReply<'a> {
self.baseline
}
#[must_use]
pub const fn observation(&self) -> &IcObservationResponseView<'a> {
&self.observation
}
#[must_use]
pub const fn settlement(&self) -> &'a IcCaptureSettlement {
self.settlement
}
#[must_use]
pub fn captured_snapshot(&self) -> Option<&IcSnapshotInfo> {
match self.observation.reply() {
IcObservationReplyView::Inventory(reply) => self
.captured_snapshot
.map(|index| &reply.snapshots()[index]),
IcObservationReplyView::Status(_) => None,
}
}
}
pub fn validate_capture_settlement<'a>(
request: &IcObservationRequest<'a>,
journal: &AttemptJournalRecord,
baseline: &'a IcSnapshotReply<'a>,
response: &'a IcObservationResponse,
challenge: &ArtifactChecksumRecord,
settlement: &'a IcCaptureSettlement,
) -> Result<IcCaptureSettlementView<'a>, IcCaptureSettlementError> {
if request.mutation().method() != Method::TakeCanisterSnapshot
|| request.payload().method() != Method::ListCanisterSnapshots
{
return Err(IcCaptureSettlementError::UnsupportedMethod);
}
let observation = validate_response(request, journal, response)?;
let IcObservationReplyView::Inventory(inventory) = observation.reply() else {
return Err(IcCaptureSettlementError::UnsupportedMethod);
};
let candidates = compare_inventories(request.mutation().target(), baseline, inventory)?;
let authority = request.authority().digest();
let baseline_digest = baseline.digest();
let inventory_digest = inventory.digest();
InventorySettlementIdentity {
authority: &settlement.authority,
mutation_attempt: settlement.mutation_attempt,
observation_attempt: settlement.observation_attempt,
challenge: &settlement.challenge,
baseline: &settlement.baseline,
inventory: &settlement.inventory,
observation_evidence: &settlement.observation_evidence,
}
.compare(&InventorySettlementIdentity {
authority: &authority,
mutation_attempt: request.mutation_attempt(),
observation_attempt: request.observation_attempt(),
challenge,
baseline: &baseline_digest,
inventory: &inventory_digest,
observation_evidence: &response.input().evidence,
})?;
let (outcome, captured_snapshot) = match &settlement.attribution {
IcCaptureAttribution::Applied { snapshot_id, .. } => {
let index = new_candidate_index(inventory, &candidates, snapshot_id)
.ok_or(IcCaptureSettlementError::NotNewCandidate)?;
(ObservationOutcomeRecord::Applied, Some(index))
}
IcCaptureAttribution::NotApplied { .. } => (ObservationOutcomeRecord::NotApplied, None),
IcCaptureAttribution::Unresolved { .. } => (ObservationOutcomeRecord::Uncertain, None),
};
Ok(IcCaptureSettlementView {
observation,
baseline,
settlement,
captured_snapshot,
outcome,
})
}
#[derive(Debug, Error)]
pub enum IcCaptureSettlementError {
#[error("capture settlement requires original capture and list observation")]
UnsupportedMethod,
#[error(transparent)]
Observation(#[from] IcObservationAssociationError),
#[error(transparent)]
Baseline(#[from] SnapshotInventoryDeltaError),
#[error("capture settlement original authority differs")]
AuthorityMismatch,
#[error("capture settlement attempt identities differ")]
AttemptMismatch,
#[error("capture settlement challenge differs")]
ChallengeMismatch,
#[error("capture settlement original baseline differs")]
BaselineMismatch,
#[error("capture settlement current inventory differs")]
InventoryMismatch,
#[error("capture settlement observation evidence differs")]
ObservationEvidenceMismatch,
#[error("capture settlement ID is not a new candidate")]
NotNewCandidate,
}
impl From<InventorySettlementMismatch> for IcCaptureSettlementError {
fn from(value: InventorySettlementMismatch) -> Self {
match value {
InventorySettlementMismatch::Authority => Self::AuthorityMismatch,
InventorySettlementMismatch::Attempt => Self::AttemptMismatch,
InventorySettlementMismatch::Challenge => Self::ChallengeMismatch,
InventorySettlementMismatch::Baseline => Self::BaselineMismatch,
InventorySettlementMismatch::Inventory => Self::InventoryMismatch,
InventorySettlementMismatch::ObservationEvidence => Self::ObservationEvidenceMismatch,
}
}
}
#[cfg(test)]
mod tests;