From 78e102f08d7f828f79fecf2b748abcbee2454023 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christopher=20J=C3=A4mthagen?= Date: Fri, 20 Jan 2017 15:59:09 +0100 Subject: [PATCH] lnwallet: fix settled/cancelled HTLCs remote pkScripts + cleanup When an HTLC is either cancelled or settled we must properly set the pkScript for the HTLC on the remote commitment, such that we can generate a valid ChannelDelta. --- lnwallet/channel.go | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index f777a7eb..1f051ccb 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -1169,9 +1169,7 @@ func (lc *LightningChannel) fetchCommitmentView(remoteChain bool, if err != nil { return nil, err } - //var isDust bool - //if ourCommitTx { - //isDust = + for _, htlc := range filteredHTLCView.ourUpdates { if (ourCommitTx && htlc.isDustLocal) || (!ourCommitTx && htlc.isDustRemote) { @@ -1972,6 +1970,12 @@ func (lc *LightningChannel) SettleHTLC(preimage [32]byte) (uint64, error) { lc.localUpdateLog.appendUpdate(pd) + // Since we will not be adding this HTLC to any commitment transaction + // anymore, we should properly set theirPrevPkScript so we can generate + // a valid ChannelDelta for a revoked remote commitment. + addEntry := lc.remoteUpdateLog.lookup(targetHTLC.Index) + addEntry.theirPrevPkScript = addEntry.theirPkScript + lc.rHashMap[paymentHash][0] = nil lc.rHashMap[paymentHash] = lc.rHashMap[paymentHash][1:] if len(lc.rHashMap[paymentHash]) == 0 { @@ -2009,6 +2013,11 @@ func (lc *LightningChannel) ReceiveHTLCSettle(preimage [32]byte, logIndex uint64 lc.remoteUpdateLog.appendUpdate(pd) + // Since we will not be adding this HTLC to any commitment transaction + // anymore, we should properly set theirPrevPkScript so we can generate + // a valid ChannelDelta for a revoked remote commitment. + htlc.theirPrevPkScript = htlc.theirPkScript + return nil } @@ -2036,6 +2045,11 @@ func (lc *LightningChannel) FailHTLC(rHash [32]byte) (uint64, error) { lc.localUpdateLog.appendUpdate(pd) + // Since we will not be adding this HTLC to any commitment transaction + // anymore, we should properly set theirPrevPkScript so we can generate + // a valid ChannelDelta for a revoked remote commitment. + addEntry.theirPrevPkScript = addEntry.theirPkScript + lc.rHashMap[rHash][0] = nil lc.rHashMap[rHash] = lc.rHashMap[rHash][1:] if len(lc.rHashMap[rHash]) == 0 { @@ -2068,6 +2082,11 @@ func (lc *LightningChannel) ReceiveFailHTLC(logIndex uint64) error { lc.remoteUpdateLog.appendUpdate(pd) + // Since we will not be adding this HTLC to any commitment transaction + // anymore, we should properly set theirPrevPkScript so we can generate + // a valid ChannelDelta for a revoked remote commitment. + htlc.theirPrevPkScript = htlc.theirPkScript + return nil }