diff --git a/bitchat/Nostr/NostrRelayManager.swift b/bitchat/Nostr/NostrRelayManager.swift index 6601b863..19302781 100644 --- a/bitchat/Nostr/NostrRelayManager.swift +++ b/bitchat/Nostr/NostrRelayManager.swift @@ -1867,7 +1867,7 @@ struct NostrFilter: Encodable { filter.kinds = [1059] // Gift wrap kind filter.since = since?.timeIntervalSince1970.toInt() filter.tagFilters = ["p": [pubkey]] - filter.limit = TransportConfig.nostrRelayDefaultFetchLimit // reasonable limit + filter.limit = TransportConfig.nostrGiftWrapFetchLimit return filter } diff --git a/bitchat/Services/TransportConfig.swift b/bitchat/Services/TransportConfig.swift index 785f7bdc..c8cf68fb 100644 --- a/bitchat/Services/TransportConfig.swift +++ b/bitchat/Services/TransportConfig.swift @@ -269,7 +269,10 @@ enum TransportConfig { // Reconnect delays get ±20% random jitter so relays that dropped together // (e.g. a network blip) don't thundering-herd the same reconnect instant. static let nostrRelayBackoffJitterRatio: Double = 0.2 - static let nostrRelayDefaultFetchLimit: Int = 100 + // Randomized envelope timestamps can put a recent DM behind older rumors. + // Match the existing location backfill allowance, while keeping requests bounded. + // Relays may impose a lower cap; this is not complete history pagination. + static let nostrGiftWrapFetchLimit: Int = 1000 // How many consecutive Tor-readiness waits (each bounded by TorManager's // bootstrap deadline) to attempt before unblocking pending EOSE callers. static let nostrTorReadyMaxWaitAttempts: Int = 3 diff --git a/bitchatTests/NostrProtocolTests.swift b/bitchatTests/NostrProtocolTests.swift index f63928c9..c86899ed 100644 --- a/bitchatTests/NostrProtocolTests.swift +++ b/bitchatTests/NostrProtocolTests.swift @@ -453,6 +453,27 @@ struct NostrProtocolTests { #expect(!signed.isValidSignature()) } + @Test func giftWrapFilter_requestsExpandedBoundedBackfill() throws { + let since = Date(timeIntervalSince1970: 1_699_739_900) + let filter = NostrFilter.giftWrapsFor(pubkey: "recipient", since: since) + let data = try JSONEncoder().encode(filter) + let object = try #require(try JSONSerialization.jsonObject(with: data) as? [String: Any]) + let limit = try #require(object["limit"] as? Int) + + #expect(object["kinds"] as? [Int] == [1059]) + #expect(object["#p"] as? [String] == ["recipient"]) + #expect(object["since"] as? Int == 1_699_739_900) + #expect(limit == 1000) + + // Model a relay honoring the requested cap and sorting by outer created_at. + // A freshly sent rumor can have an older envelope than 100 retained wraps. + let recentRumorEnvelope = 1_699_920_800 + let retainedEnvelopes = (0..<100).map { 1_699_990_000 + $0 } + let newestFirst = (retainedEnvelopes + [recentRumorEnvelope]).sorted(by: >) + #expect(!newestFirst.prefix(100).contains(recentRumorEnvelope)) + #expect(newestFirst.prefix(limit).contains(recentRumorEnvelope)) + } + @Test func geohashNotesSingleFilter_encodesExpectedTagShape() throws { let since = Date(timeIntervalSince1970: 1_234_567) let filter = NostrFilter.geohashNotes("u4pruyd", since: since, limit: 42)