@@ -4,7 +4,10 @@ import kotlinx.serialization.SerialName
44import kotlinx.serialization.Serializable
55import kotlinx.serialization.encodeToString
66import kotlinx.serialization.json.Json
7+ import kotlinx.serialization.json.JsonElement
8+ import kotlinx.serialization.json.jsonArray
79import kotlinx.serialization.json.jsonObject
10+ import kotlinx.serialization.json.jsonPrimitive
811import org.spacesprotocol.libveritas.*
912import java.io.InputStreamReader
1013import java.net.HttpURLConnection
@@ -60,15 +63,23 @@ private class AnchorPool {
6063 var observed: String = " " // raw entries JSON array string
6164
6265 fun merged (): String {
63- val parts = mutableListOf<String >()
66+ val all = mutableListOf<JsonElement >()
6467 for (src in listOf (trusted, semiTrusted, observed)) {
65- if (src.isNotEmpty()) {
66- // Strip outer brackets and add contents
67- val inner = src.trim().removePrefix(" [" ).removeSuffix(" ]" ).trim()
68- if (inner.isNotEmpty()) parts.add(inner)
69- }
68+ if (src.isEmpty()) continue
69+ try {
70+ all.addAll(json.parseToJsonElement(src).jsonArray)
71+ } catch (_: Exception ) {}
72+ }
73+ val seen = mutableSetOf<Long >()
74+ val deduped = mutableListOf<Pair <Long , JsonElement >>()
75+ for (e in all) {
76+ val h = (e as ? kotlinx.serialization.json.JsonObject )
77+ ?.get(" block" )?.jsonObject
78+ ?.get(" height" )?.jsonPrimitive?.content?.toLongOrNull() ? : 0L
79+ if (seen.add(h)) deduped.add(h to e)
7080 }
71- return " [${parts.joinToString(" ," )} ]"
81+ deduped.sortByDescending { it.first }
82+ return " [${deduped.joinToString(" ," ) { it.second.toString() }} ]"
7283 }
7384}
7485
@@ -126,7 +137,7 @@ class Fabric(
126137 fun badge (zone : Zone ): VerificationBadge =
127138 badgeFor(zone.sovereignty, zone.anchorHash)
128139
129- fun badgeFor (sovereignty : String , anchorHash : String ): VerificationBadge {
140+ fun badgeFor (sovereignty : String , anchorHash : ByteArray ): VerificationBadge {
130141 val hasAny = synchronized(lock) { trusted != null || observed != null || semiTrusted != null }
131142 if (! hasAny) return VerificationBadge .Unverified
132143
@@ -671,22 +682,19 @@ class Fabric(
671682
672683 // -- Private trust helpers --
673684
674- private fun isRootTrusted (anchorHash : String ): Boolean {
685+ private fun isRootTrusted (anchorHash : ByteArray ): Boolean {
675686 val ts = trusted ? : return false
676- val rootBytes = anchorHash.hexToByteArray()
677- return ts.roots.any { it.contentEquals(rootBytes) }
687+ return ts.roots.any { it.contentEquals(anchorHash) }
678688 }
679689
680- private fun isRootObserved (anchorHash : String ): Boolean {
690+ private fun isRootObserved (anchorHash : ByteArray ): Boolean {
681691 val ts = observed ? : return false
682- val rootBytes = anchorHash.hexToByteArray()
683- return ts.roots.any { it.contentEquals(rootBytes) }
692+ return ts.roots.any { it.contentEquals(anchorHash) }
684693 }
685694
686- private fun isRootSemiTrusted (anchorHash : String ): Boolean {
695+ private fun isRootSemiTrusted (anchorHash : ByteArray ): Boolean {
687696 val ts = semiTrusted ? : return false
688- val rootBytes = anchorHash.hexToByteArray()
689- return ts.roots.any { it.contentEquals(rootBytes) }
697+ return ts.roots.any { it.contentEquals(anchorHash) }
690698 }
691699}
692700
0 commit comments