Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .changeset/paid-peering-delivery.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"nostream": minor
---

feat(peering): deliver paid peering invoices and gate peer feeds

Configured peers can request their BOLT11 invoice as a signed `kind 402` event
and pay it through Relay B's existing processor. Feed `REQ`, `COUNT` and NIP-77
requests are refused with a payment-required reason until a paid period is
active. Expired invoices are reissued after a cooldown, and a missed webhook is
rechecked before access is denied.
6 changes: 4 additions & 2 deletions resources/default-settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,10 @@ peering:
# windowSeconds: 3600
scopes: []
# Terms for peering, reusing the NIP-11 fee-object shape. `period` is in
# seconds. An empty list means settlement-free peering.
# Example: [{ amount: 1000, unit: msats, period: 2592000 }]
# seconds. An empty list means settlement-free peering. One positive term
# makes this a paid feed: payments must be enabled, and a paid msats amount
# must be a whole number of satoshis.
# Example: [{ amount: 21000, unit: msats, period: 2592000 }]
terms: []
# Peering peers trusted by their relay pubkey (hex or npub), never by URL
# alone. A connection that proves one of these keys with NIP-42 AUTH is
Expand Down
7 changes: 7 additions & 0 deletions src/@types/adapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ export type IWebSocketAdapter = EventEmitter & {
getNegentropySessions(): INegentropySessions
/** Removes a subscription and aborts any in-flight read still serving it. */
endSubscription(subscriptionId: string): void
/**
* Schedules a paid-access expiry check for this subscription. The adapter owns
* the timer and clears it on CLOSE, socket close and subscription ID reuse.
* `check` returns the next period end when access continues, `null` when access
* is free and no longer expires, or `false` when the subscription should close.
*/
setSubscriptionExpiry(subscriptionId: string, periodEnd: Date, check: () => Promise<Date | null | false>): void
}

