Skip to content

Commit

Permalink
Fix SQL App Group Crash (#339)
Browse files Browse the repository at this point in the history
* bump the ios version

* add functions to disconnect and reconnect the database

* write a test for it
  • Loading branch information
nplasterer authored May 24, 2024
1 parent c5baafd commit 46afeef
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 12 deletions.
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", exact: "0.4.4-beta5"),
.package(url: "https://github.com/xmtp/libxmtp-swift", exact: "0.4.4-beta6"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand Down
2 changes: 2 additions & 0 deletions Sources/XMTPiOS/ApiClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ extension GenericErrorDescribing {
let .GroupMetadata(message),
let .Generic(message):
return message
case .GroupMutablePermissions(message: let message):
return message
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions Sources/XMTPiOS/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,21 @@ public final class Client {
let fm = FileManager.default
try fm.removeItem(atPath: dbPath)
}

@available(*, deprecated, message: "This function is delicate and should be used with caution. App will error if database not properly reconnected. See: reconnectLocalDatabase()")
public func dropLocalDatabaseConnection() throws {
guard let client = v3Client else {
throw ClientError.noV3Client("Error no V3 client initialized")
}
try client.releaseDbConnection()
}

public func reconnectLocalDatabase() async throws {
guard let client = v3Client else {
throw ClientError.noV3Client("Error no V3 client initialized")
}
try await client.dbReconnect()
}

func getUserContact(peerAddress: String) async throws -> ContactBundle? {
let peerAddress = EthereumAddress(peerAddress).toChecksumAddress()
Expand Down
6 changes: 3 additions & 3 deletions Sources/XMTPiOS/Group.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ public struct Group: Identifiable, Equatable, Hashable {
return try metadata().creatorAccountAddress().lowercased() == client.address.lowercased()
}

public func permissionLevel() throws -> GroupPermissions {
return try metadata().policyType()
}
// public func permissionLevel() throws -> GroupPermissions {
// return try metadata().policyType()
// }

public func adminAddress() throws -> String {
return try metadata().creatorAccountAddress()
Expand Down
35 changes: 35 additions & 0 deletions Tests/XMTPTests/ClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,41 @@ class ClientTests: XCTestCase {
groupCount = try await boClient.conversations.groups().count
XCTAssertEqual(groupCount, 0)
}

func testCanDropReconnectDatabase() async throws {
let bo = try PrivateKey.generate()
let alix = try PrivateKey.generate()
var boClient = try await Client.create(
account: bo,
options: .init(
api: .init(env: .local, isSecure: false),
mlsAlpha: true
)
)

let alixClient = try await Client.create(
account: alix,
options: .init(
api: .init(env: .local, isSecure: false),
mlsAlpha: true
)
)

_ = try await boClient.conversations.newGroup(with: [alixClient.address])
try await boClient.conversations.sync()

var groupCount = try await boClient.conversations.groups().count
XCTAssertEqual(groupCount, 1)

try boClient.dropLocalDatabaseConnection()

await assertThrowsAsyncError(try await boClient.conversations.groups())

try await boClient.reconnectLocalDatabase()

groupCount = try await boClient.conversations.groups().count
XCTAssertEqual(groupCount, 1)
}

func testCanMessage() async throws {
let fixtures = await fixtures()
Expand Down
8 changes: 4 additions & 4 deletions Tests/XMTPTests/GroupTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ class GroupTests: XCTestCase {
XCTAssertEqual(aliceGroup.memberAddresses.count, 3)
XCTAssertEqual(bobGroup.memberAddresses.count, 3)

XCTAssertEqual(try bobGroup.permissionLevel(), .everyoneIsAdmin)
XCTAssertEqual(try aliceGroup.permissionLevel(), .everyoneIsAdmin)
// XCTAssertEqual(try bobGroup.permissionLevel(), .everyoneIsAdmin)
// XCTAssertEqual(try aliceGroup.permissionLevel(), .everyoneIsAdmin)
XCTAssertEqual(try bobGroup.adminAddress().lowercased(), fixtures.bobClient.address.lowercased())
XCTAssertEqual(try aliceGroup.adminAddress().lowercased(), fixtures.bobClient.address.lowercased())
XCTAssert(try bobGroup.isAdmin())
Expand Down Expand Up @@ -156,8 +156,8 @@ class GroupTests: XCTestCase {
XCTAssertEqual(aliceGroup.memberAddresses.count, 2)
XCTAssertEqual(bobGroup.memberAddresses.count, 2)

XCTAssertEqual(try bobGroup.permissionLevel(), .groupCreatorIsAdmin)
XCTAssertEqual(try aliceGroup.permissionLevel(), .groupCreatorIsAdmin)
// XCTAssertEqual(try bobGroup.permissionLevel(), .groupCreatorIsAdmin)
// XCTAssertEqual(try aliceGroup.permissionLevel(), .groupCreatorIsAdmin)
XCTAssertEqual(try bobGroup.adminAddress().lowercased(), fixtures.bobClient.address.lowercased())
XCTAssertEqual(try aliceGroup.adminAddress().lowercased(), fixtures.bobClient.address.lowercased())
XCTAssert(try bobGroup.isAdmin())
Expand Down
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.10.11"
spec.version = "0.10.12"
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.4.4-beta5'
spec.dependency 'LibXMTP', '= 0.4.4-beta6'
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",
"state" : {
"revision" : "f2b73d14be21b64feecfa70c789b3302525975ab",
"version" : "0.4.4-beta5"
"revision" : "11ac13baf3b2d36571a8ad5b0d8bda3017635563",
"version" : "0.4.4-beta6"
}
},
{
Expand Down

0 comments on commit 46afeef

Please sign in to comment.