Skip to content
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
59 changes: 20 additions & 39 deletions Bitkit/Services/LightningService.swift
Comment thread
jvsena42 marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ class LightningService {

Logger.debug("Checking lightning process lock...")
try await StateLocker.lock(.lightning, wait: 30) // Wait 30 seconds to lock because maybe extension is still running
// Unlock if setup fails before a node exists. After a successful build the node keeps the lock until stop.
var shouldReleaseLightningLock = true
Comment thread
jvsena42 marked this conversation as resolved.
defer {
if shouldReleaseLightningLock {
try? StateLocker.unlock(.lightning)
}
}

guard var mnemonic = try Keychain.loadString(key: .bip39Mnemonic(index: walletIndex)) else {
throw CustomServiceError.mnemonicNotFound
Expand Down Expand Up @@ -154,48 +161,22 @@ class LightningService {
builder.setEntropyBip39Mnemonic(mnemonic: mnemonic, passphrase: passphrase)

try await ServiceQueue.background(.ldk) {
do {
if !lnurlAuthServerUrl.isEmpty {
self.node = try builder.buildWithVssStore(
vssUrl: vssUrl,
storeId: storeId,
lnurlAuthServerUrl: lnurlAuthServerUrl,
fixedHeaders: [:]
)
} else {
self.node = try builder.buildWithVssStoreAndFixedHeaders(
vssUrl: vssUrl,
storeId: storeId,
fixedHeaders: [:]
)
}
} catch let error as BuildError {
guard case .DangerousValue = error else { throw error }

// Stale ChannelMonitor vs ChannelManager — retry with accept_stale to recover.
Logger.warn(
"Build failed with DangerousValue. Retrying with accept_stale_channel_monitors for recovery.",
context: "Recovery"
if !lnurlAuthServerUrl.isEmpty {
self.node = try builder.buildWithVssStore(
vssUrl: vssUrl,
storeId: storeId,
lnurlAuthServerUrl: lnurlAuthServerUrl,
fixedHeaders: [:]
)
} else {
self.node = try builder.buildWithVssStoreAndFixedHeaders(
vssUrl: vssUrl,
storeId: storeId,
fixedHeaders: [:]
)
builder.setAcceptStaleChannelMonitors(accept: true)

if !lnurlAuthServerUrl.isEmpty {
self.node = try builder.buildWithVssStore(
vssUrl: vssUrl,
storeId: storeId,
lnurlAuthServerUrl: lnurlAuthServerUrl,
fixedHeaders: [:]
)
} else {
self.node = try builder.buildWithVssStoreAndFixedHeaders(
vssUrl: vssUrl,
storeId: storeId,
fixedHeaders: [:]
)
}
Logger.info("Stale monitor recovery: build succeeded with accept_stale", context: "Recovery")
}
}
Comment thread
ben-kaufman marked this conversation as resolved.
shouldReleaseLightningLock = false

Logger.info("LDK node setup")

Expand Down
1 change: 1 addition & 0 deletions changelog.d/next/664.security.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Lightning no longer automatically starts from outdated channel monitor data after a storage mismatch.
Loading