-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mls): recover from stale epoch on message sending (#2076)
* feat: parse epoch timestamp in conversation response * feat: persist epoch timestamp * feat: verify if we have lost commits when sending fails * test: add tests for stale epoch handler * fix: don't compare against last processed event instead use time heuristic * fix: verify epoch using CC in WrongEpochHandler * refactor: replace MLSWrongEpochHandler with StaleEpochVerifier since they are identical * fix: fail re-try if verifying epoch fails * Revert "feat: persist epoch timestamp" This reverts commit e968af6. * Revert "feat: parse epoch timestamp in conversation response" This reverts commit f1bf8b4. * chore: remove any remaining trace of epochTimestamp
- Loading branch information
Showing
20 changed files
with
611 additions
and
503 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/message/StaleEpochVerifier.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.kalium.logic.feature.message | ||
|
||
import com.wire.kalium.logger.KaliumLogger | ||
import com.wire.kalium.logic.CoreFailure | ||
import com.wire.kalium.logic.MLSFailure | ||
import com.wire.kalium.logic.data.conversation.Conversation | ||
import com.wire.kalium.logic.data.conversation.ConversationRepository | ||
import com.wire.kalium.logic.data.conversation.MLSConversationRepository | ||
import com.wire.kalium.logic.data.id.ConversationId | ||
import com.wire.kalium.logic.data.message.SystemMessageInserter | ||
import com.wire.kalium.logic.feature.conversation.JoinExistingMLSConversationUseCase | ||
import com.wire.kalium.logic.functional.Either | ||
import com.wire.kalium.logic.functional.flatMap | ||
import com.wire.kalium.logic.functional.map | ||
import com.wire.kalium.logic.kaliumLogger | ||
import com.wire.kalium.util.DateTimeUtil.toIsoDateTimeString | ||
import kotlinx.datetime.Clock | ||
import kotlinx.datetime.Instant | ||
|
||
interface StaleEpochVerifier { | ||
suspend fun verifyEpoch(conversationId: ConversationId, timestamp: Instant? = null): Either<CoreFailure, Unit> | ||
} | ||
|
||
internal class StaleEpochVerifierImpl( | ||
private val systemMessageInserter: SystemMessageInserter, | ||
private val conversationRepository: ConversationRepository, | ||
private val mlsConversationRepository: MLSConversationRepository, | ||
private val joinExistingMLSConversation: JoinExistingMLSConversationUseCase | ||
) : StaleEpochVerifier { | ||
|
||
private val logger by lazy { kaliumLogger.withFeatureId(KaliumLogger.Companion.ApplicationFlow.MESSAGES) } | ||
override suspend fun verifyEpoch(conversationId: ConversationId, timestamp: Instant?): Either<CoreFailure, Unit> { | ||
logger.i("Verifying stale epoch") | ||
return getUpdatedConversationProtocolInfo(conversationId).flatMap { protocol -> | ||
if (protocol is Conversation.ProtocolInfo.MLS) { | ||
Either.Right(protocol) | ||
} else { | ||
Either.Left(MLSFailure.ConversationDoesNotSupportMLS) | ||
} | ||
}.flatMap { protocolInfo -> | ||
mlsConversationRepository.isGroupOutOfSync(protocolInfo.groupId, protocolInfo.epoch) | ||
.map { epochIsStale -> | ||
epochIsStale | ||
} | ||
}.flatMap { hasMissedCommits -> | ||
if (hasMissedCommits) { | ||
logger.w("Epoch stale due to missing commits, re-joining") | ||
joinExistingMLSConversation(conversationId).flatMap { | ||
systemMessageInserter.insertLostCommitSystemMessage( | ||
conversationId, | ||
(timestamp ?: Clock.System.now()).toIsoDateTimeString() | ||
) | ||
} | ||
} else { | ||
logger.i("Epoch stale due to unprocessed events") | ||
Either.Right(Unit) | ||
} | ||
} | ||
} | ||
|
||
private suspend fun getUpdatedConversationProtocolInfo(conversationId: ConversationId): Either<CoreFailure, Conversation.ProtocolInfo> { | ||
return conversationRepository.fetchConversation(conversationId).flatMap { | ||
conversationRepository.getConversationProtocolInfo(conversationId) | ||
} | ||
} | ||
} |
99 changes: 0 additions & 99 deletions
99
...n/kotlin/com/wire/kalium/logic/sync/receiver/conversation/message/MLSWrongEpochHandler.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.