mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-25 12:25:19 +00:00
* Sign public broadcasts; verify relayed messages via persisted signing keys; keep scheduled relays in sparse graphs and speed their jitter; persist announce signing key for offline auth; add short backoff after disconnect errors to reduce reconnect thrash * Add connected vs reachable model: retain peers after link drop, expire after reachability window; expose all peers in snapshots; compute isReachable in UI; add meshReachable state and sorting; avoid removing peers on link events; notify UI on stale removals * ContentView: handle new .meshReachable connection state in header icon switch (exhaustive switch fix) * Logs: tag relayed announces as 'Reachable via mesh' and annotate public message logs with (direct|mesh) path for easier field analysis * Fix syntax error: remove stray else/log inserted into writeOrEnqueue; keep logs clean * UI: use 'point.3.connected.trianglepath.dotted' for mesh-reachable; change people count to include connected+reachable (exclude Nostr-only) * UI: switch to 'point.3.filled.connected.trianglepath.dotted' for mesh-reachable icons in list and header * Reachability: reduce retention to 21s for all peers (verified and unverified) to minimize stale presence * mesh DMs/acks: route to reachable peers; queue READ/DELIVERED until handshake; add Transport.isPeerReachable; UI: hide offline non-mutuals; DM header: better name fallback + show transport + encryption icons; fix NostrTransport conformance * Verification sheet: compute encryption status and fingerprint using short mesh ID mapping (fix 'not encrypted/handshake' for DMs with stable key) * Announce cadence: faster discovery (4s), sparse 15±4s, dense 30±8s; initial 0.6s; post-subscribe 50ms; min-force 150ms; maintenance 5s; proactive announces on handshake + recent-traffic nudge * Relay: increase broadcast TTL cap in sparse graphs to 6; tighten jitter for handshake (10–35ms) and directed (20–60ms) relays * Range/robustness: store-and-forward for directed packets (15s) with flush on new links + periodic; announces: no subset + afterglow re-announce on first-seen; adaptive scanning: force ON when <=2 neighbors or recent traffic * Fix warnings: remove unused msgID and unused mutable var in directed spool flush * Announces: TTL 7 (sparse only) via RelayController; no fanout subset for announces; neighbor-change rebroadcast of last 2–3 announces. Fragments: faster pacing (5ms global, 4ms directed). * Peer list: real-time icon updates by publishing snapshots on connectivity checks; add unread message indicator (envelope) next to peers with unread DMs * UI: unread envelope uses orange; hasUnreadMessages checks Nostr conv key for peers with known Nostr pubkeys (geohash DM consistency) * Logs/robustness: debounce disconnect notifications (1.5s), debounce 'reconnected' logs (2s), add weak-link cooldown after timeouts on very weak RSSI (<= -90) * Peer icons: faster, accurate reachability\n\n- Run connectivity checks every maintenance tick (5s)\n- Publish peer snapshots on central unsubscribe for instant UI refresh\n- Lower inactivity timeout to 8s and disconnect debounce to 0.9s\n- Gate reachability on mesh-attached (>=1 direct link); no links => no reachable peers\n- Keep 21s retention for verified/unverified, but only when attached to mesh\n\nImproves list responsiveness when walking out of range and prevents stale 'reachable' states when isolated. --------- Co-authored-by: jack <jackjackbits@users.noreply.github.com>
70 lines
2.8 KiB
Swift
70 lines
2.8 KiB
Swift
import Foundation
|
|
|
|
// RelayDecision encapsulates a single relay scheduling choice.
|
|
struct RelayDecision {
|
|
let shouldRelay: Bool
|
|
let newTTL: UInt8
|
|
let delayMs: Int
|
|
}
|
|
|
|
// RelayController centralizes flood control policy for relays.
|
|
struct RelayController {
|
|
static func decide(ttl: UInt8,
|
|
senderIsSelf: Bool,
|
|
isEncrypted: Bool,
|
|
isDirectedEncrypted: Bool,
|
|
isDirectedFragment: Bool,
|
|
isHandshake: Bool,
|
|
isAnnounce: Bool,
|
|
degree: Int,
|
|
highDegreeThreshold: Int) -> RelayDecision {
|
|
// Suppress obvious non-relays
|
|
if ttl <= 1 || senderIsSelf { return RelayDecision(shouldRelay: false, newTTL: ttl, delayMs: 0) }
|
|
|
|
// For session-critical or directed traffic, be deterministic and reliable
|
|
if isHandshake || isDirectedFragment || isDirectedEncrypted {
|
|
// Always relay with no TTL cap for these types
|
|
let newTTL = (ttl &- 1)
|
|
// Slight jitter to desynchronize without adding too much latency
|
|
// Tighter for faster multi-hop handshakes and directed DMs
|
|
let delayRange: ClosedRange<Int> = isHandshake ? 10...35 : 20...60
|
|
let delayMs = Int.random(in: delayRange)
|
|
return RelayDecision(shouldRelay: true, newTTL: newTTL, delayMs: delayMs)
|
|
}
|
|
|
|
// Degree-aware probability to reduce floods in dense graphs (broadcast/public)
|
|
let baseProb: Double
|
|
switch degree {
|
|
case 0...2: baseProb = 1.0
|
|
case 3...4: baseProb = 0.9
|
|
case 5...6: baseProb = 0.7
|
|
case 7...9: baseProb = 0.55
|
|
default: baseProb = 0.45
|
|
}
|
|
let prob = baseProb
|
|
let shouldRelay = Double.random(in: 0...1) <= prob
|
|
|
|
// TTL clamping for broadcast
|
|
// - Dense graphs: keep very low to avoid floods
|
|
// - Sparse graphs: allow slightly longer reach for multi-hop discovery
|
|
// - Announces in sparse graphs get a bit more headroom
|
|
let ttlCap: UInt8 = {
|
|
if degree >= highDegreeThreshold { return 3 }
|
|
return isAnnounce ? 7 : 6
|
|
}()
|
|
let clamped = max(1, min(ttl, ttlCap))
|
|
let newTTL = clamped &- 1
|
|
|
|
// Wider jitter window to allow duplicate suppression to win more often
|
|
// For sparse graphs (<=2), relay quickly to avoid cancellation races
|
|
let delayMs: Int
|
|
switch degree {
|
|
case 0...2: delayMs = Int.random(in: 10...40)
|
|
case 3...5: delayMs = Int.random(in: 60...150)
|
|
case 6...9: delayMs = Int.random(in: 80...180)
|
|
default: delayMs = Int.random(in: 100...220)
|
|
}
|
|
return RelayDecision(shouldRelay: shouldRelay, newTTL: newTTL, delayMs: delayMs)
|
|
}
|
|
}
|