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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import Combine
import Foundation
import OSLog
import SwiftDashSDK

private let kMaxProgressDelta = 0.1 // 10%
Expand Down Expand Up @@ -178,7 +179,20 @@ class SyncingActivityMonitor: NSObject, NetworkReachabilityHandling {
private var lastPeakDate: Date?
private var cancellables = Set<AnyCancellable>()

private var observers: [SyncingActivityMonitorObserver] = []
/// The wallet the in-flight sync cycle started on. `.syncDone` arrives
/// from a singleton SPV stream that carries no wallet identity, while the
/// durable watermark is read from whichever wallet is active at log time —
/// so the completion is pinned to the wallet its cycle began on.
private var syncCycleWalletId: Data?
Comment thread
romchornyi marked this conversation as resolved.
Resolved

/// Weak, because this is a singleton that outlives every observer and a
/// strong array here turns any missed `remove(observer:)` into an
/// unbounded leak — one did, silently, until a large-wallet scan had
/// accumulated 3321 live `SyncModelImpl`s and the per-tick fan-out over
/// them became the dominant cost. Every observer is owned by whoever
/// created it (a view model, a `UIView`, a `@StateObject`), so the monitor
/// has no reason to keep any of them alive.
private let observers = NSHashTable<AnyObject>.weakObjects()
private let observersLock = NSLock()

override init() {
Expand All @@ -199,22 +213,21 @@ class SyncingActivityMonitor: NSObject, NetworkReachabilityHandling {
public func add(observer: SyncingActivityMonitorObserver) {
observersLock.lock()
defer { observersLock.unlock() }
observers.append(observer)
observers.add(observer)
}

@objc(removeObserver:)
public func remove(observer: SyncingActivityMonitorObserver) {
observersLock.lock()
defer { observersLock.unlock() }
if let idx = observers.firstIndex(where: { $0 === observer }) {
observers.remove(at: idx)
}
observers.remove(observer)
}

private func observerSnapshot() -> [SyncingActivityMonitorObserver] {
observersLock.lock()
defer { observersLock.unlock() }
return observers
// `allObjects` already drops entries whose object has gone away.
return observers.allObjects.compactMap { $0 as? SyncingActivityMonitorObserver }
}

deinit {
Expand Down Expand Up @@ -304,9 +317,102 @@ extension SyncingActivityMonitor {

applyProgressWithPeakSmoothing(sdkProgress)
isSyncing = (mapped == .syncing)
let wasDone = state == .syncDone
let wasSyncing = state == .syncing
state = mapped

// `syncDone` is derived purely from the SPV network phases — it knows
// nothing about how much of what was scanned is durably persisted.
// The two can be far apart: the durable watermark is what a relaunch
// resumes from, and what the transaction list is built out of, so a
// wallet can report "synced" while rows are still materializing.
//
// Logged at the transition rather than gated on, because whether the
// watermark reliably reaches the tip is exactly what is unproven. One
// line per completion answers it from an ordinary session.
// Every entry into `.syncing` opens a new cycle, so the id is replaced
// rather than only filled: a cycle that ended in `.syncFailed` or
// `.noConnection` never reaches the `.syncDone` branch that clears it,
// and a stale id would make the next cycle's completion look like it
// belonged to a wallet that is no longer active.
if mapped == .syncing && !wasSyncing {
syncCycleWalletId = SwiftDashSDKWalletSource.activeWalletId
}

if mapped == .syncDone && !wasDone {
logDurableWatermarkAtCompletion(
scannedTip: sdkSyncProgress.headers?.currentHeight,
cycleWalletId: syncCycleWalletId)
syncCycleWalletId = nil
}
}

/// One-shot read of the persisted sync height at the moment the UI first
/// calls a sync complete, next to the height that was actually scanned.
/// A single fetch on a state transition — not a per-tick cost.
private func logDurableWatermarkAtCompletion(scannedTip: UInt32?, cycleWalletId: Data?) {
let tip = scannedTip.map(String.init) ?? "unavailable"

guard let snapshot = SwiftDashSDKWalletSource.persistedSyncedHeightSnapshot() else {
Self.logger.warning(
"""
⛓️ SYNCSTATE :: reported done at tip \(tip, privacy: .public); \
no active wallet to read a durable watermark from
""")
return
}

// A completion queued before a wallet switch would otherwise be
// measured against the wallet that replaced it.
if let cycleWalletId, snapshot.walletId != cycleWalletId {
Self.logger.warning(
"""
⛓️ SYNCSTATE :: reported done at tip \(tip, privacy: .public) for a wallet that is \
no longer active; durable watermark not read
""")
return
}

guard let durable = snapshot.height else {
Self.logger.warning(
"""
⛓️ SYNCSTATE :: reported done at tip \(tip, privacy: .public); \
durable watermark unavailable
""")
return
}

guard let scannedTip else {
Self.logger.warning(
"""
⛓️ SYNCSTATE :: reported done with an unavailable scanned tip; \
durable watermark \(durable, privacy: .public)
""")
return
}

// `durable > scannedTip` is not "caught up": the persister is ahead of
// the height the scan reports, which is itself worth seeing.
guard scannedTip >= durable else {
Self.logger.warning(
"""
⛓️ SYNCSTATE :: reported done — scanned tip \(scannedTip, privacy: .public), \
durable watermark \(durable, privacy: .public), watermark AHEAD by \
\(durable - scannedTip, privacy: .public) block(s)
""")
return
}

Self.logger.info(
"""
⛓️ SYNCSTATE :: reported done — scanned tip \(scannedTip, privacy: .public), \
durable watermark \(durable, privacy: .public), behind by \
\(scannedTip - durable, privacy: .public) block(s)
""")
}

private static let logger = Logger(subsystem: "org.dashfoundation.dash", category: "sync-state")

/// Map SwiftDashSDK's per-phase progress to the snapshot fields the
/// existing UI consumers expect. The phase priority order
/// (headers → filterHeaders → filters → masternodes → finished)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,21 @@ public final class SwiftDashSDKSPVCoordinator: NSObject, ObservableObject {

@MainActor
private func applyProgress(_ p: PlatformSpvSyncProgress) {
// Everything below runs on the main actor, once per progress tick
// (~1Hz for as long as any sync phase advances), and reaches
// synchronous FFI and a main-context SwiftData fetch. Nothing here has
// ever been timed, so a uniform app-wide stutter during sync has no
// way of being attributed. Measured here rather than assumed.
// Monotonic: a wall-clock adjustment mid-tick would otherwise turn a
// fast tick into a fabricated stall, or hide a real one.
let tickStartedAt = DispatchTime.now()
defer {
let heldMs = Int(DispatchTime.now().uptimeNanoseconds - tickStartedAt.uptimeNanoseconds) / 1_000_000
if heldMs >= 50 {
Self.logger.warning(
"🛰️ SPVCOORD :: progress tick held the main thread \(heldMs, privacy: .public)ms")
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
let mappedState = mapState(p.overallState)
let translated = SPVSyncProgress(
state: mappedState,
Expand Down Expand Up @@ -603,6 +618,17 @@ public final class SwiftDashSDKSPVCoordinator: NSObject, ObservableObject {
/// publisher was removed in the SDK refactor, so the bridge replaces it.
@MainActor
private func refreshBalanceBridge() {
// Timed separately from the tick: this is the FFI half, and knowing
// which half is expensive decides whether the fix is throttling the
// tick or moving the read off the main actor.
let startedAt = DispatchTime.now()
defer {
let ms = Int(DispatchTime.now().uptimeNanoseconds - startedAt.uptimeNanoseconds) / 1_000_000
if ms >= 50 {
Self.logger.warning(
"🛰️ SPVCOORD :: balance bridge held the main thread \(ms, privacy: .public)ms")
}
}
guard let wallet = SwiftDashSDKHost.shared.wallet else {
SwiftDashSDKWalletState.shared.clearBalance()
return
Expand Down
11 changes: 10 additions & 1 deletion DashWallet/Sources/UI/Home/Views/Cells/SyncingHeaderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ import DashUIKit
// MARK: - SyncingHeaderView

struct SyncingHeaderView: View {
@ObservedObject private var model = SyncModelImpl()
// `@StateObject`, not `@ObservedObject`: this is the transaction list's
// section header, so SwiftUI re-initializes the struct on every list
// update — and during a sync those never stop. `@ObservedObject` does not
// own its value, so each re-init built another `SyncModelImpl`, whose
// `init` registers with `SyncingActivityMonitor.shared` and with
// `NotificationCenter`. Both hold it, `deinit` never ran, and the monitor
// then called every accumulated instance on each progress tick — each one
// republishing and invalidating this view again. A large-wallet scan
// reached 3321 live instances that way.
@StateObject private var model = SyncModelImpl()
var onFilterTap: () -> Void
var onSyncTap: () -> Void

Expand Down
8 changes: 7 additions & 1 deletion DashWallet/Sources/UI/Home/Views/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,13 @@ struct HomeViewContent<Content: View>: View {
#endif

@ObservedObject var viewModel: HomeViewModel
@ObservedObject private var balanceModel = BalanceModel()
// Owned here, so `@StateObject`: `BalanceModel.init` registers with
// `SyncingActivityMonitor.shared`, which holds its observers strongly, so
// an instance built by a struct re-init can never be released. `HomeView`
// is re-created far less often than the list header that hit this hard
// (see `SyncingHeaderView`), but four live models were still found in a
// single session.
@StateObject private var balanceModel = BalanceModel()
#if DASHPAY
@ObservedObject var joinDPViewModel: JoinDashPayViewModel
#endif
Expand Down
Loading
Loading