Skip to content

[BUG] Events with identical created_at timestamps returned in non-deterministic order violating NIP-01 #477

@saniddhyaDubey

Description

@saniddhyaDubey

Describe the bug
findByFilters in event-repository.ts orders query results by event_created_at DESC only. When multiple events share the same created_at timestamp, the secondary sort by event_id ASC required by NIP-01 is missing, resulting in non-deterministic ordering of tied events across different clients and relays.

Three concrete scenarios where this manifests:

  1. High frequency publishing - a client publishes multiple events within the same second, two subscribers receive them in different orders from the same relay.
  2. limit: 1 with timestamp collision - two events from the same pubkey have identical created_at, different clients get different "latest" events unpredictably.
  3. Cross-relay inconsistency - same REQ sent to nostream and a NIP-01 compliant relay returns events in different orders, causing inconsistent feeds for the same subscription.

To Reproduce
Steps to reproduce the behavior:

  1. Publish two or more events with identical created_at timestamps to the relay
  2. Send a REQ message with a limit filter: ["REQ", "sub1", { "limit": n }]
  3. Observe that the order of events with the same created_at is non-deterministic and inconsistent across different clients or relays

Expected behavior
Per NIP-01, when two or more events share the same created_at timestamp, they must be returned ordered by event_id ASC (lexicographical order) as a tiebreaker. The same REQ with the same filters should return events in the same deterministic order across all clients and relays.

System:

  • OS: N/A — code-level bug, not system specific
  • Platform: docker, standalone
  • Version: v2.1.0

Logs
N/A — bug produces no error logs, only incorrect event ordering in query results.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions