Skip to content

Commit

Permalink
Bugfix/dx 1973 fix synchronisation conversation view (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriyVasyk authored Mar 18, 2024
1 parent b7d4bbb commit 934da7e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 80 deletions.
25 changes: 10 additions & 15 deletions Sources/DXProtocol/Session/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,8 @@ public struct Session: Codable {

try identityStore.saveIdentity(theirIdentityKey, for: address)

let lock = SessionLock(address: address)
lock.lock()
defer { lock.unlock() }
try sessionStore.lockSession(for: address)
defer { sessionStore.unlockSession(for: address) }

var session = try sessionStore.loadSession(for: address)
if nil == session {
Expand Down Expand Up @@ -159,9 +158,8 @@ public struct Session: Codable {
throw DXError.untrustedIdentity("Abort processing PreKey Message for untrusted identity")
}

let lock = SessionLock(address: address)
lock.lock()
defer { lock.unlock() }
try sessionStore.lockSession(for: address)
defer { sessionStore.unlockSession(for: address) }

let theirBaseKey = message.senderBaseKey
let messageVersion = Int(message.messageVersion)
Expand Down Expand Up @@ -235,9 +233,8 @@ public struct Session: Codable {
for address: ProtocolAddress,
sessionStore: SessionStorable,
identityStore: IdentityKeyStorable) throws -> MessageContainer {
let lock = SessionLock(address: address)
lock.lock()
defer { lock.unlock() }
try sessionStore.lockSession(for: address)
defer { sessionStore.unlockSession(for: address) }

guard var session = try sessionStore.loadSession(for: address) else {
throw DXError.sessionNotFound("Failed to find session while encrypting message")
Expand Down Expand Up @@ -327,9 +324,8 @@ extension Session {
identityStore: IdentityKeyStorable,
preKeyStore: PreKeyStorable,
signedPreKeyStore: SignedPreKeyStorable) throws -> Data {
let lock = SessionLock(address: address)
lock.lock()
defer { lock.unlock() }
try sessionStore.lockSession(for: address)
defer { sessionStore.unlockSession(for: address) }

var session = try self.processPreKeyMessage(
preKeyMessage,
Expand Down Expand Up @@ -367,9 +363,8 @@ extension Session {
from address: ProtocolAddress,
sessionStore: SessionStorable,
identityStore: IdentityKeyStorable) throws -> Data {
let lock = SessionLock(address: address)
lock.lock()
defer { lock.unlock() }
try sessionStore.lockSession(for: address)
defer { sessionStore.unlockSession(for: address) }

guard var session = try sessionStore.loadSession(for: address) else {
throw DXError.sessionNotFound("Failed to find session while decrypting message")
Expand Down
31 changes: 0 additions & 31 deletions Sources/DXProtocol/Session/SessionLock.swift

This file was deleted.

34 changes: 0 additions & 34 deletions Sources/DXProtocol/Session/SessionLockStorage.swift

This file was deleted.

9 changes: 9 additions & 0 deletions Sources/DXProtocol/Store/SessionStorable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ import Foundation
/// `SessionStorable` protocol declares a set of methods allowing to store
/// the sessions according to DX Protocol
public protocol SessionStorable: AnyObject {
/// Attempts to acquire a lock on session, blocking an execution until the lock can be acquired
/// - Parameter address: The address of the remote client the session is associated with
/// - Throws: Throws if the operation failed to be performed
func lockSession(for address: ProtocolAddress) throws

/// Relinquishes a previously acquired lock on session
/// - Parameter address: The address of the remote client the session is associated with
func unlockSession(for address: ProtocolAddress)

/// Returns a session for a given user's address.
/// - Parameter address: The address of the remote client
/// - Returns: The session, or nil if session does not exist
Expand Down
28 changes: 28 additions & 0 deletions Tests/DXProtocolTests/Mocks/InMemoryTestKeysStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ final class InMemoryTestKeysStore: IdentityKeyStorable, PreKeyStorable, SignedPr
private var prekeyMap: [UUID: OneTimePreKeyPair] = [:]
private var signedPrekeyMap: [UUID: SignedPreKeyPair] = [:]
private var sessionMap: [ProtocolAddress: Session] = [:]

private let queue = DispatchQueue(label: "TestKeysStoreQueue")
private var storage: [ProtocolAddress: NSRecursiveLock] = [:]

private var isIdentityTrusted: Bool

Expand Down Expand Up @@ -128,6 +131,16 @@ final class InMemoryTestKeysStore: IdentityKeyStorable, PreKeyStorable, SignedPr
}

// MARK: - SessionStorable

public func lockSession(for address: ProtocolAddress) throws {
let lock = self.lock(for: address)
lock.lock()
}

public func unlockSession(for address: ProtocolAddress) {
let lock = self.lock(for: address)
lock.unlock()
}

public func loadSession(for address: ProtocolAddress) throws -> Session? {
return sessionMap[address]
Expand All @@ -145,4 +158,19 @@ final class InMemoryTestKeysStore: IdentityKeyStorable, PreKeyStorable, SignedPr
public func storeSession(_ record: Session, for address: ProtocolAddress) throws {
sessionMap[address] = record
}

// MARK: - Private

private func lock(for address: ProtocolAddress) -> NSRecursiveLock {
self.queue.sync {
var result: NSRecursiveLock
if let lock = self.storage[address] {
result = lock
} else {
result = NSRecursiveLock()
self.storage[address] = result
}
return result
}
}
}

0 comments on commit 934da7e

Please sign in to comment.