mirror of
https://github.com/permissionlesstech/bitchat-android.git
synced 2026-07-25 10:25:19 +00:00
fix: Ensure empty neighbor lists in newer announcements clear mesh edges (#620)
* fix: Ensure empty neighbor lists in newer announcements clear mesh edges - Refactored MeshGraphService.updateFromAnnouncement to prioritize timestamp checks. - Treated null neighbor lists (omitted TLV) as empty lists to allow peer disconnection/isolation updates to propagate. - Added MeshGraphServiceTest to verify timestamp logic and edge eviction. * fix(test): Use TestOnly API to reset singleton instead of reflection - Added MeshGraphService.resetForTesting() - Updated MeshGraphServiceTest to use the new API, avoiding fragile reflection on companion object fields.
This commit is contained in:
@@ -33,23 +33,20 @@ class MeshGraphService private constructor() {
|
||||
// Always update nickname if provided
|
||||
if (originNickname != null) nicknames[originPeerID] = originNickname
|
||||
|
||||
// If no neighbors TLV present, do not modify edges or timestamps
|
||||
if (neighborsOrNull == null) {
|
||||
publishSnapshot()
|
||||
return
|
||||
}
|
||||
|
||||
// Newer-only replacement per origin (based on TLV-bearing announcements only)
|
||||
// 1. Check timestamp first to ensure this is the latest word from the peer
|
||||
val prevTs = lastUpdate[originPeerID]
|
||||
if (prevTs != null && prevTs >= timestamp) {
|
||||
// Older or equal TLV-bearing update: ignore
|
||||
// Older or equal update: ignore
|
||||
return
|
||||
}
|
||||
lastUpdate[originPeerID] = timestamp
|
||||
|
||||
// Update what originPeerID announces
|
||||
// 2. Latest announcement determines state.
|
||||
// If neighborsOrNull is null (TLV omitted), it means the peer is not reporting any neighbors (empty list).
|
||||
val neighbors = neighborsOrNull ?: emptyList()
|
||||
|
||||
// Filter out self-loops just in case
|
||||
val newSet = neighborsOrNull.distinct().take(10).filter { it != originPeerID }.toSet()
|
||||
val newSet = neighbors.distinct().take(10).filter { it != originPeerID }.toSet()
|
||||
announcements[originPeerID] = newSet
|
||||
|
||||
publishSnapshot()
|
||||
@@ -119,5 +116,12 @@ class MeshGraphService private constructor() {
|
||||
fun getInstance(): MeshGraphService = INSTANCE ?: synchronized(this) {
|
||||
INSTANCE ?: MeshGraphService().also { INSTANCE = it }
|
||||
}
|
||||
|
||||
@org.jetbrains.annotations.TestOnly
|
||||
fun resetForTesting() {
|
||||
synchronized(this) {
|
||||
INSTANCE = null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user