Repository navigation
perf: load watch-only accounts after node start #2248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ovitrif
wants to merge
4
commits into
master
Choose a base branch
from
perf/watch-only-accounts-after-start
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+444
−308
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b399f25
perf: load watch-only accounts after node start
ovitrif a84d871
docs: run app-close-proof network work in workers
ovitrif c98fc07
fix: load watch-only accounts without a foreground notice
ovitrif 6f2d58d
fix: queue the account load as soon as the node runs
ovitrif File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
app/src/main/java/to/bitkit/repositories/WatchOnlyAccountsWorker.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| package to.bitkit.repositories | ||
|
|
||
| import android.content.Context | ||
| import androidx.hilt.work.HiltWorker | ||
| import androidx.work.BackoffPolicy | ||
| import androidx.work.Constraints | ||
| import androidx.work.CoroutineWorker | ||
| import androidx.work.ExistingWorkPolicy | ||
| import androidx.work.NetworkType | ||
| import androidx.work.OneTimeWorkRequestBuilder | ||
| import androidx.work.WorkManager | ||
| import androidx.work.WorkerParameters | ||
| import dagger.assisted.Assisted | ||
| import dagger.assisted.AssistedInject | ||
| import dagger.hilt.android.qualifiers.ApplicationContext | ||
| import to.bitkit.ext.runSuspendCatching | ||
| import to.bitkit.services.LightningService | ||
| import to.bitkit.services.isRejectedPaykitServerAccount | ||
| import to.bitkit.utils.Logger | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
| import kotlin.time.Duration.Companion.seconds | ||
| import kotlin.time.toJavaDuration | ||
|
|
||
| /** | ||
| * Queues [WatchOnlyAccountsWorker] after each node start. The node is built without the Paykit Server accounts, so its | ||
| * start does not grow with their number; a worker loads them, which WorkManager keeps when the app closes. | ||
| */ | ||
| @Singleton | ||
| class WatchOnlyAccountsWork @Inject constructor( | ||
| @ApplicationContext private val context: Context, | ||
| ) { | ||
| companion object { | ||
| const val WORK_NAME = "watch-only-accounts-load" | ||
| private val RETRY_DELAY = 15.seconds | ||
| } | ||
|
|
||
| fun schedule() { | ||
| // A new start replaces the work of the node before it, which waits in backoff or loads into a stopped node. | ||
| WorkManager.getInstance(context).enqueueUniqueWork(WORK_NAME, ExistingWorkPolicy.REPLACE, request()) | ||
| } | ||
|
|
||
| internal fun request() = OneTimeWorkRequestBuilder<WatchOnlyAccountsWorker>() | ||
| .setConstraints(Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build()) | ||
| // A transient failure (VSS, Electrum) retries soon after it clears, not after an exponential backoff. | ||
| .setBackoffCriteria(BackoffPolicy.LINEAR, RETRY_DELAY.toJavaDuration()) | ||
| .build() | ||
| } | ||
|
|
||
| /** Loads the Paykit Server accounts into the running node, then syncs the wallet so their funds and activity show. */ | ||
| @HiltWorker | ||
| class WatchOnlyAccountsWorker @AssistedInject constructor( | ||
| @Assisted appContext: Context, | ||
| @Assisted workerParams: WorkerParameters, | ||
| private val lightningService: LightningService, | ||
| private val walletRepo: WalletRepo, | ||
| ) : CoroutineWorker(appContext, workerParams) { | ||
| companion object { | ||
| private const val TAG = "WatchOnlyAccountsWorker" | ||
| } | ||
|
|
||
| override suspend fun doWork(): Result { | ||
| // The next start queues this work again. | ||
| if (lightningService.status?.isRunning != true) { | ||
| Logger.info("Skipped loading Paykit Server accounts: node not running", context = TAG) | ||
| return Result.success() | ||
|
ovitrif marked this conversation as resolved.
Resolved
|
||
| } | ||
| // The service logs each account that fails to load. | ||
| val result = runSuspendCatching { lightningService.reconcileWatchOnlyAccounts(syncAfterReconcile = false) } | ||
| val failure = result.exceptionOrNull() | ||
| when { | ||
| failure != null && !failure.isRejectedPaykitServerAccount() -> { | ||
| Logger.info("Retrying Paykit Server accounts that failed to load", context = TAG) | ||
| return Result.retry() | ||
| } | ||
| failure != null -> Logger.info("Skipped Paykit Server accounts the node rejects", context = TAG) | ||
| result.getOrNull() != true -> { | ||
| Logger.info("Skipped loading Paykit Server accounts: node stopped", context = TAG) | ||
| return Result.success() | ||
| } | ||
| else -> Logger.info("Loaded Paykit Server accounts", context = TAG) | ||
| } | ||
| walletRepo.syncNodeAndWallet().onFailure { | ||
| Logger.warn("Failed to sync wallet after loading Paykit Server accounts", it, context = TAG) | ||
| } | ||
|
ovitrif marked this conversation as resolved.
Resolved
|
||
| return Result.success() | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.