Improve relay tracking with thunks, add/rename some kinds

This commit is contained in:
Jon Staab
2025-10-24 06:41:56 -07:00
parent c026084635
commit a03a289e93
6 changed files with 29 additions and 20 deletions
+9 -1
View File
@@ -136,7 +136,6 @@ export class Thunk {
...this.options, ...this.options,
event, event,
onSuccess: (result: PublishResult) => { onSuccess: (result: PublishResult) => {
tracker.track(event.id, result.relay)
this.options.onSuccess?.(result) this.options.onSuccess?.(result)
this.results[result.relay] = result this.results[result.relay] = result
this._notify() this._notify()
@@ -150,6 +149,10 @@ export class Thunk {
onTimeout: this._setTimeout, onTimeout: this._setTimeout,
onAborted: this._setAborted, onAborted: this._setAborted,
onComplete: (result: PublishResult) => { onComplete: (result: PublishResult) => {
if (result.status !== PublishStatus.Success) {
tracker.removeRelay(event.id, result.relay)
}
this.options.onComplete?.(result) this.options.onComplete?.(result)
this._subs = [] this._subs = []
}, },
@@ -201,6 +204,11 @@ export class Thunk {
enqueue() { enqueue() {
thunkQueue.push(this) thunkQueue.push(this)
for (const url of this.options.relays) {
tracker.track(this.event.id, url)
}
repository.publish(this.event) repository.publish(this.event)
thunks.update($thunks => append(this, $thunks)) thunks.update($thunks => append(this, $thunks))
+2 -2
View File
@@ -17,7 +17,7 @@ export type MakeUserDataOptions<T> = {
loadItem: UserDataLoader loadItem: UserDataLoader
} }
const makeUserData = <T>({mapStore, loadItem}: MakeUserDataOptions<T>) => export const makeUserData = <T>({mapStore, loadItem}: MakeUserDataOptions<T>) =>
withGetter( withGetter(
derived([mapStore, pubkey], ([$mapStore, $pubkey]) => { derived([mapStore, pubkey], ([$mapStore, $pubkey]) => {
if (!$pubkey) return undefined if (!$pubkey) return undefined
@@ -28,7 +28,7 @@ const makeUserData = <T>({mapStore, loadItem}: MakeUserDataOptions<T>) =>
}), }),
) )
const makeUserLoader = export const makeUserLoader =
(loadItem: UserDataLoader) => (loadItem: UserDataLoader) =>
async (relays: string[] = [], force = false) => { async (relays: string[] = [], force = false) => {
const $pubkey = pubkey.get() const $pubkey = pubkey.get()
+2 -2
View File
@@ -1452,9 +1452,9 @@ export const displayList = <T>(xs: T[], conj = "and", n = 6) => {
return `${xs.slice(0, -1).join(", ")}, ${conj} ${xs.slice(-1).join("")}` return `${xs.slice(0, -1).join(", ")}, ${conj} ${xs.slice(-1).join("")}`
} }
/** Generates a hash string from input string */ /** Generates a hash from input string */
export const hash = (s: string) => export const hash = (s: string) =>
Math.abs(s.split("").reduce((a, b) => ((a << 5) - a + b.charCodeAt(0)) | 0, 0)).toString() Math.abs(s.split("").reduce((a, b) => ((a << 5) - a + b.charCodeAt(0)) | 0, 0))
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Curried utilities for working with collections // Curried utilities for working with collections
+6 -6
View File
@@ -1,4 +1,4 @@
import {AUTH_JOIN} from "@welshman/util" import {RELAY_JOIN} from "@welshman/util"
import {describe, expect, it, vi, beforeEach, afterEach} from "vitest" import {describe, expect, it, vi, beforeEach, afterEach} from "vitest"
import {Socket, SocketStatus, SocketEvent} from "../src/socket" import {Socket, SocketStatus, SocketEvent} from "../src/socket"
import {AuthStatus, AuthStateEvent} from "../src/auth" import {AuthStatus, AuthStateEvent} from "../src/auth"
@@ -56,7 +56,7 @@ describe("policy", () => {
expect(sendSpy).toHaveBeenCalledWith(authEvent) expect(sendSpy).toHaveBeenCalledWith(authEvent)
// Auth join event should not be buffered // Auth join event should not be buffered
const joinEvent: ClientMessage = ["EVENT", {id: "789", kind: AUTH_JOIN}] const joinEvent: ClientMessage = ["EVENT", {id: "789", kind: RELAY_JOIN}]
socket.send(joinEvent) socket.send(joinEvent)
expect(sendSpy).toHaveBeenCalledWith(joinEvent) expect(sendSpy).toHaveBeenCalledWith(joinEvent)
@@ -159,21 +159,21 @@ describe("policy", () => {
cleanup() cleanup()
}) })
it("should not retry AUTH_JOIN events", () => { it("should not retry RELAY_JOIN events", () => {
const cleanup = socketPolicyAuthBuffer(socket) const cleanup = socketPolicyAuthBuffer(socket)
const sendSpy = vi.spyOn(socket, "send") const sendSpy = vi.spyOn(socket, "send")
// Send an AUTH_JOIN event // Send an RELAY_JOIN event
const event: ClientMessage = [ const event: ClientMessage = [
"EVENT", "EVENT",
{id: "123", kind: AUTH_JOIN, content: "", tags: [], pubkey: "", sig: ""}, {id: "123", kind: RELAY_JOIN, content: "", tags: [], pubkey: "", sig: ""},
] ]
socket.emit(SocketEvent.Send, event) socket.emit(SocketEvent.Send, event)
// Receive auth-required rejection // Receive auth-required rejection
socket.emit(SocketEvent.Receive, ["OK", "123", false, "auth-required: need to auth first"]) socket.emit(SocketEvent.Receive, ["OK", "123", false, "auth-required: need to auth first"])
// Should not retry AUTH_JOIN events // Should not retry RELAY_JOIN events
expect(sendSpy).not.toHaveBeenCalled() expect(sendSpy).not.toHaveBeenCalled()
cleanup() cleanup()
+2 -2
View File
@@ -1,5 +1,5 @@
import {on, ms, nthNe, always, call, sleep, ago, now} from "@welshman/lib" import {on, ms, nthNe, always, call, sleep, ago, now} from "@welshman/lib"
import {AUTH_JOIN, StampedEvent, SignedEvent} from "@welshman/util" import {RELAY_JOIN, StampedEvent, SignedEvent} from "@welshman/util"
import { import {
ClientMessage, ClientMessage,
isClientAuth, isClientAuth,
@@ -42,7 +42,7 @@ export const socketPolicyAuthBuffer = (socket: Socket) => {
if (isClientAuth(message)) return if (isClientAuth(message)) return
// Always allow sending join requests // Always allow sending join requests
if (isClientEvent(message) && message[1].kind === AUTH_JOIN) return if (isClientEvent(message) && message[1].kind === RELAY_JOIN) return
// If the auth flow is complete, no need to buffer anymore // If the auth flow is complete, no need to buffer anymore
if (terminalStatuses.includes(socket.auth.status)) return if (terminalStatuses.includes(socket.auth.status)) return
+8 -7
View File
@@ -106,10 +106,10 @@ export const DVM_RESPONSE_OTS = 6900
export const DVM_RESPONSE_OP_RETURN = 6901 export const DVM_RESPONSE_OP_RETURN = 6901
export const DVM_RESPONSE_PUBLISH_SCHEDULE = 6905 export const DVM_RESPONSE_PUBLISH_SCHEDULE = 6905
export const DVM_FEEDBACK = 7000 export const DVM_FEEDBACK = 7000
export const RELAY_ADD_USER = 8000 export const RELAY_ADD_MEMBER = 8000
export const RELAY_REMOVE_USER = 8001 export const RELAY_REMOVE_MEMBER = 8001
export const ROOM_ADD_USER = 9000 export const ROOM_ADD_MEMBER = 9000
export const ROOM_REMOVE_USER = 9001 export const ROOM_REMOVE_MEMBER = 9001
export const ROOM_EDIT_META = 9002 export const ROOM_EDIT_META = 9002
export const ROOM_ADD_PERM = 9003 export const ROOM_ADD_PERM = 9003
export const ROOM_REMOVE_PERM = 9004 export const ROOM_REMOVE_PERM = 9004
@@ -142,8 +142,9 @@ export const RELAY_MEMBERS = 13534
export const LIGHTNING_PUB_RPC = 21000 export const LIGHTNING_PUB_RPC = 21000
export const CLIENT_AUTH = 22242 export const CLIENT_AUTH = 22242
export const BLOSSOM_AUTH = 24242 export const BLOSSOM_AUTH = 24242
export const AUTH_JOIN = 28934 export const RELAY_JOIN = 28934
export const AUTH_INVITE = 28935 export const RELAY_INVITE = 28935
export const RELAY_LEAVE = 28936
export const WALLET_INFO = 13194 export const WALLET_INFO = 13194
export const WALLET_REQUEST = 23194 export const WALLET_REQUEST = 23194
export const WALLET_RESPONSE = 23195 export const WALLET_RESPONSE = 23195
@@ -190,7 +191,7 @@ export const COMMUNITY = 34550
export const ROOM = 35834 export const ROOM = 35834
export const ROOM_META = 39000 export const ROOM_META = 39000
export const ROOM_ADMINS = 39001 export const ROOM_ADMINS = 39001
export const GROUP_MEMBERS = 39002 export const ROOM_MEMBERS = 39002
export const ROOM_CREATE_PERMISSION = 39004 export const ROOM_CREATE_PERMISSION = 39004
export const FOLLOW_PACK = 39089 export const FOLLOW_PACK = 39089