// Copyright 2026 Beacon Contributors // SPDX-License-Identifier: AGPL-3.0-or-later package polling import "time" // StoredClaim is what the store holds of an observer's claim, of this server's own or foreign. type StoredClaim struct { Foreign bool ClaimedAt time.Time ReceivedAt time.Time Signed bool } // AdmitClaim decides whether a claim may replace what the observer has stored, own and foreign together. The // store calls it under the observer's seat lock, so two claims arriving at once are judged one after the other. // // - A claim must be newer than the stored one of its own kind, and than a stored one of the other kind. The one // exception is a claim of this server's own with the same timestamp as the stored foreign one: the same claim // arrived by two routes, and the route that makes it this server's own wins. // - A claim that follows a different one within ClaimMinInterval is refused whichever kind either is, so // changing sides does not get an observer past the spacing. // - With c.SignedSince set, an unsigned claim is refused while a signed one received after it is stored: an // observer that signs does not stop, so the signature was taken off on the way. func AdmitClaim(c Claim, own bool, receivedAt time.Time, stored []StoredClaim) bool { for _, r := range stored { if !c.SignedSince.IsZero() && !c.Signed && r.Signed && r.ReceivedAt.After(c.SignedSince) { return false } sameClaimOtherRoute := own && r.Foreign && c.Timestamp.Equal(r.ClaimedAt) if sameClaimOtherRoute { continue } if !c.Timestamp.After(r.ClaimedAt) { return false } if r.ReceivedAt.After(receivedAt.Add(-ClaimMinInterval)) { return false } } return true }