use super::response_identity::{ResponseIdentity, ResponseIdentityMismatch};
use crate::model::{
attempt_journal::AttemptJournalRecord,
ic_lifecycle_reply::{IcLifecycleReply, IcLifecycleReplyError},
ic_mutation::{IcMutationAcknowledgement, IcMutationRequest, IcMutationRequestError},
ic_request::IcManagementMethodRecord,
ic_snapshot_reply::{IcSnapshotReply, IcSnapshotReplyError},
};
use thiserror::Error;
#[derive(Debug)]
pub enum IcMutationReplyView<'a> {
Capture(IcSnapshotReply<'a>),
Lifecycle(IcLifecycleReply<'a>),
}
#[derive(Debug)]
pub struct IcMutationAcknowledgementView<'a> {
acknowledgement: &'a IcMutationAcknowledgement,
reply: IcMutationReplyView<'a>,
}
impl<'a> IcMutationAcknowledgementView<'a> {
#[must_use]
pub const fn acknowledgement(&self) -> &'a IcMutationAcknowledgement {
self.acknowledgement
}
#[must_use]
pub const fn reply(&self) -> &IcMutationReplyView<'a> {
&self.reply
}
}
pub fn validate_acknowledgement<'a>(
request: &IcMutationRequest<'a>,
journal: &AttemptJournalRecord,
acknowledgement: &'a IcMutationAcknowledgement,
) -> Result<IcMutationAcknowledgementView<'a>, IcMutationAssociationError> {
request.validate_journal(journal)?;
let input = acknowledgement.input();
ResponseIdentity {
authority: &input.authority,
mutation_attempt: input.mutation_attempt,
context: &input.context,
target: &input.target,
}
.compare(&ResponseIdentity {
authority: &request.authority().digest(),
mutation_attempt: request.mutation_attempt(),
context: request.plan().context(),
target: request.payload().target(),
})
.map_err(|mismatch| match mismatch {
ResponseIdentityMismatch::Authority => IcMutationAssociationError::AuthorityMismatch,
ResponseIdentityMismatch::Attempt => IcMutationAssociationError::AttemptMismatch,
ResponseIdentityMismatch::Context => IcMutationAssociationError::ContextMismatch,
ResponseIdentityMismatch::Target => IcMutationAssociationError::TargetMismatch,
})?;
let reply = match request.payload().method() {
IcManagementMethodRecord::TakeCanisterSnapshot => {
IcMutationReplyView::Capture(IcSnapshotReply::decode(request.payload(), &input.reply)?)
}
_ => IcMutationReplyView::Lifecycle(IcLifecycleReply::decode(
request.payload(),
&input.reply,
)?),
};
Ok(IcMutationAcknowledgementView {
acknowledgement,
reply,
})
}
#[derive(Debug, Error)]
pub enum IcMutationAssociationError {
#[error(transparent)]
Reservation(#[from] IcMutationRequestError),
#[error("IC mutation acknowledgement authority mismatch")]
AuthorityMismatch,
#[error("IC mutation acknowledgement attempt mismatch")]
AttemptMismatch,
#[error("IC mutation acknowledgement context mismatch")]
ContextMismatch,
#[error("IC mutation acknowledgement target mismatch")]
TargetMismatch,
#[error(transparent)]
Snapshot(#[from] IcSnapshotReplyError),
#[error(transparent)]
Lifecycle(#[from] IcLifecycleReplyError),
}
#[cfg(test)]
mod tests;