Repository navigation
fix: let send reach spark before the node runs #2244
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
base: master
Are you sure you want to change the base?
Changes from all commits
a409579
cc01758
b337747
272067d
1064562
1b89d0f
cf58265
90fddb0
652c67b
fa15097
7c9ab36
54c3525
e5584b2
37f8ec4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -429,6 +429,22 @@ class AppViewModel @Inject constructor( | |
| if (canPresent()) _mainScreenEffect.emit(effect) | ||
| } | ||
|
|
||
| /** Scanned payments that wait for the node or a usable channel before they can be reviewed. */ | ||
| private val _isSendWaitingForNode = MutableStateFlow(0) | ||
| val isSendWaitingForNode: StateFlow<Boolean> = _isSendWaitingForNode | ||
| .map { it > 0 } | ||
| .stateIn(viewModelScope, SharingStarted.Eagerly, false) | ||
|
|
||
| /** Runs [wait] counted in [isSendWaitingForNode], since the recipient steps have no overlay of their own. */ | ||
| private suspend fun <T> showingNodeWait(wait: suspend () -> T): T { | ||
| _isSendWaitingForNode.update { it + 1 } | ||
| return try { | ||
| wait() | ||
| } finally { | ||
| _isSendWaitingForNode.update { it - 1 } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Waits for the node's channels before a Lightning send. When Spark covers [amountSats] there is no wait for a | ||
| * usable channel: a running node only publishes its current channels, which the send limit reads, and a node not | ||
|
|
@@ -441,7 +457,11 @@ class AppViewModel @Inject constructor( | |
| requireCurrentPaymentPreparation(walletGeneration) | ||
| val isNodeRunning = lightningRepo.lightningState.value.nodeLifecycleState.isRunning() | ||
| val sparkCovers = amountSats != null && lightningRepo.canSendWithSpark(amountSats) | ||
| if (isNodeRunning || !sparkCovers) lightningRepo.waitForUsableChannels(awaitUsable = !sparkCovers) | ||
| when { | ||
| // A wait for a usable channel is shown; with Spark covering, only the channels are published | ||
| !sparkCovers -> showingNodeWait { lightningRepo.waitForUsableChannels(awaitUsable = true) } | ||
| isNodeRunning -> lightningRepo.waitForUsableChannels(awaitUsable = false) | ||
| } | ||
| requireCurrentPaymentPreparation(walletGeneration) | ||
| walletRepo.refreshMaxSendLightning() | ||
| requireCurrentPaymentPreparation(walletGeneration) | ||
|
|
@@ -3575,6 +3595,11 @@ class AppViewModel @Inject constructor( | |
| setSendEffect(SendEffect.NavigateToAddress) | ||
| } | ||
|
|
||
| /** A typed address can wait for the node, so leaving it must stop that wait and its late result. */ | ||
| fun cancelAddressValidation() { | ||
| addressValidationJob?.cancel() | ||
| } | ||
|
|
||
| private fun resetAddressInput() { | ||
| addressValidationJob?.cancel() | ||
| _sendUiState.update { state -> | ||
|
|
@@ -3672,6 +3697,14 @@ class AppViewModel @Inject constructor( | |
| } | ||
|
|
||
| if (invoice.amountSatoshis > 0uL) { | ||
| // Manual entry opens before the node runs, so wait for its channels unless Spark covers the amount | ||
| if (!lightningRepo.lightningState.value.nodeLifecycleState.isRunning()) { | ||
| // A foreground start can queue behind a background stop, which reads as unable to run | ||
| if (!lightningRepo.canSendWithSpark(invoice.amountSatoshis)) { | ||
| showingNodeWait { lightningRepo.awaitPendingNodeStart() } | ||
| } | ||
| waitForUsableChannels(amountSats = invoice.amountSatoshis) | ||
|
ovitrif marked this conversation as resolved.
Resolved
|
||
| } | ||
| lightningRepo.syncState() | ||
| if (!lightningRepo.canSend(invoice.amountSatoshis)) { | ||
| val maxSendLightning = walletRepo.balanceState.value.maxSendLightningSats | ||
|
|
@@ -3847,7 +3880,10 @@ class AppViewModel @Inject constructor( | |
| return@takeIf canSend | ||
| } | ||
|
|
||
| private suspend fun LightningInvoice.isOwnInvoice(): Boolean = isPayee(lightningRepo.awaitNodeId()) | ||
| /** On a cold start the node id is known only once the node runs, so that wait is shown. */ | ||
| private suspend fun LightningInvoice.isOwnInvoice(): Boolean = isPayee( | ||
| lightningRepo.getLastKnownNodeId() ?: showingNodeWait { lightningRepo.awaitNodeId() } | ||
| ) | ||
|
|
||
| private fun LightningInvoice.isPayee(nodeId: String?): Boolean { | ||
| val payee = payeeNodeId?.toHex() ?: return false | ||
|
|
@@ -5549,8 +5585,12 @@ class AppViewModel @Inject constructor( | |
| SendMethod.LIGHTNING -> { | ||
| sparkWithdrawalValidity(amount)?.let { return it } | ||
| val maxSendable = maxSendableLightningSats() | ||
| // Spark keeps the routing reserve that Available leaves out | ||
| val sparkDebit = amount.safe() + _sendUiState.value.estimatedRoutingFee.safe() | ||
| when (val lnurl = _sendUiState.value.lnurl) { | ||
| null -> amount <= maxSendable && lightningRepo.canSend(amount) | ||
| // The cached limit is re-derived only once the node runs, so it can trail Spark's balance | ||
| null -> (amount <= maxSendable || lightningRepo.canSendWithSpark(sparkDebit)) && | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [MEDIUM] Revalidate the entered amount when the Spark fee arrives With the node down and a zero cache, tap Available while the Spark Max quote is still in flight. The amount passes validation with a zero reserve. A later quote can lower Available below the entered amount, but the fee update leaves the validity flag true and Continue uses that flag. The user could advance to review with an amount the updated limit rejects. Revalidate the entered amount when the quote arrives and enforce the updated limit on Continue. A focused Kotlin probe confirmed that fresh validation rejects the amount while the stored validity remains true. |
||
| lightningRepo.canSend(amount) | ||
| is LnurlParams.LnurlWithdraw -> amount <= lnurl.data.maxWithdrawableSat() | ||
| is LnurlParams.LnurlPay -> { | ||
| val maxSat = lnurl.data.maxSendableSat() | ||
|
|
@@ -9194,7 +9234,9 @@ class AppViewModel @Inject constructor( | |
| val decodedInvoice = currentState.decodedInvoice ?: return | ||
| val bolt11 = decodedInvoice.bolt11 | ||
|
|
||
| val maxSendLightning = walletRepo.balanceState.value.maxSendLightningSats | ||
| // The cached limit is re-derived only once the node runs, so Spark's live amount is quoted while it trails | ||
| val cachedMax = walletRepo.balanceState.value.maxSendLightningSats | ||
| val maxSendLightning = maxOf(cachedMax, lightningRepo.sparkSendableSats()) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [MEDIUM] Quote the Spark limit when only Spark can pay With the node stopped, open an amountless invoice when the cached Spending limit exceeds Spark’s live sendable balance. The keypad uses the Spark limit, but the Max quote takes the larger cached limit. That amount cannot route through Spark, so the estimate falls through to the stopped node and leaves a zero reserve. Available would then offer an amount the wallet cannot fund whenever the route fee exceeds the remaining fee headroom. Use the same live limit for the quote and the keypad while Spark is the only available rail. The route selection and a focused Kotlin probe confirmed the quote and validation behavior. |
||
| if (maxSendLightning == 0uL) { | ||
| _sendUiState.update { it.copy(estimatedRoutingFee = 0uL) } | ||
| return | ||
|
|
@@ -9538,6 +9580,7 @@ class AppViewModel @Inject constructor( | |
| // A read still preparing belongs to this Send visit: it must not reopen a sheet or reach QuickPay. | ||
| // A scan that closes Send itself keeps running, and no later visit may cancel it. | ||
| if (isRunningScan.get() != true) cancelSendVisitScan() else sendVisitScanJob = null | ||
| cancelAddressValidation() | ||
| hasLeftSendRecipient = true | ||
| sendStateGeneration++ | ||
| // A closed send is over: a swap failing for it later gives no contact back to whatever comes next. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.