export interface ICacheAdapter {
Expand Down
2 changes: 2 additions & 0 deletions src/@types/repositories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ export interface IPeeringPaymentRepository {
/** SELECT ... FOR UPDATE; must run inside a transaction. */
lockByPeer(pubkey: Pubkey, client: DatabaseClient): Promise<PeeringPayment | undefined>
save(payment: PeeringPayment, client?: DatabaseClient): Promise<PeeringPayment>
/** Updates only the processor-check diagnostics, so a concurrent settlement is not overwritten. */
recordCheck(pubkey: Pubkey, checkedAt: Date, error: string | null, client?: DatabaseClient): Promise<void>
list(): Promise<PeeringPayment[]>
/** Active or renewal-pending relationships whose period ends by `before`. */
findDueForRenewal(before: Date, limit: number, offset: number): Promise<PeeringPayment[]>
Expand Down
12 changes: 12 additions & 0 deletions src/@types/services.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DatabaseTransaction, Pubkey } from './base'
import { Invoice } from './invoice'
import { NotificationOutboxPayload } from './notification-outbox'
import { PeeringAccess } from './peering'

export interface IMaintenanceService {
clearOldEvents(): Promise<void>
Expand Down Expand Up @@ -54,6 +55,17 @@ export interface IPaymentsService {
getUnconfirmedPeeringInvoices(limit?: number, offset?: number): Promise<Invoice[]>
}

export interface IPeeringBillingService {
/** Access decision for a configured peer's feed. Optionally rechecks a pending invoice first. */
getAccess(peerPubkey: Pubkey, options?: { now?: Date; recheck?: boolean }): Promise<PeeringAccess>
/** Returns a usable invoice, creating or reissuing one when needed. */
ensureInvoice(peerPubkey: Pubkey, options?: { now?: Date; renewal?: boolean }): Promise<Invoice | undefined>
/** Ends expired periods once, marking them lapsed and alerting the operator. */
processLapses(now?: Date): Promise<number>
/** Clears a pointer and updates status after the processor confirms an invoice expired. */
handleInvoiceExpired(invoice: Invoice): Promise<void>
}

export interface ISubscriptionService {
/** One pass of renewal invoices, period ends and grace expiries. Returns how many subscriptions changed. */
processLifecycle(now?: Date): Promise<number>
Expand Down
84 changes: 84 additions & 0 deletions src/adapters/web-socket-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export class WebSocketAdapter extends EventEmitter implements IWebSocketAdapter
private alive: boolean
private pendingSend: Promise<void> | undefined
private subscriptions: Map<SubscriptionId, SubscriptionFilter[]>
private readonly subscriptionExpiries = new Map<SubscriptionId, { timer: NodeJS.Timeout; token: symbol }>()
private readonly session: Nip42SessionManager
/** Configured peering peer proven by AUTH on this socket, if any. */
private peerPubkey: string | undefined
Expand Down Expand Up @@ -152,14 +153,94 @@ export class WebSocketAdapter extends EventEmitter implements IWebSocketAdapter

public onUnsubscribed(subscriptionId: string): void {
logger('client %s unsubscribed %s', this.clientId, subscriptionId)
this.clearSubscriptionExpiry(subscriptionId)
this.subscriptions.delete(subscriptionId)
}

public onSubscribed(subscriptionId: string, filters: SubscriptionFilter[]): void {
logger('client %s subscribed %s to %o', this.clientId, subscriptionId, filters)
// Reusing an id starts a new subscription; the old expiry must not close it.
this.clearSubscriptionExpiry(subscriptionId)
this.subscriptions.set(subscriptionId, filters)
}

public setSubscriptionExpiry(
subscriptionId: SubscriptionId,
periodEnd: Date,
check: () => Promise<Date | null | false>,
): void {
this.clearSubscriptionExpiry(subscriptionId)

const token = Symbol(subscriptionId)
const remaining = Math.max(0, periodEnd.getTime() - Date.now())
const delay = Math.min(remaining, 2_147_483_647)

const timer = setTimeout(() => {
if (this.subscriptionExpiries.get(subscriptionId)?.token !== token) {
return
}

if (remaining > delay) {
this.setSubscriptionExpiry(subscriptionId, periodEnd, check)
return
}

void this.runSubscriptionExpiry(subscriptionId, token, check)
}, delay)

this.subscriptionExpiries.set(subscriptionId, { timer, token })
}

private async runSubscriptionExpiry(
subscriptionId: SubscriptionId,
token: symbol,
check: () => Promise<Date | null | false>,
): Promise<void> {
let nextPeriodEnd: Date | null | false = false

try {
nextPeriodEnd = await check()
} catch (error) {
logger.error('unable to check paid subscription expiry for %s: %o', subscriptionId, error)
}

// The id may have been reused, closed or disconnected while the check ran.
if (this.subscriptionExpiries.get(subscriptionId)?.token !== token) {
return
}

if (nextPeriodEnd === null) {
// Free peering: access no longer has an expiry, so stop watching it.
this.clearSubscriptionExpiry(subscriptionId)
return
}

if (nextPeriodEnd instanceof Date && nextPeriodEnd.getTime() > Date.now()) {
this.setSubscriptionExpiry(subscriptionId, nextPeriodEnd, check)
return
}

this.clearSubscriptionExpiry(subscriptionId)
this.endSubscription(subscriptionId)
this.sendMessage(createClosedMessage(subscriptionId, 'payment-required: peering access expired'))
}

private clearSubscriptionExpiry(subscriptionId: SubscriptionId): void {
const entry = this.subscriptionExpiries.get(subscriptionId)
if (!entry) {
return
}

clearTimeout(entry.timer)
this.subscriptionExpiries.delete(subscriptionId)
}

private clearAllSubscriptionExpiries(): void {
for (const subscriptionId of this.subscriptionExpiries.keys()) {
this.clearSubscriptionExpiry(subscriptionId)
}
}

public onBroadcast(event: Event): void {
this.webSocketServer.emit(WebSocketServerAdapterEvent.Broadcast, event)
if (cluster.isWorker && typeof process.send === 'function') {
Expand Down Expand Up @@ -245,6 +326,8 @@ export class WebSocketAdapter extends EventEmitter implements IWebSocketAdapter
}

public drainAndClose(reason = 'relay shutting down'): void {
this.clearAllSubscriptionExpiries()

this.subscriptions.forEach((_filters, subscriptionId) => {
this.sendMessage(createClosedMessage(subscriptionId, `error: ${reason}`))
})
Expand Down Expand Up @@ -466,6 +549,7 @@ export class WebSocketAdapter extends EventEmitter implements IWebSocketAdapter
private onClientClose() {
recordWebsocketConnectionClosed()
this.alive = false
this.clearAllSubscriptionExpiries()
this.subscriptions.clear()
this.negentropySessions.closeAll()
this.session.clear()
Expand Down
36 changes: 27 additions & 9 deletions src/app/maintenance-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FSWatcher } from 'fs'
import { mergeDeepLeft, path, pipe } from 'ramda'
import { ICacheAdapter } from '../@types/adapters'
import { IRunnable } from '../@types/base'
import { InvoiceStatus } from '../@types/invoice'
import { InvoicePurpose, InvoiceStatus } from '../@types/invoice'
import { Nip05Verification } from '../@types/nip05'
import { OperatorNotificationEventType } from '../@types/operator-notifications'
import {
Expand All @@ -14,6 +14,7 @@ import {
IMaintenanceService,
INotificationOutboxService,
IPaymentsService,
IPeeringBillingService,
ISubscriptionService,
} from '../@types/services'
import { Settings } from '../@types/settings'
Expand Down Expand Up @@ -111,6 +112,7 @@ export class MaintenanceWorker implements IRunnable {
private readonly notificationOutboxRepository: INotificationOutboxRepository,
private readonly opsCache?: ICacheAdapter,
private readonly subscriptionService?: ISubscriptionService,
private readonly peeringBillingService?: IPeeringBillingService,
) {
this.process
.on('SIGINT', this.onExit.bind(this))
Expand Down Expand Up @@ -287,14 +289,18 @@ export class MaintenanceWorker implements IRunnable {
id: invoice.id,
status: InvoiceStatus.EXPIRED,
})
try {
await this.notificationOutboxRepository.enqueue(OperatorNotificationEventType.ADMISSION_INVOICE_FAILED, {
invoiceId: invoice.id,
pubkey: invoice.pubkey,
reason: 'expired',
})
} catch (error) {
logger.error('Unable to enqueue admission.invoice.failed notification', error)
if (invoice.purpose === InvoicePurpose.PEERING) {
await this.peeringBillingService?.handleInvoiceExpired(invoice)
} else {
try {
await this.notificationOutboxRepository.enqueue(OperatorNotificationEventType.ADMISSION_INVOICE_FAILED, {
invoiceId: invoice.id,
pubkey: invoice.pubkey,
reason: 'expired',
})
} catch (error) {
logger.error('Unable to enqueue admission.invoice.failed notification', error)
}
}
successful++
continue
Expand All @@ -307,6 +313,7 @@ export class MaintenanceWorker implements IRunnable {
}

await this.processSubscriptions()
await this.processPeeringLapses()

await clearOldEventsPromise
await this.publishOpsHeartbeat({
Expand All @@ -326,6 +333,17 @@ export class MaintenanceWorker implements IRunnable {
}
}

private async processPeeringLapses(): Promise<void> {
try {
const changed = await this.peeringBillingService?.processLapses()
if (changed) {
logger('lapsed %d peering relationship(s)', changed)
}
} catch (error) {
logger.error('Unable to process peering lapses', error)
}
}

private async processNotificationOutbox(): Promise<void> {
try {
const delivered = await this.notificationOutboxService.processBatch()
Expand Down
2 changes: 2 additions & 0 deletions src/factories/maintenance-worker-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createAdminOpsCacheAdapter } from './admin-ops-cache-adapter-factory'
import { createMaintenanceService } from './maintenance-service-factory'
import { createNotificationOutboxService } from './notification-outbox-service-factory'
import { createPaymentsService } from './payments-service-factory'
import { createPeeringBillingService } from './peering-billing-service-factory'
import { createSubscriptionService } from './subscription-service-factory'
import { createSettings } from './settings-factory'
import { getMasterDbClient } from '../database/client'
Expand All @@ -26,5 +27,6 @@ export const maintenanceWorkerFactory = () => {
notificationOutboxRepository,
createAdminOpsCacheAdapter(),
createSubscriptionService(),
createPeeringBillingService(),
)
}
24 changes: 24 additions & 0 deletions src/factories/message-handler-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
IReportRepository,
IUserRepository,
} from '../@types/repositories'
import { IPeeringBillingService } from '../@types/services'
import { RedisAdapter } from '../adapters/redis-adapter'
import { getCacheClient } from '../cache/client'
import { getMasterDbClient } from '../database/client'
Expand All @@ -22,6 +23,7 @@ import { UnsubscribeMessageHandler } from '../handlers/unsubscribe-message-handl
import { UserSubscriptionRepository } from '../repositories/user-subscription-repository'
import { PeeringRequestStatsStore } from '../utils/peering-request-stats'
import { eventStrategyFactory } from './event-strategy-factory'
import { createPeeringBillingService } from './peering-billing-service-factory'
import { rateLimiterFactory } from './rate-limiter-factory'
import { createSettings } from './settings-factory'
import { wotGraphServiceFactory } from './wot-graph-service-factory'
Expand Down Expand Up @@ -50,6 +52,25 @@ export const getPeerRequestStatsStore = (): PeeringRequestStatsStore => {
return peerRequestStatsStore
}

let peeringBillingService: IPeeringBillingService | undefined = undefined
export const getPeeringBillingService = (): IPeeringBillingService => {
if (!peeringBillingService) {
peeringBillingService = createPeeringBillingService()
}
return peeringBillingService
}

/**
* Only a connection that proved a configured peer identity needs billing. Creating
* the billing service eagerly would build a payments processor for every ordinary
* REQ/COUNT/NEG-OPEN, which can fail before the handler runs when a payment
* provider is misconfigured.
*/
export const peeringBillingServiceFor = (
adapter: IWebSocketAdapter,
get: () => IPeeringBillingService = getPeeringBillingService,
): IPeeringBillingService | undefined => (adapter.getPeerPubkey?.() ? get() : undefined)

export const messageHandlerFactory =
(
eventRepository: IEventRepository,
Expand Down Expand Up @@ -91,6 +112,7 @@ export const messageHandlerFactory =
inviteCodeRepository,
rateLimiterFactory,
getPeerRequestStatsStore(),
peeringBillingServiceFor(adapter),
)
case MessageType.CLOSE:
return new UnsubscribeMessageHandler(adapter)
Expand All @@ -101,6 +123,7 @@ export const messageHandlerFactory =
createSettings,
rateLimiterFactory,
getPeerRequestStatsStore(),
peeringBillingServiceFor(adapter),
)
case MessageType.AUTH:
return new AuthMessageHandler(adapter, createSettings)
Expand All @@ -111,6 +134,7 @@ export const messageHandlerFactory =
createSettings,
rateLimiterFactory,
getPeerRequestStatsStore(),
peeringBillingServiceFor(adapter),
)
case MessageType.NEG_MSG:
return new NegMsgMessageHandler(adapter, createSettings)
Expand Down
Loading
Loading