libp2r2p 0.12.21 → 0.12.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/private-messenger/index.js +196 -33
package/package.json
CHANGED
|
@@ -38,8 +38,11 @@
|
|
|
38
38
|
// - Browser offline/online events add explicit offline ranges with a small skew.
|
|
39
39
|
// - Recovery defaults to 7 days and can be overridden per channel; zero disables durable recovery.
|
|
40
40
|
// - Channel state not actively leased or watched within the configured stale window is pruned.
|
|
41
|
-
// -
|
|
42
|
-
// -
|
|
41
|
+
// - The recovering asker reads relays first, then asks seeders for relay-uncovered intervals and the unproven left edge.
|
|
42
|
+
// - record.covered means relay/EOSE coverage; record.asked means an ask publication received at least one relay ACK.
|
|
43
|
+
// - Asked ranges are removed from persisted offlineRanges and advance askedThrough; no reply is awaited.
|
|
44
|
+
// - Priority/tail recovery uses only recently present seeders. Full recovery tries every configured seeder.
|
|
45
|
+
// - Seeder presence announcements occur every 10min; auto-discovered seeders are capped to the 8 most recently active.
|
|
43
46
|
// - Seeder/watchtower channels store reconstructed router events in a separate IndexedDB queue and auto-reply to recovery asks.
|
|
44
47
|
// - Seeder replies stream compact routers with createMissingMessageReplyPacker({ messenger, question }).update(seed), then finalize(optionalLastSeed).
|
|
45
48
|
// - For other event-list replies, use createEventReplyPacker({ messenger, question, code }).update(event).
|
|
@@ -122,6 +125,8 @@ const STORAGE_POLICY_BROADCAST_CHANNEL = 'libp2r2p:private-messenger:storage-pol
|
|
|
122
125
|
const DEFAULT_OFFLINE_SKEW_SECONDS = 30
|
|
123
126
|
const DEFAULT_RELOAD_GAP_DELAY_MS = 500
|
|
124
127
|
const DEFAULT_SEEDER_PRESENCE_INTERVAL_MS = 10 * 60 * 1000
|
|
128
|
+
const SEEDER_PRESENCE_COALESCE_MS = 250
|
|
129
|
+
const SEEDER_PRESENCE_MAX_PENDING = 4096
|
|
125
130
|
const DEFAULT_SEEDER_ONLINE_SECONDS = 20 * 60
|
|
126
131
|
const DEFAULT_MAX_DYNAMIC_RECOVERY_SEEDERS = 8
|
|
127
132
|
const DEFAULT_MESSAGE_QUEUE_MAX_BYTES = 16 * 1024 * 1024 // 16 MiB
|
|
@@ -515,6 +520,8 @@ export class PrivateMessenger {
|
|
|
515
520
|
this.watchRevisionByChannel = new Map()
|
|
516
521
|
this.presenceTimers = new Map()
|
|
517
522
|
this.presenceJobs = new Map()
|
|
523
|
+
this.pendingSeederPresence = new Map()
|
|
524
|
+
this.seederPresenceFlushTimer = null
|
|
518
525
|
this.seedQueueOperationTail = Promise.resolve()
|
|
519
526
|
this.stopRelayListWatcher = null
|
|
520
527
|
this.relayListWatcherPubkey = ''
|
|
@@ -952,6 +959,10 @@ export class PrivateMessenger {
|
|
|
952
959
|
this.channelDebugRefs.delete(pubkey)
|
|
953
960
|
}
|
|
954
961
|
}
|
|
962
|
+
this.discardSeederPresence([
|
|
963
|
+
...removedPubkeys,
|
|
964
|
+
...changed.map(channel => channel.pubkey)
|
|
965
|
+
])
|
|
955
966
|
|
|
956
967
|
const storageSnapshot = await activatePrivateMessengerStorage({
|
|
957
968
|
userPubkey: this.userPubkey,
|
|
@@ -1219,6 +1230,8 @@ export class PrivateMessenger {
|
|
|
1219
1230
|
if (!this.offlineRecoverySecondsFor(pubkey)) return []
|
|
1220
1231
|
const channel = this.channels.get(pubkey)
|
|
1221
1232
|
const configuredSeeders = channel?.seeders || []
|
|
1233
|
+
// Full recovery intentionally attempts all configured seeders even when
|
|
1234
|
+
// recent presence has not been observed.
|
|
1222
1235
|
if (configuredSeeders.length) return configuredSeeders.filter(seeder => seeder !== this.userPubkey)
|
|
1223
1236
|
|
|
1224
1237
|
const activity = this.readState().channels[pubkey]?.seederActivity || {}
|
|
@@ -1274,6 +1287,78 @@ export class PrivateMessenger {
|
|
|
1274
1287
|
return true
|
|
1275
1288
|
}
|
|
1276
1289
|
|
|
1290
|
+
seederPresenceKey (channelPubkey, senderPubkey) {
|
|
1291
|
+
return JSON.stringify([channelPubkey, senderPubkey])
|
|
1292
|
+
}
|
|
1293
|
+
|
|
1294
|
+
async receiveSeederPresence (channelPubkey, message) {
|
|
1295
|
+
if (this.closePromise || this.sendingPaused() || message.provenance === 'hearsay' ||
|
|
1296
|
+
!this.desiredChannels.has(channelPubkey)) return
|
|
1297
|
+
if (!this.#admitInboundPeer(channelPubkey, 'yell', message)) return
|
|
1298
|
+
const senderPubkey = message.senderPubkey || message.event?.pubkey
|
|
1299
|
+
if (!senderPubkey) return
|
|
1300
|
+
const channel = this.channels.get(channelPubkey)
|
|
1301
|
+
if (!channel) return
|
|
1302
|
+
if (channel.seeders?.length && !channel.seeders.includes(senderPubkey)) return
|
|
1303
|
+
const at = messageTime(message)
|
|
1304
|
+
const key = this.seederPresenceKey(channelPubkey, senderPubkey)
|
|
1305
|
+
const current = this.pendingSeederPresence.get(key)
|
|
1306
|
+
if (current && current.at >= at) return
|
|
1307
|
+
if (current) this.pendingSeederPresence.delete(key)
|
|
1308
|
+
if (!channel.seeders?.length) {
|
|
1309
|
+
const maxDynamic = Number.isSafeInteger(this.maxDynamicRecoverySeeders) &&
|
|
1310
|
+
this.maxDynamicRecoverySeeders > 0
|
|
1311
|
+
? this.maxDynamicRecoverySeeders
|
|
1312
|
+
: 0
|
|
1313
|
+
if (!maxDynamic) return
|
|
1314
|
+
const channelEntries = [...this.pendingSeederPresence.entries()]
|
|
1315
|
+
.filter(([, pending]) => pending.channelPubkey === channelPubkey)
|
|
1316
|
+
if (channelEntries.length >= maxDynamic) {
|
|
1317
|
+
channelEntries.sort((left, right) => left[1].at - right[1].at)
|
|
1318
|
+
for (const [pendingKey] of channelEntries.slice(
|
|
1319
|
+
0,
|
|
1320
|
+
channelEntries.length - maxDynamic + 1
|
|
1321
|
+
)) {
|
|
1322
|
+
this.pendingSeederPresence.delete(pendingKey)
|
|
1323
|
+
}
|
|
1324
|
+
}
|
|
1325
|
+
}
|
|
1326
|
+
while (this.pendingSeederPresence.size >= SEEDER_PRESENCE_MAX_PENDING) {
|
|
1327
|
+
this.pendingSeederPresence.delete(this.pendingSeederPresence.keys().next().value)
|
|
1328
|
+
}
|
|
1329
|
+
this.pendingSeederPresence.set(key, { channelPubkey, senderPubkey, at })
|
|
1330
|
+
if (this.seederPresenceFlushTimer) return
|
|
1331
|
+
this.seederPresenceFlushTimer = this._setTimeout(() => {
|
|
1332
|
+
this.seederPresenceFlushTimer = null
|
|
1333
|
+
this.flushSeederPresence()
|
|
1334
|
+
}, SEEDER_PRESENCE_COALESCE_MS)
|
|
1335
|
+
this.seederPresenceFlushTimer?.unref?.()
|
|
1336
|
+
}
|
|
1337
|
+
|
|
1338
|
+
flushSeederPresence () {
|
|
1339
|
+
const pending = [...this.pendingSeederPresence.values()]
|
|
1340
|
+
this.pendingSeederPresence.clear()
|
|
1341
|
+
for (const { channelPubkey, senderPubkey, at } of pending) {
|
|
1342
|
+
if (this.closePromise || this.sendingPaused() || !this.desiredChannels.has(channelPubkey)) continue
|
|
1343
|
+
const channel = this.channels.get(channelPubkey)
|
|
1344
|
+
if (!channel) continue
|
|
1345
|
+
const configuredSeeders = channel.seeders || []
|
|
1346
|
+
if (configuredSeeders.length && !configuredSeeders.includes(senderPubkey)) continue
|
|
1347
|
+
this.markSeederActive(channelPubkey, senderPubkey, { announced: true, at })
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
1350
|
+
|
|
1351
|
+
discardSeederPresence (channels) {
|
|
1352
|
+
const channelSet = channels ? new Set(channels) : null
|
|
1353
|
+
for (const [key, pending] of this.pendingSeederPresence) {
|
|
1354
|
+
if (!channelSet || channelSet.has(pending.channelPubkey)) this.pendingSeederPresence.delete(key)
|
|
1355
|
+
}
|
|
1356
|
+
if (!this.pendingSeederPresence.size && this.seederPresenceFlushTimer) {
|
|
1357
|
+
this._clearTimeout(this.seederPresenceFlushTimer)
|
|
1358
|
+
this.seederPresenceFlushTimer = null
|
|
1359
|
+
}
|
|
1360
|
+
}
|
|
1361
|
+
|
|
1277
1362
|
contentKeyStatus (contentKeyPubkey) {
|
|
1278
1363
|
if (!contentKeyPubkey) return 'none'
|
|
1279
1364
|
return contentKeyPubkey === this.contentKeyPubkey ? 'known' : 'unknown'
|
|
@@ -1425,7 +1510,9 @@ export class PrivateMessenger {
|
|
|
1425
1510
|
onAsk: message => this.receive(pubkey, () => this.handleAsk(pubkey, message), message, { inboundType: 'ask' }),
|
|
1426
1511
|
onReply: message => this.receive(pubkey, () => this.handleReply(pubkey, message), message, { inboundType: 'reply' }),
|
|
1427
1512
|
onTell: message => this.receive(pubkey, () => this.handleTell(pubkey, message), message, { inboundType: 'tell' }),
|
|
1428
|
-
onYell: message =>
|
|
1513
|
+
onYell: message => messageCode(message) === SEEDER_PRESENCE_CODE
|
|
1514
|
+
? this.receiveSeederPresence(pubkey, message)
|
|
1515
|
+
: this.receive(pubkey, () => this.handleYell(pubkey, message), message, { inboundType: 'yell' }),
|
|
1429
1516
|
onNym: message => this.receive(pubkey, () => this.handleNym(pubkey, message), message, { inboundType: 'nym' }),
|
|
1430
1517
|
onMessage: message => {
|
|
1431
1518
|
const type = eventType(message.event)
|
|
@@ -1581,7 +1668,7 @@ export class PrivateMessenger {
|
|
|
1581
1668
|
start: range.start,
|
|
1582
1669
|
end: range.end,
|
|
1583
1670
|
covered: [],
|
|
1584
|
-
|
|
1671
|
+
asked: [],
|
|
1585
1672
|
oldestCreatedAt: null,
|
|
1586
1673
|
relays: new Map(),
|
|
1587
1674
|
tier: '',
|
|
@@ -1654,6 +1741,12 @@ export class PrivateMessenger {
|
|
|
1654
1741
|
return false
|
|
1655
1742
|
}
|
|
1656
1743
|
|
|
1744
|
+
unhandledRecoveryRanges (record, range) {
|
|
1745
|
+
return record
|
|
1746
|
+
? subtractRanges(range, record.asked)
|
|
1747
|
+
: [range]
|
|
1748
|
+
}
|
|
1749
|
+
|
|
1657
1750
|
hasPendingRecovery (channelPubkey) {
|
|
1658
1751
|
return (this.readState().channels[channelPubkey]?.offlineRanges || []).length > 0
|
|
1659
1752
|
}
|
|
@@ -1754,7 +1847,7 @@ export class PrivateMessenger {
|
|
|
1754
1847
|
state.eose = done
|
|
1755
1848
|
state.pending = done
|
|
1756
1849
|
? []
|
|
1757
|
-
: subtractRanges({ start: record.start, end: record.end },
|
|
1850
|
+
: subtractRanges({ start: record.start, end: record.end }, this.recoveryCoverage(record, state))
|
|
1758
1851
|
state.events += entry.events || 0
|
|
1759
1852
|
if (Number.isFinite(entry.elapsedMs)) {
|
|
1760
1853
|
state.latencyMs = state.latencyMs == null
|
|
@@ -1781,17 +1874,51 @@ export class PrivateMessenger {
|
|
|
1781
1874
|
record.lastErrors = (history.relays || []).flatMap(entry => [...(entry.errors || []), ...(entry.error ? [entry.error] : [])])
|
|
1782
1875
|
}
|
|
1783
1876
|
|
|
1784
|
-
|
|
1877
|
+
recoveryCoverage (record, relayState = null) {
|
|
1878
|
+
return mergeRanges([
|
|
1879
|
+
...(relayState ? relayState.covered : record.covered),
|
|
1880
|
+
...record.asked
|
|
1881
|
+
])
|
|
1882
|
+
}
|
|
1883
|
+
|
|
1884
|
+
applyRelayCoverage (record, ranges) {
|
|
1785
1885
|
if (!ranges?.length) return
|
|
1786
|
-
record.claimed = mergeRanges([...record.claimed, ...ranges])
|
|
1787
1886
|
record.covered = mergeRanges([...record.covered, ...ranges])
|
|
1788
1887
|
for (const state of record.relays.values()) {
|
|
1789
1888
|
if (state.eose) continue
|
|
1790
|
-
state.pending = subtractRanges({ start: record.start, end: record.end },
|
|
1889
|
+
state.pending = subtractRanges({ start: record.start, end: record.end }, this.recoveryCoverage(record, state))
|
|
1890
|
+
}
|
|
1891
|
+
}
|
|
1892
|
+
|
|
1893
|
+
async persistAskedCoverage (channelPubkey, ranges) {
|
|
1894
|
+
const state = this.readState()
|
|
1895
|
+
const current = state.channels[channelPubkey]
|
|
1896
|
+
if (!current || !ranges.length) return
|
|
1897
|
+
current.offlineRanges = mergeRanges((current.offlineRanges || [])
|
|
1898
|
+
.flatMap(range => subtractRanges(range, ranges)))
|
|
1899
|
+
current.askedThrough = Math.max(
|
|
1900
|
+
Number.isSafeInteger(current.askedThrough) ? current.askedThrough : 0,
|
|
1901
|
+
Math.max(...ranges.map(range => range.end))
|
|
1902
|
+
)
|
|
1903
|
+
state.channels[channelPubkey] = current
|
|
1904
|
+
await this.writeState(state)
|
|
1905
|
+
}
|
|
1906
|
+
|
|
1907
|
+
async applyAskedCoverage (record, ranges) {
|
|
1908
|
+
if (!ranges?.length) return
|
|
1909
|
+
record.asked = mergeRanges([...record.asked, ...ranges])
|
|
1910
|
+
for (const state of record.relays.values()) {
|
|
1911
|
+
if (state.eose) continue
|
|
1912
|
+
state.pending = subtractRanges(
|
|
1913
|
+
{ start: record.start, end: record.end },
|
|
1914
|
+
this.recoveryCoverage(record, state)
|
|
1915
|
+
)
|
|
1791
1916
|
}
|
|
1917
|
+
await this.persistAskedCoverage(record.channelPubkey, ranges)
|
|
1792
1918
|
}
|
|
1793
1919
|
|
|
1794
1920
|
activeRecoverySeeders (channelPubkey) {
|
|
1921
|
+
// Priority/tail recovery is the only path that requires recent presence.
|
|
1795
1922
|
if (!this.offlineRecoverySecondsFor(channelPubkey)) return []
|
|
1796
1923
|
const channel = this.channels.get(channelPubkey)
|
|
1797
1924
|
if (!channel?.signer) return []
|
|
@@ -1819,7 +1946,7 @@ export class PrivateMessenger {
|
|
|
1819
1946
|
)
|
|
1820
1947
|
if (!parts.length) continue
|
|
1821
1948
|
intersects = true
|
|
1822
|
-
if (subtractRanges(parts[0], record
|
|
1949
|
+
if (subtractRanges(parts[0], this.recoveryCoverage(record)).length) return true
|
|
1823
1950
|
}
|
|
1824
1951
|
return !intersects
|
|
1825
1952
|
})
|
|
@@ -1876,22 +2003,27 @@ export class PrivateMessenger {
|
|
|
1876
2003
|
async finalizeRecoveryRange ({ channelPubkey, range, record }) {
|
|
1877
2004
|
if (this.pauseReasons.size) throw this.pausedError()
|
|
1878
2005
|
if (!await this.checkRecoveryOnline()) return { complete: false, failures: [], offline: true }
|
|
1879
|
-
const uncovered = subtractRanges({ start: range.start, end: range.end }, record
|
|
2006
|
+
const uncovered = subtractRanges({ start: range.start, end: range.end }, this.recoveryCoverage(record))
|
|
1880
2007
|
const oldest = record.oldestCreatedAt
|
|
1881
|
-
const leftEdge = oldest
|
|
1882
|
-
? [{ start: range.start, end: range.end }]
|
|
1883
|
-
:
|
|
1884
|
-
? [{ start: range.start, end: Math.min(oldest, range.end) }]
|
|
1885
|
-
: []
|
|
2008
|
+
const leftEdge = oldest != null && oldest > range.start
|
|
2009
|
+
? [{ start: range.start, end: Math.min(oldest, range.end) }]
|
|
2010
|
+
: []
|
|
1886
2011
|
const seedRanges = mergeRanges([...uncovered, ...leftEdge])
|
|
2012
|
+
.flatMap(seedRange => subtractRanges(seedRange, record.asked))
|
|
1887
2013
|
const failures = []
|
|
2014
|
+
const remaining = []
|
|
1888
2015
|
for (const seedRange of seedRanges) {
|
|
1889
2016
|
if (this.pauseReasons.size) throw this.pausedError()
|
|
1890
2017
|
const attempt = await this.#askSeedersForMissingRangeAttempt(channelPubkey, seedRange.start, seedRange.end)
|
|
1891
2018
|
failures.push(...attempt.failures.map(failure => failure.error))
|
|
2019
|
+
if (!attempt.asks.length) {
|
|
2020
|
+
remaining.push(seedRange)
|
|
2021
|
+
continue
|
|
2022
|
+
}
|
|
2023
|
+
await this.applyAskedCoverage(record, [seedRange])
|
|
1892
2024
|
}
|
|
1893
2025
|
record.lastErrors = failures
|
|
1894
|
-
return { complete:
|
|
2026
|
+
return { complete: remaining.length === 0, failures, remaining }
|
|
1895
2027
|
}
|
|
1896
2028
|
|
|
1897
2029
|
scheduleLiveRecovery (pubkey) {
|
|
@@ -1972,6 +2104,7 @@ export class PrivateMessenger {
|
|
|
1972
2104
|
|
|
1973
2105
|
unwatch (channels) {
|
|
1974
2106
|
const pubkeys = channels ? uniq(Array.isArray(channels) ? channels : [channels]) : [...this.desiredChannels]
|
|
2107
|
+
this.discardSeederPresence(pubkeys)
|
|
1975
2108
|
for (const extension of this.extensions) extension.unwatch?.(pubkeys)
|
|
1976
2109
|
for (const pubkey of pubkeys) {
|
|
1977
2110
|
this.desiredChannels.delete(pubkey)
|
|
@@ -2099,6 +2232,7 @@ export class PrivateMessenger {
|
|
|
2099
2232
|
applyPause (reason) {
|
|
2100
2233
|
if (typeof reason !== 'string' || !reason.trim()) throw new ValidationError('PAUSE_REASON_REQUIRED')
|
|
2101
2234
|
this.assertOpen()
|
|
2235
|
+
if (reason !== 'capacity') this.discardSeederPresence()
|
|
2102
2236
|
this.pauseReasons.add(reason)
|
|
2103
2237
|
this.pauseRevisions.set(reason, (this.pauseRevisions.get(reason) || 0) + 1)
|
|
2104
2238
|
const pausedError = this.pausedError()
|
|
@@ -2430,6 +2564,7 @@ export class PrivateMessenger {
|
|
|
2430
2564
|
const messageCreatedAtMs = eventCreatedAtMs(message.event, {
|
|
2431
2565
|
readMillisecondsTag: timestamped
|
|
2432
2566
|
})
|
|
2567
|
+
const protocolResolved = missingMessagesAskCoalescingHint(message)
|
|
2433
2568
|
|
|
2434
2569
|
let resolved = null
|
|
2435
2570
|
if (this.resolveInboundCoalescing) {
|
|
@@ -2467,7 +2602,11 @@ export class PrivateMessenger {
|
|
|
2467
2602
|
this.onError?.(coalescingError(new ValidationError('INVALID_INBOUND_COALESCING_RESULT')))
|
|
2468
2603
|
}
|
|
2469
2604
|
}
|
|
2470
|
-
if (!digest &&
|
|
2605
|
+
if (!digest && protocolResolved) {
|
|
2606
|
+
digest = protocolResolved.key
|
|
2607
|
+
keep = protocolResolved.keep
|
|
2608
|
+
policy = 'protocol'
|
|
2609
|
+
} else if (!digest && tagged.key) {
|
|
2471
2610
|
digest = idempotencyDigest(tagged.key)
|
|
2472
2611
|
policy = 'idempotency'
|
|
2473
2612
|
}
|
|
@@ -3389,8 +3528,16 @@ export class PrivateMessenger {
|
|
|
3389
3528
|
if (!seconds) return
|
|
3390
3529
|
const now = nowSeconds()
|
|
3391
3530
|
const current = this.readState().channels[pubkey] || {}
|
|
3392
|
-
const checkpoint = Math.max(
|
|
3393
|
-
|
|
3531
|
+
const checkpoint = Math.max(
|
|
3532
|
+
current.lastSeenAt || 0,
|
|
3533
|
+
current.recoveredThrough || 0,
|
|
3534
|
+
current.askedThrough || 0
|
|
3535
|
+
)
|
|
3536
|
+
const since = current.askedThrough
|
|
3537
|
+
? Math.max(current.askedThrough + 1, checkpoint - this.offlineSkewSeconds)
|
|
3538
|
+
: checkpoint
|
|
3539
|
+
? checkpoint - this.offlineSkewSeconds
|
|
3540
|
+
: now - seconds
|
|
3394
3541
|
this.addOfflineRange(pubkey, Math.max(0, now - seconds, since), now)
|
|
3395
3542
|
}
|
|
3396
3543
|
|
|
@@ -3486,8 +3633,8 @@ export class PrivateMessenger {
|
|
|
3486
3633
|
},
|
|
3487
3634
|
useBulkPacing: priority !== 'normal'
|
|
3488
3635
|
})
|
|
3489
|
-
asks.push(ask)
|
|
3490
3636
|
await assertMessagePublished(ask, ask.question)
|
|
3637
|
+
asks.push(ask)
|
|
3491
3638
|
} catch (err) {
|
|
3492
3639
|
const error = sanitizePublicationError(err, {
|
|
3493
3640
|
operation: 'seeder-recovery-ask',
|
|
@@ -3768,7 +3915,7 @@ export class PrivateMessenger {
|
|
|
3768
3915
|
const seeders = this.activeRecoverySeeders(channelPubkey)
|
|
3769
3916
|
if (!seeders.length) return []
|
|
3770
3917
|
const failures = []
|
|
3771
|
-
const
|
|
3918
|
+
const asked = []
|
|
3772
3919
|
for (const interval of mergeRanges(intervals)) {
|
|
3773
3920
|
if (this.pauseReasons.size) throw this.pausedError()
|
|
3774
3921
|
const attempt = await this.#askSeedersForMissingRangeAttempt(channelPubkey, interval.start, interval.end, {
|
|
@@ -3776,13 +3923,13 @@ export class PrivateMessenger {
|
|
|
3776
3923
|
priority
|
|
3777
3924
|
})
|
|
3778
3925
|
failures.push(...attempt.failures.map(failure => failure.error))
|
|
3779
|
-
if (
|
|
3926
|
+
if (attempt.asks.length) asked.push(interval)
|
|
3780
3927
|
}
|
|
3781
|
-
if (
|
|
3928
|
+
if (asked.length) {
|
|
3782
3929
|
for (const record of this.recoveryRanges.values()) {
|
|
3783
3930
|
if (record.channelPubkey !== channelPubkey) continue
|
|
3784
|
-
const applied = intersectRanges(
|
|
3785
|
-
if (applied.length) this.
|
|
3931
|
+
const applied = intersectRanges(asked, [{ start: record.start, end: record.end }])
|
|
3932
|
+
if (applied.length) await this.applyAskedCoverage(record, applied)
|
|
3786
3933
|
}
|
|
3787
3934
|
}
|
|
3788
3935
|
return failures
|
|
@@ -3808,7 +3955,7 @@ export class PrivateMessenger {
|
|
|
3808
3955
|
[{ start: range.start, end: range.end }]
|
|
3809
3956
|
)
|
|
3810
3957
|
if (!priorityIntervals.length) continue
|
|
3811
|
-
const pending = subtractRanges({ start: range.start, end: range.end }, record
|
|
3958
|
+
const pending = subtractRanges({ start: range.start, end: range.end }, this.recoveryCoverage(record))
|
|
3812
3959
|
const wanted = intersectRanges(priorityIntervals, pending)
|
|
3813
3960
|
if (!wanted.length) continue
|
|
3814
3961
|
const candidates = this.orderedRecoveryRelays(record)
|
|
@@ -3825,13 +3972,13 @@ export class PrivateMessenger {
|
|
|
3825
3972
|
})
|
|
3826
3973
|
if (winner?.history) {
|
|
3827
3974
|
if (this.isPartialRecoveryHistory(winner.history)) this.updateRecoveryRecord(record, winner.history, { completeRelays: new Set() })
|
|
3828
|
-
else this.
|
|
3975
|
+
else this.applyRelayCoverage(record, wanted)
|
|
3829
3976
|
}
|
|
3830
3977
|
}
|
|
3831
3978
|
if (this.pauseReasons.size || this.closePromise) throw this.pausedError()
|
|
3832
3979
|
const currentUncovered = () => intersectRanges(
|
|
3833
3980
|
wanted,
|
|
3834
|
-
subtractRanges({ start: range.start, end: range.end }, record
|
|
3981
|
+
subtractRanges({ start: range.start, end: range.end }, this.recoveryCoverage(record))
|
|
3835
3982
|
)
|
|
3836
3983
|
const normalUncovered = intersectRanges(currentUncovered(), normalWindows)
|
|
3837
3984
|
if (normalUncovered.length) await this.askPrioritySeeders(channelPubkey, normalUncovered, { priority: 'normal' })
|
|
@@ -3909,7 +4056,7 @@ export class PrivateMessenger {
|
|
|
3909
4056
|
} else {
|
|
3910
4057
|
// Legacy fetch seam (tests/custom channels): resolves only when
|
|
3911
4058
|
// the whole range was read, so treat it as fully relay-covered.
|
|
3912
|
-
|
|
4059
|
+
this.applyRelayCoverage(record, [{ start: range.start, end: range.end }])
|
|
3913
4060
|
if (history?.oldestCreatedAt != null) {
|
|
3914
4061
|
record.oldestCreatedAt = record.oldestCreatedAt == null
|
|
3915
4062
|
? history.oldestCreatedAt
|
|
@@ -3926,11 +4073,11 @@ export class PrivateMessenger {
|
|
|
3926
4073
|
const lifecycleChanged = controller.signal.aborted || this.closePromise || !this.channels.has(pubkey) || !this.stopByChannel.has(pubkey) ||
|
|
3927
4074
|
(this.watchRevisionByChannel.get(pubkey) || 0) !== watchRevision
|
|
3928
4075
|
if (lifecycleChanged) {
|
|
3929
|
-
remaining.push(range)
|
|
4076
|
+
remaining.push(...this.unhandledRecoveryRanges(record, range))
|
|
3930
4077
|
continue
|
|
3931
4078
|
}
|
|
3932
4079
|
if (this.recoveryRecordActive(record)) {
|
|
3933
|
-
remaining.push(range)
|
|
4080
|
+
remaining.push(...this.unhandledRecoveryRanges(record, range))
|
|
3934
4081
|
continue
|
|
3935
4082
|
}
|
|
3936
4083
|
const recoveryStartedAt = this._now()
|
|
@@ -3952,7 +4099,7 @@ export class PrivateMessenger {
|
|
|
3952
4099
|
})
|
|
3953
4100
|
failures.push(...result.failures)
|
|
3954
4101
|
if (result.complete) recoveredThrough = Math.max(recoveredThrough, range.end)
|
|
3955
|
-
else remaining.push(range)
|
|
4102
|
+
else remaining.push(...(result.remaining || this.unhandledRecoveryRanges(record, range)))
|
|
3956
4103
|
} catch (err) {
|
|
3957
4104
|
const expectedPause = isExpectedRecoveryPause(err, controller)
|
|
3958
4105
|
const record = this.recoveryRanges.get(this.recoveryRangeKey(pubkey, range))
|
|
@@ -3961,7 +4108,7 @@ export class PrivateMessenger {
|
|
|
3961
4108
|
}
|
|
3962
4109
|
if (!expectedPause && !(controller.liveInterrupted && err === controller.signal.reason)) this.onError?.(err)
|
|
3963
4110
|
if (!expectedPause) failures.push(err)
|
|
3964
|
-
remaining.push(range)
|
|
4111
|
+
remaining.push(...this.unhandledRecoveryRanges(record, range))
|
|
3965
4112
|
} finally { this.recoveryControllers.delete(controller) }
|
|
3966
4113
|
}
|
|
3967
4114
|
const fresh = this.readState()
|
|
@@ -4184,6 +4331,7 @@ export class PrivateMessenger {
|
|
|
4184
4331
|
await (this.seedStorage ? this.seedStorage.removeLocal({ channelPubkey: pubkey }) : this.seedQueue?.removeBy('byChannel', pubkey))
|
|
4185
4332
|
}
|
|
4186
4333
|
for (const pubkey of stalePubkeys) this.dropRecoveryState(pubkey)
|
|
4334
|
+
this.discardSeederPresence(stalePubkeys)
|
|
4187
4335
|
this.state = state
|
|
4188
4336
|
if (stalePubkeys.length) await this.removeChannelStates(stalePubkeys)
|
|
4189
4337
|
})
|
|
@@ -4221,6 +4369,7 @@ export class PrivateMessenger {
|
|
|
4221
4369
|
this.decryptionScheduler.close()
|
|
4222
4370
|
this.stopDecryptionForeground?.()
|
|
4223
4371
|
if (this.closePromise) return this.closePromise
|
|
4372
|
+
this.discardSeederPresence()
|
|
4224
4373
|
const initSettledPromise = this.initSettledPromise
|
|
4225
4374
|
this.sendRoutingLifetime.abort()
|
|
4226
4375
|
this.publicationRelayHealth.clear()
|
|
@@ -4434,6 +4583,20 @@ function idempotencyDigest (idempotencyKey) {
|
|
|
4434
4583
|
return coalescingDigest(['idempotency', idempotencyKey])
|
|
4435
4584
|
}
|
|
4436
4585
|
|
|
4586
|
+
function missingMessagesAskCoalescingHint (message) {
|
|
4587
|
+
if (messageCode(message) !== MISSING_MESSAGES_ASK_CODE) return null
|
|
4588
|
+
const payload = message?.payload?.payload
|
|
4589
|
+
const since = payload?.since
|
|
4590
|
+
const until = payload?.until
|
|
4591
|
+
if (!Number.isSafeInteger(since) || !Number.isSafeInteger(until) || until < since) return null
|
|
4592
|
+
const priority = payload.priority == null ? 'normal' : payload.priority
|
|
4593
|
+
if (!['normal', 'lower', 'lowest'].includes(priority)) return null
|
|
4594
|
+
return {
|
|
4595
|
+
key: coalescingDigest(['missing-messages-ask', since, until, priority]),
|
|
4596
|
+
keep: 'latest'
|
|
4597
|
+
}
|
|
4598
|
+
}
|
|
4599
|
+
|
|
4437
4600
|
function coalescingDigest (value) {
|
|
4438
4601
|
return bytesToHex(sha256(encoder.encode(JSON.stringify(value))))
|
|
4439
4602
|
}
|