use clock time instead of system

This commit is contained in:
Valere 2022-05-11 10:58:13 +02:00
parent 546d6fe56d
commit 992f477ab1
6 changed files with 30 additions and 21 deletions

View File

@ -16,6 +16,8 @@
package org.matrix.android.sdk.api.session.crypto.model package org.matrix.android.sdk.api.session.crypto.model
import org.matrix.android.sdk.internal.util.time.Clock
/** /**
* IncomingRoomKeyRequest class defines the incoming room keys request. * IncomingRoomKeyRequest class defines the incoming room keys request.
*/ */
@ -71,13 +73,13 @@ data class IncomingRoomKeyRequest(
} }
} }
fun fromRestRequest(senderId: String, request: RoomKeyShareRequest): IncomingRoomKeyRequest? { fun fromRestRequest(senderId: String, request: RoomKeyShareRequest, clock: Clock): IncomingRoomKeyRequest? {
return IncomingRoomKeyRequest( return IncomingRoomKeyRequest(
userId = senderId, userId = senderId,
deviceId = request.requestingDeviceId, deviceId = request.requestingDeviceId,
requestId = request.requestId, requestId = request.requestId,
requestBody = request.body, requestBody = request.body,
localCreationTimestamp = System.currentTimeMillis() localCreationTimestamp = clock.epochMillis()
) )
} }
} }

View File

