Skip to content

Commit

Permalink
fix: use wallet way of detecting sync state
Browse files Browse the repository at this point in the history
  • Loading branch information
Syn-McJ committed Sep 29, 2024
1 parent f9744cd commit 1f56b0c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
8 changes: 4 additions & 4 deletions DashWallet.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10747,7 +10747,7 @@
CLIENT_ID = 0c38beb67db0c68191326be347d7ec0abd7d77adb02a79db1abeba343f16a0f7;
CLIENT_SECRET = cc980185754f905e24250f877792817c03540b3d0e0959721df291c816797e59;
CODE_SIGN_ENTITLEMENTS = dashwallet/dashwallet.entitlements;
CURRENT_PROJECT_VERSION = 165;
CURRENT_PROJECT_VERSION = 170;
DEVELOPMENT_TEAM = 44RJ69WHFF;
EXCLUDED_ARCHS = "";
"EXCLUDED_ARCHS[sdk=iphonesimulator*]" = "";
Expand Down Expand Up @@ -10883,7 +10883,7 @@
CLIENT_ID = 0c38beb67db0c68191326be347d7ec0abd7d77adb02a79db1abeba343f16a0f7;
CLIENT_SECRET = cc980185754f905e24250f877792817c03540b3d0e0959721df291c816797e59;
CODE_SIGN_ENTITLEMENTS = dashwallet/dashwallet.entitlements;
CURRENT_PROJECT_VERSION = 165;
CURRENT_PROJECT_VERSION = 170;
DEVELOPMENT_TEAM = 44RJ69WHFF;
EXCLUDED_ARCHS = "";
GCC_PRECOMPILE_PREFIX_HEADER = YES;
Expand Down Expand Up @@ -11018,7 +11018,7 @@
CLIENT_ID = 0c38beb67db0c68191326be347d7ec0abd7d77adb02a79db1abeba343f16a0f7;
CLIENT_SECRET = cc980185754f905e24250f877792817c03540b3d0e0959721df291c816797e59;
CODE_SIGN_ENTITLEMENTS = dashwallet/dashwallet.entitlements;
CURRENT_PROJECT_VERSION = 165;
CURRENT_PROJECT_VERSION = 170;
DEVELOPMENT_TEAM = 44RJ69WHFF;
EXCLUDED_ARCHS = "";
GCC_PRECOMPILE_PREFIX_HEADER = YES;
Expand Down Expand Up @@ -11163,7 +11163,7 @@
CLIENT_ID = 0c38beb67db0c68191326be347d7ec0abd7d77adb02a79db1abeba343f16a0f7;
CLIENT_SECRET = cc980185754f905e24250f877792817c03540b3d0e0959721df291c816797e59;
CODE_SIGN_ENTITLEMENTS = dashwallet/dashwallet.entitlements;
CURRENT_PROJECT_VERSION = 165;
CURRENT_PROJECT_VERSION = 170;
DEVELOPMENT_TEAM = 44RJ69WHFF;
EXCLUDED_ARCHS = "";
GCC_PRECOMPILE_PREFIX_HEADER = YES;
Expand Down
53 changes: 42 additions & 11 deletions DashWallet/Sources/Models/CoinJoin/CoinJoinService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ class CoinJoinService: NSObject {

if (mode != .none && self.mode == .none) {
configureMixing(amount: balance)
configureObservers()
} else if mode == .none {
removeObservers()
}

updateBalance(balance: balance)
Expand All @@ -117,9 +120,7 @@ class CoinJoinService: NSObject {
} else {
coinJoinManager.refreshUnusedKeys()
coinJoinManager.initMasternodeGroup()
coinJoinManager.doAutomaticDenominating()

DSLogger.log("[SW] CoinJoin: Mixing \(coinJoinManager.startMixing() ? "started successfully" : "start failed, will retry")") // TODO: failed statuses: \(coinJoinManager.statuses)
coinJoinManager.doAutomaticDenominating(withReport: true)
}
}

Expand Down Expand Up @@ -153,12 +154,6 @@ class CoinJoinService: NSObject {
return self.coinJoinManager
}

private func synchronized(_ lock: NSLock, closure: () -> Void) {
lock.lock()
defer { lock.unlock() }
closure()
}

private func updateBalance(balance: UInt64) {
guard let coinJoinManager = self.coinJoinManager else { return }

Expand Down Expand Up @@ -211,7 +206,7 @@ class CoinJoinService: NSObject {
chain: DSChain
) {
synchronized(self.updateMutex) {
DSLogger.log("[SW] CoinJoin: \(mode), \(timeSkew) ms, \(hasAnonymizableBalance), \(networkStatus), synced: \(chain.chainManager!.isSynced)")
DSLogger.log("[SW] CoinJoin: \(mode), \(timeSkew) ms, \(hasAnonymizableBalance), \(networkStatus), synced: \(SyncingActivityMonitor.shared.state == .syncDone)")

self.networkStatus = networkStatus
self.hasAnonymizableBalance = hasAnonymizableBalance
Expand All @@ -224,7 +219,7 @@ class CoinJoinService: NSObject {
configureMixing(amount: balance)

if hasAnonymizableBalance {
if networkStatus == .online && chain.chainManager!.isSynced {
if networkStatus == .online && SyncingActivityMonitor.shared.state == .syncDone {
updateMixingState(state: .mixing)
} else {
updateMixingState(state: .paused)
Expand Down Expand Up @@ -259,6 +254,27 @@ class CoinJoinService: NSObject {
}
}
}

private func configureObservers() {
NotificationCenter.default.publisher(for: NSNotification.Name.DSWalletBalanceDidChange)
.sink { [weak self] _ in
self?.updateBalance(balance: DWEnvironment.sharedInstance().currentAccount.balance)
}
.store(in: &cancellableBag)

SyncingActivityMonitor.shared.add(observer: self)
}

private func removeObservers() {
cancellableBag.removeAll()
SyncingActivityMonitor.shared.remove(observer: self)
}

private func synchronized(_ lock: NSLock, closure: () -> Void) {
lock.lock()
defer { lock.unlock() }
closure()
}
}

extension CoinJoinService: DSCoinJoinManagerDelegate {
Expand Down Expand Up @@ -296,3 +312,18 @@ extension CoinJoinService: DSCoinJoinManagerDelegate {
}
}

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,
timeSkew: TimeInterval(0), // TODO
hasAnonymizableBalance: self.hasAnonymizableBalance,
networkStatus: self.networkStatus,
chain: DWEnvironment.sharedInstance().currentChain
)
}
}

0 comments on commit 1f56b0c

Please sign in to comment.