This commit is contained in:
callebtc
2026-01-09 16:03:17 +07:00
parent 31275856dd
commit 3c7e14f49d
3 changed files with 25 additions and 12 deletions
+2 -4
View File
@@ -779,8 +779,7 @@ final class BLEService: NSObject {
private func broadcastPacket(_ packet: BitchatPacket, transferId: String? = nil) { private func broadcastPacket(_ packet: BitchatPacket, transferId: String? = nil) {
// Apply route if recipient exists (centralized route application) // Apply route if recipient exists (centralized route application)
let packetToSend: BitchatPacket let packetToSend: BitchatPacket
if let recipientID = packet.recipientID, if let recipientPeerID = PeerID(hexData: packet.recipientID) {
let recipientPeerID = PeerID(hexData: recipientID) {
packetToSend = applyRouteIfAvailable(packet, to: recipientPeerID) packetToSend = applyRouteIfAvailable(packet, to: recipientPeerID)
} else { } else {
packetToSend = packet packetToSend = packet
@@ -2519,8 +2518,7 @@ extension BLEService {
// If we're the last intermediate hop, forward to destination // If we're the last intermediate hop, forward to destination
if index == route.count - 1 { if index == route.count - 1 {
guard packet.ttl > 1 else { return true } guard packet.ttl > 1 else { return true }
guard let recipientID = packet.recipientID, guard let destinationPeer = PeerID(hexData: packet.recipientID),
let destinationPeer = PeerID(hexData: recipientID),
isPeerConnected(destinationPeer) else { isPeerConnected(destinationPeer) else {
return false return false
} }
+2 -2
View File
@@ -76,7 +76,7 @@ final class MeshTopologyTracker {
func computeRoute(from start: Data?, to goal: Data?, maxHops: Int = 10) -> [Data]? { func computeRoute(from start: Data?, to goal: Data?, maxHops: Int = 10) -> [Data]? {
guard let source = sanitize(start), let target = sanitize(goal) else { return nil } guard let source = sanitize(start), let target = sanitize(goal) else { return nil }
if source == target { return nil } // Direct connection, no intermediate hops if source == target { return [] } // Direct connection, no intermediate hops
let graph = queue.sync { adjacency } let graph = queue.sync { adjacency }
guard graph[source] != nil, graph[target] != nil else { return nil } guard graph[source] != nil, graph[target] != nil else { return nil }
@@ -97,7 +97,7 @@ final class MeshTopologyTracker {
nextPath.append(neighbor) nextPath.append(neighbor)
if neighbor == target { if neighbor == target {
// Remove start and end, return only intermediate hops // Remove start and end, return only intermediate hops
guard nextPath.count >= 2 else { return nil } guard nextPath.count >= 2 else { return [] }
return Array(nextPath.dropFirst().dropLast()) return Array(nextPath.dropFirst().dropLast())
} }
if nextPath.count <= maxHops { if nextPath.count <= maxHops {
@@ -22,7 +22,8 @@ struct MeshTopologyTrackerTests {
tracker.recordDirectLink(between: a, and: b) tracker.recordDirectLink(between: a, and: b)
let route = try #require(tracker.computeRoute(from: a, to: b)) let route = try #require(tracker.computeRoute(from: a, to: b))
#expect(route == [a, b]) // Direct connection returns empty route (no intermediate hops)
#expect(route == [])
} }
@Test func multiHopRouteComputation() throws { @Test func multiHopRouteComputation() throws {
@@ -37,7 +38,8 @@ struct MeshTopologyTrackerTests {
tracker.recordDirectLink(between: c, and: d) tracker.recordDirectLink(between: c, and: d)
let route = try #require(tracker.computeRoute(from: a, to: d)) let route = try #require(tracker.computeRoute(from: a, to: d))
#expect(route == [a, b, c, d]) // Route should only contain intermediate hops (b, c), excluding start (a) and end (d)
#expect(route == [b, c])
} }
@Test func recordRouteAddsEdges() throws { @Test func recordRouteAddsEdges() throws {
@@ -50,8 +52,8 @@ struct MeshTopologyTrackerTests {
a.append(Data(repeating: 0, count: BinaryProtocol.senderIDSize - a.count)) a.append(Data(repeating: 0, count: BinaryProtocol.senderIDSize - a.count))
let route = try #require(tracker.computeRoute(from: a, to: c)) let route = try #require(tracker.computeRoute(from: a, to: c))
#expect(route.first == a) // Route should only contain intermediate hops (b), excluding start (a) and end (c)
#expect(route.last == c) #expect(route == [b])
} }
@Test func removingDirectLinkBreaksRoute() throws { @Test func removingDirectLinkBreaksRoute() throws {
@@ -63,7 +65,8 @@ struct MeshTopologyTrackerTests {
tracker.recordDirectLink(between: a, and: b) tracker.recordDirectLink(between: a, and: b)
tracker.recordDirectLink(between: b, and: c) tracker.recordDirectLink(between: b, and: c)
let initialRoute = try #require(tracker.computeRoute(from: a, to: c)) let initialRoute = try #require(tracker.computeRoute(from: a, to: c))
#expect(initialRoute == [a, b, c]) // Route should only contain intermediate hops (b), excluding start (a) and end (c)
#expect(initialRoute == [b])
tracker.removeDirectLink(between: b, and: c) tracker.removeDirectLink(between: b, and: c)
#expect(tracker.computeRoute(from: a, to: c) == nil) #expect(tracker.computeRoute(from: a, to: c) == nil)
@@ -77,10 +80,22 @@ struct MeshTopologyTrackerTests {
tracker.recordRoute([a, b, c]) tracker.recordRoute([a, b, c])
let initialRoute = try #require(tracker.computeRoute(from: a, to: c)) let initialRoute = try #require(tracker.computeRoute(from: a, to: c))
#expect(initialRoute == [a, b, c]) // Route should only contain intermediate hops (b), excluding start (a) and end (c)
#expect(initialRoute == [b])
tracker.removePeer(b) tracker.removePeer(b)
#expect(tracker.computeRoute(from: a, to: c) == nil) #expect(tracker.computeRoute(from: a, to: c) == nil)
} }
@Test func sameStartAndEndReturnsEmptyRoute() throws {
let tracker = MeshTopologyTracker()
let a = try hex("0102030405060708")
let b = try hex("1112131415161718")
tracker.recordDirectLink(between: a, and: b)
// When start == end, route should be empty (no intermediate hops needed)
let route = try #require(tracker.computeRoute(from: a, to: a))
#expect(route == [])
}
} }