summaryrefslogtreecommitdiffstats
path: root/src/db/model.rs
diff options
context:
space:
mode:
authoraur-build <aur-build@localhost>2026-11-18 14:47:59 +0000
committeraur-build <aur-build@localhost>2026-11-18 14:47:59 +0000
commit7946e9ffc48748992f051ada15bec772b16a585d (patch)
tree96b6674efb30774647687a117759723ff3207965 /src/db/model.rs
parent3370a487d6e1100623ebfd982967f1223d251607 (diff)
downloadaur-build-7946e9ffc48748992f051ada15bec772b16a585d.tar.gz
aur-build-7946e9ffc48748992f051ada15bec772b16a585d.zip
hash: canonical_json is canonical now, and a serialization failure is an error
The function behind `Identity::hash` — the digest an approval echoes back — did two things its name promised and it did not do. It did not order anything: the doc comment conceded that callers were expected to pass "already-canonical values", which is a convention, not a property, and a struct serializes in whatever order its fields were declared. And on a serialization failure it returned `"{}"`, so every value that failed to serialize hashed to one shared digest. For a system whose whole job is provenance, "two identities that could not be serialized would look identical to the approval echo" is the worst possible failure to have available. Serialization now routes through `serde_json::Value`, whose map is a `BTreeMap`, so object keys come out sorted whatever the input's construction order — the name describes what happens. A failure propagates: `canonical_json` returns `Result`, `Identity::hash` returns `Result`, and the four call sites (detect, put_world_snapshot, the world step, both enqueue paths) pass it on. There is no path left where a digest is invented for a value that could not be rendered. `tests/hash.rs` pins all three properties: an unserializable value is an error naming itself, two same-field-different-declaration-order values render identically (and to the exact compact sorted bytes), and the identity digest changes when any element of the tuple changes.
Diffstat (limited to 'src/db/model.rs')
-rw-r--r--src/db/model.rs9
1 files changed, 6 insertions, 3 deletions
diff --git a/src/db/model.rs b/src/db/model.rs
index 07065c5..e962d6f 100644
--- a/src/db/model.rs
+++ b/src/db/model.rs
@@ -58,11 +58,14 @@ impl Identity {
/// The hash that makes I3 a fact: two byte-identical inputs never produce
/// two candidates, and any change to what becomes a byte in the artifact
/// produces a new one.
+ ///
/// A digest of the canonical JSON of the identity tuple. This is the value
/// an approval must echo: two candidates share an `identity_hash` only if
- /// every pin, digest and version in them is identical.
- pub fn hash(&self) -> String {
- format!("sha256:{}", hash::hex(hash::canonical_json(self).as_bytes()))
+ /// every pin, digest and version in them is identical. A value that cannot
+ /// be serialized is an error rather than a digest — a shared fallback digest
+ /// would be the one way two identities could collide by accident.
+ pub fn hash(&self) -> crate::error::Result<String> {
+ Ok(format!("sha256:{}", hash::hex(hash::canonical_json(self)?.as_bytes())))
}
}