-
Notifications
You must be signed in to change notification settings - Fork 1
fix: recover orphaned channel monitors from RN migration #462
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
Open
jvsena42
wants to merge
8
commits into
master
Choose a base branch
from
fix/reimport-channel-monitor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+136
−4
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cc57da0
chore: add recovery key and property to orphaned channel check
jvsena42 0ef0e8a
chore: make fetchRNRemoteLdkData public and return boolean for success
jvsena42 d90d7d2
fix: implement the recover flow for orphaned channels
jvsena42 8692427
chore: remove redundant MainActor annotaion
jvsena42 bb7a28a
fix: call lightningService.stop() directly to bypass the lifecycle gu…
jvsena42 f69b7ba
fix: handle multiple force close swep
jvsena42 3240b56
fix: check if the sweep already exists as confirmed onchain linked to…
jvsena42 0c505d7
Merge branch 'master' into fix/reimport-channel-monitor
jvsena42 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,6 +59,8 @@ class WalletViewModel: ObservableObject { | |
|
|
||
| static var peerSimulation: BlocktankPeerSimulation = .none | ||
|
|
||
| private static let channelRecoveryRestartDelayMs: UInt64 = 500 | ||
|
|
||
| @Published var isRestoringWallet = false | ||
| @Published var balanceInTransferToSavings: Int = 0 | ||
| @Published var balanceInTransferToSpending: Int = 0 | ||
|
|
@@ -234,6 +236,78 @@ class WalletViewModel: ObservableObject { | |
| Task { @MainActor in | ||
| try await sync() | ||
| } | ||
|
|
||
| // One-time check for orphaned channel monitors from RN migration | ||
| Task { | ||
| await checkForOrphanedChannelMonitorRecovery() | ||
| } | ||
| } | ||
|
|
||
| private func checkForOrphanedChannelMonitorRecovery() async { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this have a guard for migrated users only? |
||
| let migrations = MigrationsService.shared | ||
| guard !migrations.isChannelRecoveryChecked else { return } | ||
|
|
||
| Logger.info("Running one-time channel monitor recovery check", context: "WalletViewModel") | ||
|
|
||
| var allMonitorsRetrieved = false | ||
| do { | ||
| guard let mnemonic = try Keychain.loadString(key: .bip39Mnemonic(index: 0)) else { | ||
| Logger.debug("Channel recovery: no mnemonic, skipping", context: "WalletViewModel") | ||
| migrations.isChannelRecoveryChecked = true | ||
| return | ||
| } | ||
| let passphrase = try? Keychain.loadString(key: .bip39Passphrase(index: 0)) | ||
|
|
||
| RNBackupClient.shared.reset() | ||
| try await RNBackupClient.shared.setup(mnemonic: mnemonic, passphrase: passphrase) | ||
|
|
||
| let retrieved = await migrations.fetchRNRemoteLdkData() | ||
|
|
||
| if let migration = migrations.pendingChannelMigration { | ||
| let monitorCount = migration.channelMonitors.count | ||
| Logger.info( | ||
| "Found \(monitorCount) monitors on RN backup, attempting recovery", | ||
| context: "WalletViewModel" | ||
| ) | ||
|
|
||
| let channelMigration = ChannelDataMigration( | ||
| channelManager: [UInt8](migration.channelManager), | ||
| channelMonitors: migration.channelMonitors.map { [UInt8]($0) } | ||
| ) | ||
| migrations.pendingChannelMigration = nil | ||
|
|
||
| // Stop and restart the lightning service directly (not via self.start()) | ||
| // to avoid the nodeLifecycleState guard racing with concurrent sync Tasks | ||
| try await lightningService.stop() | ||
| try await Task.sleep(nanoseconds: Self.channelRecoveryRestartDelayMs * 1_000_000) | ||
|
|
||
| let electrumServerUrl = electrumConfigService.getCurrentServer().fullUrl | ||
| let rgsServerUrl = rgsConfigService.getCurrentServerUrl() | ||
| try await lightningService.setup( | ||
| walletIndex: 0, | ||
| electrumServerUrl: electrumServerUrl, | ||
| rgsServerUrl: rgsServerUrl.isEmpty ? nil : rgsServerUrl, | ||
| channelMigration: channelMigration | ||
| ) | ||
| try await lightningService.start() | ||
|
|
||
| nodeLifecycleState = .running | ||
| syncState() | ||
| Logger.info("Channel monitor recovery complete", context: "WalletViewModel") | ||
| } else { | ||
| Logger.info("No channel monitors found on RN backup", context: "WalletViewModel") | ||
| } | ||
|
|
||
| allMonitorsRetrieved = retrieved | ||
| } catch { | ||
| Logger.error("Channel monitor recovery check failed: \(error)", context: "WalletViewModel") | ||
| } | ||
|
|
||
| if allMonitorsRetrieved { | ||
| migrations.isChannelRecoveryChecked = true | ||
| } else { | ||
| Logger.warn("Some monitors failed to download, will retry on next startup", context: "WalletViewModel") | ||
| } | ||
| } | ||
|
|
||
| private func fetchTrustedPeersFromBlocktank() async -> [LnPeer]? { | ||
|
|
@@ -251,7 +325,6 @@ class WalletViewModel: ObservableObject { | |
| break | ||
| } | ||
|
|
||
|
|
||
| var info: IBtInfo? | ||
| do { | ||
| info = try await coreService.blocktank.info(refresh: true) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can run this before
lightningService.setup()as well I guess, that would eliminate the need to stop and restart the node