damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit 2a4ee6c48c55eaf400a6e109f00820e9d60b3921
parent fa520d48d3a88ed7125fdd03e0cefc58b12d45c5
Author: William Casarin <jb55@jb55.com>
Date:   Mon, 17 Jul 2023 14:12:41 -0700

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

Diffstat:
Mdamus/Models/HomeModel.swift | 4++--
Mdamus/Util/LNUrls.swift | 53++++++++++++++++++++++++++++++++++++++++++++++++-----
Mdamus/Util/Zap.swift | 7+++++--
Mdamus/Util/Zaps.swift | 2+-
4 files changed, 56 insertions(+), 10 deletions(-)

diff --git a/damus/Models/HomeModel.swift b/damus/Models/HomeModel.swift @@ -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 } diff --git a/damus/Util/LNUrls.swift b/damus/Util/LNUrls.swift @@ -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 } } diff --git a/damus/Util/Zap.swift b/damus/Util/Zap.swift @@ -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 } diff --git a/damus/Util/Zaps.swift b/damus/Util/Zaps.swift @@ -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]