gossip works

This commit is contained in:
callebtc
2025-09-05 16:29:46 +02:00
parent f4b33b5a09
commit 5769e70ea4
5 changed files with 62 additions and 26 deletions
@@ -24,9 +24,10 @@ object GossipTLV {
}
/**
* Scan a TLV-encoded announce payload and extract neighbor peerIDs if present.
* Scan a TLV-encoded announce payload and extract neighbor peerIDs.
* Returns null if the TLV is not present at all; returns an empty list if present with length 0.
*/
fun decodeNeighborsFromAnnouncementPayload(payload: ByteArray): List<String> {
fun decodeNeighborsFromAnnouncementPayload(payload: ByteArray): List<String>? {
val result = mutableListOf<String>()
var offset = 0
while (offset + 2 <= payload.size) {
@@ -45,10 +46,11 @@ object GossipTLV {
result.add(bytesToPeerIdHex(idBytes))
pos += 8
}
break // only one neighbors TLV expected
return result // present (possibly empty)
}
}
return result
// Not present
return null
}
private fun hexStringPeerIdTo8Bytes(hexString: String): ByteArray {
@@ -73,4 +75,3 @@ object GossipTLV {
return sb.toString()
}
}
@@ -28,33 +28,40 @@ class MeshGraphService private constructor() {
* Update graph from a verified announcement.
* Replaces previous neighbors for origin if this is newer (by timestamp).
*/
fun updateFromAnnouncement(originPeerID: String, originNickname: String?, neighbors: List<String>, timestamp: ULong) {
// Newer-only replacement per origin
val prevTs = lastUpdate[originPeerID]
if (prevTs != null && prevTs >= timestamp) return
lastUpdate[originPeerID] = timestamp
fun updateFromAnnouncement(originPeerID: String, originNickname: String?, neighborsOrNull: List<String>?, timestamp: ULong) {
synchronized(this) {
// Update nickname
// 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)
val prevTs = lastUpdate[originPeerID]
if (prevTs != null && prevTs >= timestamp) {
// Older or equal TLV-bearing update: ignore
return
}
lastUpdate[originPeerID] = timestamp
// Remove old symmetric edges contributed by this origin
val prevNeighbors = adjacency[originPeerID]?.toSet().orEmpty()
prevNeighbors.forEach { n ->
adjacency[n]?.remove(originPeerID)
}
// Replace origin's adjacency with new set
val newSet = neighbors.distinct().take(10).filter { it != originPeerID }.toMutableSet()
// Replace origin's adjacency with new set (may be empty)
val newSet = neighborsOrNull.distinct().take(10).filter { it != originPeerID }.toMutableSet()
adjacency[originPeerID] = newSet
// Ensure nodes exist for neighbors
// Ensure undirected edges
newSet.forEach { n ->
adjacency.putIfAbsent(n, mutableSetOf())
// Add symmetric edge for undirected graph visualization
adjacency[n]?.add(originPeerID)
}
// Compute snapshot
publishSnapshot()
}
}
@@ -93,4 +100,3 @@ class MeshGraphService private constructor() {
}
}
}