OXYGENHUB
# fd2d4822f7f3 oxygenflow / crates / flow-core / src / canonical.rs
3.2 KiB · Rust · last changed OxygenFlow M0: pipeline parser and canonical job graph, receipts, the runner API types, flow-init, the Firecracker tier, flow check, run, rootfs import and runner 1 month ago source blame history raw
1
//! The canonical job graph (spec §3.2) and object hashes. Pipelines,
2
//! jobs and receipts are encoded with Oxygen's deterministic CBOR profile
3
//! (Oxygen spec §5.1: unsigned integer map keys in order) and hashed the
4
//! way Oxygen hashes objects: `BLAKE3("oxy1 " || type || " " || len || 0x00 || body)`.
5
6
use crate::pipeline::{Job, Pipeline, Sandbox};
7
use oxy_core::cbor::{encode, Value};
8
9
/// A 32-byte object reference, shown as hex.
10
pub type Hash = [u8; 32];
11
12
/// Oxygen's object hash construction for a Flow object type.
13
pub fn object_hash(ty: &str, body: &[u8]) -> Hash {
14
    let mut h = blake3::Hasher::new();
15
    h.update(format!("oxy1 {ty} {}\0", body.len()).as_bytes());
16
    h.update(body);
17
    *h.finalize().as_bytes()
18
}
19
20
fn text(s: &str) -> Value {
21
    Value::Text(s.to_string())
22
}
23
24
fn texts(v: &[String]) -> Value {
25
    Value::Array(v.iter().map(|s| text(s)).collect())
26
}
27
28
fn hex32(s: &str) -> Value {
29
    Value::Bytes(hex::decode(s).unwrap_or_default())
30
}
31
32
pub fn sandbox_value(s: &Sandbox) -> Value {
33
    Value::map([
34
        (0, text(&s.name)),
35
        (1, text(s.runtime.as_str())),
36
        (2, hex32(&s.rootfs)),
37
        (3, Value::Uint(s.memory_mib as u64)),
38
        (4, Value::Uint(s.cpus as u64)),
39
        (5, text(s.network.as_str())),
40
    ])
41
}
42
43
pub fn job_value(j: &Job) -> Value {
44
    Value::map([
45
        (0, text(&j.name)),
46
        (1, text(&j.sandbox)),
47
        (2, texts(&j.scope)),
48
        (3, texts(&j.depends_on)),
49
        // Environment as sorted [name, value] pairs: the profile has integer map keys only.
50
        (4, Value::Array(j.env.iter().map(|(k, v)| Value::Array(vec![text(k), text(v)])).collect())),
51
        (5, texts(&j.exec)),
52
    ])
53
}
54
55
impl Pipeline {
56
    /// The canonical CBOR job graph: sandboxes and jobs in name order.
57
    pub fn canonical(&self) -> Vec<u8> {
58
        encode(&Value::map([
59
            (0, Value::Uint(self.schema_version as u64)),
60
            (1, Value::Array(self.sandboxes.values().map(sandbox_value).collect())),
61
            (2, Value::Array(self.jobs.values().map(job_value).collect())),
62
        ]))
63
    }
64
65
    /// `pipeline_hash` of receipts and provenance.
66
    pub fn hash(&self) -> Hash {
67
        object_hash("pipeline", &self.canonical())
68
    }
69
}
70
71
impl Job {
72
    /// `Hash(J)` of spec §6.2: the job with its sandbox, so a change to
73
    /// either is a different job.
74
    pub fn hash(&self, sandbox: &Sandbox) -> Hash {
75
        object_hash("job", &encode(&Value::Array(vec![job_value(self), sandbox_value(sandbox)])))
76
    }
77
}
78
79
/// BLAKE3 tree hash of the files a scope selects (spec §6.2): sorted
80
/// `(path, content hash)` pairs. An empty scope selects every file.
81
pub fn scope_tree_hash<'a>(scope: &[String], files: impl IntoIterator<Item = (&'a str, Hash)>) -> Hash {
82
    let set = {
83
        let mut b = globset::GlobSetBuilder::new();
84
        for g in scope {
85
            if let Ok(g) = globset::Glob::new(g) {
86
                b.add(g);
87
            }
88
        }
89
        b.build().unwrap_or_else(|_| globset::GlobSet::empty())
90
    };
91
    let mut picked: Vec<(&str, Hash)> = files.into_iter().filter(|(p, _)| scope.is_empty() || set.is_match(p)).collect();
92
    picked.sort();
93
    let body = encode(&Value::Array(picked.iter().map(|(p, h)| Value::Array(vec![text(p), Value::Bytes(h.to_vec())])).collect()));
94
    object_hash("scope", &body)
95
}