The node-engine seat's manifest-reads answer and event come from mesh-contracts' nodeengine contract, judged by their reader (ADR 0618 §3, §4, issue 774)
The contract carries no checks, so sdk/enginereads judges the layout's version, the node, each capability, the reason and the times. The local replaces of mesh-sdk are gone: the lead pins mesh-sdk and mesh-contracts at the join. Claude-Session: https://claude.ai/code/session_01JG73KG7AfSdh6FP8VrBqCn
This commit is contained in:
@@ -18,19 +18,99 @@ import (
|
||||
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
"git.novox.be/novox/mesh-sdk/go/busretry"
|
||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
||||
"git.novox.be/novox/mesh-sdk/go/delivery"
|
||||
|
||||
"git.novox.be/novox/mesh-contracts/go/seats/nodeengine"
|
||||
)
|
||||
|
||||
// The verb and the event as a manifest names them, and the subject the event is said on with the engine's machine last.
|
||||
// The verb and the event as the seat's contract names them (mesh-contracts seats/nodeengine), as a manifest names them,
|
||||
// and the subject the event is said on with the engine's machine last.
|
||||
const (
|
||||
Verb = "manifest-reads"
|
||||
Event = "manifest-reads-changed"
|
||||
Invoke = "seat:" + catalogue.EngineSeat + "." + Verb
|
||||
Consumes = catalogue.EngineSeat + "." + Event
|
||||
SubjectPrefix = "mesh.seat." + catalogue.EngineSeat + ".event." + Event + "."
|
||||
Verb = nodeengine.VerbManifestReads
|
||||
Event = nodeengine.EventManifestReadsChanged
|
||||
Invoke = "seat:" + nodeengine.Seat + "." + Verb
|
||||
Consumes = nodeengine.Seat + "." + Event
|
||||
)
|
||||
|
||||
// SubjectPrefix is the event's subject up to the machine, its last token.
|
||||
var SubjectPrefix = strings.TrimSuffix(nodeengine.SubjectManifestReadsChanged("*"), "*")
|
||||
|
||||
// The reasons an engine says its reads, in the event's why (ADR 0618 §4).
|
||||
const (
|
||||
// ReadsStarted: the engine started, or what it states changed.
|
||||
ReadsStarted = "started"
|
||||
// ReadsReturned: the engine's node returned after being away.
|
||||
ReadsReturned = "returned"
|
||||
)
|
||||
|
||||
// ContractVersion is the layout version of the verb's answer and the event's body this reader reads.
|
||||
const ContractVersion = 1
|
||||
|
||||
// checkNode refuses a node that is no subject token.
|
||||
func checkNode(node string) error {
|
||||
if node == "" || strings.ContainsAny(node, ".*> \t\n") {
|
||||
return fmt.Errorf("%q is no node", node)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// checkReads refuses an empty reader capability.
|
||||
func checkReads(node string, reads []string) error {
|
||||
for _, c := range reads {
|
||||
if strings.TrimSpace(c) == "" {
|
||||
return fmt.Errorf("the reads of %s name an empty reader capability", node)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DecodeAnswer reads the verb's answer as the contract lays it out, and judges it: the layout's version, the node a
|
||||
// name, no empty capability, and its stated-at an RFC 3339 time (the contract carries no checks; its reader does).
|
||||
func DecodeAnswer(raw []byte) (nodeengine.ManifestReadsAnswer, time.Time, error) {
|
||||
var a nodeengine.ManifestReadsAnswer
|
||||
if err := json.Unmarshal(raw, &a); err != nil {
|
||||
return a, time.Time{}, err
|
||||
}
|
||||
if a.V != ContractVersion {
|
||||
return a, time.Time{}, fmt.Errorf("the answer's layout is version %d, not %d", a.V, ContractVersion)
|
||||
}
|
||||
if err := checkNode(a.Node); err != nil {
|
||||
return a, time.Time{}, err
|
||||
}
|
||||
if err := checkReads(a.Node, a.ManifestReads); err != nil {
|
||||
return a, time.Time{}, err
|
||||
}
|
||||
at, err := time.Parse(time.RFC3339Nano, a.StatedAt)
|
||||
if err != nil {
|
||||
return a, time.Time{}, fmt.Errorf("stated-at %q is no RFC 3339 time", a.StatedAt)
|
||||
}
|
||||
return a, at, nil
|
||||
}
|
||||
|
||||
// DecodeChanged reads the event's body as the contract lays it out, and judges it as DecodeAnswer does, its why one of
|
||||
// ReadsStarted and ReadsReturned.
|
||||
func DecodeChanged(raw []byte) (nodeengine.ManifestReadsChangedEvent, error) {
|
||||
var e nodeengine.ManifestReadsChangedEvent
|
||||
if err := json.Unmarshal(raw, &e); err != nil {
|
||||
return e, err
|
||||
}
|
||||
if e.V != ContractVersion {
|
||||
return e, fmt.Errorf("the event's layout is version %d, not %d", e.V, ContractVersion)
|
||||
}
|
||||
if err := checkNode(e.Node); err != nil {
|
||||
return e, err
|
||||
}
|
||||
if e.Why != ReadsStarted && e.Why != ReadsReturned {
|
||||
return e, fmt.Errorf("the reads of %s changed for %q, not %s or %s", e.Node, e.Why, ReadsStarted, ReadsReturned)
|
||||
}
|
||||
if err := checkReads(e.Node, e.ManifestReads); err != nil {
|
||||
return e, err
|
||||
}
|
||||
if _, err := time.Parse(time.RFC3339Nano, e.At); err != nil {
|
||||
return e, fmt.Errorf("at %q is no RFC 3339 time", e.At)
|
||||
}
|
||||
return e, nil
|
||||
}
|
||||
|
||||
// Where a node's statement came from.
|
||||
const (
|
||||
// FromVerb: the node's engine answered the verb.
|
||||
@@ -101,12 +181,15 @@ func Ask(call Caller, node string, now time.Time) Statement {
|
||||
var answered *busretry.Answered
|
||||
switch {
|
||||
case err == nil:
|
||||
a, derr := delivery.DecodeManifestReadsAnswer(raw)
|
||||
a, at, derr := DecodeAnswer(raw)
|
||||
if derr == nil && a.Node != node {
|
||||
derr = fmt.Errorf("it names the node %s", a.Node)
|
||||
}
|
||||
if derr != nil {
|
||||
s.Answered, s.Words = true, "the engine answered what is no statement: "+derr.Error()
|
||||
return s
|
||||
}
|
||||
s.ManifestReads, s.StatedAt, s.From, s.Answered = slices.Clone(a.ManifestReads), a.StatedAt.UTC(), FromVerb, true
|
||||
s.ManifestReads, s.StatedAt, s.From, s.Answered = slices.Clone(a.ManifestReads), at.UTC(), FromVerb, true
|
||||
case errors.As(err, &answered):
|
||||
s.Answered, s.Words = true, "the engine does not serve "+Verb+": "+answered.Words
|
||||
default:
|
||||
@@ -122,7 +205,7 @@ func NodeOf(subject string, data []byte) (string, bool, error) {
|
||||
if !ok || node == "" || strings.Contains(node, ".") {
|
||||
return "", false, nil
|
||||
}
|
||||
e, err := delivery.DecodeManifestReadsChanged(data)
|
||||
e, err := DecodeChanged(data)
|
||||
if err != nil {
|
||||
return node, true, err
|
||||
}
|
||||
|
||||
@@ -13,4 +13,3 @@ require (
|
||||
golang.org/x/sys v0.48.0 // indirect
|
||||
)
|
||||
|
||||
replace git.novox.be/novox/mesh-sdk/go => ../../0618-sdk/go
|
||||
|
||||
Reference in New Issue
Block a user