enginereads: a statement older than an event heard does not stand (issue 1975)

Builds: issue 1975 note 4 (the lead's handover; no design line)
This commit is contained in:
2026-11-12 10:46:28 +01:00
parent 6a914b1d17
commit e575659187
2 changed files with 137 additions and 2 deletions
@@ -8,6 +8,8 @@ import (
"strings"
"testing"
"time"
"git.novox.be/novox/mesh-contracts/go/seats/nodeengine"
)
// The answer and the event are judged as the contract lays them out and its reader checks: the layout's version, a node
@@ -210,3 +212,64 @@ func must(b []byte, err error) []byte {
}
return b
}
// Issue 1975: an engine states its bootstrapped seats, then, a moment later, the full list, and says so with an event.
// An answer taken before the full statement must not stand: the node reads as the event says at once, and is asked until
// its statement is at least as new; an engine that keeps answering older than its own event is asked a bounded number of
// times and not looped on.
func TestAStatementOlderThanAHeardEventDoesNotStand(t *testing.T) {
const early, late = "2026-11-12T01:14:36.498Z", "2026-11-12T01:14:39.001Z"
event := nodeengine.ManifestReadsChangedEvent{V: 1, Node: "ace", Why: ReadsStarted, At: late, ManifestReads: []string{"seat:a", "seat:b"}}
answer := func(reads, at string) json.RawMessage {
return json.RawMessage(`{"v":1,"node":"ace","manifest-reads":[` + reads + `],"stated-at":"` + at + `"}`)
}
// The engine's first answer is the bootstrapped one, taken while the event is heard (the event says the full list).
calls := 0
k := &Keeper{Sync: true, Call: func(string, string, any) (json.RawMessage, error) {
calls++
if calls == 1 {
return answer(`"seat:a"`, early), nil
}
return answer(`"seat:a","seat:b"`, late), nil
}}
k.AskNode("ace", "the assigner started")
k.HeardChanged(event)
if got := k.Reads([]string{"ace"})["ace"]; len(got) != 2 {
t.Fatalf("a statement older than the event heard stands: reads %v", got)
}
if got := k.Present([]string{"ace"})["ace"]; len(got) != 2 {
t.Errorf("present reads %v, want the event's", got)
}
k.AskNode("ace", nodeengine.EventManifestReadsChanged)
if s := k.Statements()["ace"]; len(s.ManifestReads) != 2 || calls != 2 {
t.Errorf("after the event's ask: %v, %d calls", s.ManifestReads, calls)
}
// An engine that answers the old statement to every ask: the event's reads stand, the asks are bounded.
calls = 0
stuck := &Keeper{Sync: true, Call: func(string, string, any) (json.RawMessage, error) {
calls++
return answer(`"seat:a"`, early), nil
}}
stuck.HeardChanged(event)
stuck.AskNode("ace", nodeengine.EventManifestReadsChanged)
if calls != 1+MaxCatchUp {
t.Errorf("asked %d times, want %d", calls, 1+MaxCatchUp)
}
if got := stuck.Reads([]string{"ace"})["ace"]; len(got) != 2 {
t.Errorf("reads %v, want the event's", got)
}
// The statement the event describes does not count as behind it: no ask beyond the one.
calls = 0
even := &Keeper{Sync: true, Call: func(string, string, any) (json.RawMessage, error) {
calls++
return answer(`"seat:a","seat:b"`, early), nil
}}
even.HeardChanged(event)
even.AskNode("ace", "x")
if calls != 1 {
t.Errorf("a statement that says what the event says was asked %d times", calls)
}
}