package main import ( "crypto/sha256" "encoding/hex" "encoding/json" "fmt" catalog "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshcatalog" "git.novox.be/novox/mesh-sdk/go/catalogue" "nats-tools/internal/keeper" ) // The keeper reads a module's asks from the module's entry in the catalog half of its revision view, not from the // indexer's event (design 47 G §344, §433). // Asks is what one catalog graph entry says of a module for the keeper: what its manifest asks of the bus as an accepted // verdict, the seats it claims, and whether it declares an event of its own (the facts keeper.Sends reads). type Asks struct { Accepted keeper.Accepted Claims []string EmitsOwn bool } // AsksOf reads one entry. An entry whose manifest is not accepted asks nothing of the bus now, as an accepted verdict of // no entries does; the digest it carries is of the entries it asks, so an entry changed in nothing the bus module reads // is not applied again. func AsksOf(e catalog.CatalogEntry) (Asks, error) { if e.Module == "" { return Asks{}, fmt.Errorf("a catalog entry names no module") } raw, err := json.Marshal(e.Manifest) if err != nil { return Asks{}, fmt.Errorf("the manifest of %s is not readable: %w", e.Module, err) } var m catalogue.Manifest if err := json.Unmarshal(raw, &m); err != nil { return Asks{}, fmt.Errorf("the manifest of %s is not readable: %w", e.Module, err) } state := m.BusState if e.Verdict != "" && e.Verdict != "accepted" { state = nil } sum := sha256.Sum256(mustJSON(state)) out := Asks{Accepted: keeper.Accepted{Module: e.Module, Digest: hex.EncodeToString(sum[:]), BusState: state}, EmitsOwn: len(m.Emits) > 0 || len(e.Streams) > 0} for _, c := range m.Claims { out.Claims = append(out.Claims, c.Name) } return out, nil } func mustJSON(v any) []byte { b, _ := json.Marshal(v) return b }