diff --git a/src/commonMain/kotlin/fr/acinq/lightning/NodeEvents.kt b/src/commonMain/kotlin/fr/acinq/lightning/NodeEvents.kt index c0c0ee372..71eabf1bd 100644 --- a/src/commonMain/kotlin/fr/acinq/lightning/NodeEvents.kt +++ b/src/commonMain/kotlin/fr/acinq/lightning/NodeEvents.kt @@ -2,6 +2,8 @@ package fr.acinq.lightning import fr.acinq.bitcoin.ByteVector32 import fr.acinq.bitcoin.Satoshi +import fr.acinq.lightning.channel.InteractiveTxParams +import fr.acinq.lightning.channel.SharedFundingInput import fr.acinq.lightning.channel.states.ChannelStateWithCommitments import fr.acinq.lightning.channel.states.Normal import fr.acinq.lightning.channel.states.WaitForFundingCreated @@ -41,5 +43,17 @@ sealed interface LiquidityEvents : NodeEvents { data class ApprovalRequested(override val amount: MilliSatoshi, override val fee: MilliSatoshi, override val source: Source, val replyTo: CompletableDeferred) : LiquidityEvents } +/** This is useful on iOS to ask the OS for time to finish some sensitive tasks. */ +sealed interface SensitiveTaskEvents : NodeEvents { + sealed class TaskIdentifier { + data class InteractiveTx(val channelId: ByteVector32, val fundingTxIndex: Long) : TaskIdentifier() { + constructor(fundingParams: InteractiveTxParams) : this(fundingParams.channelId, (fundingParams.sharedInput as? SharedFundingInput.Multisig2of2)?.fundingTxIndex?.let { it + 1 } ?: 0) + } + } + data class TaskStarted(val id: TaskIdentifier) : SensitiveTaskEvents + data class TaskEnded(val id: TaskIdentifier) : SensitiveTaskEvents + +} + /** This will be emitted in a corner case where the user restores a wallet on an older version of the app, which is unable to read the channel data. */ object UpgradeRequired : NodeEvents diff --git a/src/commonMain/kotlin/fr/acinq/lightning/channel/states/Channel.kt b/src/commonMain/kotlin/fr/acinq/lightning/channel/states/Channel.kt index a58b20c48..a49274f17 100644 --- a/src/commonMain/kotlin/fr/acinq/lightning/channel/states/Channel.kt +++ b/src/commonMain/kotlin/fr/acinq/lightning/channel/states/Channel.kt @@ -78,6 +78,7 @@ sealed class ChannelState { is Syncing -> this@ChannelState.state else -> this@ChannelState } + maybeSignalSensitiveTask(oldState, newState) return when { // we only want to fire the PaymentSent event when we transition to Closing for the first time oldState is ChannelStateWithCommitments && oldState !is Closing && newState is Closing -> emitClosingEvents(oldState, newState) @@ -85,6 +86,21 @@ sealed class ChannelState { } } + /** Some transitions imply that we are in the middle of tasks that may require some time. */ + private fun ChannelContext.maybeSignalSensitiveTask(oldState: ChannelState, newState: ChannelState) { + val spliceStatusBefore = (oldState as? Normal)?.spliceStatus + val spliceStatusAfter = (newState as? Normal)?.spliceStatus + when { + spliceStatusBefore !is SpliceStatus.InProgress && spliceStatusAfter is SpliceStatus.InProgress -> // splice initiated + staticParams.nodeParams._nodeEvents.tryEmit(SensitiveTaskEvents.TaskStarted(SensitiveTaskEvents.TaskIdentifier.InteractiveTx(spliceStatusAfter.spliceSession.fundingParams))) + spliceStatusBefore is SpliceStatus.InProgress && spliceStatusAfter !is SpliceStatus.WaitingForSigs -> // splice aborted before reaching signing phase + staticParams.nodeParams._nodeEvents.tryEmit(SensitiveTaskEvents.TaskEnded(SensitiveTaskEvents.TaskIdentifier.InteractiveTx(spliceStatusBefore.spliceSession.fundingParams))) + spliceStatusBefore is SpliceStatus.WaitingForSigs && spliceStatusAfter !is SpliceStatus.WaitingForSigs -> // splice leaving signing phase (successfully or not) + staticParams.nodeParams._nodeEvents.tryEmit(SensitiveTaskEvents.TaskEnded(SensitiveTaskEvents.TaskIdentifier.InteractiveTx(spliceStatusBefore.session.fundingParams))) + else -> {} + } + } + private fun ChannelContext.emitClosingEvents(oldState: ChannelStateWithCommitments, newState: Closing): List { val channelBalance = oldState.commitments.latest.localCommit.spec.toLocal return if (channelBalance > 0.msat) {