Skip to content

Commit 755bb09

Browse files
committed
htlcswitch: skip decoding hop if the htlc is already acked
We now move the check earlier in the loop so we don't even need to decode the hop for already processed ADDs.
1 parent e3f95dc commit 755bb09

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

htlcswitch/link.go

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3757,10 +3757,26 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg) {
37573757
l.log.Tracef("processing %d remote adds for height %d",
37583758
len(fwdPkg.Adds), fwdPkg.Height)
37593759

3760-
decodeReqs := make(
3761-
[]hop.DecodeHopIteratorRequest, 0, len(fwdPkg.Adds),
3762-
)
3763-
for _, update := range fwdPkg.Adds {
3760+
// decodeReqs is a list of requests sent to the onion decoder. We expect
3761+
// the same length of responses to be returned.
3762+
decodeReqs := make([]hop.DecodeHopIteratorRequest, 0, len(fwdPkg.Adds))
3763+
3764+
// unackedAdds is a list of ADDs that's waiting for the remote's
3765+
// settle/fail update.
3766+
unackedAdds := make([]*lnwire.UpdateAddHTLC, 0, len(fwdPkg.Adds))
3767+
3768+
for i, update := range fwdPkg.Adds {
3769+
// If this index is already found in the ack filter, the
3770+
// response to this forwarding decision has already been
3771+
// committed by one of our commitment txns. ADDs in this state
3772+
// are waiting for the rest of the fwding package to get acked
3773+
// before being garbage collected.
3774+
if fwdPkg.State == channeldb.FwdStateProcessed &&
3775+
fwdPkg.AckFilter.Contains(uint16(i)) {
3776+
3777+
continue
3778+
}
3779+
37643780
if msg, ok := update.UpdateMsg.(*lnwire.UpdateAddHTLC); ok {
37653781
// Before adding the new htlc to the state machine,
37663782
// parse the onion object in order to obtain the
@@ -3777,6 +3793,7 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg) {
37773793
}
37783794

37793795
decodeReqs = append(decodeReqs, req)
3796+
unackedAdds = append(unackedAdds, msg)
37803797
}
37813798
}
37823799

@@ -3799,23 +3816,10 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg) {
37993816

38003817
var switchPackets []*htlcPacket
38013818

3802-
for i, update := range fwdPkg.Adds {
3819+
for i, update := range unackedAdds {
38033820
idx := uint16(i)
3804-
3805-
//nolint:forcetypeassert
3806-
add := *update.UpdateMsg.(*lnwire.UpdateAddHTLC)
38073821
sourceRef := fwdPkg.SourceRef(idx)
3808-
3809-
if fwdPkg.State == channeldb.FwdStateProcessed &&
3810-
fwdPkg.AckFilter.Contains(idx) {
3811-
3812-
// If this index is already found in the ack filter,
3813-
// the response to this forwarding decision has already
3814-
// been committed by one of our commitment txns. ADDs
3815-
// in this state are waiting for the rest of the fwding
3816-
// package to get acked before being garbage collected.
3817-
continue
3818-
}
3822+
add := *update
38193823

38203824
// An incoming HTLC add has been full-locked in. As a result we
38213825
// can now examine the forwarding details of the HTLC, and the
@@ -3835,8 +3839,10 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg) {
38353839
add.ID, failureCode, add.OnionBlob, &sourceRef,
38363840
)
38373841

3838-
l.log.Errorf("unable to decode onion hop "+
3839-
"iterator: %v", failureCode)
3842+
l.log.Errorf("unable to decode onion hop iterator "+
3843+
"for htlc(id=%v, hash=%x): %v", add.ID,
3844+
add.PaymentHash, failureCode)
3845+
38403846
continue
38413847
}
38423848

0 commit comments

Comments
 (0)