From 7ed0909169f605fdb15e2d895a9a55ff3670f400 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Mon, 2 Mar 2026 17:09:52 -0500 Subject: [PATCH] Blocks the size of RelayAuthStatus arrays from growing forever with auth messages --- .../AccountFollowsLoaderSubAssembler.kt | 2 -- .../relay/client/auth/RelayAuthStatus.kt | 24 +++++++++---------- .../relay/client/auth/RelayAuthenticator.kt | 2 +- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/reqCommand/account/follows/AccountFollowsLoaderSubAssembler.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/reqCommand/account/follows/AccountFollowsLoaderSubAssembler.kt index 9b6ebb953..eb23bfe13 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/reqCommand/account/follows/AccountFollowsLoaderSubAssembler.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/reqCommand/account/follows/AccountFollowsLoaderSubAssembler.kt @@ -152,8 +152,6 @@ class AccountFollowsLoaderSubAssembler( if (users.isEmpty()) return null - println("AccountFollowNeeds ${users.size}") - val connectedRelays = client.connectedRelaysFlow().value val perRelay = pickRelaysToLoadUsers(users, accounts, connectedRelays, failureTracker.cannotConnectRelays, hasTried) diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/auth/RelayAuthStatus.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/auth/RelayAuthStatus.kt index d868297c2..ac84dfd84 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/auth/RelayAuthStatus.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/auth/RelayAuthStatus.kt @@ -20,16 +20,17 @@ */ package com.vitorpamplona.quartz.nip01Core.relay.client.auth +import androidx.collection.LruCache import com.vitorpamplona.quartz.nip01Core.core.HexKey import com.vitorpamplona.quartz.nip42RelayAuth.RelayAuthEvent class RelayAuthStatus { // Keeps track of auth responses to update the relay with all filters // after the authentication happen - private val authResponseWatcher: MutableMap = mutableMapOf() + private val authResponseWatcher: LruCache = LruCache(10) // Avoids sending multiple replies for each auth. - private val uniqueAuthChallengesSent: MutableSet = mutableSetOf() + private val uniqueAuthChallengesSent: LruCache = LruCache(10) enum class AuthEventReceiptStatus { AUTHENTICATING, @@ -43,15 +44,14 @@ class RelayAuthStatus { ) fun saveAuthSubmission(authEvent: RelayAuthEvent): Boolean { - val challenge = authEvent.challenge() - if (challenge == null) return false + val challenge = authEvent.challenge() ?: return false val challengePair = ChallengePair(authEvent.pubKey, challenge) // only send replies to new challenges to avoid infinite loop: - return if (challengePair !in uniqueAuthChallengesSent) { - authResponseWatcher[authEvent.id] = AuthEventReceiptStatus.AUTHENTICATING - uniqueAuthChallengesSent.add(challengePair) + return if (uniqueAuthChallengesSent[challengePair] == null) { + authResponseWatcher.put(authEvent.id, AuthEventReceiptStatus.AUTHENTICATING) + uniqueAuthChallengesSent.put(challengePair, challengePair) true } else { false @@ -61,10 +61,9 @@ class RelayAuthStatus { fun checkAuthResults( eventId: HexKey, success: Boolean, - ): Boolean = - if (authResponseWatcher.containsKey(eventId)) { - val wasAlreadyAuthenticated = authResponseWatcher[eventId] - + ): Boolean { + val wasAlreadyAuthenticated = authResponseWatcher[eventId] + return if (wasAlreadyAuthenticated != null) { if (success) { authResponseWatcher.put(eventId, AuthEventReceiptStatus.AUTHENTICATED) } else { @@ -75,6 +74,7 @@ class RelayAuthStatus { } else { false } + } - fun hasFinishedAllAuths() = authResponseWatcher.all { it.value != AuthEventReceiptStatus.AUTHENTICATING } + fun hasFinishedAllAuths() = authResponseWatcher.snapshot().all { it.value != AuthEventReceiptStatus.AUTHENTICATING } } diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/auth/RelayAuthenticator.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/auth/RelayAuthenticator.kt index dfb28ffe4..226f0ce8d 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/auth/RelayAuthenticator.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/auth/RelayAuthenticator.kt @@ -66,7 +66,7 @@ class RelayAuthenticator( } override fun onConnecting(relay: IRelayClient) { - authStatus.put(relay.url, RelayAuthStatus()) + authStatus[relay.url] = RelayAuthStatus() } override fun onDisconnected(relay: IRelayClient) {