diff --git a/.changeset/delete-event-linear-id-collection.md b/.changeset/delete-event-linear-id-collection.md new file mode 100644 index 000000000..0cda2ee8f --- /dev/null +++ b/.changeset/delete-event-linear-id-collection.md @@ -0,0 +1,7 @@ +--- +'nostream': patch +--- + +fix(nip09): collect the ids of a deletion request in one pass + +`DeleteEventStrategy` built the list of event ids from the `e` tags of a kind-5 event with `[...eventIds, tag[1]]` inside a `reduce`, which copies everything collected so far for each tag. An event with several thousand `e` tags (a message can hold about 7,000) therefore blocked the worker for tens of milliseconds before a single id was looked up, and it did so on every such event. The ids are now collected in one pass, and an id that appears more than once is asked for once. Which events are deleted is unchanged. diff --git a/src/handlers/event-strategies/delete-event-strategy.ts b/src/handlers/event-strategies/delete-event-strategy.ts index 562c4a261..60fa66ab5 100644 --- a/src/handlers/event-strategies/delete-event-strategy.ts +++ b/src/handlers/event-strategies/delete-event-strategy.ts @@ -21,13 +21,18 @@ export class DeleteEventStrategy implements IEventStrategy> const isValidETag = (tag: Tag) => tag.length >= 2 && tag[0] === EventTags.Event && /^[0-9a-f]{64}$/.test(tag[1]) - const eventIdsToDelete = event.tags.reduce( - (eventIds, tag) => (isValidETag(tag) ? [...eventIds, tag[1]] : eventIds), - [] as string[], - ) + // One pass over the tags. Building the list with `[...eventIds, tag[1]]` copies everything collected so far for + // each tag, which is quadratic: an event with thousands of `e` tags blocked the worker for tens of milliseconds + // before any of them was looked up. Repeated ids are asked for once. + const eventIdsToDelete = new Set() + for (const tag of event.tags) { + if (isValidETag(tag)) { + eventIdsToDelete.add(tag[1]) + } + } - if (eventIdsToDelete.length) { - await this.eventRepository.deleteByPubkeyAndIds(event.pubkey, eventIdsToDelete) + if (eventIdsToDelete.size) { + await this.eventRepository.deleteByPubkeyAndIds(event.pubkey, [...eventIdsToDelete]) } const count = await this.eventRepository.create(event) diff --git a/test/unit/handlers/event-strategies/delete-event-strategy.spec.ts b/test/unit/handlers/event-strategies/delete-event-strategy.spec.ts index ac4b8be4f..a7cdcc4f4 100644 --- a/test/unit/handlers/event-strategies/delete-event-strategy.spec.ts +++ b/test/unit/handlers/event-strategies/delete-event-strategy.spec.ts @@ -74,6 +74,66 @@ describe('DeleteEventStrategy', () => { ]) }) + it('asks for each id once, in the order it first appears', async () => { + const a = 'a'.repeat(64) + const b = 'b'.repeat(64) + const c = 'c'.repeat(64) + const repeating: Event = { + ...event, + tags: [ + [EventTags.Event, b], + [EventTags.Event, a], + [EventTags.Event, b], + [EventTags.Event, c], + [EventTags.Event, a], + ], + } + + await strategy.execute(repeating) + + expect(eventRepositoryDeleteByPubkeyAndIdsStub).to.have.been.calledOnceWithExactly(event.pubkey, [b, a, c]) + }) + + it('ignores tags that are not a valid e tag', async () => { + const valid = 'd'.repeat(64) + const mixed: Event = { + ...event, + tags: [ + [EventTags.Pubkey, 'e'.repeat(64)], + [EventTags.Event] as any, + [EventTags.Event, 'not-an-id'], + [EventTags.Event, 'D'.repeat(64)], + [EventTags.Event, valid, 'wss://relay.example'], + ], + } + + await strategy.execute(mixed) + + expect(eventRepositoryDeleteByPubkeyAndIdsStub).to.have.been.calledOnceWithExactly(event.pubkey, [valid]) + }) + + it('does not delete anything when no tag is a valid e tag', async () => { + await strategy.execute({ ...event, tags: [[EventTags.Event, 'not-an-id'], [EventTags.Pubkey, 'f'.repeat(64)]] }) + + expect(eventRepositoryDeleteByPubkeyAndIdsStub).not.to.have.been.called + expect(eventRepositoryCreateStub).to.have.been.calledOnce + }) + + it('handles an event with as many e tags as the largest message allows', async () => { + const many = 7000 + const big: Event = { + ...event, + tags: Array.from({ length: many }, (_, i) => [EventTags.Event, i.toString(16).padStart(64, '0')]), + } + + await strategy.execute(big) + + const [, ids] = eventRepositoryDeleteByPubkeyAndIdsStub.firstCall.args + expect(ids).to.have.length(many) + expect(ids[0]).to.equal('0'.repeat(64)) + expect(ids[many - 1]).to.equal((many - 1).toString(16).padStart(64, '0')) + }) + it('does not delete events if there are no e tags', async () => { event.tags = []