From 05a90df67a0d612b061c1a7b3166e253d8c1511d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christopher=20J=C3=A4mthagen?= Date: Fri, 20 Jan 2017 13:20:02 +0100 Subject: [PATCH] lnwallet: return error from toChannelDelta() if htlc output is not found --- lnwallet/channel.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 7c0930e1..f30ef509 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -260,8 +260,9 @@ func (c *commitment) toChannelDelta(ourCommit bool) (*channeldb.ChannelDelta, er // purposes, we create a small helper function to locate the output // index of a particular HTLC within the current commitment // transaction. - locateOutputIndex := func(p *PaymentDescriptor) uint16 { + locateOutputIndex := func(p *PaymentDescriptor) (uint16, error) { var idx uint16 + var found bool pkScript := p.theirPrevPkScript if ourCommit { @@ -272,13 +273,17 @@ func (c *commitment) toChannelDelta(ourCommit bool) (*channeldb.ChannelDelta, er if contains(dups[p.RHash], uint16(i)) { continue } - + found = true idx = uint16(i) dups[p.RHash] = append(dups[p.RHash], idx) break } } - return idx + if !found { + return 0, fmt.Errorf("could not find a matching output for the HTLC " + + "in the commitment transaction") + } + return idx, nil } for _, htlc := range c.outgoingHTLCs { @@ -288,7 +293,7 @@ func (c *commitment) toChannelDelta(ourCommit bool) (*channeldb.ChannelDelta, er RHash: htlc.RHash, RefundTimeout: htlc.Timeout, RevocationDelay: 0, - OutputIndex: locateOutputIndex(htlc), + OutputIndex: index, } delta.Htlcs = append(delta.Htlcs, h) } @@ -299,7 +304,7 @@ func (c *commitment) toChannelDelta(ourCommit bool) (*channeldb.ChannelDelta, er RHash: htlc.RHash, RefundTimeout: htlc.Timeout, RevocationDelay: 0, - OutputIndex: locateOutputIndex(htlc), + OutputIndex: index, } delta.Htlcs = append(delta.Htlcs, h) }