summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/db/model.rs9
-rw-r--r--src/db/queries.rs2
-rw-r--r--src/hash.rs19
-rw-r--r--src/pipeline/mod.rs4
-rw-r--r--src/pipeline/steps.rs2
-rw-r--r--src/watch/detect.rs2
6 files changed, 24 insertions, 14 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())))
}
}
diff --git a/src/db/queries.rs b/src/db/queries.rs
index fa530b4..ca73e5f 100644
--- a/src/db/queries.rs
+++ b/src/db/queries.rs
@@ -833,7 +833,7 @@ pub fn put_world_snapshot(writer: &Writer, members: &[WorldMember]) -> Result<i6
let mut sorted = members.to_vec();
sorted.sort();
sorted.dedup();
- let hash = crate::hash::canonical_json(&sorted);
+ let hash = crate::hash::canonical_json(&sorted)?;
let json = serde_json::to_string(&sorted)?;
let at = time::now();
writer.tx(move |tx| {
diff --git a/src/hash.rs b/src/hash.rs
index 5243520..0321437 100644
--- a/src/hash.rs
+++ b/src/hash.rs
@@ -69,10 +69,17 @@ pub fn recipe_hash(entries: &[(String, String)]) -> String {
hex::encode(h.finalize())
}
-/// Canonical JSON used for identity digests. Key order in serde_json maps is
-/// not stable across object construction, so we serialize through a
-/// `BTreeMap`-preserving path: callers pass already-canonical values (arrays of
-/// objects with a fixed field order) and we hash the compact rendering.
-pub fn canonical_json<T: serde::Serialize>(v: &T) -> String {
- serde_json::to_string(v).unwrap_or_else(|_| "{}".into())
+/// Serialize a value to canonical JSON: compact, with every object's keys in
+/// sorted order, so equal values always produce equal bytes.
+///
+/// The ordering is not free in `serde_json` — a struct serializes in field
+/// declaration order and a map in whatever order it happens to hold — so the
+/// value is routed through `serde_json::Value`, whose map is a `BTreeMap`.
+/// That makes the name true rather than aspirational.
+///
+/// A serialization failure is an error. It used to be `"{}"`, which meant every
+/// value that failed to serialize hashed to the same digest — and this is the
+/// function behind `Identity::hash`, the value an approval echoes (§6 M-2).
+pub fn canonical_json<T: serde::Serialize>(v: &T) -> Result<String, serde_json::Error> {
+ serde_json::to_string(&serde_json::to_value(v)?)
}
diff --git a/src/pipeline/mod.rs b/src/pipeline/mod.rs
index 459cea6..8f4012e 100644
--- a/src/pipeline/mod.rs
+++ b/src/pipeline/mod.rs
@@ -279,7 +279,7 @@ pub fn enqueue_for_candidate(cfg: &Config, db: &Db, c: &CandidateRow) -> Result<
let pb = db::get_pkgbase_by_id(db, c.pkgbase_id)?
.ok_or_else(|| Error::not_found("pkgbase"))?;
let world = publish::index_world(db)?;
- let world_hash = crate::hash::canonical_json(&world);
+ let world_hash = crate::hash::canonical_json(&world)?;
let id = builds::build_id(&pb.name, &c.identity_hash, &world_hash, &cfg.build_profile);
let world_id = db::put_world_snapshot(&db.writer, &world)?;
builds::enqueue(db, &id, pb.id, Some(c.id), "new_version", Some(world_id), &cfg.build_profile, 0)?;
@@ -298,7 +298,7 @@ pub fn enqueue_manual_rebuild(cfg: &Config, db: &Db, pkgbase: &str) -> Result<St
rel.aur_commit, rel.recipe_hash
));
let world = publish::index_world(db)?;
- let world_hash = crate::hash::canonical_json(&world);
+ let world_hash = crate::hash::canonical_json(&world)?;
let id = builds::build_id(pkgbase, &identity, &world_hash, &cfg.build_profile);
let world_id = db::put_world_snapshot(&db.writer, &world)?;
builds::enqueue(db, &id, pb.id, None, "manual_rebuild", Some(world_id), &cfg.build_profile, 1)?;
diff --git a/src/pipeline/steps.rs b/src/pipeline/steps.rs
index 50e41d3..6bdf12c 100644
--- a/src/pipeline/steps.rs
+++ b/src/pipeline/steps.rs
@@ -103,7 +103,7 @@ fn plan(ctx: &Ctx) -> Result<StepResult> {
return Ok(StepResult::Blocked(missing));
}
let world = crate::pipeline::publish::index_world(&ctx.db)?;
- let hash = crate::hash::canonical_json(&world);
+ let hash = crate::hash::canonical_json(&world)?;
ctx.log_line(&format!(
" deps: {} ours/official, {} from official repos, world {}",
wanted.len() - official,
diff --git a/src/watch/detect.rs b/src/watch/detect.rs
index 3134e19..71d1b29 100644
--- a/src/watch/detect.rs
+++ b/src/watch/detect.rs
@@ -174,7 +174,7 @@ fn analyse(cfg: &Config, db: &Db, pb: &Pkgbase, head: &str) -> Result<CheckRepor
upstream_pins: pins.clone(),
source_digests: digests.clone(),
};
- let identity_hash = identity.hash();
+ let identity_hash = identity.hash()?;
let snap_id = db::put_snapshot(
&db.writer,
pb.id,