Skip to content
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

Remove spurious interactive-tx commit_sig retransmission #737

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,11 @@ data class InteractiveTxSigningSession(
is Either.Left -> localCommit.value.commitTx.input
is Either.Right -> localCommit.value.publishableTxs.commitTx.input
}
// This value tells our peer whether we need them to retransmit their commit_sig on reconnection or not.
val reconnectNextLocalCommitmentNumber = when (localCommit) {
is Either.Left -> localCommit.value.index
is Either.Right -> localCommit.value.index + 1
}

fun receiveCommitSig(channelKeys: KeyManager.ChannelKeys, channelParams: ChannelParams, remoteCommitSig: CommitSig, currentBlockHeight: Long, logger: MDCLogger): Pair<InteractiveTxSigningSession, InteractiveTxSigningSessionAction> {
return when (localCommit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ import fr.acinq.lightning.channel.Helpers.Closing.claimRevokedRemoteCommitTxOutp
import fr.acinq.lightning.channel.Helpers.Closing.getRemotePerCommitmentSecret
import fr.acinq.lightning.crypto.KeyManager
import fr.acinq.lightning.db.ChannelClosingType
import fr.acinq.lightning.logging.*
import fr.acinq.lightning.logging.LoggingContext
import fr.acinq.lightning.logging.MDCLogger
import fr.acinq.lightning.serialization.Encryption.from
import fr.acinq.lightning.transactions.Transactions.TransactionWithInputInfo.ClosingTx
import fr.acinq.lightning.utils.*
import fr.acinq.lightning.utils.msat
import fr.acinq.lightning.utils.sat
import fr.acinq.lightning.wire.*
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.filterNotNull
Expand Down Expand Up @@ -306,7 +308,7 @@ sealed class PersistedChannelState : ChannelState() {
val myFirstPerCommitmentPoint = keyManager.channelKeys(state.channelParams.localParams.fundingKeyPath).commitmentPoint(0)
ChannelReestablish(
channelId = channelId,
nextLocalCommitmentNumber = 1,
nextLocalCommitmentNumber = state.signingSession.reconnectNextLocalCommitmentNumber,
nextRemoteRevocationNumber = 0,
yourLastCommitmentSecret = PrivateKey(ByteVector32.Zeroes),
myCurrentPerCommitmentPoint = myFirstPerCommitmentPoint,
Expand All @@ -316,15 +318,28 @@ sealed class PersistedChannelState : ChannelState() {
is ChannelStateWithCommitments -> {
val yourLastPerCommitmentSecret = state.commitments.remotePerCommitmentSecrets.lastIndex?.let { state.commitments.remotePerCommitmentSecrets.getHash(it) } ?: ByteVector32.Zeroes
val myCurrentPerCommitmentPoint = keyManager.channelKeys(state.commitments.params.localParams.fundingKeyPath).commitmentPoint(state.commitments.localCommitIndex)
// If we disconnected while signing a funding transaction, we may need our peer to retransmit their commit_sig.
val nextLocalCommitmentNumber = when (state) {
is WaitForFundingConfirmed -> when (state.rbfStatus) {
is RbfStatus.WaitingForSigs -> state.rbfStatus.session.reconnectNextLocalCommitmentNumber
else -> state.commitments.localCommitIndex + 1
}
is Normal -> when (state.spliceStatus) {
is SpliceStatus.WaitingForSigs -> state.spliceStatus.session.reconnectNextLocalCommitmentNumber
else -> state.commitments.localCommitIndex + 1
}
else -> state.commitments.localCommitIndex + 1
}
// If we disconnected while signing a funding transaction, we may need our peer to (re)transmit their tx_signatures.
val unsignedFundingTxId = when (state) {
is WaitForFundingConfirmed -> state.getUnsignedFundingTxId()
is Normal -> state.getUnsignedFundingTxId() // a splice was in progress, we tell our peer that we are remembering it and are expecting signatures
is Normal -> state.getUnsignedFundingTxId()
else -> null
}
val tlvs: TlvStream<ChannelReestablishTlv> = unsignedFundingTxId?.let { TlvStream(ChannelReestablishTlv.NextFunding(it)) } ?: TlvStream.empty()
ChannelReestablish(
channelId = channelId,
nextLocalCommitmentNumber = state.commitments.localCommitIndex + 1,
nextLocalCommitmentNumber = nextLocalCommitmentNumber,
nextRemoteRevocationNumber = state.commitments.remoteCommitIndex,
yourLastCommitmentSecret = PrivateKey(yourLastPerCommitmentSecret),
myCurrentPerCommitmentPoint = myCurrentPerCommitmentPoint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,37 @@ data class Syncing(val state: PersistedChannelState, val channelReestablishSent:
Pair(state, listOf())
}
is WaitForFundingSigned -> {
when (cmd.message.nextFundingTxId) {
// We retransmit our commit_sig, and will send our tx_signatures once we've received their commit_sig.
state.signingSession.fundingTx.txId -> {
val actions = buildList {
if (cmd.message.nextFundingTxId == state.signingSession.fundingTx.txId && cmd.message.nextLocalCommitmentNumber == 0L) {
// They haven't received our commit_sig: we retransmit it, and will send our tx_signatures once we've received
// their commit_sig or their tx_signatures (depending on who must send tx_signatures first).
logger.info { "re-sending commit_sig for channel creation with fundingTxId=${state.signingSession.fundingTx.txId}" }
val commitSig = state.signingSession.remoteCommit.sign(channelKeys(), state.channelParams, state.signingSession)
Pair(state, listOf(ChannelAction.Message.Send(commitSig)))
add(ChannelAction.Message.Send(commitSig))
}
else -> Pair(state, listOf())
}
Pair(state, actions)
}
is WaitForFundingConfirmed -> {
when (cmd.message.nextFundingTxId) {
null -> Pair(state, listOf())
else -> {
if (state.rbfStatus is RbfStatus.WaitingForSigs && state.rbfStatus.session.fundingTx.txId == cmd.message.nextFundingTxId) {
// We retransmit our commit_sig, and will send our tx_signatures once we've received their commit_sig.
logger.info { "re-sending commit_sig for rbf attempt with fundingTxId=${cmd.message.nextFundingTxId}" }
val commitSig = state.rbfStatus.session.remoteCommit.sign(channelKeys(), state.commitments.params, state.rbfStatus.session)
val actions = listOf(ChannelAction.Message.Send(commitSig))
val actions = buildList {
if (cmd.message.nextLocalCommitmentNumber == 0L) {
// They haven't received our commit_sig: we retransmit it.
// We're waiting for signatures from them, and will send our tx_signatures once we receive them.
logger.info { "re-sending commit_sig for rbf attempt with fundingTxId=${cmd.message.nextFundingTxId}" }
val commitSig = state.rbfStatus.session.remoteCommit.sign(channelKeys(), state.commitments.params, state.rbfStatus.session)
add(ChannelAction.Message.Send(commitSig))
}
}
Pair(state, actions)
} else if (state.latestFundingTx.txId == cmd.message.nextFundingTxId) {
// We've already received their commit_sig and sent our tx_signatures. We retransmit our tx_signatures
// and our commit_sig if they haven't received it already.
val actions = buildList {
if (state.latestFundingTx.sharedTx is PartiallySignedSharedTransaction) {
// We have not received their tx_signatures: we retransmit our commit_sig because we don't know if they received it.
if (cmd.message.nextLocalCommitmentNumber == 0L) {
logger.info { "re-sending commit_sig for fundingTxId=${cmd.message.nextFundingTxId}" }
val commitSig = state.commitments.latest.remoteCommit.sign(
channelKeys(),
Expand All @@ -75,11 +83,11 @@ data class Syncing(val state: PersistedChannelState, val channelReestablishSent:
}
is WaitForChannelReady -> {
val actions = ArrayList<ChannelAction>()

// We've already received their commit_sig and sent our tx_signatures. We retransmit our tx_signatures
// and our commit_sig if they haven't received it already.
if (state.commitments.latest.fundingTxId == cmd.message.nextFundingTxId) {
if (state.commitments.latest.localFundingStatus is LocalFundingStatus.UnconfirmedFundingTx) {
if (state.commitments.latest.localFundingStatus.sharedTx is PartiallySignedSharedTransaction) {
// If we have not received their tx_signatures, we can't tell whether they had received our commit_sig, so we need to retransmit it
if (cmd.message.nextLocalCommitmentNumber == 0L) {
logger.info { "re-sending commit_sig for fundingTxId=${state.commitments.latest.fundingTxId}" }
val commitSig = state.commitments.latest.remoteCommit.sign(
channelKeys(),
Expand All @@ -99,12 +107,10 @@ data class Syncing(val state: PersistedChannelState, val channelReestablishSent:
logger.warning { "cannot re-send tx_signatures for fundingTxId=${cmd.message.nextFundingTxId}, transaction is already confirmed" }
}
}

logger.debug { "re-sending channel_ready" }
val nextPerCommitmentPoint = channelKeys().commitmentPoint(1)
val channelReady = ChannelReady(state.commitments.channelId, nextPerCommitmentPoint)
actions.add(ChannelAction.Message.Send(channelReady))

Pair(state, actions)
}
is LegacyWaitForFundingLocked -> {
Expand Down Expand Up @@ -132,16 +138,20 @@ data class Syncing(val state: PersistedChannelState, val channelReestablishSent:

// resume splice signing session if any
val spliceStatus1 = if (state.spliceStatus is SpliceStatus.WaitingForSigs && state.spliceStatus.session.fundingTx.txId == cmd.message.nextFundingTxId) {
// We retransmit our commit_sig, and will send our tx_signatures once we've received their commit_sig.
logger.info { "re-sending commit_sig for splice attempt with fundingTxIndex=${state.spliceStatus.session.fundingTxIndex} fundingTxId=${state.spliceStatus.session.fundingTx.txId}" }
val commitSig = state.spliceStatus.session.remoteCommit.sign(channelKeys(), state.commitments.params, state.spliceStatus.session)
actions.add(ChannelAction.Message.Send(commitSig))
if (cmd.message.nextLocalCommitmentNumber == state.commitments.remoteCommitIndex) {
// They haven't received our commit_sig: we retransmit it.
// We're waiting for signatures from them, and will send our tx_signatures once we receive them.
logger.info { "re-sending commit_sig for splice attempt with fundingTxIndex=${state.spliceStatus.session.fundingTxIndex} fundingTxId=${state.spliceStatus.session.fundingTx.txId}" }
val commitSig = state.spliceStatus.session.remoteCommit.sign(channelKeys(), state.commitments.params, state.spliceStatus.session)
actions.add(ChannelAction.Message.Send(commitSig))
}
state.spliceStatus
} else if (state.commitments.latest.fundingTxId == cmd.message.nextFundingTxId) {
when (val localFundingStatus = state.commitments.latest.localFundingStatus) {
is LocalFundingStatus.UnconfirmedFundingTx -> {
if (localFundingStatus.sharedTx is PartiallySignedSharedTransaction) {
// If we have not received their tx_signatures, we can't tell whether they had received our commit_sig, so we need to retransmit it
// We've already received their commit_sig and sent our tx_signatures. We retransmit our tx_signatures
// and our commit_sig if they haven't received it already.
if (cmd.message.nextLocalCommitmentNumber == state.commitments.remoteCommitIndex) {
logger.info { "re-sending commit_sig for fundingTxIndex=${state.commitments.latest.fundingTxIndex} fundingTxId=${state.commitments.latest.fundingTxId}" }
val commitSig = state.commitments.latest.remoteCommit.sign(
channelKeys(),
Expand Down Expand Up @@ -406,7 +416,7 @@ data class Syncing(val state: PersistedChannelState, val channelReestablishSent:
// we resend the same updates and the same sig, and preserve the same ordering
val signedUpdates = commitments.changes.localChanges.signed
val commitSigs = commitments.active.map { it.nextRemoteCommit }.filterIsInstance<NextRemoteCommit>().map { it.sig }
SyncResult.Success(retransmit = when (retransmitRevocation) {
val retransmit = when (retransmitRevocation) {
null -> buildList {
addAll(signedUpdates)
addAll(commitSigs)
Expand All @@ -424,7 +434,8 @@ data class Syncing(val state: PersistedChannelState, val channelReestablishSent:
addAll(commitSigs)
}
}
})
}
SyncResult.Success(retransmit)
}
remoteChannelReestablish.nextLocalCommitmentNumber == (commitments.nextRemoteCommitIndex + 1) -> {
// we just sent a new commit_sig, they have received it but we haven't received their revocation
Expand All @@ -449,6 +460,11 @@ data class Syncing(val state: PersistedChannelState, val channelReestablishSent:
// they have acknowledged the last commit_sig we sent
SyncResult.Success(retransmit = listOfNotNull(retransmitRevocation))
}
remoteChannelReestablish.nextLocalCommitmentNumber == commitments.remoteCommitIndex && remoteChannelReestablish.nextFundingTxId != null -> {
// they haven't received the commit_sig we sent as part of signing a splice transaction
// we will retransmit it before exchanging tx_signatures
SyncResult.Success(retransmit = listOfNotNull(retransmitRevocation))
}
remoteChannelReestablish.nextLocalCommitmentNumber < (commitments.remoteCommitIndex + 1) -> {
// they are behind
SyncResult.Failure.RemoteLate
Expand Down
Loading
Loading