Skip to content

Commit

Permalink
Rework channel reestablish (#629)
Browse files Browse the repository at this point in the history
- properly handle `channel_reestablish` in state `Syncing(Shutdown)`
- implement the equivalent of ACINQ/eclair#2036, a major rework/cleanup of the reestablish logic, with separation of concerns, strong typing, etc.
  • Loading branch information
pm47 authored Apr 17, 2024
1 parent ada0347 commit 4b73825
Show file tree
Hide file tree
Showing 6 changed files with 282 additions and 220 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ data class CannotSignWithoutChanges (override val channelId: Byte
data class CannotSignBeforeRevocation (override val channelId: ByteVector32) : ChannelException(channelId, "cannot sign until next revocation hash is received")
data class UnexpectedRevocation (override val channelId: ByteVector32) : ChannelException(channelId, "received unexpected RevokeAndAck message")
data class InvalidRevocation (override val channelId: ByteVector32) : ChannelException(channelId, "invalid revocation")
data class RevocationSyncError (override val channelId: ByteVector32) : ChannelException(channelId, "revocation sync error")
data class InvalidFailureCode (override val channelId: ByteVector32) : ChannelException(channelId, "UpdateFailMalformedHtlc message doesn't have BADONION bit set")
data class PleasePublishYourCommitment (override val channelId: ByteVector32) : ChannelException(channelId, "please publish your local commitment")
data class CommandUnavailableInThisState (override val channelId: ByteVector32, val state: String) : ChannelException(channelId, "cannot execute command in state=$state")
Expand Down
51 changes: 0 additions & 51 deletions src/commonMain/kotlin/fr/acinq/lightning/channel/Helpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -172,57 +172,6 @@ object Helpers {
toRemote > it.localChannelReserve(commitments.params)
}

/**
* Tells whether or not their expected next remote commitment number matches with our data
*
* @return
* - true if parties are in sync or remote is behind
* - false if we are behind
*/
fun checkLocalCommit(commitments: Commitments, nextRemoteRevocationNumber: Long): Boolean {
return when {
// they just sent a new commit_sig, we have received it but they didn't receive our revocation
commitments.localCommitIndex == nextRemoteRevocationNumber -> true
// we are in sync
commitments.localCommitIndex == nextRemoteRevocationNumber + 1 -> true
// remote is behind: we return true because things are fine on our side
commitments.localCommitIndex > nextRemoteRevocationNumber + 1 -> true
// we are behind
else -> false
}
}

/**
* Tells whether or not their expected next local commitment number matches with our data
*
* @return
* - true if parties are in sync or remote is behind
* - false if we are behind
*/
fun checkRemoteCommit(commitments: Commitments, nextLocalCommitmentNumber: Long): Boolean {
return when {
commitments.remoteNextCommitInfo.isLeft ->
when {
// we just sent a new commit_sig but they didn't receive it
nextLocalCommitmentNumber == commitments.nextRemoteCommitIndex -> true
// we just sent a new commit_sig, they have received it but we haven't received their revocation
nextLocalCommitmentNumber == (commitments.nextRemoteCommitIndex + 1) -> true
// they are behind
nextLocalCommitmentNumber < commitments.nextRemoteCommitIndex -> true
else -> false
}
commitments.remoteNextCommitInfo.isRight ->
when {
// they have acknowledged the last commit_sig we sent
nextLocalCommitmentNumber == (commitments.remoteCommitIndex + 1) -> true
// they are behind
nextLocalCommitmentNumber < (commitments.remoteCommitIndex + 1) -> true
else -> false
}
else -> false
}
}

/** This helper method will publish txs only if they haven't yet reached minDepth. */
fun LoggingContext.publishIfNeeded(txs: List<ChannelAction.Blockchain.PublishTx>, irrevocablySpent: Map<OutPoint, Transaction>): List<ChannelAction.Blockchain.PublishTx> {
val (skip, process) = txs.partition { it.tx.inputsAlreadySpent(irrevocablySpent) }
Expand Down
Loading

0 comments on commit 4b73825

Please sign in to comment.