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
25 changes: 21 additions & 4 deletions LDKNodeMonday/App/WalletClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,27 +96,44 @@ public class WalletClient {
lsp: LightningServiceProvider? = nil
) async {
do {
let targetMode = appMode ?? .live

await MainActor.run {
self.appState = .loading
switch appMode {
switch targetMode {
case .mock:
self.appMode = .mock
self.keyClient = .mock
self.lightningClient = .mock
default:
case .live:
self.appMode = .live
self.keyClient = .live
self.lightningClient = .live
@unknown default:
self.appMode = .live
self.keyClient = .live
self.lightningClient = .live
}
}
try await lightningClient.restart()

switch targetMode {
case .mock:
try LightningNodeService.stopAndReleaseShared()
case .live:
_ = try LightningNodeService.rebuildShared(keyService: self.keyClient)
@unknown default:
_ = try LightningNodeService.rebuildShared(keyService: self.keyClient)
}

try await lightningClient.start()
lightningClient.listenForEvents()

await MainActor.run {
self.network = newNetwork
self.server = newServer
self.appState = .wallet

if let lsp = lsp {
if let lsp {
self.lsp = lsp
}
}
Expand Down
6 changes: 4 additions & 2 deletions LDKNodeMonday/Service/KeyService/KeyService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ extension KeyService {
let newBackupInfo = BackupInfo(
mnemonic: currentBackupInfo.mnemonic,
networkString: networkString,
serverURL: currentBackupInfo.serverURL
serverURL: currentBackupInfo.serverURL,
lspString: currentBackupInfo.lspNodeId
)
try self.saveBackupInfo(backupInfo: newBackupInfo)
}
Expand All @@ -65,7 +66,8 @@ extension KeyService {
let newBackupInfo = BackupInfo(
mnemonic: currentBackupInfo.mnemonic,
networkString: currentBackupInfo.networkString,
serverURL: url
serverURL: url,
lspString: currentBackupInfo.lspNodeId
)
try self.saveBackupInfo(backupInfo: newBackupInfo)
}
Expand Down
80 changes: 73 additions & 7 deletions LDKNodeMonday/Service/Lightning Service/LightningNodeService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class LightningNodeService {
var network: Network
var server: EsploraServer
var lsp: LightningServiceProvider
private var eventListenerTask: Task<Void, Never>?

deinit {
cancelEventListenerTask()
}

init(
keyService: KeyClient = .live
Expand Down Expand Up @@ -195,11 +200,59 @@ class LightningNodeService {
}
}

private static func existingInstance() -> LightningNodeService? {
lock.lock()
let instance = _shared
lock.unlock()
return instance
}

static func stopAndReleaseShared() throws {
guard let instance = existingInstance() else { return }
do {
try instance.stop()
} catch let error as NodeError {
if case .NotRunning = error {
instance.cancelEventListenerTask()
} else {
throw error
}
}
stopTrackingSharedInstance()
}

static func rebuildShared(keyService: KeyClient) throws -> LightningNodeService {
if let instance = existingInstance() {
do {
try instance.stop()
} catch let error as NodeError {
if case .NotRunning = error {
instance.cancelEventListenerTask()
} else {
throw error
}
}
}

let service = LightningNodeService(keyService: keyService)
lock.lock()
_shared = service
lock.unlock()
return service
}

private static func stopTrackingSharedInstance() {
lock.lock()
_shared = nil
lock.unlock()
}

func start() async throws {
try ldkNode.start()
}

func stop() throws {
cancelEventListenerTask()
try ldkNode.stop()
}

Expand All @@ -211,8 +264,11 @@ class LightningNodeService {
}

func reset() throws {
if LightningNodeService.shared.status().isRunning {
try LightningNodeService.shared.stop()
if let instance = LightningNodeService.existingInstance() {
if instance.status().isRunning {
try instance.stop()
}
instance.cancelEventListenerTask()
}

// Clean up wallet data to prevent conflicts on next initialization
Expand All @@ -223,7 +279,7 @@ class LightningNodeService {

try? FileManager.default.removeItem(atPath: networkPath)

LightningNodeService._shared = nil
LightningNodeService.stopTrackingSharedInstance()
}

func nodeId() -> String {
Expand Down Expand Up @@ -390,14 +446,17 @@ extension LightningNodeService {

extension LightningNodeService {
func listenForEvents() {
Task {
while true {
let event = await ldkNode.nextEventAsync()
eventListenerTask?.cancel()
eventListenerTask = Task { [weak self] in
guard let self else { return }
while !Task.isCancelled {
let event = await self.ldkNode.nextEventAsync()
if Task.isCancelled { break }
NotificationCenter.default.post(
name: .ldkEventReceived,
object: event
)
try? ldkNode.eventHandled()
try? self.ldkNode.eventHandled()
}
}
}
Expand All @@ -407,6 +466,13 @@ extension LightningNodeService {
}
}

extension LightningNodeService {
fileprivate func cancelEventListenerTask() {
eventListenerTask?.cancel()
eventListenerTask = nil
}
}

extension LightningNodeService {
func save(mnemonic: Mnemonic) throws {
let backupInfo = BackupInfo(
Expand Down