From ddb93af09fbc740602da1b1e87ae18682bd4dc87 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Thu, 20 Jun 2024 14:33:25 -0300 Subject: [PATCH 01/49] feat(DittoTables): create EventZapRow --- src/db/DittoTables.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/db/DittoTables.ts b/src/db/DittoTables.ts index aed8c8c2..e56a062e 100644 --- a/src/db/DittoTables.ts +++ b/src/db/DittoTables.ts @@ -7,6 +7,7 @@ export interface DittoTables { author_stats: AuthorStatsRow; event_stats: EventStatsRow; pubkey_domains: PubkeyDomainRow; + event_zaps: EventZapRow; } interface AuthorStatsRow { @@ -69,3 +70,11 @@ interface PubkeyDomainRow { domain: string; last_updated_at: number; } + +interface EventZapRow { + receipt_id: string; + target_event_id: string; + sender_pubkey: string; + amount: number; + comment: string; +} From bac0b488010740becae4d81fbcd2bafddfceccc9 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Thu, 20 Jun 2024 14:36:19 -0300 Subject: [PATCH 02/49] feat: add migration for event_zaps;create idx_event_zaps_id_amount --- src/db/migrations/027_add_zap_events.ts | 26 +++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/db/migrations/027_add_zap_events.ts diff --git a/src/db/migrations/027_add_zap_events.ts b/src/db/migrations/027_add_zap_events.ts new file mode 100644 index 00000000..058e6baf --- /dev/null +++ b/src/db/migrations/027_add_zap_events.ts @@ -0,0 +1,26 @@ +import { Kysely } from 'kysely'; + +export async function up(db: Kysely): Promise { + await db.schema + .createTable('event_zaps') + .ifNotExists() + .addColumn('receipt_id', 'text', (col) => col.primaryKey()) + .addColumn('target_event_id', 'text', (col) => col.notNull()) + .addColumn('sender_pubkey', 'text', (col) => col.notNull()) + .addColumn('amount', 'integer', (col) => col.notNull()) + .addColumn('comment', 'text', (col) => col.notNull()) + .execute(); + + await db.schema + .createIndex('idx_event_zaps_id_amount') + .on('event_zaps') + .column('amount') + .column('target_event_id') + .ifNotExists() + .execute(); +} + +export async function down(db: Kysely): Promise { + await db.schema.dropIndex('idx_event_zaps_id_amount').execute(); + await db.schema.dropTable('event_zaps').execute(); +} From 1b4ebaccd82f5c8eb1763bc0b6891b178719bcba Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Thu, 20 Jun 2024 15:26:08 -0300 Subject: [PATCH 03/49] refactor: resolve import specifier via the active import map --- src/firehose.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/firehose.ts b/src/firehose.ts index 2c776fe4..f715c686 100644 --- a/src/firehose.ts +++ b/src/firehose.ts @@ -3,7 +3,7 @@ import { Stickynotes } from '@soapbox/stickynotes'; import { Storages } from '@/storages.ts'; import { nostrNow } from '@/utils.ts'; -import * as pipeline from './pipeline.ts'; +import * as pipeline from '@/pipeline.ts'; const console = new Stickynotes('ditto:firehose'); From 2d937a7378b4b3e5d4d7899d02fd9d4d721c9974 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Fri, 21 Jun 2024 18:20:10 -0300 Subject: [PATCH 04/49] refactor(event_zaps): rename amount to amount_millisats --- src/db/DittoTables.ts | 2 +- src/db/migrations/027_add_zap_events.ts | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/db/DittoTables.ts b/src/db/DittoTables.ts index e56a062e..863ca61e 100644 --- a/src/db/DittoTables.ts +++ b/src/db/DittoTables.ts @@ -75,6 +75,6 @@ interface EventZapRow { receipt_id: string; target_event_id: string; sender_pubkey: string; - amount: number; + amount_millisats: number; comment: string; } diff --git a/src/db/migrations/027_add_zap_events.ts b/src/db/migrations/027_add_zap_events.ts index 058e6baf..fe2e1d20 100644 --- a/src/db/migrations/027_add_zap_events.ts +++ b/src/db/migrations/027_add_zap_events.ts @@ -3,11 +3,10 @@ import { Kysely } from 'kysely'; export async function up(db: Kysely): Promise { await db.schema .createTable('event_zaps') - .ifNotExists() .addColumn('receipt_id', 'text', (col) => col.primaryKey()) .addColumn('target_event_id', 'text', (col) => col.notNull()) .addColumn('sender_pubkey', 'text', (col) => col.notNull()) - .addColumn('amount', 'integer', (col) => col.notNull()) + .addColumn('amount_millisats', 'integer', (col) => col.notNull()) .addColumn('comment', 'text', (col) => col.notNull()) .execute(); @@ -21,6 +20,6 @@ export async function up(db: Kysely): Promise { } export async function down(db: Kysely): Promise { - await db.schema.dropIndex('idx_event_zaps_id_amount').execute(); + await db.schema.dropIndex('idx_event_zaps_id_amount').ifExists().execute(); await db.schema.dropTable('event_zaps').execute(); } From ec82e14410c57019ac1567a6c56aea8e44a85018 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Fri, 21 Jun 2024 18:35:26 -0300 Subject: [PATCH 05/49] fix: add amount_millisats in event_zaps index --- src/db/migrations/027_add_zap_events.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/db/migrations/027_add_zap_events.ts b/src/db/migrations/027_add_zap_events.ts index fe2e1d20..58b231fa 100644 --- a/src/db/migrations/027_add_zap_events.ts +++ b/src/db/migrations/027_add_zap_events.ts @@ -13,7 +13,7 @@ export async function up(db: Kysely): Promise { await db.schema .createIndex('idx_event_zaps_id_amount') .on('event_zaps') - .column('amount') + .column('amount_millisats') .column('target_event_id') .ifNotExists() .execute(); From 2c08b9a2f0a16a3cf275cd49bc8bcfefec890f55 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Fri, 21 Jun 2024 20:36:59 -0300 Subject: [PATCH 06/49] feat: create scavenger and handle kind 9735 --- src/utils/scavenger.ts | 48 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/utils/scavenger.ts diff --git a/src/utils/scavenger.ts b/src/utils/scavenger.ts new file mode 100644 index 00000000..0ffaa669 --- /dev/null +++ b/src/utils/scavenger.ts @@ -0,0 +1,48 @@ +import { NostrEvent, NSchema as n } from '@nostrify/nostrify'; +import { Kysely } from 'kysely'; +import { z } from 'zod'; + +import { DittoTables } from '@/db/DittoTables.ts'; +import { getAmount } from '@/utils/bolt11.ts'; + +interface ScavengerEventOpts { + savedEvent: Promise; + kysely: Kysely; +} + +/** Consumes the event already stored in the database and uses it to insert into a new custom table, if eligible. + * Scavenger is organism that eats dead or rotting biomass, such as animal flesh or plant material. */ +async function scavengerEvent({ savedEvent, kysely }: ScavengerEventOpts): Promise { + const event = await savedEvent; + if (!event) return; + + switch (event.kind) { + case 9735: + await handleEvent9735(kysely, event); + break; + } +} + +async function handleEvent9735(kysely: Kysely, event: NostrEvent) { + const zapRequestString = event?.tags?.find(([name]) => name === 'description')?.[1]; + if (!zapRequestString) return; + const zapRequest = n.json().pipe(n.event()).optional().catch(undefined).parse(zapRequestString); + if (!zapRequest) return; + + const amountSchema = z.coerce.number().int().nonnegative().catch(0); + const amount_millisats = amountSchema.parse(getAmount(event?.tags.find(([name]) => name === 'bolt11')?.[1])); + if (!amount_millisats || amount_millisats < 1) return; + + const zappedEventId = zapRequest.tags.find(([name]) => name === 'e')?.[1]; + if (!zappedEventId) return; + + await kysely.insertInto('event_zaps').values({ + receipt_id: event.id, + target_event_id: zappedEventId, + sender_pubkey: zapRequest.pubkey, + amount_millisats, + comment: zapRequest.content, + }).execute(); +} + +export { scavengerEvent }; From 1b30f10a9fec1f554d488a88d3825dd8d8f9f6e2 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Fri, 21 Jun 2024 21:39:18 -0300 Subject: [PATCH 07/49] test(scavenger): store valid data into event_zaps table --- src/utils/scavenger.test.ts | 55 +++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/utils/scavenger.test.ts diff --git a/src/utils/scavenger.test.ts b/src/utils/scavenger.test.ts new file mode 100644 index 00000000..a557c5b1 --- /dev/null +++ b/src/utils/scavenger.test.ts @@ -0,0 +1,55 @@ +import { assertEquals } from '@std/assert'; +import { generateSecretKey } from 'nostr-tools'; + +import { genEvent, getTestDB } from '@/test.ts'; +import { scavengerEvent } from '@/utils/scavenger.ts'; + +Deno.test('store one zap receipt in nostr_events; convert it into event_zaps table format and store it', async () => { + await using db = await getTestDB(); + const kysely = db.kysely; + + const sk = generateSecretKey(); + + const event = genEvent({ + 'id': '67b48a14fb66c60c8f9070bdeb37afdfcc3d08ad01989460448e4081eddda446', + 'pubkey': '9630f464cca6a5147aa8a35f0bcdd3ce485324e732fd39e09233b1d848238f31', + 'created_at': 1674164545, + 'kind': 9735, + 'tags': [ + ['p', '32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245'], + ['P', '97c70a44366a6535c145b333f973ea86dfdc2d7a99da618c40c64705ad98e322'], + ['e', '3624762a1274dd9636e0c552b53086d70bc88c165bc4dc0f9e836a1eaf86c3b8'], + [ + 'bolt11', + 'lnbc10u1p3unwfusp5t9r3yymhpfqculx78u027lxspgxcr2n2987mx2j55nnfs95nxnzqpp5jmrh92pfld78spqs78v9euf2385t83uvpwk9ldrlvf6ch7tpascqhp5zvkrmemgth3tufcvflmzjzfvjt023nazlhljz2n9hattj4f8jq8qxqyjw5qcqpjrzjqtc4fc44feggv7065fqe5m4ytjarg3repr5j9el35xhmtfexc42yczarjuqqfzqqqqqqqqlgqqqqqqgq9q9qxpqysgq079nkq507a5tw7xgttmj4u990j7wfggtrasah5gd4ywfr2pjcn29383tphp4t48gquelz9z78p4cq7ml3nrrphw5w6eckhjwmhezhnqpy6gyf0', + ], + [ + 'description', + '{"pubkey":"97c70a44366a6535c145b333f973ea86dfdc2d7a99da618c40c64705ad98e322","content":"","id":"d9cc14d50fcb8c27539aacf776882942c1a11ea4472f8cdec1dea82fab66279d","created_at":1674164539,"sig":"77127f636577e9029276be060332ea565deaf89ff215a494ccff16ae3f757065e2bc59b2e8c113dd407917a010b3abd36c8d7ad84c0e3ab7dab3a0b0caa9835d","kind":9734,"tags":[["e","3624762a1274dd9636e0c552b53086d70bc88c165bc4dc0f9e836a1eaf86c3b8"],["p","32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245"],["relays","wss://relay.damus.io","wss://nostr-relay.wlvs.space","wss://nostr.fmt.wiz.biz","wss://relay.nostr.bg","wss://nostr.oxtr.dev","wss://nostr.v0l.io","wss://brb.io","wss://nostr.bitcoiner.social","ws://monad.jb55.com:8080","wss://relay.snort.social"]]}', + ], + ['preimage', '5d006d2cf1e73c7148e7519a4c68adc81642ce0e25a432b2434c99f97344c15f'], + ], + 'content': '', + }, sk); + + await db.store.event(event); + + // deno-lint-ignore require-await + await scavengerEvent({ savedEvent: (async () => event)(), kysely: kysely }); + + const zapReceipts = await kysely.selectFrom('nostr_events').selectAll().execute(); + const customEventZaps = await kysely.selectFrom('event_zaps').selectAll().execute(); + + assertEquals(zapReceipts.length, 1); // basic check + assertEquals(customEventZaps.length, 1); // basic check + + const expected = { + receipt_id: event.id, + target_event_id: '3624762a1274dd9636e0c552b53086d70bc88c165bc4dc0f9e836a1eaf86c3b8', + sender_pubkey: '97c70a44366a6535c145b333f973ea86dfdc2d7a99da618c40c64705ad98e322', + amount_millisats: 1000000, + comment: '', + }; + + assertEquals(customEventZaps[0], expected); +}); From 771d7f79db09ae8f302b2302e094734c1860c6be Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Fri, 21 Jun 2024 21:45:55 -0300 Subject: [PATCH 08/49] refactor(scavenger): put SQL insert into try-catch block --- src/utils/scavenger.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/utils/scavenger.ts b/src/utils/scavenger.ts index 0ffaa669..d9874cb7 100644 --- a/src/utils/scavenger.ts +++ b/src/utils/scavenger.ts @@ -36,13 +36,17 @@ async function handleEvent9735(kysely: Kysely, event: NostrEvent) { const zappedEventId = zapRequest.tags.find(([name]) => name === 'e')?.[1]; if (!zappedEventId) return; - await kysely.insertInto('event_zaps').values({ - receipt_id: event.id, - target_event_id: zappedEventId, - sender_pubkey: zapRequest.pubkey, - amount_millisats, - comment: zapRequest.content, - }).execute(); + try { + await kysely.insertInto('event_zaps').values({ + receipt_id: event.id, + target_event_id: zappedEventId, + sender_pubkey: zapRequest.pubkey, + amount_millisats, + comment: zapRequest.content, + }).execute(); + } catch { + // receipt_id is unique, do nothing + } } export { scavengerEvent }; From 89b56539d177bcd1816b8928917fbf437b458a3e Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Fri, 21 Jun 2024 22:23:34 -0300 Subject: [PATCH 09/49] test(scavenger): code coverage 100.00% --- src/utils/scavenger.test.ts | 86 +++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/src/utils/scavenger.test.ts b/src/utils/scavenger.test.ts index a557c5b1..9d768999 100644 --- a/src/utils/scavenger.test.ts +++ b/src/utils/scavenger.test.ts @@ -36,6 +36,8 @@ Deno.test('store one zap receipt in nostr_events; convert it into event_zaps tab // deno-lint-ignore require-await await scavengerEvent({ savedEvent: (async () => event)(), kysely: kysely }); + // deno-lint-ignore require-await + await scavengerEvent({ savedEvent: (async () => event)(), kysely: kysely }); // just to trigger "Error: FOREIGN KEY constraint failed" const zapReceipts = await kysely.selectFrom('nostr_events').selectAll().execute(); const customEventZaps = await kysely.selectFrom('event_zaps').selectAll().execute(); @@ -53,3 +55,87 @@ Deno.test('store one zap receipt in nostr_events; convert it into event_zaps tab assertEquals(customEventZaps[0], expected); }); + +// The function tests below only handle the edge cases and don't assert anything +// If no error happens = ok + +Deno.test('savedEvent is undefined', async () => { + await using db = await getTestDB(); + const kysely = db.kysely; + + // deno-lint-ignore require-await + await scavengerEvent({ savedEvent: (async () => undefined)(), kysely: kysely }); + + // no error happened = ok +}); + +Deno.test('zap receipt does not have a "description" tag', async () => { + await using db = await getTestDB(); + const kysely = db.kysely; + + const sk = generateSecretKey(); + + const event = genEvent({ kind: 9735 }, sk); + + // deno-lint-ignore require-await + await scavengerEvent({ savedEvent: (async () => event)(), kysely: kysely }); + + // no error happened = ok +}); + +Deno.test('zap receipt does not have a zap request stringified value in the "description" tag', async () => { + await using db = await getTestDB(); + const kysely = db.kysely; + + const sk = generateSecretKey(); + + const event = genEvent({ kind: 9735, tags: [['description', 'yolo']] }, sk); + + // deno-lint-ignore require-await + await scavengerEvent({ savedEvent: (async () => event)(), kysely: kysely }); + + // no error happened = ok +}); + +Deno.test('zap receipt does not have a "bolt11" tag', async () => { + await using db = await getTestDB(); + const kysely = db.kysely; + + const sk = generateSecretKey(); + + const event = genEvent({ + kind: 9735, + tags: [[ + 'description', + '{"pubkey":"97c70a44366a6535c145b333f973ea86dfdc2d7a99da618c40c64705ad98e322","content":"","id":"d9cc14d50fcb8c27539aacf776882942c1a11ea4472f8cdec1dea82fab66279d","created_at":1674164539,"sig":"77127f636577e9029276be060332ea565deaf89ff215a494ccff16ae3f757065e2bc59b2e8c113dd407917a010b3abd36c8d7ad84c0e3ab7dab3a0b0caa9835d","kind":9734,"tags":[["e","3624762a1274dd9636e0c552b53086d70bc88c165bc4dc0f9e836a1eaf86c3b8"],["p","32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245"],["relays","wss://relay.damus.io","wss://nostr-relay.wlvs.space","wss://nostr.fmt.wiz.biz","wss://relay.nostr.bg","wss://nostr.oxtr.dev","wss://nostr.v0l.io","wss://brb.io","wss://nostr.bitcoiner.social","ws://monad.jb55.com:8080","wss://relay.snort.social"]]}', + ]], + }, sk); + + // deno-lint-ignore require-await + await scavengerEvent({ savedEvent: (async () => event)(), kysely: kysely }); + + // no error happened = ok +}); + +Deno.test('zap request inside zap receipt does not have an "e" tag', async () => { + await using db = await getTestDB(); + const kysely = db.kysely; + + const sk = generateSecretKey(); + + const event = genEvent({ + kind: 9735, + tags: [[ + 'bolt11', + 'lnbc10u1p3unwfusp5t9r3yymhpfqculx78u027lxspgxcr2n2987mx2j55nnfs95nxnzqpp5jmrh92pfld78spqs78v9euf2385t83uvpwk9ldrlvf6ch7tpascqhp5zvkrmemgth3tufcvflmzjzfvjt023nazlhljz2n9hattj4f8jq8qxqyjw5qcqpjrzjqtc4fc44feggv7065fqe5m4ytjarg3repr5j9el35xhmtfexc42yczarjuqqfzqqqqqqqqlgqqqqqqgq9q9qxpqysgq079nkq507a5tw7xgttmj4u990j7wfggtrasah5gd4ywfr2pjcn29383tphp4t48gquelz9z78p4cq7ml3nrrphw5w6eckhjwmhezhnqpy6gyf0', + ], [ + 'description', + '{"pubkey":"97c70a44366a6535c145b333f973ea86dfdc2d7a99da618c40c64705ad98e322","content":"","id":"d9cc14d50fcb8c27539aacf776882942c1a11ea4472f8cdec1dea82fab66279d","created_at":1674164539,"sig":"77127f636577e9029276be060332ea565deaf89ff215a494ccff16ae3f757065e2bc59b2e8c113dd407917a010b3abd36c8d7ad84c0e3ab7dab3a0b0caa9835d","kind":9734,"tags":[["p","32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245"],["relays","wss://relay.damus.io","wss://nostr-relay.wlvs.space","wss://nostr.fmt.wiz.biz","wss://relay.nostr.bg","wss://nostr.oxtr.dev","wss://nostr.v0l.io","wss://brb.io","wss://nostr.bitcoiner.social","ws://monad.jb55.com:8080","wss://relay.snort.social"]]}', + ]], + }, sk); + + // deno-lint-ignore require-await + await scavengerEvent({ savedEvent: (async () => event)(), kysely: kysely }); + + // no error happened = ok +}); From 9731fc257299efe7a5f3663a3a4f4e5a88874f33 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Fri, 21 Jun 2024 22:25:34 -0300 Subject: [PATCH 10/49] feat: add scavenger to the pipeline --- src/pipeline.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/pipeline.ts b/src/pipeline.ts index 40ba9c12..bd56f09e 100644 --- a/src/pipeline.ts +++ b/src/pipeline.ts @@ -17,6 +17,7 @@ import { verifyEventWorker } from '@/workers/verify.ts'; import { nip05Cache } from '@/utils/nip05.ts'; import { updateStats } from '@/utils/stats.ts'; import { getTagSet } from '@/utils/tags.ts'; +import { scavengerEvent } from '@/utils/scavenger.ts'; const debug = Debug('ditto:pipeline'); @@ -50,7 +51,7 @@ async function handleEvent(event: DittoEvent, signal: AbortSignal): Promise { +async function storeEvent(event: DittoEvent, signal?: AbortSignal): Promise { if (NKinds.ephemeral(event.kind)) return; const store = await Storages.db(); const kysely = await DittoDB.getInstance(); await updateStats({ event, store, kysely }).catch(debug); await store.event(event, { signal }); + + return event; } /** Parse kind 0 metadata and track indexes in the database. */ From 0d7ef68353817977e8acc3a81091b0f9e0cdcaaa Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Sun, 23 Jun 2024 13:38:40 -0300 Subject: [PATCH 11/49] feat: add pagination and sort by amount - zapped_by endpoint --- src/controllers/api/statuses.ts | 50 ++++++++++++++++----------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/controllers/api/statuses.ts b/src/controllers/api/statuses.ts index 28e0778a..30550bbb 100644 --- a/src/controllers/api/statuses.ts +++ b/src/controllers/api/statuses.ts @@ -8,14 +8,21 @@ import { z } from 'zod'; import { type AppController } from '@/app.ts'; import { Conf } from '@/config.ts'; import { DittoDB } from '@/db/DittoDB.ts'; -import { getAmount } from '@/utils/bolt11.ts'; import { getAncestors, getAuthor, getDescendants, getEvent } from '@/queries.ts'; import { getUnattachedMediaByIds } from '@/db/unattached-media.ts'; import { renderEventAccounts } from '@/views.ts'; import { renderReblog, renderStatus } from '@/views/mastodon/statuses.ts'; import { Storages } from '@/storages.ts'; import { hydrateEvents, purifyEvent } from '@/storages/hydrate.ts'; -import { createEvent, paginated, paginationSchema, parseBody, updateListEvent } from '@/utils/api.ts'; +import { + createEvent, + listPaginationSchema, + paginated, + paginatedList, + paginationSchema, + parseBody, + updateListEvent, +} from '@/utils/api.ts'; import { getInvoice, getLnurl } from '@/utils/lnurl.ts'; import { lookupPubkey } from '@/utils/lookup.ts'; import { addTag, deleteTag } from '@/utils/tags.ts'; @@ -545,33 +552,26 @@ const zapController: AppController = async (c) => { const zappedByController: AppController = async (c) => { const id = c.req.param('id'); + const params = listPaginationSchema.parse(c.req.query()); const store = await Storages.db(); - const amountSchema = z.coerce.number().int().nonnegative().catch(0); + const db = await DittoDB.getInstance(); - const events = (await store.query([{ kinds: [9735], '#e': [id], limit: 100 }])).map((event) => { - const zapRequestString = event.tags.find(([name]) => name === 'description')?.[1]; - if (!zapRequestString) return; - try { - const zapRequest = n.json().pipe(n.event()).parse(zapRequestString); - const amount = zapRequest?.tags.find(([name]: any) => name === 'amount')?.[1]; - if (!amount) { - const amount = getAmount(event?.tags.find(([name]) => name === 'bolt11')?.[1]); - if (!amount) return; - zapRequest.tags.push(['amount', amount]); - } - return zapRequest; - } catch { - return; - } - }).filter(Boolean) as DittoEvent[]; + const zaps = await db.selectFrom('event_zaps') + .selectAll() + .where('target_event_id', '=', id) + .orderBy('amount_millisats', 'desc') + .limit(params.limit) + .offset(params.offset).execute(); - await hydrateEvents({ events, store }); + const authors = await store.query([{ kinds: [0], authors: zaps.map((zap) => zap.sender_pubkey) }]); const results = (await Promise.all( - events.map(async (event) => { - const amount = amountSchema.parse(event.tags.find(([name]) => name === 'amount')?.[1]); - const comment = event?.content ?? ''; - const account = event?.author ? await renderAccount(event.author) : await accountFromPubkey(event.pubkey); + zaps.map(async (zap) => { + const amount = zap.amount_millisats; + const comment = zap.comment; + + const sender = authors.find((author) => author.pubkey === zap.sender_pubkey); + const account = sender ? await renderAccount(sender) : await accountFromPubkey(zap.sender_pubkey); return { comment, @@ -581,7 +581,7 @@ const zappedByController: AppController = async (c) => { }), )).filter(Boolean); - return c.json(results); + return paginatedList(c, params, results); }; export { From 05bf417bcccb0f75e6cc229d9c2879cbb74bf590 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Sun, 23 Jun 2024 22:49:15 -0300 Subject: [PATCH 12/49] perf(event_zaps): make two separate indexes instead of a compound index --- src/db/migrations/027_add_zap_events.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/db/migrations/027_add_zap_events.ts b/src/db/migrations/027_add_zap_events.ts index 58b231fa..2fcc101c 100644 --- a/src/db/migrations/027_add_zap_events.ts +++ b/src/db/migrations/027_add_zap_events.ts @@ -11,15 +11,22 @@ export async function up(db: Kysely): Promise { .execute(); await db.schema - .createIndex('idx_event_zaps_id_amount') + .createIndex('idx_event_zaps_amount_millisats') .on('event_zaps') .column('amount_millisats') + .ifNotExists() + .execute(); + + await db.schema + .createIndex('idx_event_zaps_target_event_id') + .on('event_zaps') .column('target_event_id') .ifNotExists() .execute(); } export async function down(db: Kysely): Promise { - await db.schema.dropIndex('idx_event_zaps_id_amount').ifExists().execute(); + await db.schema.dropIndex('idx_event_zaps_amount_millisats').ifExists().execute(); + await db.schema.dropIndex('idx_event_zaps_target_event_id').ifExists().execute(); await db.schema.dropTable('event_zaps').execute(); } From e1ee3bd8e97665826426aada49f2867501dcd50d Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Sun, 23 Jun 2024 23:45:32 -0300 Subject: [PATCH 13/49] refactor: remove scavenger, put logic directly into pipeline --- src/pipeline.test.ts | 125 ++++++++++++++++++++++++++++++++ src/pipeline.ts | 38 +++++++++- src/utils/scavenger.test.ts | 141 ------------------------------------ src/utils/scavenger.ts | 52 ------------- 4 files changed, 159 insertions(+), 197 deletions(-) create mode 100644 src/pipeline.test.ts delete mode 100644 src/utils/scavenger.ts diff --git a/src/pipeline.test.ts b/src/pipeline.test.ts new file mode 100644 index 00000000..64cb523b --- /dev/null +++ b/src/pipeline.test.ts @@ -0,0 +1,125 @@ +import { assertEquals } from '@std/assert'; +import { generateSecretKey } from 'nostr-tools'; + +import { genEvent, getTestDB } from '@/test.ts'; +import { handleZaps } from '@/pipeline.ts'; + +Deno.test('store one zap receipt in nostr_events; convert it into event_zaps table format and store it', async () => { + await using db = await getTestDB(); + const kysely = db.kysely; + + const sk = generateSecretKey(); + + const event = genEvent({ + 'id': '67b48a14fb66c60c8f9070bdeb37afdfcc3d08ad01989460448e4081eddda446', + 'pubkey': '9630f464cca6a5147aa8a35f0bcdd3ce485324e732fd39e09233b1d848238f31', + 'created_at': 1674164545, + 'kind': 9735, + 'tags': [ + ['p', '32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245'], + ['P', '97c70a44366a6535c145b333f973ea86dfdc2d7a99da618c40c64705ad98e322'], + ['e', '3624762a1274dd9636e0c552b53086d70bc88c165bc4dc0f9e836a1eaf86c3b8'], + [ + 'bolt11', + 'lnbc10u1p3unwfusp5t9r3yymhpfqculx78u027lxspgxcr2n2987mx2j55nnfs95nxnzqpp5jmrh92pfld78spqs78v9euf2385t83uvpwk9ldrlvf6ch7tpascqhp5zvkrmemgth3tufcvflmzjzfvjt023nazlhljz2n9hattj4f8jq8qxqyjw5qcqpjrzjqtc4fc44feggv7065fqe5m4ytjarg3repr5j9el35xhmtfexc42yczarjuqqfzqqqqqqqqlgqqqqqqgq9q9qxpqysgq079nkq507a5tw7xgttmj4u990j7wfggtrasah5gd4ywfr2pjcn29383tphp4t48gquelz9z78p4cq7ml3nrrphw5w6eckhjwmhezhnqpy6gyf0', + ], + [ + 'description', + '{"pubkey":"97c70a44366a6535c145b333f973ea86dfdc2d7a99da618c40c64705ad98e322","content":"","id":"d9cc14d50fcb8c27539aacf776882942c1a11ea4472f8cdec1dea82fab66279d","created_at":1674164539,"sig":"77127f636577e9029276be060332ea565deaf89ff215a494ccff16ae3f757065e2bc59b2e8c113dd407917a010b3abd36c8d7ad84c0e3ab7dab3a0b0caa9835d","kind":9734,"tags":[["e","3624762a1274dd9636e0c552b53086d70bc88c165bc4dc0f9e836a1eaf86c3b8"],["p","32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245"],["relays","wss://relay.damus.io","wss://nostr-relay.wlvs.space","wss://nostr.fmt.wiz.biz","wss://relay.nostr.bg","wss://nostr.oxtr.dev","wss://nostr.v0l.io","wss://brb.io","wss://nostr.bitcoiner.social","ws://monad.jb55.com:8080","wss://relay.snort.social"]]}', + ], + ['preimage', '5d006d2cf1e73c7148e7519a4c68adc81642ce0e25a432b2434c99f97344c15f'], + ], + 'content': '', + }, sk); + + await db.store.event(event); + + await handleZaps(kysely, event); + await handleZaps(kysely, event); + + const zapReceipts = await kysely.selectFrom('nostr_events').selectAll().execute(); + const customEventZaps = await kysely.selectFrom('event_zaps').selectAll().execute(); + + assertEquals(zapReceipts.length, 1); // basic check + assertEquals(customEventZaps.length, 1); // basic check + + const expected = { + receipt_id: event.id, + target_event_id: '3624762a1274dd9636e0c552b53086d70bc88c165bc4dc0f9e836a1eaf86c3b8', + sender_pubkey: '97c70a44366a6535c145b333f973ea86dfdc2d7a99da618c40c64705ad98e322', + amount_millisats: 1000000, + comment: '', + }; + + assertEquals(customEventZaps[0], expected); +}); + +// The function tests below only handle the edge cases and don't assert anything +// If no error happens = ok + +Deno.test('zap receipt does not have a "description" tag', async () => { + await using db = await getTestDB(); + const kysely = db.kysely; + + const sk = generateSecretKey(); + + const event = genEvent({ kind: 9735 }, sk); + + await handleZaps(kysely, event); + + // no error happened = ok +}); + +Deno.test('zap receipt does not have a zap request stringified value in the "description" tag', async () => { + await using db = await getTestDB(); + const kysely = db.kysely; + + const sk = generateSecretKey(); + + const event = genEvent({ kind: 9735, tags: [['description', 'yolo']] }, sk); + + await handleZaps(kysely, event); + + // no error happened = ok +}); + +Deno.test('zap receipt does not have a "bolt11" tag', async () => { + await using db = await getTestDB(); + const kysely = db.kysely; + + const sk = generateSecretKey(); + + const event = genEvent({ + kind: 9735, + tags: [[ + 'description', + '{"pubkey":"97c70a44366a6535c145b333f973ea86dfdc2d7a99da618c40c64705ad98e322","content":"","id":"d9cc14d50fcb8c27539aacf776882942c1a11ea4472f8cdec1dea82fab66279d","created_at":1674164539,"sig":"77127f636577e9029276be060332ea565deaf89ff215a494ccff16ae3f757065e2bc59b2e8c113dd407917a010b3abd36c8d7ad84c0e3ab7dab3a0b0caa9835d","kind":9734,"tags":[["e","3624762a1274dd9636e0c552b53086d70bc88c165bc4dc0f9e836a1eaf86c3b8"],["p","32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245"],["relays","wss://relay.damus.io","wss://nostr-relay.wlvs.space","wss://nostr.fmt.wiz.biz","wss://relay.nostr.bg","wss://nostr.oxtr.dev","wss://nostr.v0l.io","wss://brb.io","wss://nostr.bitcoiner.social","ws://monad.jb55.com:8080","wss://relay.snort.social"]]}', + ]], + }, sk); + + await handleZaps(kysely, event); + + // no error happened = ok +}); + +Deno.test('zap request inside zap receipt does not have an "e" tag', async () => { + await using db = await getTestDB(); + const kysely = db.kysely; + + const sk = generateSecretKey(); + + const event = genEvent({ + kind: 9735, + tags: [[ + 'bolt11', + 'lnbc10u1p3unwfusp5t9r3yymhpfqculx78u027lxspgxcr2n2987mx2j55nnfs95nxnzqpp5jmrh92pfld78spqs78v9euf2385t83uvpwk9ldrlvf6ch7tpascqhp5zvkrmemgth3tufcvflmzjzfvjt023nazlhljz2n9hattj4f8jq8qxqyjw5qcqpjrzjqtc4fc44feggv7065fqe5m4ytjarg3repr5j9el35xhmtfexc42yczarjuqqfzqqqqqqqqlgqqqqqqgq9q9qxpqysgq079nkq507a5tw7xgttmj4u990j7wfggtrasah5gd4ywfr2pjcn29383tphp4t48gquelz9z78p4cq7ml3nrrphw5w6eckhjwmhezhnqpy6gyf0', + ], [ + 'description', + '{"pubkey":"97c70a44366a6535c145b333f973ea86dfdc2d7a99da618c40c64705ad98e322","content":"","id":"d9cc14d50fcb8c27539aacf776882942c1a11ea4472f8cdec1dea82fab66279d","created_at":1674164539,"sig":"77127f636577e9029276be060332ea565deaf89ff215a494ccff16ae3f757065e2bc59b2e8c113dd407917a010b3abd36c8d7ad84c0e3ab7dab3a0b0caa9835d","kind":9734,"tags":[["p","32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245"],["relays","wss://relay.damus.io","wss://nostr-relay.wlvs.space","wss://nostr.fmt.wiz.biz","wss://relay.nostr.bg","wss://nostr.oxtr.dev","wss://nostr.v0l.io","wss://brb.io","wss://nostr.bitcoiner.social","ws://monad.jb55.com:8080","wss://relay.snort.social"]]}', + ]], + }, sk); + + await handleZaps(kysely, event); + + // no error happened = ok +}); diff --git a/src/pipeline.ts b/src/pipeline.ts index b60ac9ed..4c942913 100644 --- a/src/pipeline.ts +++ b/src/pipeline.ts @@ -1,7 +1,8 @@ import { NKinds, NostrEvent, NSchema as n } from '@nostrify/nostrify'; import Debug from '@soapbox/stickynotes/debug'; -import { sql } from 'kysely'; +import { Kysely, sql } from 'kysely'; import { LRUCache } from 'lru-cache'; +import { z } from 'zod'; import { Conf } from '@/config.ts'; import { DittoDB } from '@/db/DittoDB.ts'; @@ -18,7 +19,8 @@ import { verifyEventWorker } from '@/workers/verify.ts'; import { nip05Cache } from '@/utils/nip05.ts'; import { updateStats } from '@/utils/stats.ts'; import { getTagSet } from '@/utils/tags.ts'; -import { scavengerEvent } from '@/utils/scavenger.ts'; +import { DittoTables } from '@/db/DittoTables.ts'; +import { getAmount } from '@/utils/bolt11.ts'; const debug = Debug('ditto:pipeline'); @@ -53,7 +55,8 @@ async function handleEvent(event: DittoEvent, signal: AbortSignal): Promise { } } -export { handleEvent }; +/** Stores the event in the 'event_zaps' table */ +async function handleZaps(kysely: Kysely, event: NostrEvent) { + const zapRequestString = event?.tags?.find(([name]) => name === 'description')?.[1]; + if (!zapRequestString) return; + const zapRequest = n.json().pipe(n.event()).optional().catch(undefined).parse(zapRequestString); + if (!zapRequest) return; + + const amountSchema = z.coerce.number().int().nonnegative().catch(0); + const amount_millisats = amountSchema.parse(getAmount(event?.tags.find(([name]) => name === 'bolt11')?.[1])); + if (!amount_millisats || amount_millisats < 1) return; + + const zappedEventId = zapRequest.tags.find(([name]) => name === 'e')?.[1]; + if (!zappedEventId) return; + + try { + await kysely.insertInto('event_zaps').values({ + receipt_id: event.id, + target_event_id: zappedEventId, + sender_pubkey: zapRequest.pubkey, + amount_millisats, + comment: zapRequest.content, + }).execute(); + } catch { + // receipt_id is unique, do nothing + } +} + +export { handleEvent, handleZaps }; diff --git a/src/utils/scavenger.test.ts b/src/utils/scavenger.test.ts index 9d768999..e69de29b 100644 --- a/src/utils/scavenger.test.ts +++ b/src/utils/scavenger.test.ts @@ -1,141 +0,0 @@ -import { assertEquals } from '@std/assert'; -import { generateSecretKey } from 'nostr-tools'; - -import { genEvent, getTestDB } from '@/test.ts'; -import { scavengerEvent } from '@/utils/scavenger.ts'; - -Deno.test('store one zap receipt in nostr_events; convert it into event_zaps table format and store it', async () => { - await using db = await getTestDB(); - const kysely = db.kysely; - - const sk = generateSecretKey(); - - const event = genEvent({ - 'id': '67b48a14fb66c60c8f9070bdeb37afdfcc3d08ad01989460448e4081eddda446', - 'pubkey': '9630f464cca6a5147aa8a35f0bcdd3ce485324e732fd39e09233b1d848238f31', - 'created_at': 1674164545, - 'kind': 9735, - 'tags': [ - ['p', '32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245'], - ['P', '97c70a44366a6535c145b333f973ea86dfdc2d7a99da618c40c64705ad98e322'], - ['e', '3624762a1274dd9636e0c552b53086d70bc88c165bc4dc0f9e836a1eaf86c3b8'], - [ - 'bolt11', - 'lnbc10u1p3unwfusp5t9r3yymhpfqculx78u027lxspgxcr2n2987mx2j55nnfs95nxnzqpp5jmrh92pfld78spqs78v9euf2385t83uvpwk9ldrlvf6ch7tpascqhp5zvkrmemgth3tufcvflmzjzfvjt023nazlhljz2n9hattj4f8jq8qxqyjw5qcqpjrzjqtc4fc44feggv7065fqe5m4ytjarg3repr5j9el35xhmtfexc42yczarjuqqfzqqqqqqqqlgqqqqqqgq9q9qxpqysgq079nkq507a5tw7xgttmj4u990j7wfggtrasah5gd4ywfr2pjcn29383tphp4t48gquelz9z78p4cq7ml3nrrphw5w6eckhjwmhezhnqpy6gyf0', - ], - [ - 'description', - '{"pubkey":"97c70a44366a6535c145b333f973ea86dfdc2d7a99da618c40c64705ad98e322","content":"","id":"d9cc14d50fcb8c27539aacf776882942c1a11ea4472f8cdec1dea82fab66279d","created_at":1674164539,"sig":"77127f636577e9029276be060332ea565deaf89ff215a494ccff16ae3f757065e2bc59b2e8c113dd407917a010b3abd36c8d7ad84c0e3ab7dab3a0b0caa9835d","kind":9734,"tags":[["e","3624762a1274dd9636e0c552b53086d70bc88c165bc4dc0f9e836a1eaf86c3b8"],["p","32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245"],["relays","wss://relay.damus.io","wss://nostr-relay.wlvs.space","wss://nostr.fmt.wiz.biz","wss://relay.nostr.bg","wss://nostr.oxtr.dev","wss://nostr.v0l.io","wss://brb.io","wss://nostr.bitcoiner.social","ws://monad.jb55.com:8080","wss://relay.snort.social"]]}', - ], - ['preimage', '5d006d2cf1e73c7148e7519a4c68adc81642ce0e25a432b2434c99f97344c15f'], - ], - 'content': '', - }, sk); - - await db.store.event(event); - - // deno-lint-ignore require-await - await scavengerEvent({ savedEvent: (async () => event)(), kysely: kysely }); - // deno-lint-ignore require-await - await scavengerEvent({ savedEvent: (async () => event)(), kysely: kysely }); // just to trigger "Error: FOREIGN KEY constraint failed" - - const zapReceipts = await kysely.selectFrom('nostr_events').selectAll().execute(); - const customEventZaps = await kysely.selectFrom('event_zaps').selectAll().execute(); - - assertEquals(zapReceipts.length, 1); // basic check - assertEquals(customEventZaps.length, 1); // basic check - - const expected = { - receipt_id: event.id, - target_event_id: '3624762a1274dd9636e0c552b53086d70bc88c165bc4dc0f9e836a1eaf86c3b8', - sender_pubkey: '97c70a44366a6535c145b333f973ea86dfdc2d7a99da618c40c64705ad98e322', - amount_millisats: 1000000, - comment: '', - }; - - assertEquals(customEventZaps[0], expected); -}); - -// The function tests below only handle the edge cases and don't assert anything -// If no error happens = ok - -Deno.test('savedEvent is undefined', async () => { - await using db = await getTestDB(); - const kysely = db.kysely; - - // deno-lint-ignore require-await - await scavengerEvent({ savedEvent: (async () => undefined)(), kysely: kysely }); - - // no error happened = ok -}); - -Deno.test('zap receipt does not have a "description" tag', async () => { - await using db = await getTestDB(); - const kysely = db.kysely; - - const sk = generateSecretKey(); - - const event = genEvent({ kind: 9735 }, sk); - - // deno-lint-ignore require-await - await scavengerEvent({ savedEvent: (async () => event)(), kysely: kysely }); - - // no error happened = ok -}); - -Deno.test('zap receipt does not have a zap request stringified value in the "description" tag', async () => { - await using db = await getTestDB(); - const kysely = db.kysely; - - const sk = generateSecretKey(); - - const event = genEvent({ kind: 9735, tags: [['description', 'yolo']] }, sk); - - // deno-lint-ignore require-await - await scavengerEvent({ savedEvent: (async () => event)(), kysely: kysely }); - - // no error happened = ok -}); - -Deno.test('zap receipt does not have a "bolt11" tag', async () => { - await using db = await getTestDB(); - const kysely = db.kysely; - - const sk = generateSecretKey(); - - const event = genEvent({ - kind: 9735, - tags: [[ - 'description', - '{"pubkey":"97c70a44366a6535c145b333f973ea86dfdc2d7a99da618c40c64705ad98e322","content":"","id":"d9cc14d50fcb8c27539aacf776882942c1a11ea4472f8cdec1dea82fab66279d","created_at":1674164539,"sig":"77127f636577e9029276be060332ea565deaf89ff215a494ccff16ae3f757065e2bc59b2e8c113dd407917a010b3abd36c8d7ad84c0e3ab7dab3a0b0caa9835d","kind":9734,"tags":[["e","3624762a1274dd9636e0c552b53086d70bc88c165bc4dc0f9e836a1eaf86c3b8"],["p","32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245"],["relays","wss://relay.damus.io","wss://nostr-relay.wlvs.space","wss://nostr.fmt.wiz.biz","wss://relay.nostr.bg","wss://nostr.oxtr.dev","wss://nostr.v0l.io","wss://brb.io","wss://nostr.bitcoiner.social","ws://monad.jb55.com:8080","wss://relay.snort.social"]]}', - ]], - }, sk); - - // deno-lint-ignore require-await - await scavengerEvent({ savedEvent: (async () => event)(), kysely: kysely }); - - // no error happened = ok -}); - -Deno.test('zap request inside zap receipt does not have an "e" tag', async () => { - await using db = await getTestDB(); - const kysely = db.kysely; - - const sk = generateSecretKey(); - - const event = genEvent({ - kind: 9735, - tags: [[ - 'bolt11', - 'lnbc10u1p3unwfusp5t9r3yymhpfqculx78u027lxspgxcr2n2987mx2j55nnfs95nxnzqpp5jmrh92pfld78spqs78v9euf2385t83uvpwk9ldrlvf6ch7tpascqhp5zvkrmemgth3tufcvflmzjzfvjt023nazlhljz2n9hattj4f8jq8qxqyjw5qcqpjrzjqtc4fc44feggv7065fqe5m4ytjarg3repr5j9el35xhmtfexc42yczarjuqqfzqqqqqqqqlgqqqqqqgq9q9qxpqysgq079nkq507a5tw7xgttmj4u990j7wfggtrasah5gd4ywfr2pjcn29383tphp4t48gquelz9z78p4cq7ml3nrrphw5w6eckhjwmhezhnqpy6gyf0', - ], [ - 'description', - '{"pubkey":"97c70a44366a6535c145b333f973ea86dfdc2d7a99da618c40c64705ad98e322","content":"","id":"d9cc14d50fcb8c27539aacf776882942c1a11ea4472f8cdec1dea82fab66279d","created_at":1674164539,"sig":"77127f636577e9029276be060332ea565deaf89ff215a494ccff16ae3f757065e2bc59b2e8c113dd407917a010b3abd36c8d7ad84c0e3ab7dab3a0b0caa9835d","kind":9734,"tags":[["p","32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245"],["relays","wss://relay.damus.io","wss://nostr-relay.wlvs.space","wss://nostr.fmt.wiz.biz","wss://relay.nostr.bg","wss://nostr.oxtr.dev","wss://nostr.v0l.io","wss://brb.io","wss://nostr.bitcoiner.social","ws://monad.jb55.com:8080","wss://relay.snort.social"]]}', - ]], - }, sk); - - // deno-lint-ignore require-await - await scavengerEvent({ savedEvent: (async () => event)(), kysely: kysely }); - - // no error happened = ok -}); diff --git a/src/utils/scavenger.ts b/src/utils/scavenger.ts deleted file mode 100644 index d9874cb7..00000000 --- a/src/utils/scavenger.ts +++ /dev/null @@ -1,52 +0,0 @@ -import { NostrEvent, NSchema as n } from '@nostrify/nostrify'; -import { Kysely } from 'kysely'; -import { z } from 'zod'; - -import { DittoTables } from '@/db/DittoTables.ts'; -import { getAmount } from '@/utils/bolt11.ts'; - -interface ScavengerEventOpts { - savedEvent: Promise; - kysely: Kysely; -} - -/** Consumes the event already stored in the database and uses it to insert into a new custom table, if eligible. - * Scavenger is organism that eats dead or rotting biomass, such as animal flesh or plant material. */ -async function scavengerEvent({ savedEvent, kysely }: ScavengerEventOpts): Promise { - const event = await savedEvent; - if (!event) return; - - switch (event.kind) { - case 9735: - await handleEvent9735(kysely, event); - break; - } -} - -async function handleEvent9735(kysely: Kysely, event: NostrEvent) { - const zapRequestString = event?.tags?.find(([name]) => name === 'description')?.[1]; - if (!zapRequestString) return; - const zapRequest = n.json().pipe(n.event()).optional().catch(undefined).parse(zapRequestString); - if (!zapRequest) return; - - const amountSchema = z.coerce.number().int().nonnegative().catch(0); - const amount_millisats = amountSchema.parse(getAmount(event?.tags.find(([name]) => name === 'bolt11')?.[1])); - if (!amount_millisats || amount_millisats < 1) return; - - const zappedEventId = zapRequest.tags.find(([name]) => name === 'e')?.[1]; - if (!zappedEventId) return; - - try { - await kysely.insertInto('event_zaps').values({ - receipt_id: event.id, - target_event_id: zappedEventId, - sender_pubkey: zapRequest.pubkey, - amount_millisats, - comment: zapRequest.content, - }).execute(); - } catch { - // receipt_id is unique, do nothing - } -} - -export { scavengerEvent }; From fdb720386da698a2ca9cd15b8f3ece5ed1187d72 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Sun, 23 Jun 2024 23:51:10 -0300 Subject: [PATCH 14/49] fix(handleZaps): reject all kinds but 9735 --- src/pipeline.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/pipeline.ts b/src/pipeline.ts index 4c942913..a8d98acd 100644 --- a/src/pipeline.ts +++ b/src/pipeline.ts @@ -228,6 +228,8 @@ async function generateSetEvents(event: NostrEvent): Promise { /** Stores the event in the 'event_zaps' table */ async function handleZaps(kysely: Kysely, event: NostrEvent) { + if (event.kind !== 9735) return; + const zapRequestString = event?.tags?.find(([name]) => name === 'description')?.[1]; if (!zapRequestString) return; const zapRequest = n.json().pipe(n.event()).optional().catch(undefined).parse(zapRequestString); From 797c8668309497aa845d392a8697f20e4605f8b9 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Mon, 24 Jun 2024 12:07:33 -0300 Subject: [PATCH 15/49] refactor: storeEvent does not return event, move kysely into a variable above --- src/pipeline.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/pipeline.ts b/src/pipeline.ts index a8d98acd..17998aa3 100644 --- a/src/pipeline.ts +++ b/src/pipeline.ts @@ -54,9 +54,11 @@ async function handleEvent(event: DittoEvent, signal: AbortSignal): Promise { +async function storeEvent(event: DittoEvent, signal?: AbortSignal): Promise { if (NKinds.ephemeral(event.kind)) return; const store = await Storages.db(); const kysely = await DittoDB.getInstance(); await updateStats({ event, store, kysely }).catch(debug); await store.event(event, { signal }); - - return event; } /** Parse kind 0 metadata and track indexes in the database. */ From 9ea6c7b00b3a283639eba3d42712c0f8c60a274f Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 29 Jun 2024 22:26:51 +0100 Subject: [PATCH 16/49] Add query timeouts --- deno.json | 2 +- deno.lock | 8 ++++---- src/controllers/nostr/relay.ts | 10 +++++++--- src/storages/EventsDB.ts | 19 +++++++++++-------- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/deno.json b/deno.json index 9c0cec5a..af21804a 100644 --- a/deno.json +++ b/deno.json @@ -26,7 +26,7 @@ "@hono/hono": "jsr:@hono/hono@^4.4.6", "@isaacs/ttlcache": "npm:@isaacs/ttlcache@^1.4.1", "@noble/secp256k1": "npm:@noble/secp256k1@^2.0.0", - "@nostrify/nostrify": "jsr:@nostrify/nostrify@^0.23.3", + "@nostrify/nostrify": "jsr:@nostrify/nostrify@^0.25.0", "@scure/base": "npm:@scure/base@^1.1.6", "@sentry/deno": "https://deno.land/x/sentry@7.112.2/index.mjs", "@soapbox/kysely-deno-sqlite": "jsr:@soapbox/kysely-deno-sqlite@^2.1.0", diff --git a/deno.lock b/deno.lock index 855f7353..f8e13dbd 100644 --- a/deno.lock +++ b/deno.lock @@ -12,7 +12,7 @@ "jsr:@nostrify/nostrify@^0.22.1": "jsr:@nostrify/nostrify@0.22.5", "jsr:@nostrify/nostrify@^0.22.4": "jsr:@nostrify/nostrify@0.22.4", "jsr:@nostrify/nostrify@^0.22.5": "jsr:@nostrify/nostrify@0.22.5", - "jsr:@nostrify/nostrify@^0.23.3": "jsr:@nostrify/nostrify@0.23.3", + "jsr:@nostrify/nostrify@^0.25.0": "jsr:@nostrify/nostrify@0.25.0", "jsr:@soapbox/kysely-deno-sqlite@^2.1.0": "jsr:@soapbox/kysely-deno-sqlite@2.2.0", "jsr:@soapbox/stickynotes@^0.4.0": "jsr:@soapbox/stickynotes@0.4.0", "jsr:@std/assert@^0.217.0": "jsr:@std/assert@0.217.0", @@ -136,8 +136,8 @@ "npm:zod@^3.23.8" ] }, - "@nostrify/nostrify@0.23.3": { - "integrity": "868b10dd094801e28f4982ef9815f0d43f2a807b6f8ad291c78ecb3eb291605a", + "@nostrify/nostrify@0.25.0": { + "integrity": "98f26f44e95ac87fc91b3f3809d38432e1a7f6aebf10380b2554b6f9526313c6", "dependencies": [ "jsr:@std/encoding@^0.224.1", "npm:@scure/base@^1.1.6", @@ -1420,7 +1420,7 @@ "jsr:@bradenmacdonald/s3-lite-client@^0.7.4", "jsr:@db/sqlite@^0.11.1", "jsr:@hono/hono@^4.4.6", - "jsr:@nostrify/nostrify@^0.23.3", + "jsr:@nostrify/nostrify@^0.25.0", "jsr:@soapbox/kysely-deno-sqlite@^2.1.0", "jsr:@soapbox/stickynotes@^0.4.0", "jsr:@std/assert@^0.225.1", diff --git a/src/controllers/nostr/relay.ts b/src/controllers/nostr/relay.ts index 4e624e9b..e86f1991 100644 --- a/src/controllers/nostr/relay.ts +++ b/src/controllers/nostr/relay.ts @@ -73,11 +73,15 @@ function connectStream(socket: WebSocket) { const pubsub = await Storages.pubsub(); try { - for (const event of await store.query(filters, { limit: FILTER_LIMIT })) { + for (const event of await store.query(filters, { limit: FILTER_LIMIT, timeout: 500 })) { send(['EVENT', subId, event]); } } catch (e) { - send(['CLOSED', subId, e.message]); + if (e instanceof RelayError) { + send(['CLOSED', subId, e.message]); + } else { + send(['CLOSED', subId, 'error: something went wrong']); + } controllers.delete(subId); return; } @@ -124,7 +128,7 @@ function connectStream(socket: WebSocket) { /** Handle COUNT. Return the number of events matching the filters. */ async function handleCount([_, subId, ...filters]: NostrClientCOUNT): Promise { const store = await Storages.db(); - const { count } = await store.count(filters); + const { count } = await store.count(filters, { timeout: 500 }); send(['COUNT', subId, { count, approximate: false }]); } diff --git a/src/storages/EventsDB.ts b/src/storages/EventsDB.ts index bd350173..c22e2567 100644 --- a/src/storages/EventsDB.ts +++ b/src/storages/EventsDB.ts @@ -51,7 +51,7 @@ class EventsDB implements NStore { } /** Insert an event (and its tags) into the database. */ - async event(event: NostrEvent, _opts?: { signal?: AbortSignal }): Promise { + async event(event: NostrEvent, opts?: { signal?: AbortSignal; timeout?: number }): Promise { event = purifyEvent(event); this.console.debug('EVENT', JSON.stringify(event)); dbEventCounter.inc({ kind: event.kind }); @@ -63,7 +63,7 @@ class EventsDB implements NStore { await this.deleteEventsAdmin(event); try { - await this.store.event(event); + await this.store.event(event, { timeout: opts?.timeout ?? 3000 }); } catch (e) { if (e.message === 'Cannot add a deleted event') { throw new RelayError('blocked', 'event deleted by user'); @@ -137,7 +137,10 @@ class EventsDB implements NStore { } /** Get events for filters from the database. */ - async query(filters: NostrFilter[], opts: { signal?: AbortSignal; limit?: number } = {}): Promise { + async query( + filters: NostrFilter[], + opts: { signal?: AbortSignal; timeout?: number; limit?: number } = {}, + ): Promise { filters = await this.expandFilters(filters); dbQueryCounter.inc(); @@ -160,28 +163,28 @@ class EventsDB implements NStore { this.console.debug('REQ', JSON.stringify(filters)); - return this.store.query(filters, opts); + return this.store.query(filters, { timeout: opts.timeout ?? 3000 }); } /** Delete events based on filters from the database. */ - async remove(filters: NostrFilter[], _opts?: { signal?: AbortSignal }): Promise { + async remove(filters: NostrFilter[], opts?: { signal?: AbortSignal; timeout?: number }): Promise { if (!filters.length) return Promise.resolve(); this.console.debug('DELETE', JSON.stringify(filters)); - return this.store.remove(filters); + return this.store.remove(filters, opts); } /** Get number of events that would be returned by filters. */ async count( filters: NostrFilter[], - opts: { signal?: AbortSignal } = {}, + opts: { signal?: AbortSignal; timeout?: number } = {}, ): Promise<{ count: number; approximate: boolean }> { if (opts.signal?.aborted) return Promise.reject(abortError()); if (!filters.length) return Promise.resolve({ count: 0, approximate: false }); this.console.debug('COUNT', JSON.stringify(filters)); - return this.store.count(filters); + return this.store.count(filters, { timeout: opts.timeout ?? 1000 }); } /** Return only the tags that should be indexed. */ From 80f6172a64fcf31148d04571863f974966c508b7 Mon Sep 17 00:00:00 2001 From: Siddharth Singh Date: Sun, 30 Jun 2024 15:35:46 +0530 Subject: [PATCH 17/49] create first version of import script --- deno.json | 1 + scripts/db-import.ts | 131 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 scripts/db-import.ts diff --git a/deno.json b/deno.json index 9c0cec5a..48c927f2 100644 --- a/deno.json +++ b/deno.json @@ -6,6 +6,7 @@ "dev": "deno run -A --watch src/server.ts", "hook": "deno run --allow-read --allow-run --allow-write https://deno.land/x/deno_hooks@0.1.1/mod.ts", "db:migrate": "deno run -A scripts/db-migrate.ts", + "db:import": "deno run -A scripts/db-import.ts", "debug": "deno run -A --inspect src/server.ts", "test": "DATABASE_URL=\"sqlite://:memory:\" deno test -A --junit-path=./deno-test.xml", "check": "deno check src/server.ts", diff --git a/scripts/db-import.ts b/scripts/db-import.ts new file mode 100644 index 00000000..33484507 --- /dev/null +++ b/scripts/db-import.ts @@ -0,0 +1,131 @@ +/** + * Script to import a user/list of users into Ditto given their npub/pubkey by looking them up on a list of relays. + */ + +import { nip19 } from 'npm:nostr-tools@^2.7.0'; +import { DittoDB } from '@/db/DittoDB.ts'; +import { EventsDB } from '@/storages/EventsDB.ts'; +import { NSchema, NRelay1, NostrEvent } from '@nostrify/nostrify'; + + +const kysely = await DittoDB.getInstance(); +const eventsDB = new EventsDB(kysely); + +interface ImportEventsOpts { + profilesOnly: boolean; +} + +type DoEvent = (evt: NostrEvent) => void | Promise; +const importUsers = async (authors: string[], relays: string[], doEvent: DoEvent = (evt: NostrEvent) => eventsDB.event(evt), opts?: Partial) => { + // Kind 0s + follow lists. + const profiles: Record> = {}; + // Kind 1s. + const notes = new Set(); + + await Promise.all(relays.map(async relay => { + if (!relay.startsWith('wss://')) console.error(`Invalid relay url ${relay}`); + const conn = new NRelay1(relay); + const kinds = [0, 3]; + if (!opts?.profilesOnly) { + kinds.push(1); + } + const matched = await conn.query([{ kinds, authors, limit: 1000 }]); + await conn.close(); + await Promise.all( + matched.map(async event => { + const { kind, pubkey } = event; + if (kind === 1 && !notes.has(event.id)) { + // add the event to eventsDB only if it has not been found already. + notes.add(event.id); + await doEvent(event); + return; + } + + profiles[pubkey] ??= {}; + const existing = profiles[pubkey][kind]; + if (existing.created_at > event.created_at) return; + else profiles[pubkey][kind] = event; + }) + ) + })) + + + for (const user in profiles) { + const profile = profiles[user]; + for (const kind in profile) { + await doEvent(profile[kind]); + } + } +} + +if (import.meta.main) { + if (!Deno.args.length) { + showHelp(); + Deno.exit(1); + } + const pubkeys: string[] = []; + const relays: string[] = []; + + const opts: Partial = {}; + + let optionsEnd = false; + let relaySectionBegun = false; + for (const arg of Deno.args) { + if (arg.startsWith('-')) { + if (optionsEnd) { + console.error("Option encountered after end of options section."); + showUsage(); + } + switch (arg) { + case '-p': + case '--profile-only': + console.log('Only importing profiles.'); + opts.profilesOnly = true; + break; + } + } + else if (arg.startsWith('npub1')) { + optionsEnd = true; + + if (relaySectionBegun) { + console.error('npub specified in relay section'); + Deno.exit(1); + } + const decoded = nip19.decode(arg as `npub1${string}`).data; + if (!NSchema.id().safeParse(decoded).success) { + console.error(`invalid pubkey ${arg}, skipping...`); + continue; + } + pubkeys.push(decoded); + } + else { + relaySectionBegun = true; + if (!arg.startsWith('wss://')) { + console.error(`invalid relay url ${arg}, skipping...`); + } + relays.push(arg); + } + } + + await importUsers(pubkeys, relays, console.log, opts); +} + +await kysely.destroy(); + +function showHelp() { + console.log('ditto - db:import'); + console.log('Import users\' posts and kind 0s from a given set of relays.\n'); + showUsage(); + console.log(` +OPTIONS: + +-p, --profile-only + Only import profiles and not posts. Default: off. +`); + +} + +function showUsage() { + console.log('Usage: deno task db:import [options] npub1xxxxxx[ npub1yyyyyyy]...' + + ' wss://first.relay[ second.relay]...'); +} From 9f3f6917d38fe995178c24ee931652544dfbd574 Mon Sep 17 00:00:00 2001 From: Siddharth Singh Date: Sun, 30 Jun 2024 15:50:25 +0530 Subject: [PATCH 18/49] import script works now --- scripts/db-import.ts | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/scripts/db-import.ts b/scripts/db-import.ts index 33484507..994728b0 100644 --- a/scripts/db-import.ts +++ b/scripts/db-import.ts @@ -16,19 +16,19 @@ interface ImportEventsOpts { } type DoEvent = (evt: NostrEvent) => void | Promise; -const importUsers = async (authors: string[], relays: string[], doEvent: DoEvent = (evt: NostrEvent) => eventsDB.event(evt), opts?: Partial) => { +const importUsers = async (authors: string[], relays: string[], opts?: Partial, doEvent: DoEvent = async (evt: NostrEvent) => await eventsDB.event(evt)) => { // Kind 0s + follow lists. const profiles: Record> = {}; // Kind 1s. const notes = new Set(); + const { profilesOnly = false } = opts || {}; + await Promise.all(relays.map(async relay => { if (!relay.startsWith('wss://')) console.error(`Invalid relay url ${relay}`); const conn = new NRelay1(relay); const kinds = [0, 3]; - if (!opts?.profilesOnly) { - kinds.push(1); - } + if (!profilesOnly) kinds.push(1); const matched = await conn.query([{ kinds, authors, limit: 1000 }]); await conn.close(); await Promise.all( @@ -43,7 +43,7 @@ const importUsers = async (authors: string[], relays: string[], doEvent: DoEvent profiles[pubkey] ??= {}; const existing = profiles[pubkey][kind]; - if (existing.created_at > event.created_at) return; + if (existing?.created_at > event.created_at) return; else profiles[pubkey][kind] = event; }) ) @@ -79,7 +79,7 @@ if (import.meta.main) { switch (arg) { case '-p': case '--profile-only': - console.log('Only importing profiles.'); + console.info('Only importing profiles.'); opts.profilesOnly = true; break; } @@ -107,25 +107,23 @@ if (import.meta.main) { } } - await importUsers(pubkeys, relays, console.log, opts); + await importUsers(pubkeys, relays, opts); + Deno.exit(0); } -await kysely.destroy(); - function showHelp() { - console.log('ditto - db:import'); - console.log('Import users\' posts and kind 0s from a given set of relays.\n'); + console.info('ditto - db:import'); + console.info('Import users\' posts and kind 0s from a given set of relays.\n'); showUsage(); - console.log(` + console.info(` OPTIONS: -p, --profile-only Only import profiles and not posts. Default: off. `); - } function showUsage() { - console.log('Usage: deno task db:import [options] npub1xxxxxx[ npub1yyyyyyy]...' + + console.info('Usage: deno task db:import [options] npub1xxxxxx[ npub1yyyyyyy]...' + ' wss://first.relay[ second.relay]...'); } From 480f4ed370736d06c48da0e2d52b9922fbc87119 Mon Sep 17 00:00:00 2001 From: Siddharth Singh Date: Sun, 30 Jun 2024 16:31:02 +0530 Subject: [PATCH 19/49] fmt --- scripts/db-import.ts | 189 ++++++++++++++++++++++--------------------- 1 file changed, 96 insertions(+), 93 deletions(-) diff --git a/scripts/db-import.ts b/scripts/db-import.ts index 994728b0..e1484c99 100644 --- a/scripts/db-import.ts +++ b/scripts/db-import.ts @@ -5,117 +5,118 @@ import { nip19 } from 'npm:nostr-tools@^2.7.0'; import { DittoDB } from '@/db/DittoDB.ts'; import { EventsDB } from '@/storages/EventsDB.ts'; -import { NSchema, NRelay1, NostrEvent } from '@nostrify/nostrify'; - +import { NostrEvent, NRelay1, NSchema } from '@nostrify/nostrify'; const kysely = await DittoDB.getInstance(); const eventsDB = new EventsDB(kysely); interface ImportEventsOpts { - profilesOnly: boolean; + profilesOnly: boolean; } type DoEvent = (evt: NostrEvent) => void | Promise; -const importUsers = async (authors: string[], relays: string[], opts?: Partial, doEvent: DoEvent = async (evt: NostrEvent) => await eventsDB.event(evt)) => { - // Kind 0s + follow lists. - const profiles: Record> = {}; - // Kind 1s. - const notes = new Set(); +const importUsers = async ( + authors: string[], + relays: string[], + opts?: Partial, + doEvent: DoEvent = async (evt: NostrEvent) => await eventsDB.event(evt), +) => { + // Kind 0s + follow lists. + const profiles: Record> = {}; + // Kind 1s. + const notes = new Set(); - const { profilesOnly = false } = opts || {}; + const { profilesOnly = false } = opts || {}; - await Promise.all(relays.map(async relay => { - if (!relay.startsWith('wss://')) console.error(`Invalid relay url ${relay}`); - const conn = new NRelay1(relay); - const kinds = [0, 3]; - if (!profilesOnly) kinds.push(1); - const matched = await conn.query([{ kinds, authors, limit: 1000 }]); - await conn.close(); - await Promise.all( - matched.map(async event => { - const { kind, pubkey } = event; - if (kind === 1 && !notes.has(event.id)) { - // add the event to eventsDB only if it has not been found already. - notes.add(event.id); - await doEvent(event); - return; - } - - profiles[pubkey] ??= {}; - const existing = profiles[pubkey][kind]; - if (existing?.created_at > event.created_at) return; - else profiles[pubkey][kind] = event; - }) - ) - })) - - - for (const user in profiles) { - const profile = profiles[user]; - for (const kind in profile) { - await doEvent(profile[kind]); + await Promise.all(relays.map(async (relay) => { + if (!relay.startsWith('wss://')) console.error(`Invalid relay url ${relay}`); + const conn = new NRelay1(relay); + const kinds = [0, 3]; + if (!profilesOnly) kinds.push(1); + const matched = await conn.query([{ kinds, authors, limit: 1000 }]); + await conn.close(); + await Promise.all( + matched.map(async (event) => { + const { kind, pubkey } = event; + if (kind === 1 && !notes.has(event.id)) { + // add the event to eventsDB only if it has not been found already. + notes.add(event.id); + await doEvent(event); + return; } + + profiles[pubkey] ??= {}; + const existing = profiles[pubkey][kind]; + if (existing?.created_at > event.created_at) return; + else profiles[pubkey][kind] = event; + }), + ); + })); + + for (const user in profiles) { + const profile = profiles[user]; + for (const kind in profile) { + await doEvent(profile[kind]); } -} + } +}; if (import.meta.main) { - if (!Deno.args.length) { - showHelp(); + if (!Deno.args.length) { + showHelp(); + Deno.exit(1); + } + const pubkeys: string[] = []; + const relays: string[] = []; + + const opts: Partial = {}; + + let optionsEnd = false; + let relaySectionBegun = false; + for (const arg of Deno.args) { + if (arg.startsWith('-')) { + if (optionsEnd) { + console.error('Option encountered after end of options section.'); + showUsage(); + } + switch (arg) { + case '-p': + case '--profile-only': + console.info('Only importing profiles.'); + opts.profilesOnly = true; + break; + } + } else if (arg.startsWith('npub1')) { + optionsEnd = true; + + if (relaySectionBegun) { + console.error('npub specified in relay section'); Deno.exit(1); + } + const decoded = nip19.decode(arg as `npub1${string}`).data; + if (!NSchema.id().safeParse(decoded).success) { + console.error(`invalid pubkey ${arg}, skipping...`); + continue; + } + pubkeys.push(decoded); + } else { + relaySectionBegun = true; + if (!arg.startsWith('wss://')) { + console.error(`invalid relay url ${arg}, skipping...`); + } + relays.push(arg); } - const pubkeys: string[] = []; - const relays: string[] = []; + } - const opts: Partial = {}; - - let optionsEnd = false; - let relaySectionBegun = false; - for (const arg of Deno.args) { - if (arg.startsWith('-')) { - if (optionsEnd) { - console.error("Option encountered after end of options section."); - showUsage(); - } - switch (arg) { - case '-p': - case '--profile-only': - console.info('Only importing profiles.'); - opts.profilesOnly = true; - break; - } - } - else if (arg.startsWith('npub1')) { - optionsEnd = true; - - if (relaySectionBegun) { - console.error('npub specified in relay section'); - Deno.exit(1); - } - const decoded = nip19.decode(arg as `npub1${string}`).data; - if (!NSchema.id().safeParse(decoded).success) { - console.error(`invalid pubkey ${arg}, skipping...`); - continue; - } - pubkeys.push(decoded); - } - else { - relaySectionBegun = true; - if (!arg.startsWith('wss://')) { - console.error(`invalid relay url ${arg}, skipping...`); - } - relays.push(arg); - } - } - - await importUsers(pubkeys, relays, opts); - Deno.exit(0); + await importUsers(pubkeys, relays, opts); + Deno.exit(0); } function showHelp() { - console.info('ditto - db:import'); - console.info('Import users\' posts and kind 0s from a given set of relays.\n'); - showUsage(); - console.info(` + console.info('ditto - db:import'); + console.info("Import users' posts and kind 0s from a given set of relays.\n"); + showUsage(); + console.info(` OPTIONS: -p, --profile-only @@ -124,6 +125,8 @@ OPTIONS: } function showUsage() { - console.info('Usage: deno task db:import [options] npub1xxxxxx[ npub1yyyyyyy]...' + - ' wss://first.relay[ second.relay]...'); + console.info( + 'Usage: deno task db:import [options] npub1xxxxxx[ npub1yyyyyyy]...' + + ' wss://first.relay[ second.relay]...', + ); } From 96fe171d6595fcc7afc78d3ec245ff5373a60a51 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 1 Jul 2024 07:22:33 +0100 Subject: [PATCH 20/49] Use kysely_deno_postgres with simple transactions --- deno.json | 2 +- deno.lock | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/deno.json b/deno.json index af21804a..cd52a343 100644 --- a/deno.json +++ b/deno.json @@ -50,7 +50,7 @@ "iso-639-1": "npm:iso-639-1@2.1.15", "isomorphic-dompurify": "npm:isomorphic-dompurify@^2.11.0", "kysely": "npm:kysely@^0.27.3", - "kysely_deno_postgres": "https://gitlab.com/soapbox-pub/kysely-deno-postgres/-/raw/b4725e74ad6ca359ba0e370b55dbb8bb845a8a83/mod.ts", + "kysely_deno_postgres": "https://gitlab.com/soapbox-pub/kysely-deno-postgres/-/raw/c6869b9e12d74af78a846ad503d84493f5db9df4/mod.ts", "light-bolt11-decoder": "npm:light-bolt11-decoder", "linkify-plugin-hashtag": "npm:linkify-plugin-hashtag@^4.1.1", "linkify-string": "npm:linkify-string@^4.1.1", diff --git a/deno.lock b/deno.lock index f8e13dbd..1377bf48 100644 --- a/deno.lock +++ b/deno.lock @@ -1395,6 +1395,10 @@ "https://gitlab.com/soapbox-pub/kysely-deno-postgres/-/raw/b4725e74ad6ca359ba0e370b55dbb8bb845a8a83/mod.ts": "662438fd3909984bb8cbaf3fd44d2121e949d11301baf21d6c3f057ccf9887de", "https://gitlab.com/soapbox-pub/kysely-deno-postgres/-/raw/b4725e74ad6ca359ba0e370b55dbb8bb845a8a83/src/PostgreSQLDriver.ts": "ea5a523bceeed420858b744beeb95d48976cb2b0d3f519a68b65a8229036cf6a", "https://gitlab.com/soapbox-pub/kysely-deno-postgres/-/raw/b4725e74ad6ca359ba0e370b55dbb8bb845a8a83/src/PostgreSQLDriverDatabaseConnection.ts": "11e2fc10a3abb3d0729613c4b7cdb9cb73b597fd77353311bb6707c73a635fc5", + "https://gitlab.com/soapbox-pub/kysely-deno-postgres/-/raw/c6869b9e12d74af78a846ad503d84493f5db9df4/deps.ts": "b3dbecae69c30a5f161323b8c8ebd91d9af1eceb98fafab3091c7281a4b64fed", + "https://gitlab.com/soapbox-pub/kysely-deno-postgres/-/raw/c6869b9e12d74af78a846ad503d84493f5db9df4/mod.ts": "662438fd3909984bb8cbaf3fd44d2121e949d11301baf21d6c3f057ccf9887de", + "https://gitlab.com/soapbox-pub/kysely-deno-postgres/-/raw/c6869b9e12d74af78a846ad503d84493f5db9df4/src/PostgreSQLDriver.ts": "0f5d1bc2b24d4e0052e38ee289fb2f5e8e1470544f61aa2afe65e1059bf35dfb", + "https://gitlab.com/soapbox-pub/kysely-deno-postgres/-/raw/c6869b9e12d74af78a846ad503d84493f5db9df4/src/PostgreSQLDriverDatabaseConnection.ts": "e5d4e0fc9737c3ec253e679a51f5b43d2bb9a3386c147b7b1d14f4f5a5f734f1", "https://gitlab.com/soapbox-pub/kysely-deno-postgres/-/raw/f2948b86190a10faa293588775e162b3a8b52e70/deps.ts": "b3dbecae69c30a5f161323b8c8ebd91d9af1eceb98fafab3091c7281a4b64fed", "https://gitlab.com/soapbox-pub/kysely-deno-postgres/-/raw/f2948b86190a10faa293588775e162b3a8b52e70/mod.ts": "662438fd3909984bb8cbaf3fd44d2121e949d11301baf21d6c3f057ccf9887de", "https://gitlab.com/soapbox-pub/kysely-deno-postgres/-/raw/f2948b86190a10faa293588775e162b3a8b52e70/src/PostgreSQLDriver.ts": "ac1a39e86fd676973bce215e19db1f26b82408b8f2bb09a3601802974ea7cec6", From d062f6bbb670b686faddfc52d3edc79ce348e8ae Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 1 Jul 2024 08:44:01 +0100 Subject: [PATCH 21/49] Try lazy pool initialization --- src/db/adapters/DittoPostgres.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/db/adapters/DittoPostgres.ts b/src/db/adapters/DittoPostgres.ts index c06a262f..6df78b97 100644 --- a/src/db/adapters/DittoPostgres.ts +++ b/src/db/adapters/DittoPostgres.ts @@ -12,7 +12,7 @@ export class DittoPostgres { static getPool(): Pool { if (!this.pool) { - this.pool = new Pool(Conf.databaseUrl, Conf.pg.poolSize); + this.pool = new Pool(Conf.databaseUrl, Conf.pg.poolSize, true); } return this.pool; } From c3ffe7c7f76fc3f1710d50fd0f992c410f56f28a Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 1 Jul 2024 08:53:03 +0100 Subject: [PATCH 22/49] EventsDB: fix limit being passed to NDatabase --- src/storages/EventsDB.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/storages/EventsDB.ts b/src/storages/EventsDB.ts index c22e2567..664be893 100644 --- a/src/storages/EventsDB.ts +++ b/src/storages/EventsDB.ts @@ -51,7 +51,7 @@ class EventsDB implements NStore { } /** Insert an event (and its tags) into the database. */ - async event(event: NostrEvent, opts?: { signal?: AbortSignal; timeout?: number }): Promise { + async event(event: NostrEvent, opts: { signal?: AbortSignal; timeout?: number } = {}): Promise { event = purifyEvent(event); this.console.debug('EVENT', JSON.stringify(event)); dbEventCounter.inc({ kind: event.kind }); @@ -63,7 +63,7 @@ class EventsDB implements NStore { await this.deleteEventsAdmin(event); try { - await this.store.event(event, { timeout: opts?.timeout ?? 3000 }); + await this.store.event(event, { ...opts, timeout: opts.timeout ?? 3000 }); } catch (e) { if (e.message === 'Cannot add a deleted event') { throw new RelayError('blocked', 'event deleted by user'); @@ -163,15 +163,15 @@ class EventsDB implements NStore { this.console.debug('REQ', JSON.stringify(filters)); - return this.store.query(filters, { timeout: opts.timeout ?? 3000 }); + return this.store.query(filters, { ...opts, timeout: opts.timeout ?? 3000 }); } /** Delete events based on filters from the database. */ - async remove(filters: NostrFilter[], opts?: { signal?: AbortSignal; timeout?: number }): Promise { + async remove(filters: NostrFilter[], opts: { signal?: AbortSignal; timeout?: number } = {}): Promise { if (!filters.length) return Promise.resolve(); this.console.debug('DELETE', JSON.stringify(filters)); - return this.store.remove(filters, opts); + return this.store.remove(filters, { ...opts, timeout: opts.timeout ?? 5000 }); } /** Get number of events that would be returned by filters. */ @@ -184,7 +184,7 @@ class EventsDB implements NStore { this.console.debug('COUNT', JSON.stringify(filters)); - return this.store.count(filters, { timeout: opts.timeout ?? 1000 }); + return this.store.count(filters, { ...opts, timeout: opts.timeout ?? 1000 }); } /** Return only the tags that should be indexed. */ From 092a20088a5ae82a89ce71b034b0b24e61b4b480 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 1 Jul 2024 09:18:42 +0100 Subject: [PATCH 23/49] Reduce timeouts --- src/controllers/nostr/relay.ts | 4 ++-- src/storages/EventsDB.ts | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/controllers/nostr/relay.ts b/src/controllers/nostr/relay.ts index e86f1991..6d79b031 100644 --- a/src/controllers/nostr/relay.ts +++ b/src/controllers/nostr/relay.ts @@ -73,7 +73,7 @@ function connectStream(socket: WebSocket) { const pubsub = await Storages.pubsub(); try { - for (const event of await store.query(filters, { limit: FILTER_LIMIT, timeout: 500 })) { + for (const event of await store.query(filters, { limit: FILTER_LIMIT, timeout: 300 })) { send(['EVENT', subId, event]); } } catch (e) { @@ -128,7 +128,7 @@ function connectStream(socket: WebSocket) { /** Handle COUNT. Return the number of events matching the filters. */ async function handleCount([_, subId, ...filters]: NostrClientCOUNT): Promise { const store = await Storages.db(); - const { count } = await store.count(filters, { timeout: 500 }); + const { count } = await store.count(filters, { timeout: 100 }); send(['COUNT', subId, { count, approximate: false }]); } diff --git a/src/storages/EventsDB.ts b/src/storages/EventsDB.ts index 664be893..61b87b00 100644 --- a/src/storages/EventsDB.ts +++ b/src/storages/EventsDB.ts @@ -63,7 +63,7 @@ class EventsDB implements NStore { await this.deleteEventsAdmin(event); try { - await this.store.event(event, { ...opts, timeout: opts.timeout ?? 3000 }); + await this.store.event(event, { ...opts, timeout: opts.timeout ?? 1000 }); } catch (e) { if (e.message === 'Cannot add a deleted event') { throw new RelayError('blocked', 'event deleted by user'); @@ -163,7 +163,7 @@ class EventsDB implements NStore { this.console.debug('REQ', JSON.stringify(filters)); - return this.store.query(filters, { ...opts, timeout: opts.timeout ?? 3000 }); + return this.store.query(filters, { ...opts, timeout: opts.timeout ?? 1000 }); } /** Delete events based on filters from the database. */ @@ -171,7 +171,7 @@ class EventsDB implements NStore { if (!filters.length) return Promise.resolve(); this.console.debug('DELETE', JSON.stringify(filters)); - return this.store.remove(filters, { ...opts, timeout: opts.timeout ?? 5000 }); + return this.store.remove(filters, { ...opts, timeout: opts.timeout ?? 3000 }); } /** Get number of events that would be returned by filters. */ @@ -184,7 +184,7 @@ class EventsDB implements NStore { this.console.debug('COUNT', JSON.stringify(filters)); - return this.store.count(filters, { ...opts, timeout: opts.timeout ?? 1000 }); + return this.store.count(filters, { ...opts, timeout: opts.timeout ?? 500 }); } /** Return only the tags that should be indexed. */ From ea987dfa14a9b7d7dc9aba5cb434985ed71c2c1f Mon Sep 17 00:00:00 2001 From: Siddharth Singh Date: Mon, 1 Jul 2024 23:06:09 +0530 Subject: [PATCH 24/49] support raw pubkeys as well as npubs --- scripts/db-import.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/db-import.ts b/scripts/db-import.ts index e1484c99..954d83df 100644 --- a/scripts/db-import.ts +++ b/scripts/db-import.ts @@ -99,6 +99,8 @@ if (import.meta.main) { continue; } pubkeys.push(decoded); + } else if (NSchema.id().safeParse(arg).success) { + pubkeys.push(arg); } else { relaySectionBegun = true; if (!arg.startsWith('wss://')) { From 434056b83920bbd7312c35d3f893efabadaf148c Mon Sep 17 00:00:00 2001 From: Siddharth Singh Date: Mon, 1 Jul 2024 23:06:41 +0530 Subject: [PATCH 25/49] rename evt to event --- scripts/db-import.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/db-import.ts b/scripts/db-import.ts index 954d83df..806ce326 100644 --- a/scripts/db-import.ts +++ b/scripts/db-import.ts @@ -14,12 +14,12 @@ interface ImportEventsOpts { profilesOnly: boolean; } -type DoEvent = (evt: NostrEvent) => void | Promise; +type DoEvent = (event: NostrEvent) => void | Promise; const importUsers = async ( authors: string[], relays: string[], opts?: Partial, - doEvent: DoEvent = async (evt: NostrEvent) => await eventsDB.event(evt), + doEvent: DoEvent = async (event: NostrEvent) => await eventsDB.event(event), ) => { // Kind 0s + follow lists. const profiles: Record> = {}; From 98e9ccfc46b0fdd4a1ed65bee53433e53d60c874 Mon Sep 17 00:00:00 2001 From: Siddharth Singh Date: Mon, 1 Jul 2024 23:08:15 +0530 Subject: [PATCH 26/49] fix import --- scripts/db-import.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/db-import.ts b/scripts/db-import.ts index 806ce326..2c8a60f4 100644 --- a/scripts/db-import.ts +++ b/scripts/db-import.ts @@ -2,7 +2,7 @@ * Script to import a user/list of users into Ditto given their npub/pubkey by looking them up on a list of relays. */ -import { nip19 } from 'npm:nostr-tools@^2.7.0'; +import { nip19 } from 'nostr-tools'; import { DittoDB } from '@/db/DittoDB.ts'; import { EventsDB } from '@/storages/EventsDB.ts'; import { NostrEvent, NRelay1, NSchema } from '@nostrify/nostrify'; From e07be77de75f31387aa82b8f3f189e58ba8f166b Mon Sep 17 00:00:00 2001 From: Siddharth Singh Date: Mon, 1 Jul 2024 23:13:20 +0530 Subject: [PATCH 27/49] fix import order --- scripts/db-import.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/db-import.ts b/scripts/db-import.ts index 2c8a60f4..03e389cb 100644 --- a/scripts/db-import.ts +++ b/scripts/db-import.ts @@ -2,10 +2,11 @@ * Script to import a user/list of users into Ditto given their npub/pubkey by looking them up on a list of relays. */ +import { NostrEvent, NRelay1, NSchema } from '@nostrify/nostrify'; import { nip19 } from 'nostr-tools'; + import { DittoDB } from '@/db/DittoDB.ts'; import { EventsDB } from '@/storages/EventsDB.ts'; -import { NostrEvent, NRelay1, NSchema } from '@nostrify/nostrify'; const kysely = await DittoDB.getInstance(); const eventsDB = new EventsDB(kysely); From 6974b78952d9836134f3cc5cba2052da8bdba589 Mon Sep 17 00:00:00 2001 From: Siddharth Singh Date: Mon, 1 Jul 2024 23:21:29 +0530 Subject: [PATCH 28/49] db:import --> nostr:pull --- deno.json | 2 +- scripts/{db-import.ts => nostr-pull.ts} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename scripts/{db-import.ts => nostr-pull.ts} (100%) diff --git a/deno.json b/deno.json index 48c927f2..21ad4f2d 100644 --- a/deno.json +++ b/deno.json @@ -6,7 +6,7 @@ "dev": "deno run -A --watch src/server.ts", "hook": "deno run --allow-read --allow-run --allow-write https://deno.land/x/deno_hooks@0.1.1/mod.ts", "db:migrate": "deno run -A scripts/db-migrate.ts", - "db:import": "deno run -A scripts/db-import.ts", + "nostr:pull": "deno run -A scripts/nostr-pull.ts", "debug": "deno run -A --inspect src/server.ts", "test": "DATABASE_URL=\"sqlite://:memory:\" deno test -A --junit-path=./deno-test.xml", "check": "deno check src/server.ts", diff --git a/scripts/db-import.ts b/scripts/nostr-pull.ts similarity index 100% rename from scripts/db-import.ts rename to scripts/nostr-pull.ts From 4e150edb5b1f3bab308a1c3b5ff73f168c091206 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 1 Jul 2024 19:48:28 +0100 Subject: [PATCH 29/49] Actually enable query timeouts :facepalm: --- src/storages/EventsDB.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/storages/EventsDB.ts b/src/storages/EventsDB.ts index 61b87b00..69959fc0 100644 --- a/src/storages/EventsDB.ts +++ b/src/storages/EventsDB.ts @@ -45,6 +45,7 @@ class EventsDB implements NStore { constructor(private kysely: Kysely) { this.store = new NDatabase(kysely, { fts: Conf.db.dialect, + timeoutStrategy: Conf.db.dialect === 'postgres' ? 'setStatementTimeout' : undefined, indexTags: EventsDB.indexTags, searchText: EventsDB.searchText, }); From 3ae6d39ebc2f5f2aa80217ca682ec447b50ebc6b Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 1 Jul 2024 20:29:06 +0100 Subject: [PATCH 30/49] Increase notifications endpoint timeout --- src/controllers/api/notifications.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controllers/api/notifications.ts b/src/controllers/api/notifications.ts index d92ccf4a..51a20a05 100644 --- a/src/controllers/api/notifications.ts +++ b/src/controllers/api/notifications.ts @@ -78,7 +78,7 @@ async function renderNotifications( const store = c.get('store'); const pubkey = await c.get('signer')?.getPublicKey()!; const { signal } = c.req.raw; - const opts = { signal, limit: params.limit }; + const opts = { signal, limit: params.limit, timeout: 5000 }; const events = await store .query(filters, opts) From ae687bb52561298437a54cf36da61a242da18a95 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 1 Jul 2024 23:05:03 +0100 Subject: [PATCH 31/49] Increase timeouts --- src/controllers/api/notifications.ts | 2 +- src/controllers/nostr/relay.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/controllers/api/notifications.ts b/src/controllers/api/notifications.ts index 51a20a05..64a5a7ca 100644 --- a/src/controllers/api/notifications.ts +++ b/src/controllers/api/notifications.ts @@ -78,7 +78,7 @@ async function renderNotifications( const store = c.get('store'); const pubkey = await c.get('signer')?.getPublicKey()!; const { signal } = c.req.raw; - const opts = { signal, limit: params.limit, timeout: 5000 }; + const opts = { signal, limit: params.limit, timeout: 10_000 }; const events = await store .query(filters, opts) diff --git a/src/controllers/nostr/relay.ts b/src/controllers/nostr/relay.ts index 6d79b031..4d8ab2cb 100644 --- a/src/controllers/nostr/relay.ts +++ b/src/controllers/nostr/relay.ts @@ -73,7 +73,7 @@ function connectStream(socket: WebSocket) { const pubsub = await Storages.pubsub(); try { - for (const event of await store.query(filters, { limit: FILTER_LIMIT, timeout: 300 })) { + for (const event of await store.query(filters, { limit: FILTER_LIMIT, timeout: 1000 })) { send(['EVENT', subId, event]); } } catch (e) { From 5f0b1ffe79956d2693cdb6f8068f6b2b144d50d2 Mon Sep 17 00:00:00 2001 From: Siddharth Singh Date: Tue, 2 Jul 2024 04:56:38 +0530 Subject: [PATCH 32/49] fetch profiles separately from notes --- scripts/nostr-pull.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/scripts/nostr-pull.ts b/scripts/nostr-pull.ts index 03e389cb..d215e435 100644 --- a/scripts/nostr-pull.ts +++ b/scripts/nostr-pull.ts @@ -32,9 +32,11 @@ const importUsers = async ( await Promise.all(relays.map(async (relay) => { if (!relay.startsWith('wss://')) console.error(`Invalid relay url ${relay}`); const conn = new NRelay1(relay); - const kinds = [0, 3]; - if (!profilesOnly) kinds.push(1); - const matched = await conn.query([{ kinds, authors, limit: 1000 }]); + + const matched = [ + ...await conn.query([{ kinds: [0, 3], authors, limit: 1000 }]), + ...(!profilesOnly ? [] : await conn.query([{ kinds: [1], authors, limit: 1000 }])), + ]; await conn.close(); await Promise.all( matched.map(async (event) => { From f1e9eb4f4c87e14f840b6bb7d3bece1db9a2b9cf Mon Sep 17 00:00:00 2001 From: Siddharth Singh Date: Tue, 2 Jul 2024 04:58:06 +0530 Subject: [PATCH 33/49] fetch each author's notes separately --- scripts/nostr-pull.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/scripts/nostr-pull.ts b/scripts/nostr-pull.ts index d215e435..ae5b0183 100644 --- a/scripts/nostr-pull.ts +++ b/scripts/nostr-pull.ts @@ -35,8 +35,11 @@ const importUsers = async ( const matched = [ ...await conn.query([{ kinds: [0, 3], authors, limit: 1000 }]), - ...(!profilesOnly ? [] : await conn.query([{ kinds: [1], authors, limit: 1000 }])), + ...(!profilesOnly ? [] : await conn.query( + authors.map((author) => ({ kinds: [1], authors: [author], limit: 200 })), + )), ]; + await conn.close(); await Promise.all( matched.map(async (event) => { From d2df6721bdcf53092e4470dfa923e923fc7d97f7 Mon Sep 17 00:00:00 2001 From: Siddharth Singh Date: Tue, 2 Jul 2024 05:01:16 +0530 Subject: [PATCH 34/49] fix kind 1 querying logic --- scripts/nostr-pull.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/scripts/nostr-pull.ts b/scripts/nostr-pull.ts index ae5b0183..c3cbb67b 100644 --- a/scripts/nostr-pull.ts +++ b/scripts/nostr-pull.ts @@ -32,13 +32,14 @@ const importUsers = async ( await Promise.all(relays.map(async (relay) => { if (!relay.startsWith('wss://')) console.error(`Invalid relay url ${relay}`); const conn = new NRelay1(relay); - - const matched = [ - ...await conn.query([{ kinds: [0, 3], authors, limit: 1000 }]), - ...(!profilesOnly ? [] : await conn.query( - authors.map((author) => ({ kinds: [1], authors: [author], limit: 200 })), - )), - ]; + const matched = await conn.query([{ kinds: [0, 3], authors, limit: 1000 }]); + if (!profilesOnly) { + matched.push( + ...await conn.query( + authors.map((author) => ({ kinds: [1], authors: [author], limit: 200 })), + ), + ); + } await conn.close(); await Promise.all( From 757e5c9baaa75fbce45822a06c1bd2be89fe776c Mon Sep 17 00:00:00 2001 From: Siddharth Singh Date: Tue, 2 Jul 2024 05:21:16 +0530 Subject: [PATCH 35/49] print name as import acknowledgement --- scripts/nostr-pull.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/scripts/nostr-pull.ts b/scripts/nostr-pull.ts index c3cbb67b..1a236f8f 100644 --- a/scripts/nostr-pull.ts +++ b/scripts/nostr-pull.ts @@ -1,5 +1,6 @@ /** - * Script to import a user/list of users into Ditto given their npub/pubkey by looking them up on a list of relays. + * Script to import a user/list of users into Ditto given their npub/pubkey + * by looking them up on a list of relays. */ import { NostrEvent, NRelay1, NSchema } from '@nostrify/nostrify'; @@ -26,13 +27,13 @@ const importUsers = async ( const profiles: Record> = {}; // Kind 1s. const notes = new Set(); - const { profilesOnly = false } = opts || {}; await Promise.all(relays.map(async (relay) => { if (!relay.startsWith('wss://')) console.error(`Invalid relay url ${relay}`); const conn = new NRelay1(relay); const matched = await conn.query([{ kinds: [0, 3], authors, limit: 1000 }]); + if (!profilesOnly) { matched.push( ...await conn.query( @@ -65,6 +66,20 @@ const importUsers = async ( for (const kind in profile) { await doEvent(profile[kind]); } + + let name = user; + // kind 0, not first idx + const event = profile[0]; + if (event) { + // if event exists, print name + const parsed = JSON.parse(event.content); + name = parsed.nip05 || parsed.name || name; + } + if (NSchema.id().safeParse(name).success) { + // if no kind 0 found and this is a pubkey, encode as npub + name = nip19.npubEncode(name); + } + console.info(`Imported user ${name}${profilesOnly ? "'s profile" : ''}.`); } }; @@ -85,6 +100,7 @@ if (import.meta.main) { if (optionsEnd) { console.error('Option encountered after end of options section.'); showUsage(); + Deno.exit(1); } switch (arg) { case '-p': From c7092f5d2d69424eb2a6e1500c6c67a73cfd76b4 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 2 Jul 2024 09:22:59 +0100 Subject: [PATCH 36/49] Increase query timeout of feeds --- src/controllers/api/timelines.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/controllers/api/timelines.ts b/src/controllers/api/timelines.ts index 5d7862b5..e6f0ae78 100644 --- a/src/controllers/api/timelines.ts +++ b/src/controllers/api/timelines.ts @@ -60,9 +60,10 @@ const suggestedTimelineController: AppController = async (c) => { async function renderStatuses(c: AppContext, filters: NostrFilter[]) { const { signal } = c.req.raw; const store = c.get('store'); + const opts = { signal, timeout: 5000 }; const events = await store - .query(filters, { signal }) + .query(filters, opts) .then((events) => hydrateEvents({ events, store, signal })); if (!events.length) { From 3e7bab538a29f03e3be728d285c2272bbe1d6e0f Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 2 Jul 2024 20:13:44 +0100 Subject: [PATCH 37/49] Increase timeout for account statuses endpoint and feed endpoints --- src/controllers/api/accounts.ts | 4 +++- src/controllers/api/notifications.ts | 2 +- src/controllers/api/timelines.ts | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/controllers/api/accounts.ts b/src/controllers/api/accounts.ts index 9e77ccc1..ad4802ca 100644 --- a/src/controllers/api/accounts.ts +++ b/src/controllers/api/accounts.ts @@ -209,7 +209,9 @@ const accountStatusesController: AppController = async (c) => { filter['#t'] = [tagged]; } - const events = await store.query([filter], { signal }) + const opts = { signal, limit, timeout: 10_000 }; + + const events = await store.query([filter], opts) .then((events) => hydrateEvents({ events, store, signal })) .then((events) => { if (exclude_replies) { diff --git a/src/controllers/api/notifications.ts b/src/controllers/api/notifications.ts index 64a5a7ca..fab7c816 100644 --- a/src/controllers/api/notifications.ts +++ b/src/controllers/api/notifications.ts @@ -78,7 +78,7 @@ async function renderNotifications( const store = c.get('store'); const pubkey = await c.get('signer')?.getPublicKey()!; const { signal } = c.req.raw; - const opts = { signal, limit: params.limit, timeout: 10_000 }; + const opts = { signal, limit: params.limit, timeout: 15_000 }; const events = await store .query(filters, opts) diff --git a/src/controllers/api/timelines.ts b/src/controllers/api/timelines.ts index e6f0ae78..62f1cd2f 100644 --- a/src/controllers/api/timelines.ts +++ b/src/controllers/api/timelines.ts @@ -60,7 +60,7 @@ const suggestedTimelineController: AppController = async (c) => { async function renderStatuses(c: AppContext, filters: NostrFilter[]) { const { signal } = c.req.raw; const store = c.get('store'); - const opts = { signal, timeout: 5000 }; + const opts = { signal, timeout: 10_000 }; const events = await store .query(filters, opts) From a7f2fb06ee11492e552d32125fe06013058daae5 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Tue, 2 Jul 2024 17:09:17 -0300 Subject: [PATCH 38/49] feat(relay): send custom error messages - since, until & kind --- src/controllers/nostr/relay.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/controllers/nostr/relay.ts b/src/controllers/nostr/relay.ts index 4d8ab2cb..4f278d64 100644 --- a/src/controllers/nostr/relay.ts +++ b/src/controllers/nostr/relay.ts @@ -77,7 +77,10 @@ function connectStream(socket: WebSocket) { send(['EVENT', subId, event]); } } catch (e) { - if (e instanceof RelayError) { + if ( + e instanceof RelayError || + e.message.slice(-('filter too far into the future'.length)) === 'filter too far into the future' + ) { send(['CLOSED', subId, e.message]); } else { send(['CLOSED', subId, 'error: something went wrong']); From e6c38550c66a668d19b9b27c215ddf4b73b053eb Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Tue, 2 Jul 2024 17:15:47 -0300 Subject: [PATCH 39/49] feat: add onError() function hono handler --- src/app.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/app.ts b/src/app.ts index 6a2205b4..2e7d8791 100644 --- a/src/app.ts +++ b/src/app.ts @@ -340,6 +340,13 @@ app.get('/', frontendController, indexController); // Fallback app.get('*', publicFiles, staticFiles, frontendController); +app.onError((err, c) => { + if (err.message === 'canceling statement due to statement timeout') { + return c.json({ error: 'Everything will be fine, I mean it. Don\t worry child.' }, 500); + } + return c.json(500); +}); + export default app; export type { AppContext, AppController, AppMiddleware }; From cac5c9c1e0144fc80c20523bb2dfdb5c90611bdb Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Tue, 2 Jul 2024 17:17:29 -0300 Subject: [PATCH 40/49] refactor: change timeout error message --- src/app.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app.ts b/src/app.ts index 2e7d8791..c47d2579 100644 --- a/src/app.ts +++ b/src/app.ts @@ -342,7 +342,7 @@ app.get('*', publicFiles, staticFiles, frontendController); app.onError((err, c) => { if (err.message === 'canceling statement due to statement timeout') { - return c.json({ error: 'Everything will be fine, I mean it. Don\t worry child.' }, 500); + return c.json({ error: "A timeout happened, don't worry :)" }, 500); } return c.json(500); }); From 360efe089d1799547d9f77e2c94e18c7a238550b Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Tue, 2 Jul 2024 17:31:12 -0300 Subject: [PATCH 41/49] test(EventsDB): throw error for large since, until and kinds filter --- src/storages/EventsDB.test.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/storages/EventsDB.test.ts b/src/storages/EventsDB.test.ts index 16b429d4..fe8ffe8a 100644 --- a/src/storages/EventsDB.test.ts +++ b/src/storages/EventsDB.test.ts @@ -191,3 +191,33 @@ Deno.test('inserting replaceable events', async () => { await eventsDB.event(newerEvent); assertEquals(await eventsDB.query([{ kinds: [0] }]), [newerEvent]); }); + +Deno.test("throws a Error when querying an event with a large 'since'", async () => { + const { eventsDB } = await createDB(); + + await assertRejects( + () => eventsDB.query([{ since: 33333333333333 }]), + Error, + 'since filter too far into the future', + ); +}); + +Deno.test("throws a Error when querying an event with a large 'until'", async () => { + const { eventsDB } = await createDB(); + + await assertRejects( + () => eventsDB.query([{ until: 66666666666666 }]), + Error, + 'until filter too far into the future', + ); +}); + +Deno.test("throws a Error when querying an event with a large 'kind'", async () => { + const { eventsDB } = await createDB(); + + await assertRejects( + () => eventsDB.query([{ kinds: [99999999999999] }]), + Error, + 'kind filter too far into the future', + ); +}); From a868512188bdc8af2c1e28fdb1a6866df8347b01 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Tue, 2 Jul 2024 18:38:16 -0300 Subject: [PATCH 42/49] refactor: error messages in app.onError() --- src/app.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/app.ts b/src/app.ts index c47d2579..4c0ffc7e 100644 --- a/src/app.ts +++ b/src/app.ts @@ -342,9 +342,9 @@ app.get('*', publicFiles, staticFiles, frontendController); app.onError((err, c) => { if (err.message === 'canceling statement due to statement timeout') { - return c.json({ error: "A timeout happened, don't worry :)" }, 500); + return c.json({ error: 'The server was unable to respond in a timely manner' }, 500); } - return c.json(500); + return c.json({ error: 'Something went wrong' }, 500); }); export default app; From 2d017e81026078b9c9c399d10e6afc10817c27a5 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Tue, 2 Jul 2024 18:39:22 -0300 Subject: [PATCH 43/49] refactor(relay): remove invalid filter condition --- src/controllers/nostr/relay.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/controllers/nostr/relay.ts b/src/controllers/nostr/relay.ts index 4f278d64..f4193379 100644 --- a/src/controllers/nostr/relay.ts +++ b/src/controllers/nostr/relay.ts @@ -78,8 +78,7 @@ function connectStream(socket: WebSocket) { } } catch (e) { if ( - e instanceof RelayError || - e.message.slice(-('filter too far into the future'.length)) === 'filter too far into the future' + e instanceof RelayError ) { send(['CLOSED', subId, e.message]); } else { From e169749b8279943bef8f63cb8c3046c7f6c4fd09 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Tue, 2 Jul 2024 18:39:55 -0300 Subject: [PATCH 44/49] refactor(EventsDB): throw RelayError instead of Error --- src/storages/EventsDB.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/storages/EventsDB.ts b/src/storages/EventsDB.ts index 69959fc0..ce74f20d 100644 --- a/src/storages/EventsDB.ts +++ b/src/storages/EventsDB.ts @@ -147,14 +147,14 @@ class EventsDB implements NStore { for (const filter of filters) { if (filter.since && filter.since >= 2_147_483_647) { - throw new Error('since filter too far into the future'); + throw new RelayError('invalid', 'since filter too far into the future'); } if (filter.until && filter.until >= 2_147_483_647) { - throw new Error('until filter too far into the future'); + throw new RelayError('invalid', 'until filter too far into the future'); } for (const kind of filter.kinds ?? []) { if (kind >= 2_147_483_647) { - throw new Error('kind filter too far into the future'); + throw new RelayError('invalid', 'kind filter too far into the future'); } } } From d3c3ecfd35abc783cac8263062a0d564ae857e35 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Tue, 2 Jul 2024 18:40:21 -0300 Subject: [PATCH 45/49] test(EventsDB): refactor to use RelayError instead of Error --- src/storages/EventsDB.test.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/storages/EventsDB.test.ts b/src/storages/EventsDB.test.ts index fe8ffe8a..32838fa6 100644 --- a/src/storages/EventsDB.test.ts +++ b/src/storages/EventsDB.test.ts @@ -192,32 +192,32 @@ Deno.test('inserting replaceable events', async () => { assertEquals(await eventsDB.query([{ kinds: [0] }]), [newerEvent]); }); -Deno.test("throws a Error when querying an event with a large 'since'", async () => { +Deno.test("throws a RelayError when querying an event with a large 'since'", async () => { const { eventsDB } = await createDB(); await assertRejects( () => eventsDB.query([{ since: 33333333333333 }]), - Error, + RelayError, 'since filter too far into the future', ); }); -Deno.test("throws a Error when querying an event with a large 'until'", async () => { +Deno.test("throws a RelayError when querying an event with a large 'until'", async () => { const { eventsDB } = await createDB(); await assertRejects( () => eventsDB.query([{ until: 66666666666666 }]), - Error, + RelayError, 'until filter too far into the future', ); }); -Deno.test("throws a Error when querying an event with a large 'kind'", async () => { +Deno.test("throws a RelayError when querying an event with a large 'kind'", async () => { const { eventsDB } = await createDB(); await assertRejects( () => eventsDB.query([{ kinds: [99999999999999] }]), - Error, + RelayError, 'kind filter too far into the future', ); }); From ba290354138fa32fc6f948651378f40c674c526a Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Tue, 2 Jul 2024 18:43:28 -0300 Subject: [PATCH 46/49] refactor: put conditional into just one line --- src/controllers/nostr/relay.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/controllers/nostr/relay.ts b/src/controllers/nostr/relay.ts index f4193379..4d8ab2cb 100644 --- a/src/controllers/nostr/relay.ts +++ b/src/controllers/nostr/relay.ts @@ -77,9 +77,7 @@ function connectStream(socket: WebSocket) { send(['EVENT', subId, event]); } } catch (e) { - if ( - e instanceof RelayError - ) { + if (e instanceof RelayError) { send(['CLOSED', subId, e.message]); } else { send(['CLOSED', subId, 'error: something went wrong']); From 193dd1a011374fc85c8a857e071dabdfb04853a6 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 4 Jul 2024 23:38:16 +0100 Subject: [PATCH 47/49] EventsDB: remove queries for ephemeral kinds --- src/storages/EventsDB.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/storages/EventsDB.ts b/src/storages/EventsDB.ts index ce74f20d..f640cc45 100644 --- a/src/storages/EventsDB.ts +++ b/src/storages/EventsDB.ts @@ -279,6 +279,12 @@ class EventsDB implements NStore { filter.search = tokens.filter((t) => typeof t === 'string').join(' '); } + + if (filter.kinds) { + // Ephemeral events are not stored, so don't bother querying for them. + // If this results in an empty kinds array, NDatabase will remove the filter before querying and return no results. + filter.kinds = filter.kinds.filter((kind) => !NKinds.ephemeral(kind)); + } } return filters; From d4713cae012d7d05ed9264d6cbba2c3d1935c7c6 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 4 Jul 2024 23:53:20 +0100 Subject: [PATCH 48/49] Move errorHandler to a separate file --- src/app.ts | 8 ++------ src/controllers/error.ts | 11 +++++++++++ 2 files changed, 13 insertions(+), 6 deletions(-) create mode 100644 src/controllers/error.ts diff --git a/src/app.ts b/src/app.ts index 4c0ffc7e..f7f4f29a 100644 --- a/src/app.ts +++ b/src/app.ts @@ -108,6 +108,7 @@ import { trendingStatusesController, trendingTagsController, } from '@/controllers/api/trends.ts'; +import { errorHandler } from '@/controllers/error.ts'; import { metricsController } from '@/controllers/metrics.ts'; import { indexController } from '@/controllers/site.ts'; import { nodeInfoController, nodeInfoSchemaController } from '@/controllers/well-known/nodeinfo.ts'; @@ -340,12 +341,7 @@ app.get('/', frontendController, indexController); // Fallback app.get('*', publicFiles, staticFiles, frontendController); -app.onError((err, c) => { - if (err.message === 'canceling statement due to statement timeout') { - return c.json({ error: 'The server was unable to respond in a timely manner' }, 500); - } - return c.json({ error: 'Something went wrong' }, 500); -}); +app.onError(errorHandler); export default app; diff --git a/src/controllers/error.ts b/src/controllers/error.ts new file mode 100644 index 00000000..fa5e4d32 --- /dev/null +++ b/src/controllers/error.ts @@ -0,0 +1,11 @@ +import { ErrorHandler } from '@hono/hono'; + +export const errorHandler: ErrorHandler = (err, c) => { + console.error(err); + + if (err.message === 'canceling statement due to statement timeout') { + return c.json({ error: 'The server was unable to respond in a timely manner' }, 500); + } + + return c.json({ error: 'Something went wrong' }, 500); +}; From 96a8ccb2e637ffdf1fec979007377d46d321750f Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 5 Jul 2024 00:00:57 +0100 Subject: [PATCH 49/49] HTTP Response metrics --- src/app.ts | 15 +++++++-------- src/metrics.ts | 6 ++++++ src/middleware/metricsMiddleware.ts | 6 +++++- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/app.ts b/src/app.ts index f7f4f29a..1cb3746b 100644 --- a/src/app.ts +++ b/src/app.ts @@ -152,18 +152,17 @@ if (Conf.cronEnabled) { app.use('*', rateLimitMiddleware(300, Time.minutes(5))); -app.use('/api/*', logger(debug)); -app.use('/.well-known/*', logger(debug)); -app.use('/users/*', logger(debug)); -app.use('/nodeinfo/*', logger(debug)); -app.use('/oauth/*', logger(debug)); +app.use('/api/*', metricsMiddleware, logger(debug)); +app.use('/.well-known/*', metricsMiddleware, logger(debug)); +app.use('/users/*', metricsMiddleware, logger(debug)); +app.use('/nodeinfo/*', metricsMiddleware, logger(debug)); +app.use('/oauth/*', metricsMiddleware, logger(debug)); -app.get('/api/v1/streaming', streamingController); -app.get('/relay', relayController); +app.get('/api/v1/streaming', metricsMiddleware, streamingController); +app.get('/relay', metricsMiddleware, relayController); app.use( '*', - metricsMiddleware, cspMiddleware(), cors({ origin: '*', exposeHeaders: ['link'] }), signerMiddleware, diff --git a/src/metrics.ts b/src/metrics.ts index 96d91599..67da3f49 100644 --- a/src/metrics.ts +++ b/src/metrics.ts @@ -6,6 +6,12 @@ export const httpRequestCounter = new Counter({ labelNames: ['method'], }); +export const httpResponseCounter = new Counter({ + name: 'http_responses_total', + help: 'Total number of HTTP responses', + labelNames: ['status', 'path'], +}); + export const streamingConnectionsGauge = new Gauge({ name: 'streaming_connections', help: 'Number of active connections to the streaming API', diff --git a/src/middleware/metricsMiddleware.ts b/src/middleware/metricsMiddleware.ts index 1a491186..d7ac43d5 100644 --- a/src/middleware/metricsMiddleware.ts +++ b/src/middleware/metricsMiddleware.ts @@ -1,10 +1,14 @@ import { MiddlewareHandler } from '@hono/hono'; -import { httpRequestCounter } from '@/metrics.ts'; +import { httpRequestCounter, httpResponseCounter } from '@/metrics.ts'; export const metricsMiddleware: MiddlewareHandler = async (c, next) => { const { method } = c.req; httpRequestCounter.inc({ method }); await next(); + + const { status } = c.res; + const path = c.req.matchedRoutes.find((r) => r.method !== 'ALL')?.path ?? c.req.routePath; + httpResponseCounter.inc({ status, path }); };