ic_backup/policy/execution_settlement/
mod.rs1use crate::{
4 model::{
5 attempt_journal::AttemptJournalRecord, execution_settlement::ExecutionSettlementRecord,
6 operation_plan::OperationPlanRecord,
7 },
8 policy::execution_progress::{
9 self, ExecutionProgressError, ExecutionProgressView, OperationProgressState,
10 },
11};
12use thiserror::Error;
13
14pub fn validate(
26 plan: &OperationPlanRecord,
27 journals: &[&AttemptJournalRecord],
28 settlement: &ExecutionSettlementRecord,
29) -> Result<ExecutionProgressView, ExecutionSettlementPolicyError> {
30 validate_journals(plan, journals.iter().copied(), settlement)
31}
32
33pub(crate) fn validate_journals<'a>(
34 plan: &OperationPlanRecord,
35 journals: impl ExactSizeIterator<Item = &'a AttemptJournalRecord>,
36 settlement: &ExecutionSettlementRecord,
37) -> Result<ExecutionProgressView, ExecutionSettlementPolicyError> {
38 if &plan.digest() != settlement.plan_intent() {
39 return Err(ExecutionSettlementPolicyError::IntentMismatch);
40 }
41 let (view, actual) = execution_progress::progress_with_journals(plan, journals)?;
42 if let Some(operation) = view
43 .operations
44 .iter()
45 .find(|operation| operation.state != OperationProgressState::Applied)
46 {
47 return Err(ExecutionSettlementPolicyError::UnsettledOperation(
48 operation.operation_sequence,
49 ));
50 }
51 if settlement.journals().len() != plan.operations().len() {
52 return Err(ExecutionSettlementPolicyError::JournalSetMismatch);
53 }
54 for (row, operation) in settlement.journals().iter().zip(plan.operations()) {
55 if row.operation_sequence() != operation.operation_sequence() {
56 return Err(ExecutionSettlementPolicyError::JournalSetMismatch);
57 }
58 let journal = actual
59 .get(&row.operation_sequence())
60 .ok_or(ExecutionSettlementPolicyError::JournalSetMismatch)?;
61 if &journal.digest() != row.history() {
62 return Err(ExecutionSettlementPolicyError::HistoryMismatch(
63 row.operation_sequence(),
64 ));
65 }
66 }
67 Ok(view)
68}
69#[derive(Debug, Eq, Error, PartialEq)]
71pub enum ExecutionSettlementPolicyError {
72 #[error("execution settlement original plan mismatch")]
74 IntentMismatch,
75 #[error(transparent)]
77 Progress(#[from] ExecutionProgressError),
78 #[error("execution settlement operation {0} is not applied")]
80 UnsettledOperation(u64),
81 #[error("execution settlement journal set mismatch")]
83 JournalSetMismatch,
84 #[error("execution settlement journal {0} history mismatch")]
86 HistoryMismatch(u64),
87}
88#[cfg(test)]
89mod tests;