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:
callebtc
2026-01-15 23:01:56 +07:00
committed by GitHub
parent 39e43fa923
commit db26b673ee
2 changed files with 140 additions and 10 deletions
@@ -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
}
}
}
}