diff --git a/go/buscalls/buscalls.go b/go/buscalls/buscalls.go index e127c4df..83df3b6b 100644 --- a/go/buscalls/buscalls.go +++ b/go/buscalls/buscalls.go @@ -333,6 +333,9 @@ type InstanceState struct { Instance string `json:"instance"` State json.RawMessage `json:"state"` Sequence uint64 `json:"sequence"` + // Expires is when the instance's state key goes, its end plus the ledger's ended-days; absent while the instance has + // not ended (design 47 H2 H-A-634 (4)). + Expires *time.Time `json:"expires,omitempty"` } // HistoryRequest reads an instance's operations from a sequence on, at most Limit of them. @@ -377,6 +380,8 @@ type InstanceChange struct { State json.RawMessage `json:"state"` Sequence uint64 `json:"sequence"` Current bool `json:"current"` + // Expires is as InstanceState's (design 47 H2 H-A-634 (4)). + Expires *time.Time `json:"expires,omitempty"` } // QueryRequest reads a declared index of a ledger: the entries whose key Key names (a key, with `*` for one name and @@ -449,6 +454,9 @@ type RebuildRequest struct { type ReindexRequest struct { Ledger string `json:"ledger"` Index string `json:"index"` + // Instance, when set, builds only that instance's entries again and deletes the ones the index holds for it that it no + // longer should (design 47 H2 H-A-690 (1)). + Instance string `json:"instance,omitempty"` } // LedgerRequest names a ledger: lag, fill. diff --git a/go/buscalls/buscalls_test.go b/go/buscalls/buscalls_test.go index 01cf18d4..2f5a823d 100644 --- a/go/buscalls/buscalls_test.go +++ b/go/buscalls/buscalls_test.go @@ -3,6 +3,7 @@ package buscalls import ( "encoding/json" "testing" + "time" ) // A-42: stream.publish is a method of its own, beside read and fill, and its request and answer keep their field names. @@ -19,3 +20,35 @@ func TestStreamPublishIsAMethodWithItsShapes(t *testing.T) { t.Errorf("answer %s", a) } } + +// Design 47 H2 H-A-634 (4), H-A-690 (1): an instance's state and a change carry `expires` while it has ended, none +// while it has not; a reindex names an instance only when asked to. +func TestAnInstancesExpiresAndAReindexInstanceKeepTheirNames(t *testing.T) { + at := time.Date(2026, 12, 1, 10, 0, 0, 0, time.UTC) + for _, c := range []struct { + name string + v any + want string + }{ + {"state ended", InstanceState{Instance: "record/a", State: json.RawMessage(`{}`), Sequence: 3, Expires: &at}, + `{"instance":"record/a","state":{},"sequence":3,"expires":"2026-12-01T10:00:00Z"}`}, + {"state open", InstanceState{Instance: "record/a", State: json.RawMessage(`{}`), Sequence: 3}, + `{"instance":"record/a","state":{},"sequence":3}`}, + {"change ended", InstanceChange{Watch: "w", Instance: "i", State: json.RawMessage(`{}`), Expires: &at}, + `{"watch":"w","instance":"i","state":{},"sequence":0,"current":false,"expires":"2026-12-01T10:00:00Z"}`}, + {"change open", InstanceChange{Watch: "w", Instance: "i", State: json.RawMessage(`{}`)}, + `{"watch":"w","instance":"i","state":{},"sequence":0,"current":false}`}, + {"reindex one", ReindexRequest{Ledger: "records", Index: "movers", Instance: "mover/x"}, + `{"ledger":"records","index":"movers","instance":"mover/x"}`}, + {"reindex whole", ReindexRequest{Ledger: "records", Index: "movers"}, `{"ledger":"records","index":"movers"}`}, + } { + b, err := json.Marshal(c.v) + if err != nil || string(b) != c.want { + t.Errorf("%s: %s (%v), want %s", c.name, b, err, c.want) + } + } + var back InstanceState + if err := json.Unmarshal([]byte(`{"instance":"i","state":null,"sequence":1,"expires":"2026-12-01T10:00:00Z"}`), &back); err != nil || back.Expires == nil || !back.Expires.Equal(at) { + t.Errorf("expires read back as %+v (%v)", back.Expires, err) + } +} diff --git a/go/catalogue/busstate.go b/go/catalogue/busstate.go index 1e1040b4..d4731262 100644 --- a/go/catalogue/busstate.go +++ b/go/catalogue/busstate.go @@ -61,6 +61,10 @@ const ( OptionDuplicates = "duplicates" ) +// ConfigEndedDays is the configuration name of a ledger that gives its ended instances a lifetime: a whole number of +// days, 1 to 3650, after an instance's end until its state key, snapshot and index keys go (design 47 H2 H-A-634 (3)). +const ConfigEndedDays = "ended-days" + // ConfigWhenFull is what a configuration of its own says a bare stream does when full: WhenFullRefuse or // WhenFullDropOldest. In a template it is fixed, never an option (ADR 0595 §3). const ( @@ -92,6 +96,10 @@ type LedgerType struct { Operations []LedgerOperation `json:"operations"` // Indexes are its declared indexes, each a second key-value bucket kept by a watcher (ADR 0597 §4). Indexes []LedgerIndex `json:"indexes,omitempty"` + // EndsWith names the field of the type's schema whose value ends an instance: an instance has ended while its state + // holds a value there (absent, null, an empty object and an empty text are no value). Absent: its instances never end + // (design 47 H2 H-A-634 (1), (3)). + EndsWith string `json:"ends-with,omitempty"` } // LedgerField is one field of a ledger type's schema. diff --git a/go/catalogue/optional_requirements_test.go b/go/catalogue/optional_requirements_test.go index d98e8db5..53ca1150 100644 --- a/go/catalogue/optional_requirements_test.go +++ b/go/catalogue/optional_requirements_test.go @@ -42,7 +42,7 @@ func TestARequirementsLongForm(t *testing.T) { records := BusStateEntry{Name: "records", Storage: "ledger", Template: "ledger", Options: map[string]json.RawMessage{"max-mib": json.RawMessage(`1024`)}} // A ledger type with each of the four effects. - listing := LedgerType{Name: "listing", + listing := LedgerType{Name: "listing", EndsWith: "sold", Schema: map[string]LedgerField{"price": {Type: "number"}, "seen": {Type: "list"}, "notes": {Type: "map"}, "text": {Type: "text"}, "digest": {Type: "text", DigestOf: "text"}}, Operations: []LedgerOperation{ @@ -53,8 +53,8 @@ func TestARequirementsLongForm(t *testing.T) { }, Indexes: []LedgerIndex{{Name: "cheapest", On: []string{"price"}}}} typeJSON, err := json.Marshal(listing) - if err != nil { - t.Fatal(err) + if err != nil || !strings.Contains(string(typeJSON), `"ends-with":"sold"`) || ConfigEndedDays != "ended-days" { + t.Fatalf("a ledger type's ends-with and the configuration name ended-days (H-A-634 (3)): %s (%v)", typeJSON, err) } ledger := records ledger.Types = []LedgerType{listing} diff --git a/go/membership/membership.go b/go/membership/membership.go index 7fdaab2f..080307f3 100644 --- a/go/membership/membership.go +++ b/go/membership/membership.go @@ -91,6 +91,9 @@ type BusBinding struct { State string `json:"state,omitempty"` Indexes map[string]string `json:"indexes,omitempty"` Snapshot string `json:"snapshot,omitempty"` + // EndedDays is the whole number of days an ended instance's state, snapshot and index keys are kept; absent while the + // held objects allow no lifetime or the ledger names none (design 47 H2 H-A-634 (3), H-A-685 (1)). + EndedDays int `json:"ended-days,omitempty"` // Types are a ledger's types, copied by the grants part from the entry the manifest-accepted event carries, so the // tool runner checks an operation against its type without reading another part's state (ADR 0596, ADR 0597 §1). Types []catalogue.LedgerType `json:"types,omitempty"` diff --git a/go/membership/membership_test.go b/go/membership/membership_test.go index c583f435..e6898710 100644 --- a/go/membership/membership_test.go +++ b/go/membership/membership_test.go @@ -44,8 +44,8 @@ func TestAMembershipRoundTripsAsIssued(t *testing.T) { bound := in bound.Bindings = []BusBinding{{Local: "records", Storage: "ledger", Template: "ledger", History: "forge_records", Subject: "forge_records.>", State: "forge_records_state", Indexes: map[string]string{"search": "forge_records_search"}, - Snapshot: "forge_records_snapshot.>", - Types: []catalogue.LedgerType{{Name: "decision", Schema: map[string]catalogue.LedgerField{"title": {Type: "text"}}, + Snapshot: "forge_records_snapshot.>", EndedDays: 30, + Types: []catalogue.LedgerType{{Name: "decision", EndsWith: "ended", Schema: map[string]catalogue.LedgerField{"title": {Type: "text"}}, Operations: []catalogue.LedgerOperation{{Name: "propose", Effect: catalogue.EffectSetFields, Fields: []string{"title"}}}}}}} boundRaw, err := json.Marshal(bound) if err != nil { @@ -55,6 +55,17 @@ func TestAMembershipRoundTripsAsIssued(t *testing.T) { if err := json.Unmarshal(boundRaw, &boundBack); err != nil || !reflect.DeepEqual(bound, boundBack) { t.Fatalf("a membership with a ledger binding read back as %+v (%v)", boundBack, err) } + // Design 47 H2 H-A-634 (3), H-A-685 (1): ended-days and ends-with are named only when set, so a binding without + // them encodes as before. + if !strings.Contains(string(boundRaw), `"ended-days":30`) || !strings.Contains(string(boundRaw), `"ends-with":"ended"`) { + t.Fatalf("the binding does not name ended-days and ends-with: %s", boundRaw) + } + plain := bound + plain.Bindings = []BusBinding{{Local: "records", Storage: "ledger", Types: []catalogue.LedgerType{{Name: "decision"}}}} + plainRaw, _ := json.Marshal(plain) + if strings.Contains(string(plainRaw), "ended-days") || strings.Contains(string(plainRaw), "ends-with") { + t.Fatalf("an absent ended-days or ends-with is written: %s", plainRaw) + } // Design 47 G §642, §619: a state's binding carries its objects store, the sent store is an entry of its own, and a // foundation part's old `objects` sections map to their stores. withObjects := in