From c0934e0639f79309f86544ee7295860c23f8ae98 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Thu, 8 May 2025 09:32:54 -0300 Subject: [PATCH] since and until are not pointers anymore because that is too annoying. --- eventstore/badger/query_planner.go | 8 ++++---- eventstore/bluge/query.go | 10 +++++----- eventstore/lmdb/query_planner.go | 8 ++++---- eventstore/mmm/query_planner.go | 8 ++++---- eventstore/slicestore/lib.go | 8 ++++---- filter.go | 24 ++++++++---------------- filter_easyjson.go | 22 ++++++++-------------- helpers.go | 10 ---------- keyer/plain.go | 4 ++-- khatru/deleting.go | 2 +- nip17/nip17.go | 2 +- nip45/hll_filter.go | 2 +- nip46/client.go | 3 +-- nip60/wallet_test.go | 7 ++----- paginator.go | 8 ++++---- pool.go | 3 +-- sdk/feeds.go | 11 +++++------ 17 files changed, 55 insertions(+), 85 deletions(-) diff --git a/eventstore/badger/query_planner.go b/eventstore/badger/query_planner.go index 0d05e9b..946f4c0 100644 --- a/eventstore/badger/query_planner.go +++ b/eventstore/badger/query_planner.go @@ -27,8 +27,8 @@ func prepareQueries(filter nostr.Filter) ( } var until uint32 = 4294967295 - if filter.Until != nil { - if fu := uint32(*filter.Until); fu < until { + if filter.Until != 0 { + if fu := uint32(filter.Until); fu < until { until = fu + 1 } } @@ -38,8 +38,8 @@ func prepareQueries(filter nostr.Filter) ( } // this is where we'll end the iteration - if filter.Since != nil { - if fs := uint32(*filter.Since); fs > since { + if filter.Since != 0 { + if fs := uint32(filter.Since); fs > since { since = fs } } diff --git a/eventstore/bluge/query.go b/eventstore/bluge/query.go index 59c6f6a..c1e561f 100644 --- a/eventstore/bluge/query.go +++ b/eventstore/bluge/query.go @@ -54,14 +54,14 @@ func (b *BlugeBackend) QueryEvents(filter nostr.Filter) iter.Seq[nostr.Event] { q = complicatedQuery } - if filter.Since != nil || filter.Until != nil { + if filter.Since != 0 || filter.Until != 0 { min := 0.0 - if filter.Since != nil { - min = float64(*filter.Since) + if filter.Since != 0 { + min = float64(filter.Since) } max := float64(nostr.Now()) - if filter.Until != nil { - max = float64(*filter.Until) + if filter.Until != 0 { + max = float64(filter.Until) } dateRangeQ := bluge.NewNumericRangeInclusiveQuery(min, max, true, true) dateRangeQ.SetField(createdAtField) diff --git a/eventstore/lmdb/query_planner.go b/eventstore/lmdb/query_planner.go index 5b8940d..383c5d8 100644 --- a/eventstore/lmdb/query_planner.go +++ b/eventstore/lmdb/query_planner.go @@ -36,8 +36,8 @@ func (b *LMDBBackend) prepareQueries(filter nostr.Filter) ( } var until uint32 = 4294967295 - if filter.Until != nil { - if fu := uint32(*filter.Until); fu < until { + if filter.Until != 0 { + if fu := uint32(filter.Until); fu < until { until = fu + 1 } } @@ -62,8 +62,8 @@ func (b *LMDBBackend) prepareQueries(filter nostr.Filter) ( } // this is where we'll end the iteration - if filter.Since != nil { - if fs := uint32(*filter.Since); fs > since { + if filter.Since != 0 { + if fs := uint32(filter.Since); fs > since { since = fs } } diff --git a/eventstore/mmm/query_planner.go b/eventstore/mmm/query_planner.go index a38d2bb..18379f6 100644 --- a/eventstore/mmm/query_planner.go +++ b/eventstore/mmm/query_planner.go @@ -36,8 +36,8 @@ func (il *IndexingLayer) prepareQueries(filter nostr.Filter) ( } var until uint32 = 4294967295 - if filter.Until != nil { - if fu := uint32(*filter.Until); fu < until { + if filter.Until != 0 { + if fu := uint32(filter.Until); fu < until { until = fu + 1 } } @@ -51,8 +51,8 @@ func (il *IndexingLayer) prepareQueries(filter nostr.Filter) ( }() // this is where we'll end the iteration - if filter.Since != nil { - if fs := uint32(*filter.Since); fs > since { + if filter.Since != 0 { + if fs := uint32(filter.Since); fs > since { since = fs } } diff --git a/eventstore/slicestore/lib.go b/eventstore/slicestore/lib.go index d30138d..1cc2a49 100644 --- a/eventstore/slicestore/lib.go +++ b/eventstore/slicestore/lib.go @@ -41,11 +41,11 @@ func (b *SliceStore) QueryEvents(filter nostr.Filter) iter.Seq[nostr.Event] { // efficiently determine where to start and end start := 0 end := len(b.internal) - if filter.Until != nil { - start, _ = slices.BinarySearchFunc(b.internal, *filter.Until, eventTimestampComparator) + if filter.Until != 0 { + start, _ = slices.BinarySearchFunc(b.internal, filter.Until, eventTimestampComparator) } - if filter.Since != nil { - end, _ = slices.BinarySearchFunc(b.internal, *filter.Since, eventTimestampComparator) + if filter.Since != 0 { + end, _ = slices.BinarySearchFunc(b.internal, filter.Since, eventTimestampComparator) } // ham diff --git a/filter.go b/filter.go index ca41555..2a80b48 100644 --- a/filter.go +++ b/filter.go @@ -11,8 +11,8 @@ type Filter struct { Kinds []Kind Authors []PubKey Tags TagMap - Since *Timestamp - Until *Timestamp + Since Timestamp + Until Timestamp Limit int Search string @@ -32,11 +32,11 @@ func (ef Filter) Matches(event Event) bool { return false } - if ef.Since != nil && event.CreatedAt < *ef.Since { + if ef.Since != 0 && event.CreatedAt < ef.Since { return false } - if ef.Until != nil && event.CreatedAt > *ef.Until { + if ef.Until != 0 && event.CreatedAt > ef.Until { return false } @@ -92,11 +92,11 @@ func FilterEqual(a Filter, b Filter) bool { } } - if !arePointerValuesEqual(a.Since, b.Since) { + if a.Since != b.Since { return false } - if !arePointerValuesEqual(a.Until, b.Until) { + if a.Until != b.Until { return false } @@ -117,6 +117,8 @@ func (ef Filter) Clone() Filter { Limit: ef.Limit, Search: ef.Search, LimitZero: ef.LimitZero, + Since: ef.Since, + Until: ef.Until, } if ef.IDs != nil { @@ -140,16 +142,6 @@ func (ef Filter) Clone() Filter { } } - if ef.Since != nil { - since := *ef.Since - clone.Since = &since - } - - if ef.Until != nil { - until := *ef.Until - clone.Until = &until - } - return clone } diff --git a/filter_easyjson.go b/filter_easyjson.go index 4a71483..55b0658 100644 --- a/filter_easyjson.go +++ b/filter_easyjson.go @@ -97,22 +97,16 @@ func easyjson4d398eaaDecodeGithubComNbdWtfGoNostr(in *jlexer.Lexer, out *Filter) case "since": if in.IsNull() { in.Skip() - out.Since = nil + out.Since = 0 } else { - if out.Since == nil { - out.Since = new(Timestamp) - } - *out.Since = Timestamp(in.Int64()) + out.Since = Timestamp(in.Int64()) } case "until": if in.IsNull() { in.Skip() - out.Until = nil + out.Until = 0 } else { - if out.Until == nil { - out.Until = new(Timestamp) - } - *out.Until = Timestamp(in.Int64()) + out.Until = Timestamp(in.Int64()) } case "limit": out.Limit = int(in.Int()) @@ -211,7 +205,7 @@ func easyjson4d398eaaEncodeGithubComNbdWtfGoNostr(out *jwriter.Writer, in Filter out.RawByte(']') } } - if in.Since != nil { + if in.Since != 0 { const prefix string = ",\"since\":" if first { first = false @@ -219,9 +213,9 @@ func easyjson4d398eaaEncodeGithubComNbdWtfGoNostr(out *jwriter.Writer, in Filter } else { out.RawString(prefix) } - out.Int64(int64(*in.Since)) + out.Int64(int64(in.Since)) } - if in.Until != nil { + if in.Until != 0 { const prefix string = ",\"until\":" if first { first = false @@ -229,7 +223,7 @@ func easyjson4d398eaaEncodeGithubComNbdWtfGoNostr(out *jwriter.Writer, in Filter } else { out.RawString(prefix) } - out.Int64(int64(*in.Until)) + out.Int64(int64(in.Until)) } if in.Limit != 0 || in.LimitZero { const prefix string = ",\"limit\":" diff --git a/helpers.go b/helpers.go index c0f8667..0546e91 100644 --- a/helpers.go +++ b/helpers.go @@ -132,16 +132,6 @@ func escapeString(dst []byte, s string) []byte { return dst } -func arePointerValuesEqual[V comparable](a *V, b *V) bool { - if a == nil && b == nil { - return true - } - if a != nil && b != nil { - return *a == *b - } - return false -} - func subIdToSerial(subId string) int64 { n := strings.Index(subId, ":") if n < 0 || n > len(subId) { diff --git a/keyer/plain.go b/keyer/plain.go index cdac081..167e23e 100644 --- a/keyer/plain.go +++ b/keyer/plain.go @@ -20,8 +20,8 @@ type KeySigner struct { // NewPlainKeySigner creates a new KeySigner from a private key. // Returns an error if the private key is invalid. -func NewPlainKeySigner(sec [32]byte) (KeySigner, error) { - return KeySigner{sec, nostr.GetPublicKey(sec), xsync.NewMapOf[nostr.PubKey, [32]byte]()}, nil +func NewPlainKeySigner(sec [32]byte) KeySigner { + return KeySigner{sec, nostr.GetPublicKey(sec), xsync.NewMapOf[nostr.PubKey, [32]byte]()} } // SignEvent signs the provided event with the signer's private key. diff --git a/khatru/deleting.go b/khatru/deleting.go index 7288a9d..31805a9 100644 --- a/khatru/deleting.go +++ b/khatru/deleting.go @@ -41,7 +41,7 @@ func (rl *Relay) handleDeleteRequest(ctx context.Context, evt nostr.Event) error Kinds: []nostr.Kind{nostr.Kind(kind)}, Authors: []nostr.PubKey{author}, Tags: nostr.TagMap{"d": []string{identifier}}, - Until: &evt.CreatedAt, + Until: evt.CreatedAt, } default: continue diff --git a/nip17/nip17.go b/nip17/nip17.go index c18bb9b..34f593c 100644 --- a/nip17/nip17.go +++ b/nip17/nip17.go @@ -156,7 +156,7 @@ func ListenForMessages( for ie := range pool.SubscribeMany(ctx, ourRelays, nostr.Filter{ Kinds: []nostr.Kind{nostr.KindGiftWrap}, Tags: nostr.TagMap{"p": []string{pk.Hex()}}, - Since: &since, + Since: since, }, nostr.SubscriptionOptions{Label: "mydms"}) { rumor, err := nip59.GiftUnwrap( ie.Event, diff --git a/nip45/hll_filter.go b/nip45/hll_filter.go index 0fab375..8b52bfa 100644 --- a/nip45/hll_filter.go +++ b/nip45/hll_filter.go @@ -11,7 +11,7 @@ import ( // // It returns -1 when the filter is not eligible for hyperloglog calculation. func HyperLogLogEventPubkeyOffsetForFilter(filter nostr.Filter) int { - if filter.IDs != nil || filter.Since != nil || filter.Until != nil || filter.Authors != nil || + if filter.IDs != nil || filter.Since != 0 || filter.Until != 0 || filter.Authors != nil || len(filter.Kinds) != 1 || filter.Search != "" || len(filter.Tags) != 1 { // obvious cases in which we won't bother to do hyperloglog stuff return -1 diff --git a/nip46/client.go b/nip46/client.go index 2cc1975..e3c6bb2 100644 --- a/nip46/client.go +++ b/nip46/client.go @@ -109,11 +109,10 @@ func NewBunker( } go func() { - now := nostr.Now() events := pool.SubscribeMany(ctx, relays, nostr.Filter{ Tags: nostr.TagMap{"p": []string{clientPublicKey.Hex()}}, Kinds: []nostr.Kind{nostr.KindNostrConnect}, - Since: &now, + Since: nostr.Now(), LimitZero: true, }, nostr.SubscriptionOptions{ Label: "bunker46client", diff --git a/nip60/wallet_test.go b/nip60/wallet_test.go index 4ee8532..39ee94a 100644 --- a/nip60/wallet_test.go +++ b/nip60/wallet_test.go @@ -19,10 +19,7 @@ import ( func TestWallet(t *testing.T) { ctx := context.Background() - kr, err := keyer.NewPlainKeySigner(nostr.MustSecretKeyFromHex("040cbf11f24b080ad9d8669d7514d9f3b7b1f58e5a6dcb75549352b041656537")) - if err != nil { - t.Fatal(err) - } + kr := keyer.NewPlainKeySigner(nostr.MustSecretKeyFromHex("040cbf11f24b080ad9d8669d7514d9f3b7b1f58e5a6dcb75549352b041656537")) privateKey, _ := btcec.NewPrivateKey() @@ -85,7 +82,7 @@ func TestWallet(t *testing.T) { // wallet metadata event metaEvent := nostr.Event{} - err = w.toEvent(ctx, kr, &metaEvent) + err := w.toEvent(ctx, kr, &metaEvent) require.NoError(t, err) events = append(events, metaEvent) diff --git a/paginator.go b/paginator.go index 5e67336..5365f92 100644 --- a/paginator.go +++ b/paginator.go @@ -11,8 +11,8 @@ func (pool *Pool) PaginatorWithInterval( ) func(ctx context.Context, urls []string, filter Filter, opts SubscriptionOptions) chan RelayEvent { return func(ctx context.Context, urls []string, filter Filter, opts SubscriptionOptions) chan RelayEvent { nextUntil := Now() - if filter.Until != nil { - nextUntil = *filter.Until + if filter.Until != 0 { + nextUntil = filter.Until } globalLimit := uint64(filter.Limit) @@ -29,7 +29,7 @@ func (pool *Pool) PaginatorWithInterval( defer close(globalCh) for { - filter.Until = &nextUntil + filter.Until = nextUntil time.Sleep(interval) keepGoing := false @@ -48,7 +48,7 @@ func (pool *Pool) PaginatorWithInterval( return } - if evt.CreatedAt < *filter.Until { + if evt.CreatedAt < filter.Until { nextUntil = evt.CreatedAt } } diff --git a/pool.go b/pool.go index 7fd0a25..d1f8ff9 100644 --- a/pool.go +++ b/pool.go @@ -492,8 +492,7 @@ func (pool *Pool) subMany( // this means the connection was closed for weird reasons, like the server shut down // so we will update the filters here to include only events seem from now on // and try to reconnect until we succeed - now := Now() - filter.Since = &now + filter.Since = Now() debugLogf("%s reconnecting because sub.Events is broken\n", nm) goto reconnect } diff --git a/sdk/feeds.go b/sdk/feeds.go index cd70778..2cec7d3 100644 --- a/sdk/feeds.go +++ b/sdk/feeds.go @@ -52,10 +52,10 @@ func (sys *System) StreamLiveFeed( serial := 0 - var since *nostr.Timestamp + var since nostr.Timestamp if data, _ := sys.KVStore.Get(latestKey); data != nil { latest = decodeTimestamp(data) - since = &latest + since = latest } filter := nostr.Filter{ @@ -127,7 +127,7 @@ func (sys *System) FetchFeedPage( if until > oldestTimestamp { // we can use our local database - filter.Until = &until + filter.Until = until count := 0 for evt := range sys.Store.QueryEvents(filter) { @@ -150,9 +150,8 @@ func (sys *System) FetchFeedPage( wg.Done() continue } - fUntil := oldestTimestamp + 1 - filter.Until = &fUntil - filter.Since = nil + filter.Until = oldestTimestamp + 1 + filter.Since = 0 for ie := range sys.Pool.FetchMany(ctx, relays, filter, nostr.SubscriptionOptions{ Label: "feedpage", }) {