diff --git a/go/process/state.go b/go/process/state.go index 4011ef92..494609be 100644 --- a/go/process/state.go +++ b/go/process/state.go @@ -398,3 +398,28 @@ func (s State) delete(key string) error { } return nil } + +// TakenState is the state a holder of a kept call keeps the ids of the asks it took in (design 47 H2 §775, §820): a +// state of 8 MiB whose keys live the ask's bound and are deleted by their time to live alone, never pruned. +const TakenState = "taken" + +// TakenKey is the key an ask taken from seat is kept under in TakenState: `.`. +func TakenKey(seat, ask string) string { return seat + "." + ask } + +// TakeOnce says whether this holder takes the ask ask of seat now (design 47 H2 §775; ADR 0668 §2): the first time it +// is given, it is kept under TakenKey for the ask's bound and TakeOnce answers true; given again within the bound, +// false, and the holder acts once. s is the holder's TakenState, declared `key-ttl`. An ask with no id, or an id that is +// not one key, is refused: a kept call carries its id. +func (s State) TakeOnce(seat, ask string, bound time.Duration) (bool, error) { + if err := checkAskID(ask); err != nil { + return false, fmt.Errorf("taking an ask of %s: %w", seat, err) + } + if seat == "" || strings.ContainsAny(seat, ".*> ") { + return false, fmt.Errorf("taking the ask %s: %q is not a seat's name", ask, seat) + } + _, err := s.CreateTTL(TakenKey(seat, ask), map[string]string{"seat": seat, "ask": ask}, max(bound, MinTTL)) + if errors.Is(err, stdio.ErrChanged) { + return false, nil + } + return err == nil, err +} diff --git a/go/process/state_test.go b/go/process/state_test.go index 6950504c..5f2321e4 100644 --- a/go/process/state_test.go +++ b/go/process/state_test.go @@ -227,6 +227,29 @@ func TestStatePutWritesAKeyWhole(t *testing.T) { if rev, err := dark.Put("k", 1); err != nil || rev == 0 || blackedOut != 0 { t.Fatalf("a Put through a short blackout: %d, %v", rev, err) } + // Design 47 H2 §775: a holder given the same ask id twice acts once; the id is kept under . with the + // ask's bound as its time to live; an ask with no id, or one that is not one key, is refused. + tkv := newFakeKV() + taken := stateOn(tkv) + acts := 0 + for range 2 { + if first, err := taken.TakeOnce("mesh-builds", "a-7", 10*time.Minute); err != nil { + t.Fatal(err) + } else if first { + acts++ + } + } + if acts != 1 || !tkv.withTTL[TakenKey("mesh-builds", "a-7")] || TakenKey("mesh-builds", "a-7") != "mesh-builds.a-7" { + t.Fatalf("acted %d times, keys with a TTL %v", acts, tkv.withTTL) + } + if first, err := taken.TakeOnce("mesh-wants", "a-7", time.Minute); !first || err != nil { + t.Fatalf("the same id on another seat is another ask: %v, %v", first, err) + } + for _, bad := range [][2]string{{"mesh-builds", ""}, {"mesh-builds", "a.7"}, {"", "a-8"}} { + if _, err := taken.TakeOnce(bad[0], bad[1], time.Minute); err == nil { + t.Errorf("seat %q, ask %q was taken", bad[0], bad[1]) + } + } } // 1840/S1: given a state, when its keys are listed, then they come sorted, and none is an empty list, not an error.