Skip to content

Commit

Permalink
Fix tx_signatures ordering for splices
Browse files Browse the repository at this point in the history
We were incorrectly splitting the shared input amount based on each peer's
balance, but for the signing order the peer that adds the shared input
takes credit for the whole amount of that input.
  • Loading branch information
t-bast committed Nov 10, 2023
1 parent 79b7477 commit 6e5a851
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ data class InteractiveTxSigningSession(
}
is Try.Success -> {
val signedLocalCommit = LocalCommit(localCommit.value.index, localCommit.value.spec, PublishableTxs(signedLocalCommitTx, listOf()))
if (shouldSignFirst(channelParams, fundingTx.tx)) {
if (shouldSignFirst(fundingParams.isInitiator, channelParams, fundingTx.tx)) {
val fundingStatus = LocalFundingStatus.UnconfirmedFundingTx(fundingTx, fundingParams, currentBlockHeight)
val commitment = Commitment(fundingTxIndex, fundingParams.remoteFundingPubkey, fundingStatus, RemoteFundingStatus.NotLocked, signedLocalCommit, remoteCommit, nextRemoteCommit = null)
val action = InteractiveTxSigningSessionAction.SendTxSigs(fundingStatus, commitment, fundingTx.localSigs)
Expand Down Expand Up @@ -873,13 +873,16 @@ data class InteractiveTxSigningSession(
}
}

fun shouldSignFirst(channelParams: ChannelParams, tx: SharedTransaction): Boolean {
return if (tx.localAmountIn == tx.remoteAmountIn) {
fun shouldSignFirst(isInitiator: Boolean, channelParams: ChannelParams, tx: SharedTransaction): Boolean {
val sharedAmountIn = tx.sharedInput?.let { it.localAmount + it.remoteAmount } ?: 0.msat
val localAmountIn = tx.localInputs.map { it.txOut.amount }.sum().toMilliSatoshi() + if (isInitiator) sharedAmountIn else 0.msat
val remoteAmountIn = tx.remoteInputs.map { it.txOut.amount }.sum().toMilliSatoshi() + if (isInitiator) 0.msat else sharedAmountIn
return if (localAmountIn == remoteAmountIn) {
// When both peers contribute the same amount, the peer with the lowest pubkey must transmit its `tx_signatures` first.
LexicographicalOrdering.isLessThan(channelParams.localParams.nodeId, channelParams.remoteParams.nodeId)
} else {
// Otherwise, the peer with the lowest total of input amount must transmit its `tx_signatures` first.
tx.localAmountIn < tx.remoteAmountIn
localAmountIn < remoteAmountIn
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ class SpliceTestsCommon : LightningTestSuite() {
spliceIn(alice, bob, listOf(50_000.sat))
}

@Test
fun `splice funds in -- non-initiator`() {
val (alice, bob) = reachNormal()
spliceIn(bob, alice, listOf(50_000.sat))
}

@Test
fun `splice funds in -- many utxos`() {
val (alice, bob) = reachNormal()
Expand Down

0 comments on commit 6e5a851

Please sign in to comment.