minerva 0.2.0

Causal ordering for distributed systems
//! The cross-repository boundary fixture from Alma TRANSPORT-17.

use super::*;

const ALMA_EXPORT: &str = include_str!("alma-transport-17-boundary.fixture");
const ALMA_SOURCE: &str = concat!(
    "# alma_commit=806d4ecf\n",
    "# alma_fixture_blob_sha256=7149e5949f6f935802e460e6973e26f13eb2724b4ea3001b4358125df9262c30\n",
);
const EXPECTED_ROWS: [(&str, [u8; 32]); 5] = [
    (
        "P",
        [
            0xe5, 0x6d, 0x4b, 0xf5, 0x19, 0xcf, 0x09, 0xaf, 0xa3, 0xc8, 0x60, 0xc2, 0xaf, 0xb3,
            0x54, 0x82, 0x33, 0x0d, 0xf4, 0x30, 0x8f, 0xb1, 0x14, 0xee, 0xf4, 0x86, 0xcd, 0xef,
            0x70, 0xc2, 0x8c, 0x45,
        ],
    ),
    (
        "S",
        [
            0xb8, 0xaf, 0xba, 0x63, 0x1b, 0x25, 0xfa, 0x4d, 0x34, 0x8c, 0xa8, 0x51, 0xdd, 0xcb,
            0xd0, 0xce, 0x51, 0x4c, 0x16, 0xd8, 0xdc, 0xb9, 0x8e, 0x0f, 0xc3, 0x1f, 0x9d, 0xae,
            0xe0, 0xd4, 0xbf, 0x8d,
        ],
    ),
    (
        "M",
        [
            0x50, 0x5a, 0x97, 0x14, 0xbf, 0x77, 0x96, 0x33, 0x97, 0x07, 0xb3, 0x99, 0x69, 0x71,
            0x05, 0x8b, 0x36, 0x3d, 0x8b, 0xee, 0xb0, 0xf5, 0x3d, 0x4f, 0x08, 0xba, 0xd7, 0x14,
            0x04, 0xc1, 0x22, 0x3d,
        ],
    ),
    (
        "A",
        [
            0x52, 0xaa, 0xc6, 0x1c, 0xd5, 0x3b, 0xb9, 0x0c, 0xe6, 0xcf, 0xf0, 0x66, 0x3b, 0x16,
            0x30, 0xd3, 0xd7, 0x56, 0xd3, 0x5b, 0x13, 0x9b, 0xc3, 0x3c, 0xd2, 0xd2, 0xc1, 0x18,
            0x95, 0xe2, 0x58, 0x64,
        ],
    ),
    (
        "N",
        [
            0xee, 0xbb, 0xc0, 0xa5, 0xea, 0xbe, 0x27, 0x57, 0x86, 0xb6, 0x06, 0xcd, 0x6d, 0x6d,
            0xc4, 0x5d, 0xe6, 0x06, 0x84, 0xf8, 0xf4, 0x24, 0x35, 0x1c, 0x66, 0x83, 0x6b, 0xfb,
            0xca, 0x79, 0x87, 0x52,
        ],
    ),
];

fn digest(hex: &str) -> [u8; 32] {
    assert_eq!(hex.len(), 64, "a BLAKE3-256 row is 32 bytes");
    let mut digest = [0; 32];
    for (byte, pair) in digest.iter_mut().zip(hex.as_bytes().chunks_exact(2)) {
        let pair = core::str::from_utf8(pair).expect("the fixture is ASCII");
        *byte = u8::from_str_radix(pair, 16).expect("the fixture is lowercase hex");
    }
    digest
}

fn exported_rows() -> Vec<(&'static str, [u8; 32])> {
    ALMA_EXPORT
        .lines()
        .filter(|line| !line.starts_with('#'))
        .map(|line| {
            let (name, value) = line.split_once('=').expect("a fixture row is named");
            (name, digest(value))
        })
        .collect()
}

/// Pins Alma's exported `P`, `S`, `M`, `A`, and `N` rows at the commit that
/// admitted kinds 8 and 9 to Session Hello v3. Minerva proves its own wire
/// duty against an independently assembled frame: opaque `N` occupies the
/// exact lineage-proof offset and survives decode byte-for-byte.
#[test]
fn test_alma_boundary_export_and_lineage_frame_are_pinned() {
    assert!(ALMA_EXPORT.starts_with(ALMA_SOURCE));
    assert_eq!(exported_rows(), EXPECTED_ROWS);

    let (_, sealed) = displaced_window();
    let seal = SealRecord::from_sealed(&sealed);
    let node = EXPECTED_ROWS[4].1;
    let proof = LineageProofRecord::try_new(vec![LineageProofEntry::new(seal.clone(), node)])
        .expect("one retained generation is consecutive");

    let mut expected = vec![0x01, 0x00, 0x00, 0x00, 0x01];
    expected.extend_from_slice(&node);
    expected.extend_from_slice(&seal.to_bytes());
    assert_eq!(&expected[5..37], &node);
    assert_eq!(proof.to_bytes(), expected);

    let decoded = LineageProofRecord::from_bytes(&expected)
        .expect("the independently assembled frame is canonical");
    assert_eq!(decoded.entries()[0].node(), node);
    assert_eq!(decoded.entries()[0].seal(), &seal);
}