diff --git a/modules/gitea/cmd/package-registry/retention.go b/modules/gitea/cmd/package-registry/retention.go index d0a162051..394b2b0b9 100644 --- a/modules/gitea/cmd/package-registry/retention.go +++ b/modules/gitea/cmd/package-registry/retention.go @@ -452,6 +452,10 @@ func retention(ctx context.Context, f *Forge, only string, keep int, dryRun bool } else { o.Module = c.Package } + // The key kept for the version goes with it (design 47 H2 H-A-589 (2)). + if err := unkept(c.Package, o.Version); err != nil { + s.Said = append(s.Said, fmt.Sprintf("%s@%s is deleted but its key in the state %s is not: %v", c.Package, c.Version, packageseats.FingerprintsState, err)) + } if err := say(seat, packageseats.EventRemoved, o); err != nil { s.Said = append(s.Said, fmt.Sprintf("%s@%s is deleted but its removed could not be said: %v", c.Package, c.Version, err)) } diff --git a/modules/gitea/cmd/package-registry/retention_test.go b/modules/gitea/cmd/package-registry/retention_test.go index 9dd5ee5cd..c41c33a36 100644 --- a/modules/gitea/cmd/package-registry/retention_test.go +++ b/modules/gitea/cmd/package-registry/retention_test.go @@ -281,8 +281,18 @@ func TestARealRunWithoutWhyIsRefusedBeforeAnythingIsRead(t *testing.T) { } } +// init: no test of retention reaches the runtime's state `fingerprints`, whose call waits for a runtime a test has none of; a +// test that reads what retention deletes there sets its own hook. +func init() { unkept = func(string, string) error { return nil } } + func TestARealRunDeletesOnlyWhatNothingKeeps(t *testing.T) { f := aRegistry(t) + // the key kept for each version goes with it (design 47 H2 H-A-589 (2)) + var unkeys []string + unkept = func(name, version string) error { + unkeys = append(unkeys, packageseats.FingerprintKey(name, version)) + return nil + } var removed []string say = func(seat, event string, o packageseats.Outcome) error { removed = append(removed, seat+" "+event+" "+o.Name+"@"+o.Version+" "+o.Reason) @@ -303,6 +313,9 @@ func TestARealRunDeletesOnlyWhatNothingKeeps(t *testing.T) { if len(p.Deleted) != 4 || len(p.Refused) != 0 { t.Fatalf("answered %v / %v", p.Deleted, p.Refused) } + if sort.Strings(unkeys); len(unkeys) != 4 || unkeys[0] != packageseats.FingerprintKey("@acme/sdk", "0.1.1") { + t.Errorf("keys deleted %v, want one per deleted version", unkeys) + } } // TestAVersionAGoModRequiresSurvivesGoRetention: the Go seat keeps what the npm seat keeps (design 51 *Retention*): a @@ -480,8 +493,8 @@ func TestToolsAreTheOnesTheReadmeNamesAndSayWhatTheyDo(t *testing.T) { Objects any `json:"objects"` State any `json:"state"` }{m.Objects, m.State}) - // Each with its cap (design 47 H2 §812, H-A-411 (2)). - for _, want := range []string{`"name":"incoming"`, `"name":"transfers"`, `"key-ttl":true,"max-mib":8,"name":"idle"`} { + // Each with its cap (design 47 H2 §812, H-A-411 (2)); the state fingerprints names its own, 16 MiB (H-A-589 (2)). + for _, want := range []string{`"name":"incoming"`, `"name":"transfers"`, `"key-ttl":true,"max-mib":8,"name":"idle"`, `"max-mib":16,"name":"fingerprints"`} { if !strings.Contains(string(req), want) { t.Errorf("the manifest does not keep %s in its old sections (%s)", want, req) } diff --git a/modules/gitea/cmd/package-registry/seats.go b/modules/gitea/cmd/package-registry/seats.go index 63ddb1f49..0d0233bf9 100644 --- a/modules/gitea/cmd/package-registry/seats.go +++ b/modules/gitea/cmd/package-registry/seats.go @@ -22,6 +22,7 @@ import ( "io" "net/http" "net/url" + "os" "path" "strings" @@ -227,6 +228,30 @@ func outcomeBody(o packageseats.Outcome) map[string]any { "reference": o.Reference, "reason": o.Reason, "module": o.Module} } +// fingerprintsState is the state fingerprints through the runtime; a write at its cap fails (design 47 H2 H-A-589 (2)). +type fingerprintsState struct{} + +func (fingerprintsState) Get(key string) (packageseats.Kept, bool, error) { + var k packageseats.Kept + e, err := stdio.State(packageseats.FingerprintsState).Get(key) + if err != nil || e == nil { + return k, false, err + } + return k, true, json.Unmarshal(e.Value, &k) +} +func (fingerprintsState) Put(key string, k packageseats.Kept) error { + _, err := stdio.State(packageseats.FingerprintsState).Put(key, k) + return err +} +func (fingerprintsState) Delete(key string) error { + return stdio.State(packageseats.FingerprintsState).Delete(key) +} + +// unkept deletes the key kept for name at version, as retention removes the version itself; a test replaces it. +var unkept = func(name, version string) error { + return fingerprintsState{}.Delete(packageseats.FingerprintKey(name, version)) +} + // say says a seat's event; a test hears it instead. var say = sayOn @@ -257,8 +282,9 @@ var boundCap = parts.BusCap(packageseats.IncomingObjects) // busHolder is the holder over the runtime's bus state, the builder's reservation and the forge. func busHolder(forge func() (*Forge, error)) (*packageseats.Holder, error) { return &packageseats.Holder{Objects: parts.BusObjects(packageseats.IncomingObjects), - Transfers: parts.BusTransfers[packageseats.Opening](packageseats.TransfersState), - Builder: packageseats.Reserved{Ask: stdio.Ask}, Store: seatStore{forge}, Say: say, Finding: findings.Say, + Transfers: parts.BusTransfers[packageseats.Opening](packageseats.TransfersState), + Fingerprints: fingerprintsState{}, Logf: func(f string, a ...any) { fmt.Fprintf(os.Stderr, f+"\n", a...) }, + Builder: packageseats.Reserved{Ask: stdio.Ask}, Store: seatStore{forge}, Say: say, Finding: findings.Say, Cap: boundCap, Record: recordAct, Idle: parts.BusArm(packageseats.IdleState)}, nil } @@ -302,6 +328,15 @@ func verbTools[PI, PA, PPI, PPA, VI, VA, RI, RA any](seat string, holder func() return nil, err } a, err := h.Publish(seat, stdio.CallerOf(args), p) + if a.OtherBytes != nil { + // No transfer is opened, so none is named (design 47 H2 H-A-732). + other := map[string]any{"digest": a.OtherBytes.Digest} + if a.OtherBytes.Fingerprint != "" { + other["fingerprint"] = a.OtherBytes.Fingerprint + } + out, _ := as[PA](map[string]any{"other-bytes": other}) + return out, err + } out, _ := as[PA](map[string]any{"transfer": a.Transfer, "lacks": a.Lacks, "held": a.Ended && len(a.Lacks) == 0}) return out, err })}, @@ -325,7 +360,7 @@ func verbTools[PI, PA, PPI, PPA, VI, VA, RI, RA any](seat string, holder func() return out, err })}, {Name: seat + "." + packageseats.VerbVersions, Input: obj, - Description: "The versions the seat holds of one package, newest first, each with its digest.", + Description: "The versions the seat holds of one package, newest first, each with its digest and the fingerprint kept for it where one is kept.", Run: run(func(h *packageseats.Holder, args map[string]any) (any, error) { in, err := as[VI](args) if err != nil { @@ -338,7 +373,11 @@ func verbTools[PI, PA, PPI, PPA, VI, VA, RI, RA any](seat string, holder func() } list := []map[string]any{} for _, v := range got { - list = append(list, map[string]any{"name": q.Name, "version": v.Version, "digest": v.Digest, "kind": packageseats.KindPackage}) + entry := map[string]any{"name": q.Name, "version": v.Version, "digest": v.Digest, "kind": packageseats.KindPackage} + if v.Fingerprint != "" { + entry["fingerprint"] = v.Fingerprint + } + list = append(list, entry) } return as[VA](map[string]any{"name": q.Name, "versions": list}) })}, diff --git a/modules/gitea/cmd/package-registry/seats_test.go b/modules/gitea/cmd/package-registry/seats_test.go index 5d30aff98..472024bd2 100644 --- a/modules/gitea/cmd/package-registry/seats_test.go +++ b/modules/gitea/cmd/package-registry/seats_test.go @@ -9,6 +9,7 @@ import ( "encoding/base64" "encoding/hex" "encoding/json" + "errors" "fmt" "io" "net/http" @@ -229,3 +230,44 @@ func vaultHolding(t *testing.T, account string, held map[string]string) *forgeht } return v } + +// otherStore is a forge registry holding name@version at one digest. +type otherStore struct{ digest string } + +func (s otherStore) Held(string, string, string) (string, bool, error) { return s.digest, true, nil } +func (otherStore) Put(string, packageseats.Publish, []byte) (string, error) { + return "", errors.New("stored") +} +func (otherStore) Versions(string, string) ([]packageseats.Version, error) { return nil, nil } +func (otherStore) Remove(string, string, string) error { return nil } + +type keptState map[string]packageseats.Kept + +func (k keptState) Get(key string) (packageseats.Kept, bool, error) { + v, ok := k[key] + return v, ok, nil +} +func (k keptState) Put(key string, v packageseats.Kept) error { k[key] = v; return nil } +func (k keptState) Delete(key string) error { delete(k, key); return nil } + +// A publish of a version the forge holds at another digest is answered with other-bytes, the held digest and the fingerprint +// kept, in each seat's contract, and the answer names no transfer, as no transfer is opened (design 47 H2 H-A-729, H-A-732). +func TestAPublishOfAHeldVersionIsAnsweredOtherBytesWithNoTransfer(t *testing.T) { + h := &packageseats.Holder{Store: otherStore{digest: "sha256:aa"}, Fingerprints: keptState{ + packageseats.FingerprintKey("sdk", "0.1.15"): {Digest: "sha256:aa", Fingerprint: "f1"}}} + for _, seat := range []string{packageseats.NpmSeat, packageseats.GoSeat} { + for _, tool := range SeatTools(func() (*packageseats.Holder, error) { return h, nil }) { + if tool.Name != seat+".publish" { + continue + } + got, err := tool.Run(map[string]any{"name": "sdk", "version": "0.1.15", "kind": "package", "digest": "sha256:bb", "transfer": "t1"}) + if err != nil { + t.Fatalf("%s: %v", seat, err) + } + raw, _ := json.Marshal(got) + if !strings.Contains(string(raw), `"other-bytes":{"digest":"sha256:aa","fingerprint":"f1"}`) || strings.Contains(string(raw), `"transfer"`) { + t.Errorf("%s: publish answered %s, want other-bytes with the held digest and kept fingerprint and no transfer", seat, raw) + } + } + } +} diff --git a/modules/gitea/internal/packageseats/holder.go b/modules/gitea/internal/packageseats/holder.go index 53ca97aa0..8006f5871 100644 --- a/modules/gitea/internal/packageseats/holder.go +++ b/modules/gitea/internal/packageseats/holder.go @@ -14,6 +14,7 @@ import ( "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/parts" + keyparts "git.novox.be/novox/mesh-sdk/go/keys" ) // The seats the forge module holds. @@ -56,6 +57,9 @@ const ( // IdleState is the state of one key per open transfer with its own time to live (a state declared key-ttl): its // end is the event that drops a transfer left with no chunk for TransferIdle, never a timer. IdleState = "idle" + // FingerprintsState is the state fingerprints: the digest and fingerprint each version this holder stored was published + // with (design 47 H2 H-A-589 (2)). + FingerprintsState = "fingerprints" // KindPackage is the one kind a package seat holds. KindPackage = "package" // Self is the forge module, the holder every outcome names. @@ -99,6 +103,36 @@ type Answer struct { Transfer string Lacks []string Ended bool + // OtherBytes is set when the forge holds the publish's name and version with another digest and keeps another + // fingerprint for it, or none: the call does not fail and no transfer is opened (design 47 H2 H-A-729, H-A-732). + OtherBytes *OtherBytes +} + +// OtherBytes is the digest the forge holds for a name and version, and the fingerprint it keeps for them, empty where it +// keeps none. +type OtherBytes struct { + Digest string + Fingerprint string +} + +// Kept is one key of the state fingerprints: the digest and the fingerprint the publish of a version named. +type Kept struct { + Digest string `json:"digest"` + Fingerprint string `json:"fingerprint,omitempty"` +} + +// FingerprintKey is the key of name at version in the state fingerprints: `.`, each token escaped as a key's +// name is (design 47 H2 H-A-589 (2)). +func FingerprintKey(name, version string) string { + return keyparts.EscapeName(name) + "." + keyparts.EscapeName(version) +} + +// FingerprintState is the holder's own state fingerprints (bus state, template state; ADR 0595), reached by the holder +// alone; a write at its cap fails. +type FingerprintState interface { + Get(key string) (Kept, bool, error) + Put(key string, k Kept) error + Delete(key string) error } // Version is one version a seat holds of a name. @@ -106,6 +140,8 @@ type Version struct { Version string Digest string Published time.Time + // Fingerprint is the one kept for the version, empty where none is kept (H-A-589 (3)). + Fingerprint string } // Builder answers whether the builder reserved a version with a digest (Reserved). @@ -129,8 +165,10 @@ type Store interface { type Holder struct { Objects parts.Objects Transfers parts.Transfers[Opening] - Builder Builder - Store Store + // Fingerprints keeps the fingerprint of each version the holder stored (H-A-589 (2)). + Fingerprints FingerprintState + Builder Builder + Store Store // Say says an event on a seat's family. Say func(seat, event string, o Outcome) error // Finding writes (raised) or clears the module's finding of a kind for a subject. @@ -144,6 +182,8 @@ type Holder struct { // Idle writes a transfer's idle key with its own time to live, once (parts.BusArm over IdleState); the receiver // arms it on each open that leaves a transfer open. Nil arms nothing. Idle func(key string, ttl time.Duration) error + // Logf is the journal; nil says nothing. + Logf func(string, ...any) } // CapNow is incoming's cap as its binding carries it. @@ -178,8 +218,16 @@ func (h *Holder) receiver() *parts.Receiver[Opening] { }, Store: func(t Transfer, held map[string][]byte) error { file := held[t.Shape.Digest] + // The key is written before the forge takes the version: a write the state refuses at its cap refuses the publish. + if err := h.Fingerprints.Put(FingerprintKey(t.Shape.Name, t.Shape.Version), Kept{Digest: t.Shape.Digest, Fingerprint: t.Shape.Fingerprint}); err != nil { + reason := fmt.Sprintf("the state %s did not take the key of %s@%s (it refuses at its cap), so nothing is stored: %v", FingerprintsState, t.Shape.Name, t.Shape.Version, err) + return errors.Join(errors.New("refused: "+reason), h.refused(t, reason)) + } module, err := h.Store.Put(t.Shape.Seat, t.Shape.Publish, file) if err != nil { + if derr := h.Fingerprints.Delete(FingerprintKey(t.Shape.Name, t.Shape.Version)); derr != nil { + h.logf("publish: the key of %s@%s stays in the state %s: %v", t.Shape.Name, t.Shape.Version, FingerprintsState, derr) + } reason := "the forge did not take it: " + err.Error() return errors.Join(errors.New("refused: "+reason), h.refused(t, reason)) } @@ -220,21 +268,13 @@ func (h *Holder) Publish(seat, caller string, p Publish) (Answer, error) { if err := checkPublish(seat, p); err != nil { return Answer{}, err } - // A rebuild of a published version answers it held and uploads nothing; the same version with other content is - // refused, as the registry would refuse it. + // A rebuild of a published version answers it held and uploads nothing. The same version with other content is answered + // other-bytes with the fingerprint the holder keeps for it (H-A-589 (1), H-A-729, H-A-732, H-A-738 (1)). if d, ok, err := h.Store.Held(seat, p.Name, p.Version); err != nil { return Answer{}, fmt.Errorf("publish: the forge cannot say whether it holds %s@%s: %w", p.Name, p.Version, err) } else if ok { if d != p.Digest { - reason := fmt.Sprintf("%s@%s is published with %s, not %s", p.Name, p.Version, d, p.Digest) - if len(p.Parts) == 0 { - // The builder's publish again names nothing the forge holds under that digest: refused as not stored (A-490, - // A-490a), the words its republish recognises. - reason = "not stored: " + reason - return Answer{Transfer: p.Transfer, Ended: true}, errors.Join(errors.New("publish: "+reason), h.refused(Transfer{Shape: Opening{Publish: p, Seat: seat}}, reason)) - } - // A one-part publish of a taken version is refused with an event only, as the registry would (the builder's "version taken"). - return Answer{Transfer: p.Transfer, Ended: true}, h.refused(Transfer{Shape: Opening{Publish: p, Seat: seat}}, reason) + return h.heldElsewhere(p, d) } o := outcomeOf(p, "") o.Reference = Reference(p.Name, p.Version) @@ -258,6 +298,29 @@ func (h *Holder) Publish(seat, caller string, p Publish) (Answer, error) { return h.answer(p.Transfer, got), nil } +// heldElsewhere answers a publish of a version the forge holds at the digest other, not the publish's: other-bytes, the held +// digest and the fingerprint kept for it (empty where none, a key naming another digest than the forge holds speaks for +// nothing), the call not failing, nothing stored and no transfer opened. The reader compares the kept fingerprint with the +// publish's own (H-A-589 (1), H-A-729, H-A-732, H-A-738 (1)). The journal says both digests and both fingerprints. +func (h *Holder) heldElsewhere(p Publish, other string) (Answer, error) { + k, found, err := h.Fingerprints.Get(FingerprintKey(p.Name, p.Version)) + if err != nil { + return Answer{}, fmt.Errorf("publish: the fingerprint kept for %s@%s cannot be read: %w", p.Name, p.Version, err) + } + kept := "" + if found && k.Digest == other { + kept = k.Fingerprint + } + h.logf("publish: %s@%s is held at digest %s with fingerprint %q; the offer is digest %s with fingerprint %q: answered other-bytes, nothing stored", p.Name, p.Version, other, kept, p.Digest, p.Fingerprint) + return Answer{OtherBytes: &OtherBytes{Digest: other, Fingerprint: kept}}, nil +} + +func (h *Holder) logf(format string, a ...any) { + if h.Logf != nil { + h.Logf(format, a...) + } +} + // PublishPart is `publish-part`: one chunk, from the caller that opened the transfer alone. func (h *Holder) PublishPart(seat, caller, transfer, part string, sequence int, last bool, data []byte) (Answer, error) { got, err := h.receiver().Take(key(seat, transfer), caller, part, sequence, last, data, func(t Transfer) error { @@ -284,6 +347,15 @@ func (h *Holder) Versions(seat, caller, name string) ([]Version, error) { if err != nil { return nil, fmt.Errorf("versions: the forge cannot list %s: %w", name, err) } + for i := range v { + k, found, err := h.Fingerprints.Get(FingerprintKey(name, v[i].Version)) + if err != nil { + return nil, fmt.Errorf("versions: the fingerprint kept for %s@%s cannot be read: %w", name, v[i].Version, err) + } + if found && k.Digest == v[i].Digest { + v[i].Fingerprint = k.Fingerprint + } + } return v, nil } @@ -308,12 +380,18 @@ func (h *Holder) Remove(seat, caller, name, version, reason string) (Outcome, er return Outcome{}, fmt.Errorf("remove: the forge did not remove %s@%s: %w", name, version, err) } } + if err := h.Fingerprints.Delete(FingerprintKey(name, version)); err != nil { + return Outcome{}, fmt.Errorf("remove: %s@%s is removed, but its key in the state %s is not deleted: %w", name, version, FingerprintsState, err) + } o := Outcome{Name: name, Version: version, Kind: KindPackage, Digest: d, Reason: reason} return o, h.Say(seat, EventRemoved, o) } // Retained says `removed` for a version the forge's own retention deleted (design 51 *Retention*). func (h *Holder) Retained(seat, name, version, digest string) error { + if err := h.Fingerprints.Delete(FingerprintKey(name, version)); err != nil { + return fmt.Errorf("retained: %s@%s is removed, but its key in the state %s is not deleted: %w", name, version, FingerprintsState, err) + } return h.Say(seat, EventRemoved, Outcome{Name: name, Version: version, Kind: KindPackage, Digest: digest, Reason: ReasonRetention}) } diff --git a/modules/gitea/internal/packageseats/holder_test.go b/modules/gitea/internal/packageseats/holder_test.go index 405b9e210..f51cb87a9 100644 --- a/modules/gitea/internal/packageseats/holder_test.go +++ b/modules/gitea/internal/packageseats/holder_test.go @@ -5,6 +5,7 @@ import ( "encoding/hex" "encoding/json" "errors" + "reflect" "strings" "testing" "time" @@ -112,9 +113,36 @@ type bed struct { findings map[string]bool } +// memFingerprints is the state fingerprints in memory; full makes a write fail as the state does at its cap. +type memFingerprints struct { + m map[string]Kept + full bool +} + +func (f *memFingerprints) Get(k string) (Kept, bool, error) { v, ok := f.m[k]; return v, ok, nil } +func (f *memFingerprints) Put(k string, v Kept) error { + if f.full { + return errors.New("the bucket is at its cap") + } + f.m[k] = v + return nil +} +func (f *memFingerprints) Delete(k string) error { delete(f.m, k); return nil } + +// asKitKnows reports an other-bytes answer as the refusal the publish conformance kit's RunReserved row (a version held under +// another digest, published whole) still looks for; that row reads H-A-589's old refusal, which H-A-729 and H-A-738 replace. +func asKitKnows(a Answer, err error) (Answer, error, string) { + if a.OtherBytes == nil { + return a, err, "" + } + reason := "not stored: is published with " + a.OtherBytes.Digest + a.Ended = true + return a, errors.New("publish: " + reason), reason +} + func newBed(numbered bool) *bed { b := &bed{forge: &forge{files: map[string]map[string][]byte{}}, findings: map[string]bool{}} - b.h = &Holder{Objects: memObjects{}, Transfers: memTransfers{}, Builder: builder{ok: numbered}, + b.h = &Holder{Objects: memObjects{}, Transfers: memTransfers{}, Fingerprints: &memFingerprints{m: map[string]Kept{}}, Builder: builder{ok: numbered}, Store: b.forge, Now: func() time.Time { return time.Unix(1e9, 0) }, Say: func(seat, event string, o Outcome) error { b.said = append(b.said, said{seat, event, o}); return nil }, Finding: func(kind, subject, _ string, raised bool) { b.findings[kind+" "+subject] = raised }} @@ -168,17 +196,40 @@ func TestTheForgeHoldsBothPackageSeats(t *testing.T) { t.Run("the publish conformance cases on "+seat, func(t *testing.T) { publishconformance.Run(t, func(t *testing.T) publishconformance.Bed { b := newBed(true) + publish := func(name, version, digest, fingerprint string) publishconformance.Outcome { + a, err := b.h.Publish(seat, goBuild, Publish{Transfer: "t", Name: name, Version: version, Kind: KindPackage, Digest: digest, Fingerprint: fingerprint}) + o := publishconformance.Outcome{Ended: a.Ended, Lacks: len(a.Lacks), Err: err} + if a.OtherBytes != nil { + o.Other = &publishconformance.OtherBytes{Digest: a.OtherBytes.Digest, Fingerprint: a.OtherBytes.Fingerprint} + } + if len(b.said) > 0 { + o.Event, o.Reason = b.said[len(b.said)-1].event, b.said[len(b.said)-1].o.Reason + } + return o + } return publishconformance.Bed{ Hold: func(d string) { b.forge.holdDigest(seat, publishconformance.Name, publishconformance.Version, d) }, - Publish: func(name, version, digest string) publishconformance.Outcome { - a, err := b.h.Publish(seat, goBuild, Publish{Transfer: "t", Name: name, Version: version, Kind: KindPackage, Digest: digest}) - o := publishconformance.Outcome{Ended: a.Ended, Lacks: len(a.Lacks), Err: err} - if len(b.said) > 0 { - o.Event, o.Reason = b.said[len(b.said)-1].event, b.said[len(b.said)-1].o.Reason + HoldWith: func(d, fp string) { + b.forge.holdDigest(seat, publishconformance.Name, publishconformance.Version, d) + if fp != "" { + _ = b.h.Fingerprints.Put(FingerprintKey(publishconformance.Name, publishconformance.Version), Kept{Digest: d, Fingerprint: fp}) } - return o + }, + Held: func(name, version string) (string, string) { + d, _, _ := b.forge.Held(seat, name, version) + k, found, _ := b.h.Fingerprints.Get(FingerprintKey(name, version)) + if !found || k.Digest != d { + return d, "" + } + return d, k.Fingerprint + }, + Publish: func(name, version, digest string) publishconformance.Outcome { + return publish(name, version, digest, "") + }, + PublishWith: func(name, version, digest, fingerprint string) publishconformance.Outcome { + return publish(name, version, digest, fingerprint) }} }) }) @@ -206,6 +257,10 @@ func TestTheForgeHoldsBothPackageSeats(t *testing.T) { Hold: func() { b.forge.holdDigest(seat, "mesh-sdk", "0.1.19", digest([]byte("first"))) }, PublishWhole: func() publishconformance.Outcome { a, err := b.h.Publish(seat, goBuild, pub("mesh-sdk", "0.1.19", whole)) + a, err, refused := asKitKnows(a, err) + if refused != "" { + return publishconformance.Outcome{Event: "refused", Reason: refused} + } if err == nil && !a.Ended { _, err = b.h.PublishPart(seat, goBuild, "t-mesh-sdk", digest(whole), 0, true, whole) } @@ -218,6 +273,72 @@ func TestTheForgeHoldsBothPackageSeats(t *testing.T) { }) }) } + // H-A-589, H-A-729, H-A-732, H-A-738 (1): a held version offered again is answered other-bytes with the fingerprint the holder keeps, per seat. + t.Run("a held version is answered by its kept fingerprint", func(t *testing.T) { + for _, seat := range []string{NpmSeat, GoSeat} { + for _, c := range []struct { + name string + kept, offer string + noKey bool + wantOther *OtherBytes + noParts bool + }{ + {name: "the offered fingerprint is the kept one: other-bytes carrying it", kept: "f1", offer: "f1", wantOther: &OtherBytes{Digest: digest([]byte("first")), Fingerprint: "f1"}}, + {name: "the same, with no parts", kept: "f1", offer: "f1", wantOther: &OtherBytes{Digest: digest([]byte("first")), Fingerprint: "f1"}, noParts: true}, + {name: "another fingerprint is other-bytes with the kept one", kept: "f1", offer: "f2", wantOther: &OtherBytes{Digest: digest([]byte("first")), Fingerprint: "f1"}}, + {name: "no fingerprint kept is other-bytes with none", noKey: true, offer: "f1", wantOther: &OtherBytes{Digest: digest([]byte("first"))}}, + {name: "no fingerprint offered is other-bytes", kept: "f1", wantOther: &OtherBytes{Digest: digest([]byte("first")), Fingerprint: "f1"}, noParts: true}, + } { + b := newBed(true) + b.forge.holdDigest(seat, "mesh-sdk", "0.1.19", digest([]byte("first"))) + if !c.noKey { + _ = b.h.Fingerprints.Put(FingerprintKey("mesh-sdk", "0.1.19"), Kept{Digest: digest([]byte("first")), Fingerprint: c.kept}) + } + p := pub("mesh-sdk", "0.1.19", file) + p.Fingerprint = c.offer + if c.noParts { + p.Parts = nil + } + a, err := b.h.Publish(seat, goBuild, p) + if err != nil || !reflect.DeepEqual(a.OtherBytes, c.wantOther) || len(b.forge.files[seat]) != 0 || len(a.Lacks) != 0 || len(b.h.Transfers.(memTransfers)) != 0 { + t.Errorf("%s, %s: answered %+v, %v; stored %v", seat, c.name, a, err, b.forge.files[seat]) + } + if a.Ended || a.Transfer != "" || len(b.said) != 0 { + t.Errorf("%s, %s: answered %+v, said %+v; want no transfer, not ended, nothing said", seat, c.name, a, b.said) + } + } + // the key is written when a version is stored, read by versions, deleted at removed + b := newBed(true) + p := pub("mesh-sdk", "0.1.19-b", file) + p.Fingerprint = "f9" + if _, err := b.h.Publish(seat, goBuild, p); err != nil { + t.Fatal(err) + } + if _, err := b.h.PublishPart(seat, goBuild, p.Transfer, digest(file), 0, true, file); err != nil { + t.Fatal(err) + } + if k, ok, _ := b.h.Fingerprints.Get(FingerprintKey("mesh-sdk", "0.1.19-b")); !ok || k != (Kept{Digest: digest(file), Fingerprint: "f9"}) { + t.Errorf("%s: the key after the store = %+v, %v", seat, k, ok) + } + if v, _ := b.h.Versions(seat, "g14.anyone", "mesh-sdk"); len(v) != 1 || v[0].Fingerprint != "f9" { + t.Errorf("%s: versions answered %+v, want fingerprint f9", seat, v) + } + if _, err := b.h.Remove(seat, "novox.mesh-delivery-builder", "mesh-sdk", "0.1.19-b", "tag-gone"); err != nil { + t.Fatal(err) + } + if _, ok, _ := b.h.Fingerprints.Get(FingerprintKey("mesh-sdk", "0.1.19-b")); ok { + t.Errorf("%s: the key stayed after removed", seat) + } + // a version stored while the state is at its cap is refused and nothing is stored + b = newBed(true) + b.h.Fingerprints.(*memFingerprints).full = true + p = pub("mesh-sdk", "0.1.20", file) + _, _ = b.h.Publish(seat, goBuild, p) + if _, err := b.h.PublishPart(seat, goBuild, p.Transfer, digest(file), 0, true, file); err == nil || !strings.Contains(err.Error(), "fingerprints did not take the key") || len(b.forge.files[seat]) != 0 || len(b.said) != 1 || b.said[0].event != EventRefused { + t.Errorf("%s: a version at the state's cap = %v; said %+v; stored %v", seat, err, b.said, b.forge.files[seat]) + } + } + }) // Design 47 G A-73, A-417: the builder's publish again names the version and the digest and no parts. t.Run("a publish with a digest and no parts is published when held, refused as not stored when not", func(t *testing.T) { for _, seat := range []string{NpmSeat, GoSeat} { @@ -367,11 +488,11 @@ func TestTheForgeHoldsBothPackageSeats(t *testing.T) { !strings.Contains(err.Error(), "send the part again") || len(b.said) != 0 || len(b.forge.files[NpmSeat]) != 0 { t.Errorf("a part with other bytes: %v, said %+v", err, b.said) } - // The same version published with other content is refused, never uploaded over. + // The same version published with other content is answered other-bytes, never uploaded over (H-A-729). b = newBed(true) b.forge.files[NpmSeat] = map[string][]byte{"x@0.1.0": []byte("first")} - if _, err := b.h.Publish(NpmSeat, nodeBuild, pub("x", "0.1.0", file)); err != nil || len(b.said) != 1 || b.said[0].event != EventRefused { - t.Errorf("a published version with other content: %v, said %+v", err, b.said) + if a, err := b.h.Publish(NpmSeat, nodeBuild, pub("x", "0.1.0", file)); err != nil || a.OtherBytes == nil || a.OtherBytes.Digest != digest([]byte("first")) || len(b.said) != 0 || string(b.forge.files[NpmSeat]["x@0.1.0"]) != "first" { + t.Errorf("a published version with other content: %+v, %v, said %+v", a, err, b.said) } }) t.Run("the builder removes a tagged version; one already gone is said removed all the same", func(t *testing.T) { diff --git a/modules/gitea/module.json b/modules/gitea/module.json index 7d11eb504..2c5fc6742 100644 --- a/modules/gitea/module.json +++ b/modules/gitea/module.json @@ -674,6 +674,10 @@ "name": "transfers", "max-mib": 8 }, + { + "name": "fingerprints", + "max-mib": 16 + }, { "name": "idle", "key-ttl": true,