-
Notifications
You must be signed in to change notification settings - Fork 235
feat: wot core service #642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
saniddhyaDubey
wants to merge
5
commits into
cameri:main
Choose a base branch
from
saniddhyaDubey:feat/wot#2-service
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5064c67
feat: WoT graph service with injected fetcher and unit tests
saniddhyaDubey ab42c1c
docs(changeset): added wot graph service and unit test
saniddhyaDubey e11d502
chore: ignore wot-service.ts in knip until worker is wired in PR#3
saniddhyaDubey 3241193
refactor(wot): rewrite service to use PostgreSQL for trust graph
saniddhyaDubey a36249d
docs(changeset): rewrite wot-service with PostgreSQL trust graph
saniddhyaDubey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "nostream": minor | ||
| --- | ||
|
|
||
| Rewrite WoT service to use PostgreSQL for trust graph — eliminates unbounded in-memory accumulation by streaming kind-3 events into the DB and computing trust via SQL GROUP BY |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "nostream": minor | ||
| --- | ||
|
|
||
| added wot graph service and unit test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,241 @@ | ||
| import { RawData, WebSocket } from 'ws' | ||
| import { randomUUID } from 'crypto' | ||
|
|
||
| import { isEventIdValid, isEventSignatureValid } from '../utils/event' | ||
| import { createLogger } from '../factories/logger-factory' | ||
| import { IEventRepository } from '../@types/repositories' | ||
| import { IWotService } from '../@types/services' | ||
| import { WoTSettings } from '../@types/settings' | ||
|
|
||
| const logger = createLogger('wot-service') | ||
|
|
||
| export const PHASE1_TIMEOUT_MS = 5_000 | ||
| export const PHASE2_BATCH_SIZE = 500 | ||
| export const PHASE2_CONCURRENCY = 5 | ||
| export const PHASE2_BATCH_TIMEOUT_MS = 30_000 | ||
|
|
||
| const KIND_FOLLOW_LIST = 3 | ||
|
|
||
| /** | ||
| * Open a WebSocket to `relayUrl`, send a REQ for `filter`, and yield each | ||
| * EVENT payload as it arrives. Closes on EOSE or timeout. | ||
| */ | ||
| async function* fetchEvents( | ||
| relayUrl: string, | ||
| filter: Record<string, unknown>, | ||
| timeoutMs: number, | ||
| ): AsyncGenerator<any> { | ||
| const subId = `wot-${randomUUID().slice(0, 8)}` | ||
|
|
||
| const queue: any[] = [] | ||
| let done = false | ||
| let notify: (() => void) | null = null | ||
|
|
||
| const wake = () => { | ||
| const fn = notify | ||
| notify = null | ||
| fn?.() | ||
| } | ||
|
|
||
| const finish = () => { | ||
| if (done) { | ||
| return | ||
| } | ||
| done = true | ||
| clearTimeout(timer) | ||
| try { ws.close() } catch { /* ignore */ } | ||
| wake() | ||
| } | ||
|
|
||
| const timer = setTimeout(finish, timeoutMs) | ||
|
|
||
| let ws: WebSocket | ||
| try { | ||
| ws = new WebSocket(relayUrl, { timeout: timeoutMs }) | ||
| } catch (err) { | ||
| logger.warn('wot-service: could not create WebSocket to %s: %o', relayUrl, err) | ||
| clearTimeout(timer) | ||
| return | ||
| } | ||
|
|
||
| ws.on('open', () => { | ||
| ws.send(JSON.stringify(['REQ', subId, filter])) | ||
| }) | ||
|
|
||
| ws.on('message', (raw: RawData) => { | ||
| try { | ||
| const msg = JSON.parse(raw.toString('utf8')) | ||
| if (!Array.isArray(msg)) { | ||
| return | ||
| } | ||
|
|
||
| if (msg[0] === 'EVENT' && msg[1] === subId && msg[2]) { | ||
| queue.push(msg[2]) | ||
| wake() | ||
| } else if (msg[0] === 'EOSE' && msg[1] === subId) { | ||
| finish() | ||
| } | ||
| } catch { /* malformed message — ignore */ } | ||
| }) | ||
|
|
||
| ws.on('error', (err) => { | ||
| logger.warn('wot-service: WebSocket error for %s: %o', relayUrl, err) | ||
| finish() | ||
| }) | ||
|
|
||
| ws.on('close', finish) | ||
|
|
||
| while (true) { | ||
| while (queue.length > 0) { | ||
| yield queue.shift() | ||
| } | ||
| if (done) { | ||
| break | ||
| } | ||
| await new Promise<void>((resolve) => { notify = resolve }) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Run up to `concurrency` async tasks from `items` at a time. | ||
| */ | ||
| async function runConcurrent<T>( | ||
| items: T[], | ||
| concurrency: number, | ||
| task: (item: T) => Promise<void>, | ||
| ): Promise<void> { | ||
| const queue = [...items] | ||
| const workers = Array.from({ length: Math.min(concurrency, queue.length) }, async () => { | ||
| while (queue.length > 0) { | ||
| const item = queue.shift() | ||
| if (item !== undefined) { | ||
| await task(item) | ||
| } | ||
| } | ||
| }) | ||
| await Promise.all(workers) | ||
| } | ||
|
|
||
| export type RelayFetcher = ( | ||
| relayUrl: string, | ||
| filter: Record<string, unknown>, | ||
| timeoutMs: number, | ||
| ) => AsyncGenerator<any> | ||
|
|
||
| export class WotService implements IWotService { | ||
| private booted = false | ||
| private building = false | ||
|
|
||
| public constructor( | ||
| private readonly eventRepository: IEventRepository, | ||
| private readonly fetcher: RelayFetcher = fetchEvents, | ||
| ) {} | ||
|
|
||
| public async buildGraph(settings: WoTSettings): Promise<string[]> { | ||
| if (this.building) { | ||
| logger('build already in progress — skipping') | ||
| return [] | ||
| } | ||
|
|
||
| this.building = true | ||
| logger.info('starting WoT graph build, seed=%s relays=%o', settings.seedPubkey, settings.seedRelays) | ||
|
|
||
| try { | ||
| // ── Phase 1: fetch and store seed's kind-3 follow list ────────────────── | ||
| for (const relayUrl of settings.seedRelays) { | ||
| for await (const event of this.fetcher( | ||
| relayUrl, | ||
| { authors: [settings.seedPubkey], kinds: [KIND_FOLLOW_LIST] }, | ||
| PHASE1_TIMEOUT_MS, | ||
| )) { | ||
| if (!(await isEventIdValid(event)) || !(await isEventSignatureValid(event))) { | ||
| continue | ||
| } | ||
| await this.eventRepository.upsert(event) | ||
| } | ||
| } | ||
|
|
||
| logger.info('phase 1 complete: seed kind-3 events stored') | ||
|
|
||
| // ── Phase 1B: read 1-hop pubkeys from DB ──────────────────────────────── | ||
| const oneHopRows = await (this.eventRepository as any).masterDbClient('event_tags') | ||
| .select('tag_value') | ||
| .whereIn( | ||
| 'event_id', | ||
| (this.eventRepository as any).masterDbClient('events') | ||
| .select('event_id') | ||
| .where('event_pubkey', Buffer.from(settings.seedPubkey, 'hex')) | ||
| .where('event_kind', KIND_FOLLOW_LIST), | ||
| ) | ||
| .where('tag_name', 'p') | ||
|
|
||
| const oneHopPubkeys: string[] = oneHopRows.map((r: any) => r.tag_value as string) | ||
|
|
||
| logger.info('phase 1B complete: %d 1-hop pubkeys', oneHopPubkeys.length) | ||
|
|
||
| if (oneHopPubkeys.length === 0) { | ||
| this.booted = true | ||
| return [settings.seedPubkey] | ||
| } | ||
|
|
||
| // ── Phase 2: fetch and store 1-hop kind-3 events (batched) ────────────── | ||
| const batches: string[][] = [] | ||
| for (let i = 0; i < oneHopPubkeys.length; i += PHASE2_BATCH_SIZE) { | ||
| batches.push(oneHopPubkeys.slice(i, i + PHASE2_BATCH_SIZE)) | ||
| } | ||
|
|
||
| await runConcurrent(batches, PHASE2_CONCURRENCY, async (batch) => { | ||
| for (const relayUrl of settings.seedRelays) { | ||
| try { | ||
| for await (const event of this.fetcher( | ||
| relayUrl, | ||
| { authors: batch, kinds: [KIND_FOLLOW_LIST] }, | ||
| PHASE2_BATCH_TIMEOUT_MS, | ||
| )) { | ||
| if (!(await isEventIdValid(event)) || !(await isEventSignatureValid(event))) { | ||
| continue | ||
| } | ||
| await this.eventRepository.upsert(event) | ||
| } | ||
| } catch (err) { | ||
| logger.warn('wot-service: phase 2 batch failed for %s: %o', relayUrl, err) | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| logger.info('phase 2 complete: 1-hop kind-3 events stored') | ||
|
|
||
| // ── Phase 3: SQL trust query ───────────────────────────────────────────── | ||
| const trustedRows = await (this.eventRepository as any).masterDbClient('event_tags') | ||
| .select('tag_value') | ||
| .whereIn( | ||
| 'event_id', | ||
| (this.eventRepository as any).masterDbClient('events') | ||
| .select('event_id') | ||
| .whereIn('event_pubkey', oneHopPubkeys.map((pk) => Buffer.from(pk, 'hex'))) | ||
| .where('event_kind', KIND_FOLLOW_LIST), | ||
| ) | ||
| .where('tag_name', 'p') | ||
| .groupBy('tag_value') | ||
| .havingRaw('COUNT(*) >= ?', [settings.minimumFollowers]) | ||
|
|
||
| const trustedSet = new Set<string>(trustedRows.map((r: any) => r.tag_value as string)) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this can be done in SQL using DISTINCT |
||
| trustedSet.add(settings.seedPubkey) | ||
| const trustedPubkeys = Array.from(trustedSet) | ||
|
|
||
| this.booted = true | ||
| logger.info('WoT graph build complete: %d trusted pubkeys', trustedPubkeys.length) | ||
|
|
||
| return trustedPubkeys | ||
| } catch (err) { | ||
| logger.error('wot-service: graph build failed: %o', err) | ||
| throw err | ||
| } finally { | ||
| this.building = false | ||
| } | ||
| } | ||
|
|
||
| public isReady(): boolean { | ||
| return this.booted | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@saniddhyaDubey this is outside the database layer and needs to be refactored into a repository