Skip to content

Commit

Permalink
fix: recheck chain explicitelly in case chain switch notification did…
Browse files Browse the repository at this point in the history
…n't fire
  • Loading branch information
Syn-McJ committed Oct 2, 2024
1 parent afa08fe commit 4d6fe66
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions DashWallet/Sources/Models/CoinJoin/CoinJoinService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ class CoinJoinService: NSObject {
private var coinJoinManager: DSCoinJoinManager? = nil
private var hasAnonymizableBalance: Bool = false
private var networkStatus: NetworkStatus = .online

private var workingChain: ChainType

private var chainModeKey: String {
get {
DWEnvironment.sharedInstance().currentChain.isMainnet() ? kCoinJoinMainnetMode : kCoinJoinTestnetMode
Expand All @@ -101,11 +102,13 @@ class CoinJoinService: NSObject {
@Published private(set) var activeSessions: Int = 0

override init() {
workingChain = DWEnvironment.sharedInstance().currentChain.chainType
super.init()

NotificationCenter.default.publisher(for: NSNotification.Name.DWCurrentNetworkDidChange)
.sink { [weak self] _ in
self?.coinJoinManager = nil
DSLogger.log("[SW] CoinJoin: change of network to \(DWEnvironment.sharedInstance().currentChain.chainType.tag), resetting")
self?.workingChain = DWEnvironment.sharedInstance().currentChain.chainType
self?.restoreMode()
}
.store(in: &permanentBag)
Expand Down Expand Up @@ -182,7 +185,7 @@ class CoinJoinService: NSObject {
}

private func createCoinJoinManager() -> DSCoinJoinManager? {
self.coinJoinManager = DSCoinJoinManager.sharedInstance(for: DWEnvironment().currentChain)
self.coinJoinManager = DSCoinJoinManager.sharedInstance(for: DWEnvironment.sharedInstance().currentChain)
coinJoinManager?.managerDelegate = self
return self.coinJoinManager
}
Expand Down Expand Up @@ -238,6 +241,10 @@ class CoinJoinService: NSObject {
networkStatus: NetworkStatus,
chain: DSChain
) {
if !recheckCurrentChain() {
return
}

synchronized(self.updateMutex) {
DSLogger.log("[SW] CoinJoin: \(mode), \(timeSkew) ms, \(hasAnonymizableBalance), \(networkStatus), synced: \(SyncingActivityMonitor.shared.state == .syncDone)")

Expand Down Expand Up @@ -289,11 +296,12 @@ class CoinJoinService: NSObject {
}

private func restoreMode() {
self.stopMixing()
self.coinJoinManager = nil
self.hasAnonymizableBalance = false
self.mode = .none
let mode = CoinJoinMode(rawValue: savedMode) ?? .none

if mode != self.mode {
updateMode(mode: mode)
}
updateMode(mode: mode)
}

private func configureObservers() {
Expand Down Expand Up @@ -357,13 +365,25 @@ extension CoinJoinService: DSCoinJoinManagerDelegate {

DSLogger.log("[SW] CoinJoin: Active sessions: \(activeSessions)")
}

private func recheckCurrentChain() -> Bool {
let chainType = DWEnvironment.sharedInstance().currentChain.chainType

if self.workingChain.tag != chainType.tag {
DSLogger.log("[SW] CoinJoin: reset chain after recheck to type \(chainType.tag)")
self.workingChain = chainType
restoreMode()
return false
}

return true
}
}

extension CoinJoinService: SyncingActivityMonitorObserver {
func syncingActivityMonitorProgressDidChange(_ progress: Double) { }

func syncingActivityMonitorStateDidChange(previousState: SyncingActivityMonitor.State, state: SyncingActivityMonitor.State) {

self.updateState(
balance: DWEnvironment.sharedInstance().currentAccount.balance,
mode: self.mode,
Expand Down

0 comments on commit 4d6fe66

Please sign in to comment.