| 237 | } |
| 238 | |
| 239 | fn append_effect_receipt(&mut self, receipt: &EffectReceipt) -> PristineResult<()> { |
| 240 | let operation = OperationTxnT::get_operation(self, receipt.payload().operation)? |
| 241 | .ok_or_else(|| PristineError::OperationNotFound { |
| 242 | id: receipt.payload().operation.to_string(), |
| 243 | })?; |
| 244 | if let Some(ordinal) = receipt.payload().effect_ordinal { |
| 245 | let effect = operation |
| 246 | .payload() |
| 247 | .delta |
| 248 | .effects |
| 249 | .get(ordinal as usize) |
| 250 | .filter(|effect| effect.ordinal == ordinal) |
| 251 | .ok_or_else(|| PristineError::EffectPlanNotFound { |
| 252 | operation: operation.id().to_string(), |
| 253 | ordinal, |
| 254 | })?; |
| 255 | let successful_observation = match receipt.payload().kind { |
| 256 | EffectReceiptKind::Applied | EffectReceiptKind::RolledBack => { |
| 257 | receipt.payload().observed_old.as_ref() == Some(&effect.expected_old) |
| 258 | && receipt.payload().observed_new.as_ref() == Some(&effect.expected_new) |
| 259 | } |
| 260 | EffectReceiptKind::Recovered => { |
| 261 | receipt.payload().observed_old.as_ref() == Some(&effect.expected_new) |
| 262 | && receipt.payload().observed_new.as_ref() == Some(&effect.expected_new) |
| 263 | } |
| 264 | EffectReceiptKind::LeaseRejected => true, |
| 265 | EffectReceiptKind::Verified => false, |
| 266 | }; |
| 267 | if !successful_observation { |
| 268 | return Err(PristineError::Inconsistent { |
| 269 | message: format!( |
| 270 | "effect receipt {} does not match operation {} effect {} leases", |
| 271 | receipt.id(), |
| 272 | operation.id(), |
| 273 | ordinal |
| 274 | ), |
| 275 | }); |
| 276 | } |
| 277 | } else if receipt.payload().kind != EffectReceiptKind::Verified { |
| 278 | return Err(PristineError::EffectPlanNotFound { |
| 279 | operation: operation.id().to_string(), |
| 280 | ordinal: u32::MAX, |
| 281 | }); |
| 282 | } else { |
| 283 | let existing = OperationTxnT::get_effect_receipts(self, operation.id())?; |
| 284 | let completed: std::collections::BTreeSet<u32> = existing |
| 285 | .iter() |
| 286 | .filter_map(|existing| match existing.payload().kind { |
| 287 | EffectReceiptKind::Applied |
| 288 | | EffectReceiptKind::RolledBack |
| 289 | | EffectReceiptKind::Recovered => existing.payload().effect_ordinal, |
| 290 | EffectReceiptKind::Verified | EffectReceiptKind::LeaseRejected => None, |
| 291 | }) |
| 292 | .collect(); |
| 293 | if let Some(missing) = operation |
| 294 | .payload() |
| 295 | .delta |
| 296 | .effects |