Skip to main content

ic_backup/policy/response_identity/
mod.rs

1//! Ordered passive update-response identity; reservation and decoding stay with callers.
2
3use crate::model::{
4    artifacts::ArtifactChecksumRecord, ic_update_response::IcUpdateResponseInput,
5    operation_plan::PlanContextRecord,
6};
7
8pub(super) struct ResponseIdentity<'a> {
9    pub authority: &'a ArtifactChecksumRecord,
10    pub mutation_attempt: u32,
11    pub context: &'a PlanContextRecord,
12    pub target: &'a str,
13}
14
15impl<'a> From<&'a IcUpdateResponseInput> for ResponseIdentity<'a> {
16    fn from(input: &'a IcUpdateResponseInput) -> Self {
17        Self {
18            authority: &input.authority,
19            mutation_attempt: input.mutation_attempt,
20            context: &input.context,
21            target: &input.target,
22        }
23    }
24}
25
26impl ResponseIdentity<'_> {
27    pub fn compare(&self, expected: &Self) -> Result<(), ResponseIdentityMismatch> {
28        if self.authority != expected.authority {
29            return Err(ResponseIdentityMismatch::Authority);
30        }
31        if self.mutation_attempt != expected.mutation_attempt {
32            return Err(ResponseIdentityMismatch::Attempt);
33        }
34        if self.context != expected.context {
35            return Err(ResponseIdentityMismatch::Context);
36        }
37        if self.target != expected.target {
38            return Err(ResponseIdentityMismatch::Target);
39        }
40        Ok(())
41    }
42}
43
44/// Passive response claims differ from the original reserved update identity.
45#[derive(Debug, thiserror::Error, Eq, PartialEq)]
46pub enum ResponseIdentityMismatch {
47    /// Full original operation authority differs.
48    #[error("response authority differs")]
49    Authority,
50    /// The already reserved mutation attempt differs.
51    #[error("response attempt differs")]
52    Attempt,
53    /// Claimed network, caller or release differs.
54    #[error("response context differs")]
55    Context,
56    /// Claimed canonical routing target differs.
57    #[error("response target differs")]
58    Target,
59}