on master help sign in
Simplify code and reduce redundant computation: remove empty-string validations, compute values once, replace fold with mergeByPubkeyServer, consistent filtering
e6337c8iko
server.knot+12−22
@@ -499,16 +499,16 @@ validGossipEvents : [GossipEvent] -> Timestamp -> GossipEventType -> [ValidGossi
499validGossipEvents = \events t eventType -> do
499validGossipEvents = \events t eventType -> do
500 e <- events
500 e <- events
501 where e.eventType == eventType
501 where e.eventType == eventType
502− where not (e.server == "")
503− where not (e.pubkey == "")
504 where not (tooLong e.pubkey maxPubkeyLen)
502 where not (tooLong e.pubkey maxPubkeyLen)
505 where not (tooLong e.server maxServerNameLen)
503 where not (tooLong e.server maxServerNameLen)
506 where not (isBadServerName e.server)
504 where not (isBadServerName e.server)
507 where isValidHex e.pubkey
505 where isValidHex e.pubkey
508− where not (normalizeServerName e.server == serverHostN)
506+ let server = normalizeServerName e.server
507+ where not (server == serverHostN)
509 where e.timestamp <= t + clockSkewTolerance
508 where e.timestamp <= t + clockSkewTolerance
509+ let ttl = clampTtl e.ttl
510− where e.timestamp + (clampTtl e.ttl) * 1000 > t
510+ where e.timestamp + ttl * 1000 > t
511− yield {eventType: eventType, pubkey: normalizeHex e.pubkey, server: normalizeServerName e.server, expiresAt: e.timestamp + (clampTtl e.ttl) * 1000, timestamp: e.timestamp, ttl: clampTtl e.ttl}
511+ yield {eventType: eventType, pubkey: normalizeHex e.pubkey, server: server, expiresAt: e.timestamp + ttl * 1000, timestamp: e.timestamp, ttl: ttl}
512
512
513sendToEach : [Text] -> (Text -> (a {})) -> a {}
513sendToEach : [Text] -> (Text -> (a {})) -> a {}
514sendToEach = \servers action ->
514sendToEach = \servers action ->
@@ -581,7 +581,7 @@ sendBatchedGossip = \peerServers allEvents ->
581broadcastOfflineGossip = \t peerServers expiredLocal -> do
581broadcastOfflineGossip = \t peerServers expiredLocal -> do
582 let expiredEntries = mergeByKey (\a b -> a.pubkey == b.pubkey) [] (do
582 let expiredEntries = mergeByKey (\a b -> a.pubkey == b.pubkey) [] (do
583 p <- expiredLocal
583 p <- expiredLocal
584− yield {pubkey: p.pubkey, ttl: (let rawTtl = (p.expiresAt - p.timestamp) / 1000 in clampTtl (if rawTtl < offlineGossipMinTtl then offlineGossipMinTtl else rawTtl)), expiresAt: p.expiresAt})
584+ yield {pubkey: p.pubkey, ttl: (let rawTtl = (p.expiresAt - p.timestamp) / 1000 in if rawTtl < offlineGossipMinTtl then offlineGossipMinTtl else if rawTtl > maxPresenceTtl then maxPresenceTtl else rawTtl), expiresAt: p.expiresAt})
585 if federationEnabled && count expiredEntries > 0 && count peerServers > 0
585 if federationEnabled && count expiredEntries > 0 && count peerServers > 0
586 then do
586 then do
587 let gossipEvents = do
587 let gossipEvents = do
@@ -725,10 +725,8 @@ serve = \req -> case req of
725 pr <- maybePrune t
725 pr <- maybePrune t
726 challenges <- *challenges
726 challenges <- *challenges
727 let challengesCapped = capPerKey challenges (\c -> c.pubkey == pubkey) maxChallengesPerKey (\c -> c.expiresAt) t
727 let challengesCapped = capPerKey challenges (\c -> c.pubkey == pubkey) maxChallengesPerKey (\c -> c.expiresAt) t
728− let withoutExpired = if count challengesCapped >= maxChallenges
729− then do c <- challengesCapped; where c.expiresAt > t; yield c
728+ let activeChallenges = do c <- challengesCapped; where c.expiresAt > t; yield c
730− else challengesCapped
731− let globalCapped = capGlobal withoutExpired maxChallenges (\c -> c.expiresAt)
729+ let globalCapped = capGlobal activeChallenges maxChallenges (\c -> c.expiresAt)
732 set *challenges = union
730 set *challenges = union
733 globalCapped
731 globalCapped
734 [{pubkey: pubkey, challenge: challenge, expiresAt: t + challengeExpiry}]
732 [{pubkey: pubkey, challenge: challenge, expiresAt: t + challengeExpiry}]
@@ -825,8 +823,7 @@ serve = \req -> case req of
825 let authorized = auth.authorized
823 let authorized = auth.authorized
826 seqCounter <- *seqCounter
824 seqCounter <- *seqCounter
827 let seqTs = nextSeqTs t seqCounter
825 let seqTs = nextSeqTs t seqCounter
828− let recipientMsgCount = countMessagesFor messages to t
826+ let shouldInsert = authorized && countMessagesFor messages to t < maxMessagesPerRecipient
829− let shouldInsert = authorized && recipientMsgCount < maxMessagesPerRecipient
830 set *seqCounter = if shouldInsert then seqTs else seqCounter
827 set *seqCounter = if shouldInsert then seqTs else seqCounter
831 set *messages = if shouldInsert
828 set *messages = if shouldInsert
832 then union messages
829 then union messages
@@ -1003,15 +1000,9 @@ serve = \req -> case req of
1003 yield {pubkey: k.pubkey, server: k.server}
1000 yield {pubkey: k.pubkey, server: k.server}
1004 let afterOffline = removeOffline merged offlineOnlyKeys trustedOffline t
1001 let afterOffline = removeOffline merged offlineOnlyKeys trustedOffline t
1005 let withinCapacity = count afterOffline <= maxPresenceEntries
1002 let withinCapacity = count afterOffline <= maxPresenceEntries
1003+ let updatesOnly = do e <- trustedDeduped; where any presence (\p -> samePubkeyServer p e); yield e
1006 let updatesOnlyMerged = if withinCapacity then afterOffline
1004 let updatesOnlyMerged = if withinCapacity then afterOffline
1007− else fold (\acc e ->
1005+ else mergeByPubkeyServer presence updatesOnly
1008− if any acc (\p -> samePubkeyServer p e && p.expiresAt >= e.expiresAt)
1009− then acc
1010− else if any presence (\p -> samePubkeyServer p e)
1011− then union
1012− (do p <- acc; where not (samePubkeyServer p e); yield p)
1013− [e]
1014− else acc) presence trustedDeduped
1015 let updatesOnlyAfterOffline = if withinCapacity then afterOffline
1006 let updatesOnlyAfterOffline = if withinCapacity then afterOffline
1016 else removeOffline updatesOnlyMerged offlineOnlyKeys trustedOffline t
1007 else removeOffline updatesOnlyMerged offlineOnlyKeys trustedOffline t
1017 let reclaimCapacity = if withinCapacity then 0 else maxPresenceEntries - count updatesOnlyAfterOffline
1008 let reclaimCapacity = if withinCapacity then 0 else maxPresenceEntries - count updatesOnlyAfterOffline
@@ -1028,8 +1019,7 @@ serve = \req -> case req of
1028 let appliedOnlineUpdates = if withinCapacity then appliedOnline
1019 let appliedOnlineUpdates = if withinCapacity then appliedOnline
1029 else union
1020 else union
1030 (do
1021 (do
1031− e <- trustedDeduped
1022+ e <- updatesOnly
1032− where any presence (\p -> samePubkeyServer p e)
1033 where none presence (\p -> samePubkeyServer p e && p.expiresAt >= e.expiresAt)
1023 where none presence (\p -> samePubkeyServer p e && p.expiresAt >= e.expiresAt)
1034 yield {pubkey: e.pubkey, server: e.server})
1024 yield {pubkey: e.pubkey, server: e.server})
1035 (do
1025 (do