use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ReceiptKind {
Drv,
Nar,
Realisation,
BuildResult,
Shape(String),
}
crate::define_hash_newtype! {
#[derive(Copy)]
NodeId
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct MaterializationReceipt {
pub kind: ReceiptKind,
pub subject: [u8; 32],
pub emitter: NodeId,
pub emitted_at: u64,
pub evidence_hash: [u8; 32],
}
impl MaterializationReceipt {
#[must_use]
pub fn new(
kind: ReceiptKind,
subject: [u8; 32],
emitter: NodeId,
emitted_at: u64,
evidence_hash: [u8; 32],
) -> Self {
Self {
kind,
subject,
emitter,
emitted_at,
evidence_hash,
}
}
#[must_use]
pub fn id(&self) -> [u8; 32] {
let bytes = serde_json::to_vec(self).expect("receipt serializes");
*blake3::hash(&bytes).as_bytes()
}
#[must_use]
pub fn for_drv(
drv_hash: [u8; 32],
emitter: NodeId,
emitted_at: u64,
evidence_hash: [u8; 32],
) -> Self {
Self::new(
ReceiptKind::Drv,
drv_hash,
emitter,
emitted_at,
evidence_hash,
)
}
#[must_use]
pub fn for_nar(
nar_hash: [u8; 32],
emitter: NodeId,
emitted_at: u64,
evidence_hash: [u8; 32],
) -> Self {
Self::new(
ReceiptKind::Nar,
nar_hash,
emitter,
emitted_at,
evidence_hash,
)
}
#[must_use]
pub fn same_subject(&self, other: &Self) -> bool {
self.kind == other.kind && self.subject == other.subject
}
#[must_use]
pub fn agrees_with(&self, other: &Self) -> bool {
self.same_subject(other) && self.evidence_hash == other.evidence_hash
}
}
#[cfg(test)]
mod tests {
use super::*;
fn sample(emitter: NodeId, ts: u64) -> MaterializationReceipt {
MaterializationReceipt::for_drv([1u8; 32], emitter, ts, [2u8; 32])
}
#[test]
fn node_id_hex_is_64_chars() {
let n = NodeId::from_bytes(b"engenho");
assert_eq!(n.to_hex().len(), 64);
}
#[test]
fn node_id_display_matches_hex() {
let n = NodeId::from_bytes(b"x");
assert_eq!(format!("{n}"), n.to_hex());
}
#[test]
fn receipt_round_trips_via_serde() {
let r = sample(NodeId::from_bytes(b"a"), 100);
let bytes = serde_json::to_vec(&r).unwrap();
let back: MaterializationReceipt = serde_json::from_slice(&bytes).unwrap();
assert_eq!(back, r);
}
#[test]
fn receipt_id_is_deterministic() {
let r1 = sample(NodeId::from_bytes(b"a"), 100);
let r2 = sample(NodeId::from_bytes(b"a"), 100);
assert_eq!(r1.id(), r2.id());
}
#[test]
fn different_emitters_yield_different_ids() {
let r1 = sample(NodeId::from_bytes(b"a"), 100);
let r2 = sample(NodeId::from_bytes(b"b"), 100);
assert_ne!(r1.id(), r2.id());
}
#[test]
fn same_subject_ignores_emitter_and_timestamp() {
let r1 = sample(NodeId::from_bytes(b"a"), 100);
let r2 = sample(NodeId::from_bytes(b"b"), 200);
assert!(r1.same_subject(&r2));
}
#[test]
fn agrees_with_requires_matching_evidence() {
let r1 = sample(NodeId::from_bytes(b"a"), 100);
let mut r2 = sample(NodeId::from_bytes(b"b"), 100);
assert!(r1.agrees_with(&r2));
r2.evidence_hash[0] ^= 0xff;
assert!(!r1.agrees_with(&r2));
assert!(r1.same_subject(&r2));
}
#[test]
fn receipt_kinds_serialize_snake_case() {
let json = serde_json::to_string(&ReceiptKind::BuildResult).unwrap();
assert_eq!(json, "\"build_result\"");
let json = serde_json::to_string(&ReceiptKind::Shape("oci".into())).unwrap();
assert_eq!(json, "{\"shape\":\"oci\"}");
}
#[test]
fn for_drv_constructor_sets_kind() {
let r = MaterializationReceipt::for_drv([3u8; 32], NodeId::from_bytes(b"x"), 42, [4u8; 32]);
assert_eq!(r.kind, ReceiptKind::Drv);
assert_eq!(r.subject, [3u8; 32]);
assert_eq!(r.emitted_at, 42);
}
#[test]
fn for_nar_constructor_sets_kind() {
let r = MaterializationReceipt::for_nar([5u8; 32], NodeId::from_bytes(b"x"), 42, [6u8; 32]);
assert_eq!(r.kind, ReceiptKind::Nar);
}
#[test]
fn shape_receipt_carries_tag() {
let r = MaterializationReceipt::new(
ReceiptKind::Shape("nixos-closure".into()),
[0u8; 32],
NodeId::from_bytes(b"x"),
0,
[0u8; 32],
);
match r.kind {
ReceiptKind::Shape(tag) => assert_eq!(tag, "nixos-closure"),
_ => panic!("expected Shape"),
}
}
}