diff --git a/packages/app/src/Cache/IndexedDB.ts b/packages/app/src/Cache/IndexedDB.ts index df0fe97a..5fa7b959 100644 --- a/packages/app/src/Cache/IndexedDB.ts +++ b/packages/app/src/Cache/IndexedDB.ts @@ -1,7 +1,7 @@ import Dexie, { Table } from "dexie"; import { TaggedNostrEvent, ReqFilter as Filter } from "@snort/system"; import * as Comlink from "comlink"; -import LRUSet from "@/Cache/LRUSet"; +import LRUSet from "@snort/shared/src/LRUSet"; type Tag = { id: string; diff --git a/packages/app/src/Cache/LRUSet.ts b/packages/shared/src/LRUSet.ts similarity index 100% rename from packages/app/src/Cache/LRUSet.ts rename to packages/shared/src/LRUSet.ts diff --git a/packages/system/src/connection.ts b/packages/system/src/connection.ts index ec51ce99..86660060 100644 --- a/packages/system/src/connection.ts +++ b/packages/system/src/connection.ts @@ -9,6 +9,8 @@ import { ConnectionStats } from "./connection-stats"; import { NostrEvent, ReqCommand, ReqFilter, TaggedNostrEvent, u256 } from "./nostr"; import { RelayInfo } from "./relay-info"; import EventKind from "./event-kind"; +import seenEvents from "./seen-events"; +import {getHex64} from "./utils"; /** * Relay settings @@ -199,6 +201,14 @@ export class Connection extends EventEmitter { OnMessage(e: WebSocket.MessageEvent) { this.#activity = unixNowMs(); if ((e.data as string).length > 0) { + // skip message processing if we've already seen it + const msgId = getHex64(e.data as string, "id"); + if (seenEvents.has(msgId)) { + console.log('already seen'); + return; + } + seenEvents.add(msgId); // TODO only do after msg validation + const msg = JSON.parse(e.data as string) as Array; const tag = msg[0] as string; switch (tag) { diff --git a/packages/system/src/seen-events.ts b/packages/system/src/seen-events.ts new file mode 100644 index 00000000..20a9cf85 --- /dev/null +++ b/packages/system/src/seen-events.ts @@ -0,0 +1,3 @@ +import LRUSet from "@snort/shared/src/LRUSet"; + +export default new LRUSet(2000); \ No newline at end of file diff --git a/packages/system/src/utils.ts b/packages/system/src/utils.ts index 6bfd3802..2787d54b 100644 --- a/packages/system/src/utils.ts +++ b/packages/system/src/utils.ts @@ -82,3 +82,10 @@ export function parseIMeta(tags: Array>) { } return ret; } + +export function getHex64(json: string, field: string): string { + let len = field.length + 3 + let idx = json.indexOf(`"${field}":`) + len + let s = json.slice(idx).indexOf(`"`) + idx + 1 + return json.slice(s, s + 64) +}