OXYGENHUB
# 50354f576ea6 oxygenflow / crates / flow-core / src / canonical.rs
3.7 KiB · Rust · last changed Ephemeral scratch mounts in M0: ephemeral://tmpfs and ephemeral://disk (a sparse disk per job), checked targets and sizes, in the canonical job and the receipt; flow-init mounts them for the job's user; make deploy-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
    let mut fields = vec![
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
    // Mounts as [source, target, size MiB] sorted by target; absent when
54
    // there are none, so jobs without mounts hash as before.
55
    if !j.mounts.is_empty() {
56
        fields.push((
57
            6,
58
            Value::Array(
59
                j.mounts
60
                    .iter()
61
                    .map(|m| Value::Array(vec![text(m.source.as_str()), text(&m.target), Value::Uint(m.size_mib as u64)]))
62
                    .collect(),
63
            ),
64
        ));
65
    }
66
    Value::map(fields)
67
}
68
69
impl Pipeline {
70
    /// The canonical CBOR job graph: sandboxes and jobs in name order.
71
    pub fn canonical(&self) -> Vec<u8> {
72
        encode(&Value::map([
73
            (0, Value::Uint(self.schema_version as u64)),
74
            (1, Value::Array(self.sandboxes.values().map(sandbox_value).collect())),
75
            (2, Value::Array(self.jobs.values().map(job_value).collect())),
76
        ]))
77
    }
78
79
    /// `pipeline_hash` of receipts and provenance.
80
    pub fn hash(&self) -> Hash {
81
        object_hash("pipeline", &self.canonical())
82
    }
83
}
84
85
impl Job {
86
    /// `Hash(J)` of spec §6.2: the job with its sandbox, so a change to
87
    /// either is a different job.
88
    pub fn hash(&self, sandbox: &Sandbox) -> Hash {
89
        object_hash("job", &encode(&Value::Array(vec![job_value(self), sandbox_value(sandbox)])))
90
    }
91
}
92
93
/// BLAKE3 tree hash of the files a scope selects (spec §6.2): sorted
94
/// `(path, content hash)` pairs. An empty scope selects every file.
95
pub fn scope_tree_hash<'a>(scope: &[String], files: impl IntoIterator<Item = (&'a str, Hash)>) -> Hash {
96
    let set = {
97
        let mut b = globset::GlobSetBuilder::new();
98
        for g in scope {
99
            if let Ok(g) = globset::Glob::new(g) {
100
                b.add(g);
101
            }
102
        }
103
        b.build().unwrap_or_else(|_| globset::GlobSet::empty())
104
    };
105
    let mut picked: Vec<(&str, Hash)> = files.into_iter().filter(|(p, _)| scope.is_empty() || set.is_match(p)).collect();
106
    picked.sort();
107
    let body = encode(&Value::Array(picked.iter().map(|(p, h)| Value::Array(vec![text(p), Value::Bytes(h.to_vec())])).collect()));
108
    object_hash("scope", &body)
109
}