gunk-dev/the-valley

inputs.go

sourcebd760cae2dadmain's published tip history
5.7 KiB
1package main
2
3// The inputs a verdict reads besides the request, the target and the
4// attestation refs, and the record that remembers them.
5//
6// The outcome record keys a standing answer by the request's head, the
7// target's tip and the evidence fingerprint. A verdict also reads the
8// floor and which signers count, and either can change while those three
9// stay where they are: the registry accepts a signer, or the group lands
10// a floor that requires less. So the integrator remembers what it read of
11// them too, in a record of its own beside the outcome record, and an
12// answer stands only while both records match the pass.
13//
14// The inputs live in their own record, not in the outcome record, because
15// the outcome record is read outside the controller (rebase/outcome.go),
16// and its readers compare its evidence fingerprint with one they compute
17// from the attestation refs alone. Neither the floor nor the
18// known-signers file is something they can read.
19
20import (
21 "context"
22 "crypto/sha256"
23 "encoding/hex"
24 "fmt"
25 "os"
26 "path/filepath"
27 "sort"
28 "strings"
29)
30
31// inputsPrefix is where the inputs records live, one per request, named as
32// its outcome record is.
33const inputsPrefix = "refs/the-valley/integration-inputs"
34
35// unreadInputs is what a pass records as its inputs when one of them did
36// not read. It is not a digest, so it never equals the inputs of a pass
37// that read them all. Only a failure stands under it.
38const unreadInputs = "unread"
39
40// inputs is what one pass read of the floor and the accepted signers.
41type inputs struct {
42 // digest is the fingerprint the inputs record holds, or unreadInputs.
43 digest string
44 // floor is the floor's documents as a directory of *.cue, opened once
45 // for the pass, so the verdict composes the documents digest was
46 // taken over. "" when the floor did not read.
47 floor string
48 // err is why an input did not read. A request it would have judged
49 // fails instead.
50 err error
51}
52
53// inputState reads the inputs for one pass, and returns what ends the
54// pass's hold on the floor. The digest is SHA-256 over these lines:
55//
56// - floor <name> <sha256>, for every *.cue document of the floor, the
57// same documents the verdict composes, by name;
58// - known-signers <sha256> of the known-signers file as it reads now;
59// - signer-projects <sha256> of the signer projects file as read at
60// start, or signer-projects none when there is none;
61// - project <name> and signer <name>, which say where a signer counts
62// and which signature is the integrator's own.
63//
64// The floor and the known-signers file are read before any request is
65// judged. The verdict reads the floor from the copy taken here. attest
66// reads the known-signers file again when it verifies a note, so a file
67// rewritten during the pass may be judged at its new bytes and recorded
68// at its old ones. The next pass then reads a different digest and judges
69// again; the reverse, an old answer recorded under new bytes, cannot
70// happen.
71func (in *integrator) inputState() (inputs, func()) {
72 h := sha256.New()
73 floor, done, err := in.policy.layer.Open(context.Background(), "valley-integrator-floor")
74 if err != nil {
75 return inputs{digest: unreadInputs, err: err}, done
76 }
77 // Open refuses a floor with no *.cue in it, so docs is never empty.
78 docs, err := filepath.Glob(filepath.Join(floor, "*.cue"))
79 sort.Strings(docs)
80 for _, doc := range docs {
81 var data []byte
82 if data, err = os.ReadFile(doc); err != nil {
83 break
84 }
85 fmt.Fprintf(h, "floor %s %x\n", filepath.Base(doc), sha256.Sum256(data))
86 }
87 if err != nil {
88 done()
89 return inputs{digest: unreadInputs, err: fmt.Errorf("reading the floor: %w", err)}, func() {}
90 }
91 keys, err := os.ReadFile(in.knownSigners)
92 if err != nil {
93 done()
94 return inputs{digest: unreadInputs, err: fmt.Errorf("reading the known-signers file: %w", err)}, func() {}
95 }
96 fmt.Fprintf(h, "known-signers %x\n", sha256.Sum256(keys))
97 if in.signerProjectsData == nil {
98 fmt.Fprintf(h, "signer-projects none\n")
99 } else {
100 fmt.Fprintf(h, "signer-projects %x\n", sha256.Sum256(in.signerProjectsData))
101 }
102 fmt.Fprintf(h, "project %s\nsigner %s\n", in.project, in.signerName)
103 return inputs{digest: hex.EncodeToString(h.Sum(nil)), floor: floor}, done
104}
105
106func (in *integrator) inputsRef(r request) string {
107 return inputsPrefix + "/" + strings.TrimPrefix(r.ref, requestPrefix+"/")
108}
109
110// readInputs reads the inputs the request's outcome record was written
111// under, or "" when that cannot be told. The inputs record names the blob
112// of the outcome record it was written with. An outcome record written
113// without one, as a landing's, a voiding's, or one from before inputs
114// were remembered, leaves the inputs record naming another blob, and
115// that reads as "".
116func (in *integrator) readInputs(r request) string {
117 outcome, ok, err := resolveRef(in.repo, in.memoRef(r))
118 if err != nil || !ok {
119 return ""
120 }
121 oid, ok, err := resolveRef(in.repo, in.inputsRef(r))
122 if err != nil || !ok {
123 return ""
124 }
125 out, ok := gitTry(in.repo, "cat-file", "blob", oid)
126 if !ok {
127 return ""
128 }
129 digest, bound, ok := strings.Cut(strings.TrimSpace(out), " ")
130 if !ok || bound != outcome || digest == "" {
131 return ""
132 }
133 return digest
134}
135
136// writeAnswer writes a request's outcome record and its inputs record in
137// one ref transaction, so neither moves without the other.
138func (in *integrator) writeAnswer(r request, value, digest string) error {
139 outcome, err := in.writeBlob([]byte(value + "\n"))
140 if err != nil {
141 return err
142 }
143 record, err := in.writeBlob([]byte(digest + " " + outcome + "\n"))
144 if err != nil {
145 return err
146 }
147 stdin := fmt.Sprintf("start\nupdate %s %s\nupdate %s %s\nprepare\ncommit\n",
148 in.memoRef(r), outcome, in.inputsRef(r), record)
149 _, err = gitInput(in.repo, []byte(stdin), "update-ref", "--stdin")
150 return err
151}