mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-25 12:25:19 +00:00
New MessageType 0x23 carries TLV-encoded board posts and tombstones,
self-signed with the author's Ed25519 key ("bitchat-board-v1" /
"bitchat-board-del-v1" domains) so notices verify without the author
present. BoardStore persists raw signed packets under Application
Support/board/ (200 posts, 5 per author, oldest evicted; expiry sweep;
tombstones retained until the deleted post's original expiry) and is
wiped on panic.
Board packets join gossip sync as bit 8 of the existing variable-length
types bitfield (a second byte old decoders already accept and ignore),
with a 60s round and its own capacity, served straight from the board
store so retention has one owner. Posts relay like broadcasts; urgent
posts get the announce-class TTL cap.
UI: a pin button in the header opens the board for the current channel
(geohash board, or mesh-local board), with urgent-pinned newest-first
listing, compose with urgent toggle and 1/3/7-day expiry, and
swipe-delete on own posts. Geohash posts also publish one-way as
Nostr kind-1 location notes when relays are reachable.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
93 lines
4.0 KiB
Swift
93 lines
4.0 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,
|
|
recipientIsSelf: Bool = false,
|
|
isEncrypted: Bool,
|
|
isDirectedEncrypted: Bool,
|
|
isFragment: Bool,
|
|
isDirectedFragment: Bool,
|
|
isHandshake: Bool,
|
|
isAnnounce: Bool,
|
|
isRequestSync: Bool = false,
|
|
isUrgentBoardPost: Bool = false,
|
|
degree: Int,
|
|
highDegreeThreshold: Int) -> RelayDecision {
|
|
let ttlCap = min(ttl, TransportConfig.messageTTLDefault)
|
|
|
|
// REQUEST_SYNC is link-local: never relay it, even when a peer crafts
|
|
// one with TTL headroom to turn every reachable node into a responder.
|
|
if isRequestSync {
|
|
return RelayDecision(shouldRelay: false, newTTL: ttlCap, delayMs: 0)
|
|
}
|
|
|
|
// Suppress obvious non-relays
|
|
if ttlCap <= 1 || senderIsSelf || recipientIsSelf {
|
|
return RelayDecision(shouldRelay: false, newTTL: ttlCap, 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 = ttlCap &- 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)
|
|
}
|
|
|
|
if isFragment {
|
|
// Dense graphs clamp harder to contain full-fanout fragment floods;
|
|
// sparse graphs get full depth so media reaches as far as text.
|
|
let fragmentCap = degree >= highDegreeThreshold
|
|
? TransportConfig.bleFragmentRelayTtlCapDense
|
|
: TransportConfig.bleFragmentRelayTtlCap
|
|
let ttlLimit = min(ttlCap, fragmentCap)
|
|
guard ttlLimit > 1 else {
|
|
return RelayDecision(shouldRelay: false, newTTL: ttlLimit, delayMs: 0)
|
|
}
|
|
let newTTL = ttlLimit &- 1
|
|
let delayMs = Int.random(in: TransportConfig.bleFragmentRelayMinDelayMs...TransportConfig.bleFragmentRelayMaxDelayMs)
|
|
return RelayDecision(shouldRelay: true, newTTL: newTTL, delayMs: delayMs)
|
|
}
|
|
|
|
// TTL clamping for broadcast
|
|
// - Dense graphs: keep lower but still allow multi-hop bridging
|
|
// - Thin chains (degree <= 2): every hop counts and flood cost is
|
|
// minimal, so relay at full incoming depth
|
|
// - Announces (and urgent board posts) get a bit more headroom
|
|
let ttlLimit: UInt8 = {
|
|
if degree >= highDegreeThreshold {
|
|
return max(UInt8(2), min(ttlCap, UInt8(5)))
|
|
}
|
|
if degree <= 2 {
|
|
return ttlCap
|
|
}
|
|
let preferred = UInt8((isAnnounce || isUrgentBoardPost) ? 7 : 6)
|
|
return max(UInt8(2), min(ttlCap, preferred))
|
|
}()
|
|
let newTTL = ttlLimit &- 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: true, newTTL: newTTL, delayMs: delayMs)
|
|
}
|
|
}
|