Skip to content

Commit

Permalink
Merge pull request oxen-io#1593 from bemusementpark/more-snodes
Browse files Browse the repository at this point in the history
Optimise SnodeAPI and Add Snode.Version and tests
  • Loading branch information
bemusementpark authored Aug 5, 2024
2 parents c6c1aa6 + 8a9faa1 commit d6c5ab2
Show file tree
Hide file tree
Showing 8 changed files with 502 additions and 686 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,24 +166,14 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(

const val RESET_SEQ_NO = "UPDATE $lastMessageServerIDTable SET $lastMessageServerID = 0;"

const val EMPTY_VERSION = "0.0.0"

// endregion
}

override fun getSnodePool(): Set<Snode> {
val database = databaseHelper.readableDatabase
return database.get(snodePoolTable, "${Companion.dummyKey} = ?", wrap("dummy_key")) { cursor ->
val snodePoolAsString = cursor.getString(cursor.getColumnIndexOrThrow(snodePool))
snodePoolAsString.split(", ").mapNotNull { snodeAsString ->
val components = snodeAsString.split("-")
val address = components[0]
val port = components.getOrNull(1)?.toIntOrNull() ?: return@mapNotNull null
val ed25519Key = components.getOrNull(2) ?: return@mapNotNull null
val x25519Key = components.getOrNull(3) ?: return@mapNotNull null
val version = components.getOrNull(4) ?: EMPTY_VERSION
Snode(address, port, Snode.KeySet(ed25519Key, x25519Key), version)
}
snodePoolAsString.split(", ").mapNotNull(::Snode)
}?.toSet() ?: setOf()
}

Expand Down Expand Up @@ -231,18 +221,7 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
val database = databaseHelper.readableDatabase
fun get(indexPath: String): Snode? {
return database.get(onionRequestPathTable, "${Companion.indexPath} = ?", wrap(indexPath)) { cursor ->
val snodeAsString = cursor.getString(cursor.getColumnIndexOrThrow(snode))
val components = snodeAsString.split("-")
val address = components[0]
val port = components.getOrNull(1)?.toIntOrNull()
val ed25519Key = components.getOrNull(2)
val x25519Key = components.getOrNull(3)
val version = components.getOrNull(4) ?: EMPTY_VERSION
if (port != null && ed25519Key != null && x25519Key != null) {
Snode(address, port, Snode.KeySet(ed25519Key, x25519Key), version)
} else {
null
}
Snode(cursor.getString(cursor.getColumnIndexOrThrow(snode)))
}
}
val result = mutableListOf<List<Snode>>()
Expand Down Expand Up @@ -276,15 +255,7 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
val database = databaseHelper.readableDatabase
return database.get(swarmTable, "${Companion.swarmPublicKey} = ?", wrap(publicKey)) { cursor ->
val swarmAsString = cursor.getString(cursor.getColumnIndexOrThrow(swarm))
swarmAsString.split(", ").mapNotNull { targetAsString ->
val components = targetAsString.split("-")
val address = components[0]
val port = components.getOrNull(1)?.toIntOrNull() ?: return@mapNotNull null
val ed25519Key = components.getOrNull(2) ?: return@mapNotNull null
val x25519Key = components.getOrNull(3) ?: return@mapNotNull null
val version = components.getOrNull(4) ?: EMPTY_VERSION
Snode(address, port, Snode.KeySet(ed25519Key, x25519Key), version)
}
swarmAsString.split(", ").mapNotNull(::Snode)
}?.toSet()
}

Expand Down
52 changes: 52 additions & 0 deletions app/src/test/java/org/session/libsignal/utilities/SnodeTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.session.libsignal.utilities

import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.core.IsEqual.equalTo
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized

@RunWith(Parameterized::class)
class SnodeVersionTest(
private val v1: String,
private val v2: String,
private val expectedEqual: Boolean,
private val expectedLessThan: Boolean
) {
companion object {
@JvmStatic
@Parameterized.Parameters(name = "{index}: testVersion({0},{1}) = (equalTo: {2}, lessThan: {3})")
fun data(): Collection<Array<Any>> = listOf(
arrayOf("1", "1", true, false),
arrayOf("1", "2", false, true),
arrayOf("2", "1", false, false),
arrayOf("1.0", "1", true, false),
arrayOf("1.0", "1.0.0", true, false),
arrayOf("1.0", "1.0.0.0", true, false),
arrayOf("1.0", "1.0.0.0.0.0", true, false),
arrayOf("2.0", "1.2", false, false),
arrayOf("1.0.0.0", "1.0.0.1", false, true),
// Snode.Version only considers the first 4 integers, so these are equal
arrayOf("1.0.0.0", "1.0.0.0.1", true, false),
arrayOf("1.0.0.1", "1.0.0.1", true, false),
// parts can be up to 16 bits, around 65,535
arrayOf("65535.65535.65535.65535", "65535.65535.65535.65535", true, false),
// values higher than this are coerced to 65535 (:
arrayOf("65535.65535.65535.65535", "65535.65535.65535.99999", true, false),
)
}

@Test
fun testVersionEqual() {
val version1 = Snode.Version(v1)
val version2 = Snode.Version(v2)
assertThat(version1 == version2, equalTo(expectedEqual))
}

@Test
fun testVersionOnePartLessThan() {
val version1 = Snode.Version(v1)
val version2 = Snode.Version(v2)
assertThat(version1 < version2, equalTo(expectedLessThan))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ data class ConfigurationSyncJob(val destination: Destination): Job {
// return a list of batch request objects
val snodeMessage = MessageSender.buildConfigMessageToSnode(destination.destinationPublicKey(), message)
val authenticated = SnodeAPI.buildAuthenticatedStoreBatchInfo(
destination.destinationPublicKey(),
config.configNamespace(),
snodeMessage
) ?: return@map null // this entry will be null otherwise
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ class Poller(private val configFactory: ConfigFactoryProtocol, debounceTimer: Ti
val messages = rawMessages["messages"] as? List<*>
val processed = if (!messages.isNullOrEmpty()) {
SnodeAPI.updateLastMessageHashValueIfPossible(snode, userPublicKey, messages, namespace)
SnodeAPI.removeDuplicates(userPublicKey, messages, namespace, true).mapNotNull { messageBody ->
val rawMessageAsJSON = messageBody as? Map<*, *> ?: return@mapNotNull null
SnodeAPI.removeDuplicates(userPublicKey, messages, namespace, true).mapNotNull { rawMessageAsJSON ->
val hashValue = rawMessageAsJSON["hash"] as? String ?: return@mapNotNull null
val b64EncodedBody = rawMessageAsJSON["data"] as? String ?: return@mapNotNull null
val timestamp = rawMessageAsJSON["t"] as? Long ?: SnodeAPI.nowWithOffset
Expand Down
Loading

0 comments on commit d6c5ab2

Please sign in to comment.