cameri / nostream

A Nostr Relay written in TypeScript
MIT License
741 stars 190 forks source link

[BUG] Throws error when receiving a replaceable event after its delete event #343

Closed jjramirezn closed 8 months ago

jjramirezn commented 10 months ago

Describe the bug When nostream receives a replaceable event that was previously deleted, an error is thrown because the event already exists in the db.

To Reproduce Steps to reproduce the behavior:

  1. Prepare a replaceable event e without publishing
  2. Publish a delete event with an e-tag equal to the id of e
  3. Publish e

Expected behavior I am not sure what would be the expected behavior for this, but what currently happens for regular events with this same flow if that in step 3 the relay returns duplicated: (no error is thrown)

System (please complete the following information):

Logs

2023-11-16T14:36:12.107Z worker:parameterized-replaceable-event-strategy received parameterized replaceable event: { id: '5a13cf84ff0aeb464f61ca283943dfb851c91d3594bcf4171354d274e4b8efc4', pubkey: 'ab343e776b4807dde2f5c41d1577a173ed98b4dfd70873eb5dd9d2924d45bdc7', created_at: 1700145302, kind: 31112, tags: [ [ 'd', 'test' ] ], content: 'replaceable', sig: '37e9e54428deb29a55b8c8e57efd6ec76f2b4330c59f11e0e69bace666cab3f4bc7b406b69e903d3d924f617121038918e4a57b5b2a25b4f8a521a7e771daf7e', [Symbol(Context)]: { remoteAddress: SocketAddress { address: '::ffff:10.10.10.1', port: 0, family: 'ipv6', flowlabel: 0 } } }
2023-11-16T14:36:12.108Z worker:event-repository upserting event: { id: '5a13cf84ff0aeb464f61ca283943dfb851c91d3594bcf4171354d274e4b8efc4', pubkey: 'ab343e776b4807dde2f5c41d1577a173ed98b4dfd70873eb5dd9d2924d45bdc7', created_at: 1700145302, kind: 31112, tags: [ [ 'd', 'test' ] ], content: 'replaceable', sig: '37e9e54428deb29a55b8c8e57efd6ec76f2b4330c59f11e0e69bace666cab3f4bc7b406b69e903d3d924f617121038918e4a57b5b2a25b4f8a521a7e771daf7e', [Symbol(Context)]: { remoteAddress: SocketAddress { address: '::ffff:10.10.10.1', port: 0, family: 'ipv6', flowlabel: 0 } }, [Symbol(Deduplication)]: [ 'test' ] }
2023-11-16T14:36:12.109Z knex:client acquired connection from pool: __knexUid1
2023-11-16T14:36:12.110Z knex:query insert into "events" ("event_content", "event_created_at", "event_deduplication", "event_delegator", "event_id", "event_kind", "event_pubkey", "event_signature", "event_tags", "expires_at", "remote_address") values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) on conflict (event_pubkey, event_kind, event_deduplication) WHERE (event_kind = 0 OR event_kind = 3 OR event_kind = 41 OR (event_kind >= 10000 AND event_kind < 20000)) OR (event_kind >= 30000 AND event_kind < 40000) do update set "event_id" = $12,"event_created_at" = $13,"event_tags" = $14,"event_content" = $15,"event_signature" = $16,"event_delegator" = $17,"remote_address" = $18,"expires_at" = $19 where "events"."event_created_at" < $20 undefined
2023-11-16T14:36:12.110Z knex:bindings [
  'replaceable',
  1700145302,
  '["test"]',
  null,
  <Buffer 5a 13 cf 84 ff 0a eb 46 4f 61 ca 28 39 43 df b8 51 c9 1d 35 94 bc f4 17 13 54 d2 74 e4 b8 ef c4>,
  31112,
  <Buffer ab 34 3e 77 6b 48 07 dd e2 f5 c4 1d 15 77 a1 73 ed 98 b4 df d7 08 73 eb 5d d9 d2 92 4d 45 bd c7>,
  <Buffer 37 e9 e5 44 28 de b2 9a 55 b8 c8 e5 7e fd 6e c7 6f 2b 43 30 c5 9f 11 e0 e6 9b ac e6 66 ca b3 f4 bc 7b 40 6b 69 e9 03 d3 d9 24 f6 17 12 10 38 91 8e 4a ... 14 more bytes>,
  '[["d","test"]]',
  null,
  '::ffff:10.10.10.1',
  <Buffer 5a 13 cf 84 ff 0a eb 46 4f 61 ca 28 39 43 df b8 51 c9 1d 35 94 bc f4 17 13 54 d2 74 e4 b8 ef c4>,
  1700145302,
  '[["d","test"]]',
  'replaceable',
  <Buffer 37 e9 e5 44 28 de b2 9a 55 b8 c8 e5 7e fd 6e c7 6f 2b 43 30 c5 9f 11 e0 e6 9b ac e6 66 ca b3 f4 bc 7b 40 6b 69 e9 03 d3 d9 24 f6 17 12 10 38 91 8e 4a ... 14 more bytes>,
  null,
  '::ffff:10.10.10.1',
  null,
  1700145302
] undefined
error handling message [
  'EVENT',
  {
    id: '5a13cf84ff0aeb464f61ca283943dfb851c91d3594bcf4171354d274e4b8efc4',
    pubkey: 'ab343e776b4807dde2f5c41d1577a173ed98b4dfd70873eb5dd9d2924d45bdc7',
    created_at: 1700145302,
    kind: 31112,
    tags: [ [Array] ],
    content: 'replaceable',
    sig: '37e9e54428deb29a55b8c8e57efd6ec76f2b4330c59f11e0e69bace666cab3f4bc7b406b69e903d3d924f617121038918e4a57b5b2a25b4f8a521a7e771daf7e',
    [Symbol(Context)]: { remoteAddress: [SocketAddress] }
  },
  [Symbol(Context)]: {
    remoteAddress: SocketAddress { address: '::ffff:10.10.10.1', port: 0, family: 'ipv6', flowlabel: 0 }
  }
] error: insert into "events" ("event_content", "event_created_at", "event_deduplication", "event_delegator", "event_id", "event_kind", "event_pubkey", "event_signature", "event_tags", "expires_at", "remote_address") values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) on conflict (event_pubkey, event_kind, event_deduplication) WHERE (event_kind = 0 OR event_kind = 3 OR event_kind = 41 OR (event_kind >= 10000 AND event_kind < 20000)) OR (event_kind >= 30000 AND event_kind < 40000) do update set "event_id" = $12,"event_created_at" = $13,"event_tags" = $14,"event_content" = $15,"event_signature" = $16,"event_delegator" = $17,"remote_address" = $18,"expires_at" = $19 where "events"."event_created_at" < $20 - duplicate key value violates unique constraint "events_event_id_unique"
    at Parser.parseErrorMessage (/app/node_modules/pg-protocol/dist/parser.js:287:98)
    at Parser.handlePacket (/app/node_modules/pg-protocol/dist/parser.js:126:29)
    at Parser.parse (/app/node_modules/pg-protocol/dist/parser.js:39:38)
    at Socket.<anonymous> (/app/node_modules/pg-protocol/dist/index.js:11:42)
    at Socket.emit (node:events:513:28)
    at addChunk (node:internal/streams/readable:324:12)
    at readableAddChunk (node:internal/streams/readable:297:9)
    at Readable.push (node:internal/streams/readable:234:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
  length: 276,
  severity: 'ERROR',
  code: '23505',
  detail: 'Key (event_id)=(\\x5a13cf84ff0aeb464f61ca283943dfb851c91d3594bcf4171354d274e4b8efc4) already exists.',
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: 'public',
  table: 'events',
  column: undefined,
  dataType: undefined,
  constraint: 'events_event_id_unique',
  file: 'nbtinsert.c',
  line: '666',
  routine: '_bt_check_unique'
}
cameri commented 8 months ago

@jjramirezn I am unable to reproduce this error. Could you provide 1) the first replaceable event, 2) the delete event, and 3) the new replaceable event ?

cameri commented 8 months ago

Nevermind, I am able to reproduce it. Will try to work on a fix.

cameri commented 8 months ago

Closed by https://github.com/Cameri/nostream/pull/354