datalog-structural-model 3.2.2

Typed Datalog rule IR, validation, and stratification
Documentation
//! Grounded value identities preserve structural boundaries independently of transport.

use datalog_structural_model::{
    ConstantSymbol, ContractDigest, GroundAtom, GroundTerm, GroundTermDigest, PredicateSymbol,
    Program, ProgramDigest,
};

fn constant(value: &str) -> GroundTerm {
    GroundTerm::constant(ConstantSymbol::new(value))
}

fn quoted(predicate: &str, args: Vec<GroundTerm>) -> GroundTerm {
    GroundTerm::try_triple_term(GroundAtom {
        predicate: PredicateSymbol::new(predicate),
        args,
    })
    .unwrap()
}

#[test]
fn grounded_identity_frames_argument_boundaries() {
    let one = quoted("p", vec![constant("a\u{1f}b")]);
    let two = quoted("p", vec![constant("a"), constant("b")]);
    assert_ne!(one.digest(), two.digest());
    assert_ne!(constant("ab").digest(), constant("a\0b").digest());
}

#[test]
fn grounded_identity_preserves_kinds_predicates_order_and_nesting() {
    let value = quoted("p", vec![constant("a"), constant("b")]);
    let alternatives = [
        constant(&value.to_string()),
        quoted("q", vec![constant("a"), constant("b")]),
        quoted("p", vec![constant("b"), constant("a")]),
        quoted("p", vec![constant("a")]),
        quoted("p", vec![]),
        quoted("p", vec![quoted("p", vec![constant("a"), constant("b")])]),
    ];
    for other in alternatives {
        assert_ne!(value.digest(), other.digest());
    }
    assert_eq!(value.digest(), value.clone().digest());
    assert_ne!(constant("é").digest(), constant("e\u{301}").digest());
}

#[derive(Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct Identities {
    ground: GroundTermDigest,
    program: ProgramDigest,
    contract: ContractDigest,
}

#[derive(Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct Document {
    term: GroundTerm,
    identities: Identities,
}

#[test]
fn grounded_identity_is_independent_of_nested_json_and_binary_transport() {
    let term = quoted(
        "outer",
        vec![constant("a\0b"), quoted("inner", vec![constant("é")])],
    );
    let document = Document {
        identities: Identities {
            ground: term.digest(),
            program: ProgramDigest::from([7; 32]),
            contract: ContractDigest::from([9; 32]),
        },
        term,
    };
    let json: Document = serde_json::from_slice(&serde_json::to_vec(&document).unwrap()).unwrap();
    let binary: Document =
        postcard::from_bytes(&postcard::to_allocvec(&document).unwrap()).unwrap();
    assert_eq!(document, json);
    assert_eq!(document, binary);
    assert_eq!(json.term.digest(), json.identities.ground);
    assert_eq!(binary.term.digest(), binary.identities.ground);
}

#[test]
fn digest_transport_rejects_wrong_byte_counts_and_values() {
    let short = serde_json::to_vec(&[0_u8; 31]).unwrap();
    let out_of_range = serde_json::to_vec(&[256_u16; 32]).unwrap();
    assert!(serde_json::from_slice::<GroundTermDigest>(&short).is_err());
    assert!(serde_json::from_slice::<GroundTermDigest>(&out_of_range).is_err());
    assert!(postcard::from_bytes::<GroundTermDigest>(&[0; 31]).is_err());
}

#[test]
fn grounded_identity_has_typed_bytes_and_strict_hex_text() {
    let digest: GroundTermDigest = constant("a").digest();
    assert_eq!(digest.as_bytes().len(), 32);
    let text = digest.to_string();
    assert_eq!(text.len(), 64);
    assert_eq!(text.parse::<GroundTermDigest>().unwrap(), digest);
    assert!(text.to_uppercase().parse::<GroundTermDigest>().is_err());
    let empty_program = Program::try_new(vec![]).unwrap();
    assert_ne!(digest.as_bytes(), empty_program.digest().as_bytes());
}

/// Independently hashed explicit v1 preimages; these values are immutable.
#[test]
fn grounded_identity_v1_vectors_are_frozen() {
    let vectors = [
        (
            constant(""),
            "4e39c36bdb88972d2399e238ba02e94d7ea941adb0512e22cc3c3b91d84ac683",
        ),
        (
            constant("a"),
            "3fd628e0de231293b54091ae3a5aaa56cb18fda254bf3859184d724e4af7bc44",
        ),
        (
            quoted("p", vec![constant("a"), constant("b")]),
            "8a260c1ff2803ebeb67f10a7aa220725cc1a4fec5106ab09974e519d14be8cc7",
        ),
    ];
    for (term, expected) in vectors {
        assert_eq!(term.digest().to_string(), expected);
    }
}