Skip to content

Commit

Permalink
Merge bca08f2 into 38b521d
Browse files Browse the repository at this point in the history
  • Loading branch information
carlaKC committed Feb 24, 2020
2 parents 38b521d + bca08f2 commit e294562
Show file tree
Hide file tree
Showing 9 changed files with 1,282 additions and 163 deletions.
114 changes: 103 additions & 11 deletions htlcswitch/htlcnotifier.go
Expand Up @@ -219,8 +219,31 @@ type ForwardingEvent struct {
// receive, or as part of a forward.
HtlcEventType

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

// Timestamp returns the time that the event occurred.
//
// Note: part of the HtlcEvent interface.
func (e *ForwardingEvent) Timestamp() time.Time {
return e.timestamp
}

// Key returns the htlc key which identifies the htlc that
// the event is associated with.
//
// Note: part of the HtlcEvent interface.
func (e *ForwardingEvent) Key() HtlcKey {
return e.HtlcKey
}

// EventType returns the type of event the htlc is associated with,
// a local send or receive, or a forward.
//
// Note: part of the HtlcEvent interface.
func (e *ForwardingEvent) EventType() HtlcEventType {
return e.HtlcEventType
}

// LinkFailEvent describes a htlc that failed on our incoming or outgoing
Expand Down Expand Up @@ -248,7 +271,30 @@ type LinkFailEvent struct {
Incoming bool

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

// Timestamp returns the time that the event occurred.
//
// Note: part of the HtlcEvent interface.
func (e *LinkFailEvent) Timestamp() time.Time {
return e.timestamp
}

// Key returns the htlc key which identifies the htlc that
// the event is associated with.
//
// Note: part of the HtlcEvent interface.
func (e *LinkFailEvent) Key() HtlcKey {
return e.HtlcKey
}

// EventType returns the type of event the htlc is associated with,
// a local send or receive, or a forward.
//
// Note: part of the HtlcEvent interface.
func (e *LinkFailEvent) EventType() HtlcEventType {
return e.HtlcEventType
}

// ForwardingFailEvent represents a htlc failure which occurred down the line
Expand All @@ -266,8 +312,31 @@ type ForwardingFailEvent struct {
// receive, or as part of a forward.
HtlcEventType

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

// Timestamp returns the time that the event occurred.
//
// Note: part of the HtlcEvent interface.
func (e *ForwardingFailEvent) Timestamp() time.Time {
return e.timestamp
}

// Key returns the htlc key which identifies the htlc that
// the event is associated with.
//
// Note: part of the HtlcEvent interface.
func (e *ForwardingFailEvent) Key() HtlcKey {
return e.HtlcKey
}

// EventType returns the type of event the htlc is associated with,
// a local send or receive, or a forward.
//
// Note: part of the HtlcEvent interface.
func (e *ForwardingFailEvent) EventType() HtlcEventType {
return e.HtlcEventType
}

// SettleEvent represents a htlc that was settled. HtlcInfo is not reliably
Expand All @@ -283,8 +352,31 @@ type SettleEvent struct {
// receive, or as part of a forward.
HtlcEventType

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

// Timestamp returns the time that the event occurred.
//
// Note: part of the HtlcEvent interface.
func (e *SettleEvent) Timestamp() time.Time {
return e.timestamp
}

// Key returns the htlc key which identifies the htlc that
// the event is associated with.
//
// Note: part of the HtlcEvent interface.
func (e *SettleEvent) Key() HtlcKey {
return e.HtlcKey
}

// EventType returns the type of event the htlc is associated with,
// a local send or receive, or a forward.
//
// Note: part of the HtlcEvent interface.
func (e *SettleEvent) EventType() HtlcEventType {
return e.HtlcEventType
}

// NotifyForwardingEvent notifies the HtlcNotifier than a htlc has been
Expand All @@ -298,7 +390,7 @@ func (h *HtlcNotifier) NotifyForwardingEvent(key HtlcKey, info HtlcInfo,
HtlcKey: key,
HtlcInfo: info,
HtlcEventType: eventType,
Timestamp: h.now(),
timestamp: h.now(),
}

log.Tracef("Notifying forward event: %v over %v, %v", eventType, key,
Expand All @@ -322,7 +414,7 @@ func (h *HtlcNotifier) NotifyLinkFailEvent(key HtlcKey, info HtlcInfo,
HtlcEventType: eventType,
LinkError: linkErr,
Incoming: incoming,
Timestamp: h.now(),
timestamp: h.now(),
}

log.Tracef("Notifying link failure event: %v over %v, %v", eventType,
Expand All @@ -343,7 +435,7 @@ func (h *HtlcNotifier) NotifyForwardingFailEvent(key HtlcKey,
event := &ForwardingFailEvent{
HtlcKey: key,
HtlcEventType: eventType,
Timestamp: h.now(),
timestamp: h.now(),
}

log.Tracef("Notifying forwarding failure event: %v over %v", eventType,
Expand All @@ -362,7 +454,7 @@ func (h *HtlcNotifier) NotifySettleEvent(key HtlcKey, eventType HtlcEventType) {
event := &SettleEvent{
HtlcKey: key,
HtlcEventType: eventType,
Timestamp: h.now(),
timestamp: h.now(),
}

log.Tracef("Notifying settle event: %v over %v", eventType, key)
Expand Down
16 changes: 16 additions & 0 deletions htlcswitch/interfaces.go
@@ -1,6 +1,8 @@
package htlcswitch

import (
"time"

"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/invoices"
Expand Down Expand Up @@ -206,3 +208,17 @@ type htlcNotifier interface {
// settled.
NotifySettleEvent(key HtlcKey, eventType HtlcEventType)
}

// HtlcEvent is an interface that is implemented by all htlc events.
type HtlcEvent interface {
// Key returns the htlc key which identifies the htlc that
// the event is associated with.
Key() HtlcKey

// Timestamp returns the time that the event occurred.
Timestamp() time.Time

// EventType returns the type of event the htlc is associated with,
// a local send or receive, or a forward.
EventType() HtlcEventType
}
36 changes: 18 additions & 18 deletions htlcswitch/switch_test.go
Expand Up @@ -2708,7 +2708,7 @@ func TestInvalidFailure(t *testing.T) {
// these tests.
type htlcNotifierEvents func(channels *clusterChannels, htlcID uint64,
ts time.Time, htlc *lnwire.UpdateAddHTLC,
hops []*hop.Payload) ([]interface{}, []interface{}, []interface{})
hops []*hop.Payload) ([]HtlcEvent, []HtlcEvent, []HtlcEvent)

// TestHtlcNotifier tests the notifying of htlc events that are routed over a
// three hop network. It sets up an Alice -> Bob -> Carol network and routes
Expand Down Expand Up @@ -2739,8 +2739,8 @@ func TestHtlcNotifier(t *testing.T) {
expectedEvents: func(channels *clusterChannels,
htlcID uint64, ts time.Time,
htlc *lnwire.UpdateAddHTLC,
hops []*hop.Payload) ([]interface{},
[]interface{}, []interface{}) {
hops []*hop.Payload) ([]HtlcEvent,
[]HtlcEvent, []HtlcEvent) {

return getThreeHopEvents(
channels, htlcID, ts, htlc, hops, nil,
Expand All @@ -2758,8 +2758,8 @@ func TestHtlcNotifier(t *testing.T) {
expectedEvents: func(channels *clusterChannels,
htlcID uint64, ts time.Time,
htlc *lnwire.UpdateAddHTLC,
hops []*hop.Payload) ([]interface{},
[]interface{}, []interface{}) {
hops []*hop.Payload) ([]HtlcEvent,
[]HtlcEvent, []HtlcEvent) {

return getThreeHopEvents(
channels, htlcID, ts, htlc, hops,
Expand Down Expand Up @@ -2893,7 +2893,7 @@ func testHtcNotifier(t *testing.T, testOpts []serverOption, iterations int,
// checkHtlcEvents checks that a subscription has the set of htlc events
// we expect it to have.
func checkHtlcEvents(t *testing.T, events <-chan interface{},
expectedEvents []interface{}) {
expectedEvents []HtlcEvent) {

for _, expected := range expectedEvents {
select {
Expand Down Expand Up @@ -2949,7 +2949,7 @@ func (n *threeHopNetwork) sendThreeHopPayment(t *testing.T) (*lnwire.UpdateAddHT
// of events will fail on Bob's outgoing link.
func getThreeHopEvents(channels *clusterChannels, htlcID uint64,
ts time.Time, htlc *lnwire.UpdateAddHTLC, hops []*hop.Payload,
linkError *LinkError) ([]interface{}, []interface{}, []interface{}) {
linkError *LinkError) ([]HtlcEvent, []HtlcEvent, []HtlcEvent) {

aliceKey := HtlcKey{
IncomingCircuit: zeroCircuit,
Expand All @@ -2961,15 +2961,15 @@ func getThreeHopEvents(channels *clusterChannels, htlcID uint64,

// Alice always needs a forwarding event because she initiates the
// send.
aliceEvents := []interface{}{
aliceEvents := []HtlcEvent{
&ForwardingEvent{
HtlcKey: aliceKey,
HtlcInfo: HtlcInfo{
OutgoingTimeLock: htlc.Expiry,
OutgoingAmt: htlc.Amount,
},
HtlcEventType: HtlcEventTypeSend,
Timestamp: ts,
timestamp: ts,
},
}

Expand Down Expand Up @@ -2998,18 +2998,18 @@ func getThreeHopEvents(channels *clusterChannels, htlcID uint64,
&ForwardingFailEvent{
HtlcKey: aliceKey,
HtlcEventType: HtlcEventTypeSend,
Timestamp: ts,
timestamp: ts,
},
)

bobEvents := []interface{}{
bobEvents := []HtlcEvent{
&LinkFailEvent{
HtlcKey: bobKey,
HtlcInfo: bobInfo,
HtlcEventType: HtlcEventTypeForward,
LinkError: linkError,
Incoming: false,
Timestamp: ts,
timestamp: ts,
},
}

Expand All @@ -3024,25 +3024,25 @@ func getThreeHopEvents(channels *clusterChannels, htlcID uint64,
&SettleEvent{
HtlcKey: aliceKey,
HtlcEventType: HtlcEventTypeSend,
Timestamp: ts,
timestamp: ts,
},
)

bobEvents := []interface{}{
bobEvents := []HtlcEvent{
&ForwardingEvent{
HtlcKey: bobKey,
HtlcInfo: bobInfo,
HtlcEventType: HtlcEventTypeForward,
Timestamp: ts,
timestamp: ts,
},
&SettleEvent{
HtlcKey: bobKey,
HtlcEventType: HtlcEventTypeForward,
Timestamp: ts,
timestamp: ts,
},
}

carolEvents := []interface{}{
carolEvents := []HtlcEvent{
&SettleEvent{
HtlcKey: HtlcKey{
IncomingCircuit: channeldb.CircuitKey{
Expand All @@ -3052,7 +3052,7 @@ func getThreeHopEvents(channels *clusterChannels, htlcID uint64,
OutgoingCircuit: zeroCircuit,
},
HtlcEventType: HtlcEventTypeReceive,
Timestamp: ts,
timestamp: ts,
},
}

Expand Down

0 comments on commit e294562

Please sign in to comment.