Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/delete-event-linear-id-collection.md
Original file line number Diff line number Diff line change
@@ -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.
17 changes: 11 additions & 6 deletions src/handlers/event-strategies/delete-event-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@ export class DeleteEventStrategy implements IEventStrategy<Event, Promise<void>>

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<string>()
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)
Expand Down
60 changes: 60 additions & 0 deletions test/unit/handlers/event-strategies/delete-event-strategy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand Down
Loading