Add BlockTime to SDKSynchronizer updates

This commit is contained in:
Francisco Gindre 2021-05-11 19:27:22 -03:00
parent 90c7a4d755
commit 0214131bde
4 changed files with 38 additions and 8 deletions

View File

@ -34,6 +34,7 @@ public enum CompactBlockProcessorError: Error {
public struct CompactBlockProcessorNotificationKey {
public static let progress = "CompactBlockProcessorNotificationKey.progress"
public static let progressHeight = "CompactBlockProcessorNotificationKey.progressHeight"
public static let progressBlockTime = "CompactBlockProcessorNotificationKey.progressBlockTime"
public static let reorgHeight = "CompactBlockProcessorNotificationKey.reorgHeight"
public static let latestScannedBlockHeight = "CompactBlockProcessorNotificationKey.latestScannedBlockHeight"
public static let rewindHeight = "CompactBlockProcessorNotificationKey.rewindHeight"
@ -603,11 +604,21 @@ public class CompactBlockProcessor {
func notifyProgress(completedRange: CompactBlockRange) {
let progress = calculateProgress(start: self.lowerBoundHeight ?? config.walletBirthday, current: completedRange.upperBound, latest: self.latestBlockHeight)
LoggerProxy.debug("\(self) progress: \(progress)")
var userInfo = [AnyHashable : Any]()
userInfo[CompactBlockProcessorNotificationKey.progress] = progress
userInfo[CompactBlockProcessorNotificationKey.progressHeight] = completedRange.upperBound
if let blockTime = try? transactionRepository.blockForHeight(completedRange.upperBound)?.time {
userInfo[CompactBlockProcessorNotificationKey.progressBlockTime] = TimeInterval(blockTime)
}
LoggerProxy.debug("""
progress: \(progress)
height: \(completedRange.upperBound)
""")
NotificationCenter.default.post(name: Notification.Name.blockProcessorUpdated,
object: self,
userInfo: [ CompactBlockProcessorNotificationKey.progress : progress,
CompactBlockProcessorNotificationKey.progressHeight : completedRange.upperBound])
userInfo: userInfo)
}
func notifyTransactions(_ txs: [ConfirmedTransactionEntity], in range: BlockRange) {

View File

@ -46,6 +46,10 @@ class TransactionSQLDAO: TransactionRepository {
private var blockDao: BlockSQLDAO
func blockForHeight(_ height: BlockHeight) throws -> Block? {
try blockDao.block(at: height)
}
func lastScannedHeight() throws -> BlockHeight {
try blockDao.latestBlockHeight()
}

View File

@ -14,6 +14,7 @@ enum TransactionRepositoryError: Error {
protocol TransactionRepository {
func countAll() throws -> Int
func countUnmined() throws -> Int
func blockForHeight(_ height: BlockHeight) throws -> Block?
func findBy(id: Int) throws -> TransactionEntity?
func findBy(rawId: Data) throws -> TransactionEntity?
func findAllSentTransactions(offset: Int, limit: Int) throws -> [ConfirmedTransactionEntity]?

View File

@ -74,9 +74,11 @@ public class SDKSynchronizer: Synchronizer {
public struct NotificationKeys {
public static let progress = "SDKSynchronizer.progress"
public static let blockHeight = "SDKSynchronizer.blockHeight"
public static let blockDate = "SDKSynchronizer.blockDate"
public static let minedTransaction = "SDKSynchronizer.minedTransaction"
public static let foundTransactions = "SDKSynchronizer.foundTransactions"
public static let error = "SDKSynchronizer.error"
}
public private(set) var status: Status {
@ -265,9 +267,16 @@ public class SDKSynchronizer: Synchronizer {
let height = userInfo[CompactBlockProcessorNotificationKey.progressHeight] as? BlockHeight else {
return
}
var blockDate: Date? = nil
if let time = userInfo[CompactBlockProcessorNotificationKey.progressBlockTime] as? TimeInterval {
blockDate = Date(timeIntervalSince1970: time)
}
self.progress = progress
self.notify(progress: progress, height: height)
self.notify(progress: progress,
height: height,
time: blockDate)
}
@objc func processorStartedDownloading(_ notification: Notification) {
@ -558,10 +567,15 @@ public class SDKSynchronizer: Synchronizer {
}
// MARK: notify state
private func notify(progress: Float, height: BlockHeight) {
NotificationCenter.default.post(name: Notification.Name.synchronizerProgressUpdated, object: self, userInfo: [
NotificationKeys.progress : progress,
NotificationKeys.blockHeight : height])
private func notify(progress: Float, height: BlockHeight, time: Date?) {
var userInfo = [AnyHashable : Any]()
userInfo[NotificationKeys.progress] = progress
userInfo[NotificationKeys.blockHeight] = height
if let blockDate = time {
userInfo[NotificationKeys.blockDate] = blockDate
}
NotificationCenter.default.post(name: Notification.Name.synchronizerProgressUpdated, object: self, userInfo: userInfo)
}
private func notify(status: Status) {