gunk-dev/the-valley

evidence.go

source96b47ed3e847main's published tip history
2.0 KiB
1package change
2
3import (
4 "crypto/sha256"
5 "encoding/hex"
6 "sort"
7)
8
9// AttestationRefs is the namespace the integrator keeps evidence under.
10const AttestationRefs = "refs/the-valley/attestations/"
11
12// EvidenceFingerprint is the evidence state the integrator writes into an
13// outcome record (integrator/refs.go): the first 16 hex digits of the
14// SHA-256 over every attestation ref on origin, one "<ref> <oid>" line
15// each, every line ending in a newline, in byte order. An outcome record
16// with a different fingerprint was written about other evidence.
17//
18// The integrator, valley status and the restacker each compute it from
19// what they read, so it is defined once, here.
20func EvidenceFingerprint(attestations []string) string {
21 sorted := append([]string(nil), attestations...)
22 sort.Strings(sorted)
23 h := sha256.New()
24 for _, line := range sorted {
25 h.Write([]byte(line + "\n"))
26 }
27 return hex.EncodeToString(h.Sum(nil))[:16]
28}
29
30// SelectNotes picks the note each check is judged by, from every note
31// stored for one tree. notes must be in the order the integrator reads
32// them: the attestation refs in byte order, and each ref's notes in the
33// order git ls-tree -r lists them. check names the check a note says it
34// is about, and verifies says whether the note verifies.
35//
36// The first note for a check that verifies is the one it is judged by,
37// and no later note replaces it, whatever it records. Until one verifies,
38// each note read replaces the one before, so the last refusal stands.
39//
40// The integrator judges a request by this choice (integrator/evidence.go),
41// and approve reuses evidence only where this choice is evidence it
42// accepts (status/coverage.go), so the two never judge a check by
43// different notes.
44func SelectNotes[N any](notes []N, check func(N) string, verifies func(N) bool) map[string]N {
45 picked := map[string]N{}
46 for _, n := range notes {
47 name := check(n)
48 if old, seen := picked[name]; seen && verifies(old) {
49 continue
50 }
51 picked[name] = n
52 }
53 return picked
54}