From b7272bdf66f9afe096c66d1c138f72fc68fa05e9 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Thu, 15 Apr 2021 10:42:47 +1000 Subject: [PATCH] Increase TTL & remove PoW --- .../libsession/messaging/messages/Message.kt | 2 +- .../control/ClosedGroupControlMessage.kt | 4 ++-- .../messages/control/ConfigurationMessage.kt | 1 - .../sending_receiving/MessageSender.kt | 8 ++----- .../org/session/libsession/snode/SnodeAPI.kt | 23 +++++++++++-------- .../session/libsession/snode/SnodeMessage.kt | 10 ++++---- 6 files changed, 23 insertions(+), 25 deletions(-) diff --git a/libsession/src/main/java/org/session/libsession/messaging/messages/Message.kt b/libsession/src/main/java/org/session/libsession/messaging/messages/Message.kt index 6f2a916351..ef60aaa83e 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/messages/Message.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/messages/Message.kt @@ -15,7 +15,7 @@ abstract class Message { var groupPublicKey: String? = null var openGroupServerMessageID: Long? = null - open val ttl: Long = 2 * 24 * 60 * 60 * 1000 + open val ttl: Long = 14 * 24 * 60 * 60 * 1000 open val isSelfSendValid: Boolean = false // validation diff --git a/libsession/src/main/java/org/session/libsession/messaging/messages/control/ClosedGroupControlMessage.kt b/libsession/src/main/java/org/session/libsession/messaging/messages/control/ClosedGroupControlMessage.kt index c02cfc4c19..fa6544fc2a 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/messages/control/ClosedGroupControlMessage.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/messages/control/ClosedGroupControlMessage.kt @@ -19,8 +19,8 @@ class ClosedGroupControlMessage() : ControlMessage() { override val ttl: Long = run { when (kind) { - is Kind.EncryptionKeyPair -> return@run 4 * 24 * 60 * 60 * 1000 - else -> return@run 2 * 24 * 60 * 60 * 1000 + is Kind.EncryptionKeyPair -> 14 * 24 * 60 * 60 * 1000 + else -> 14 * 24 * 60 * 60 * 1000 } } diff --git a/libsession/src/main/java/org/session/libsession/messaging/messages/control/ConfigurationMessage.kt b/libsession/src/main/java/org/session/libsession/messaging/messages/control/ConfigurationMessage.kt index fe06cc6c01..2fbf13588f 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/messages/control/ConfigurationMessage.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/messages/control/ConfigurationMessage.kt @@ -87,7 +87,6 @@ class ConfigurationMessage(var closedGroups: List, var openGroups: } } - override val ttl: Long = 4 * 24 * 60 * 60 * 1000 override val isSelfSendValid: Boolean = true companion object { diff --git a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSender.kt b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSender.kt index 225c38b7da..eac8acb32f 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSender.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSender.kt @@ -31,7 +31,6 @@ import org.session.libsession.messaging.sending_receiving.attachments.Attachment import org.session.libsession.messaging.sending_receiving.linkpreview.LinkPreview as SignalLinkPreview import org.session.libsession.messaging.sending_receiving.quotes.QuoteModel as SignalQuote - object MessageSender { // Error @@ -147,15 +146,12 @@ object MessageSender { is Destination.OpenGroup -> throw Error.PreconditionFailure("Destination should not be open groups!") } val wrappedMessage = MessageWrapper.wrap(kind, message.sentTimestamp!!, senderPublicKey, ciphertext) - // Calculate proof of work + // Send the result if (destination is Destination.Contact && message is VisibleMessage && !isSelfSend) { SnodeConfiguration.shared.broadcaster.broadcast("calculatingPoW", message.sentTimestamp!!) } - val recipient = message.recipient!! val base64EncodedData = Base64.encodeBytes(wrappedMessage) - val nonce = ProofOfWork.calculate(base64EncodedData, recipient, message.sentTimestamp!!, message.ttl.toInt()) ?: throw Error.ProofOfWorkCalculationFailed - // Send the result - val snodeMessage = SnodeMessage(recipient, base64EncodedData, message.ttl, message.sentTimestamp!!, nonce) + val snodeMessage = SnodeMessage(message.recipient!!, base64EncodedData, message.ttl, message.sentTimestamp!!) if (destination is Destination.Contact && message is VisibleMessage && !isSelfSend) { SnodeConfiguration.shared.broadcaster.broadcast("sendingMessage", message.sentTimestamp!!) } 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 9be9db1d29..9f8d417ed3 100644 --- a/libsession/src/main/java/org/session/libsession/snode/SnodeAPI.kt +++ b/libsession/src/main/java/org/session/libsession/snode/SnodeAPI.kt @@ -12,6 +12,7 @@ import org.session.libsignal.service.loki.api.utilities.HTTP import org.session.libsignal.service.loki.database.LokiAPIDatabaseProtocol import org.session.libsignal.service.loki.utilities.Broadcaster import org.session.libsignal.service.loki.utilities.prettifiedDescription +import org.session.libsignal.service.loki.utilities.removing05PrefixIfNeeded import org.session.libsignal.service.loki.utilities.retryIfNeeded import org.session.libsignal.utilities.* import org.session.libsignal.utilities.logging.Log @@ -37,16 +38,18 @@ object SnodeAPI { // use port 4433 if API level can handle network security config and enforce pinned certificates private val seedPort = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) 443 else 4433 - private val seedNodePool: Set = setOf( - "https://storage.seed1.loki.network:$seedPort", - "https://storage.seed3.loki.network:$seedPort", - "https://public.loki.foundation:$seedPort" - ) - internal val snodeFailureThreshold = 4 + private val seedNodePool by lazy { + if (useTestnet) { + setOf( "http://public.loki.foundation:38157" ) + } else { + setOf( "https://storage.seed1.loki.network:$seedPort", "https://storage.seed3.loki.network:$seedPort", "https://public.loki.foundation:$seedPort" ) + } + } + private val snodeFailureThreshold = 4 private val targetSwarmSnodeCount = 2 - private val useOnionRequests = true + internal val useTestnet = false internal var powDifficulty = 1 // Error @@ -164,7 +167,7 @@ object SnodeAPI { cachedSwarmCopy.addAll(cachedSwarm) return task { cachedSwarmCopy } } else { - val parameters = mapOf( "pubKey" to publicKey ) + val parameters = mapOf( "pubKey" to if (useTestnet) publicKey.removing05PrefixIfNeeded() else publicKey ) return getRandomSnode().bind { invoke(Snode.Method.GetSwarm, it, publicKey, parameters) }.map(sharedContext) { @@ -177,7 +180,7 @@ object SnodeAPI { fun getRawMessages(snode: Snode, publicKey: String): RawResponsePromise { val lastHashValue = database.getLastMessageHashValue(snode, publicKey) ?: "" - val parameters = mapOf( "pubKey" to publicKey, "lastHash" to lastHashValue ) + val parameters = mapOf( "pubKey" to if (useTestnet) publicKey.removing05PrefixIfNeeded() else publicKey, "lastHash" to lastHashValue ) return invoke(Snode.Method.GetMessages, snode, publicKey, parameters) } @@ -190,7 +193,7 @@ object SnodeAPI { } fun sendMessage(message: SnodeMessage): Promise, Exception> { - val destination = message.recipient + val destination = if (useTestnet) message.recipient.removing05PrefixIfNeeded() else message.recipient return retryIfNeeded(maxRetryCount) { getTargetSnodes(destination).map { swarm -> swarm.map { snode -> diff --git a/libsession/src/main/java/org/session/libsession/snode/SnodeMessage.kt b/libsession/src/main/java/org/session/libsession/snode/SnodeMessage.kt index 558447c549..0a653e400b 100644 --- a/libsession/src/main/java/org/session/libsession/snode/SnodeMessage.kt +++ b/libsession/src/main/java/org/session/libsession/snode/SnodeMessage.kt @@ -1,5 +1,7 @@ package org.session.libsession.snode +import org.session.libsignal.service.loki.utilities.removing05PrefixIfNeeded + data class SnodeMessage( // The hex encoded public key of the recipient. val recipient: String, @@ -8,16 +10,14 @@ data class SnodeMessage( // The time to live for the message in milliseconds. val ttl: Long, // When the proof of work was calculated. - val timestamp: Long, - // The base 64 encoded proof of work. - val nonce: String + val timestamp: Long ) { internal fun toJSON(): Map { return mutableMapOf( - "pubKey" to recipient, + "pubKey" to if (SnodeAPI.useTestnet) recipient.removing05PrefixIfNeeded() else recipient, "data" to data, "ttl" to ttl.toString(), "timestamp" to timestamp.toString(), - "nonce" to nonce) + "nonce" to "") } }