Skip to content

Add Payment Hash to a subset of htlc events #7309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/release-notes/release-notes-0.16.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Release Notes

## Misc

- [Add the payment hash to a subset of
htlc events](https://github.com/lightningnetwork/lnd/pull/7309)

# Contributors (Alphabetical Order)

- ziggie1984
39 changes: 34 additions & 5 deletions htlcswitch/htlcnotifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (h *HtlcNotifier) SubscribeHtlcEvents() (*subscribe.Client, error) {

// HtlcKey uniquely identifies the htlc.
type HtlcKey struct {
// IncomingCircuit is the channel an htlc id of the incoming htlc.
// IncomingCircuit is the channel and htlc id of the incoming htlc.
IncomingCircuit models.CircuitKey

// OutgoingCircuit is the channel and htlc id of the outgoing htlc.
Expand Down Expand Up @@ -226,6 +226,9 @@ type ForwardingEvent struct {

// Timestamp is the time when this htlc was forwarded.
Timestamp time.Time

// Payment hash this htlc locks funds to.
PaymentHash lntypes.Hash
}

// LinkFailEvent describes a htlc that failed on our incoming or outgoing
Expand Down Expand Up @@ -254,6 +257,9 @@ type LinkFailEvent struct {

// Timestamp is the time when the link failure occurred.
Timestamp time.Time

// Payment hash this htlc locks funds to.
PaymentHash lntypes.Hash
}

// ForwardingFailEvent represents a htlc failure which occurred down the line
Expand All @@ -273,6 +279,9 @@ type ForwardingFailEvent struct {

// Timestamp is the time when the forwarding failure was received.
Timestamp time.Time

// Payment hash this htlc locks funds to.
PaymentHash lntypes.Hash
}

// SettleEvent represents a htlc that was settled. HtlcInfo is not reliably
Expand All @@ -293,6 +302,9 @@ type SettleEvent struct {

// Timestamp is the time when this htlc was settled.
Timestamp time.Time

// Payment hash this htlc locks funds to.
PaymentHash lntypes.Hash
}

type FinalHtlcEvent struct {
Expand All @@ -312,13 +324,14 @@ type FinalHtlcEvent struct {
//
// Note this is part of the htlcNotifier interface.
func (h *HtlcNotifier) NotifyForwardingEvent(key HtlcKey, info HtlcInfo,
eventType HtlcEventType) {
eventType HtlcEventType, paymentHash lntypes.Hash) {

event := &ForwardingEvent{
HtlcKey: key,
HtlcInfo: info,
HtlcEventType: eventType,
Timestamp: h.now(),
PaymentHash: paymentHash,
}

log.Tracef("Notifying forward event: %v over %v, %v", eventType, key,
Expand All @@ -334,7 +347,8 @@ func (h *HtlcNotifier) NotifyForwardingEvent(key HtlcKey, info HtlcInfo,
//
// Note this is part of the htlcNotifier interface.
func (h *HtlcNotifier) NotifyLinkFailEvent(key HtlcKey, info HtlcInfo,
eventType HtlcEventType, linkErr *LinkError, incoming bool) {
eventType HtlcEventType, linkErr *LinkError,
incoming bool, paymentHash lntypes.Hash) {

event := &LinkFailEvent{
HtlcKey: key,
Expand All @@ -343,6 +357,7 @@ func (h *HtlcNotifier) NotifyLinkFailEvent(key HtlcKey, info HtlcInfo,
LinkError: linkErr,
Incoming: incoming,
Timestamp: h.now(),
PaymentHash: paymentHash,
}

log.Tracef("Notifying link failure event: %v over %v, %v", eventType,
Expand All @@ -358,12 +373,13 @@ func (h *HtlcNotifier) NotifyLinkFailEvent(key HtlcKey, info HtlcInfo,
//
// Note this is part of the htlcNotifier interface.
func (h *HtlcNotifier) NotifyForwardingFailEvent(key HtlcKey,
eventType HtlcEventType) {
eventType HtlcEventType, paymentHash lntypes.Hash) {

event := &ForwardingFailEvent{
HtlcKey: key,
HtlcEventType: eventType,
Timestamp: h.now(),
PaymentHash: paymentHash,
}

log.Tracef("Notifying forwarding failure event: %v over %v", eventType,
Expand All @@ -379,13 +395,15 @@ func (h *HtlcNotifier) NotifyForwardingFailEvent(key HtlcKey,
//
// Note this is part of the htlcNotifier interface.
func (h *HtlcNotifier) NotifySettleEvent(key HtlcKey,
preimage lntypes.Preimage, eventType HtlcEventType) {
preimage lntypes.Preimage, eventType HtlcEventType,
paymentHash lntypes.Hash) {

event := &SettleEvent{
HtlcKey: key,
Preimage: preimage,
HtlcEventType: eventType,
Timestamp: h.now(),
PaymentHash: paymentHash,
}

log.Tracef("Notifying settle event: %v over %v", eventType, key)
Expand Down Expand Up @@ -471,3 +489,14 @@ func getEventType(pkt *htlcPacket) HtlcEventType {
return HtlcEventTypeForward
}
}

// newPayHash returns the payment hash for the package provided.
func newPayHash(pkt *htlcPacket) lntypes.Hash {
// In case the circuit is nil a zero payment hash is returned to satisfy
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to add the zero payment hash here, because some unit tests failed because the circuit was sometimes nil.

// the htlc notifier api.
if pkt.circuit == nil {
return lntypes.Hash{}
}

return pkt.circuit.PaymentHash
}
10 changes: 6 additions & 4 deletions htlcswitch/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,23 +354,25 @@ type htlcNotifier interface {
// NotifyForwardingEvent notifies the HtlcNotifier than a htlc has been
// forwarded.
NotifyForwardingEvent(key HtlcKey, info HtlcInfo,
eventType HtlcEventType)
eventType HtlcEventType, paymentHash lntypes.Hash)

// NotifyIncomingLinkFailEvent notifies that a htlc has failed on our
// incoming link. It takes an isReceive bool to differentiate between
// our node's receives and forwards.
NotifyLinkFailEvent(key HtlcKey, info HtlcInfo,
eventType HtlcEventType, linkErr *LinkError, incoming bool)
eventType HtlcEventType, linkErr *LinkError, incoming bool,
paymentHash lntypes.Hash)

// NotifyForwardingFailEvent notifies the HtlcNotifier that a htlc we
// forwarded has failed down the line.
NotifyForwardingFailEvent(key HtlcKey, eventType HtlcEventType)
NotifyForwardingFailEvent(key HtlcKey, eventType HtlcEventType,
paymentHash lntypes.Hash)

// NotifySettleEvent notifies the HtlcNotifier that a htlc that we
// committed to as part of a forward or a receive to our node has been
// settled.
NotifySettleEvent(key HtlcKey, preimage lntypes.Preimage,
eventType HtlcEventType)
eventType HtlcEventType, paymentHash lntypes.Hash)

// NotifyFinalHtlcEvent notifies the HtlcNotifier that the final outcome
// for an htlc has been determined.
Expand Down
9 changes: 8 additions & 1 deletion htlcswitch/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -1490,6 +1490,7 @@ func (l *channelLink) handleDownstreamUpdateAdd(pkt *htlcPacket) error {
OutgoingAmt: htlc.Amount,
},
getEventType(pkt),
htlc.PaymentHash,
)

l.tryBatchUpdateCommitTx()
Expand Down Expand Up @@ -1572,6 +1573,7 @@ func (l *channelLink) handleDownstreamPkt(pkt *htlcPacket) {
newHtlcKey(pkt),
htlc.PaymentPreimage,
getEventType(pkt),
newPayHash(pkt),
)

// Immediately update the commitment tx to minimize latency.
Expand Down Expand Up @@ -1645,10 +1647,13 @@ func (l *channelLink) handleDownstreamPkt(pkt *htlcPacket) {
getEventType(pkt),
pkt.linkFailure,
false,
newPayHash(pkt),
)
} else {
l.cfg.HtlcNotifier.NotifyForwardingFailEvent(
newHtlcKey(pkt), getEventType(pkt),
newHtlcKey(pkt),
getEventType(pkt),
newPayHash(pkt),
)
}

Expand Down Expand Up @@ -3339,6 +3344,7 @@ func (l *channelLink) settleHTLC(preimage lntypes.Preimage,
},
preimage,
HtlcEventTypeReceive,
lntypes.Hash(pd.RHash),
)

return nil
Expand Down Expand Up @@ -3414,6 +3420,7 @@ func (l *channelLink) sendHTLCError(pd *lnwallet.PaymentDescriptor,
eventType,
failure,
true,
lntypes.Hash(pd.RHash),
)
}

Expand Down
9 changes: 5 additions & 4 deletions htlcswitch/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -1106,20 +1106,21 @@ type mockHTLCNotifier struct {
}

func (h *mockHTLCNotifier) NotifyForwardingEvent(key HtlcKey, info HtlcInfo,
eventType HtlcEventType) { //nolint:whitespace
eventType HtlcEventType, paymentHash lntypes.Hash) { //nolint:whitespace
}

func (h *mockHTLCNotifier) NotifyLinkFailEvent(key HtlcKey, info HtlcInfo,
eventType HtlcEventType, linkErr *LinkError,
incoming bool) { //nolint:whitespace
incoming bool, paymentHash lntypes.Hash) { //nolint:whitespace
}

func (h *mockHTLCNotifier) NotifyForwardingFailEvent(key HtlcKey,
eventType HtlcEventType) { //nolint:whitespace
eventType HtlcEventType, paymentHash lntypes.Hash) { //nolint:whitespace
}

func (h *mockHTLCNotifier) NotifySettleEvent(key HtlcKey,
preimage lntypes.Preimage, eventType HtlcEventType) { //nolint:whitespace,lll
preimage lntypes.Preimage, eventType HtlcEventType,
paymentHash lntypes.Hash) { //nolint:whitespace
}

func (h *mockHTLCNotifier) NotifyFinalHtlcEvent(key models.CircuitKey,
Expand Down
7 changes: 5 additions & 2 deletions htlcswitch/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ func (s *Switch) SendHTLC(firstHop lnwire.ShortChannelID, attemptID uint64,
HtlcEventTypeSend,
linkErr,
false,
htlc.PaymentHash,
)

return linkErr
Expand All @@ -576,6 +577,7 @@ func (s *Switch) SendHTLC(firstHop lnwire.ShortChannelID, attemptID uint64,
HtlcEventTypeSend,
linkErr,
false,
htlc.PaymentHash,
)

return errDustThresholdExceeded
Expand Down Expand Up @@ -976,10 +978,11 @@ func (s *Switch) handleLocalResponse(pkt *htlcPacket) {
switch htlc := pkt.htlc.(type) {
case *lnwire.UpdateFulfillHTLC:
s.cfg.HtlcNotifier.NotifySettleEvent(key, htlc.PaymentPreimage,
eventType)
eventType, pkt.circuit.PaymentHash)

case *lnwire.UpdateFailHTLC:
s.cfg.HtlcNotifier.NotifyForwardingFailEvent(key, eventType)
s.cfg.HtlcNotifier.NotifyForwardingFailEvent(key, eventType,
pkt.circuit.PaymentHash)
}
}

Expand Down
7 changes: 7 additions & 0 deletions htlcswitch/switch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3603,6 +3603,7 @@ func getThreeHopEvents(channels *clusterChannels, htlcID uint64,
},
HtlcEventType: HtlcEventTypeSend,
Timestamp: ts,
PaymentHash: htlc.PaymentHash,
},
}

Expand Down Expand Up @@ -3632,6 +3633,7 @@ func getThreeHopEvents(channels *clusterChannels, htlcID uint64,
HtlcKey: aliceKey,
HtlcEventType: HtlcEventTypeSend,
Timestamp: ts,
PaymentHash: htlc.PaymentHash,
},
)

Expand All @@ -3643,6 +3645,7 @@ func getThreeHopEvents(channels *clusterChannels, htlcID uint64,
LinkError: linkError,
Incoming: false,
Timestamp: ts,
PaymentHash: htlc.PaymentHash,
},
&FinalHtlcEvent{
CircuitKey: bobKey.IncomingCircuit,
Expand All @@ -3665,6 +3668,7 @@ func getThreeHopEvents(channels *clusterChannels, htlcID uint64,
Preimage: *preimage,
HtlcEventType: HtlcEventTypeSend,
Timestamp: ts,
PaymentHash: htlc.PaymentHash,
},
)

Expand All @@ -3674,12 +3678,14 @@ func getThreeHopEvents(channels *clusterChannels, htlcID uint64,
HtlcInfo: bobInfo,
HtlcEventType: HtlcEventTypeForward,
Timestamp: ts,
PaymentHash: htlc.PaymentHash,
},
&SettleEvent{
HtlcKey: bobKey,
Preimage: *preimage,
HtlcEventType: HtlcEventTypeForward,
Timestamp: ts,
PaymentHash: htlc.PaymentHash,
},
&FinalHtlcEvent{
CircuitKey: bobKey.IncomingCircuit,
Expand All @@ -3701,6 +3707,7 @@ func getThreeHopEvents(channels *clusterChannels, htlcID uint64,
Preimage: *preimage,
HtlcEventType: HtlcEventTypeReceive,
Timestamp: ts,
PaymentHash: htlc.PaymentHash,
}, &FinalHtlcEvent{
CircuitKey: models.CircuitKey{
ChanID: channels.carolToBob.ShortChanID(),
Expand Down
Loading