Skip to content

Commit d6c5ab2

Browse files
Merge pull request #1593 from bemusementpark/more-snodes
Optimise SnodeAPI and Add Snode.Version and tests
2 parents c6c1aa6 + 8a9faa1 commit d6c5ab2

File tree

8 files changed

+502
-686
lines changed

8 files changed

+502
-686
lines changed

app/src/main/java/org/thoughtcrime/securesms/database/LokiAPIDatabase.kt

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -166,24 +166,14 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
166166

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

169-
const val EMPTY_VERSION = "0.0.0"
170-
171169
// endregion
172170
}
173171

174172
override fun getSnodePool(): Set<Snode> {
175173
val database = databaseHelper.readableDatabase
176174
return database.get(snodePoolTable, "${Companion.dummyKey} = ?", wrap("dummy_key")) { cursor ->
177175
val snodePoolAsString = cursor.getString(cursor.getColumnIndexOrThrow(snodePool))
178-
snodePoolAsString.split(", ").mapNotNull { snodeAsString ->
179-
val components = snodeAsString.split("-")
180-
val address = components[0]
181-
val port = components.getOrNull(1)?.toIntOrNull() ?: return@mapNotNull null
182-
val ed25519Key = components.getOrNull(2) ?: return@mapNotNull null
183-
val x25519Key = components.getOrNull(3) ?: return@mapNotNull null
184-
val version = components.getOrNull(4) ?: EMPTY_VERSION
185-
Snode(address, port, Snode.KeySet(ed25519Key, x25519Key), version)
186-
}
176+
snodePoolAsString.split(", ").mapNotNull(::Snode)
187177
}?.toSet() ?: setOf()
188178
}
189179

