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

fix: android perform rgs download and update on background thread #181

Merged
merged 2 commits into from
Oct 14, 2023
Merged
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
65 changes: 42 additions & 23 deletions lib/android/src/main/java/com/reactnativeldk/Helpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -215,39 +215,58 @@ fun URL.downloadFile(destination: String, completion: (error: Error?) -> Unit) {
}

fun RapidGossipSync.downloadAndUpdateGraph(downloadUrl: String, tempStoragePath: String, timestamp: Long, completion: (error: Error?) -> Unit) {
val destinationFile = "$tempStoragePath$timestamp.bin"
Thread(Runnable {
val destinationFile = "$tempStoragePath$timestamp.bin"

URL(downloadUrl + timestamp).downloadFile(destinationFile) {
if (it != null) {
return@downloadFile completion(it)
}
URL(downloadUrl + timestamp).downloadFile(destinationFile) {
if (it != null) {
UiThreadUtil.runOnUiThread {
completion(it)
}
return@downloadFile
}

val res = update_network_graph_no_std(File(destinationFile).readBytes(), Option_u64Z.some((System.currentTimeMillis() / 1000)))
if (!res.is_ok()) {
val error = res as? Result_u32GraphSyncErrorZ.Result_u32GraphSyncErrorZ_Err
val res = update_network_graph_no_std(File(destinationFile).readBytes(), Option_u64Z.some((System.currentTimeMillis() / 1000)))
if (!res.is_ok()) {
val error = res as? Result_u32GraphSyncErrorZ.Result_u32GraphSyncErrorZ_Err

(error?.err as? GraphSyncError.LightningError)?.let { lightningError ->
return@downloadFile completion(Error("Rapid sync LightningError. " + lightningError.lightning_error._err))
}
(error?.err as? GraphSyncError.LightningError)?.let { lightningError ->
UiThreadUtil.runOnUiThread {
completion(Error("Rapid sync LightningError. " + lightningError.lightning_error._err))
}
return@downloadFile
}

(error?.err as? GraphSyncError.DecodeError)?.let { decodeError ->
(decodeError.decode_error as? DecodeError.Io)?.let { decodeIOError ->
return@downloadFile completion(Error("Rapid sync DecodeError. " + decodeIOError.io.ordinal))
(error?.err as? GraphSyncError.DecodeError)?.let { decodeError ->
(decodeError.decode_error as? DecodeError.Io)?.let { decodeIOError ->
UiThreadUtil.runOnUiThread {
completion(Error("Rapid sync DecodeError. " + decodeIOError.io.ordinal))
}
return@downloadFile
}

UiThreadUtil.runOnUiThread {
completion(Error("Rapid sync DecodeError"))
}
return@downloadFile
}

return@downloadFile completion(Error("Rapid sync DecodeError"))
UiThreadUtil.runOnUiThread {
completion(Error("Unknown rapid sync error."))
}
return@downloadFile
}

return@downloadFile completion(Error("Unknown rapid sync error."))
}
val usedFile = File(destinationFile)
if (usedFile.exists()) {
usedFile.delete()
}

val usedFile = File(destinationFile)
if (usedFile.exists()) {
usedFile.delete()
UiThreadUtil.runOnUiThread {
completion(null)
}
}

completion(null)
}
}).start()
}

fun ChannelHandshakeConfig.mergeWithMap(map: ReadableMap?): ChannelHandshakeConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class LdkPersister {

return ChannelMonitorUpdateStatus.LDKChannelMonitorUpdateStatus_Completed
} catch (e: Exception) {
LdkEventEmitter.send(EventTypes.emergency_force_close_channel, body)
return ChannelMonitorUpdateStatus.LDKChannelMonitorUpdateStatus_UnrecoverableError
}
}
Expand Down
Loading