Skip to content

Commit ae73061

Browse files
ben-kaufmanovitrif
authored andcommitted
feat: channel monitor recovery flow
Depends on: synonymdev/ldk-node#76
1 parent 170a50d commit ae73061

3 files changed

Lines changed: 31 additions & 6 deletions

File tree

app/src/main/java/to/bitkit/env/Env.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ internal object Env {
2323
val network = Network.valueOf(BuildConfig.NETWORK)
2424
val locales = BuildConfig.LOCALES.split(",")
2525
const val walletSyncIntervalSecs = 10_uL
26+
const val walletSyncTimeoutSecs = 10_uL
2627
val platform = "Android ${Build.VERSION.RELEASE} (API ${Build.VERSION.SDK_INT})"
2728
const val version = "$VERSION_NAME (${BuildConfig.VERSION_CODE})"
2829

app/src/main/java/to/bitkit/services/LightningService.kt

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class LightningService @Inject constructor(
138138
)
139139
}
140140

141-
@Suppress("ForbiddenComment")
141+
@Suppress("ForbiddenComment", "LongMethod")
142142
private suspend fun build(
143143
walletIndex: Int,
144144
customServerUrl: String?,
@@ -186,11 +186,34 @@ class LightningService @Inject constructor(
186186
"Building node with \n\t vssUrl: '$vssUrl'\n\t lnurlAuthServerUrl: '$lnurlAuthServerUrl'",
187187
context = TAG,
188188
)
189-
if (lnurlAuthServerUrl.isNotEmpty()) {
190-
builder.buildWithVssStore(vssUrl, vssStoreId, lnurlAuthServerUrl, fixedHeaders)
191-
} else {
192-
builder.buildWithVssStoreAndFixedHeaders(vssUrl, vssStoreId, fixedHeaders)
189+
190+
fun buildNode() = runCatching {
191+
if (lnurlAuthServerUrl.isNotEmpty()) {
192+
builder.buildWithVssStore(vssUrl, vssStoreId, lnurlAuthServerUrl, fixedHeaders)
193+
} else {
194+
builder.buildWithVssStoreAndFixedHeaders(vssUrl, vssStoreId, fixedHeaders)
195+
}
193196
}
197+
198+
buildNode().recoverCatching {
199+
if (it !is BuildException.DangerousValue) throw it
200+
// Handle `DangerousValue` with recovery flow
201+
Logger.warn(
202+
"Build failed with DangerousValue. Retrying with `accept_stale_channel_monitors` for recovery.",
203+
it,
204+
context = TAG,
205+
)
206+
builder.setAcceptStaleChannelMonitors(true)
207+
buildNode()
208+
.onFailure { recoveryError ->
209+
Logger.error(
210+
"Error in recovery attempt with `accept_stale_channel_monitors`.",
211+
recoveryError,
212+
context = TAG,
213+
)
214+
}
215+
.getOrThrow()
216+
}.getOrThrow()
194217
} catch (e: BuildException) {
195218
throw LdkError(e)
196219
} finally {
@@ -226,6 +249,7 @@ class LightningService @Inject constructor(
226249
lightningWalletSyncIntervalSecs = Env.walletSyncIntervalSecs,
227250
feeRateCacheUpdateIntervalSecs = Env.walletSyncIntervalSecs,
228251
),
252+
connectionTimeoutSecs = Env.walletSyncTimeoutSecs, // 10s
229253
),
230254
)
231255
}

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" }
5858
ktor-client-logging = { module = "io.ktor:ktor-client-logging", version.ref = "ktor" }
5959
ktor-client-okhttp = { module = "io.ktor:ktor-client-okhttp", version.ref = "ktor" }
6060
ktor-serialization-kotlinx-json = { module = "io.ktor:ktor-serialization-kotlinx-json", version.ref = "ktor" }
61-
ldk-node-android = { module = "com.synonym:ldk-node-android", version = "0.7.0-rc.32" }
61+
ldk-node-android = { module = "com.synonym:ldk-node-android", version = "0.7.0-rc.34" }
6262
lifecycle-process = { group = "androidx.lifecycle", name = "lifecycle-process", version.ref = "lifecycle" }
6363
lifecycle-runtime-compose = { module = "androidx.lifecycle:lifecycle-runtime-compose", version.ref = "lifecycle" }
6464
lifecycle-runtime-ktx = { module = "androidx.lifecycle:lifecycle-runtime-ktx", version.ref = "lifecycle" }

0 commit comments

Comments
 (0)