Repository navigation
fix: keep allowance history across a wallet restore #1708
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
17
commits into
master
Choose a base branch
from
fix/allowance-backup
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.
+715
−21
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f5f3c97
fix: keep allowance history across a wallet restore
ovitrif 24a46e6
fix: restore an allowance backup that omits optional fields
ovitrif da79536
fix: never back up unreadable allowance state as empty
ovitrif 8c81493
fix: rebuild auto-paid totals from the allowance ledger on recovery
ovitrif 652a95c
fix: count an automatic allowance payment once across journal and ledger
ovitrif 2af0313
fix: let the node settle a restored allowance attempt
ovitrif 2b45d6b
docs: add a journey for allowance history after a restore
ovitrif 7b28fa0
Merge remote-tracking branch 'origin/fix/allowance-follow-ups' into f…
ovitrif 411385d
fix: wait for the node before settling a restored allowance attempt
ovitrif ca13ac0
Merge remote-tracking branch 'origin/fix/allowance-follow-ups' into f…
ovitrif fbbfcfc
fix: keep a payment list's owner in the wallet backup
ovitrif ab457ea
Merge remote-tracking branch 'origin/fix/allowance-follow-ups' into f…
ovitrif 67b3756
Merge remote-tracking branch 'origin/fix/allowance-follow-ups' into f…
ovitrif d6ba1c9
fix: write allowance journal stages in ascii
ovitrif bc5f8ef
Merge remote-tracking branch 'origin/fix/allowance-follow-ups' into f…
ovitrif f0eaf48
fix: journal a manual lightning hash before the send
ovitrif 0f06291
Merge branch 'master' into fix/allowance-backup
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
159 changes: 159 additions & 0 deletions
159
app/src/main/java/to/bitkit/models/PaykitAllowanceStateBackup.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,159 @@ | ||
| package to.bitkit.models | ||
|
|
||
| import kotlinx.serialization.Serializable | ||
| import to.bitkit.repositories.PaykitAllowance | ||
| import to.bitkit.repositories.PaykitAllowanceLimits | ||
| import to.bitkit.repositories.PaykitAllowanceLocalState | ||
| import to.bitkit.repositories.PaykitPaymentRequestId | ||
| import java.math.BigDecimal | ||
| import java.time.ZoneOffset | ||
| import java.time.format.DateTimeFormatter | ||
| import java.util.Locale | ||
| import kotlin.time.Instant | ||
| import java.time.Instant as JavaInstant | ||
|
|
||
| /** | ||
| * One identity's allowance state in the wallet backup, in the wire format both apps share: dates are RFC 3339 strings | ||
| * with milliseconds and stages are lowercase. A restored wallet keeps its journal, so recovery can still settle open | ||
| * attempts, and its automatic-payment history. | ||
| */ | ||
| @Serializable | ||
| data class PaykitAllowanceStateBackup( | ||
| val groups: List<Group> = emptyList(), | ||
| val journal: List<JournalEntry> = emptyList(), | ||
| val autoPaidSatsByAllowance: Map<String, ULong> = emptyMap(), | ||
| val autoPaidRequestIds: Set<PaykitPaymentRequestId> = emptySet(), | ||
| val presentedProposalIds: Set<String> = emptySet(), | ||
| val unannouncedOfferIds: Set<String> = emptySet(), | ||
| val notifiedRequestIds: Set<String> = emptySet(), | ||
| val lastTrustedTime: String? = null, | ||
| ) { | ||
| @Serializable | ||
| data class Group( | ||
| val id: String, | ||
| val counterparty: String, | ||
| val limits: Limits, | ||
| val allowanceIds: List<String>, | ||
| val createdAt: String, | ||
| ) | ||
|
|
||
| /** US dollar limits travel as decimal strings, as iOS keeps them. */ | ||
| @Serializable | ||
| data class Limits( | ||
| val perPaymentUsd: String, | ||
| val monthlyUsd: String, | ||
| val perPaymentSats: ULong, | ||
| val monthlySats: ULong, | ||
| ) | ||
|
|
||
| @Serializable | ||
| data class JournalEntry( | ||
| val attemptId: String, | ||
| val isAutomatic: Boolean, | ||
| val requestId: PaykitPaymentRequestId, | ||
| val allowanceId: String? = null, | ||
| val amountSats: ULong, | ||
| val paymentEndpointIdentifier: String, | ||
| val paymentHash: String? = null, | ||
| val onchainAddress: String? = null, | ||
| val transactionId: String? = null, | ||
| val stage: String, | ||
| val createdAt: String, | ||
| val paymentListVersion: ULong? = null, | ||
| val previousPaymentListVersion: ULong? = null, | ||
| ) | ||
|
|
||
| constructor(state: PaykitAllowanceLocalState) : this( | ||
| groups = state.groups.map { | ||
| val limits = Limits( | ||
| perPaymentUsd = it.limits.perPaymentUsd.toString(), | ||
| monthlyUsd = it.limits.monthlyUsd.toString(), | ||
| perPaymentSats = it.limits.perPaymentSats, | ||
| monthlySats = it.limits.monthlySats, | ||
| ) | ||
| Group(it.id, it.counterparty, limits, it.allowanceIds, isoTime(it.createdAtMillis)) | ||
| }, | ||
| journal = state.journal.map { | ||
| JournalEntry( | ||
| attemptId = it.attemptId, | ||
| isAutomatic = it.isAutomatic, | ||
| requestId = it.requestId, | ||
| allowanceId = it.allowanceId, | ||
| amountSats = it.amountSats, | ||
| paymentEndpointIdentifier = it.paymentEndpointIdentifier, | ||
| paymentHash = it.paymentHash, | ||
| onchainAddress = it.onchainAddress, | ||
| transactionId = it.transactionId, | ||
| stage = it.stage.name.lowercase(Locale.ROOT), | ||
| createdAt = isoTime(it.createdAtMillis), | ||
| paymentListVersion = it.paymentListVersion, | ||
| previousPaymentListVersion = it.previousPaymentListVersion, | ||
| ) | ||
| }, | ||
| autoPaidSatsByAllowance = state.autoPaidSatsByAllowance, | ||
| autoPaidRequestIds = state.autoPaidRequestIds, | ||
| presentedProposalIds = state.presentedProposalIds, | ||
| unannouncedOfferIds = state.acceptingOfferIds, | ||
| notifiedRequestIds = state.notifiedRequestIds, | ||
| lastTrustedTime = state.lastTrustedTimeMillis?.let(::isoTime), | ||
| ) | ||
|
|
||
| fun restored(): PaykitAllowanceLocalState { | ||
| val state = PaykitAllowanceLocalState( | ||
| groups = groups.map { | ||
| val limits = PaykitAllowanceLimits( | ||
| perPaymentUsd = BigDecimal(it.limits.perPaymentUsd).toInt(), | ||
| monthlyUsd = BigDecimal(it.limits.monthlyUsd).toInt(), | ||
| perPaymentSats = it.limits.perPaymentSats, | ||
| monthlySats = it.limits.monthlySats, | ||
| ) | ||
| PaykitAllowanceLocalState.Group(it.id, it.counterparty, limits, it.allowanceIds, millis(it.createdAt)) | ||
| }, | ||
| journal = journal.map { | ||
| PaykitAllowanceLocalState.JournalEntry( | ||
| attemptId = it.attemptId, | ||
| isAutomatic = it.isAutomatic, | ||
| requestId = it.requestId, | ||
| allowanceId = it.allowanceId, | ||
| amountSats = it.amountSats, | ||
| paymentEndpointIdentifier = it.paymentEndpointIdentifier, | ||
| paymentHash = it.paymentHash, | ||
| onchainAddress = it.onchainAddress, | ||
| transactionId = it.transactionId, | ||
| // A stage this version does not know stays open, so recovery settles it from the node. | ||
| stage = stage(it.stage), | ||
| createdAtMillis = millis(it.createdAt), | ||
| paymentListVersion = it.paymentListVersion, | ||
| previousPaymentListVersion = it.previousPaymentListVersion, | ||
| ) | ||
| }, | ||
| presentedProposalIds = presentedProposalIds, | ||
| acceptingOfferIds = unannouncedOfferIds, | ||
| notifiedRequestIds = notifiedRequestIds, | ||
| lastTrustedTimeMillis = lastTrustedTime?.let(::millis), | ||
| autoPaidSatsByAllowance = autoPaidSatsByAllowance, | ||
| autoPaidRequestIds = autoPaidRequestIds, | ||
| ) | ||
| if (autoPaidSatsByAllowance.isNotEmpty() || autoPaidRequestIds.isNotEmpty()) return state | ||
| // A backup written by iOS keeps no lasting totals, so they are rebuilt from the restored journal. | ||
| val paid = state.journal.filter { it.isAutomatic && it.stage == PaykitAllowanceLocalState.Stage.SUCCEEDED } | ||
| return state.copy( | ||
| autoPaidSatsByAllowance = paid | ||
| .groupBy { PaykitAllowance.key(it.requestId.counterparty, it.allowanceId) } | ||
| .mapValues { (_, payments) -> | ||
| payments.fold(0uL) { total, payment -> total.safe() + payment.amountSats.safe() } | ||
| }, | ||
| autoPaidRequestIds = paid.mapTo(mutableSetOf()) { it.requestId }, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| private fun stage(value: String): PaykitAllowanceLocalState.Stage = | ||
| PaykitAllowanceLocalState.Stage.entries.firstOrNull { it.name.equals(value, ignoreCase = true) } | ||
| ?: PaykitAllowanceLocalState.Stage.UNKNOWN | ||
|
|
||
| private val isoTimeFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").withZone(ZoneOffset.UTC) | ||
|
|
||
| private fun isoTime(millis: Long): String = isoTimeFormat.format(JavaInstant.ofEpochMilli(millis)) | ||
|
|
||
| private fun millis(isoTime: String): Long = Instant.parse(isoTime).toEpochMilliseconds() | ||
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
Oops, something went wrong.
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.