mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-25 19:25:20 +00:00
Sync cleanups: normalize SyncTypeFlags, single announce-ID path, TODO (#1373)
Follow-ups deferred from the REQUEST_SYNC review (#1371): - SyncTypeFlags.init(rawValue:) now masks to the union of bits that map to a known message type (derived from the bit↔type table, so it tracks new types automatically). Phantom bits from a truncated/garbled flags field — or a type a newer peer added — no longer live in the set as membership no contains() matches yet toData() re-serializes. - GossipSyncManager stored each latest announce as (hex-id string, packet) and diffed announces against the stored string while every other type recomputed the ID via PacketIdUtil. Collapsed the store to just the packet and recompute the ID everywhere, removing the latent dual-path divergence. - Documented the REQUEST_SYNC TLV table and marked fragmentIdFilter (0x06) with a TODO(v2): it's parsed/re-serialized but never populated or honored (reserved for incremental fragment sync) — finish or drop, not silent dead surface. Adds SyncTypeFlags phantom-bit/round-trip tests and a GossipSyncManager test that an announce already in the requester's filter is suppressed (guards the recompute path). Full suite: 1034 tests pass. Co-authored-by: jack <jackjackbits@users.noreply.github.com> Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
co-authored by
jack
Claude Opus 4.8
parent
7341696280
commit
c2a5668569
@@ -4,6 +4,15 @@ import Foundation
|
||||
// - 0x01: P (uint8) — Golomb-Rice parameter
|
||||
// - 0x02: M (uint32, big-endian) — hash range (N * 2^P)
|
||||
// - 0x03: data (opaque) — GR bitstream bytes (MSB-first)
|
||||
// - 0x04: types (bitfield) — SyncTypeFlags of covered message types
|
||||
// - 0x05: sinceTimestamp (uint64, big-endian) — oldest ts the filter covers
|
||||
// - 0x06: fragmentIdFilter (utf8) — reserved
|
||||
//
|
||||
// TODO(v2): fragmentIdFilter (0x06) is parsed and re-serialized but never
|
||||
// populated or honored — it's the reserved surface for incremental fragment
|
||||
// sync (request the missing fragments of one file by ID instead of diffing the
|
||||
// whole fragment set). Either wire it into buildGcsPayload/_handleRequestSync
|
||||
// or drop the field; don't leave it as silent dead protocol surface.
|
||||
struct RequestSyncPacket {
|
||||
let p: Int
|
||||
let m: UInt32
|
||||
|
||||
@@ -90,7 +90,7 @@ final class GossipSyncManager {
|
||||
private var messages = PacketStore()
|
||||
private var fragments = PacketStore()
|
||||
private var fileTransfers = PacketStore()
|
||||
private var latestAnnouncementByPeer: [PeerID: (id: String, packet: BitchatPacket)] = [:]
|
||||
private var latestAnnouncementByPeer: [PeerID: BitchatPacket] = [:]
|
||||
private var archiveDirty = false
|
||||
|
||||
// Timer
|
||||
@@ -206,9 +206,8 @@ final class GossipSyncManager {
|
||||
removeState(for: sender)
|
||||
return
|
||||
}
|
||||
let idHex = PacketIdUtil.computeId(packet).hexEncodedString()
|
||||
let sender = PeerID(hexData: packet.senderID)
|
||||
latestAnnouncementByPeer[sender] = (id: idHex, packet: packet)
|
||||
latestAnnouncementByPeer[sender] = packet
|
||||
case .message:
|
||||
guard isBroadcastRecipient else { return }
|
||||
guard isPacketFresh(packet) else { return }
|
||||
@@ -312,10 +311,9 @@ final class GossipSyncManager {
|
||||
// keys needed to verify everything else, and there is at most one per
|
||||
// peer, so the resend cost is negligible.
|
||||
if requestedTypes.contains(.announce) {
|
||||
for (_, pair) in latestAnnouncementByPeer {
|
||||
let (idHex, pkt) = pair
|
||||
for (_, pkt) in latestAnnouncementByPeer {
|
||||
guard isPacketFresh(pkt) else { continue }
|
||||
let idBytes = Data(hexString: idHex) ?? Data()
|
||||
let idBytes = PacketIdUtil.computeId(pkt)
|
||||
if !mightContain(idBytes) {
|
||||
var toSend = pkt
|
||||
toSend.ttl = 0
|
||||
@@ -372,8 +370,8 @@ final class GossipSyncManager {
|
||||
private func buildGcsPayload(for types: SyncTypeFlags) -> Data {
|
||||
var candidates: [BitchatPacket] = []
|
||||
if types.contains(.announce) {
|
||||
for (_, pair) in latestAnnouncementByPeer where isPacketFresh(pair.packet) {
|
||||
candidates.append(pair.packet)
|
||||
for (_, pkt) in latestAnnouncementByPeer where isPacketFresh(pkt) {
|
||||
candidates.append(pkt)
|
||||
}
|
||||
}
|
||||
if types.contains(.message) {
|
||||
@@ -431,8 +429,8 @@ final class GossipSyncManager {
|
||||
// Periodic cleanup of expired messages and announcements
|
||||
private func cleanupExpiredMessages() {
|
||||
// Remove expired announcements
|
||||
latestAnnouncementByPeer = latestAnnouncementByPeer.filter { _, pair in
|
||||
isPacketFresh(pair.packet)
|
||||
latestAnnouncementByPeer = latestAnnouncementByPeer.filter { _, pkt in
|
||||
isPacketFresh(pkt)
|
||||
}
|
||||
|
||||
let messageCountBefore = messages.packets.count
|
||||
@@ -512,8 +510,8 @@ final class GossipSyncManager {
|
||||
let nowMs = UInt64(now.timeIntervalSince1970 * 1000)
|
||||
guard nowMs >= timeoutMs else { return }
|
||||
let cutoff = nowMs - timeoutMs
|
||||
let stalePeerIDs = latestAnnouncementByPeer.compactMap { peerID, pair in
|
||||
pair.packet.timestamp < cutoff ? peerID : nil
|
||||
let stalePeerIDs = latestAnnouncementByPeer.compactMap { peerID, pkt in
|
||||
pkt.timestamp < cutoff ? peerID : nil
|
||||
}
|
||||
guard !stalePeerIDs.isEmpty else { return }
|
||||
for peerKey in stalePeerIDs {
|
||||
|
||||
@@ -7,9 +7,25 @@ struct SyncTypeFlags: OptionSet {
|
||||
let rawValue: UInt64
|
||||
|
||||
init(rawValue: UInt64) {
|
||||
self.rawValue = rawValue & 0x00FF_FFFF_FFFF_FFFF // Trim to max 8 bytes
|
||||
// Drop any bit that doesn't map to a known message type. Wire data can
|
||||
// carry up to 8 bytes of flags; without this mask, bits with no type
|
||||
// (a truncated/garbled field, or a type a newer peer added) would live
|
||||
// in the set as phantom membership that no `contains` check matches and
|
||||
// `toData` re-serializes — a meaningless "accepted but does nothing"
|
||||
// state. Masking here keeps every instance normalized at the source.
|
||||
self.rawValue = rawValue & SyncTypeFlags.knownTypeMask
|
||||
}
|
||||
|
||||
/// Union of every bit that maps to a message type. Derived from the
|
||||
/// bit↔type table so it tracks automatically when a type is added.
|
||||
private static let knownTypeMask: UInt64 = {
|
||||
var mask: UInt64 = 0
|
||||
for bit in 0..<64 where SyncTypeFlags.type(forBit: bit) != nil {
|
||||
mask |= (1 << UInt64(bit))
|
||||
}
|
||||
return mask
|
||||
}()
|
||||
|
||||
private static func bitIndex(for type: MessageType) -> Int? {
|
||||
switch type {
|
||||
case .announce: return 0
|
||||
|
||||
Reference in New Issue
Block a user