Plumtree sync (#393)

* wip plumtree

* sync works

* fix logging

* ttl to 0

* fix send packet to one peer

* spec

* wip GCS instead of bloom

* remove bloom filter remainders

* clean

* prune old announcements

* remove announcements from sync after LEAVE

* sync after 1 second

* pruning

* track own announcement and prune messages without announcements

* fix pruning

* getGcsMaxFilterBytes default value 400 bytes

* parameters
This commit is contained in:
callebtc
2025-09-14 03:32:10 +02:00
committed by GitHub
parent 3967ef8922
commit 73c91b9509
18 changed files with 963 additions and 60 deletions
@@ -41,7 +41,7 @@ class PacketRelayManager(private val myPeerID: String) {
val packet = routed.packet
val peerID = routed.peerID ?: "unknown"
Log.d(TAG, "Evaluating relay for packet type ${'$'}{packet.type} from ${'$'}peerID (TTL: ${'$'}{packet.ttl})")
Log.d(TAG, "Evaluating relay for packet type ${packet.type} from ${peerID} (TTL: ${packet.ttl})")
// Double-check this packet isn't addressed to us
if (isPacketAddressedToMe(packet)) {
@@ -63,7 +63,7 @@ class PacketRelayManager(private val myPeerID: String) {
// Decrement TTL by 1
val relayPacket = packet.copy(ttl = (packet.ttl - 1u).toUByte())
Log.d(TAG, "Decremented TTL from ${'$'}{packet.ttl} to ${'$'}{relayPacket.ttl}")
Log.d(TAG, "Decremented TTL from ${packet.ttl} to ${relayPacket.ttl}")
// Apply relay logic based on packet type and debug switch
val shouldRelay = isRelayEnabled() && shouldRelayPacket(relayPacket, peerID)
@@ -71,7 +71,7 @@ class PacketRelayManager(private val myPeerID: String) {
if (shouldRelay) {
relayPacket(RoutedPacket(relayPacket, peerID, routed.relayAddress))
} else {
Log.d(TAG, "Relay decision: NOT relaying packet type ${'$'}{packet.type}")
Log.d(TAG, "Relay decision: NOT relaying packet type ${packet.type}")
}
}
@@ -103,7 +103,7 @@ class PacketRelayManager(private val myPeerID: String) {
private fun shouldRelayPacket(packet: BitchatPacket, fromPeerID: String): Boolean {
// Always relay if TTL is high enough (indicates important message)
if (packet.ttl >= 4u) {
Log.d(TAG, "High TTL (${ '$' }{packet.ttl}), relaying")
Log.d(TAG, "High TTL (${packet.ttl}), relaying")
return true
}
@@ -112,7 +112,7 @@ class PacketRelayManager(private val myPeerID: String) {
// Small networks always relay to ensure connectivity
if (networkSize <= 3) {
Log.d(TAG, "Small network (${ '$' }networkSize peers), relaying")
Log.d(TAG, "Small network (${networkSize} peers), relaying")
return true
}
@@ -126,52 +126,16 @@ class PacketRelayManager(private val myPeerID: String) {
}
val shouldRelay = Random.nextDouble() < relayProb
Log.d(TAG, "Network size: ${'$'}networkSize, Relay probability: ${'$'}relayProb, Decision: ${'$'}shouldRelay")
Log.d(TAG, "Network size: ${networkSize}, Relay probability: ${relayProb}, Decision: ${shouldRelay}")
return shouldRelay
}
/**
* Relay message with adaptive probability and timing (same as iOS)
* Moved from MessageHandler.kt
*/
suspend fun relayMessage(routed: RoutedPacket) {
val packet = routed.packet
if (packet.ttl == 0u.toUByte()) {
Log.d(TAG, "TTL expired, not relaying message")
return
}
val relayPacket = packet.copy(ttl = (packet.ttl - 1u).toUByte())
// Check network size and apply adaptive relay probability
val networkSize = delegate?.getNetworkSize() ?: 1
val relayProb = when {
networkSize <= 10 -> 1.0
networkSize <= 30 -> 0.85
networkSize <= 50 -> 0.7
networkSize <= 100 -> 0.55
else -> 0.4
}
val shouldRelay = relayPacket.ttl >= 4u || networkSize <= 3 || Random.nextDouble() < relayProb
if (shouldRelay) {
val delay = Random.nextLong(50, 500) // Random delay like iOS
Log.d(TAG, "Relaying message after ${'$'}delay ms delay")
delay(delay)
relayPacket(routed.copy(packet = relayPacket))
} else {
Log.d(TAG, "Relay decision: NOT relaying message (network size: ${'$'}networkSize, prob: ${'$'}relayProb)")
}
}
/**
* Actually broadcast the packet for relay
*/
private fun relayPacket(routed: RoutedPacket) {
Log.d(TAG, "🔄 Relaying packet type ${'$'}{routed.packet.type} with TTL ${'$'}{routed.packet.ttl}")
Log.d(TAG, "🔄 Relaying packet type ${routed.packet.type} with TTL ${routed.packet.ttl}")
delegate?.broadcastPacket(routed)
}
@@ -181,9 +145,9 @@ class PacketRelayManager(private val myPeerID: String) {
fun getDebugInfo(): String {
return buildString {
appendLine("=== Packet Relay Manager Debug Info ===")
appendLine("Relay Scope Active: ${'$'}{relayScope.isActive}")
appendLine("My Peer ID: ${'$'}myPeerID")
appendLine("Network Size: ${'$'}{delegate?.getNetworkSize() ?: \"unknown\"}")
appendLine("Relay Scope Active: ${relayScope.isActive}")
appendLine("My Peer ID: ${myPeerID}")
appendLine("Network Size: ${delegate?.getNetworkSize() ?: "unknown"}")
}
}