@@ -153,10 +153,11 @@ class PlatformSynchronizationService @Inject constructor(
153153
154154 val UPDATE_TIMER_DELAY = 15 .seconds
155155 val PUSH_PERIOD = if (BuildConfig .DEBUG || Constants .IS_TESTNET_BUILD ) 3 .minutes else 3 .hours
156- val WEEKLY_PUSH_PERIOD = 7 .days
156+ val WEEKLY_PUSH_PERIOD = 7 .days.inWholeMilliseconds
157157 val CUTOFF_MIN = if (BuildConfig .DEBUG || Constants .IS_TESTNET_BUILD ) 3 .minutes else 3 .hours
158158 val CUTOFF_MAX = if (BuildConfig .DEBUG || Constants .IS_TESTNET_BUILD ) 6 .minutes else 6 .hours
159159 private val PUBLISH = MarkerFactory .getMarker(" PUBLISH" )
160+ val NON_CONTACTS_UPDATE_PERIOD = 1 .minutes.inWholeMilliseconds
160161 }
161162
162163 private var platformSyncJob: Job ? = null
@@ -171,6 +172,8 @@ class PlatformSynchronizationService @Inject constructor(
171172 // TODO: cancel these on shutdown?
172173 private val syncJob = SupervisorJob ()
173174 private val syncScope = CoroutineScope (Dispatchers .IO + syncJob)
175+ private var lastTopupUpdateTime = 0L
176+ private var lastMetadataUpdateTime = 0L
174177
175178 override fun init () {
176179 syncScope.launch { platformRepo.init () }
@@ -234,7 +237,7 @@ class PlatformSynchronizationService @Inject constructor(
234237 val meetsSaveFrequency = when (saveSettings.saveFrequency) {
235238 TxMetadataSaveFrequency .afterTenTransactions -> newDataItems >= 10
236239 TxMetadataSaveFrequency .afterEveryTransaction -> newDataItems >= 1
237- TxMetadataSaveFrequency .oncePerWeek -> lastPush < now - WEEKLY_PUSH_PERIOD .inWholeMilliseconds && newDataItems >= 1
240+ TxMetadataSaveFrequency .oncePerWeek -> lastPush < now - WEEKLY_PUSH_PERIOD && newDataItems >= 1
238241 }
239242 // publish no more frequently than every 3 hours
240243 val shouldPushToNetwork = (lastPush < now - PUSH_PERIOD .inWholeMilliseconds)
@@ -281,7 +284,8 @@ class PlatformSynchronizationService @Inject constructor(
281284 }
282285 log.info(" updateContactRequests($initialSync ) checking if can run" )
283286 // only allow this method to execute once at a time
284- if (updatingContacts.get()) {
287+ // allow it to continue if the last state was recovery complete
288+ if (updatingContacts.get() && lastPreBlockStage != PreBlockStage .RecoveryComplete ) {
285289 log.info(" updateContactRequests is already running: {}" , lastPreBlockStage)
286290 return
287291 }
@@ -361,9 +365,10 @@ class PlatformSynchronizationService @Inject constructor(
361365 updatingContacts.set(true )
362366 updateSyncStatus(PreBlockStage .Starting )
363367 updateSyncStatus(PreBlockStage .Initialization )
364- checkDatabaseIntegrity(userId)
365-
366- updateSyncStatus(PreBlockStage .FixMissingProfiles )
368+ if (! initialSync) {
369+ checkDatabaseIntegrity(userId)
370+ updateSyncStatus(PreBlockStage .FixMissingProfiles )
371+ }
367372
368373 // Get all out our contact requests
369374 val toContactDocuments = platform.contactRequests.get(
@@ -435,29 +440,47 @@ class PlatformSynchronizationService @Inject constructor(
435440 updateSyncStatus(PreBlockStage .GetNewProfiles )
436441
437442 coroutineScope {
443+ try {
444+ val myEncryptionKey = platformRepo.getWalletEncryptionKey()
445+
438446 awaitAll(
439447 // fetch updated invitations
440448 async {
441- updateInvitations()
442- updateSyncStatus(PreBlockStage .GetInvites )
449+ if (Constants .SUPPORTS_INVITES ) {
450+ updateInvitations()
451+ updateSyncStatus(PreBlockStage .GetInvites )
452+ }
443453 },
444454 // fetch updated transaction metadata
445455 async {
446- updateTransactionMetadata()
447- updateSyncStatus(PreBlockStage .TransactionMetadata )
448- }, // TODO: this is skipped in VOTING state, but shouldn't be
456+ val shouldUpdate = System .currentTimeMillis() - lastMetadataUpdateTime >= NON_CONTACTS_UPDATE_PERIOD
457+ if (shouldUpdate) {
458+ updateTransactionMetadata(myEncryptionKey)
459+ updateSyncStatus(PreBlockStage .TransactionMetadata )
460+ lastMetadataUpdateTime = System .currentTimeMillis()
461+ }
462+ }, // TODO: this is skipped in VOTING state, but shouldn't be
449463 // fetch updated profiles from the network
450464 async {
451465 updateContactProfiles(userId, min(lastContactRequestTimeToMe, lastContactRequestTimeFromMe))
452466 updateSyncStatus(PreBlockStage .GetUpdatedProfiles )
453467 },
454468 // check for unused topups
455469 async {
456- checkTopUps()
457- updateSyncStatus(PreBlockStage .Topups )
470+ val shouldUpdate = System .currentTimeMillis() - lastTopupUpdateTime >= NON_CONTACTS_UPDATE_PERIOD
471+ if (shouldUpdate) {
472+ checkTopUps(myEncryptionKey)
473+ updateSyncStatus(PreBlockStage .Topups )
474+ lastTopupUpdateTime = System .currentTimeMillis()
475+ }
458476 }
459477 )
478+ } catch (e: Exception ) {
479+ log.error(" error obtaining encryption key" , e)
480+ return @coroutineScope
481+ }
460482 }
483+
461484 } else {
462485 if (config.get(DashPayConfig .FREQUENT_CONTACTS ) == null ) {
463486 platformRepo.updateFrequentContacts()
@@ -788,12 +811,8 @@ class PlatformSynchronizationService @Inject constructor(
788811 }
789812 }
790813
791- private suspend fun updateTransactionMetadata () {
792- if (! Constants .SUPPORTS_TXMETADATA ) {
793- return
794- }
814+ private suspend fun updateTransactionMetadata (myEncryptionKey : KeyParameter ? ) {
795815 val watch = Stopwatch .createStarted()
796- val myEncryptionKey = platformRepo.getWalletEncryptionKey()
797816
798817 val lastTxMetadataRequestTime = if (transactionMetadataDocumentDao.countAllRequests() > 0 ) {
799818 val lastTimeStamp = transactionMetadataDocumentDao.getLastTimestamp()
@@ -1058,6 +1077,7 @@ class PlatformSynchronizationService @Inject constructor(
10581077
10591078 private suspend fun publishTransactionMetadata (
10601079 txMetadataItems : List <TransactionMetadataCacheItem >,
1080+ myEncryptionKey : KeyParameter ? ,
10611081 progressListener : (suspend (Int ) -> Unit )? = null
10621082 ): Int {
10631083 if (! platformRepo.hasBlockchainIdentity) {
@@ -1085,11 +1105,11 @@ class PlatformSynchronizationService @Inject constructor(
10851105 )
10861106 }
10871107 progressListener?.invoke(10 )
1088- val walletEncryptionKey = platformRepo.getWalletEncryptionKey()
1108+ // val walletEncryptionKey = platformRepo.getWalletEncryptionKey()
10891109 val keyIndex = 1 + transactionMetadataDocumentDao.countAllRequests()
10901110 platformRepo.blockchainIdentity.publishTxMetaData(
10911111 metadataList,
1092- walletEncryptionKey ,
1112+ myEncryptionKey ,
10931113 keyIndex,
10941114 TxMetadataDocument .VERSION_PROTOBUF
10951115 ) { progress ->
@@ -1291,7 +1311,8 @@ class PlatformSynchronizationService @Inject constructor(
12911311 log.info(" publishing ${itemsToPublish.values.size} tx metadata items to platform" )
12921312
12931313 // publish non-empty items
1294- publishTransactionMetadata(itemsToPublish.values.filter { it.isNotEmpty() }) {
1314+ val myEncryptionKey = platformRepo.getWalletEncryptionKey()
1315+ publishTransactionMetadata(itemsToPublish.values.filter { it.isNotEmpty() }, myEncryptionKey) {
12951316 progressListener?.invoke(10 + it * 90 / 100 )
12961317 }
12971318 log.info(" published ${itemsToPublish.values.size} tx metadata items to platform" )
@@ -1302,7 +1323,7 @@ class PlatformSynchronizationService @Inject constructor(
13021323 config.set(DashPayConfig .LAST_METADATA_PUSH , System .currentTimeMillis())
13031324 itemsSaved = changedItems.size
13041325
1305- updateTransactionMetadata()
1326+ updateTransactionMetadata(myEncryptionKey )
13061327 } catch (_: CancellationException ) {
13071328 log.info(" publishing updates canceled" )
13081329 } catch (e: Exception ) {
@@ -1646,12 +1667,10 @@ class PlatformSynchronizationService @Inject constructor(
16461667 }
16471668
16481669 private var hasCheckedTopups = false // only run once
1649- private suspend fun checkTopUps () {
1670+ private suspend fun checkTopUps (myEncryptionKey : KeyParameter ? ) {
16501671 if (! hasCheckedTopups) {
1651- platformRepo.getWalletEncryptionKey()?.let {
1652- topUpRepository.checkTopUps(it)
1653- hasCheckedTopups = true
1654- }
1672+ topUpRepository.checkTopUps(myEncryptionKey)
1673+ hasCheckedTopups = true
16551674 }
16561675 }
16571676}
0 commit comments