Repository navigation
fix: release savings payments a force transfer no longer needs #2142
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
176a38c
fix: release savings payments a force transfer no longer needs
ovitrif 5f91841
chore: changelog for the released force transfer payment
ovitrif 54604e9
fix: release exit funding once other funding filled the exit
ovitrif 8a16f21
fix: keep an approved exit funding release owned by Send Pending
ovitrif aee5d77
fix: retry exit funding from send pending while the exit asks for it
ovitrif 2afb912
fix: never pay an approved exit funding release again
ovitrif 40d4816
Merge remote-tracking branch 'origin/master' into fix/spark-exit-rele…
ovitrif 378c2e2
fix: retry a savings payment after a partial payment from elsewhere
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
89 changes: 89 additions & 0 deletions
89
app/src/main/java/to/bitkit/repositories/SparkExitFundingRelease.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,89 @@ | ||
| package to.bitkit.repositories | ||
|
|
||
| import to.bitkit.env.Env | ||
| import java.io.File | ||
|
|
||
| /** | ||
| * A savings send to a Spark exit's funding address, told apart by that address alone. Once the exit no longer needs | ||
| * it, the send can be released like a Shop payment whose request ended, instead of blocking every new send. | ||
| */ | ||
| object SparkExitFundingRelease { | ||
| /** The exit state the checks read: the wallet's record and its earlier exits' addresses. */ | ||
| data class Exit(val record: SparkExitRecord?, val earlier: List<SparkExitEarlierAddress>) | ||
|
|
||
| private fun OnchainSendAttempt.hasCandidate(txid: String?): Boolean { | ||
| if (txid == null) return false | ||
| return this.txid.equals(txid, ignoreCase = true) || candidateTxids.any { it.equals(txid, ignoreCase = true) } | ||
| } | ||
|
|
||
| /** Whether [attempt] pays the funding address of the exit in [exit]'s record or of an earlier exit. */ | ||
| fun isExitFunding(attempt: OnchainSendAttempt, exit: Exit): Boolean = | ||
| attempt.mayFundSparkExit && ( | ||
| exit.record?.funding?.firstOrNull()?.address == attempt.address || | ||
| exit.earlier.any { it.address == attempt.address } | ||
| ) | ||
|
|
||
| /** | ||
| * Whether the exit no longer needs [attempt]: it was cancelled or ended, or it holds all the funding it asked for | ||
| * and none of it is [attempt]'s. While the exit still waits for one of [attempt]'s transactions, or asks for | ||
| * funding at its address, it needs it; one it adopted means the payment landed, so it is never released. | ||
| */ | ||
| fun exitNoLongerNeeds(attempt: OnchainSendAttempt, exit: Exit): Boolean { | ||
| if (!attempt.mayFundSparkExit) return false | ||
| val record = exit.record?.takeIf { it.funding.firstOrNull()?.address == attempt.address } | ||
| ?: return exit.earlier.any { it.address == attempt.address } | ||
| // A payment the exit still waits for; once other funding filled it, the txid stays behind unwatched. | ||
| val awaited = record.fundingPaymentTxid?.takeIf { record.isAwaitingOwnPayment || record.isEndedPaymentPending } | ||
| val landed = record.funding.mapNotNull { it.txid } + record.leftover.map { it.txid } + listOfNotNull(awaited) | ||
| if (landed.any { attempt.hasCandidate(it) }) return false | ||
| return !record.isActive || record.fundingShortfallSats == null | ||
| } | ||
|
|
||
| /** Whether one of [attempt]'s transactions landed at the exit, as funding or leftover: it reached the network. */ | ||
| fun isAdopted(attempt: OnchainSendAttempt, exit: Exit): Boolean { | ||
| val record = exit.record ?: return false | ||
| val landed = record.funding.mapNotNull { it.txid } + record.leftover.map { it.txid } | ||
| return landed.any { attempt.hasCandidate(it) } | ||
| } | ||
|
|
||
| /** | ||
| * Whether savings may pay [attempt] to the exit again now, over what its funding address holds ([utxos], null | ||
| * when it could not be read): while the exit still asks for funding there, waits for this very payment, or holds | ||
| * this payment's own coin, and never once its release was approved. A retry re-signs the same inputs, so it never | ||
| * pays twice, and what savings pays beyond the ask comes back as leftover. A send to no exit's address is not this | ||
| * check's to refuse. | ||
| */ | ||
| fun mayPayAgain(attempt: OnchainSendAttempt, exit: Exit, utxos: List<EsploraUtxo>?): Boolean { | ||
| // A release approved for it is Send Pending's to finish, even once the exit's record is gone. | ||
| if (attempt.mayFundSparkExit && attempt.releaseApproved) return false | ||
| if (!isExitFunding(attempt, exit)) return true | ||
| val record = exit.record?.takeIf { it.isActive && it.funding.firstOrNull()?.address == attempt.address } | ||
| if (record == null || utxos == null) return false | ||
| // Other wallets' coins alone cover the funding: nothing is left to ask, and its release takes care of it. | ||
| val isOwnPaid = attempt.hasCandidate(record.fundingPaymentTxid) | ||
| val unpaid = if (isOwnPaid) record.copy(fundingPaymentTxid = null) else record | ||
| val others = utxos.filterNot { attempt.hasCandidate(it.txid) } | ||
| if (SparkExitMath.fundingState(unpaid, others) == SparkExitFundingState.Seen) return false | ||
| return when (SparkExitMath.fundingState(record, utxos)) { | ||
| // Partial payments from other wallets still leave an ask. | ||
| is SparkExitFundingState.Ask -> true | ||
| // Waiting for this very payment. | ||
| SparkExitFundingState.Paid -> isOwnPaid | ||
| // Covered with this payment's own unconfirmed coin: a retry only bumps it. | ||
| SparkExitFundingState.Seen -> SparkExitMath.newFunding(utxos, record).any { | ||
| !it.status.confirmed && attempt.hasCandidate(it.txid) | ||
| } | ||
| else -> false | ||
| } | ||
| } | ||
|
|
||
| /** The stored exit of [attempt]'s wallet; null when it cannot be read, so nothing is released. */ | ||
| fun storedExit(attempt: OnchainSendAttempt): Exit? = runCatching { | ||
| Exit( | ||
| record = SparkExitStore(File(Env.sparkExitRecordPath(attempt.walletIndex))).load(), | ||
| earlier = runCatching { | ||
| SparkExitEarlierAddressStore(File(Env.sparkExitEarlierAddressesPath(attempt.walletIndex))).load() | ||
| }.getOrDefault(emptyList()), | ||
| ) | ||
| }.getOrNull() | ||
| } |
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
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
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.