Skip to content

Commit

Permalink
lnwallet: eliminate usage of LightningChannel.theirPrevPkScript
Browse files Browse the repository at this point in the history
This commit removes the theirPrevPkScript field from the
LightningChannel struct all together. It’s no longer needed as the more
fundamental mutation bug has been fixed within the channel state
machine.
  • Loading branch information
Roasbeef committed Apr 12, 2017
1 parent a3fd738 commit 4cd277c
Showing 1 changed file with 7 additions and 30 deletions.
37 changes: 7 additions & 30 deletions lnwallet/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func (c *commitment) toChannelDelta(ourCommit bool) (*channeldb.ChannelDelta, er
found bool
)

pkScript := p.theirPrevPkScript
pkScript := p.theirPkScript
if ourCommit {
pkScript = p.ourPkScript
}
Expand All @@ -294,8 +294,8 @@ func (c *commitment) toChannelDelta(ourCommit bool) (*channeldb.ChannelDelta, er
}
}
if !found {
return 0, fmt.Errorf("could not find a matching " +
"output for the HTLC in the commitment transaction")
return 0, fmt.Errorf("unable to find htlc: script=%x, value=%v",
pkScript, p.Amount)
}

return idx, nil
Expand All @@ -309,8 +309,8 @@ func (c *commitment) toChannelDelta(ourCommit bool) (*channeldb.ChannelDelta, er
if (ourCommit && htlc.isDustLocal) ||
(!ourCommit && htlc.isDustRemote) {
index = maxUint16
} else if index, err = locateOutputIndex(htlc); err != nil {
return nil, err
} else if index, err = locateOutputIndex(&htlc); err != nil {
return nil, fmt.Errorf("unable to find outgoing htlc: %v", err)
}

h := &channeldb.HTLC{
Expand All @@ -328,8 +328,8 @@ func (c *commitment) toChannelDelta(ourCommit bool) (*channeldb.ChannelDelta, er
if (ourCommit && htlc.isDustLocal) ||
(!ourCommit && htlc.isDustRemote) {
index = maxUint16
} else if index, err = locateOutputIndex(htlc); err != nil {
return nil, err
} else if index, err = locateOutputIndex(&htlc); err != nil {
return nil, fmt.Errorf("unable to find incoming htlc: %v", err)
}

h := &channeldb.HTLC{
Expand Down Expand Up @@ -2075,12 +2075,6 @@ 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 {
Expand Down Expand Up @@ -2117,12 +2111,6 @@ 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
}

Expand Down Expand Up @@ -2150,11 +2138,6 @@ 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 {
Expand Down Expand Up @@ -2187,11 +2170,6 @@ 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
}

Expand Down Expand Up @@ -2282,7 +2260,6 @@ func (lc *LightningChannel) addHTLC(commitTx *wire.MsgTx, ourCommit bool,
if ourCommit {
paymentDesc.ourPkScript = htlcP2WSH
} else {
paymentDesc.theirPrevPkScript = paymentDesc.theirPkScript
paymentDesc.theirPkScript = htlcP2WSH
}

Expand Down

0 comments on commit 4cd277c

Please sign in to comment.