@@ -231,18 +221,7 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
231221
val database = databaseHelper.readableDatabase
232222
fun get(indexPath: String): Snode? {
233223
return database.get(onionRequestPathTable, "${Companion.indexPath} = ?", wrap(indexPath)) { cursor ->
234-
val snodeAsString = cursor.getString(cursor.getColumnIndexOrThrow(snode))
235-
val components = snodeAsString.split("-")
236-
val address = components[0]
237-
val port = components.getOrNull(1)?.toIntOrNull()
238-
val ed25519Key = components.getOrNull(2)
239-
val x25519Key = components.getOrNull(3)
240-
val version = components.getOrNull(4) ?: EMPTY_VERSION
241-
if (port != null && ed25519Key != null && x25519Key != null) {
242-
Snode(address, port, Snode.KeySet(ed25519Key, x25519Key), version)
243-
} else {
244-
null
245-
}
224+
Snode(cursor.getString(cursor.getColumnIndexOrThrow(snode)))
246225
}
247226
}
248227
val result = mutableListOf<List<Snode>>()
@@ -276,15 +255,7 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
276255
val database = databaseHelper.readableDatabase
277256
return database.get(swarmTable, "${Companion.swarmPublicKey} = ?", wrap(publicKey)) { cursor ->
278257
val swarmAsString = cursor.getString(cursor.getColumnIndexOrThrow(swarm))
279-
swarmAsString.split(", ").mapNotNull { targetAsString ->
280-
val components = targetAsString.split("-")
281-
val address = components[0]
282-
val port = components.getOrNull(1)?.toIntOrNull() ?: return@mapNotNull null
283-
val ed25519Key = components.getOrNull(2) ?: return@mapNotNull null
284-
val x25519Key = components.getOrNull(3) ?: return@mapNotNull null
285-
val version = components.getOrNull(4) ?: EMPTY_VERSION
286-
Snode(address, port, Snode.KeySet(ed25519Key, x25519Key), version)
287-
}
258+
swarmAsString.split(", ").mapNotNull(::Snode)
288259
}?.toSet()
289260
}
290261

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.session.libsignal.utilities
2+
3+
import org.hamcrest.MatcherAssert.assertThat
4+
import org.hamcrest.core.IsEqual.equalTo
5+
import org.junit.Test
6+
import org.junit.runner.RunWith
7+
import org.junit.runners.Parameterized
8+
9+
@RunWith(Parameterized::class)
10+
class SnodeVersionTest(
11+
private val v1: String,
12+
private val v2: String,
13+
private val expectedEqual: Boolean,
14+
private val expectedLessThan: Boolean
15+
) {
16+
companion object {
17+
@JvmStatic
18+
@Parameterized.Parameters(name = "{index}: testVersion({0},{1}) = (equalTo: {2}, lessThan: {3})")
19+
fun data(): Collection<Array<Any>> = listOf(
20+
arrayOf("1", "1", true, false),
21+
arrayOf("1", "2", false, true),
22+
arrayOf("2", "1", false, false),
23+
arrayOf("1.0", "1", true, false),
24+
arrayOf("1.0", "1.0.0", true, false),
25+
arrayOf("1.0", "1.0.0.0", true, false),
26+
arrayOf("1.0", "1.0.0.0.0.0", true, false),
27+
arrayOf("2.0", "1.2", false, false),
28+
arrayOf("1.0.0.0", "1.0.0.1", false, true),
29+
// Snode.Version only considers the first 4 integers, so these are equal
30+
arrayOf("1.0.0.0", "1.0.0.0.1", true, false),
31+
arrayOf("1.0.0.1", "1.0.0.1", true, false),
32+
// parts can be up to 16 bits, around 65,535
33+
arrayOf("65535.65535.65535.65535", "65535.65535.65535.65535", true, false),
34+
// values higher than this are coerced to 65535 (:
35+
arrayOf("65535.65535.65535.65535", "65535.65535.65535.99999", true, false),
36+
)
37+
}
38+
39+
@Test
40+
fun testVersionEqual() {
41+
val version1 = Snode.Version(v1)
42+
val version2 = Snode.Version(v2)
43+
assertThat(version1 == version2, equalTo(expectedEqual))
44+
}
45+
46+
@Test
47+
fun testVersionOnePartLessThan() {
48+
val version1 = Snode.Version(v1)
49+
val version2 = Snode.Version(v2)
50+
assertThat(version1 < version2, equalTo(expectedLessThan))
51+
}
52+
}

libsession/src/main/java/org/session/libsession/messaging/jobs/ConfigurationSyncJob.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ data class ConfigurationSyncJob(val destination: Destination): Job {
6363
// return a list of batch request objects
6464
val snodeMessage = MessageSender.buildConfigMessageToSnode(destination.destinationPublicKey(), message)
6565
val authenticated = SnodeAPI.buildAuthenticatedStoreBatchInfo(
66-
destination.destinationPublicKey(),
6766
config.configNamespace(),
6867
snodeMessage
6968
) ?: return@map null // this entry will be null otherwise

libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/Poller.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ class Poller(private val configFactory: ConfigFactoryProtocol, debounceTimer: Ti
140140
val messages = rawMessages["messages"] as? List<*>
141141
val processed = if (!messages.isNullOrEmpty()) {
142142
SnodeAPI.updateLastMessageHashValueIfPossible(snode, userPublicKey, messages, namespace)
143-
SnodeAPI.removeDuplicates(userPublicKey, messages, namespace, true).mapNotNull { messageBody ->
144-
val rawMessageAsJSON = messageBody as? Map<*, *> ?: return@mapNotNull null
143+
SnodeAPI.removeDuplicates(userPublicKey, messages, namespace, true).mapNotNull { rawMessageAsJSON ->
145144
val hashValue = rawMessageAsJSON["hash"] as? String ?: return@mapNotNull null
146145
val b64EncodedBody = rawMessageAsJSON["data"] as? String ?: return@mapNotNull null
147146
val timestamp = rawMessageAsJSON["t"] as? Long ?: SnodeAPI.nowWithOffset

0 commit comments

Comments
 (0)