use crate::layer2::sync::codec::unframe_record;
use crate::layer2::sync::petri::evaluate_lifecycle_guard;
use crate::layer2::sync::service::{kind_to_lifecycle, normalize_qualified_hash, MCardSyncEngine};
use crate::layer2::sync::types::{MCardRecord, RecordFrame, SyncOutboxEntry, TransferAttempt};
use std::collections::BTreeMap;
impl MCardSyncEngine {
pub(crate) fn is_durably_queued(&self, entry: &SyncOutboxEntry) -> bool {
let Some(port) = self.outbox_port.as_ref() else { return true };
let persisted = match port.load_outbox() {
Ok(entries) => entries,
Err(_) => return false,
};
persisted.iter().any(|candidate| {
candidate.delivery_id == entry.delivery_id
&& candidate.transfer_id == entry.transfer_id
&& candidate.status == entry.status
&& candidate.records == entry.records
&& candidate.transitions == entry.transitions
})
}
pub(crate) fn validate_inbound_attempt(
&self,
attempt: &TransferAttempt,
records: &[RecordFrame],
) -> Result<Vec<(String, MCardRecord, String)>, (String, String)> {
if attempt.target_node_id != self.node_id {
return Err((
"guard-rejected".to_string(),
format!("Target node mismatch: {}", attempt.target_node_id),
));
}
let mut verified: Vec<(String, MCardRecord, String)> = Vec::new();
for frame in records {
let (rec, q_hash) = unframe_record(frame, "blake3")?;
let bare = q_hash.split(':').nth(1).unwrap_or("").to_string();
verified.push((bare, rec, q_hash));
}
let mut expected: Vec<String> =
attempt.record_hashes.iter().map(|h| normalize_qualified_hash(h)).collect();
expected.sort();
let mut actual: Vec<String> =
verified.iter().map(|(_, _, q)| q.clone()).collect();
actual.sort();
if expected != actual {
return Err((
"declared-mismatch".to_string(),
"record hash set does not match supplied frames".to_string(),
));
}
let mut simulated: BTreeMap<String, BTreeMap<String, i64>> = BTreeMap::new();
for transition in &attempt.transitions {
let current = simulated
.get(&transition.handle)
.cloned()
.or_else(|| self.handle_markings.get(&transition.handle).cloned())
.unwrap_or_else(Self::default_lifecycle_marking);
let Some(lifecycle_id) = kind_to_lifecycle(&transition.kind) else {
return Err((
"guard-rejected".to_string(),
format!("Unknown lifecycle transition for kind {}", transition.kind),
));
};
let gate = evaluate_lifecycle_guard(¤t, lifecycle_id, Some(transition));
if !gate.enabled {
return Err((
gate.error_code.unwrap_or_else(|| "guard-rejected".to_string()),
gate.reason.unwrap_or_else(|| "transition rejected".to_string()),
));
}
simulated.insert(transition.handle.clone(), gate.post_marking);
if transition.kind == "RELOCATE" || transition.kind == "FORK" {
if let Some(to_handle) = &transition.to_handle {
if to_handle != &transition.handle {
let target = simulated
.get(to_handle)
.cloned()
.or_else(|| self.handle_markings.get(to_handle).cloned())
.unwrap_or_else(Self::default_lifecycle_marking);
let target_tid = if transition.kind == "FORK" { "t_fork_in" } else { "t_relocate_in" };
let target_gate =
evaluate_lifecycle_guard(&target, target_tid, Some(transition));
if !target_gate.enabled {
return Err((
target_gate
.error_code
.unwrap_or_else(|| "guard-rejected".to_string()),
target_gate
.reason
.unwrap_or_else(|| "target handle rejected".to_string()),
));
}
simulated.insert(to_handle.clone(), target_gate.post_marking);
}
}
}
}
Ok(verified)
}
}