From 9390d3bbfd2db5706b8a383a06bacf51526f6ed2 Mon Sep 17 00:00:00 2001 From: carla Date: Thu, 6 Feb 2020 19:35:16 +0200 Subject: [PATCH] htlcswitch: replace outgoing failure with interface Add a FailureDetail interface which allows us have different kinds of failures for link errors. This interface will be used to cover failures that occur when on invoice payment, because the errors have already been enumerated in the invoices package. --- htlcswitch/failure.go | 14 +++++++------- htlcswitch/failure_detail.go | 14 ++++++++++++-- htlcswitch/switch.go | 7 ++++--- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/htlcswitch/failure.go b/htlcswitch/failure.go index c4ef73c5c..9734a2e02 100644 --- a/htlcswitch/failure.go +++ b/htlcswitch/failure.go @@ -34,8 +34,8 @@ type LinkError struct { // node. msg lnwire.FailureMessage - // OutgoingFailure enriches the wire error with additional information. - OutgoingFailure + // FailureDetail enriches the wire error with additional information. + FailureDetail } // NewLinkError returns a LinkError with the failure message provided. @@ -48,11 +48,11 @@ func NewLinkError(msg lnwire.FailureMessage) *LinkError { // NewDetailedLinkError returns a link error that enriches a wire message with // a failure detail. func NewDetailedLinkError(msg lnwire.FailureMessage, - detail OutgoingFailure) *LinkError { + detail FailureDetail) *LinkError { return &LinkError{ - msg: msg, - OutgoingFailure: detail, + msg: msg, + FailureDetail: detail, } } @@ -72,11 +72,11 @@ func (l *LinkError) WireMessage() lnwire.FailureMessage { func (l *LinkError) Error() string { // If the link error has no failure detail, return the wire message's // error. - if l.OutgoingFailure == OutgoingFailureNone { + if l.FailureDetail == nil { return l.msg.Error() } - return fmt.Sprintf("%v: %v", l.msg.Error(), l.OutgoingFailure) + return fmt.Sprintf("%v: %v", l.msg.Error(), l.FailureDetail) } // ForwardingError wraps an lnwire.FailureMessage in a struct that also diff --git a/htlcswitch/failure_detail.go b/htlcswitch/failure_detail.go index 95f43f118..9c068cebf 100644 --- a/htlcswitch/failure_detail.go +++ b/htlcswitch/failure_detail.go @@ -1,5 +1,13 @@ package htlcswitch +// FailureDetail is an interface implemented by failures that occur on +// our incoming or outgoing link, or within the switch itself. +type FailureDetail interface { + // FailureString returns the string representation of a failure + // detail. + FailureString() string +} + // OutgoingFailure is an enum which is used to enrich failures which occur in // the switch or on our outgoing link with additional metadata. type OutgoingFailure int @@ -36,8 +44,10 @@ const ( OutgoingFailureCircularRoute ) -// String returns the string representation of a failure detail. -func (fd OutgoingFailure) String() string { +// FailureString returns the string representation of a failure detail. +// +// Note: it is part of the FailureDetail interface. +func (fd OutgoingFailure) FailureString() string { switch fd { case OutgoingFailureNone: return "no failure detail" diff --git a/htlcswitch/switch.go b/htlcswitch/switch.go index 5db9dac55..7ca56223a 100644 --- a/htlcswitch/switch.go +++ b/htlcswitch/switch.go @@ -923,8 +923,8 @@ func (s *Switch) parseFailedPayment(deobfuscator ErrorDecrypter, ) log.Errorf("%v: (hash=%v, pid=%d): %v", - linkError.OutgoingFailure, paymentHash, paymentID, - err) + linkError.FailureDetail.FailureString(), + paymentHash, paymentID, err) return linkError } @@ -942,7 +942,8 @@ func (s *Switch) parseFailedPayment(deobfuscator ErrorDecrypter, OutgoingFailureOnChainTimeout, ) - log.Info("%v: hash=%v, pid=%d", linkError.OutgoingFailure, + log.Info("%v: hash=%v, pid=%d", + linkError.FailureDetail.FailureString(), paymentHash, paymentID) return linkError