Compare commits

..

5 Commits

Author SHA1 Message Date
fiatjaf
255f7bc827 delete all previous replaceable events by default. 2024-04-10 21:34:23 -03:00
fiatjaf
3214dac302 fix pre-search on policies. 2024-03-30 14:23:17 -03:00
fiatjaf
5efadf6256 do not give away so much. 2024-03-29 18:25:47 -03:00
fiatjaf
27d6769009 format last commit. 2024-03-29 18:24:44 -03:00
Sebastix
44baacac42 * sort kinds before the binary search is run
* optimized return messages with more context why the policy blocks an event
2024-03-29 18:24:21 -03:00
2 changed files with 21 additions and 10 deletions

View File

@@ -38,9 +38,11 @@ func (rl *Relay) AddEvent(ctx context.Context, evt *nostr.Event) error {
if err != nil {
continue
}
if previous := <-ch; previous != nil && isOlder(previous, evt) {
for _, del := range rl.DeleteEvent {
del(ctx, previous)
for previous := range ch {
if isOlder(previous, evt) {
for _, del := range rl.DeleteEvent {
del(ctx, previous)
}
}
}
}
@@ -53,9 +55,11 @@ func (rl *Relay) AddEvent(ctx context.Context, evt *nostr.Event) error {
if err != nil {
continue
}
if previous := <-ch; previous != nil && isOlder(previous, evt) {
for _, del := range rl.DeleteEvent {
del(ctx, previous)
for previous := range ch {
if isOlder(previous, evt) {
for _, del := range rl.DeleteEvent {
del(ctx, previous)
}
}
}
}

View File

@@ -2,7 +2,7 @@ package policies
import (
"context"
"fmt"
"slices"
"github.com/nbd-wtf/go-nostr"
@@ -14,6 +14,9 @@ import (
// If ignoreKinds is given this restriction will not apply to these kinds (useful for allowing a bigger).
// If onlyKinds is given then all other kinds will be ignored.
func PreventTooManyIndexableTags(max int, ignoreKinds []int, onlyKinds []int) func(context.Context, *nostr.Event) (bool, string) {
slices.Sort(ignoreKinds)
slices.Sort(onlyKinds)
ignore := func(kind int) bool { return false }
if len(ignoreKinds) > 0 {
ignore = func(kind int) bool {
@@ -74,21 +77,25 @@ func RestrictToSpecifiedKinds(kinds ...uint16) func(context.Context, *nostr.Even
}
}
// sort the kinds in increasing order
slices.Sort(kinds)
return func(ctx context.Context, event *nostr.Event) (reject bool, msg string) {
// these are cheap and very questionable optimizations, but they exist for a reason:
// we would have to ensure that the kind number is within the bounds of a uint16 anyway
if event.Kind > max {
return true, "event kind not allowed"
return true, fmt.Sprintf("event kind not allowed (it should be lower than %d)", max)
}
if event.Kind < min {
return true, "event kind not allowed"
return true, fmt.Sprintf("event kind not allowed (it should be higher than %d)", min)
}
// hopefully this map of uint16s is very fast
if _, allowed := slices.BinarySearch(kinds, uint16(event.Kind)); allowed {
return false, ""
}
return true, "event kind not allowed"
return true, fmt.Sprintf("received event kind %d not allowed", event.Kind)
}
}