@ -317,12 +317,12 @@ internal class DeviceListManager @Inject constructor(
val t0 = clock.epochMillis() val t0 = clock.epochMillis()
try { try {
val result = doKeyDownloadForUsers(downloadUsers) val result = doKeyDownloadForUsers(downloadUsers)
Timber.v("## CRYPTO | downloadKeys() : doKeyDownloadForUsers succeeds after ${System.currentTimeMillis() - t0} ms") Timber.v("## CRYPTO | downloadKeys() : doKeyDownloadForUsers succeeds after ${clock.epochMillis() - t0} ms")
result.also { result.also {
it.addEntriesFromMap(stored) it.addEntriesFromMap(stored)
} }
} catch (failure: Throwable) { } catch (failure: Throwable) {
Timber.w(failure, "## CRYPTO | downloadKeys() : doKeyDownloadForUsers failed after ${System.currentTimeMillis() - t0} ms") Timber.w(failure, "## CRYPTO | downloadKeys() : doKeyDownloadForUsers failed after ${clock.epochMillis() - t0} ms")
if (forceDownload) { if (forceDownload) {
throw failure throw failure
} else { } else {

View File

@ -44,6 +44,7 @@ import org.matrix.android.sdk.internal.crypto.store.IMXCryptoStore
import org.matrix.android.sdk.internal.crypto.tasks.SendToDeviceTask import org.matrix.android.sdk.internal.crypto.tasks.SendToDeviceTask
import org.matrix.android.sdk.internal.session.SessionScope import org.matrix.android.sdk.internal.session.SessionScope
import org.matrix.android.sdk.internal.task.SemaphoreCoroutineSequencer import org.matrix.android.sdk.internal.task.SemaphoreCoroutineSequencer
import org.matrix.android.sdk.internal.util.time.Clock
import timber.log.Timber import timber.log.Timber
import java.util.concurrent.Executors import java.util.concurrent.Executors
import javax.inject.Inject import javax.inject.Inject
@ -60,7 +61,9 @@ internal class IncomingKeyRequestManager @Inject constructor(
private val cryptoConfig: MXCryptoConfig, private val cryptoConfig: MXCryptoConfig,
private val messageEncrypter: MessageEncrypter, private val messageEncrypter: MessageEncrypter,
private val coroutineDispatchers: MatrixCoroutineDispatchers, private val coroutineDispatchers: MatrixCoroutineDispatchers,
private val sendToDeviceTask: SendToDeviceTask) { private val sendToDeviceTask: SendToDeviceTask,
private val clock: Clock,
) {
private val dispatcher = Executors.newSingleThreadExecutor().asCoroutineDispatcher() private val dispatcher = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
private val outgoingRequestScope = CoroutineScope(SupervisorJob() + dispatcher) private val outgoingRequestScope = CoroutineScope(SupervisorJob() + dispatcher)
@ -135,7 +138,7 @@ internal class IncomingKeyRequestManager @Inject constructor(
MegolmRequestAction.Cancel -> { MegolmRequestAction.Cancel -> {
// ignore, we can't cancel as it's not known (probably already processed) // ignore, we can't cancel as it's not known (probably already processed)
// still notify app layer if it was passed up previously // still notify app layer if it was passed up previously
IncomingRoomKeyRequest.fromRestRequest(senderId, request)?.let { iReq -> IncomingRoomKeyRequest.fromRestRequest(senderId, request, clock)?.let { iReq ->
outgoingRequestScope.launch(coroutineDispatchers.computation) { outgoingRequestScope.launch(coroutineDispatchers.computation) {
val listenersCopy = synchronized(gossipingRequestListeners) { val listenersCopy = synchronized(gossipingRequestListeners) {
gossipingRequestListeners.toList() gossipingRequestListeners.toList()
@ -164,7 +167,7 @@ internal class IncomingKeyRequestManager @Inject constructor(
gossipingRequestListeners.toList() gossipingRequestListeners.toList()
} }
listenersCopy.onEach { listenersCopy.onEach {
IncomingRoomKeyRequest.fromRestRequest(senderId, request)?.let { iReq -> IncomingRoomKeyRequest.fromRestRequest(senderId, request, clock)?.let { iReq ->
withContext(coroutineDispatchers.main) { withContext(coroutineDispatchers.main) {
tryOrNull { it.onRequestCancelled(iReq) } tryOrNull { it.onRequestCancelled(iReq) }
} }
@ -287,7 +290,7 @@ internal class IncomingKeyRequestManager @Inject constructor(
sessionId = request.sessionId, sessionId = request.sessionId,
roomId = request.roomId roomId = request.roomId
), ),
localCreationTimestamp = System.currentTimeMillis() localCreationTimestamp = clock.epochMillis()
) )
listenersCopy.onEach { listenersCopy.onEach {
withContext(coroutineDispatchers.main) { withContext(coroutineDispatchers.main) {

View File

@ -26,6 +26,7 @@ import org.matrix.android.sdk.api.session.crypto.model.ImportRoomKeysResult
import org.matrix.android.sdk.api.util.awaitCallback import org.matrix.android.sdk.api.util.awaitCallback
import org.matrix.android.sdk.internal.crypto.keysbackup.DefaultKeysBackupService import org.matrix.android.sdk.internal.crypto.keysbackup.DefaultKeysBackupService
import org.matrix.android.sdk.internal.crypto.store.IMXCryptoStore import org.matrix.android.sdk.internal.crypto.store.IMXCryptoStore
import org.matrix.android.sdk.internal.util.time.Clock
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
@ -40,7 +41,8 @@ private val loggerTag = LoggerTag("OutgoingGossipingRequestManager", LoggerTag.C
internal class PerSessionBackupQueryRateLimiter @Inject constructor( internal class PerSessionBackupQueryRateLimiter @Inject constructor(
private val coroutineDispatchers: MatrixCoroutineDispatchers, private val coroutineDispatchers: MatrixCoroutineDispatchers,
private val keysBackupService: Lazy<DefaultKeysBackupService>, private val keysBackupService: Lazy<DefaultKeysBackupService>,
private val cryptoStore: IMXCryptoStore private val cryptoStore: IMXCryptoStore,
private val clock: Clock,
) { ) {
companion object { companion object {
@ -54,7 +56,7 @@ internal class PerSessionBackupQueryRateLimiter @Inject constructor(
data class LastTry( data class LastTry(
val backupVersion: String, val backupVersion: String,
val timestamp: Long = System.currentTimeMillis() val timestamp: Long
) )
/** /**
@ -66,7 +68,7 @@ internal class PerSessionBackupQueryRateLimiter @Inject constructor(
private var backupVersion: KeysVersionResult? = null private var backupVersion: KeysVersionResult? = null
private var savedKeyBackupKeyInfo: SavedKeyBackupKeyInfo? = null private var savedKeyBackupKeyInfo: SavedKeyBackupKeyInfo? = null
var backupWasCheckedFromServer: Boolean = false var backupWasCheckedFromServer: Boolean = false
val now = System.currentTimeMillis() val now = clock.epochMillis()
fun refreshBackupInfoIfNeeded(force: Boolean = false) { fun refreshBackupInfoIfNeeded(force: Boolean = false) {
if (backupWasCheckedFromServer && !force) return if (backupWasCheckedFromServer && !force) return
@ -124,7 +126,7 @@ internal class PerSessionBackupQueryRateLimiter @Inject constructor(
return true return true
} else { } else {
Timber.tag(loggerTag.value).v("Failed to find key in backup session:$sessionId in $roomId") Timber.tag(loggerTag.value).v("Failed to find key in backup session:$sessionId in $roomId")
lastFailureMap[cacheKey] = LastTry(currentVersion.version) lastFailureMap[cacheKey] = LastTry(currentVersion.version, clock.epochMillis())
return false return false
} }
} }

View File

@ -43,6 +43,7 @@ import org.matrix.android.sdk.internal.crypto.store.IMXCryptoStore
import org.matrix.android.sdk.internal.crypto.tasks.SendToDeviceTask import org.matrix.android.sdk.internal.crypto.tasks.SendToDeviceTask
import org.matrix.android.sdk.internal.crypto.tasks.createUniqueTxnId import org.matrix.android.sdk.internal.crypto.tasks.createUniqueTxnId
import org.matrix.android.sdk.internal.session.SessionScope import org.matrix.android.sdk.internal.session.SessionScope
import org.matrix.android.sdk.internal.util.time.Clock
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
@ -56,7 +57,8 @@ internal class SecretShareManager @Inject constructor(
private val messageEncrypter: MessageEncrypter, private val messageEncrypter: MessageEncrypter,
private val ensureOlmSessionsForDevicesAction: EnsureOlmSessionsForDevicesAction, private val ensureOlmSessionsForDevicesAction: EnsureOlmSessionsForDevicesAction,
private val sendToDeviceTask: SendToDeviceTask, private val sendToDeviceTask: SendToDeviceTask,
private val coroutineDispatchers: MatrixCoroutineDispatchers private val coroutineDispatchers: MatrixCoroutineDispatchers,
private val clock: Clock,
) { ) {
companion object { companion object {
@ -100,7 +102,7 @@ internal class SecretShareManager @Inject constructor(
// For now we just keep an in memory cache // For now we just keep an in memory cache
cryptoCoroutineScope.launch { cryptoCoroutineScope.launch {
verifMutex.withLock { verifMutex.withLock {
recentlyVerifiedDevices[deviceId] = System.currentTimeMillis() recentlyVerifiedDevices[deviceId] = clock.epochMillis()
} }
} }
} }
@ -217,7 +219,7 @@ internal class SecretShareManager @Inject constructor(
recentlyVerifiedDevices[deviceId] recentlyVerifiedDevices[deviceId]
} ?: return false } ?: return false
val age = System.currentTimeMillis() - verifTimestamp val age = clock.epochMillis() - verifTimestamp
return age < SECRET_SHARE_WINDOW_DURATION return age < SECRET_SHARE_WINDOW_DURATION
} }

View File

@ -1108,7 +1108,7 @@ internal class RealmCryptoStore @Inject constructor(
} }
private fun createUnknownTrail() = AuditTrail( private fun createUnknownTrail() = AuditTrail(
System.currentTimeMillis(), clock.epochMillis(),
TrailType.Unknown, TrailType.Unknown,
IncomingKeyRequestInfo( IncomingKeyRequestInfo(
"", "",
@ -1187,7 +1187,7 @@ internal class RealmCryptoStore @Inject constructor(
this.requestedIndex = fromIndex this.requestedIndex = fromIndex
this.requestState = OutgoingRoomKeyRequestState.UNSENT this.requestState = OutgoingRoomKeyRequestState.UNSENT
this.setRequestBody(requestBody) this.setRequestBody(requestBody)
this.creationTimeStamp = System.currentTimeMillis() this.creationTimeStamp = clock.epochMillis()
}.toOutgoingKeyRequest() }.toOutgoingKeyRequest()
} else { } else {
request = existing request = existing
@ -1268,7 +1268,7 @@ internal class RealmCryptoStore @Inject constructor(
fromUser: String, fromUser: String,
fromDevice: String) { fromDevice: String) {
monarchy.writeAsync { realm -> monarchy.writeAsync { realm ->
val now = System.currentTimeMillis() val now = clock.epochMillis()
realm.createObject<AuditTrailEntity>().apply { realm.createObject<AuditTrailEntity>().apply {
this.ageLocalTs = now this.ageLocalTs = now
this.type = TrailType.IncomingKeyRequest.name this.type = TrailType.IncomingKeyRequest.name
@ -1296,7 +1296,7 @@ internal class RealmCryptoStore @Inject constructor(
userId: String, userId: String,
deviceId: String) { deviceId: String) {
monarchy.writeAsync { realm -> monarchy.writeAsync { realm ->
val now = System.currentTimeMillis() val now = clock.epochMillis()
realm.createObject<AuditTrailEntity>().apply { realm.createObject<AuditTrailEntity>().apply {
this.ageLocalTs = now this.ageLocalTs = now
this.type = TrailType.OutgoingKeyWithheld.name this.type = TrailType.OutgoingKeyWithheld.name
@ -1346,7 +1346,7 @@ internal class RealmCryptoStore @Inject constructor(
incoming: Boolean incoming: Boolean
) { ) {
monarchy.writeAsync { realm -> monarchy.writeAsync { realm ->
val now = System.currentTimeMillis() val now = clock.epochMillis()
realm.createObject<AuditTrailEntity>().apply { realm.createObject<AuditTrailEntity>().apply {
this.ageLocalTs = now this.ageLocalTs = now
this.type = if (incoming) TrailType.IncomingKeyForward.name else TrailType.OutgoingKeyForward.name this.type = if (incoming) TrailType.IncomingKeyForward.name else TrailType.OutgoingKeyForward.name
@ -1683,7 +1683,7 @@ internal class RealmCryptoStore @Inject constructor(
// Only keep one month history // Only keep one month history
val prevMonthTs = System.currentTimeMillis() - 4 * 7 * 24 * 60 * 60 * 1_000L val prevMonthTs = clock.epochMillis() - 4 * 7 * 24 * 60 * 60 * 1_000L
realm.where<AuditTrailEntity>() realm.where<AuditTrailEntity>()
.lessThan(AuditTrailEntityFields.AGE_LOCAL_TS, prevMonthTs) .lessThan(AuditTrailEntityFields.AGE_LOCAL_TS, prevMonthTs)
.findAll() .findAll()