diff --git a/src/nostrMiddleware.ts b/src/nostrMiddleware.ts index 4dd3b281..034dbce8 100644 --- a/src/nostrMiddleware.ts +++ b/src/nostrMiddleware.ts @@ -105,7 +105,7 @@ export default (serverMethods: Types.ServerMethods, mainHandler: Main, nostrSett return { Stop: () => { mainHandler.adminManager.setNostrConnected(false); return nostr.Stop }, - Send: async (...args) => nostr.Send(...args), + Send: (...args) => nostr.Send(...args), Ping: () => nostr.Ping(), Reset: (settings: NostrSettings) => nostr.Reset(settings) } diff --git a/src/services/lnd/lnd.ts b/src/services/lnd/lnd.ts index b36f09e7..daf7b411 100644 --- a/src/services/lnd/lnd.ts +++ b/src/services/lnd/lnd.ts @@ -142,20 +142,15 @@ export default class { return new Promise((res, rej) => { const interval = setInterval(async () => { try { - const info = await this.GetInfo() - if (!info.syncedToChain || !info.syncedToGraph) { - this.log("LND responding but not synced yet, waiting...") - return - } + await this.GetInfo() clearInterval(interval) this.ready = true res() } catch (err) { this.log("LND is not ready yet, will try again in 1 second") - } - if (Date.now() - now > 1000 * 60 * 10) { - clearInterval(interval) - rej(new Error("LND not synced after 10 minutes")) + if (Date.now() - now > 1000 * 60) { + rej(new Error("LND not ready after 1 minute")) + } } }, 1000) }) diff --git a/src/services/main/debitManager.ts b/src/services/main/debitManager.ts index 53375217..28579d31 100644 --- a/src/services/main/debitManager.ts +++ b/src/services/main/debitManager.ts @@ -153,14 +153,13 @@ export class DebitManager { } notifyPaymentSuccess = (debitRes: NdebitSuccess, event: { pub: string, id: string, appId: string }) => { - this.logger("✅ [DEBIT REQUEST] Payment successful, sending OK response to", event.pub.slice(0, 16) + "...", "for event", event.id.slice(0, 16) + "...") this.sendDebitResponse(debitRes, event) } sendDebitResponse = (debitRes: NdebitFailure | NdebitSuccess, event: { pub: string, id: string, appId: string }) => { - this.logger("📤 [DEBIT RESPONSE] Sending Kind 21002 response:", JSON.stringify(debitRes), "to", event.pub.slice(0, 16) + "...") const e = newNdebitResponse(JSON.stringify(debitRes), event) this.storage.NostrSender().Send({ type: 'app', appId: event.appId }, { type: 'event', event: e, encrypt: { toPub: event.pub } }) + } payNdebitInvoice = async (event: NostrEvent, pointerdata: NdebitData): Promise => { diff --git a/src/services/main/watchdog.ts b/src/services/main/watchdog.ts index 50bd7873..778e09ce 100644 --- a/src/services/main/watchdog.ts +++ b/src/services/main/watchdog.ts @@ -196,19 +196,15 @@ export class Watchdog { const knownMaxIndex = Math.max(maxFromDb, this.latestPaymentIndexOffset) const newLatest = await this.lnd.GetLatestPaymentIndex(knownMaxIndex) const historyMismatch = newLatest > knownMaxIndex - if (historyMismatch) { - this.log("Payment index advanced from", knownMaxIndex, "to", newLatest, "- updating offset (likely LND restart or external payment)") - this.latestPaymentIndexOffset = newLatest - } const other = { ilnd: this.initialLndBalance, hf: this.accumulatedHtlcFees, iu: this.initialUsersBalance, tu: totalUsersBalance, km: knownMaxIndex, nl: newLatest, oext: otherExternal } //getLogger({ component: 'watchdog_debug2' })(JSON.stringify({ deltaLnd, deltaUsers, totalExternal, other })) const deny = await this.checkBalanceUpdate(deltaLnd, deltaUsers) + if (historyMismatch) { + getLogger({ component: 'bark' })("History mismatch detected in absolute update, locking outgoing operations") + this.lnd.LockOutgoingOperations() + return + } if (deny) { - if (historyMismatch) { - getLogger({ component: 'bark' })("Balance mismatch with unexpected payment history, locking outgoing operations") - this.lnd.LockOutgoingOperations() - return - } this.log("Balance mismatch detected in absolute update, but history is ok") } this.lnd.UnlockOutgoingOperations() diff --git a/src/services/nostr/handler.ts b/src/services/nostr/handler.ts index b9ecfe70..d1fa46ec 100644 --- a/src/services/nostr/handler.ts +++ b/src/services/nostr/handler.ts @@ -132,12 +132,12 @@ const handleNostrSettings = (settings: NostrSettings) => { send(event) }) } */ -const sendToNostr: NostrSend = async (initiator, data, relays) => { +const sendToNostr: NostrSend = (initiator, data, relays) => { if (!subProcessHandler) { getLogger({ component: "nostrMiddleware" })(ERROR, "nostr was not initialized") return } - await subProcessHandler.Send(initiator, data, relays) + subProcessHandler.Send(initiator, data, relays) } send({ type: 'ready' }) diff --git a/src/services/nostr/nostrPool.ts b/src/services/nostr/nostrPool.ts index ee610a35..d41da382 100644 --- a/src/services/nostr/nostrPool.ts +++ b/src/services/nostr/nostrPool.ts @@ -16,7 +16,7 @@ export type SendDataContent = { type: "content", content: string, pub: string } export type SendDataEvent = { type: "event", event: UnsignedEvent, encrypt?: { toPub: string } } export type SendData = SendDataContent | SendDataEvent export type SendInitiator = { type: 'app', appId: string } | { type: 'client', clientId: string } -export type NostrSend = (initiator: SendInitiator, data: SendData, relays?: string[] | undefined) => Promise +export type NostrSend = (initiator: SendInitiator, data: SendData, relays?: string[] | undefined) => void export type LinkedProviderInfo = { pubkey: string, clientId: string, relayUrl: string } export type AppInfo = { appId: string, publicKey: string, privateKey: string, name: string, provider?: LinkedProviderInfo } @@ -203,26 +203,21 @@ export class NostrPool { const signed = finalizeEvent(event, Buffer.from(keys.privateKey, 'hex')) let sent = false const log = getLogger({ appName: keys.name }) - this.log(`📤 Publishing Kind ${event.kind} event to ${relays.length} relay(s): ${relays.join(', ')}`) + // const r = relays ? relays : this.getServiceRelays() const pool = new SimplePool() - try { - await Promise.all(pool.publish(relays, signed).map(async p => { - try { - await p - sent = true - } catch (e: any) { - this.log(ERROR, `Failed to publish Kind ${event.kind} event:`, e.message || e) - log(e) - } - })) - if (!sent) { - this.log(ERROR, `Failed to send Kind ${event.kind} event to any relay`) - log("failed to send event") - } else { - this.log(`✅ Kind ${event.kind} event published successfully (id: ${signed.id.slice(0, 16)}...)`) + await Promise.all(pool.publish(relays, signed).map(async p => { + try { + await p + sent = true + } catch (e: any) { + console.log(e) + log(e) } - } finally { - pool.close(relays) + })) + if (!sent) { + log("failed to send event") + } else { + //log("sent event") } } diff --git a/src/services/nostr/sender.ts b/src/services/nostr/sender.ts index 8437b9af..1fd336a5 100644 --- a/src/services/nostr/sender.ts +++ b/src/services/nostr/sender.ts @@ -1,7 +1,7 @@ import { NostrSend, SendData, SendInitiator } from "./nostrPool.js" -import { ERROR, getLogger } from "../helpers/logger.js" +import { getLogger } from "../helpers/logger.js" export class NostrSender { - private _nostrSend: NostrSend = async () => { throw new Error('nostr send not initialized yet') } + private _nostrSend: NostrSend = () => { throw new Error('nostr send not initialized yet') } private isReady: boolean = false private onReadyCallbacks: (() => void)[] = [] private pendingSends: { initiator: SendInitiator, data: SendData, relays?: string[] | undefined }[] = [] @@ -12,12 +12,7 @@ export class NostrSender { this.isReady = true this.onReadyCallbacks.forEach(cb => cb()) this.onReadyCallbacks = [] - // Process pending sends with proper error handling - this.pendingSends.forEach(send => { - this._nostrSend(send.initiator, send.data, send.relays).catch(e => { - this.log(ERROR, "failed to send pending event", e.message || e) - }) - }) + this.pendingSends.forEach(send => this._nostrSend(send.initiator, send.data, send.relays)) this.pendingSends = [] } OnReady(callback: () => void) { @@ -27,16 +22,13 @@ export class NostrSender { this.onReadyCallbacks.push(callback) } } - Send(initiator: SendInitiator, data: SendData, relays?: string[] | undefined): void { + Send(initiator: SendInitiator, data: SendData, relays?: string[] | undefined) { if (!this.isReady) { this.log("tried to send before nostr was ready, caching request") this.pendingSends.push({ initiator, data, relays }) return } - // Fire and forget but log errors - this._nostrSend(initiator, data, relays).catch(e => { - this.log(ERROR, "failed to send event", e.message || e) - }) + this._nostrSend(initiator, data, relays) } IsReady() { return this.isReady diff --git a/src/services/storage/tlv/tlvFilesStorageProcessor.ts b/src/services/storage/tlv/tlvFilesStorageProcessor.ts index caccb949..2b4189e4 100644 --- a/src/services/storage/tlv/tlvFilesStorageProcessor.ts +++ b/src/services/storage/tlv/tlvFilesStorageProcessor.ts @@ -126,7 +126,7 @@ class TlvFilesStorageProcessor { throw new Error('Unknown metric type: ' + t) } }) - this.wrtc.attachNostrSend(async (initiator: SendInitiator, data: SendData, relays?: string[] | undefined) => { + this.wrtc.attachNostrSend((initiator: SendInitiator, data: SendData, relays?: string[] | undefined) => { this.sendResponse({ success: true, type: 'nostrSend', diff --git a/src/services/webRTC/index.ts b/src/services/webRTC/index.ts index a2cc90af..8ee8d884 100644 --- a/src/services/webRTC/index.ts +++ b/src/services/webRTC/index.ts @@ -27,11 +27,11 @@ export default class webRTC { attachNostrSend(f: NostrSend) { this._nostrSend = f } - private nostrSend: NostrSend = async (initiator: SendInitiator, data: SendData, relays?: string[] | undefined) => { + private nostrSend: NostrSend = (initiator: SendInitiator, data: SendData, relays?: string[] | undefined) => { if (!this._nostrSend) { throw new Error("No nostrSend attached") } - await this._nostrSend(initiator, data, relays) + this._nostrSend(initiator, data, relays) } private sendCandidate = (u: WebRtcUserInfo, candidate: string) => {