Skip to content

Conversation

@dskvr
Copy link

@dskvr dskvr commented Nov 20, 2025

NIP-91 AND tag filters

Intent

  • Implement NIP-91-style AND semantics for tag filters using &<tag> keys while preserving existing OR behavior for #<tag>.
  • Keep the DB query engine, monitor engine and Perl reference filter in sync on tag semantics without regressing performance.

Changes

  • src/filters.h:
    • Extend NostrFilter with tagsAnd to represent ANDed tag values and track unique tag keys across # and &.
    • Parse &<tag> keys, hex-decode &e/&p, and normalise values via FilterSetBytes, de-duplicating overlaps so values present in & are removed from the corresponding #.
    • Adjust indexOnlyScans so presence of any AND tag forces full-event scans and keeps the 3-tag-key limit based on the union of # and & keys.
  • src/DBQuery.h:
    • Allow tag-based scans to be driven by either tags or tagsAnd, choosing the most selective tag key.
    • When scanning by an AND tag, use only one value from the AND set for the LMDB cursor and disable index-only mode (full events are re-checked against the remaining AND values).
  • src/ActiveMonitors.h:
    • Include tagsAnd alongside tags in the tag monitor index so subscription monitors receive events that satisfy AND tag filters.
  • test/dumbFilter.pl:
    • Replace hard-coded #e/#p/#t logic with generic OR (#<tag>) and AND (&<tag>) handling to match the NostrFilter implementation, including ignoring values that appear in both AND and OR for the same tag key.
  • test/filterFuzzTest.pl:
    • Extend fuzzed filters to include &t, &e and &p so scan and monitor fuzz tests cover NIP-91 AND-tag semantics.

Testing

From the project root:

  • Build the binary: make.
  • Populate a test database as described in test/README.md (for example, import the wellordered 500k dataset).
  • Run the existing fuzz tests, which exercise the new AND-tag logic:
    • perl test/filterFuzzTest.pl scan-limit
    • perl test/filterFuzzTest.pl scan
    • perl test/filterFuzzTest.pl monitor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant