Skip to content

Commit ce7e890

Browse files
authored
Revert "fix: prevent channel monitor loss when remote backup fails"
1 parent bae91a6 commit ce7e890

File tree

3 files changed

+54
-39
lines changed

3 files changed

+54
-39
lines changed

lib/android/src/main/java/com/reactnativeldk/classes/LdkPersister.kt

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,29 @@ class LdkPersister {
2323

2424
val isNew = !file.exists()
2525

26-
// Always write locally first
27-
val serialized = data.write()
28-
file.writeBytes(serialized)
26+
if (BackupClient.skipRemoteBackup) {
27+
file.writeBytes(data.write())
28+
if (isNew) {
29+
LdkEventEmitter.send(EventTypes.new_channel, body)
30+
}
31+
return ChannelMonitorUpdateStatus.LDKChannelMonitorUpdateStatus_Completed
32+
}
33+
34+
BackupClient.addToPersistQueue(BackupClient.Label.CHANNEL_MONITOR(channelId=channelId), data.write()) { error ->
35+
if (error != null) {
36+
LdkEventEmitter.send(EventTypes.native_log, "Failed to persist channel (${channelId}) to remote backup: $error")
37+
return@addToPersistQueue
38+
}
39+
40+
try {
41+
file.writeBytes(data.write())
42+
} catch (e: Exception) {
43+
//If this fails we can't do much but LDK will retry on startup
44+
LdkEventEmitter.send(EventTypes.native_log, "Failed to locally persist channel (${channelId}) to disk")
45+
return@addToPersistQueue
46+
}
2947

30-
// Update chain monitor on main thread to avoid threading issues
31-
LdkModule.reactContext?.runOnUiThread {
48+
//Update chain monitor with successful persist
3249
val res = LdkModule.chainMonitor?.channel_monitor_updated(channelFundingOutpoint, data._latest_update_id)
3350
if (res == null || !res.is_ok) {
3451
LdkEventEmitter.send(EventTypes.native_log, "Failed to update chain monitor with persisted channel (${channelId})")
@@ -40,16 +57,7 @@ class LdkPersister {
4057
}
4158
}
4259

43-
// Kick off remote backup asynchronously; never block/skip local persist
44-
if (!BackupClient.skipRemoteBackup) {
45-
BackupClient.addToPersistQueue(BackupClient.Label.CHANNEL_MONITOR(channelId=channelId), serialized) { error ->
46-
if (error != null) {
47-
LdkEventEmitter.send(EventTypes.native_log, "Failed to persist channel (${channelId}) to remote backup: $error")
48-
}
49-
}
50-
}
51-
52-
return ChannelMonitorUpdateStatus.LDKChannelMonitorUpdateStatus_Completed
60+
return ChannelMonitorUpdateStatus.LDKChannelMonitorUpdateStatus_InProgress
5361
} catch (e: Exception) {
5462
return ChannelMonitorUpdateStatus.LDKChannelMonitorUpdateStatus_UnrecoverableError
5563
}

lib/ios/Classes/LdkPersist.swift

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,33 @@ class LdkPersister: Persist {
2727
}
2828

2929
let isNew = !FileManager().fileExists(atPath: channelStoragePath.path)
30-
31-
// Always write locally first
32-
let serialized = Data(data.write())
33-
try serialized.write(to: channelStoragePath)
34-
35-
// Notify chain monitor on main thread to avoid threading issues
36-
DispatchQueue.main.async {
37-
let res = Ldk.chainMonitor?.channelMonitorUpdated(
38-
fundingTxo: channelFundingOutpoint,
39-
completedUpdateId: data.getLatestUpdateId()
40-
)
30+
31+
// If we're not remotely backing up no need to update status later
32+
if BackupClient.skipRemoteBackup {
33+
try Data(data.write()).write(to: channelStoragePath)
34+
if isNew {
35+
LdkEventEmitter.shared.send(withEvent: .new_channel, body: body)
36+
}
37+
return ChannelMonitorUpdateStatus.Completed
38+
}
39+
40+
BackupClient.addToPersistQueue(.channelMonitor(id: channelIdHex), data.write()) { error in
41+
if let error {
42+
LdkEventEmitter.shared.send(withEvent: .native_log, body: "Error. Failed persist channel on remote server (\(channelIdHex)). \(error.localizedDescription)")
43+
return
44+
}
45+
46+
// Callback for when the persist queue queue entry is processed
47+
do {
48+
try Data(data.write()).write(to: channelStoragePath)
49+
} catch {
50+
// If this fails we can't do much but LDK will retry on startup
51+
LdkEventEmitter.shared.send(withEvent: .native_log, body: "Error. Failed to locally persist channel (\(channelIdHex)). \(error.localizedDescription)")
52+
return
53+
}
54+
55+
// Update chainmonitor with successful persist
56+
let res = Ldk.chainMonitor?.channelMonitorUpdated(fundingTxo: channelFundingOutpoint, completedUpdateId: data.getLatestUpdateId())
4157
if let error = res?.getError() {
4258
LdkEventEmitter.shared.send(withEvent: .native_log, body: "Error. Failed to update chain monitor for channel (\(channelIdHex)) Error \(error.getValueType()).")
4359
} else {
@@ -47,19 +63,10 @@ class LdkPersister: Persist {
4763
}
4864
}
4965
}
50-
51-
// Kick off remote backup asynchronously; log but never block/skip local persist
52-
if !BackupClient.skipRemoteBackup {
53-
BackupClient.addToPersistQueue(.channelMonitor(id: channelIdHex), [UInt8](serialized)) { error in
54-
if let error {
55-
LdkEventEmitter.shared.send(withEvent: .native_log, body: "Error. Failed persist channel on remote server (\(channelIdHex)). \(error.localizedDescription)")
56-
}
57-
}
58-
}
59-
60-
return ChannelMonitorUpdateStatus.Completed
66+
67+
return ChannelMonitorUpdateStatus.InProgress
6168
} catch {
62-
LdkEventEmitter.shared.send(withEvent: .native_log, body: "Error. Failed to locally persist channel (\(channelIdHex)). \(error.localizedDescription)")
69+
LdkEventEmitter.shared.send(withEvent: .native_log, body: "Error. Failed to persist channel (\(channelIdHex)) to disk Error \(error.localizedDescription).")
6370
return ChannelMonitorUpdateStatus.UnrecoverableError
6471
}
6572
}

lib/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@synonymdev/react-native-ldk",
33
"title": "React Native LDK",
4-
"version": "0.0.161",
4+
"version": "0.0.160",
55
"description": "React Native wrapper for LDK",
66
"main": "./dist/index.js",
77
"types": "./dist/index.d.ts",

0 commit comments

Comments
 (0)