Optimize BLE mesh network for robustness and range (#314)

- Fixed relay probability calculation for 2-node networks (0% relay needed)
- Added protocol ACKs to prevent unnecessary retransmissions
- Implemented MessageState for enhanced duplicate detection
- Added exponential backoff for collision avoidance
- Fixed duplicate sends for bidirectional connections
- Resolved packet ID generation issues using immutable fields only
- Implemented smart rate limiting with progressive throttling
- Removed unnecessary debug logging and fixed build warnings
- Optimized message routing to prevent flooding in small networks

Co-authored-by: jack <jackjackbits@users.noreply.github.com>
This commit is contained in:
jack
2025-07-24 19:49:37 +02:00
committed by GitHub
co-authored by jack
parent 498bb30d82
commit 492f90edd5
8 changed files with 831 additions and 172 deletions
@@ -365,7 +365,8 @@ final class PrivateChatE2ETests: XCTestCase {
timestamp: packet.timestamp,
payload: encrypted,
signature: packet.signature,
ttl: packet.ttl
ttl: packet.ttl,
sequenceNumber: 1
)
self.bob.simulateIncomingPacket(encryptedPacket)
} catch {
@@ -122,7 +122,8 @@ final class PublicChatE2ETests: XCTestCase {
timestamp: packet.timestamp,
payload: relayPayload,
signature: packet.signature,
ttl: packet.ttl - 1
ttl: packet.ttl - 1,
sequenceNumber: 1
)
// Simulate relay to Charlie
@@ -450,7 +451,8 @@ final class PublicChatE2ETests: XCTestCase {
timestamp: packet.timestamp,
payload: relayPayload,
signature: packet.signature,
ttl: packet.ttl - 1
ttl: packet.ttl - 1,
sequenceNumber: 1
)
// Relay to next hops
@@ -197,7 +197,8 @@ final class IntegrationTests: XCTestCase {
timestamp: packet.timestamp,
payload: encrypted,
signature: packet.signature,
ttl: packet.ttl
ttl: packet.ttl,
sequenceNumber: 1
)
self.nodes["Bob"]!.simulateIncomingPacket(encPacket)
}
@@ -698,7 +699,8 @@ final class IntegrationTests: XCTestCase {
timestamp: packet.timestamp,
payload: encrypted,
signature: packet.signature,
ttl: packet.ttl
ttl: packet.ttl,
sequenceNumber: 1
)
self.nodes["Bob"]!.simulateIncomingPacket(encPacket)
}
@@ -805,7 +807,8 @@ final class IntegrationTests: XCTestCase {
timestamp: packet.timestamp,
payload: relayPayload,
signature: packet.signature,
ttl: packet.ttl - 1
ttl: packet.ttl - 1,
sequenceNumber: 1
)
for hop in nextHops {
@@ -73,7 +73,8 @@ class MockBluetoothMeshService: BluetoothMeshService {
timestamp: UInt64(Date().timeIntervalSince1970 * 1000),
payload: payload,
signature: nil,
ttl: 3
ttl: 3,
sequenceNumber: 1
)
sentMessages.append((message, packet))
@@ -111,7 +112,8 @@ class MockBluetoothMeshService: BluetoothMeshService {
timestamp: UInt64(Date().timeIntervalSince1970 * 1000),
payload: payload,
signature: nil,
ttl: 3
ttl: 3,
sequenceNumber: 1
)
sentMessages.append((message, packet))
+4 -2
View File
@@ -55,7 +55,8 @@ class TestHelpers {
recipientID: String? = nil,
payload: Data = "test payload".data(using: .utf8)!,
signature: Data? = nil,
ttl: UInt8 = 3
ttl: UInt8 = 3,
sequenceNumber: UInt32 = 1
) -> BitchatPacket {
return BitchatPacket(
type: type,
@@ -64,7 +65,8 @@ class TestHelpers {
timestamp: UInt64(Date().timeIntervalSince1970 * 1000),
payload: payload,
signature: signature,
ttl: ttl
ttl: ttl,
sequenceNumber: sequenceNumber
)
}