From 5809a428d68f6ebc4977057de340285cd95b1fa6 Mon Sep 17 00:00:00 2001 From: ThomasSession Date: Tue, 23 Jul 2024 15:34:46 +1000 Subject: [PATCH] Clearing the snode and onion request dbs on launch --- .../thoughtcrime/securesms/ApplicationContext.java | 11 +++++++++++ .../securesms/database/LokiAPIDatabase.kt | 5 +++++ .../org/session/libsession/snode/OnionRequestAPI.kt | 2 ++ .../java/org/session/libsession/snode/SnodeAPI.kt | 2 ++ .../libsession/utilities/TextSecurePreferences.kt | 12 ++++++++++++ .../libsignal/database/LokiAPIDatabaseProtocol.kt | 1 + 6 files changed, 33 insertions(+) diff --git a/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java b/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java index 03b56d6b61..975d12c8e4 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java +++ b/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java @@ -214,6 +214,17 @@ public class ApplicationContext extends Application implements DefaultLifecycleO DatabaseModule.init(this); MessagingModuleConfiguration.configure(this); super.onCreate(); + + // we need to clear the snode and onionrequest databases once on first launch + // in order to apply a patch that adds a version number to the Snode objects. + if(!TextSecurePreferences.hasAppliedPatchSnodeVersion(this)) { + ThreadUtils.queue(() -> { + lokiAPIDatabase.clearSnodePool(); + lokiAPIDatabase.clearOnionRequestPaths(); + TextSecurePreferences.setHasAppliedPatchSnodeVersion(this, true); + }); + } + messagingModuleConfiguration = new MessagingModuleConfiguration( this, storage, diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/LokiAPIDatabase.kt b/app/src/main/java/org/thoughtcrime/securesms/database/LokiAPIDatabase.kt index 0d390d74ed..68e344b8fe 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/LokiAPIDatabase.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/LokiAPIDatabase.kt @@ -255,6 +255,11 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database( return result } + override fun clearSnodePool() { + val database = databaseHelper.writableDatabase + database.delete(snodePoolTable, null, null) + } + override fun clearOnionRequestPaths() { val database = databaseHelper.writableDatabase fun delete(indexPath: String) { diff --git a/libsession/src/main/java/org/session/libsession/snode/OnionRequestAPI.kt b/libsession/src/main/java/org/session/libsession/snode/OnionRequestAPI.kt index b9fcdab50c..23f561600e 100644 --- a/libsession/src/main/java/org/session/libsession/snode/OnionRequestAPI.kt +++ b/libsession/src/main/java/org/session/libsession/snode/OnionRequestAPI.kt @@ -50,6 +50,8 @@ object OnionRequestAPI { get() { val paths = _paths.get() + Log.d("", "***** Getting ONION from database: ${paths?.count()}") + if (paths != null) { return paths } // Storing this in an atomic variable as it was causing a number of background diff --git a/libsession/src/main/java/org/session/libsession/snode/SnodeAPI.kt b/libsession/src/main/java/org/session/libsession/snode/SnodeAPI.kt index 9ba49e2b19..fd4be97c23 100644 --- a/libsession/src/main/java/org/session/libsession/snode/SnodeAPI.kt +++ b/libsession/src/main/java/org/session/libsession/snode/SnodeAPI.kt @@ -153,6 +153,8 @@ object SnodeAPI { internal fun getRandomSnode(): Promise { val snodePool = this.snodePool + Log.d("", "***** Getting snode pool from database: ${snodePool.count()}") + if (snodePool.count() < minimumSnodePoolCount) { val target = seedNodePool.random() val url = "$target/json_rpc" diff --git a/libsession/src/main/java/org/session/libsession/utilities/TextSecurePreferences.kt b/libsession/src/main/java/org/session/libsession/utilities/TextSecurePreferences.kt index af16d93f5a..2a92689aab 100644 --- a/libsession/src/main/java/org/session/libsession/utilities/TextSecurePreferences.kt +++ b/libsession/src/main/java/org/session/libsession/utilities/TextSecurePreferences.kt @@ -292,6 +292,8 @@ interface TextSecurePreferences { const val ALLOW_MESSAGE_REQUESTS = "libsession.ALLOW_MESSAGE_REQUESTS" + const val PATCH_SNODE_VERSION_2024_07_23 = "libsession.patch_snode_version_2024_07_23" + @JvmStatic fun getLastConfigurationSyncTime(context: Context): Long { return getLongPreference(context, LAST_CONFIGURATION_SYNC_TIME, 0) @@ -1013,6 +1015,16 @@ interface TextSecurePreferences { fun clearAll(context: Context) { getDefaultSharedPreferences(context).edit().clear().commit() } + + @JvmStatic + fun hasAppliedPatchSnodeVersion(context: Context): Boolean { + return getBooleanPreference(context, PATCH_SNODE_VERSION_2024_07_23, false) + } + + @JvmStatic + fun setHasAppliedPatchSnodeVersion(context: Context, applied: Boolean) { + setBooleanPreference(context, PATCH_SNODE_VERSION_2024_07_23, applied) + } } } diff --git a/libsignal/src/main/java/org/session/libsignal/database/LokiAPIDatabaseProtocol.kt b/libsignal/src/main/java/org/session/libsignal/database/LokiAPIDatabaseProtocol.kt index 37c00a037d..d7458ff651 100644 --- a/libsignal/src/main/java/org/session/libsignal/database/LokiAPIDatabaseProtocol.kt +++ b/libsignal/src/main/java/org/session/libsignal/database/LokiAPIDatabaseProtocol.kt @@ -10,6 +10,7 @@ interface LokiAPIDatabaseProtocol { fun getSnodePool(): Set fun setSnodePool(newValue: Set) fun getOnionRequestPaths(): List> + fun clearSnodePool() fun clearOnionRequestPaths() fun setOnionRequestPaths(newValue: List>) fun getSwarm(publicKey: String): Set?