Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance improvements to 1to1 dm peerinboxId #413

Merged
merged 18 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ let package = Package(
.package(url: "https://github.com/1024jp/GzipSwift", from: "5.2.0"),
.package(url: "https://github.com/bufbuild/connect-swift", exact: "0.12.0"),
.package(url: "https://github.com/apple/swift-docc-plugin.git", from: "1.0.0"),
.package(url: "https://github.com/xmtp/libxmtp-swift.git", exact: "0.5.10"),
.package(url: "https://github.com/xmtp/libxmtp-swift.git", exact: "0.6.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand Down
1 change: 1 addition & 0 deletions Sources/XMTPiOS/ApiClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ extension GenericErrorDescribing {
let .GroupMutablePermissions(message),
let .SignatureRequestError(message),
let .Erc1271SignatureError(message),
let .FailedToConvertToU32(message),
let .Verifier(message):
return message
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/XMTPiOS/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ public final class Client {
accountAddress: address,
options: options,
privateKeyBundleV1: v1Bundle,
signingKey: nil,
signingKey: signingKey,
inboxId: inboxId
)

Expand Down
25 changes: 12 additions & 13 deletions Sources/XMTPiOS/Conversations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public enum ConversationOrder {
}

final class ConversationStreamCallback: FfiConversationCallback {
func onError(error: LibXMTP.FfiSubscribeError) {
print("Error ConversationStreamCallback \(error)")
}

let callback: (FfiConversation) -> Void

init(callback: @escaping (FfiConversation) -> Void) {
Expand All @@ -66,6 +70,10 @@ final class ConversationStreamCallback: FfiConversationCallback {
}

final class V2SubscriptionCallback: FfiV2SubscriptionCallback {
func onError(error: LibXMTP.GenericError) {
print("Error V2SubscriptionCallback \(error)")
}

let callback: (Envelope) -> Void

init(callback: @escaping (Envelope) -> Void) {
Expand Down Expand Up @@ -139,7 +147,7 @@ public actor Conversations {
guard let v3Client = client.v3Client else {
return []
}
var options = FfiListConversationsOptions(createdAfterNs: nil, createdBeforeNs: nil, limit: nil)
var options = FfiListConversationsOptions(createdAfterNs: nil, createdBeforeNs: nil, limit: nil, consentState: nil)
if let createdAfter {
options.createdAfterNs = Int64(createdAfter.millisecondsSinceEpoch)
}
Expand All @@ -159,7 +167,7 @@ public actor Conversations {
guard let v3Client = client.v3Client else {
return []
}
var options = FfiListConversationsOptions(createdAfterNs: nil, createdBeforeNs: nil, limit: nil)
var options = FfiListConversationsOptions(createdAfterNs: nil, createdBeforeNs: nil, limit: nil, consentState: nil)
if let createdAfter {
options.createdAfterNs = Int64(createdAfter.millisecondsSinceEpoch)
}
Expand All @@ -180,7 +188,7 @@ public actor Conversations {
guard let v3Client = client.v3Client else {
return []
}
var options = FfiListConversationsOptions(createdAfterNs: nil, createdBeforeNs: nil, limit: nil)
var options = FfiListConversationsOptions(createdAfterNs: nil, createdBeforeNs: nil, limit: nil, consentState: consentState?.toFFI)
if let createdAfter {
options.createdAfterNs = Int64(createdAfter.millisecondsSinceEpoch)
}
Expand All @@ -192,8 +200,7 @@ public actor Conversations {
}
let ffiConversations = try await v3Client.conversations().list(opts: options)

let filteredConversations = try filterByConsentState(ffiConversations, consentState: consentState)
let sortedConversations = try sortConversations(filteredConversations, order: order)
let sortedConversations = try sortConversations(ffiConversations, order: order)

return try sortedConversations.map { try $0.toConversation(client: client) }
}
Expand Down Expand Up @@ -226,14 +233,6 @@ public actor Conversations {
}
}

private func filterByConsentState(
_ conversations: [FfiConversation],
consentState: ConsentState?
) throws -> [FfiConversation] {
guard let state = consentState else { return conversations }
return try conversations.filter { try $0.consentState() == state.toFFI }
}

public func streamGroups() async throws -> AsyncThrowingStream<Group, Error> {
AsyncThrowingStream { continuation in
let ffiStreamActor = FfiStreamActor()
Expand Down
11 changes: 2 additions & 9 deletions Sources/XMTPiOS/Dm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,8 @@ public struct Dm: Identifiable, Equatable, Hashable {
}

public var peerInboxId: String {
get async throws {
var ids = try await members.map(\.inboxId)
if let index = ids.firstIndex(of: client.inboxID) {
ids.remove(at: index)
}
guard let inboxId = ids.first else {
throw ClientError.missingInboxId
}
return inboxId
get throws {
try ffiConversation.dmPeerInboxId()
}
}

Expand Down
4 changes: 4 additions & 0 deletions Sources/XMTPiOS/Group.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import Foundation
import LibXMTP

final class MessageCallback: FfiMessageCallback {
func onError(error: LibXMTP.FfiSubscribeError) {
print("Error MessageCallback \(error)")
}

let client: Client
let callback: (LibXMTP.FfiMessage) -> Void

Expand Down
88 changes: 88 additions & 0 deletions Tests/XMTPTests/V3ClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -362,4 +362,92 @@ class V3ClientTests: XCTestCase {

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

func createDms(client: Client, peers: [Client], numMessages: Int) async throws -> [Dm] {
var dms: [Dm] = []
for peer in peers {
let dm = try await peer.conversations.findOrCreateDm(with: client.address)
dms.append(dm)
for i in 0..<numMessages {
try await dm.send(content: "Alix message \(i)")
}
}
return dms
}

func createV2Convos(client: Client, peers: [Client], numMessages: Int) async throws -> [Conversation] {
var convos: [Conversation] = []
for peer in peers {
let convo = try await peer.conversations.newConversation(with: client.address)
convos.append(convo)
for i in 0..<numMessages {
try await convo.send(content: "Alix message \(i)")
}
}
return convos
}

func createV2Clients(num: Int) async throws -> [Client] {
var clients: [Client] = []
for _ in 0..<num {
let wallet = try PrivateKey.generate()
let client = try await Client.create(
account: wallet,
options: .init(
api: .init(env: .local, isSecure: false)
)
)
clients.append(client)
}
return clients
}

func createV3Clients(num: Int) async throws -> [Client] {
let key = try Crypto.secureRandomBytes(count: 32)
var clients: [Client] = []
for _ in 0..<num {
let wallet = try PrivateKey.generate()
let client = try await Client.createV3(
account: wallet,
options: .init(
api: .init(env: .local, isSecure: false),
enableV3: true,
encryptionKey: key
)
)
clients.append(client)
}
return clients
}

func testCompareV2AndV3Dms() async throws {
let alixClient = try await createV2Clients(num: 1).first!
let davonV3Client = try await createV3Clients(num: 1).first!

let initialPeers = try await createV2Clients(num: 50)
let initialV3Peers = try await createV3Clients(num: 50)

try await createDms(client: davonV3Client, peers: initialV3Peers, numMessages: 1)
try await createV2Convos(client: alixClient, peers: initialPeers, numMessages: 1)

var start = Date()
var v2Convos = try await alixClient.conversations.list()
var end = Date()
print("Alix loaded \(v2Convos.count) v2Convos in \(end.timeIntervalSince(start) * 1000)ms")

start = Date()
v2Convos = try await alixClient.conversations.list()
end = Date()
print("Alix 2nd loaded \(v2Convos.count) v2Convos in \(end.timeIntervalSince(start) * 1000)ms")

start = Date()
try await davonV3Client.conversations.sync()
end = Date()
print("Davon synced \(v2Convos.count) Dms in \(end.timeIntervalSince(start) * 1000)ms")

start = Date()
let dms = try await davonV3Client.conversations.listConversations()
end = Date()
print("Davon loaded \(dms.count) Dms in \(end.timeIntervalSince(start) * 1000)ms")
}
}
4 changes: 2 additions & 2 deletions XMTP.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |spec|
#

spec.name = "XMTP"
spec.version = "0.16.0"
spec.version = "0.16.1"
spec.summary = "XMTP SDK Cocoapod"

# This description is used to generate tags and improve search results.
Expand Down Expand Up @@ -44,5 +44,5 @@ Pod::Spec.new do |spec|
spec.dependency "web3.swift"
spec.dependency "GzipSwift"
spec.dependency "Connect-Swift", "= 0.12.0"
spec.dependency 'LibXMTP', '= 0.5.10'
spec.dependency 'LibXMTP', '= 0.6.0'
end
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/xmtp/libxmtp-swift.git",
"state" : {
"revision" : "aea8058324fc349288bba50089b8edd2644971be",
"version" : "0.5.10"
"revision" : "91653cdaf999119f99189178867e32dcc53b11e8",
"version" : "0.6.0"
}
},
{
Expand Down
Loading