1
0
mirror of git://jb55.com/damus synced 2024-09-18 19:23:49 +00:00

zaps: don't spam lnurls when validate zaps

lnurls.lookup_or_fetch not fetched lnurl1dp68gurn8ghj7um9dej8xct5wvhxcmmv9uh8wetvdskkkmn0wahz7mrww4excup0df3r2dg3mj444
fetching static payreq lnurl1dp68gurn8ghj7um9dej8xct5wvhxcmmv9uh8wetvdskkkmn0wahz7mrww4excup0df3r2dg3mj444
lnurls.lookup_or_fetch already fetching lnurl1dp68gurn8ghj7um9dej8xct5wvhxcmmv9uh8wetvdskkkmn0wahz7mrww4excup0df3r2dg3mj444
lnurls.lookup_or_fetch already fetching lnurl1dp68gurn8ghj7um9dej8xct5wvhxcmmv9uh8wetvdskkkmn0wahz7mrww4excup0df3r2dg3mj444
lnurls.lookup_or_fetch already fetching lnurl1dp68gurn8ghj7um9dej8xct5wvhxcmmv9uh8wetvdskkkmn0wahz7mrww4excup0df3r2dg3mj444
lnurls.lookup_or_fetch already fetching lnurl1dp68gurn8ghj7um9dej8xct5wvhxcmmv9uh8wetvdskkkmn0wahz7mrww4excup0df3r2dg3mj444

Changelog-Fixed: Don't spam lnurls when validating zaps
This commit is contained in:
William Casarin 2023-07-17 14:12:41 -07:00
parent fa520d48d3
commit 2a4ee6c48c
4 changed files with 56 additions and 10 deletions

View File

@ -1390,9 +1390,9 @@ func process_zap_event(damus_state: DamusState, ev: NostrEvent, completion: @esc
completion(.failed)
return
}
Task {
guard let zapper = await fetch_zapper_from_lnurl(lnurl) else {
guard let zapper = await fetch_zapper_from_lnurl(lnurls: damus_state.lnurls, pubkey: ptag, lnurl: lnurl) else {
completion(.failed)
return
}

View File

@ -7,14 +7,57 @@
import Foundation
enum LNUrlState {
case not_fetched
case fetching(Task<LNUrlPayRequest?, Never>)
case fetched(LNUrlPayRequest)
case failed(tries: Int)
}
class LNUrls {
var endpoints: [String: LNUrlPayRequest]
var endpoints: [String: LNUrlState]
init() {
self.endpoints = [:]
}
func lookup(_ id: String) -> LNUrlPayRequest? {
return self.endpoints[id]
@MainActor
func lookup_or_fetch(pubkey: String, lnurl: String) async -> LNUrlPayRequest? {
switch lookup(pubkey: pubkey) {
case .failed(let tries):
print("lnurls.lookup_or_fetch failed \(tries) \(lnurl)")
guard tries < 5 else { return nil }
self.endpoints[pubkey] = .failed(tries: tries + 1)
case .fetched(let pr):
print("lnurls.lookup_or_fetch fetched \(lnurl)")
return pr
case .fetching(let task):
print("lnurls.lookup_or_fetch already fetching \(lnurl)")
return await task.value
case .not_fetched:
print("lnurls.lookup_or_fetch not fetched \(lnurl)")
break
}
let task = Task {
let v = await fetch_static_payreq(lnurl)
return v
}
self.endpoints[pubkey] = .fetching(task)
let v = await task.value
if let v {
self.endpoints[pubkey] = .fetched(v)
} else {
self.endpoints[pubkey] = .failed(tries: 1)
}
return v
}
func lookup(pubkey: String) -> LNUrlState {
return self.endpoints[pubkey] ?? .not_fetched
}
}

View File

@ -412,8 +412,9 @@ func decode_nostr_event_json(_ desc: String) -> NostrEvent? {
return ev
}
func fetch_zapper_from_lnurl(_ lnurl: String) async -> String? {
guard let endpoint = await fetch_static_payreq(lnurl) else {
func fetch_zapper_from_lnurl(lnurls: LNUrls, pubkey: String, lnurl: String) async -> String? {
guard let endpoint = await lnurls.lookup_or_fetch(pubkey: pubkey, lnurl: lnurl) else {
return nil
}
@ -442,6 +443,8 @@ func decode_lnurl(_ lnurl: String) -> URL? {
}
func fetch_static_payreq(_ lnurl: String) async -> LNUrlPayRequest? {
print("fetching static payreq \(lnurl)")
guard let url = decode_lnurl(lnurl) else {
return nil
}

View File

@ -11,7 +11,7 @@ class Zaps {
private(set) var zaps: [String: Zapping]
let our_pubkey: String
var our_zaps: [String: [Zapping]]
private(set) var event_counts: [String: Int]
private(set) var event_totals: [String: Int64]