Optimise SnodeAPI further

This commit is contained in:
bemusementpark 2024-08-03 21:19:31 +09:30
parent c1d40cdbe7
commit 3c8302f7a4
2 changed files with 8 additions and 9 deletions

View File

@ -455,7 +455,7 @@ object SnodeAPI {
val hashes = messageHashes.takeIf { it.size != 1 } ?: (messageHashes + "///////////////////////////////////////////") // TODO remove this when bug is fixed on nodes. val hashes = messageHashes.takeIf { it.size != 1 } ?: (messageHashes + "///////////////////////////////////////////") // TODO remove this when bug is fixed on nodes.
return retryIfNeeded(maxRetryCount) { return retryIfNeeded(maxRetryCount) {
val timestamp = System.currentTimeMillis() + clockOffset val timestamp = System.currentTimeMillis() + clockOffset
val signData = "${Snode.Method.GetExpiries.rawValue}$timestamp${hashes.joinToString(separator = "")}".toByteArray() val signData = sequenceOf(Snode.Method.GetExpiries.rawValue).plus(timestamp.toString()).plus(hashes).toByteArray()
val ed25519PublicKey = userEd25519KeyPair.publicKey.asHexString val ed25519PublicKey = userEd25519KeyPair.publicKey.asHexString
val signature = try { val signature = try {
@ -499,7 +499,7 @@ object SnodeAPI {
val shortenOrExtend = if (extend) "extend" else if (shorten) "shorten" else "" val shortenOrExtend = if (extend) "extend" else if (shorten) "shorten" else ""
val signData = "${Snode.Method.Expire.rawValue}$shortenOrExtend$newExpiry${messageHashes.joinToString(separator = "")}".toByteArray() val signData = sequenceOf(Snode.Method.Expire.rawValue).plus(shortenOrExtend).plus(newExpiry.toString()).plus(messageHashes).toByteArray()
val signature = try { val signature = try {
signAndEncode(signData, userEd25519KeyPair) signAndEncode(signData, userEd25519KeyPair)
@ -633,7 +633,7 @@ object SnodeAPI {
getSingleTargetSnode(userPublicKey).bind { snode -> getSingleTargetSnode(userPublicKey).bind { snode ->
retryIfNeeded(maxRetryCount) { retryIfNeeded(maxRetryCount) {
getNetworkTime(snode).bind { (_, timestamp) -> getNetworkTime(snode).bind { (_, timestamp) ->
val verificationData = (Snode.Method.DeleteAll.rawValue + Namespace.ALL + timestamp.toString()).toByteArray() val verificationData = sequenceOf(Snode.Method.DeleteAll.rawValue, Namespace.ALL, timestamp.toString()).toByteArray()
val deleteMessageParams = buildMap { val deleteMessageParams = buildMap {
this["pubkey"] = userPublicKey this["pubkey"] = userPublicKey
this["pubkey_ed25519"] = userED25519KeyPair.publicKey.asHexString this["pubkey_ed25519"] = userED25519KeyPair.publicKey.asHexString
@ -722,9 +722,8 @@ object SnodeAPI {
if (newFailureCount >= snodeFailureThreshold) { if (newFailureCount >= snodeFailureThreshold) {
Log.d("Loki", "Failure threshold reached for: $snode; dropping it.") Log.d("Loki", "Failure threshold reached for: $snode; dropping it.")
publicKey?.let { dropSnodeFromSwarmIfNeeded(snode, it) } publicKey?.let { dropSnodeFromSwarmIfNeeded(snode, it) }
snodePool -= snode snodePool = (snodePool - snode).also { Log.d("Loki", "Snode pool count: ${it.count()}.") }
Log.d("Loki", "Snode pool count: ${snodePool.count()}.") snodeFailureCount -= snode
snodeFailureCount.remove(snode)
} }
} }
when (statusCode) { when (statusCode) {

View File

@ -52,6 +52,8 @@ class Snode(val address: String, val port: Int, val publicKeySet: KeySet?, val v
fun Version(value: String) = CACHE.getOrElse(value) { fun Version(value: String) = CACHE.getOrElse(value) {
Snode.Version(value) Snode.Version(value)
} }
fun Version(parts: List<Int>) = Version(parts.joinToString("."))
} }
@JvmInline @JvmInline
@ -66,14 +68,12 @@ class Snode(val address: String, val port: Int, val publicKeySet: KeySet?, val v
} }
} }
constructor(parts: List<Int>): this( internal constructor(parts: List<Int>): this(
parts.asSequence() parts.asSequence()
.map { it.toByte().toULong() } .map { it.toByte().toULong() }
.foldToVersionAsULong() .foldToVersionAsULong()
) )
constructor(value: Int): this(value.toULong())
internal constructor(value: String): this( internal constructor(value: String): this(
value.splitToSequence(".") value.splitToSequence(".")
.map { it.toULongOrNull() ?: 0UL } .map { it.toULongOrNull() ?: 0UL }