on master help sign in
Simplify code and improve type safety: use ServerName for GossipEvent.server, generalize mergeByKey, remove redundant normalization in isBadServerName, remove dead containsColon helper
commitc0ca8aa15f68d35bfec4ca60bd4aa531bb27d34b authoriko <mail@iko.soy> dateFri 10 Apr 2026, 15:16 (6 months ago) parent54d081a
1 file changed+13−20
server.knot+13−20
server.knot+13−20
@@ -54,7 +54,7 @@ type PollEvent = {id: MessageIdHex, eventType: PollEventType, payload: {encrypte
5454
5555type GossipEventType = Text where \s -> s == "online" || s == "offline"
5656
57−type GossipEvent = {eventType: GossipEventType, pubkey: PubkeyHex, server: Text, timestamp: Timestamp, ttl: Int<S>}
57+type GossipEvent = {eventType: GossipEventType, pubkey: PubkeyHex, server: ServerName, timestamp: Timestamp, ttl: Int<S>}
5858
5959type ValidGossipEntry = {eventType: GossipEventType, pubkey: PubkeyHex, server: ServerName, expiresAt: Timestamp, timestamp: Timestamp, ttl: Int<S>}
6060
@@ -64,8 +64,8 @@ type PubkeyHex = Text where \s -> isValidHex s && not (tooLong s maxPubkeyLen) &
6464type BlobHex = Text where \s -> isValidHex s && not (tooLong s maxBlobLen) && s == normalizeHex s
6565type SignatureHex = Text where \s -> isValidHex s && not (tooLong s maxSignatureLen) && s == normalizeHex s
6666type MessageIdHex = Text where \s -> isValidHex s && not (tooLong s maxMessageIdLen) && s == normalizeHex s
67−type ServerName = Text where \s -> not (tooLong s maxServerNameLen) && not (isBadServerName s) && s == normalizeServerName s
68−type OptionalServerName = Text where \s -> s == "" || (not (tooLong s maxServerNameLen) && not (isBadServerName s) && s == normalizeServerName s)
67+type ServerName = Text where \s -> not (tooLong s maxServerNameLen) && s == normalizeServerName s && not (isBadServerName s)
68+type OptionalServerName = Text where \s -> s == "" || (not (tooLong s maxServerNameLen) && s == normalizeServerName s && not (isBadServerName s))
6969
7070type Timestamp = Int<Ms>
7171
@@ -224,8 +224,6 @@ isCgnatRange = \s ->
224224 then True {}
225225 else False {}
226226
227−containsColon = \s -> contains ":" s
228−
229227containsMultipleColons = \s -> count (do c <- chars s; where c == ":"; yield c) > 1
230228
231229hasOctalOctet = \s ->
@@ -298,8 +296,7 @@ isHighOctetPrefix = \sn ->
298296matchesHostname = \sn name nameLen ->
299297 take nameLen sn == name && (drop nameLen sn == "" || take 1 (drop nameLen sn) == ":")
300298
301−isBadServerName = \s ->
302− let sn = toLower (stripTrailingDot s) in
299+isBadServerName = \sn ->
303300 sn == ""
304301 || take 1 sn == "."
305302 || take 1 sn == ":"
@@ -449,15 +446,15 @@ collectPeerServers = \items getServer ->
449446
450447samePubkeyServer = \a b -> a.pubkey == b.pubkey && a.server == b.server
451448
452−mergeByKey = \sameKey initial items ->
449+mergeByKey = \sameKey getFreshness initial items ->
453450 fold (\acc e ->
454− if any acc (\a -> sameKey a e && a.expiresAt >= e.expiresAt)
451+ if any acc (\a -> sameKey a e && getFreshness a >= getFreshness e)
455452 then acc
456453 else union
457454 (do a <- acc; where not (sameKey a e); yield a)
458455 [e]) initial items
459456
460−mergeByPubkeyServer = mergeByKey samePubkeyServer
457+mergeByPubkeyServer = mergeByKey samePubkeyServer (\x -> x.expiresAt)
461458
462459removeOffline = \presenceSet offlineOnlyKeys trustedOffline t ->
463460 diff presenceSet (do
@@ -476,13 +473,11 @@ countMessagesFor = \messages toKey t ->
476473validGossipEvents : [GossipEvent] -> Timestamp -> [ValidGossipEntry]
477474validGossipEvents = \events t -> do
478475 e <- events
479− where not (tooLong e.server maxServerNameLen) && not (isBadServerName e.server)
480− let server = normalizeServerName e.server
481− where server != serverHostN && e.timestamp <= t + clockSkewTolerance
476+ where e.server != serverHostN && e.timestamp <= t + clockSkewTolerance
482477 let ttl = clampTtl e.ttl
483478 let expiresAt = e.timestamp + ttl * 1000
484479 where expiresAt > t
485− yield {eventType: e.eventType, pubkey: e.pubkey, server: server, expiresAt: expiresAt, timestamp: e.timestamp, ttl: ttl}
480+ yield {eventType: e.eventType, pubkey: e.pubkey, server: e.server, expiresAt: expiresAt, timestamp: e.timestamp, ttl: ttl}
486481
487482sendToEach : [ServerName] -> (ServerName -> (a {})) -> a {}
488483sendToEach = \servers action ->
@@ -556,12 +551,10 @@ sendBatchedGossip = \peerServers allEvents hops ->
556551
557552broadcastOfflineGossip = \t peerServers expiredLocal ->
558553 when (federationEnabled && count peerServers > 0 && count expiredLocal > 0) (do
559− let gossipEvents = do
560− e <- mergeByKey (\a b -> a.pubkey == b.pubkey) [] (do
561− p <- expiredLocal
562− let ttl = clamp offlineGossipMinTtl maxPresenceTtl ((p.expiresAt - p.timestamp) / 1000)
563− yield {pubkey: p.pubkey, ttl: ttl, expiresAt: p.expiresAt})
564− yield {eventType: "offline", pubkey: e.pubkey, server: serverHostN, timestamp: e.expiresAt, ttl: e.ttl}
554+ let gossipEvents = mergeByKey (\a b -> a.pubkey == b.pubkey) (\x -> x.timestamp) [] (do
555+ p <- expiredLocal
556+ let ttl = clamp offlineGossipMinTtl maxPresenceTtl ((p.expiresAt - p.timestamp) / 1000)
557+ yield {eventType: "offline", pubkey: p.pubkey, server: serverHostN, timestamp: p.expiresAt, ttl: ttl})
565558 sendBatchedGossip peerServers gossipEvents 2)
566559
567560forkBroadcastOffline = \t result ->