Skip to content

Commit

Permalink
Re-Enable History Sync (#452)
Browse files Browse the repository at this point in the history
* performance follow ups

* write a test

* re-enable history syncing and add more tests

* bring back sync consent
  • Loading branch information
nplasterer authored Dec 20, 2024
1 parent cfd45dd commit bd7c6b0
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 136 deletions.
6 changes: 1 addition & 5 deletions Sources/XMTPiOS/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public final class Client {
accountAddress: address,
nonce: 0,
legacySignedPrivateKeyProto: nil,
historySyncUrl: nil
historySyncUrl: options.historySyncUrl
)

try await options.preAuthenticateToInboxCallback?()
Expand Down Expand Up @@ -540,10 +540,6 @@ public final class Client {
}
}

public func requestMessageHistorySync() async throws {
try await ffiClient.sendSyncRequest(kind: .messages)
}

public func inboxState(refreshFromNetwork: Bool) async throws -> InboxState
{
return InboxState(
Expand Down
28 changes: 16 additions & 12 deletions Tests/XMTPTests/ClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -304,28 +304,31 @@ class ClientTests: XCTestCase {
func testRevokesAllOtherInstallations() async throws {
let key = try Crypto.secureRandomBytes(count: 32)
let alix = try PrivateKey.generate()
let options = ClientOptions.init(
api: .init(env: .local, isSecure: false),
dbEncryptionKey: key
)

let alixClient = try await Client.create(
account: alix,
options: options
options: ClientOptions.init(
api: .init(env: .local, isSecure: false),
dbEncryptionKey: key
)
)
try alixClient.dropLocalDatabaseConnection()
try alixClient.deleteLocalDatabase()

let alixClient2 = try await Client.create(
account: alix,
options: options
options: ClientOptions.init(
api: .init(env: .local, isSecure: false),
dbEncryptionKey: key,
dbDirectory: "xmtp_db1"
)
)
try alixClient2.dropLocalDatabaseConnection()
try alixClient2.deleteLocalDatabase()

let alixClient3 = try await Client.create(
account: alix,
options: options
options: ClientOptions.init(
api: .init(env: .local, isSecure: false),
dbEncryptionKey: key,
dbDirectory: "xmtp_db2"
)
)

let state = try await alixClient3.inboxState(refreshFromNetwork: true)
Expand Down Expand Up @@ -532,7 +535,8 @@ class ClientTests: XCTestCase {
print("PERF: Built a client with inboxId in \(time3)s")

// Measure time to build a client with an inboxId and apiClient
try await Client.connectToApiBackend(api: ClientOptions.Api(env: .dev, isSecure: true))
try await Client.connectToApiBackend(
api: ClientOptions.Api(env: .dev, isSecure: true))
let start4 = Date()
try await Client.create(
account: fakeWallet,
Expand Down
119 changes: 0 additions & 119 deletions Tests/XMTPTests/ConversationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,123 +189,4 @@ class ConversationTests: XCTestCase {

await fulfillment(of: [expectation1], timeout: 3)
}

func testSyncConsent() async throws {
let fixtures = try await fixtures()

let key = try Crypto.secureRandomBytes(count: 32)
let alix = try PrivateKey.generate()
var alixClient = try await Client.create(
account: alix,
options: .init(
api: .init(env: .local, isSecure: false),
dbEncryptionKey: key,
dbDirectory: "xmtp_db"
)
)

let dm = try await alixClient.conversations.findOrCreateDm(
with: fixtures.bo.walletAddress)
try await dm.updateConsentState(state: .denied)
XCTAssertEqual(try dm.consentState(), .denied)

try await fixtures.boClient.conversations.sync()
let boDm = try await fixtures.boClient.findConversation(conversationId: dm.id)

var alixClient2 = try await Client.create(
account: alix,
options: .init(
api: .init(env: .local, isSecure: false),
dbEncryptionKey: key,
dbDirectory: "xmtp_db2"
)
)

let state = try await alixClient2.inboxState(refreshFromNetwork: true)
XCTAssertEqual(state.installations.count, 2)

try await fixtures.boClient.conversations.sync()
try await boDm?.sync()
try await alixClient2.preferences.syncConsent()
try await alixClient.conversations.syncAllConversations()
sleep(2)
try await alixClient2.conversations.syncAllConversations()
sleep(2)

if let dm2 = try await alixClient2.findConversation(conversationId: dm.id) {
XCTAssertEqual(try dm2.consentState(), .denied)

try await alixClient2.preferences.setConsentState(
entries: [
ConsentRecord(
value: dm2.id,
entryType: .conversation_id,
consentType: .allowed
)
]
)
let convoState = try await alixClient2.preferences
.conversationState(
conversationId: dm2.id)
XCTAssertEqual(convoState, .allowed)
XCTAssertEqual(try dm2.consentState(), .allowed)
}
}

func testStreamConsent() async throws {
let fixtures = try await fixtures()

let key = try Crypto.secureRandomBytes(count: 32)
let alix = try PrivateKey.generate()

let alixClient = try await Client.create(
account: alix,
options: .init(
api: .init(env: .local, isSecure: false),
dbEncryptionKey: key,
dbDirectory: "xmtp_db"
)
)

let alixGroup = try await alixClient.conversations.newGroup(with: [fixtures.bo.walletAddress])

let alixClient2 = try await Client.create(
account: alix,
options: .init(
api: .init(env: .local, isSecure: false),
dbEncryptionKey: key,
dbDirectory: "xmtp_db2"
)
)

try await alixGroup.send(content: "Hello")
try await alixClient.conversations.syncAllConversations()
try await alixClient2.conversations.syncAllConversations()
let alixGroup2 = try alixClient2.findGroup(groupId: alixGroup.id)!

var consentList = [ConsentRecord]()
let expectation = XCTestExpectation(description: "Stream Consent")
expectation.expectedFulfillmentCount = 3

Task(priority: .userInitiated) {
for try await entry in await alixClient.preferences.streamConsent() {
consentList.append(entry)
expectation.fulfill()
}
}
sleep(1)
try await alixGroup2.updateConsentState(state: .denied)
let dm = try await alixClient2.conversations.newConversation(with: fixtures.caro.walletAddress)
try await dm.updateConsentState(state: .denied)

sleep(5)
try await alixClient.conversations.syncAllConversations()
try await alixClient2.conversations.syncAllConversations()

await fulfillment(of: [expectation], timeout: 3)
print(consentList)
XCTAssertEqual(try alixGroup.consentState(), .denied)
}


}
171 changes: 171 additions & 0 deletions Tests/XMTPTests/HistorySyncTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
//
// HistorySyncTests.swift
// XMTPiOS
//
// Created by Naomi Plasterer on 12/19/24.
//

import Foundation
import XCTest

@testable import XMTPiOS

@available(iOS 15, *)
class HistorySyncTests: XCTestCase {
func testSyncConsent() async throws {
let fixtures = try await fixtures()

let key = try Crypto.secureRandomBytes(count: 32)
let alix = try PrivateKey.generate()
let alixClient = try await Client.create(
account: alix,
options: .init(
api: .init(env: .local, isSecure: false),
dbEncryptionKey: key,
dbDirectory: "xmtp_db"
)
)

let group = try await alixClient.conversations.newGroup(
with: [fixtures.bo.walletAddress])
try await group.updateConsentState(state: .denied)
XCTAssertEqual(try group.consentState(), .denied)


let alixClient2 = try await Client.create(
account: alix,
options: .init(
api: .init(env: .local, isSecure: false),
dbEncryptionKey: key,
dbDirectory: "xmtp_db2"
)
)

let state = try await alixClient2.inboxState(refreshFromNetwork: true)
XCTAssertEqual(state.installations.count, 2)

try await alixClient2.preferences.syncConsent()
try await alixClient.conversations.syncAllConversations()
sleep(2)
try await alixClient2.conversations.syncAllConversations()
sleep(2)

if let dm2 = try await alixClient2.findConversation(conversationId: group.id) {
XCTAssertEqual(try dm2.consentState(), .denied)

try await alixClient2.preferences.setConsentState(
entries: [
ConsentRecord(
value: dm2.id,
entryType: .conversation_id,
consentType: .allowed
)
]
)
let convoState = try await alixClient2.preferences
.conversationState(
conversationId: dm2.id)
XCTAssertEqual(convoState, .allowed)
XCTAssertEqual(try dm2.consentState(), .allowed)
}
}

func testSyncMessages() async throws {
let fixtures = try await fixtures()

let key = try Crypto.secureRandomBytes(count: 32)
let alix = try PrivateKey.generate()
let alixClient = try await Client.create(
account: alix,
options: .init(
api: .init(env: .local, isSecure: false),
dbEncryptionKey: key,
dbDirectory: "xmtp_db"
)
)

let group = try await alixClient.conversations.newGroup(
with: [fixtures.bo.walletAddress])
try await group.send(content: "hi")
let messageCount = try await group.messages().count
XCTAssertEqual(messageCount, 2)

let alixClient2 = try await Client.create(
account: alix,
options: .init(
api: .init(env: .local, isSecure: false),
dbEncryptionKey: key,
dbDirectory: "xmtp_db2"
)
)

let state = try await alixClient2.inboxState(refreshFromNetwork: true)
XCTAssertEqual(state.installations.count, 2)

try await alixClient.conversations.syncAllConversations()
sleep(2)
try await alixClient2.conversations.syncAllConversations()
sleep(2)

if let dm2 = try await alixClient2.findConversation(conversationId: group.id) {
let messageCount = try await group.messages().count
XCTAssertEqual(messageCount, 2)
}
}


func testStreamConsent() async throws {
let fixtures = try await fixtures()

let key = try Crypto.secureRandomBytes(count: 32)
let alix = try PrivateKey.generate()

let alixClient = try await Client.create(
account: alix,
options: .init(
api: .init(env: .local, isSecure: false),
dbEncryptionKey: key,
dbDirectory: "xmtp_db"
)
)

let alixGroup = try await alixClient.conversations.newGroup(with: [fixtures.bo.walletAddress])

let alixClient2 = try await Client.create(
account: alix,
options: .init(
api: .init(env: .local, isSecure: false),
dbEncryptionKey: key,
dbDirectory: "xmtp_db2"
)
)

try await alixGroup.send(content: "Hello")
try await alixClient.conversations.syncAllConversations()
try await alixClient2.conversations.syncAllConversations()
let alixGroup2 = try alixClient2.findGroup(groupId: alixGroup.id)!

var consentList = [ConsentRecord]()
let expectation = XCTestExpectation(description: "Stream Consent")
expectation.expectedFulfillmentCount = 3

Task(priority: .userInitiated) {
for try await entry in await alixClient.preferences.streamConsent() {
consentList.append(entry)
expectation.fulfill()
}
}
sleep(1)
try await alixGroup2.updateConsentState(state: .denied)
let dm = try await alixClient2.conversations.newConversation(with: fixtures.caro.walletAddress)
try await dm.updateConsentState(state: .denied)

sleep(5)
try await alixClient.conversations.syncAllConversations()
try await alixClient2.conversations.syncAllConversations()

await fulfillment(of: [expectation], timeout: 3)
print(consentList)
XCTAssertEqual(try alixGroup.consentState(), .denied)
}
}

0 comments on commit bd7c6b0

Please sign in to comment.