Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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 Bitkit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@
repositoryURL = "https://github.com/synonymdev/ldk-node";
requirement = {
kind = revision;
revision = c5698d00066e0e50f33696afc562d71023da2373;
revision = 1531af727811f03ec0542aec3befb5a26198b192;
};
};
96DEA0382DE8BBA1009932BF /* XCRemoteSwiftPackageReference "bitkit-core" */ = {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 32 additions & 3 deletions Bitkit/Services/MigrationsService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2098,9 +2098,10 @@ extension MigrationsService {
}

/// Fetches channel manager and monitors from RN remote backup.
/// When `walletIndex` is provided, filters out monitors that already exist in local LDK storage.
/// Returns `true` if all monitors were successfully retrieved (or none exist), `false` if some failed.
@discardableResult
func fetchRNRemoteLdkData() async -> Bool {
func fetchRNRemoteLdkData(walletIndex: Int? = nil) async -> Bool {
do {
let files = try await RNBackupClient.shared.listFiles(fileGroup: "ldk")

Expand All @@ -2109,10 +2110,38 @@ extension MigrationsService {
return true
}

let expectedCount = files.channel_monitors.count
// Filter out monitors that already exist locally (previously migrated)
var localChannelIds: Set<String> = []
if let walletIndex {
let ldkPath = Env.ldkStorage(walletIndex: walletIndex)
let channelsPath = ldkPath.appendingPathComponent("channels")
let monitorsPath = ldkPath.appendingPathComponent("monitors")
let monitorDir = FileManager.default.fileExists(atPath: channelsPath.path) ? channelsPath : monitorsPath

if FileManager.default.fileExists(atPath: monitorDir.path),
let localFiles = try? FileManager.default.contentsOfDirectory(atPath: monitorDir.path)
{
localChannelIds = Set(localFiles.filter { $0.hasSuffix(".bin") }.map { $0.replacingOccurrences(of: ".bin", with: "") })
}
}

let remoteMonitorFiles = files.channel_monitors.filter { file in
let channelId = file.replacingOccurrences(of: ".bin", with: "")
return !localChannelIds.contains(channelId)
}

if !localChannelIds.isEmpty {
let skipped = files.channel_monitors.count - remoteMonitorFiles.count
Logger.info(
"Filtered \(skipped) already-migrated monitors, \(remoteMonitorFiles.count) orphaned remaining",
context: "Migration"
)
}

let expectedCount = remoteMonitorFiles.count
let monitorResults = await withTaskGroup(of: (String, Data?).self) { group in
var results: [(String, Data?)] = []
for monitorFile in files.channel_monitors {
for monitorFile in remoteMonitorFiles {
let channelId = monitorFile.replacingOccurrences(of: ".bin", with: "")
group.addTask {
await (channelId, self.retrieveChannelMonitorWithRetry(channelId: channelId))
Expand Down
2 changes: 1 addition & 1 deletion Bitkit/ViewModels/WalletViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ class WalletViewModel: ObservableObject {
RNBackupClient.shared.reset()
try await RNBackupClient.shared.setup(mnemonic: mnemonic, passphrase: passphrase)

let allRetrieved = await migrations.fetchRNRemoteLdkData()
let allRetrieved = await migrations.fetchRNRemoteLdkData(walletIndex: walletIndex)

if let migration = migrations.pendingChannelMigration {
Logger.info(
Expand Down
Loading