use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use crate::transfer::{Transfer, TransferKind};
pub const TRANSFER_REGISTRY_SCHEMA: &str = "vela.transfer-registry.v0.1";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TransferLane {
Certified,
TargetChecked,
Exploratory,
}
pub fn lane_of(t: &Transfer) -> TransferLane {
match t.homomorphism.kind {
TransferKind::LeanHomomorphism | TransferKind::FrozenVerifier => TransferLane::Certified,
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransferRecord {
pub transfer_id: String,
pub lane: TransferLane,
pub source_claim: String,
pub target_claim: String,
pub source_type: String,
pub target_type: String,
pub domain_pair: String,
pub kind: TransferKind,
pub map_decl: String,
#[serde(default, skip_serializing_if = "String::is_empty")]
pub theorem_verification: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub theorem_id: Option<u32>,
pub structural_ok: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub structural_error: Option<String>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct LaneCounts {
pub certified: usize,
pub target_checked: usize,
pub exploratory: usize,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransferRegistry {
pub schema: String,
pub total: usize,
pub structural_ok: usize,
pub lanes: LaneCounts,
pub by_domain_pair: BTreeMap<String, Vec<String>>,
pub records: Vec<TransferRecord>,
}
fn record_of(t: &Transfer) -> TransferRecord {
let h = &t.homomorphism;
let (structural_ok, structural_error) = match t.verify() {
Ok(()) => (true, None),
Err(e) => (false, Some(e)),
};
TransferRecord {
transfer_id: t.transfer_id.clone(),
lane: lane_of(t),
source_claim: t.source_claim.clone(),
target_claim: t.target_claim.clone(),
source_type: h.source_type.clone(),
target_type: h.target_type.clone(),
domain_pair: format!("{} → {}", h.source_type, h.target_type),
kind: h.kind.clone(),
map_decl: h.map_decl.clone(),
theorem_verification: h.theorem_verification.clone(),
theorem_id: h.theorem_id,
structural_ok,
structural_error,
}
}
pub fn build_registry(transfers: &[Transfer]) -> TransferRegistry {
let mut by_id: BTreeMap<String, TransferRecord> = BTreeMap::new();
for t in transfers {
by_id.insert(t.transfer_id.clone(), record_of(t));
}
let mut records: Vec<TransferRecord> = by_id.into_values().collect();
records.sort_by(|a, b| {
a.domain_pair
.cmp(&b.domain_pair)
.then(a.transfer_id.cmp(&b.transfer_id))
});
let mut lanes = LaneCounts::default();
let mut by_domain_pair: BTreeMap<String, Vec<String>> = BTreeMap::new();
let mut structural_ok = 0usize;
for r in &records {
match r.lane {
TransferLane::Certified => lanes.certified += 1,
TransferLane::TargetChecked => lanes.target_checked += 1,
TransferLane::Exploratory => lanes.exploratory += 1,
}
if r.structural_ok {
structural_ok += 1;
}
by_domain_pair
.entry(r.domain_pair.clone())
.or_default()
.push(r.transfer_id.clone());
}
TransferRegistry {
schema: TRANSFER_REGISTRY_SCHEMA.to_string(),
total: records.len(),
structural_ok,
lanes,
by_domain_pair,
records,
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::transfer::{HomomorphismDescriptor, TransferDraft};
use ed25519_dalek::SigningKey;
fn key() -> SigningKey {
SigningKey::from_bytes(&[7u8; 32])
}
fn transfer(src_type: &str, tgt_type: &str, src: &str, tgt: &str) -> Transfer {
let draft = TransferDraft {
source_claim: src.to_string(),
source_claim_digest: "0".repeat(16),
source_gate_status_claimed: String::new(),
source_attachments: vec![],
target_claim: tgt.to_string(),
target_premise_digest: "1".repeat(16),
target_attachments: vec![],
homomorphism: HomomorphismDescriptor {
kind: TransferKind::LeanHomomorphism,
map_decl: format!("Vela.Transfer{src_type}to{tgt_type}.map"),
source_type: src_type.to_string(),
target_type: tgt_type.to_string(),
theorem_verification: format!("vlv_{}", "a".repeat(16)),
theorem_id: Some(35),
},
provenance: Default::default(),
note: String::new(),
};
Transfer::build(draft, &key()).expect("build transfer")
}
#[test]
fn folds_lanes_pairs_and_structural_check() {
let a = transfer("cwc", "dna", "vat_cwc_1", "vfr_dna_1");
let b = transfer("sidon", "golomb", "vf_sidon_1", "vfr_golomb_1");
let reg = build_registry(&[a.clone(), b]);
assert_eq!(reg.total, 2);
assert_eq!(reg.lanes.certified, 2);
assert_eq!(reg.lanes.target_checked, 0);
assert_eq!(reg.structural_ok, 2, "freshly built transfers verify");
assert!(reg.by_domain_pair.contains_key("cwc → dna"));
assert!(reg.by_domain_pair.contains_key("sidon → golomb"));
assert_eq!(reg.records[0].domain_pair, "cwc → dna");
assert_eq!(reg.records[0].theorem_id, Some(35));
}
#[test]
fn dedupes_by_content_address() {
let a = transfer("cwc", "dna", "vat_cwc_1", "vfr_dna_1");
let reg = build_registry(&[a.clone(), a]);
assert_eq!(reg.total, 1, "same vtr_ id folds once");
}
#[test]
fn flags_tampered_transfer_without_failing_the_fold() {
let mut a = transfer("cwc", "dna", "vat_cwc_1", "vfr_dna_1");
a.source_claim = "vat_TAMPERED".to_string(); let reg = build_registry(&[a]);
assert_eq!(reg.total, 1);
assert_eq!(reg.structural_ok, 0);
assert!(reg.records[0].structural_error.is_some());
}
}