Skip to content

Commit 3b6e28d

Browse files
committed
channeldb+htlcswitch: make sure circuit is not nil in teardownCircuit
1 parent 2fc79d8 commit 3b6e28d

File tree

3 files changed

+28
-36
lines changed

3 files changed

+28
-36
lines changed

channeldb/forwarding_package.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ func (f *PkgFilter) Decode(r io.Reader) error {
211211
return err
212212
}
213213

214+
// String returns a human-readable string.
215+
func (f *PkgFilter) String() string {
216+
return fmt.Sprintf("count=%v, filter=%v", f.count, f.filter)
217+
}
218+
214219
// FwdPkg records all adds, settles, and fails that were locked in as a result
215220
// of the remote peer sending us a revocation. Each package is identified by
216221
// the short chanid and remote commitment height corresponding to the revocation

htlcswitch/link.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3297,7 +3297,7 @@ func (l *channelLink) processRemoteSettleFails(fwdPkg *channeldb.FwdPkg,
32973297
return
32983298
}
32993299

3300-
l.log.Debugf("settle-fail-filter %v", fwdPkg.SettleFailFilter)
3300+
l.log.Debugf("settle-fail-filter: %v", fwdPkg.SettleFailFilter)
33013301

33023302
var switchPackets []*htlcPacket
33033303
for i, pd := range settleFails {

htlcswitch/switch.go

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,7 @@ func (s *Switch) handleLocalResponse(pkt *htlcPacket) {
967967
// This can only happen if the circuit is still open, which is why this
968968
// ordering is chosen.
969969
if err := s.teardownCircuit(pkt); err != nil {
970-
log.Warnf("Unable to teardown circuit %s: %v",
970+
log.Errorf("Unable to teardown circuit %s: %v",
971971
pkt.inKey(), err)
972972
return
973973
}
@@ -1359,48 +1359,35 @@ func (s *Switch) teardownCircuit(pkt *htlcPacket) error {
13591359
case *lnwire.UpdateFailHTLC:
13601360
pktType = "FAIL"
13611361
default:
1362-
err := fmt.Errorf("cannot tear down packet of type: %T", htlc)
1363-
log.Errorf(err.Error())
1364-
return err
1362+
return fmt.Errorf("cannot tear down packet of type: %T", htlc)
13651363
}
13661364

1367-
switch {
1368-
case pkt.circuit.HasKeystone():
1369-
log.Debugf("Tearing down open circuit with %s pkt, removing circuit=%v "+
1370-
"with keystone=%v", pktType, pkt.inKey(), pkt.outKey())
1365+
var paymentHash lntypes.Hash
13711366

1372-
err := s.circuits.DeleteCircuits(pkt.inKey())
1373-
if err != nil {
1374-
log.Warnf("Failed to tear down open circuit (%s, %d) <-> (%s, %d) "+
1375-
"with payment_hash-%v using %s pkt",
1376-
pkt.incomingChanID, pkt.incomingHTLCID,
1377-
pkt.outgoingChanID, pkt.outgoingHTLCID,
1378-
pkt.circuit.PaymentHash, pktType)
1379-
return err
1380-
}
1381-
1382-
log.Debugf("Closed completed %s circuit for %x: "+
1383-
"(%s, %d) <-> (%s, %d)", pktType, pkt.circuit.PaymentHash,
1384-
pkt.incomingChanID, pkt.incomingHTLCID,
1385-
pkt.outgoingChanID, pkt.outgoingHTLCID)
1367+
// Perform a defensive check to make sure we don't try to access a nil
1368+
// circuit.
1369+
circuit := pkt.circuit
1370+
if circuit != nil {
1371+
copy(paymentHash[:], circuit.PaymentHash[:])
1372+
}
13861373

1387-
default:
1388-
log.Debugf("Tearing down incomplete circuit with %s for inkey=%v",
1389-
pktType, pkt.inKey())
1374+
log.Debugf("Tearing down circuit with %s pkt, removing circuit=%v "+
1375+
"with keystone=%v", pktType, pkt.inKey(), pkt.outKey())
13901376

1391-
err := s.circuits.DeleteCircuits(pkt.inKey())
1392-
if err != nil {
1393-
log.Warnf("Failed to tear down pending %s circuit for %x: "+
1394-
"(%s, %d)", pktType, pkt.circuit.PaymentHash,
1395-
pkt.incomingChanID, pkt.incomingHTLCID)
1396-
return err
1397-
}
1377+
err := s.circuits.DeleteCircuits(pkt.inKey())
1378+
if err != nil {
1379+
log.Warnf("Failed to tear down circuit (%s, %d) <-> (%s, %d) "+
1380+
"with payment_hash=%v using %s pkt", pkt.incomingChanID,
1381+
pkt.incomingHTLCID, pkt.outgoingChanID,
1382+
pkt.outgoingHTLCID, pkt.circuit.PaymentHash, pktType)
13981383

1399-
log.Debugf("Removed pending onion circuit for %x: "+
1400-
"(%s, %d)", pkt.circuit.PaymentHash,
1401-
pkt.incomingChanID, pkt.incomingHTLCID)
1384+
return err
14021385
}
14031386

1387+
log.Debugf("Closed %s circuit for %v: (%s, %d) <-> (%s, %d)", pktType,
1388+
paymentHash, pkt.incomingChanID, pkt.incomingHTLCID,
1389+
pkt.outgoingChanID, pkt.outgoingHTLCID)
1390+
14041391
return nil
14051392
}
14061393

0 commit comments

Comments
 (0)